su_osx_runloop.c 32.3 KB
Newer Older
Martti Mela's avatar
Martti Mela committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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
 *
 */

25 26 27 28 29 30 31 32 33
/**@ingroup su_wait
 * @CFILE su_osx_runloop.c
 *
 * OS-Independent Socket Syncronization Interface.
 *
 * This looks like nth reincarnation of "reactor".  It implements the
 * poll/select/WaitForMultipleObjects and message passing functionality.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
34
 * @author Martti Mela <martti.mela@nokia.com>
35 36
 *
 * @date Created: Tue Sep 14 15:51:04 1999 ppessi
Martti Mela's avatar
Martti Mela committed
37 38 39 40 41 42
 */

#include "config.h"

#include <stdlib.h>
#include <assert.h>
43
#include <stdarg.h>
Martti Mela's avatar
Martti Mela committed
44 45 46
#include <stdio.h>
#include <string.h>
#include <limits.h>
47
#include <errno.h>
Martti Mela's avatar
Martti Mela committed
48

49
#define su_port_s su_osx_port_s
Martti Mela's avatar
Martti Mela committed
50 51

#include "su_port.h"
52
#include "sofia-sip/su_osx_runloop.h"
Martti Mela's avatar
Martti Mela committed
53
#include "sofia-sip/su_alloc.h"
54 55 56 57 58 59 60 61 62 63
#include "sofia-sip/su_debug.h"

#if HAVE_FUNC
#define enter (void)SU_DEBUG_9(("%s: entering\n", __func__))
#elif HAVE_FUNCTION
#define enter (void)SU_DEBUG_9(("%s: entering\n", __FUNCTION__))
#else
#define enter (void)0
#endif

Martti Mela's avatar
Martti Mela committed
64 65
static su_port_t *su_osx_runloop_create(void) __attribute__((__malloc__));

66
/* Callback for CFObserver and CFSocket */
67 68
static void cf_observer_cb(CFRunLoopObserverRef observer,
			   CFRunLoopActivity activity,
69 70
			   void *info);

71 72 73 74
static void su_osx_port_socket_cb(CFSocketRef s,
				  CFSocketCallBackType callbackType,
				  CFDataRef address,
				  const void *data,
75 76
				  void *info);

77 78 79 80 81 82 83 84 85
static void su_osx_port_deinit(void *arg);

static void su_osx_port_decref(su_port_t *self, int blocking, char const *who)
{
  (void)su_base_port_decref(self, blocking, who);
}


static CFSocketCallBackType map_poll_event_to_cf_event(int events);
Martti Mela's avatar
Martti Mela committed
86

87
static int su_osx_port_send(su_port_t *self, su_msg_r rmsg);
Martti Mela's avatar
Martti Mela committed
88

89
static int su_osx_port_register(su_port_t *self,
90 91
			    su_root_t *root,
			    su_wait_t *wait,
Martti Mela's avatar
Martti Mela committed
92 93 94
			    su_wakeup_f callback,
			    su_wakeup_arg_t *arg,
			    int priority);
95
static int su_osx_port_unregister(su_port_t *port,
96 97 98
			      su_root_t *root,
			      su_wait_t *wait,
			      su_wakeup_f callback,
Martti Mela's avatar
Martti Mela committed
99
			      su_wakeup_arg_t *arg);
100

101
static int su_osx_port_deregister(su_port_t *self, int i);
102

103
static int su_osx_port_unregister_all(su_port_t *self,
104 105
			   su_root_t *root);

106 107 108 109
static int su_osx_port_eventmask(su_port_t *, int , int, int );
static void su_osx_port_run(su_port_t *self);
static void su_osx_port_break(su_port_t *self);
static su_duration_t su_osx_port_step(su_port_t *self, su_duration_t tout);
110

111
static int su_osx_port_multishot(su_port_t *port, int multishot);
112

113
static int su_osx_port_wait_events(su_port_t *self, su_duration_t tout);
114

115 116 117 118
static char const *su_osx_port_name(su_port_t const *self)
{
  return "CFRunLoop";
}
119

120
/*
121
 * Port is a per-thread reactor.
Martti Mela's avatar
Martti Mela committed
122
 *
123
 * Multiple root objects executed by single thread share a su_port_t object.
Martti Mela's avatar
Martti Mela committed
124
 */
125 126
struct su_osx_port_s {
  su_socket_port_t sup_socket[1];
127

128 129 130
#define sup_pthread sup_socket->sup_base
#define sup_base sup_socket->sup_base->sup_base
#define sup_home sup_socket->sup_base->sup_base->sup_home
Martti Mela's avatar
Martti Mela committed
131

132
  unsigned         sup_source_fired;
133

134 135 136
  CFRunLoopRef        sup_main_loop;
  CFRunLoopSourceRef *sup_sources;
  CFSocketRef        *sup_sockets;
137 138 139 140 141 142 143 144 145

  CFRunLoopObserverRef sup_observer;
  CFRunLoopObserverContext sup_observer_cntx[1];
  /* Struct for CFSocket callbacks; contains current CFSource index */
  struct osx_magic {
    su_port_t *o_port;
    int        o_current;
    int        o_count;
  } osx_magic[1];
146

147
  unsigned         sup_multishot; /**< Multishot operation? */
Martti Mela's avatar
Martti Mela committed
148

149 150
  unsigned         sup_registers; /** Counter incremented by
				      su_port_register() or
Martti Mela's avatar
Martti Mela committed
151
				      su_port_unregister()
152 153 154
				   */
  int              sup_n_waits; /**< Active su_wait_t in su_waits */
  int              sup_size_waits; /**< Size of allocate su_waits */
155

156
  int              sup_pri_offset; /**< Offset to prioritized waits */
157

158 159
#define INDEX_MAX (0x7fffffff)

160
  /** Indices from index returned by su_root_register() to tables below.
161 162 163 164
   *
   * Free elements are negative. Free elements form a list, value of free
   * element is (0 - index of next free element).
   *
165
   * First element sup_indices[0] points to first free element.
166 167 168 169
   */
  int             *sup_indices;

  int             *sup_reverses; /** Reverse index */
170 171 172
  su_wakeup_f     *sup_wait_cbs;
  su_wakeup_arg_t**sup_wait_args;
  su_root_t      **sup_wait_roots;
173

174
  su_wait_t       *sup_waits;
Martti Mela's avatar
Martti Mela committed
175 176 177
};


178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
su_port_vtable_t const su_osx_port_vtable[1] =
  {{
      /* su_vtable_size: */ sizeof su_osx_port_vtable,
      su_pthread_port_lock,
      su_pthread_port_unlock,
      su_base_port_incref,
      su_osx_port_decref,
      su_base_port_gsource,
      su_osx_port_send,
      su_osx_port_register,
      su_osx_port_unregister,
      su_osx_port_deregister,
      su_osx_port_unregister_all,
      su_osx_port_eventmask,
      su_osx_port_run,
      su_osx_port_break,
      su_osx_port_step,
195
      su_pthread_port_thread,
196 197 198 199 200 201 202 203 204 205 206 207 208
      su_base_port_add_prepoll,
      su_base_port_remove_prepoll,
      su_base_port_timers,
      su_osx_port_multishot,
      su_osx_port_wait_events,
      su_base_port_getmsgs,
      su_base_port_getmsgs_from,
      su_osx_port_name,
      su_base_port_start_shared,
      su_pthread_port_wait,
      su_pthread_port_execute,
    }};

209
/* XXX - mela static void su_osx_port_destroy(su_port_t *self); */
Martti Mela's avatar
Martti Mela committed
210

211 212 213 214 215 216 217
/** Create a reactor object.
 *
 * Allocate and initialize the instance of su_root_t.
 *
 * @param magic     pointer to user data
 *
 * @return A pointer to allocated su_root_t instance, NULL on error.
218 219
 *
 * @NEW_1_12_4.
220
 */
Martti Mela's avatar
Martti Mela committed
221 222 223 224 225
su_root_t *su_root_osx_runloop_create(su_root_magic_t *magic)
{
  return su_root_create_with_port(magic, su_osx_runloop_create());
}

226 227 228 229
void osx_enabler_cb(CFSocketRef s,
		    CFSocketCallBackType type,
		    CFDataRef address,
		    const void *data,
230 231 232
		    void *info)
{
  CFRunLoopRef  rl;
233
  struct osx_magic  *magic = (struct osx_magic *) info;
234 235 236
  su_port_t    *self = magic->o_port;
  su_duration_t tout = 0;
  su_time_t     now = su_now();
237

238 239
  rl = CFRunLoopGetCurrent();

240
  if (self->sup_base->sup_running) {
241

242 243
    if (self->sup_base->sup_prepoll)
      self->sup_base->sup_prepoll(self->sup_base->sup_pp_magic, self->sup_base->sup_pp_root);
244

245 246
    if (self->sup_base->sup_head)
      su_base_port_getmsgs(self);
247

248 249
    if (self->sup_base->sup_timers)
      su_timer_expire(&self->sup_base->sup_timers, &tout, now);
250
  }
251

252 253 254 255
  CFRunLoopWakeUp(rl);
}


Martti Mela's avatar
Martti Mela committed
256 257
/**@internal
 *
258
 * Allocates and initializes a message port.
Martti Mela's avatar
Martti Mela committed
259 260 261
 *
 * @return
 *   If successful a pointer to the new message port is returned, otherwise
262
 *   NULL is returned.
Martti Mela's avatar
Martti Mela committed
263 264 265
 */
su_port_t *su_osx_runloop_create(void)
{
266
  su_port_t *self = su_home_new(sizeof *self);
267

268 269
  if (!self)
    return self;
270

271
  enter;
272

273 274
  if (su_home_destructor(su_port_home(self), su_osx_port_deinit) < 0)
    return su_home_unref(su_port_home(self)), NULL;
275

276
  self->sup_multishot = SU_ENABLE_MULTISHOT_POLL;
277

278 279 280 281
  if (su_socket_port_init(self->sup_base, su_osx_port_vtable) == 0) {
    self->osx_magic->o_port = self;
    self->sup_observer_cntx->info = self->osx_magic;
    self->sup_observer =
282
      CFRunLoopObserverCreate(NULL,
283 284
			      kCFRunLoopAfterWaiting | kCFRunLoopBeforeWaiting,
			      TRUE, 0, cf_observer_cb, self->sup_observer_cntx);
285
#if 0
286
    CFRunLoopAddObserver(CFRunLoopGetCurrent(),
287
			 self->sup_observer,
288
			 kCFRunLoopDefaultMode);
289
#endif
Martti Mela's avatar
Martti Mela committed
290
  }
291 292 293
  else
    return su_home_unref(su_port_home(self)), NULL;

294
  return self;
Martti Mela's avatar
Martti Mela committed
295 296
}

297
static
298 299
void cf_observer_cb(CFRunLoopObserverRef observer,
		    CFRunLoopActivity activity,
300 301 302
		    void *info)
{
  CFRunLoopRef  rl;
303
  struct osx_magic  *magic = (struct osx_magic *) info;
304 305 306 307 308 309
  su_port_t    *self = magic->o_port;
  su_duration_t tout = 0;
  su_time_t     now = su_now();

  rl = CFRunLoopGetCurrent();

310
  if (self->sup_base->sup_running) {
311

312 313
    if (self->sup_base->sup_prepoll)
      self->sup_base->sup_prepoll(self->sup_base->sup_pp_magic, self->sup_base->sup_pp_root);
314

315 316
    if (self->sup_base->sup_head)
      su_port_getmsgs(self);
317

318 319
    if (self->sup_base->sup_timers)
      su_timer_expire(&self->sup_base->sup_timers, &tout, now);
320 321 322 323
  } else
    SU_DEBUG_9(("cf_observer_cb(): PORT IS NOT RUNNING!\n"));

  CFRunLoopWakeUp(rl);
324

325 326 327
  return;
}

Martti Mela's avatar
Martti Mela committed
328
/** @internal Destroy a port. */
329
static void su_osx_port_deinit(void *arg)
Martti Mela's avatar
Martti Mela committed
330
{
331
  su_port_t *self = arg;
Martti Mela's avatar
Martti Mela committed
332

333
  SU_DEBUG_9(("%s(%p) called\n", "su_osx_port_deinit", (void *)self));
Martti Mela's avatar
Martti Mela committed
334

335
  su_socket_port_deinit(self->sup_base);
336 337
}

338 339
static
CFSocketCallBackType map_poll_event_to_cf_event(int events)
340 341 342 343 344
{
  CFSocketCallBackType type = 0;

  if (events & SU_WAIT_IN)
    type |= kCFSocketReadCallBack;
345

346 347
  if (events & SU_WAIT_OUT)
    type |= kCFSocketWriteCallBack;
348

349 350 351
#if 0
  if (events & SU_WAIT_CONNECT)
    type |= kCFSocketConnectCallBack;
352

353 354 355 356 357
  if (events & SU_WAIT_ACCEPT)
    type |= kCFSocketAcceptCallBack;
#endif

  return type;
Martti Mela's avatar
Martti Mela committed
358 359
}

360

361
#if 0
362 363 364 365 366 367 368
static
int map_cf_event_to_poll_event(CFSocketCallBackType type)
{
  int event = 0;

  if (type & kCFSocketReadCallBack)
    event |= SU_WAIT_IN;
369

370 371
  if (type & kCFSocketWriteCallBack)
    event |= SU_WAIT_OUT;
372

373 374
  if (type & kCFSocketConnectCallBack)
    event |= SU_WAIT_CONNECT;
375

376 377 378 379 380
  if (type & kCFSocketAcceptCallBack)
    event |= SU_WAIT_ACCEPT;

  return event;
}
381
#endif
382

383
static
384 385 386 387
void su_osx_port_socket_cb(CFSocketRef s,
			   CFSocketCallBackType type,
			   CFDataRef address,
			   const void *data,
388
			   void *info)
Martti Mela's avatar
Martti Mela committed
389
{
390 391 392
  struct osx_magic *magic = (struct osx_magic *) info;
  su_port_t        *self = magic->o_port;
  int               curr = magic->o_current;
393
  su_duration_t tout = 0;
394

395 396 397 398 399
#if SU_HAVE_POLL
  {
    su_root_t *root;
    su_wait_t *waits = self->sup_waits;
    int n = self->sup_indices[curr];
400

401
    assert(self->sup_reverses[n] == curr);
402

403
    SU_DEBUG_9(("socket_cb(%p): count %u index %d\n", self->sup_sources[n], magic->o_count, curr));
404

405 406
    waits[n].revents = map_poll_event_to_cf_event(type);

407
    root = self->sup_wait_roots[n];
408 409
    self->sup_wait_cbs[n](root ? su_root_magic(root) : NULL,
			  &waits[n],
410
			  self->sup_wait_args[n]);
411

412 413
    if (self->sup_base->sup_running) {
      su_port_getmsgs(self);
414

415 416
      if (self->sup_base->sup_timers)
	su_timer_expire(&self->sup_base->sup_timers, &tout, su_now());
417

418
      if (self->sup_base->sup_head)
419 420
	tout = 0;

421
      /* CFRunLoopWakeUp(CFRunLoopGetCurrent()); */
422
    }
423

424 425 426 427
    /* Tell to run loop an su socket fired */
    self->sup_source_fired = 1;
  }
#endif
428

Martti Mela's avatar
Martti Mela committed
429 430 431
}

/** @internal Send a message to the port. */
432
int su_osx_port_send(su_port_t *self, su_msg_r rmsg)
Martti Mela's avatar
Martti Mela committed
433
{
434 435
  CFRunLoopRef rl;

Martti Mela's avatar
Martti Mela committed
436
  if (self) {
437
    int wakeup;
Martti Mela's avatar
Martti Mela committed
438

439
    //XXX - mela SU_OSX_PORT_LOCK(self, "su_osx_port_send");
440

441
    wakeup = self->sup_base->sup_head == NULL;
Martti Mela's avatar
Martti Mela committed
442

443 444
    *self->sup_base->sup_tail = rmsg[0]; rmsg[0] = NULL;
    self->sup_base->sup_tail = &(*self->sup_base->sup_tail)->sum_next;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

#if SU_HAVE_MBOX
    /* if (!pthread_equal(pthread_self(), self->sup_tid)) */
    if (wakeup)
    {
      assert(self->sup_mbox[MBOX_SEND] != INVALID_SOCKET);

      if (send(self->sup_mbox[MBOX_SEND], "X", 1, 0) == -1) {
#if HAVE_SOCKETPAIR
	if (su_errno() != EWOULDBLOCK)
#endif
	  su_perror("su_msg_send: send()");
      }
    }
#endif
Martti Mela's avatar
Martti Mela committed
460

461
    //XXX - mela SU_OSX_PORT_UNLOCK(self, "su_osx_port_send");
Martti Mela's avatar
Martti Mela committed
462 463

    rl = CFRunLoopGetCurrent();
464
    CFRunLoopWakeUp(rl);
Martti Mela's avatar
Martti Mela committed
465 466 467 468 469 470 471 472

    return 0;
  }
  else {
    su_msg_destroy(rmsg);
    return -1;
  }
}
473 474
static int o_count;

Martti Mela's avatar
Martti Mela committed
475 476 477 478 479 480 481 482
/** @internal
 *
 *  Register a @c su_wait_t object. The wait object, a callback function and
 *  a 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.
483
 *
Martti Mela's avatar
Martti Mela committed
484 485 486 487 488
 * @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
489
 * @param priority   relative priority of the wait object
Martti Mela's avatar
Martti Mela committed
490
 *              (0 is normal, 1 important, 2 realtime)
491
 *
Martti Mela's avatar
Martti Mela committed
492
 * @return
493
 *   The function @su_osx_port_register returns nonzero index of the wait object,
Martti Mela's avatar
Martti Mela committed
494
 *   or -1 upon an error.  */
495
int su_osx_port_register(su_port_t *self,
496 497
			 su_root_t *root,
			 su_wait_t *wait,
498 499 500
			 su_wakeup_f callback,
			 su_wakeup_arg_t *arg,
			 int priority)
Martti Mela's avatar
Martti Mela committed
501
{
502
  int i, j, n;
Martti Mela's avatar
Martti Mela committed
503
  CFRunLoopRef rl;
504 505
  CFRunLoopSourceRef *sources, source;
  CFSocketRef cf_socket, *sockets;
506
  int events = 0;
507
  struct osx_magic *osx_magic = NULL;
508 509
  CFSocketContext cf_socket_cntx[1] = {{0, NULL, NULL, NULL, NULL}};
  CFOptionFlags flags = 0;
Martti Mela's avatar
Martti Mela committed
510

511
  // XXX - mela assert(SU_OSX_PORT_OWN_THREAD(self));
Martti Mela's avatar
Martti Mela committed
512 513 514

  n = self->sup_n_waits;

515 516 517
  if (n >= SU_WAIT_MAX)
    return su_seterrno(ENOMEM);

Martti Mela's avatar
Martti Mela committed
518 519 520
  if (n >= self->sup_size_waits) {
    /* Reallocate size arrays */
    int size;
521 522
    int *indices;
    int *reverses;
Martti Mela's avatar
Martti Mela committed
523 524 525 526 527 528
    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)
529
      size = su_root_size_hint;
530
    else
Martti Mela's avatar
Martti Mela committed
531 532
      size = 2 * self->sup_size_waits;

533 534
    if (size < SU_WAIT_MIN)
      size = SU_WAIT_MIN;
535 536 537 538 539

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

540
    indices = realloc(self->sup_indices, (size + 1) * sizeof(*indices));
Martti Mela's avatar
Martti Mela committed
541 542 543
    if (indices) {
      self->sup_indices = indices;

544 545
      for (i = self->sup_size_waits; i <= size; i++)
	indices[i] = -1 - i;
546
    }
Martti Mela's avatar
Martti Mela committed
547

548 549 550 551 552 553
    reverses = realloc(self->sup_reverses, size * sizeof(*waits));
    if (reverses) {
      for (i = self->sup_size_waits; i < size; i++)
	reverses[i] = -1;
      self->sup_reverses = reverses;
    }
554

555 556 557 558
    sources = realloc(self->sup_sources, size * sizeof(*sources));
    if (sources)
      self->sup_sources = sources;

559 560 561 562 563 564 565 566
    sockets = realloc(self->sup_sockets, size * sizeof(*sockets));
    if (sockets)
      self->sup_sockets = sockets;

    waits = realloc(self->sup_waits, size * sizeof(*waits));
    if (waits)
      self->sup_waits = waits;

Martti Mela's avatar
Martti Mela committed
567 568 569 570 571 572 573 574 575 576
    wait_cbs = realloc(self->sup_wait_cbs, size * sizeof(*wait_cbs));
    if (wait_cbs)
      self->sup_wait_cbs = wait_cbs;

    wait_args = realloc(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 = realloc(self->sup_wait_roots, size * sizeof(*wait_tasks));
577
    if (wait_tasks)
Martti Mela's avatar
Martti Mela committed
578 579
      self->sup_wait_roots = wait_tasks;

580
    if (!(indices &&
581
	  reverses && sources && sockets && waits && wait_cbs && wait_args && wait_tasks)) {
Martti Mela's avatar
Martti Mela committed
582 583 584 585 586 587
      return -1;
    }

    self->sup_size_waits = size;
  }

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

Martti Mela's avatar
Martti Mela committed
590 591
  if (priority > 0) {
    /* Insert */
592 593 594
    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;
595 596
      self->sup_reverses[n] = self->sup_reverses[n-1];
      self->sup_sources[n] = self->sup_sources[n-1];
597 598
      self->sup_sockets[n] = self->sup_sockets[n-1];
      self->sup_waits[n] = self->sup_waits[n-1];
Martti Mela's avatar
Martti Mela committed
599 600
      self->sup_wait_cbs[n] = self->sup_wait_cbs[n-1];
      self->sup_wait_args[n] = self->sup_wait_args[n-1];
601
      self->sup_wait_roots[n] = self->sup_wait_roots[n-1];
Martti Mela's avatar
Martti Mela committed
602
    }
603

604
    self->sup_pri_offset++;
Martti Mela's avatar
Martti Mela committed
605 606 607
  }
  else {
    /* Append - no need to move anything */
608
    n = self->sup_n_waits;
Martti Mela's avatar
Martti Mela committed
609 610
  }

611 612 613 614
  self->sup_n_waits++;

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

  self->sup_reverses[n] = i;
Martti Mela's avatar
Martti Mela committed
617 618 619 620 621
  self->sup_waits[n] = *wait;
  self->sup_wait_cbs[n] = callback;
  self->sup_wait_args[n] = arg;
  self->sup_wait_roots[n] = root;

622
  self->sup_registers++;
Martti Mela's avatar
Martti Mela committed
623

624 625 626 627 628 629 630 631 632 633 634
  /* XXX -- mela: leak, leak -- free() somewheeeere */
  osx_magic = calloc(1, sizeof(*osx_magic));
  osx_magic->o_port = self;
  osx_magic->o_current = i;
  osx_magic->o_count = ++o_count;
  cf_socket_cntx->info = osx_magic;

  events = map_poll_event_to_cf_event(wait->events);

  cf_socket = CFSocketCreateWithNative(NULL,
				       (CFSocketNativeHandle) su_wait_socket(wait),
635
				       events, su_osx_port_socket_cb, cf_socket_cntx);
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

  flags = CFSocketGetSocketFlags(cf_socket);
  flags &= ~kCFSocketCloseOnInvalidate;

  CFSocketSetSocketFlags(cf_socket, flags);

  CFRetain(cf_socket);
  source = CFSocketCreateRunLoopSource(NULL, cf_socket, 0);

  SU_DEBUG_9(("source(%p): count %u index %d\n", source, o_count, i));

  rl = CFRunLoopGetCurrent();

  CFRunLoopAddSource(rl, source, kCFRunLoopDefaultMode);

  CFRetain(source);
  self->sup_sources[n] = source;
  self->sup_sockets[n] = cf_socket;

  CFRunLoopWakeUp(rl);

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

  return i;
660
}
Martti Mela's avatar
Martti Mela committed
661

662 663
/** Deregister a su_wait_t object. */
static
664
int su_osx_port_deregister0(su_port_t *self, int i)
665 666
{
  CFRunLoopRef rl;
667
  int n, N, *indices, *reverses;
Martti Mela's avatar
Martti Mela committed
668

669 670 671
  indices = self->sup_indices;
  reverses = self->sup_reverses;

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

674
  N = --self->sup_n_waits;
675

676
  rl = CFRunLoopGetCurrent();
677 678 679 680 681
  CFSocketInvalidate(self->sup_sockets[n]);
  CFRelease(self->sup_sockets[n]);
  CFRunLoopRemoveSource(rl, self->sup_sources[n], kCFRunLoopDefaultMode);
  CFRelease(self->sup_sources[n]);

682
  CFRunLoopWakeUp(rl);
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

  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_sources[n] = self->sup_sources[j];
      self->sup_sockets[n] = self->sup_sockets[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;
699 700 701
    }
  }

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
  if (n < N) {
    assert(reverses[N] > 0);
    assert(indices[reverses[N]] == N);

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

    self->sup_sources[n] = self->sup_sources[N];
    self->sup_sockets[n] = self->sup_sockets[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_sources[n] = NULL;
  self->sup_sockets[n] = NULL;
  self->sup_wait_cbs[n] = NULL;
  self->sup_wait_args[n] = NULL;
  self->sup_wait_roots[n] = NULL;
726

727 728
  indices[i] = indices[0];
  indices[0] = -i;
Martti Mela's avatar
Martti Mela committed
729 730 731

  self->sup_registers++;

732
  return i;
Martti Mela's avatar
Martti Mela committed
733 734
}

735

Martti Mela's avatar
Martti Mela committed
736
/** Unregister a su_wait_t object.
737
 *
738
 *  The function su_osx_port_unregister() unregisters a su_wait_t object. The
Martti Mela's avatar
Martti Mela committed
739 740
 *  wait object, a callback function and a argument are removed from the
 *  port object.
741
 *
Martti Mela's avatar
Martti Mela committed
742 743 744 745
 * @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)
746
 * @param arg      - argument given to callback function when it is invoked
Martti Mela's avatar
Martti Mela committed
747
 *                   (may be NULL)
748
 *
Martti Mela's avatar
Martti Mela committed
749 750
 * @return Nonzero index of the wait object, or -1 upon an error.
 */
751
int su_osx_port_unregister(su_port_t *self,
752 753
		       su_root_t *root,
		       su_wait_t *wait,
754 755
		       su_wakeup_f callback, /* XXX - ignored */
		       su_wakeup_arg_t *arg)
Martti Mela's avatar
Martti Mela committed
756
{
757
  int n, N;
Martti Mela's avatar
Martti Mela committed
758 759

  assert(self);
760
  // XXX - mela assert(SU_OSX_PORT_OWN_THREAD(self));
Martti Mela's avatar
Martti Mela committed
761 762 763 764

  N = self->sup_n_waits;

  for (n = 0; n < N; n++) {
765
    if (SU_WAIT_CMP(wait[0], self->sup_waits[n]) == 0) {
766
      return su_osx_port_deregister0(self, self->sup_reverses[n]);
Martti Mela's avatar
Martti Mela committed
767 768 769
    }
  }

770
  su_seterrno(ENOENT);
Martti Mela's avatar
Martti Mela committed
771

772
  return -1;
Martti Mela's avatar
Martti Mela committed
773 774 775
}

/** Deregister a su_wait_t object.
776 777
 *
 *  The function su_osx_port_deregister() deregisters a su_wait_t registrattion.
Martti Mela's avatar
Martti Mela committed
778 779
 *  The wait object, a callback function and a argument are removed from the
 *  port object.
780
 *
Martti Mela's avatar
Martti Mela committed
781 782
 * @param self     - pointer to port object
 * @param i        - registration index
783
 *
Martti Mela's avatar
Martti Mela committed
784 785
 * @return Index of the wait object, or -1 upon an error.
 */
786
int su_osx_port_deregister(su_port_t *self, int i)
Martti Mela's avatar
Martti Mela committed
787
{
788 789
  su_wait_t wait[1] = { SU_WAIT_INIT };
  int retval;
Martti Mela's avatar
Martti Mela committed
790 791

  assert(self);
792
  // XXX - mela assert(SU_OSX_PORT_OWN_THREAD(self));
Martti Mela's avatar
Martti Mela committed
793

794 795
  if (i <= 0 || i > self->sup_size_waits)
    return su_seterrno(EBADF);
Martti Mela's avatar
Martti Mela committed
796

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

800
  retval = su_osx_port_deregister0(self, i);
Martti Mela's avatar
Martti Mela committed
801 802 803

  su_wait_destroy(wait);

804
  return retval;
Martti Mela's avatar
Martti Mela committed
805 806
}

807

Martti Mela's avatar
Martti Mela committed
808 809 810
/** @internal
 * Unregister all su_wait_t objects.
 *
811
 * The function su_osx_port_unregister_all() unregisters all su_wait_t objects
812
 * and destroys all queued timers associated with given root object.
813
 *
Martti Mela's avatar
Martti Mela committed
814 815
 * @param  self     - pointer to port object
 * @param  root     - pointer to root object
816
 *
Martti Mela's avatar
Martti Mela committed
817 818
 * @return Number of wait objects removed.
 */
819
int su_osx_port_unregister_all(su_port_t *self,
Martti Mela's avatar
Martti Mela committed
820 821
			   su_root_t *root)
{
822 823 824 825 826 827 828
  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;
  CFRunLoopRef        rl;
829
  CFRunLoopSourceRef *sources;
830
  CFSocketRef        *sockets;
Martti Mela's avatar
Martti Mela committed
831

832
  // XXX - assert(SU_OSX_PORT_OWN_THREAD(self));
Martti Mela's avatar
Martti Mela committed
833

834
  N          = self->sup_n_waits;
835 836
  indices    = self->sup_indices;
  reverses   = self->sup_reverses;
837 838 839 840
  sources    = self->sup_sources;
  sockets    = self->sup_sockets;
  waits      = self->sup_waits;
  wait_cbs   = self->sup_wait_cbs;
Martti Mela's avatar
Martti Mela committed
841
  wait_args  = self->sup_wait_args;
842 843
  wait_roots = self->sup_wait_roots;

844 845 846 847 848
  rl = CFRunLoopGetCurrent();

  for (i = j = 0; i < N; i++) {
    index = reverses[i]; assert(index > 0 && indices[index] == i);

Martti Mela's avatar
Martti Mela committed
849
    if (wait_roots[i] == root) {
850 851 852 853 854
      if (i < self->sup_pri_offset)
	self->sup_pri_offset--;

      indices[index] = indices[0];
      indices[0] = -index;
Martti Mela's avatar
Martti Mela committed
855 856
      continue;
    }
857

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
    if (i != j) {
      indices[index] = j;

      CFSocketInvalidate(self->sup_sockets[j]);
      CFRelease(self->sup_sockets[j]);
      CFRunLoopRemoveSource(rl, sources[j], kCFRunLoopDefaultMode);
      CFRelease(sources[j]);

      reverses[j]   = reverses[i];
      sources[j]    = sources[i];
      sockets[j]    = sockets[i];
      waits[j]      = waits[i];
      wait_cbs[j]   = wait_cbs[i];
      wait_args[j]  = wait_args[i];
      wait_roots[j] = wait_roots[i];
    }
874

Martti Mela's avatar
Martti Mela committed
875 876
    j++;
  }
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897

  /* Prepare for removing CFSources */
  for (i = j; i < N; i++) {
    reverses[i] = -1;

    CFSocketInvalidate(self->sup_sockets[i]);
    CFRelease(self->sup_sockets[i]);
    CFRunLoopRemoveSource(rl, sources[i], kCFRunLoopDefaultMode);
    CFRunLoopSourceInvalidate(sources[i]);

    sources[i] = NULL;
    sockets[i] = NULL;
    wait_cbs[i] = NULL;
    wait_args[i] = NULL;
    wait_roots[i] = NULL;
  }
  memset(&waits[j], 0, (char *)&waits[N] - (char *)&waits[j]);

  /* Tell run loop things have changed */
  CFRunLoopWakeUp(rl);

Martti Mela's avatar
Martti Mela committed
898 899 900
  self->sup_n_waits = j;
  self->sup_registers++;

901
  return N - j;
Martti Mela's avatar
Martti Mela committed
902 903 904 905
}

/**Set mask for a registered event. @internal
 *
906
 * The function su_osx_port_eventmask() sets the mask describing events that can
Martti Mela's avatar
Martti Mela committed
907 908 909 910 911 912 913 914 915 916
 * signal the registered callback.
 *
 * @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.
 */
917
int su_osx_port_eventmask(su_port_t *self, int index, int socket, int events)
Martti Mela's avatar
Martti Mela committed
918
{
919
  int n, ret;
Martti Mela's avatar
Martti Mela committed
920 921

  assert(self);
922
  // XXX - mela assert(SU_OSX_PORT_OWN_THREAD(self));
Martti Mela's avatar
Martti Mela committed
923

924
  if (index <= 0 || index > self->sup_size_waits)
925 926
    return su_seterrno(EBADF);
  n = self->sup_indices[index];
927
  if (n < 0)
928
    return su_seterrno(EBADF);
Martti Mela's avatar
Martti Mela committed
929

930 931 932 933
  ret = su_wait_mask(&self->sup_waits[n], socket, events);

  CFSocketSetSocketFlags(self->sup_sockets[n],
			 map_poll_event_to_cf_event(events));
934

935
  return ret;
Martti Mela's avatar
Martti Mela committed
936 937
}

938 939 940
/** @internal
 *
 *  Copies the su_wait_t objects from the port. The number of wait objects
941
 *  can be found out by calling su_osx_port_query() with @a n_waits as zero.
942
 *
943 944 945 946 947 948 949 950
 * @note This function is called only by friends.
 *
 * @param self     - pointer to port object
 * @param waits    - pointer to array to which wait objects are copied
 * @param n_waits  - number of wait objects fitting in array waits
 *
 * @return Number of wait objects, or 0 upon an error.
 */
951
unsigned su_osx_port_query(su_port_t *self, su_wait_t *waits, unsigned n_waits)
952 953 954
{
  unsigned n;

955
  // XXX - mela assert(SU_OSX_PORT_OWN_THREAD(self));
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970

  n = self->sup_n_waits;

  if (n_waits != 0) {
    if (waits && n_waits >= n)
      memcpy(waits, self->sup_waits, n * sizeof(*waits));
    else
      n = 0;
  }

  return n;
}

/** @internal Enable multishot mode.
 *
971
 * The function su_osx_port_multishot() enables, disables or queries the
972 973 974 975 976 977 978
 * 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 disables, only first
 * socket event is served.
 *
 * @param self      pointer to port object
 * @param multishot multishot mode (0 => disables, 1 => enables, -1 => query)
979
 *
980 981 982 983
 * @retval 0 multishot mode is disabled
 * @retval 1 multishot mode is enabled
 * @retval -1 an error occurred
 */
984
int su_osx_port_multishot(su_port_t *self, int multishot)
Martti Mela's avatar
Martti Mela committed
985
{
986 987
  if (multishot < 0)
    return self->sup_multishot;
Martti Mela's avatar
Martti Mela committed
988
  else if (multishot == 0 || multishot == 1)
989
    return self->sup_multishot = multishot;
990
  else
Martti Mela's avatar
Martti Mela committed
991 992 993
    return (errno = EINVAL), -1;
}

994
#if 0
Martti Mela's avatar
Martti Mela committed
995 996
/** @internal Enable threadsafe operation. */
static
997
int su_osx_port_threadsafe(su_port_t *port)
Martti Mela's avatar
Martti Mela committed
998 999 1000
{
  return su_home_threadsafe(port->sup_home);
}
1001
#endif
Martti Mela's avatar
Martti Mela committed
1002

1003
/** Prepare root to be run on OSX Run Loop.
1004 1005 1006 1007 1008 1009
 *
 * Sets #su_root_t object to be callable by the application's run loop. This
 * function is to be used instead of su_root_run() for OSX applications
 * using Core Foundation's Run Loop.
 *
 * The function su_root_osx_prepare_run() returns immmediately.
1010
 *
1011
 * @param root     pointer to root object
1012
 *
1013
 * @NEW_1_12_4.
1014 1015 1016 1017 1018 1019 1020
 */
void su_root_osx_prepare_run(su_root_t *root)
{
  su_port_t *self = root->sur_task->sut_port;
  CFRunLoopRef rl;
  su_duration_t tout = 0;

1021
  // XXX - mela assert(SU_OSX_PORT_OWN_THREAD(self));
1022 1023 1024

  enter;

1025
  self->sup_base->sup_running = 1;
1026 1027
  rl = CFRunLoopGetCurrent();

1028 1029
  if (self->sup_base->sup_prepoll)
    self->sup_base->sup_prepoll(self->sup_base->sup_pp_magic, self->sup_base->sup_pp_root);
1030

1031 1032
  if (self->sup_base->sup_head)
    su_port_getmsgs(self);
1033

1034 1035
  if (self->sup_base->sup_timers)
    su_timer_expire(&self->sup_base->sup_timers, &tout, su_now());
1036

1037
  if (!self->sup_base->sup_running)
1038 1039 1040 1041 1042 1043 1044 1045
    return;

  CFRetain(rl);
  self->sup_main_loop = rl;

  return;
}

Martti Mela's avatar
Martti Mela committed
1046
/** @internal Main loop.
1047
 *
1048
 * The function @c su_osx_port_run() waits for wait objects and the timers
1049 1050
 * associated with the port object.  When any wait object is signaled or
 * timer is expired, it invokes the callbacks, and returns waiting.
1051
 *
1052
 * The function @c su_osx_port_run() runs until @c su_osx_port_break() is called
Martti Mela's avatar
Martti Mela committed
1053
 * from a callback.
1054
 *
1055
 * @param self     pointer to port object
1056
 *
1057
 */
1058
void su_osx_port_run(su_port_t *self)
Martti Mela's avatar
Martti Mela committed
1059
{
1060
  CFRunLoopRef rl;
1061 1062
  su_duration_t tout = 0;

1063
  // XXX - mela assert(SU_OSX_PORT_OWN_THREAD(self));
Martti Mela's avatar
Martti Mela committed
1064 1065 1066

  enter;

1067
  self->sup_base->sup_running = 1;
1068
  rl = CFRunLoopGetCurrent();
1069

1070 1071
  if (self->sup_base->sup_prepoll)
    self->sup_base->sup_prepoll(self->sup_base->sup_pp_magic, self->sup_base->sup_pp_root);
1072

1073 1074
  if (self->sup_base->sup_head)
    su_port_getmsgs(self);
1075

1076 1077
  if (self->sup_base->sup_timers)
    su_timer_expire(&self->sup_base->sup_timers, &tout, su_now());
1078

1079
  if (!self->sup_base->sup_running)
1080 1081
    return;

1082 1083
  CFRetain(rl);
  self->sup_main_loop = rl;
Martti Mela's avatar
Martti Mela committed
1084

1085
  /* if there are messages do a quick wait */
1086
  if (self->sup_base->sup_head)
1087
    tout = 0;
1088

1089
  CFRunLoopRun();
1090

1091
  self->sup_main_loop = NULL;
1092 1093 1094 1095 1096

}

#if tuning
/* This version can help tuning... */
1097
void su_osx_port_run_tune(su_port_t *self)
1098 1099 1100 1101 1102 1103
{
  int i;
  int timers = 0, messages = 0, events = 0;
  su_duration_t tout = 0, tout0;
  su_time_t started = su_now(), woken = started, bedtime = woken;

1104
  // XXX - mela assert(SU_OSX_PORT_OWN_THREAD(self));
1105

1106
  for (self->sup_base->sup_running = 1; self->sup_base->sup_running;) {
1107 1108 1109 1110
    tout0 = tout, tout = 2000;

    timers = 0, messages = 0;

1111 1112
    if (self->sup_base->sup_prepoll)
      self->sup_base->sup_prepoll(self->sup_base->sup_pp_magic, self->sup_base->sup_pp_root);
1113

1114 1115
    if (self->sup_base->sup_head)
      messages = su_port_getmsgs(self);
1116

1117 1118
    if (self->sup_base->sup_timers)
      timers = su_timer_expire(&self->sup_base->sup_timers, &tout, su_now());
1119

1120
    if (!self->sup_base->sup_running)
1121 1122
      break;

1123
    if (self->sup_base->sup_head)      /* if there are messages do a quick wait */
1124 1125 1126 1127
      tout = 0;

    bedtime = su_now();

1128
    events = su_osx_port_wait_events(self, tout);
1129 1130 1131 1132

    woken = su_now();

    if (messages || timers || events)
1133
      SU_DEBUG_1(("su_osx_port_run(%p): %.6f: %u messages %u timers %u "
1134 1135 1136 1137
		  "events slept %.6f/%.3f\n",
		  self, su_time_diff(woken, started), messages, timers, events,
		  su_time_diff(woken, bedtime), tout0 * 1e-3));

1138
    if (!self->sup_base->sup_running)
1139 1140
      break;
  }
Martti Mela's avatar
Martti Mela committed
1141
}
1142
#endif
Martti Mela's avatar
Martti Mela committed
1143 1144

/** @internal
1145 1146
 * The function @c su_osx_port_break() is used to terminate execution of @c
 * su_osx_port_run(). It can be called from a callback function.
1147
 *
Martti Mela's avatar
Martti Mela committed
1148
 * @param self     pointer to port
1149
 *
Martti Mela's avatar
Martti Mela committed
1150
 */
1151
void su_osx_port_break(su_port_t *self)
Martti Mela's avatar
Martti Mela committed
1152 1153 1154
{
  if (self->sup_main_loop)
    CFRunLoopStop(self->sup_main_loop);
1155

1156
  self->sup_base->sup_running = 0;
1157 1158 1159
}

/** @internal
1160
 * The function @c su_osx_port_wait_events() is used to poll() for wait objects
1161 1162 1163 1164 1165 1166 1167
 *
 * @param self     pointer to port
 * @param tout     timeout in milliseconds
 *
 * @return number of events handled
 */
static
1168
int su_osx_port_wait_events(su_port_t *self, su_duration_t tout)
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
{
  int i, events = 0;
  su_wait_t *waits = self->sup_waits;
  unsigned n = self->sup_n_waits;
#if HAVE_POLL
  unsigned version = self->sup_registers;
#endif
  su_root_t *root;

  i = su_wait(waits, n, tout);

  if (i >= 0 && (unsigned)i < n) {
1181
#if HAVE_POLL
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
    /* poll() can return events for multiple wait objects */
    if (self->sup_multishot) {
      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 /* !HAVE_POLL */
    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;
}

Martti Mela's avatar
Martti Mela committed
1213 1214
/** @internal Block until wait object is signaled or timeout.
 *
1215
 * This function waits for wait objects and the timers associated with
Martti Mela's avatar
Martti Mela committed
1216
 * the root object.  When any wait object is signaled or timer is
1217 1218
 * expired, it invokes the callbacks.
 *
Martti Mela's avatar
Martti Mela committed
1219
 *   This function returns when a callback has been invoked or @c tout
1220
 *   milliseconds is elapsed.
Martti Mela's avatar
Martti Mela committed
1221 1222 1223
 *
 * @param self     pointer to port
 * @param tout     timeout in milliseconds
1224
 *
1225
 * @return
Martti Mela's avatar
Martti Mela committed
1226 1227 1228
 *   Milliseconds to the next invocation of timer, or @c SU_WAIT_FOREVER if
 *   there are no active timers.
 */
1229
su_duration_t su_osx_port_step(su_port_t *self, su_duration_t tout)
Martti Mela's avatar
Martti Mela committed
1230 1231
{
  CFRunLoopRef rl;
1232 1233
  su_time_t now = su_now();
  CFAbsoluteTime start;
Martti Mela's avatar
Martti Mela committed
1234 1235 1236 1237 1238 1239 1240
  int ret, timeout = tout > INT32_MAX ? INT32_MAX : tout;

  rl = CFRunLoopGetCurrent();

  if (!rl)
    return -1;

1241
  CFRunLoopWakeUp(rl);
Martti Mela's avatar
Martti Mela committed
1242 1243 1244 1245

  if (tout < timeout)
    timeout = tout;

1246 1247