su_win32_port.c 16.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@ingroup su_wait
26
 * @CFILE su_win32_port.c
27
 *
28
 * Port implementation using WSAEVENTs. Incomplete.
29 30 31 32
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Kai Vehmanen <kai.vehmanen@nokia.com>
 *
33 34
 * @date Created: Mon Feb  5 20:29:21 2007 ppessi
 * @date Original: Tue Sep 14 15:51:04 1999 ppessi
35 36 37 38
 */

#include "config.h"

39 40 41 42 43
#define su_port_s su_wsaevent_port_s

#include "sofia-sip/su.h"
#include "su_port.h"
#include "sofia-sip/su_alloc.h"
44 45 46 47 48 49 50 51 52

#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>

53
/** Port based on su_wait() aka WSAWaitForMultipleEvents. */
54

55 56
#define INDEX_MAX (64)

57
struct su_wsaevent_port_s {
58
  su_socket_port_t sup_base[1];
59

60
#define sup_home sup_base->sup_base->sup_base->sup_home
61 62 63

  unsigned         sup_multishot; /**< Multishot operation? */

64 65
  unsigned         sup_registers; /** Counter incremented by
				      su_port_register() or
66 67 68 69 70 71 72
				      su_port_unregister()
				   */

  int              sup_n_waits; /**< Active su_wait_t in su_waits */
  int              sup_size_waits; /**< Size of allocated su_waits */
  int              sup_pri_offset; /**< Offset to prioritized waits */

73
  /** Indices from index returned by su_root_register() to tables below.
74 75 76 77
   *
   * Free elements are negative. Free elements form a list, value of free
   * element is (0 - index of next free element).
   *
78
   * First element sup_indices[0] points to first free element.
79 80 81 82
   */
  int             *sup_indices;

  int             *sup_reverses; /** Reverse index */
83 84 85 86
  su_wakeup_f     *sup_wait_cbs;
  su_wakeup_arg_t**sup_wait_args;
  su_root_t      **sup_wait_roots;
  su_wait_t       *sup_waits;
87 88 89

};

90
static void su_wsevent_port_decref(su_port_t *, int blocking, char const *who);
91

92
static int su_wsevent_port_register(su_port_t *self,
93 94
				 su_root_t *root,
				 su_wait_t *wait,
95 96 97
				 su_wakeup_f callback,
				 su_wakeup_arg_t *arg,
				 int priority);
98
static int su_wsevent_port_unregister(su_port_t *port,
99 100 101
				   su_root_t *root,
				   su_wait_t *wait,
				   su_wakeup_f callback,
102
				   su_wakeup_arg_t *arg);
103 104
static int su_wsevent_port_deregister(su_port_t *self, int i);
static int su_wsevent_port_unregister_all(su_port_t *self, su_root_t *root);
105
static int su_wsevent_port_eventmask(su_port_t *self,
106 107 108
				  int index,
				  int socket,
				  int events);
109 110 111
static int su_wsevent_port_multishot(su_port_t *self, int multishot);
static int su_wsevent_port_wait_events(su_port_t *self, su_duration_t tout);
static char const *su_wsevent_port_name(su_port_t const *self);
112

113
su_port_vtable_t const su_wsevent_port_vtable[1] =
114
  {{
115
      /* su_vtable_size: */ sizeof su_wsevent_port_vtable,
116 117 118
      su_pthread_port_lock,
      su_pthread_port_unlock,
      su_base_port_incref,
119
      su_wsevent_port_decref,
120
      su_base_port_gsource,
121
      su_socket_port_send,
122 123 124 125 126
      su_wsevent_port_register,
      su_wsevent_port_unregister,
      su_wsevent_port_deregister,
      su_wsevent_port_unregister_all,
      su_wsevent_port_eventmask,
127 128 129
      su_base_port_run,
      su_base_port_break,
      su_base_port_step,
130
      su_pthread_port_thread,
131 132 133
      su_base_port_add_prepoll,
      su_base_port_remove_prepoll,
      su_base_port_timers,
134 135
      su_wsevent_port_multishot,
      su_wsevent_port_wait_events,
136 137
      su_base_port_getmsgs,
      su_base_port_getmsgs_from,
138
      su_wsevent_port_name,
139 140 141 142 143
      su_base_port_start_shared,
      su_pthread_port_wait,
      su_pthread_port_execute,
    }};

144
static char const *su_wsevent_port_name(su_port_t const *self)
145 146 147 148
{
  return "poll";
}

149
static void su_wsevent_port_deinit(void *arg)
150 151 152
{
  su_port_t *self = arg;

153
  SU_DEBUG_9(("%s(%p) called\n", "su_wsevent_port_deinit", self));
154

155
  su_socket_port_deinit(self->sup_base);
156 157
}

158
static void su_wsevent_port_decref(su_port_t *self, int blocking, char const *who)
159 160 161 162 163 164 165 166 167 168 169 170
{
  su_base_port_decref(self, blocking, who);
}

/** @internal
 *
 *  Register a @c su_wait_t object. The wait object, a callback function and
 *  an argument pointer is stored in the port object.  The callback function
 *  will be called when the wait object is signaled.
 *
 *  Please note if identical wait objects are inserted, only first one is
 *  ever signalled.
171
 *
172 173 174 175 176
 * @param self	     pointer to port
 * @param root	     pointer to root object
 * @param waits	     pointer to wait object
 * @param callback   callback function pointer
 * @param arg	     argument given to callback function when it is invoked
177
 * @param priority   relative priority of the wait object
178
 *              (0 is normal, 1 important, 2 realtime)
179
 *
180
 * @return
181
 *   Positive index of the wait object,
182 183
 *   or -1 upon an error.
 */
184
int su_wsevent_port_register(su_port_t *self,
185 186
			  su_root_t *root,
			  su_wait_t *wait,
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
			  su_wakeup_f callback,
			  su_wakeup_arg_t *arg,
			  int priority)
{
  int i, j, n;

  assert(su_port_own_thread(self));

  n = self->sup_n_waits;

  if (n >= SU_WAIT_MAX)
    return su_seterrno(ENOMEM);

  if (n >= self->sup_size_waits) {
    su_home_t *h = self->sup_home;
    /* Reallocate size arrays */
    int size;
    int *indices;
    int *reverses;
    su_wait_t *waits;
    su_wakeup_f *wait_cbs;
    su_wakeup_arg_t **wait_args;
    su_root_t **wait_tasks;

    if (self->sup_size_waits == 0)
      size = su_root_size_hint;
213
    else
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
      size = 2 * self->sup_size_waits;

    if (size < SU_WAIT_MIN)
      size = SU_WAIT_MIN;

    /* Too large */
    if (-3 - size > 0)
      return (errno = ENOMEM), -1;

    indices = su_realloc(h, self->sup_indices, (size + 1) * sizeof(*indices));
    if (indices) {
      self->sup_indices = indices;

      if (self->sup_size_waits == 0)
	indices[0] = -1;

      for (i = self->sup_size_waits + 1; i <= size; i++)
	indices[i] = -1 - i;
    }

    reverses = su_realloc(h, self->sup_reverses, size * sizeof(*waits));
    if (reverses) {
      for (i = self->sup_size_waits; i < size; i++)
	reverses[i] = -1;
      self->sup_reverses = reverses;
    }
240

241 242 243 244 245 246 247 248 249 250 251 252 253 254
    waits = su_realloc(h, self->sup_waits, size * sizeof(*waits));
    if (waits)
      self->sup_waits = waits;

    wait_cbs = su_realloc(h, self->sup_wait_cbs, size * sizeof(*wait_cbs));
    if (wait_cbs)
      self->sup_wait_cbs = wait_cbs;

    wait_args = su_realloc(h, self->sup_wait_args, size * sizeof(*wait_args));
    if (wait_args)
      self->sup_wait_args = wait_args;

    /* Add sup_wait_roots array, if needed */
    wait_tasks = su_realloc(h, self->sup_wait_roots, size * sizeof(*wait_tasks));
255
    if (wait_tasks)
256 257
      self->sup_wait_roots = wait_tasks;

258
    if (!(indices &&
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
	  reverses && waits && wait_cbs && wait_args && wait_tasks)) {
      return -1;
    }

    self->sup_size_waits = size;
  }

  i = -self->sup_indices[0]; assert(i <= self->sup_size_waits);

  if (priority > 0) {
    /* Insert */
    for (n = self->sup_n_waits; n > 0; n--) {
      j = self->sup_reverses[n-1]; assert(self->sup_indices[j] == n - 1);
      self->sup_indices[j] = n;
      self->sup_reverses[n] = j;
      self->sup_waits[n] = self->sup_waits[n-1];
      self->sup_wait_cbs[n] = self->sup_wait_cbs[n-1];
      self->sup_wait_args[n] = self->sup_wait_args[n-1];
277
      self->sup_wait_roots[n] = self->sup_wait_roots[n-1];
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    }

    self->sup_pri_offset++;
  }
  else {
    /* Append - no need to move anything */
    n = self->sup_n_waits;
  }

  self->sup_n_waits++;

  self->sup_indices[0] = self->sup_indices[i];  /* Free index */
  self->sup_indices[i] = n;

  self->sup_reverses[n] = i;
  self->sup_waits[n] = *wait;
  self->sup_wait_cbs[n] = callback;
  self->sup_wait_args[n] = arg;
  self->sup_wait_roots[n] = root;

  self->sup_registers++;

  /* Just like epoll, we return -1 or positive integer */

  return i;
}

/** Deregister a su_wait_t object. */
306
static int su_wsevent_port_deregister0(su_port_t *self, int i, int destroy_wait)
307 308 309 310 311 312 313 314 315 316
{
  int n, N, *indices, *reverses;

  indices = self->sup_indices;
  reverses = self->sup_reverses;

  n = indices[i]; assert(n >= 0);

  if (destroy_wait)
    su_wait_destroy(&self->sup_waits[n]);
317

318
  N = --self->sup_n_waits;
319

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
  if (n < self->sup_pri_offset) {
    int j = --self->sup_pri_offset;
    if (n != j) {
      assert(reverses[j] > 0);
      assert(indices[reverses[j]] == j);
      indices[reverses[j]] = n;
      reverses[n] = reverses[j];

      self->sup_waits[n] = self->sup_waits[j];
      self->sup_wait_cbs[n] = self->sup_wait_cbs[j];
      self->sup_wait_args[n] = self->sup_wait_args[j];
      self->sup_wait_roots[n] = self->sup_wait_roots[j];
      n = j;
    }
  }

  if (n < N) {
    assert(reverses[N] > 0);
    assert(indices[reverses[N]] == N);

    indices[reverses[N]] = n;
    reverses[n] = reverses[N];

    self->sup_waits[n] = self->sup_waits[N];
    self->sup_wait_cbs[n] = self->sup_wait_cbs[N];
    self->sup_wait_args[n] = self->sup_wait_args[N];
    self->sup_wait_roots[n] = self->sup_wait_roots[N];
    n = N;
  }

  reverses[n] = -1;
  memset(&self->sup_waits[n], 0, sizeof self->sup_waits[n]);
  self->sup_wait_cbs[n] = NULL;
  self->sup_wait_args[n] = NULL;
  self->sup_wait_roots[n] = NULL;
355

356 357 358 359 360 361 362 363 364 365
  indices[i] = indices[0];
  indices[0] = -i;

  self->sup_registers++;

  return i;
}


/** Unregister a su_wait_t object.
366
 *
367 368
 * Unregisters a su_wait_t object. The wait object, a callback function and
 * a argument are removed from the port object.
369
 *
370 371 372 373
 * @param self     - pointer to port object
 * @param root     - pointer to root object
 * @param wait     - pointer to wait object
 * @param callback - callback function pointer (may be NULL)
374
 * @param arg      - argument given to callback function when it is invoked
375 376
 *                   (may be NULL)
 *
377
 * @deprecated Use su_wsevent_port_deregister() instead.
378 379 380
 *
 * @return Nonzero index of the wait object, or -1 upon an error.
 */
381
int su_wsevent_port_unregister(su_port_t *self,
382 383
			    su_root_t *root,
			    su_wait_t *wait,
384 385 386 387 388 389 390 391 392 393 394 395
			    su_wakeup_f callback, /* XXX - ignored */
			    su_wakeup_arg_t *arg)
{
  int n, N;

  assert(self);
  assert(su_port_own_thread(self));

  N = self->sup_n_waits;

  for (n = 0; n < N; n++) {
    if (SU_WAIT_CMP(wait[0], self->sup_waits[n]) == 0) {
396
      return su_wsevent_port_deregister0(self, self->sup_reverses[n], 0);
397 398 399 400 401 402 403 404 405
    }
  }

  su_seterrno(ENOENT);

  return -1;
}

/** Deregister a su_wait_t object.
406
 *
407 408 409
 *  Deregisters a registration by index. The wait object, a callback
 *  function and a argument are removed from the port object. The wait
 *  object is destroyed.
410
 *
411 412
 * @param self     - pointer to port object
 * @param i        - registration index
413
 *
414 415
 * @return Index of the wait object, or -1 upon an error.
 */
416
int su_wsevent_port_deregister(su_port_t *self, int i)
417 418 419 420 421 422 423 424 425 426 427 428
{
  su_wait_t wait[1] = { SU_WAIT_INIT };
  int retval;

  assert(self);
  assert(su_port_own_thread(self));

  if (i <= 0 || i > self->sup_size_waits)
    return su_seterrno(EBADF);

  if (self->sup_indices[i] < 0)
    return su_seterrno(EBADF);
429

430
  retval = su_wsevent_port_deregister0(self, i, 1);
431 432 433 434 435 436 437 438 439 440

  su_wait_destroy(wait);

  return retval;
}


/** @internal
 * Unregister all su_wait_t objects.
 *
441 442
 * Unregisters all su_wait_t objects and destroys all queued timers
 * associated with given root object.
443
 *
444 445
 * @param  self     - pointer to port object
 * @param  root     - pointer to root object
446
 *
447 448
 * @return Number of wait objects removed.
 */
449
int su_wsevent_port_unregister_all(su_port_t *self,
450 451 452 453 454 455 456 457 458 459 460 461 462 463
				su_root_t *root)
{
  int i, j, index, N;
  int             *indices, *reverses;
  su_wait_t       *waits;
  su_wakeup_f     *wait_cbs;
  su_wakeup_arg_t**wait_args;
  su_root_t      **wait_roots;

  assert(su_port_own_thread(self));

  N          = self->sup_n_waits;
  indices    = self->sup_indices;
  reverses   = self->sup_reverses;
464 465
  waits      = self->sup_waits;
  wait_cbs   = self->sup_wait_cbs;
466
  wait_args  = self->sup_wait_args;
467 468
  wait_roots = self->sup_wait_roots;

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
  for (i = j = 0; i < N; i++) {
    index = reverses[i]; assert(index > 0 && indices[index] == i);

    if (wait_roots[i] == root) {
      /* XXX - we should free all resources associated with this, too */
      if (i < self->sup_pri_offset)
	self->sup_pri_offset--;

      indices[index] = indices[0];
      indices[0] = -index;
      continue;
    }

    if (i != j) {
      indices[index] = j;
      reverses[j]   = reverses[i];
      waits[j]      = waits[i];
      wait_cbs[j]   = wait_cbs[i];
      wait_args[j]  = wait_args[i];
      wait_roots[j] = wait_roots[i];
    }
490

491 492
    j++;
  }
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
  for (i = j; i < N; i++) {
    reverses[i] = -1;
    wait_cbs[i] = NULL;
    wait_args[i] = NULL;
    wait_roots[i] = NULL;
  }
  memset(&waits[j], 0, (char *)&waits[N] - (char *)&waits[j]);

  self->sup_n_waits = j;
  self->sup_registers++;

  return N - j;
}

/**Set mask for a registered event. @internal
 *
510
 * Sets the mask describing events that can signal the registered callback.
511 512 513 514 515 516 517 518 519
 *
 * @param port   pointer to port object
 * @param index  registration index
 * @param socket socket
 * @param events new event mask
 *
 * @retval 0 when successful,
 * @retval -1 upon an error.
 */
520
int su_wsevent_port_eventmask(su_port_t *self, int index, int socket, int events)
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
{
  int n;
  assert(self);
  assert(su_port_own_thread(self));

  if (index <= 0 || index > self->sup_size_waits)
    return su_seterrno(EBADF);
  n = self->sup_indices[index];
  if (n < 0)
    return su_seterrno(EBADF);

  return su_wait_mask(&self->sup_waits[n], socket, events);
}

/** @internal Enable multishot mode.
 *
 * Enables, disables or queries the multishot mode for the port. The
 * multishot mode determines how the events are scheduled by port. If
 * multishot mode is enabled, port serves all the sockets that have received
 * network events. If it is disabled, only first socket event is served.
 *
 * @param self      pointer to port object
 * @param multishot multishot mode (0 => disables, 1 => enables, -1 => query)
544
 *
545 546 547 548 549
 * @retval 0 multishot mode is disabled
 * @retval 1 multishot mode is enabled
 * @retval -1 an error occurred
 */
static
550
int su_wsevent_port_multishot(su_port_t *self, int multishot)
551 552 553 554 555
{
  if (multishot < 0)
    return self->sup_multishot;
  else if (multishot == 0 || multishot == 1)
    return self->sup_multishot = multishot;
556
  else
557 558 559 560 561 562 563 564 565 566 567 568 569
    return (errno = EINVAL), -1;
}


/** @internal
 * Wait (poll()) for wait objects in port.
 *
 * @param self     pointer to port
 * @param tout     timeout in milliseconds
 *
 * @return number of events handled
 */
static
570
int su_wsevent_port_wait_events(su_port_t *self, su_duration_t tout)
571 572 573 574 575 576 577 578 579 580 581 582
{
  int i, events = 0;
  su_wait_t *waits = self->sup_waits;
  int n = self->sup_n_waits;
  su_root_t *root;

  i = su_wait(waits, (unsigned)n, tout);

  if (i >= 0 && i < n) {
#if 0
    /* poll() can return events for multiple wait objects */
    if (self->sup_multishot) {
Michael Jerris's avatar
Michael Jerris committed
583 584
      unsigned version = self->sup_registers;

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
      for (; i < n; i++) {
        if (waits[i].revents) {
          root = self->sup_wait_roots[i];
          self->sup_wait_cbs[i](root ? su_root_magic(root) : NULL,
                                &waits[i],
                                self->sup_wait_args[i]);
          events++;
          /* Callback function used su_register()/su_deregister() */
          if (version != self->sup_registers)
            break;
        }
      }
    }
#else
    if (0) ;
#endif
    else {
      root = self->sup_wait_roots[i];
      self->sup_wait_cbs[i](root ? su_root_magic(root) : NULL,
                            &self->sup_waits[i],
                            self->sup_wait_args[i]);
      events++;
    }
  }

  return events;
}

613 614
/** Create a port using WSAEVENTs and WSAWaitForMultipleEvents. */
su_port_t *su_wsaevent_port_create(void)
615 616 617 618 619 620
{
  su_port_t *self = su_home_new(sizeof *self);

  if (!self)
    return self;

621
  if (su_home_destructor(su_port_home(self), su_wsevent_port_deinit) < 0)
622 623 624 625
    return su_home_unref(su_port_home(self)), NULL;

  self->sup_multishot = SU_ENABLE_MULTISHOT_POLL;

626
  if (su_socket_port_init(self->sup_base, su_wsevent_port_vtable) < 0)
627 628 629 630 631
    return su_home_unref(su_port_home(self)), NULL;

  return self;
}

632 633 634 635 636
int su_wsaevent_clone_start(su_root_t *parent,
			    su_clone_r return_clone,
			    su_root_magic_t *magic,
			    su_root_init_f init,
			    su_root_deinit_f deinit)
637
{
638
  return su_pthreaded_port_start(su_wsaevent_port_create,
639 640 641
				 parent, return_clone, magic, init, deinit);
}