| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include <pj/assert.h> |
| 25 |
#include <pj/errno.h> |
| 26 |
#include <pj/ioqueue.h> |
| 27 |
#include <pj/list.h> |
| 28 |
#include <pj/lock.h> |
| 29 |
#include <pj/log.h> |
| 30 |
#include <pj/os.h> |
| 31 |
#include <pj/pool.h> |
| 32 |
#include <pj/sock.h> |
| 33 |
#include <pj/string.h> |
| 34 |
|
| 35 |
|
| 36 |
#if PJ_IOQUEUE_IMP == PJ_IOQUEUE_IMP_KQUEUE |
| 37 |
|
| 38 |
#include <sys/event.h> |
| 39 |
|
| 40 |
#define os_kqueue_open kqueue |
| 41 |
#define os_kqueue_close close |
| 42 |
#define os_kqueue_ctl kevent |
| 43 |
#define os_kqueue_wait kevent |
| 44 |
|
| 45 |
#define THIS_FILE "ioq_kqueue" |
| 46 |
|
| 47 |
|
| 48 |
#define TRACE_(expr) |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
#include "ioqueue_common_abs.h" |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
struct pj_ioqueue_key_t { |
| 59 |
DECLARE_COMMON_KEY |
| 60 |
}; |
| 61 |
|
| 62 |
struct queue { |
| 63 |
pj_ioqueue_key_t *key; |
| 64 |
enum ioqueue_event_type event_type; |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
struct pj_ioqueue_t { |
| 71 |
DECLARE_COMMON_IOQUEUE unsigned max, count; |
| 72 |
pj_ioqueue_key_t active_list; |
| 73 |
int kfd; |
| 74 |
|
| 75 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 76 |
pj_mutex_t *ref_cnt_mutex; |
| 77 |
pj_ioqueue_key_t closing_list; |
| 78 |
pj_ioqueue_key_t free_list; |
| 79 |
#endif |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
#include "ioqueue_common_abs.c" |
| 86 |
|
| 87 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 88 |
|
| 89 |
static void scan_closing_keys(pj_ioqueue_t *ioqueue); |
| 90 |
#endif |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
PJ_DEF(const char *) pj_ioqueue_name(void) |
| 96 |
{ |
| 97 |
return "kqueue"; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
PJ_DEF(pj_status_t) pj_ioqueue_create( pj_pool_t *pool, |
| 106 |
pj_size_t max_fd, |
| 107 |
pj_ioqueue_t **p_ioqueue) |
| 108 |
{ |
| 109 |
return pj_ioqueue_create2(pool, max_fd, NULL, p_ioqueue); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
PJ_DEF(pj_status_t) pj_ioqueue_create2(pj_pool_t *pool, |
| 118 |
pj_size_t max_fd, |
| 119 |
const pj_ioqueue_cfg *cfg, |
| 120 |
pj_ioqueue_t **p_ioqueue) |
| 121 |
{ |
| 122 |
pj_ioqueue_t *ioqueue; |
| 123 |
pj_status_t rc; |
| 124 |
pj_lock_t *lock; |
| 125 |
pj_size_t i; |
| 126 |
|
| 127 |
|
| 128 |
PJ_ASSERT_RETURN(pool != NULL && p_ioqueue != NULL && max_fd > 0, |
| 129 |
PJ_EINVAL); |
| 130 |
|
| 131 |
|
| 132 |
PJ_ASSERT_RETURN(sizeof(pj_ioqueue_op_key_t) - sizeof(void *) >= |
| 133 |
sizeof(union operation_key), |
| 134 |
PJ_EBUG); |
| 135 |
|
| 136 |
ioqueue = pj_pool_alloc(pool, sizeof(pj_ioqueue_t)); |
| 137 |
|
| 138 |
ioqueue_init(ioqueue); |
| 139 |
|
| 140 |
if (cfg) |
| 141 |
pj_memcpy(&ioqueue->cfg, cfg, sizeof(*cfg)); |
| 142 |
else |
| 143 |
pj_ioqueue_cfg_default(&ioqueue->cfg); |
| 144 |
ioqueue->max = max_fd; |
| 145 |
ioqueue->count = 0; |
| 146 |
pj_list_init(&ioqueue->active_list); |
| 147 |
|
| 148 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
rc = pj_mutex_create_simple(pool, NULL, &ioqueue->ref_cnt_mutex); |
| 158 |
if (rc != PJ_SUCCESS) |
| 159 |
return rc; |
| 160 |
|
| 161 |
|
| 162 |
pj_list_init(&ioqueue->free_list); |
| 163 |
pj_list_init(&ioqueue->closing_list); |
| 164 |
|
| 165 |
|
| 166 |
for (i = 0; i < max_fd; ++i) { |
| 167 |
pj_ioqueue_key_t *key; |
| 168 |
|
| 169 |
key = PJ_POOL_ALLOC_T(pool, pj_ioqueue_key_t); |
| 170 |
key->ref_count = 0; |
| 171 |
rc = pj_lock_create_recursive_mutex(pool, NULL, &key->lock); |
| 172 |
if (rc != PJ_SUCCESS) { |
| 173 |
key = ioqueue->free_list.next; |
| 174 |
while (key != &ioqueue->free_list) { |
| 175 |
pj_lock_destroy(key->lock); |
| 176 |
key = key->next; |
| 177 |
} |
| 178 |
pj_mutex_destroy(ioqueue->ref_cnt_mutex); |
| 179 |
return rc; |
| 180 |
} |
| 181 |
|
| 182 |
pj_list_push_back(&ioqueue->free_list, key); |
| 183 |
} |
| 184 |
#endif |
| 185 |
|
| 186 |
rc = pj_lock_create_simple_mutex(pool, "ioq%p", &lock); |
| 187 |
if (rc != PJ_SUCCESS) |
| 188 |
return rc; |
| 189 |
|
| 190 |
rc = pj_ioqueue_set_lock(ioqueue, lock, PJ_TRUE); |
| 191 |
if (rc != PJ_SUCCESS) |
| 192 |
return rc; |
| 193 |
|
| 194 |
|
| 195 |
ioqueue->kfd = os_kqueue_open(); |
| 196 |
if (ioqueue->kfd == -1) { |
| 197 |
pj_lock_acquire(ioqueue->lock); |
| 198 |
ioqueue_destroy(ioqueue); |
| 199 |
return PJ_RETURN_OS_ERROR(pj_get_native_os_error()); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
pj_set_cloexec_flag(ioqueue->kfd); |
| 204 |
|
| 205 |
PJ_LOG(4, |
| 206 |
("pjlib", "%s I/O Queue created (%p)", pj_ioqueue_name(), ioqueue)); |
| 207 |
|
| 208 |
*p_ioqueue = ioqueue; |
| 209 |
return PJ_SUCCESS; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
PJ_DEF(pj_status_t) pj_ioqueue_destroy(pj_ioqueue_t *ioqueue) |
| 218 |
{ |
| 219 |
pj_ioqueue_key_t *key; |
| 220 |
|
| 221 |
PJ_ASSERT_RETURN(ioqueue, PJ_EINVAL); |
| 222 |
PJ_ASSERT_RETURN(ioqueue->kfd > 0, PJ_EINVALIDOP); |
| 223 |
|
| 224 |
pj_lock_acquire(ioqueue->lock); |
| 225 |
os_kqueue_close(ioqueue->kfd); |
| 226 |
ioqueue->kfd = -1; |
| 227 |
|
| 228 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 229 |
|
| 230 |
key = ioqueue->active_list.next; |
| 231 |
while (key != &ioqueue->active_list) { |
| 232 |
pj_lock_destroy(key->lock); |
| 233 |
key = key->next; |
| 234 |
} |
| 235 |
|
| 236 |
key = ioqueue->closing_list.next; |
| 237 |
while (key != &ioqueue->closing_list) { |
| 238 |
pj_lock_destroy(key->lock); |
| 239 |
key = key->next; |
| 240 |
} |
| 241 |
|
| 242 |
key = ioqueue->free_list.next; |
| 243 |
while (key != &ioqueue->free_list) { |
| 244 |
pj_lock_destroy(key->lock); |
| 245 |
key = key->next; |
| 246 |
} |
| 247 |
|
| 248 |
pj_mutex_destroy(ioqueue->ref_cnt_mutex); |
| 249 |
#endif |
| 250 |
return ioqueue_destroy(ioqueue); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
PJ_DEF(pj_status_t) |
| 259 |
pj_ioqueue_register_sock2(pj_pool_t *pool, pj_ioqueue_t *ioqueue, |
| 260 |
pj_sock_t sock, pj_grp_lock_t *grp_lock, |
| 261 |
void *user_data, const pj_ioqueue_callback *cb, |
| 262 |
pj_ioqueue_key_t **p_key) |
| 263 |
{ |
| 264 |
pj_ioqueue_key_t *key = NULL; |
| 265 |
pj_uint32_t value; |
| 266 |
int status; |
| 267 |
pj_status_t rc = PJ_SUCCESS; |
| 268 |
struct kevent events[2]; |
| 269 |
|
| 270 |
PJ_ASSERT_RETURN( |
| 271 |
pool && ioqueue && sock != PJ_INVALID_SOCKET && cb && p_key, PJ_EINVAL); |
| 272 |
|
| 273 |
pj_lock_acquire(ioqueue->lock); |
| 274 |
|
| 275 |
if (ioqueue->count >= ioqueue->max) { |
| 276 |
rc = PJ_ETOOMANY; |
| 277 |
TRACE_((THIS_FILE, "pj_ioqueue_register_sock error: too many files")); |
| 278 |
goto on_return; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 285 |
|
| 286 |
|
| 287 |
scan_closing_keys(ioqueue); |
| 288 |
|
| 289 |
pj_assert(!pj_list_empty(&ioqueue->free_list)); |
| 290 |
if (pj_list_empty(&ioqueue->free_list)) { |
| 291 |
rc = PJ_ETOOMANY; |
| 292 |
goto on_return; |
| 293 |
} |
| 294 |
|
| 295 |
key = ioqueue->free_list.next; |
| 296 |
pj_list_erase(key); |
| 297 |
#else |
| 298 |
|
| 299 |
key = (pj_ioqueue_key_t *)pj_pool_zalloc(pool, sizeof(pj_ioqueue_key_t)); |
| 300 |
#endif |
| 301 |
|
| 302 |
rc = ioqueue_init_key(pool, ioqueue, key, sock, grp_lock, user_data, cb); |
| 303 |
if (rc != PJ_SUCCESS) { |
| 304 |
key = NULL; |
| 305 |
goto on_return; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
EV_SET(&events[0], key->fd, EVFILT_READ, EV_ADD, 0, 0, key); |
| 310 |
EV_SET(&events[1], key->fd, EVFILT_WRITE, EV_ADD | EV_DISABLE, 0, 0, key); |
| 311 |
|
| 312 |
|
| 313 |
status = os_kqueue_ctl(ioqueue->kfd, events, 2, NULL, 0, NULL); |
| 314 |
if (status == -1) { |
| 315 |
rc = pj_get_os_error(); |
| 316 |
pj_lock_destroy(key->lock); |
| 317 |
key = NULL; |
| 318 |
TRACE_((THIS_FILE, "pj_ioqueue_register_sock error: kevent rc=%d", |
| 319 |
status)); |
| 320 |
goto on_return; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
value = 1; |
| 325 |
if (ioctl(sock, FIONBIO, &value)) { |
| 326 |
rc = pj_get_netos_error(); |
| 327 |
goto on_return; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
pj_list_insert_before(&ioqueue->active_list, key); |
| 332 |
++ioqueue->count; |
| 333 |
|
| 334 |
on_return: |
| 335 |
if (rc != PJ_SUCCESS) { |
| 336 |
if (key && key->grp_lock) |
| 337 |
pj_grp_lock_dec_ref_dbg(key->grp_lock, "ioqueue", 0); |
| 338 |
} |
| 339 |
*p_key = key; |
| 340 |
pj_lock_release(ioqueue->lock); |
| 341 |
|
| 342 |
return rc; |
| 343 |
} |
| 344 |
|
| 345 |
PJ_DEF(pj_status_t) |
| 346 |
pj_ioqueue_register_sock(pj_pool_t *pool, pj_ioqueue_t *ioqueue, pj_sock_t sock, |
| 347 |
void *user_data, const pj_ioqueue_callback *cb, |
| 348 |
pj_ioqueue_key_t **p_key) |
| 349 |
{ |
| 350 |
return pj_ioqueue_register_sock2(pool, ioqueue, sock, NULL, user_data, cb, |
| 351 |
p_key); |
| 352 |
} |
| 353 |
|
| 354 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 355 |
|
| 356 |
static void increment_counter(pj_ioqueue_key_t *key) |
| 357 |
{ |
| 358 |
pj_mutex_lock(key->ioqueue->ref_cnt_mutex); |
| 359 |
++key->ref_count; |
| 360 |
pj_mutex_unlock(key->ioqueue->ref_cnt_mutex); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
static void decrement_counter(pj_ioqueue_key_t *key) |
| 369 |
{ |
| 370 |
pj_lock_acquire(key->ioqueue->lock); |
| 371 |
pj_mutex_lock(key->ioqueue->ref_cnt_mutex); |
| 372 |
--key->ref_count; |
| 373 |
if (key->ref_count == 0) { |
| 374 |
|
| 375 |
pj_assert(key->closing == 1); |
| 376 |
pj_gettickcount(&key->free_time); |
| 377 |
key->free_time.msec += PJ_IOQUEUE_KEY_FREE_DELAY; |
| 378 |
pj_time_val_normalize(&key->free_time); |
| 379 |
|
| 380 |
pj_list_erase(key); |
| 381 |
pj_list_push_back(&key->ioqueue->closing_list, key); |
| 382 |
} |
| 383 |
pj_mutex_unlock(key->ioqueue->ref_cnt_mutex); |
| 384 |
pj_lock_release(key->ioqueue->lock); |
| 385 |
} |
| 386 |
#endif |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
PJ_DEF(pj_status_t) pj_ioqueue_unregister(pj_ioqueue_key_t *key) |
| 394 |
{ |
| 395 |
pj_ioqueue_t *ioqueue; |
| 396 |
int status; |
| 397 |
struct kevent events[2]; |
| 398 |
|
| 399 |
PJ_ASSERT_RETURN(key != NULL, PJ_EINVAL); |
| 400 |
|
| 401 |
ioqueue = key->ioqueue; |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
pj_ioqueue_lock_key(key); |
| 408 |
|
| 409 |
|
| 410 |
if (IS_CLOSING(key)) { |
| 411 |
pj_ioqueue_unlock_key(key); |
| 412 |
return PJ_SUCCESS; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
pj_lock_acquire(ioqueue->lock); |
| 417 |
|
| 418 |
|
| 419 |
if (ioqueue->count > 0) { |
| 420 |
--ioqueue->count; |
| 421 |
} else { |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
pj_assert(!"Bad ioqueue count in key unregistration!"); |
| 426 |
PJ_LOG(1, (THIS_FILE, "Bad ioqueue count in key unregistration!")); |
| 427 |
} |
| 428 |
|
| 429 |
#if !PJ_IOQUEUE_HAS_SAFE_UNREG |
| 430 |
pj_list_erase(key); |
| 431 |
#endif |
| 432 |
|
| 433 |
|
| 434 |
EV_SET(&events[0], key->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); |
| 435 |
EV_SET(&events[1], key->fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); |
| 436 |
|
| 437 |
|
| 438 |
status = os_kqueue_ctl(ioqueue->kfd, events, 2, NULL, 0, NULL); |
| 439 |
if (status == -1) { |
| 440 |
pj_status_t rc = pj_get_os_error(); |
| 441 |
pj_lock_release(ioqueue->lock); |
| 442 |
pj_ioqueue_unlock_key(key); |
| 443 |
return rc; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
pj_sock_close(key->fd); |
| 448 |
|
| 449 |
pj_lock_release(ioqueue->lock); |
| 450 |
|
| 451 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 452 |
|
| 453 |
key->closing = 1; |
| 454 |
|
| 455 |
pj_ioqueue_unlock_key(key); |
| 456 |
|
| 457 |
|
| 458 |
ioqueue_drain_pending_writes(key); |
| 459 |
|
| 460 |
|
| 461 |
decrement_counter(key); |
| 462 |
|
| 463 |
|
| 464 |
if (key->grp_lock) { |
| 465 |
|
| 466 |
pj_grp_lock_t *grp_lock = key->grp_lock; |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
pj_grp_lock_dec_ref_dbg(grp_lock, "ioqueue", 0); |
| 472 |
} |
| 473 |
#else |
| 474 |
if (key->grp_lock) { |
| 475 |
|
| 476 |
pj_grp_lock_t *grp_lock = key->grp_lock; |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
pj_grp_lock_dec_ref_dbg(grp_lock, "ioqueue", 0); |
| 482 |
pj_grp_lock_release(grp_lock); |
| 483 |
} else { |
| 484 |
pj_ioqueue_unlock_key(key); |
| 485 |
} |
| 486 |
|
| 487 |
pj_lock_destroy(key->lock); |
| 488 |
#endif |
| 489 |
|
| 490 |
return PJ_SUCCESS; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
static void ioqueue_remove_from_set2(pj_ioqueue_t *ioqueue, |
| 499 |
pj_ioqueue_key_t *key, |
| 500 |
unsigned event_types) |
| 501 |
{ |
| 502 |
struct kevent event; |
| 503 |
if (event_types & READABLE_EVENT) { |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
} |
| 511 |
if (event_types & WRITEABLE_EVENT) { |
| 512 |
if (!key_has_pending_write(key) && !key_has_pending_connect(key)) { |
| 513 |
EV_SET(&event, key->fd, EVFILT_WRITE, EV_DISABLE, 0, 0, key); |
| 514 |
os_kqueue_ctl(ioqueue->kfd, &event, 1, NULL, 0, NULL); |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
static void ioqueue_remove_from_set( pj_ioqueue_t *ioqueue, |
| 520 |
pj_ioqueue_key_t *key, |
| 521 |
enum ioqueue_event_type event_type ) |
| 522 |
{ |
| 523 |
ioqueue_remove_from_set2(ioqueue, key, event_type); |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
static void ioqueue_add_to_set2(pj_ioqueue_t *ioqueue, pj_ioqueue_key_t *key, |
| 533 |
unsigned event_types) |
| 534 |
{ |
| 535 |
struct kevent event; |
| 536 |
|
| 537 |
if (event_types & READABLE_EVENT) { |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
} |
| 543 |
|
| 544 |
if (event_types & WRITEABLE_EVENT) { |
| 545 |
EV_SET(&event, key->fd, EVFILT_WRITE, EV_ENABLE, 0, 0, key); |
| 546 |
os_kqueue_ctl(ioqueue->kfd, &event, 1, NULL, 0, NULL); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
static void ioqueue_add_to_set( pj_ioqueue_t *ioqueue, |
| 551 |
pj_ioqueue_key_t *key, |
| 552 |
enum ioqueue_event_type event_type ) |
| 553 |
{ |
| 554 |
ioqueue_add_to_set2(ioqueue, key, event_type); |
| 555 |
} |
| 556 |
|
| 557 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 558 |
|
| 559 |
static void scan_closing_keys(pj_ioqueue_t *ioqueue) |
| 560 |
{ |
| 561 |
pj_time_val now; |
| 562 |
pj_ioqueue_key_t *h; |
| 563 |
|
| 564 |
pj_gettickcount(&now); |
| 565 |
h = ioqueue->closing_list.next; |
| 566 |
while (h != &ioqueue->closing_list) { |
| 567 |
pj_ioqueue_key_t *next = h->next; |
| 568 |
|
| 569 |
pj_assert(h->closing != 0); |
| 570 |
|
| 571 |
if (PJ_TIME_VAL_GTE(now, h->free_time)) { |
| 572 |
pj_list_erase(h); |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
pj_list_push_back(&ioqueue->free_list, h); |
| 578 |
} |
| 579 |
h = next; |
| 580 |
} |
| 581 |
} |
| 582 |
#endif |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
PJ_DEF(int) pj_ioqueue_poll(pj_ioqueue_t *ioqueue, const pj_time_val *timeout) |
| 589 |
{ |
| 590 |
int i, count, event_cnt, processed_cnt; |
| 591 |
struct timespec xtimeout = {1, 0}; |
| 592 |
enum |
| 593 |
{ |
| 594 |
MAX_EVENTS = PJ_IOQUEUE_MAX_CAND_EVENTS |
| 595 |
}; |
| 596 |
struct kevent events[MAX_EVENTS]; |
| 597 |
struct queue queue[MAX_EVENTS]; |
| 598 |
pj_timestamp t1, t2; |
| 599 |
|
| 600 |
PJ_CHECK_STACK(); |
| 601 |
|
| 602 |
if (timeout) { |
| 603 |
xtimeout.tv_sec = timeout->sec; |
| 604 |
xtimeout.tv_nsec = timeout->msec * 1000000; |
| 605 |
} |
| 606 |
|
| 607 |
TRACE_((THIS_FILE, "start kqueue wait, msec=%d", |
| 608 |
xtimeout.tv_sec * 1000 + xtimeout.tv_nsec / 1000000)); |
| 609 |
|
| 610 |
pj_get_timestamp(&t1); |
| 611 |
count = |
| 612 |
os_kqueue_wait(ioqueue->kfd, NULL, 0, events, MAX_EVENTS, &xtimeout); |
| 613 |
pj_get_timestamp(&t2); |
| 614 |
if (count == 0) { |
| 615 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
if (count == 0 && !pj_list_empty(&ioqueue->closing_list)) { |
| 620 |
pj_lock_acquire(ioqueue->lock); |
| 621 |
scan_closing_keys(ioqueue); |
| 622 |
pj_lock_release(ioqueue->lock); |
| 623 |
} |
| 624 |
#endif |
| 625 |
TRACE_((THIS_FILE, "kqueue wait timed out")); |
| 626 |
return count; |
| 627 |
} else if (count < 0) { |
| 628 |
TRACE_((THIS_FILE, "kqueue wait error")); |
| 629 |
return -pj_get_netos_error(); |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
pj_lock_acquire(ioqueue->lock); |
| 634 |
|
| 635 |
for (event_cnt = 0, i = 0; i < count; ++i) { |
| 636 |
pj_ioqueue_key_t *h = (pj_ioqueue_key_t *)events[i].udata; |
| 637 |
|
| 638 |
TRACE_((THIS_FILE, "event %d: events=%d", i, events[i].filter)); |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
if ((events[i].filter & EVFILT_READ) && |
| 644 |
(key_has_pending_read(h) || key_has_pending_accept(h)) && |
| 645 |
!IS_CLOSING(h)) { |
| 646 |
|
| 647 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 648 |
increment_counter(h); |
| 649 |
#endif |
| 650 |
queue[event_cnt].key = h; |
| 651 |
queue[event_cnt].event_type = READABLE_EVENT; |
| 652 |
++event_cnt; |
| 653 |
continue; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
if ((events[i].filter & EVFILT_WRITE) && |
| 660 |
(key_has_pending_write(h) || key_has_pending_connect(h)) && |
| 661 |
!IS_CLOSING(h)) { |
| 662 |
|
| 663 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 664 |
increment_counter(h); |
| 665 |
#endif |
| 666 |
queue[event_cnt].key = h; |
| 667 |
queue[event_cnt].event_type = WRITEABLE_EVENT; |
| 668 |
++event_cnt; |
| 669 |
continue; |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
for (i = 0; i < event_cnt; ++i) { |
| 674 |
if (queue[i].key->grp_lock) |
| 675 |
pj_grp_lock_add_ref_dbg(queue[i].key->grp_lock, "ioqueue", 0); |
| 676 |
} |
| 677 |
|
| 678 |
pj_lock_release(ioqueue->lock); |
| 679 |
|
| 680 |
processed_cnt = 0; |
| 681 |
|
| 682 |
|
| 683 |
for (i = 0; i < event_cnt; ++i) { |
| 684 |
|
| 685 |
|
| 686 |
if (processed_cnt < PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL) { |
| 687 |
switch (queue[i].event_type) { |
| 688 |
case READABLE_EVENT: |
| 689 |
if (ioqueue_dispatch_read_event(ioqueue, queue[i].key)) |
| 690 |
++processed_cnt; |
| 691 |
break; |
| 692 |
case WRITEABLE_EVENT: |
| 693 |
if (ioqueue_dispatch_write_event(ioqueue, queue[i].key)) |
| 694 |
++processed_cnt; |
| 695 |
break; |
| 696 |
case EXCEPTION_EVENT: |
| 697 |
if (ioqueue_dispatch_exception_event(ioqueue, queue[i].key)) |
| 698 |
++processed_cnt; |
| 699 |
break; |
| 700 |
case NO_EVENT: |
| 701 |
pj_assert(!"Invalid event!"); |
| 702 |
break; |
| 703 |
} |
| 704 |
} |
| 705 |
#if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 706 |
decrement_counter(queue[i].key); |
| 707 |
#endif |
| 708 |
|
| 709 |
if (queue[i].key->grp_lock) |
| 710 |
pj_grp_lock_dec_ref_dbg(queue[i].key->grp_lock, "ioqueue", 0); |
| 711 |
} |
| 712 |
|
| 713 |
if (!event_cnt) { |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
long msec = xtimeout.tv_sec * 1000 + xtimeout.tv_nsec / 1000000; |
| 720 |
long delay = msec - pj_elapsed_usec(&t1, &t2)/1000; |
| 721 |
if (delay > 10) delay = 10; |
| 722 |
if (delay > 0) |
| 723 |
pj_thread_sleep(delay); |
| 724 |
} |
| 725 |
|
| 726 |
TRACE_((THIS_FILE, " poll: count=%d events=%d processed=%d", count, |
| 727 |
event_cnt, processed_cnt)); |
| 728 |
|
| 729 |
return processed_cnt; |
| 730 |
} |
| 731 |
|
| 732 |
PJ_DEF(pj_oshandle_t) pj_ioqueue_get_os_handle( pj_ioqueue_t *ioqueue ) |
| 733 |
{ |
| 734 |
return ioqueue ? (pj_oshandle_t)&ioqueue->kfd : NULL; |
| 735 |
} |
| 736 |
|
| 737 |
#endif |