su_kqueue_port.c 16.5 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 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
Pekka Pessi's avatar
Pekka Pessi committed
26
 * @CFILE su_kqueue_port.c
Martti Mela's avatar
Martti Mela committed
27 28 29 30
 *
 * Port implementation using kqueue()
 *
 * @author Martti Mela <Martti.Mela@nokia.com>
31
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
Martti Mela's avatar
Martti Mela committed
32 33 34 35 36 37 38 39 40 41 42
 *
 * @date Created: Sun Feb 18 19:55:37 EET 2007 mela
 */

#include "config.h"
#include "sofia-sip/su.h"

#define su_port_s su_kqueue_port_s

#include "su_port.h"

43
#if HAVE_KQUEUE
Martti Mela's avatar
Martti Mela committed
44 45 46

#include "sofia-sip/su_alloc.h"

47 48
#include <sys/event.h>

Martti Mela's avatar
Martti Mela committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#define SU_ENABLE_MULTISHOT_KQUEUE 1

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

/** Port based on kqueue(). */

struct su_kqueue_port_s {
  su_socket_port_t sup_base[1];

#define sup_home sup_base->sup_base->sup_base->sup_home

  /** kqueue fd */
  int              sup_kqueue;
  unsigned         sup_multishot; /**< Multishot operation? */

  unsigned         sup_registers; /** Counter incremented by 
				      su_port_register() or 
				      su_port_unregister()
				   */
74 75 76
  int              sup_n_registrations;
  int              sup_max_index; /**< Indexes are equal or smaller than this */
  int              sup_size_indices; /**< Size of allocated index table */
Martti Mela's avatar
Martti Mela committed
77 78 79

#define INDEX_MAX (0x7fffffff)

80 81 82 83 84 85 86 87 88
  /** Structure containing registration data */
  struct su_register {
    struct su_register *ser_next; /* Next in free list */
    su_wakeup_f     ser_cb; 
    su_wakeup_arg_t*ser_arg; 
    su_root_t      *ser_root; 
    int             ser_id; /** registration identifier */
    su_wait_t       ser_wait[1];
  } **sup_indices;
Martti Mela's avatar
Martti Mela committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
};

static void su_kqueue_port_decref(su_port_t *, int blocking, char const *who);

static int su_kqueue_port_register(su_port_t *self,
				 su_root_t *root, 
				 su_wait_t *wait, 
				 su_wakeup_f callback,
				 su_wakeup_arg_t *arg,
				 int priority);
static int su_kqueue_port_unregister(su_port_t *port,
				   su_root_t *root, 
				   su_wait_t *wait,	
				   su_wakeup_f callback, 
				   su_wakeup_arg_t *arg);
static int su_kqueue_port_deregister(su_port_t *self, int i);
static int su_kqueue_port_unregister_all(su_port_t *self, su_root_t *root);
static int su_kqueue_port_eventmask(su_port_t *self, 
				  int index,
				  int socket,
				  int events);
static int su_kqueue_port_multishot(su_port_t *self, int multishot);
static int su_kqueue_port_wait_events(su_port_t *self, su_duration_t tout);
static char const *su_kqueue_port_name(su_port_t const *self);

su_port_vtable_t const su_kqueue_port_vtable[1] =
  {{
      /* su_vtable_size: */ sizeof su_kqueue_port_vtable,
      su_pthread_port_lock,
      su_pthread_port_unlock,
      su_base_port_incref,
      su_kqueue_port_decref,
      su_base_port_gsource,
      su_socket_port_send,
      su_kqueue_port_register,
      su_kqueue_port_unregister,
      su_kqueue_port_deregister,
      su_kqueue_port_unregister_all,
      su_kqueue_port_eventmask,
      su_base_port_run,
      su_base_port_break,
      su_base_port_step,
131
      su_pthread_port_thread,
Martti Mela's avatar
Martti Mela committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
      su_base_port_add_prepoll,
      su_base_port_remove_prepoll,
      su_base_port_timers,
      su_kqueue_port_multishot,
      su_kqueue_port_wait_events,
      su_base_port_getmsgs,
      su_base_port_getmsgs_from,
      su_kqueue_port_name,
      su_base_port_start_shared,
      su_pthread_port_wait,
      su_pthread_port_execute,
    }};

static char const *su_kqueue_port_name(su_port_t const *self)
{
  return "kqueue";
}

static void su_kqueue_port_deinit(void *arg)
{
  su_port_t *self = arg;

  SU_DEBUG_9(("%s(%p) called\n", "su_kqueue_port_deinit", (void *)self));

  su_socket_port_deinit(self->sup_base);
157 158

  close(self->sup_kqueue);
Martti Mela's avatar
Martti Mela committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
}

static void su_kqueue_port_decref(su_port_t *self, int blocking, char const *who)
{
  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.
 * 
 * @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
 * @param priority   relative priority of the wait object 
 *              (0 is normal, 1 important, 2 realtime)
 * 
 * @return
 *   Positive index of the wait object, 
 *   or -1 upon an error.
 */
int su_kqueue_port_register(su_port_t *self,
188 189 190 191 192
			    su_root_t *root, 
			    su_wait_t *wait, 
			    su_wakeup_f callback,
			    su_wakeup_arg_t *arg,
			    int priority)
Martti Mela's avatar
Martti Mela committed
193 194
{
  int i, j, n;
195 196 197 198
  struct su_register *ser;
  struct su_register **indices = self->sup_indices;
  struct kevent ev[1];
  int flags;
Martti Mela's avatar
Martti Mela committed
199 200 201

  assert(su_port_own_thread(self));

202
  n = self->sup_size_indices;
Martti Mela's avatar
Martti Mela committed
203 204 205 206

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

207
  ser = indices[0];
Martti Mela's avatar
Martti Mela committed
208

209 210
  if (!ser) {
    su_home_t *h = su_port_home(self);
Martti Mela's avatar
Martti Mela committed
211

212 213 214 215 216 217 218 219 220 221
    i = self->sup_max_index, j = i == 0 ? 15 : i + 16;
    
    if (j >= self->sup_size_indices) {
      /* Reallocate index table */
      n = n < 1024 ? 2 * n : n + 1024;
      indices = su_realloc(h, indices, n * sizeof(indices[0]));
      if (!indices)
	return -1;
      self->sup_indices = indices;
      self->sup_size_indices = n;
Martti Mela's avatar
Martti Mela committed
222 223
    }

224 225 226
    /* Allocate registrations */
    ser = su_zalloc(h, (j - i) * (sizeof *ser));
    if (!ser)
Martti Mela's avatar
Martti Mela committed
227 228
      return -1;

229
    indices[0] = ser;
Martti Mela's avatar
Martti Mela committed
230

231 232 233 234
    for (i++; i <= j; i++) {
      ser->ser_id = i;
      ser->ser_next = i < j ? ser + 1 : NULL;
      indices[i] = ser++;
Martti Mela's avatar
Martti Mela committed
235 236
    }

237 238 239
    self->sup_max_index = j;

    ser = indices[0];
Martti Mela's avatar
Martti Mela committed
240
  }
241 242 243 244 245 246 247 248 249

  i = ser->ser_id;

  flags = (wait->events & SU_WAIT_IN) ? EV_ADD : EV_ADD | EV_DISABLE;
  EV_SET(ev, wait->fd, EVFILT_READ, flags, 0, 0, (void *)i);
  if (kevent(self->sup_kqueue, ev, 1, NULL, 0, NULL) == -1) {
    SU_DEBUG_0(("kevent((%u, %s, %u, %p)) failed: %s\n",
		wait->fd, "EVFILT_READ", flags, (void *)i, strerror(errno)));
    return -1;
Martti Mela's avatar
Martti Mela committed
250 251
  }

252 253 254 255 256 257 258 259 260
  flags = (wait->events & SU_WAIT_OUT) ? EV_ADD : EV_ADD | EV_DISABLE;
  EV_SET(ev, wait->fd, EVFILT_WRITE, flags, 0, 0, (void *)i);
  if (kevent(self->sup_kqueue, ev, 1, NULL, 0, NULL) == -1) {
    int error = errno;
    SU_DEBUG_0(("kevent((%u, %s, %u, %p)) failed: %s\n",
		wait->fd, "EVFILT_WRITE", flags, (void *)i, strerror(error)));

    EV_SET(ev, wait->fd, EVFILT_READ, EV_DELETE, 0, 0, (void *)i);
    kevent(self->sup_kqueue, ev, 1, NULL, 0, NULL);
Martti Mela's avatar
Martti Mela committed
261

262 263 264 265
    errno = error;
    return -1;
  }
  indices[0] = ser->ser_next;
Martti Mela's avatar
Martti Mela committed
266

267 268 269 270 271
  ser->ser_next = NULL;
  *ser->ser_wait = *wait;
  ser->ser_cb = callback;
  ser->ser_arg = arg;
  ser->ser_root = root;
Martti Mela's avatar
Martti Mela committed
272 273

  self->sup_registers++;
274
  self->sup_n_registrations++;
Martti Mela's avatar
Martti Mela committed
275

276
  return i;			/* return index */
Martti Mela's avatar
Martti Mela committed
277 278 279 280 281
}

/** Deregister a su_wait_t object. */
static int su_kqueue_port_deregister0(su_port_t *self, int i, int destroy_wait)
{
282 283 284 285 286 287 288 289 290 291
  struct su_register **indices = self->sup_indices;
  struct su_register *ser;
  struct kevent ev[1];
  su_wait_t *wait;

  ser = self->sup_indices[i];
  if (ser == NULL || ser->ser_cb == NULL) {
    su_seterrno(ENOENT);
    return -1;
  }
Martti Mela's avatar
Martti Mela committed
292

293
  assert(ser->ser_id == i);
Martti Mela's avatar
Martti Mela committed
294

295
  wait = ser->ser_wait;
Martti Mela's avatar
Martti Mela committed
296

297 298 299 300 301
  EV_SET(ev, wait->fd, EVFILT_READ, EV_DELETE, 0, 0, (void *)i);
  if (kevent(self->sup_kqueue, ev, 1, NULL, 0, NULL) == -1) {
    SU_DEBUG_0(("remove kevent((%u, %s, %s, %p)) failed: %s\n",
		wait->fd, "EVFILT_READ", "EV_DELETE", (void *)i,
		strerror(errno)));
Martti Mela's avatar
Martti Mela committed
302 303
  }

304 305 306 307 308
  EV_SET(ev, wait->fd, EVFILT_WRITE, EV_DELETE, 0, 0, (void *)i);
  if (kevent(self->sup_kqueue, ev, 1, NULL, 0, NULL) == -1) {
    SU_DEBUG_0(("remove kevent((%u, %s, %s, %p)) failed: %s\n",
		wait->fd, "EVFILT_WRITE", "EV_DELETE", (void *)i,
		strerror(errno)));
Martti Mela's avatar
Martti Mela committed
309 310
  }

311 312
  if (destroy_wait)
    su_wait_destroy(wait);
Martti Mela's avatar
Martti Mela committed
313
  
314 315 316
  memset(ser, 0, sizeof *ser);
  ser->ser_id = i;
  ser->ser_next = indices[0], indices[0] = ser;
Martti Mela's avatar
Martti Mela committed
317

318
  self->sup_n_registrations--;
Martti Mela's avatar
Martti Mela committed
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
  self->sup_registers++;

  return i;
}


/** Unregister a su_wait_t object.
 *  
 *  The function su_kqueue_port_unregister() unregisters a su_wait_t object. The
 *  wait object, a callback function and a argument are removed from the
 *  port object.
 * 
 * @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)
 * @param arg      - argument given to callback function when it is invoked 
 *                   (may be NULL)
 *
 * @deprecated Use su_kqueue_port_deregister() instead. 
 *
 * @return Nonzero index of the wait object, or -1 upon an error.
 */
int su_kqueue_port_unregister(su_port_t *self,
			      su_root_t *root, 
			      su_wait_t *wait,	
			      su_wakeup_f callback, /* XXX - ignored */
			      su_wakeup_arg_t *arg)
{
348 349 350
  int i, I;

  struct su_register *ser;
Martti Mela's avatar
Martti Mela committed
351 352 353 354

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

355
  I = self->sup_max_index;
Martti Mela's avatar
Martti Mela committed
356

357 358 359 360 361 362 363
  for (i = 1; i <= I; i++) {
    ser = self->sup_indices[i];

    if (ser->ser_cb &&
	arg == ser->ser_arg &&
	SU_WAIT_CMP(wait[0], ser->ser_wait[0]) == 0)
      return su_kqueue_port_deregister0(self, ser->ser_id, 0);
Martti Mela's avatar
Martti Mela committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  }

  su_seterrno(ENOENT);

  return -1;
}

/** Deregister a su_wait_t object.
 *  
 *  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.
 * 
 * @param self     - pointer to port object
 * @param i        - registration index
 * 
 * @return Index of the wait object, or -1 upon an error.
 */
int su_kqueue_port_deregister(su_port_t *self, int i)
{
384
  struct su_register *ser;
Martti Mela's avatar
Martti Mela committed
385

386
  if (i <= 0 || i > self->sup_max_index)
Martti Mela's avatar
Martti Mela committed
387 388
    return su_seterrno(EBADF);

389 390
  ser = self->sup_indices[i];
  if (!ser->ser_cb)
Martti Mela's avatar
Martti Mela committed
391 392
    return su_seterrno(EBADF);

393
  return su_kqueue_port_deregister0(self, i, 1);
Martti Mela's avatar
Martti Mela committed
394 395 396
}

/** @internal
397
 * Unregister all su_wait_t objects belonging to a root.
Martti Mela's avatar
Martti Mela committed
398 399 400 401 402 403 404 405 406 407 408 409
 *
 * The function su_kqueue_port_unregister_all() unregisters all su_wait_t objects
 * and destroys all queued timers associated with given root object.
 * 
 * @param  self     - pointer to port object
 * @param  root     - pointer to root object
 * 
 * @return Number of wait objects removed.
 */
int su_kqueue_port_unregister_all(su_port_t *self, 
				su_root_t *root)
{
410
  int i, I, n;
Martti Mela's avatar
Martti Mela committed
411

412
  struct su_register *ser;
Martti Mela's avatar
Martti Mela committed
413

414 415
  assert(self); assert(root);
  assert(su_port_own_thread(self));
Martti Mela's avatar
Martti Mela committed
416

417
  I = self->sup_max_index;
Martti Mela's avatar
Martti Mela committed
418

419 420 421
  for (i = 1, n = 0; i <= I; i++) {
    ser = self->sup_indices[i];
    if (ser->ser_root != root)
Martti Mela's avatar
Martti Mela committed
422
      continue;
423 424
    su_kqueue_port_deregister0(self, ser->ser_id, 0);
    n++;
Martti Mela's avatar
Martti Mela committed
425 426
  }

427
  return n;
Martti Mela's avatar
Martti Mela committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
}

/**Set mask for a registered event. @internal
 *
 * The function su_kqueue_port_eventmask() sets the mask describing events
 * that can 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.
 */
int su_kqueue_port_eventmask(su_port_t *self, int index, int socket, int events)
{
445 446 447 448
  struct su_register *ser;
  struct kevent ev[1];
  su_wait_t *wait;
  int flags;
Martti Mela's avatar
Martti Mela committed
449

450
  if (index <= 0 || index > self->sup_max_index)
Martti Mela's avatar
Martti Mela committed
451
    return su_seterrno(EBADF);
452 453 454

  ser = self->sup_indices[index];
  if (!ser->ser_cb)
Martti Mela's avatar
Martti Mela committed
455 456
    return su_seterrno(EBADF);

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
  wait = ser->ser_wait;

  assert(socket == wait->fd);

  wait->events = events;

  flags = (wait->events & SU_WAIT_IN) ? EV_ADD | EV_ENABLE : EV_ADD | EV_DISABLE;
  EV_SET(ev, wait->fd, EVFILT_READ, flags, 0, 0, (void *)index);
  if (kevent(self->sup_kqueue, ev, 1, NULL, 0, NULL) == -1) {
    SU_DEBUG_0(("modify kevent((%u, %s, %s, %p)) failed: %s\n",
		wait->fd, "EVFILT_READ",
		(events & SU_WAIT_IN) ? "EV_ENABLE" : "EV_DISABLE",
		(void *)index, strerror(errno)));
  }

  flags = (wait->events & SU_WAIT_OUT) ? EV_ADD | EV_ENABLE : EV_ADD | EV_DISABLE;
  EV_SET(ev, wait->fd, EVFILT_WRITE, flags, 0, 0, (void *)index);
  if (kevent(self->sup_kqueue, ev, 1, NULL, 0, NULL) == -1) {
    SU_DEBUG_0(("modify kevent((%u, %s, %s, %p)) failed: %s\n",
		wait->fd, "EVFILT_WRITE",
		(events & SU_WAIT_OUT) ? "EV_ENABLE" : "EV_DISABLE",
		(void *)index, strerror(errno)));
  }

  return 0;
Martti Mela's avatar
Martti Mela committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
}

/** @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)
 * 
 * @retval 0 multishot mode is disabled
 * @retval 1 multishot mode is enabled
 * @retval -1 an error occurred
 */
static
int su_kqueue_port_multishot(su_port_t *self, int multishot)
{
  if (multishot < 0)
    return self->sup_multishot;
  else if (multishot == 0 || multishot == 1)
    return self->sup_multishot = multishot;
  else 
    return (errno = EINVAL), -1;
}


/** @internal
 * Wait (kqueue()) for wait objects in port.
 *
 * @param self     pointer to port
 * @param tout     timeout in milliseconds
 *
 * @return number of events handled
 */
static
int su_kqueue_port_wait_events(su_port_t *self, su_duration_t tout)
{
521
  int j, n, events = 0, index;
Martti Mela's avatar
Martti Mela committed
522
  unsigned version = self->sup_registers;
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

  int const M = 4;
  struct kevent ev[M];
  
  struct timespec ts;

  ts.tv_sec = tout / 1000;
  ts.tv_nsec = tout % 1000 * 1000000;

  n = kevent(self->sup_kqueue, NULL, 0,
	     ev, self->sup_multishot ? M : 1,
	     tout < SU_DURATION_MAX ? &ts : NULL);

  assert(n <= M);
  
  for (j = 0; j < n; j++) {
    struct su_register *ser;
    su_root_magic_t *magic;

    index = (int)ev[j].udata;
    if (index <= 0 || self->sup_max_index < index)
      continue;
    ser = self->sup_indices[index];

    magic = ser->ser_root ? su_root_magic(ser->ser_root) : NULL;
    ser->ser_wait->revents =
      (ser->ser_wait->events | SU_WAIT_HUP) &
      (
       ((ev[j].filter == EVFILT_READ) ? SU_WAIT_IN : 0) |
       ((ev[j].filter == EVFILT_WRITE) ? SU_WAIT_OUT : 0) |
       ((ev[j].flags & EV_EOF) ? SU_WAIT_HUP : 0)
       );
    if (ser->ser_wait->revents) {
      ser->ser_cb(magic, ser->ser_wait, ser->ser_arg);
Martti Mela's avatar
Martti Mela committed
557
      events++;
558 559 560
      if (version != self->sup_registers)
	/* Callback function used su_register()/su_deregister() */
	return events;
Martti Mela's avatar
Martti Mela committed
561
    }
562
  }    
Martti Mela's avatar
Martti Mela committed
563

564
  return n;
Martti Mela's avatar
Martti Mela committed
565 566 567
}


568
/** Create a port using kqueue() (or poll()/select(), if kqueue() fails).
Martti Mela's avatar
Martti Mela committed
569 570 571 572
 */
su_port_t *su_kqueue_port_create(void)
{
  su_port_t *self = NULL;
573
  int kq = kqueue();
Martti Mela's avatar
Martti Mela committed
574

575 576 577 578 579 580 581
  if (kq < 0) {
#if HAVE_POLL
    return su_poll_port_create();
#else
    return su_select_port_create();
#endif
  }
Martti Mela's avatar
Martti Mela committed
582 583 584

  self = su_home_new(sizeof *self);
  if (!self)
585
    goto failed;
Martti Mela's avatar
Martti Mela committed
586 587

  if (su_home_destructor(su_port_home(self), su_kqueue_port_deinit) < 0)
588
    goto failed;
Martti Mela's avatar
Martti Mela committed
589

590 591 592 593 594 595
  self->sup_kqueue = kq, kq = -1;
  self->sup_indices = su_zalloc(su_port_home(self),
				(sizeof self->sup_indices[0]) * 
				(self->sup_size_indices = 64));
  if (!self->sup_indices)
    goto failed;
Martti Mela's avatar
Martti Mela committed
596 597

  if (su_socket_port_init(self->sup_base, su_kqueue_port_vtable) < 0)
598
    goto failed;
Martti Mela's avatar
Martti Mela committed
599

600
  self->sup_multishot = SU_ENABLE_MULTISHOT_KQUEUE;
Martti Mela's avatar
Martti Mela committed
601 602

  return self;
603 604 605 606 607 608

 failed:
  if (kq != -1)
    close(kq);
  su_home_unref(su_port_home(self));
  return NULL;
Martti Mela's avatar
Martti Mela committed
609 610 611 612 613 614 615 616
}

int su_kqueue_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)
{
617
  return su_pthreaded_port_start(su_kqueue_port_create, 
Martti Mela's avatar
Martti Mela committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
				 parent, return_clone, magic, init, deinit);
}

#else

su_port_t *su_kqueue_port_create(void)
{
  return su_default_port_create();
}

int su_kqueue_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)
{
  return su_default_clone_start(parent, return_clone, magic, init, deinit);
}

637
#endif  /* HAVE_KQUEUE */