Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * sock_select.c |
| 22 | * |
| 23 | * This is the implementation of IOQueue using pj_sock_select(). |
| 24 | * It runs anywhere where pj_sock_select() is available (currently |
| 25 | * Win32, Linux, Linux kernel, etc.). |
| 26 | */ |
| 27 | |
| 28 | #include <pj/ioqueue.h> |
| 29 | #include <pj/os.h> |
| 30 | #include <pj/lock.h> |
| 31 | #include <pj/log.h> |
| 32 | #include <pj/list.h> |
| 33 | #include <pj/pool.h> |
| 34 | #include <pj/string.h> |
| 35 | #include <pj/assert.h> |
| 36 | #include <pj/sock.h> |
| 37 | #include <pj/compat/socket.h> |
| 38 | #include <pj/sock_select.h> |
| 39 | #include <pj/errno.h> |
| 40 | |
Benny Prijono | 40fe908 | 2008-02-08 15:21:41 +0000 | [diff] [blame] | 41 | /* Now that we have access to OS'es <sys/select>, lets check again that |
| 42 | * PJ_IOQUEUE_MAX_HANDLES is not greater than FD_SETSIZE |
| 43 | */ |
| 44 | #if PJ_IOQUEUE_MAX_HANDLES > FD_SETSIZE |
| 45 | # error "PJ_IOQUEUE_MAX_HANDLES cannot be greater than FD_SETSIZE" |
| 46 | #endif |
| 47 | |
| 48 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 49 | /* |
| 50 | * Include declaration from common abstraction. |
| 51 | */ |
| 52 | #include "ioqueue_common_abs.h" |
| 53 | |
| 54 | /* |
| 55 | * ISSUES with ioqueue_select() |
| 56 | * |
| 57 | * EAGAIN/EWOULDBLOCK error in recv(): |
| 58 | * - when multiple threads are working with the ioqueue, application |
| 59 | * may receive EAGAIN or EWOULDBLOCK in the receive callback. |
| 60 | * This error happens because more than one thread is watching for |
| 61 | * the same descriptor set, so when all of them call recv() or recvfrom() |
| 62 | * simultaneously, only one will succeed and the rest will get the error. |
| 63 | * |
| 64 | */ |
| 65 | #define THIS_FILE "ioq_select" |
| 66 | |
| 67 | /* |
| 68 | * The select ioqueue relies on socket functions (pj_sock_xxx()) to return |
| 69 | * the correct error code. |
| 70 | */ |
| 71 | #if PJ_RETURN_OS_ERROR(100) != PJ_STATUS_FROM_OS(100) |
| 72 | # error "Error reporting must be enabled for this function to work!" |
| 73 | #endif |
| 74 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 75 | /* |
| 76 | * During debugging build, VALIDATE_FD_SET is set. |
| 77 | * This will check the validity of the fd_sets. |
| 78 | */ |
| 79 | /* |
| 80 | #if defined(PJ_DEBUG) && PJ_DEBUG != 0 |
| 81 | # define VALIDATE_FD_SET 1 |
| 82 | #else |
| 83 | # define VALIDATE_FD_SET 0 |
| 84 | #endif |
| 85 | */ |
| 86 | #define VALIDATE_FD_SET 0 |
| 87 | |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 88 | #if 0 |
| 89 | # define TRACE__(args) PJ_LOG(3,args) |
| 90 | #else |
| 91 | # define TRACE__(args) |
| 92 | #endif |
| 93 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 94 | /* |
| 95 | * This describes each key. |
| 96 | */ |
| 97 | struct pj_ioqueue_key_t |
| 98 | { |
| 99 | DECLARE_COMMON_KEY |
| 100 | }; |
| 101 | |
| 102 | /* |
| 103 | * This describes the I/O queue itself. |
| 104 | */ |
| 105 | struct pj_ioqueue_t |
| 106 | { |
| 107 | DECLARE_COMMON_IOQUEUE |
| 108 | |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 109 | unsigned max, count; /* Max and current key count */ |
| 110 | int nfds; /* The largest fd value (for select)*/ |
| 111 | pj_ioqueue_key_t active_list; /* List of active keys. */ |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 112 | pj_fd_set_t rfdset; |
| 113 | pj_fd_set_t wfdset; |
| 114 | #if PJ_HAS_TCP |
| 115 | pj_fd_set_t xfdset; |
| 116 | #endif |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 117 | |
| 118 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 119 | pj_mutex_t *ref_cnt_mutex; |
| 120 | pj_ioqueue_key_t closing_list; |
| 121 | pj_ioqueue_key_t free_list; |
| 122 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* Include implementation for common abstraction after we declare |
| 126 | * pj_ioqueue_key_t and pj_ioqueue_t. |
| 127 | */ |
| 128 | #include "ioqueue_common_abs.c" |
| 129 | |
Benny Prijono | cde7142 | 2007-09-19 12:03:28 +0000 | [diff] [blame] | 130 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 131 | /* Scan closing keys to be put to free list again */ |
| 132 | static void scan_closing_keys(pj_ioqueue_t *ioqueue); |
| 133 | #endif |
| 134 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 135 | /* |
| 136 | * pj_ioqueue_name() |
| 137 | */ |
| 138 | PJ_DEF(const char*) pj_ioqueue_name(void) |
| 139 | { |
| 140 | return "select"; |
| 141 | } |
| 142 | |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 143 | /* |
| 144 | * Scan the socket descriptor sets for the largest descriptor. |
| 145 | * This value is needed by select(). |
| 146 | */ |
| 147 | #if defined(PJ_SELECT_NEEDS_NFDS) && PJ_SELECT_NEEDS_NFDS!=0 |
| 148 | static void rescan_fdset(pj_ioqueue_t *ioqueue) |
| 149 | { |
| 150 | pj_ioqueue_key_t *key = ioqueue->active_list.next; |
| 151 | int max = 0; |
| 152 | |
| 153 | while (key != &ioqueue->active_list) { |
| 154 | if (key->fd > max) |
| 155 | max = key->fd; |
| 156 | key = key->next; |
| 157 | } |
| 158 | |
| 159 | ioqueue->nfds = max; |
| 160 | } |
| 161 | #else |
| 162 | static void rescan_fdset(pj_ioqueue_t *ioqueue) |
| 163 | { |
| 164 | ioqueue->nfds = FD_SETSIZE-1; |
| 165 | } |
| 166 | #endif |
| 167 | |
| 168 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 169 | /* |
| 170 | * pj_ioqueue_create() |
| 171 | * |
| 172 | * Create select ioqueue. |
| 173 | */ |
| 174 | PJ_DEF(pj_status_t) pj_ioqueue_create( pj_pool_t *pool, |
| 175 | pj_size_t max_fd, |
| 176 | pj_ioqueue_t **p_ioqueue) |
| 177 | { |
| 178 | pj_ioqueue_t *ioqueue; |
| 179 | pj_lock_t *lock; |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 180 | unsigned i; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 181 | pj_status_t rc; |
| 182 | |
| 183 | /* Check that arguments are valid. */ |
| 184 | PJ_ASSERT_RETURN(pool != NULL && p_ioqueue != NULL && |
| 185 | max_fd > 0 && max_fd <= PJ_IOQUEUE_MAX_HANDLES, |
| 186 | PJ_EINVAL); |
| 187 | |
| 188 | /* Check that size of pj_ioqueue_op_key_t is sufficient */ |
| 189 | PJ_ASSERT_RETURN(sizeof(pj_ioqueue_op_key_t)-sizeof(void*) >= |
| 190 | sizeof(union operation_key), PJ_EBUG); |
| 191 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 192 | /* Create and init common ioqueue stuffs */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 193 | ioqueue = PJ_POOL_ALLOC_T(pool, pj_ioqueue_t); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 194 | ioqueue_init(ioqueue); |
| 195 | |
| 196 | ioqueue->max = max_fd; |
| 197 | ioqueue->count = 0; |
| 198 | PJ_FD_ZERO(&ioqueue->rfdset); |
| 199 | PJ_FD_ZERO(&ioqueue->wfdset); |
| 200 | #if PJ_HAS_TCP |
| 201 | PJ_FD_ZERO(&ioqueue->xfdset); |
| 202 | #endif |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 203 | pj_list_init(&ioqueue->active_list); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 204 | |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 205 | rescan_fdset(ioqueue); |
| 206 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 207 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 208 | /* When safe unregistration is used (the default), we pre-create |
| 209 | * all keys and put them in the free list. |
| 210 | */ |
| 211 | |
| 212 | /* Mutex to protect key's reference counter |
| 213 | * We don't want to use key's mutex or ioqueue's mutex because |
| 214 | * that would create deadlock situation in some cases. |
| 215 | */ |
| 216 | rc = pj_mutex_create_simple(pool, NULL, &ioqueue->ref_cnt_mutex); |
| 217 | if (rc != PJ_SUCCESS) |
| 218 | return rc; |
| 219 | |
| 220 | |
| 221 | /* Init key list */ |
| 222 | pj_list_init(&ioqueue->free_list); |
| 223 | pj_list_init(&ioqueue->closing_list); |
| 224 | |
| 225 | |
| 226 | /* Pre-create all keys according to max_fd */ |
| 227 | for (i=0; i<max_fd; ++i) { |
| 228 | pj_ioqueue_key_t *key; |
| 229 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 230 | key = PJ_POOL_ALLOC_T(pool, pj_ioqueue_key_t); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 231 | key->ref_count = 0; |
| 232 | rc = pj_mutex_create_recursive(pool, NULL, &key->mutex); |
| 233 | if (rc != PJ_SUCCESS) { |
| 234 | key = ioqueue->free_list.next; |
| 235 | while (key != &ioqueue->free_list) { |
| 236 | pj_mutex_destroy(key->mutex); |
| 237 | key = key->next; |
| 238 | } |
| 239 | pj_mutex_destroy(ioqueue->ref_cnt_mutex); |
| 240 | return rc; |
| 241 | } |
| 242 | |
| 243 | pj_list_push_back(&ioqueue->free_list, key); |
| 244 | } |
| 245 | #endif |
| 246 | |
| 247 | /* Create and init ioqueue mutex */ |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 248 | rc = pj_lock_create_simple_mutex(pool, "ioq%p", &lock); |
| 249 | if (rc != PJ_SUCCESS) |
| 250 | return rc; |
| 251 | |
| 252 | rc = pj_ioqueue_set_lock(ioqueue, lock, PJ_TRUE); |
| 253 | if (rc != PJ_SUCCESS) |
| 254 | return rc; |
| 255 | |
| 256 | PJ_LOG(4, ("pjlib", "select() I/O Queue created (%p)", ioqueue)); |
| 257 | |
| 258 | *p_ioqueue = ioqueue; |
| 259 | return PJ_SUCCESS; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * pj_ioqueue_destroy() |
| 264 | * |
| 265 | * Destroy ioqueue. |
| 266 | */ |
| 267 | PJ_DEF(pj_status_t) pj_ioqueue_destroy(pj_ioqueue_t *ioqueue) |
| 268 | { |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 269 | pj_ioqueue_key_t *key; |
| 270 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 271 | PJ_ASSERT_RETURN(ioqueue, PJ_EINVAL); |
| 272 | |
| 273 | pj_lock_acquire(ioqueue->lock); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 274 | |
| 275 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 276 | /* Destroy reference counters */ |
| 277 | key = ioqueue->active_list.next; |
| 278 | while (key != &ioqueue->active_list) { |
| 279 | pj_mutex_destroy(key->mutex); |
| 280 | key = key->next; |
| 281 | } |
| 282 | |
| 283 | key = ioqueue->closing_list.next; |
| 284 | while (key != &ioqueue->closing_list) { |
| 285 | pj_mutex_destroy(key->mutex); |
| 286 | key = key->next; |
| 287 | } |
| 288 | |
| 289 | key = ioqueue->free_list.next; |
| 290 | while (key != &ioqueue->free_list) { |
| 291 | pj_mutex_destroy(key->mutex); |
| 292 | key = key->next; |
| 293 | } |
| 294 | |
| 295 | pj_mutex_destroy(ioqueue->ref_cnt_mutex); |
| 296 | #endif |
| 297 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 298 | return ioqueue_destroy(ioqueue); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /* |
| 303 | * pj_ioqueue_register_sock() |
| 304 | * |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 305 | * Register socket handle to ioqueue. |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 306 | */ |
| 307 | PJ_DEF(pj_status_t) pj_ioqueue_register_sock( pj_pool_t *pool, |
| 308 | pj_ioqueue_t *ioqueue, |
| 309 | pj_sock_t sock, |
| 310 | void *user_data, |
| 311 | const pj_ioqueue_callback *cb, |
| 312 | pj_ioqueue_key_t **p_key) |
| 313 | { |
| 314 | pj_ioqueue_key_t *key = NULL; |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 315 | #if defined(PJ_WIN32) && PJ_WIN32!=0 || \ |
| 316 | defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE!=0 |
| 317 | u_long value; |
| 318 | #else |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 319 | pj_uint32_t value; |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 320 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 321 | pj_status_t rc = PJ_SUCCESS; |
| 322 | |
| 323 | PJ_ASSERT_RETURN(pool && ioqueue && sock != PJ_INVALID_SOCKET && |
| 324 | cb && p_key, PJ_EINVAL); |
| 325 | |
| 326 | pj_lock_acquire(ioqueue->lock); |
| 327 | |
| 328 | if (ioqueue->count >= ioqueue->max) { |
| 329 | rc = PJ_ETOOMANY; |
| 330 | goto on_return; |
| 331 | } |
| 332 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 333 | /* If safe unregistration (PJ_IOQUEUE_HAS_SAFE_UNREG) is used, get |
| 334 | * the key from the free list. Otherwise allocate a new one. |
| 335 | */ |
| 336 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
Benny Prijono | cde7142 | 2007-09-19 12:03:28 +0000 | [diff] [blame] | 337 | |
| 338 | /* Scan closing_keys first to let them come back to free_list */ |
| 339 | scan_closing_keys(ioqueue); |
| 340 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 341 | pj_assert(!pj_list_empty(&ioqueue->free_list)); |
| 342 | if (pj_list_empty(&ioqueue->free_list)) { |
| 343 | rc = PJ_ETOOMANY; |
| 344 | goto on_return; |
| 345 | } |
| 346 | |
| 347 | key = ioqueue->free_list.next; |
| 348 | pj_list_erase(key); |
| 349 | #else |
| 350 | key = (pj_ioqueue_key_t*)pj_pool_zalloc(pool, sizeof(pj_ioqueue_key_t)); |
| 351 | #endif |
| 352 | |
| 353 | rc = ioqueue_init_key(pool, ioqueue, key, sock, user_data, cb); |
| 354 | if (rc != PJ_SUCCESS) { |
| 355 | key = NULL; |
| 356 | goto on_return; |
| 357 | } |
| 358 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 359 | /* Set socket to nonblocking. */ |
| 360 | value = 1; |
Benny Prijono | 9cf138e | 2006-01-19 03:58:29 +0000 | [diff] [blame] | 361 | #if defined(PJ_WIN32) && PJ_WIN32!=0 || \ |
| 362 | defined(PJ_WIN32_WINCE) && PJ_WIN32_WINCE!=0 |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 363 | if (ioctlsocket(sock, FIONBIO, &value)) { |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 364 | #else |
| 365 | if (ioctl(sock, FIONBIO, &value)) { |
| 366 | #endif |
| 367 | rc = pj_get_netos_error(); |
| 368 | goto on_return; |
| 369 | } |
| 370 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 371 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 372 | /* Put in active list. */ |
| 373 | pj_list_insert_before(&ioqueue->active_list, key); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 374 | ++ioqueue->count; |
| 375 | |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 376 | /* Rescan fdset to get max descriptor */ |
| 377 | rescan_fdset(ioqueue); |
| 378 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 379 | on_return: |
| 380 | /* On error, socket may be left in non-blocking mode. */ |
| 381 | *p_key = key; |
| 382 | pj_lock_release(ioqueue->lock); |
| 383 | |
| 384 | return rc; |
| 385 | } |
| 386 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 387 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 388 | /* Increment key's reference counter */ |
| 389 | static void increment_counter(pj_ioqueue_key_t *key) |
| 390 | { |
| 391 | pj_mutex_lock(key->ioqueue->ref_cnt_mutex); |
| 392 | ++key->ref_count; |
| 393 | pj_mutex_unlock(key->ioqueue->ref_cnt_mutex); |
| 394 | } |
| 395 | |
| 396 | /* Decrement the key's reference counter, and when the counter reach zero, |
| 397 | * destroy the key. |
| 398 | * |
| 399 | * Note: MUST NOT CALL THIS FUNCTION WHILE HOLDING ioqueue's LOCK. |
| 400 | */ |
| 401 | static void decrement_counter(pj_ioqueue_key_t *key) |
| 402 | { |
Benny Prijono | 324409e | 2007-10-31 07:53:17 +0000 | [diff] [blame] | 403 | pj_lock_acquire(key->ioqueue->lock); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 404 | pj_mutex_lock(key->ioqueue->ref_cnt_mutex); |
| 405 | --key->ref_count; |
| 406 | if (key->ref_count == 0) { |
| 407 | |
| 408 | pj_assert(key->closing == 1); |
| 409 | pj_gettimeofday(&key->free_time); |
| 410 | key->free_time.msec += PJ_IOQUEUE_KEY_FREE_DELAY; |
| 411 | pj_time_val_normalize(&key->free_time); |
| 412 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 413 | pj_list_erase(key); |
| 414 | pj_list_push_back(&key->ioqueue->closing_list, key); |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 415 | /* Rescan fdset to get max descriptor */ |
| 416 | rescan_fdset(key->ioqueue); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 417 | } |
| 418 | pj_mutex_unlock(key->ioqueue->ref_cnt_mutex); |
Benny Prijono | 324409e | 2007-10-31 07:53:17 +0000 | [diff] [blame] | 419 | pj_lock_release(key->ioqueue->lock); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 420 | } |
| 421 | #endif |
| 422 | |
| 423 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 424 | /* |
| 425 | * pj_ioqueue_unregister() |
| 426 | * |
| 427 | * Unregister handle from ioqueue. |
| 428 | */ |
| 429 | PJ_DEF(pj_status_t) pj_ioqueue_unregister( pj_ioqueue_key_t *key) |
| 430 | { |
| 431 | pj_ioqueue_t *ioqueue; |
| 432 | |
| 433 | PJ_ASSERT_RETURN(key, PJ_EINVAL); |
| 434 | |
| 435 | ioqueue = key->ioqueue; |
| 436 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 437 | /* Lock the key to make sure no callback is simultaneously modifying |
| 438 | * the key. We need to lock the key before ioqueue here to prevent |
| 439 | * deadlock. |
| 440 | */ |
| 441 | pj_mutex_lock(key->mutex); |
| 442 | |
| 443 | /* Also lock ioqueue */ |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 444 | pj_lock_acquire(ioqueue->lock); |
| 445 | |
| 446 | pj_assert(ioqueue->count > 0); |
| 447 | --ioqueue->count; |
| 448 | pj_list_erase(key); |
| 449 | PJ_FD_CLR(key->fd, &ioqueue->rfdset); |
| 450 | PJ_FD_CLR(key->fd, &ioqueue->wfdset); |
| 451 | #if PJ_HAS_TCP |
| 452 | PJ_FD_CLR(key->fd, &ioqueue->xfdset); |
| 453 | #endif |
| 454 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 455 | /* Close socket. */ |
| 456 | pj_sock_close(key->fd); |
| 457 | |
| 458 | /* Clear callback */ |
| 459 | key->cb.on_accept_complete = NULL; |
| 460 | key->cb.on_connect_complete = NULL; |
| 461 | key->cb.on_read_complete = NULL; |
| 462 | key->cb.on_write_complete = NULL; |
| 463 | |
| 464 | /* Must release ioqueue lock first before decrementing counter, to |
| 465 | * prevent deadlock. |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 466 | */ |
| 467 | pj_lock_release(ioqueue->lock); |
| 468 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 469 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 470 | /* Mark key is closing. */ |
| 471 | key->closing = 1; |
| 472 | |
| 473 | /* Decrement counter. */ |
| 474 | decrement_counter(key); |
| 475 | |
| 476 | /* Done. */ |
| 477 | pj_mutex_unlock(key->mutex); |
| 478 | #else |
| 479 | pj_mutex_destroy(key->mutex); |
| 480 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 481 | |
| 482 | return PJ_SUCCESS; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | /* This supposed to check whether the fd_set values are consistent |
| 487 | * with the operation currently set in each key. |
| 488 | */ |
| 489 | #if VALIDATE_FD_SET |
| 490 | static void validate_sets(const pj_ioqueue_t *ioqueue, |
| 491 | const pj_fd_set_t *rfdset, |
| 492 | const pj_fd_set_t *wfdset, |
| 493 | const pj_fd_set_t *xfdset) |
| 494 | { |
| 495 | pj_ioqueue_key_t *key; |
| 496 | |
| 497 | /* |
| 498 | * This basicly would not work anymore. |
| 499 | * We need to lock key before performing the check, but we can't do |
| 500 | * so because we're holding ioqueue mutex. If we acquire key's mutex |
| 501 | * now, the will cause deadlock. |
| 502 | */ |
| 503 | pj_assert(0); |
| 504 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 505 | key = ioqueue->active_list.next; |
| 506 | while (key != &ioqueue->active_list) { |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 507 | if (!pj_list_empty(&key->read_list) |
| 508 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0 |
| 509 | || !pj_list_empty(&key->accept_list) |
| 510 | #endif |
| 511 | ) |
| 512 | { |
| 513 | pj_assert(PJ_FD_ISSET(key->fd, rfdset)); |
| 514 | } |
| 515 | else { |
| 516 | pj_assert(PJ_FD_ISSET(key->fd, rfdset) == 0); |
| 517 | } |
| 518 | if (!pj_list_empty(&key->write_list) |
| 519 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0 |
| 520 | || key->connecting |
| 521 | #endif |
| 522 | ) |
| 523 | { |
| 524 | pj_assert(PJ_FD_ISSET(key->fd, wfdset)); |
| 525 | } |
| 526 | else { |
| 527 | pj_assert(PJ_FD_ISSET(key->fd, wfdset) == 0); |
| 528 | } |
| 529 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0 |
| 530 | if (key->connecting) |
| 531 | { |
| 532 | pj_assert(PJ_FD_ISSET(key->fd, xfdset)); |
| 533 | } |
| 534 | else { |
| 535 | pj_assert(PJ_FD_ISSET(key->fd, xfdset) == 0); |
| 536 | } |
| 537 | #endif /* PJ_HAS_TCP */ |
| 538 | |
| 539 | key = key->next; |
| 540 | } |
| 541 | } |
| 542 | #endif /* VALIDATE_FD_SET */ |
| 543 | |
| 544 | |
| 545 | /* ioqueue_remove_from_set() |
| 546 | * This function is called from ioqueue_dispatch_event() to instruct |
| 547 | * the ioqueue to remove the specified descriptor from ioqueue's descriptor |
| 548 | * set for the specified event. |
| 549 | */ |
| 550 | static void ioqueue_remove_from_set( pj_ioqueue_t *ioqueue, |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 551 | pj_ioqueue_key_t *key, |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 552 | enum ioqueue_event_type event_type) |
| 553 | { |
| 554 | pj_lock_acquire(ioqueue->lock); |
| 555 | |
| 556 | if (event_type == READABLE_EVENT) |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 557 | PJ_FD_CLR((pj_sock_t)key->fd, &ioqueue->rfdset); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 558 | else if (event_type == WRITEABLE_EVENT) |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 559 | PJ_FD_CLR((pj_sock_t)key->fd, &ioqueue->wfdset); |
Benny Prijono | 3569c0d | 2007-04-06 10:29:20 +0000 | [diff] [blame] | 560 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0 |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 561 | else if (event_type == EXCEPTION_EVENT) |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 562 | PJ_FD_CLR((pj_sock_t)key->fd, &ioqueue->xfdset); |
Benny Prijono | 3569c0d | 2007-04-06 10:29:20 +0000 | [diff] [blame] | 563 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 564 | else |
| 565 | pj_assert(0); |
| 566 | |
| 567 | pj_lock_release(ioqueue->lock); |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * ioqueue_add_to_set() |
| 572 | * This function is called from pj_ioqueue_recv(), pj_ioqueue_send() etc |
| 573 | * to instruct the ioqueue to add the specified handle to ioqueue's descriptor |
| 574 | * set for the specified event. |
| 575 | */ |
| 576 | static void ioqueue_add_to_set( pj_ioqueue_t *ioqueue, |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 577 | pj_ioqueue_key_t *key, |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 578 | enum ioqueue_event_type event_type ) |
| 579 | { |
| 580 | pj_lock_acquire(ioqueue->lock); |
| 581 | |
| 582 | if (event_type == READABLE_EVENT) |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 583 | PJ_FD_SET((pj_sock_t)key->fd, &ioqueue->rfdset); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 584 | else if (event_type == WRITEABLE_EVENT) |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 585 | PJ_FD_SET((pj_sock_t)key->fd, &ioqueue->wfdset); |
Benny Prijono | 3569c0d | 2007-04-06 10:29:20 +0000 | [diff] [blame] | 586 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0 |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 587 | else if (event_type == EXCEPTION_EVENT) |
Benny Prijono | 63ab356 | 2006-07-08 19:46:43 +0000 | [diff] [blame] | 588 | PJ_FD_SET((pj_sock_t)key->fd, &ioqueue->xfdset); |
Benny Prijono | 3569c0d | 2007-04-06 10:29:20 +0000 | [diff] [blame] | 589 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 590 | else |
| 591 | pj_assert(0); |
| 592 | |
| 593 | pj_lock_release(ioqueue->lock); |
| 594 | } |
| 595 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 596 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 597 | /* Scan closing keys to be put to free list again */ |
| 598 | static void scan_closing_keys(pj_ioqueue_t *ioqueue) |
| 599 | { |
| 600 | pj_time_val now; |
| 601 | pj_ioqueue_key_t *h; |
| 602 | |
| 603 | pj_gettimeofday(&now); |
| 604 | h = ioqueue->closing_list.next; |
| 605 | while (h != &ioqueue->closing_list) { |
| 606 | pj_ioqueue_key_t *next = h->next; |
| 607 | |
| 608 | pj_assert(h->closing != 0); |
| 609 | |
| 610 | if (PJ_TIME_VAL_GTE(now, h->free_time)) { |
| 611 | pj_list_erase(h); |
| 612 | pj_list_push_back(&ioqueue->free_list, h); |
| 613 | } |
| 614 | h = next; |
| 615 | } |
| 616 | } |
| 617 | #endif |
| 618 | |
| 619 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 620 | /* |
| 621 | * pj_ioqueue_poll() |
| 622 | * |
| 623 | * Few things worth written: |
| 624 | * |
| 625 | * - we used to do only one callback called per poll, but it didn't go |
| 626 | * very well. The reason is because on some situation, the write |
| 627 | * callback gets called all the time, thus doesn't give the read |
| 628 | * callback to get called. This happens, for example, when user |
| 629 | * submit write operation inside the write callback. |
| 630 | * As the result, we changed the behaviour so that now multiple |
| 631 | * callbacks are called in a single poll. It should be fast too, |
| 632 | * just that we need to be carefull with the ioqueue data structs. |
| 633 | * |
| 634 | * - to guarantee preemptiveness etc, the poll function must strictly |
| 635 | * work on fd_set copy of the ioqueue (not the original one). |
| 636 | */ |
| 637 | PJ_DEF(int) pj_ioqueue_poll( pj_ioqueue_t *ioqueue, const pj_time_val *timeout) |
| 638 | { |
| 639 | pj_fd_set_t rfdset, wfdset, xfdset; |
| 640 | int count, counter; |
| 641 | pj_ioqueue_key_t *h; |
| 642 | struct event |
| 643 | { |
| 644 | pj_ioqueue_key_t *key; |
| 645 | enum ioqueue_event_type event_type; |
| 646 | } event[PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL]; |
| 647 | |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 648 | PJ_ASSERT_RETURN(ioqueue, -PJ_EINVAL); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 649 | |
| 650 | /* Lock ioqueue before making fd_set copies */ |
| 651 | pj_lock_acquire(ioqueue->lock); |
| 652 | |
| 653 | /* We will only do select() when there are sockets to be polled. |
| 654 | * Otherwise select() will return error. |
| 655 | */ |
| 656 | if (PJ_FD_COUNT(&ioqueue->rfdset)==0 && |
Benny Prijono | 3569c0d | 2007-04-06 10:29:20 +0000 | [diff] [blame] | 657 | PJ_FD_COUNT(&ioqueue->wfdset)==0 |
| 658 | #if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0 |
| 659 | && PJ_FD_COUNT(&ioqueue->xfdset)==0 |
| 660 | #endif |
| 661 | ) |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 662 | { |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 663 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 664 | scan_closing_keys(ioqueue); |
| 665 | #endif |
| 666 | pj_lock_release(ioqueue->lock); |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 667 | TRACE__((THIS_FILE, " poll: no fd is set")); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 668 | if (timeout) |
| 669 | pj_thread_sleep(PJ_TIME_VAL_MSEC(*timeout)); |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | /* Copy ioqueue's pj_fd_set_t to local variables. */ |
| 674 | pj_memcpy(&rfdset, &ioqueue->rfdset, sizeof(pj_fd_set_t)); |
| 675 | pj_memcpy(&wfdset, &ioqueue->wfdset, sizeof(pj_fd_set_t)); |
| 676 | #if PJ_HAS_TCP |
| 677 | pj_memcpy(&xfdset, &ioqueue->xfdset, sizeof(pj_fd_set_t)); |
| 678 | #else |
| 679 | PJ_FD_ZERO(&xfdset); |
| 680 | #endif |
| 681 | |
| 682 | #if VALIDATE_FD_SET |
| 683 | validate_sets(ioqueue, &rfdset, &wfdset, &xfdset); |
| 684 | #endif |
| 685 | |
| 686 | /* Unlock ioqueue before select(). */ |
| 687 | pj_lock_release(ioqueue->lock); |
| 688 | |
Benny Prijono | 42c5b9e | 2006-05-10 19:24:40 +0000 | [diff] [blame] | 689 | count = pj_sock_select(ioqueue->nfds+1, &rfdset, &wfdset, &xfdset, |
| 690 | timeout); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 691 | |
Benny Prijono | 34b0074 | 2008-03-13 21:51:51 +0000 | [diff] [blame] | 692 | if (count == 0) |
| 693 | return 0; |
| 694 | else if (count < 0) |
Benny Prijono | 37e8d33 | 2006-01-20 21:03:36 +0000 | [diff] [blame] | 695 | return -pj_get_netos_error(); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 696 | else if (count > PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL) |
| 697 | count = PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL; |
| 698 | |
| 699 | /* Scan descriptor sets for event and add the events in the event |
| 700 | * array to be processed later in this function. We do this so that |
| 701 | * events can be processed in parallel without holding ioqueue lock. |
| 702 | */ |
| 703 | pj_lock_acquire(ioqueue->lock); |
| 704 | |
| 705 | counter = 0; |
| 706 | |
| 707 | /* Scan for writable sockets first to handle piggy-back data |
| 708 | * coming with accept(). |
| 709 | */ |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 710 | h = ioqueue->active_list.next; |
| 711 | for ( ; h!=&ioqueue->active_list && counter<count; h = h->next) { |
| 712 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 713 | if ( (key_has_pending_write(h) || key_has_pending_connect(h)) |
Benny Prijono | 3059eb6 | 2006-10-04 20:46:27 +0000 | [diff] [blame] | 714 | && PJ_FD_ISSET(h->fd, &wfdset) && !IS_CLOSING(h)) |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 715 | { |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 716 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 717 | increment_counter(h); |
| 718 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 719 | event[counter].key = h; |
| 720 | event[counter].event_type = WRITEABLE_EVENT; |
| 721 | ++counter; |
| 722 | } |
| 723 | |
| 724 | /* Scan for readable socket. */ |
| 725 | if ((key_has_pending_read(h) || key_has_pending_accept(h)) |
Benny Prijono | 3059eb6 | 2006-10-04 20:46:27 +0000 | [diff] [blame] | 726 | && PJ_FD_ISSET(h->fd, &rfdset) && !IS_CLOSING(h)) |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 727 | { |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 728 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 729 | increment_counter(h); |
| 730 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 731 | event[counter].key = h; |
| 732 | event[counter].event_type = READABLE_EVENT; |
| 733 | ++counter; |
| 734 | } |
| 735 | |
| 736 | #if PJ_HAS_TCP |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 737 | if (key_has_pending_connect(h) && PJ_FD_ISSET(h->fd, &xfdset) && |
Benny Prijono | 3059eb6 | 2006-10-04 20:46:27 +0000 | [diff] [blame] | 738 | !IS_CLOSING(h)) |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 739 | { |
| 740 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 741 | increment_counter(h); |
| 742 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 743 | event[counter].key = h; |
| 744 | event[counter].event_type = EXCEPTION_EVENT; |
| 745 | ++counter; |
| 746 | } |
| 747 | #endif |
| 748 | } |
| 749 | |
| 750 | pj_lock_release(ioqueue->lock); |
| 751 | |
| 752 | count = counter; |
| 753 | |
| 754 | /* Now process all events. The dispatch functions will take care |
| 755 | * of locking in each of the key |
| 756 | */ |
| 757 | for (counter=0; counter<count; ++counter) { |
| 758 | switch (event[counter].event_type) { |
| 759 | case READABLE_EVENT: |
| 760 | ioqueue_dispatch_read_event(ioqueue, event[counter].key); |
| 761 | break; |
| 762 | case WRITEABLE_EVENT: |
| 763 | ioqueue_dispatch_write_event(ioqueue, event[counter].key); |
| 764 | break; |
| 765 | case EXCEPTION_EVENT: |
| 766 | ioqueue_dispatch_exception_event(ioqueue, event[counter].key); |
| 767 | break; |
| 768 | case NO_EVENT: |
| 769 | pj_assert(!"Invalid event!"); |
| 770 | break; |
| 771 | } |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 772 | |
| 773 | #if PJ_IOQUEUE_HAS_SAFE_UNREG |
| 774 | decrement_counter(event[counter].key); |
| 775 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 778 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 779 | return count; |
| 780 | } |
| 781 | |