Debian Codegraph @

Xpjproject/pjlib/include/pj/ioqueue.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 #ifndef __PJ_IOQUEUE_H__
20 #define __PJ_IOQUEUE_H__
21
22 /**
23 * @file ioqueue.h
24 * @brief I/O Dispatching Mechanism
25 */
26
27 #include <pj/types.h>
28
29 PJ_BEGIN_DECL
30
31 /**
32 * @defgroup PJ_IO Input/Output
33 * @brief Input/Output
34 * @ingroup PJ_OS
35 *
36 * This section contains API building blocks to perform network I/O and
37 * communications. If provides:
38 * - @ref PJ_SOCK
39 *\n
40 * A highly portable socket abstraction, runs on all kind of
41 * network APIs such as standard BSD socket, Windows socket, Linux
42 * \b kernel socket, PalmOS networking API, etc.
43 *
44 * - @ref pj_addr_resolve
45 *\n
46 * Portable address resolution, which implements #pj_gethostbyname().
47 *
48 * - @ref PJ_SOCK_SELECT
49 *\n
50 * A portable \a select() like API (#pj_sock_select()) which can be
51 * implemented with various back-ends.
52 *
53 * - @ref PJ_IOQUEUE
54 *\n
55 * Framework for dispatching network events.
56 *
57 * For more information see the modules below.
58 */
59
60 /**
61 * @defgroup PJ_IOQUEUE IOQueue: I/O Event Dispatching with Proactor Pattern
62 * @ingroup PJ_IO
63 * @{
64 *
65 * I/O Queue provides API for performing asynchronous I/O operations. It
66 * conforms to proactor pattern, which allows application to submit an
67 * asynchronous operation and to be notified later when the operation has
68 * completed.
69 *
70 * The I/O Queue can work on both socket and file descriptors. For
71 * asynchronous file operations however, one must make sure that the correct
72 * file I/O back-end is used, because not all file I/O back-end can be
73 * used with the ioqueue. Please see \ref PJ_FILE_IO for more details.
74 *
75 * The framework works natively in platforms where asynchronous operation API
76 * exists, such as in Windows NT with IoCompletionPort/IOCP. In other
77 * platforms, the I/O queue abstracts the operating system's event poll API
78 * to provide semantics similar to IoCompletionPort with minimal penalties
79 * (i.e. per ioqueue and per handle mutex protection).
80 *
81 * The I/O queue provides more than just unified abstraction. It also:
82 * - makes sure that the operation uses the most effective way to utilize
83 * the underlying mechanism, to achieve the maximum theoritical
84 * throughput possible on a given platform.
85 * - choose the most efficient mechanism for event polling on a given
86 * platform.
87 *
88 * Currently, the I/O Queue is implemented using:
89 * - <tt><b>select()</b></tt>, as the common denominator, but the least
90 * efficient. Also the number of descriptor is limited to
91 * \c PJ_IOQUEUE_MAX_HANDLES (which by default is 64).
92 * - <tt><b>/dev/epoll</b></tt> on Linux (user mode and kernel mode),
93 * a much faster replacement for select() on Linux (and more importantly
94 * doesn't have limitation on number of descriptors).
95 * - <b>I/O Completion ports</b> on Windows NT/2000/XP, which is the most
96 * efficient way to dispatch events in Windows NT based OSes, and most
97 * importantly, it doesn't have the limit on how many handles to monitor.
98 * And it works with files (not only sockets) as well.
99 *
100 *
101 * \section pj_ioqueue_concurrency_sec Concurrency Rules
102 *
103 * The ioqueue has been fine tuned to allow multiple threads to poll the
104 * handles simultaneously, to maximize scalability when the application is
105 * running on multiprocessor systems. When more than one threads are polling
106 * the ioqueue and there are more than one handles are signaled, more than
107 * one threads will execute the callback simultaneously to serve the events.
108 * These parallel executions are completely safe when the events happen for
109 * two different handles.
110 *
111 * However, with multithreading, care must be taken when multiple events
112 * happen on the same handle, or when event is happening on a handle (and
113 * the callback is being executed) and application is performing
114 * unregistration to the handle at the same time.
115 *
116 * The treatments of above scenario differ according to the concurrency
117 * setting that are applied to the handle.
118 *
119 * \subsection pj_ioq_concur_set Concurrency Settings for Handles
120 *
121 * Concurrency can be set on per handle (key) basis, by using
122 * #pj_ioqueue_set_concurrency() function. The default key concurrency value
123 * for the handle is inherited from the key concurrency setting of the ioqueue,
124 * and the key concurrency setting for the ioqueue can be changed by using
125 * #pj_ioqueue_set_default_concurrency(). The default key concurrency setting
126 * for ioqueue itself is controlled by compile time setting
127 * PJ_IOQUEUE_DEFAULT_ALLOW_CONCURRENCY.
128 *
129 * Note that this key concurrency setting only controls whether multiple
130 * threads are allowed to operate <b>on the same key</b> at the same time.
131 * The ioqueue itself always allows multiple threads to enter the ioqeuue at
132 * the same time, and also simultaneous callback calls to <b>differrent
133 * keys</b> is always allowed regardless to the key concurrency setting.
134 *
135 * \subsection pj_ioq_parallel Parallel Callback Executions for the Same Handle
136 *
137 * Note that when key concurrency is enabled (i.e. parallel callback calls on
138 * the same key is allowed; this is the default setting), the ioqueue will only
139 * perform simultaneous callback executions on the same key when the key has
140 * invoked multiple pending operations. This could be done for example by
141 * calling #pj_ioqueue_recvfrom() more than once on the same key, each with
142 * the same key but different operation key (pj_ioqueue_op_key_t). With this
143 * scenario, when multiple packets arrive on the key at the same time, more
144 * than one threads may execute the callback simultaneously, each with the
145 * same key but different operation key.
146 *
147 * When there is only one pending operation on the key (e.g. there is only one
148 * #pj_ioqueue_recvfrom() invoked on the key), then events occuring to the
149 * same key will be queued by the ioqueue, thus no simultaneous callback calls
150 * will be performed.
151 *
152 * \subsection pj_ioq_allow_concur Concurrency is Enabled (Default Value)
153 *
154 * The default setting for the ioqueue is to allow multiple threads to
155 * execute callbacks for the same handle/key. This setting is selected to
156 * promote good performance and scalability for application.
157 *
158 * However this setting has a major drawback with regard to synchronization,
159 * and application MUST carefully follow the following guidelines to ensure
160 * that parallel access to the key does not cause problems:
161 *
162 * - Always note that callback may be called simultaneously for the same
163 * key.
164 * - <b>Care must be taken when unregistering a key</b> from the
165 * ioqueue. Application must take care that when one thread is issuing
166 * an unregistration, other thread is not simultaneously invoking the
167 * callback <b>to the same key</b>.
168 *\n
169 * This happens because the ioqueue functions are working with a pointer
170 * to the key, and there is a possible race condition where the pointer
171 * has been rendered invalid by other threads before the ioqueue has a
172 * chance to acquire mutex on it.
173 *
174 * \subsection pj_ioq_disallow_concur Concurrency is Disabled
175 *
176 * Alternatively, application may disable key concurrency to make
177 * synchronization easier. As noted above, there are three ways to control
178 * key concurrency setting:
179 * - by controlling on per handle/key basis, with #pj_ioqueue_set_concurrency().
180 * - by changing default key concurrency setting on the ioqueue, with
181 * #pj_ioqueue_set_default_concurrency().
182 * - by changing the default concurrency on compile time, by declaring
183 * PJ_IOQUEUE_DEFAULT_ALLOW_CONCURRENCY macro to zero in your config_site.h
184 *
185 * \section pj_ioqeuue_examples_sec Examples
186 *
187 * For some examples on how to use the I/O Queue, please see:
188 *
189 * - I/O Queue TCP test: \src{pjlib/src/pjlib-test/ioq_tcp.c}
190 * - I/O Queue UDP test: \src{pjlib/src/pjlib-test/ioq_udp.c}
191 * - I/O Queue Performance test: \src{pjlib/src/pjlib-test/ioq_perf.c}
192 */
193
194
195 /**
196 * This structure describes operation specific key to be submitted to
197 * I/O Queue when performing the asynchronous operation. This key will
198 * be returned to the application when completion callback is called.
199 *
200 * Application normally wants to attach it's specific data in the
201 * \c user_data field so that it can keep track of which operation has
202 * completed when the callback is called. Alternatively, application can
203 * also extend this struct to include its data, because the pointer that
204 * is returned in the completion callback will be exactly the same as
205 * the pointer supplied when the asynchronous function is called.
206 */
207 typedef struct pj_ioqueue_op_key_t
208 {
209 void *internal__[32]; /**< Internal I/O Queue data. */
210 void *activesock_data; /**< Active socket data. */
211 void *user_data; /**< Application data. */
212 } pj_ioqueue_op_key_t;
213
214 /**
215 * This structure describes the callbacks to be called when I/O operation
216 * completes.
217 */
218 typedef struct pj_ioqueue_callback
219 {
220 /**
221 * This callback is called when #pj_ioqueue_recv or #pj_ioqueue_recvfrom
222 * completes.
223 *
224 * @param key The key.
225 * @param op_key Operation key.
226 * @param bytes_read >= 0 to indicate the amount of data read,
227 * otherwise negative value containing the error
228 * code. To obtain the pj_status_t error code, use
229 * (pj_status_t code = -bytes_read).
230 */
231 void (*on_read_complete)(pj_ioqueue_key_t *key,
232 pj_ioqueue_op_key_t *op_key,
233 pj_ssize_t bytes_read);
234
235 /**
236 * This callback is called when #pj_ioqueue_send or #pj_ioqueue_sendto
237 * completes.
238 *
239 * @param key The key.
240 * @param op_key Operation key.
241 * @param bytes_sent >= 0 to indicate the amount of data written,
242 * otherwise negative value containing the error
243 * code. To obtain the pj_status_t error code, use
244 * (pj_status_t code = -bytes_sent).
245 */
246 void (*on_write_complete)(pj_ioqueue_key_t *key,
247 pj_ioqueue_op_key_t *op_key,
248 pj_ssize_t bytes_sent);
249
250 /**
251 * This callback is called when #pj_ioqueue_accept completes.
252 *
253 * @param key The key.
254 * @param op_key Operation key.
255 * @param sock Newly connected socket.
256 * @param status Zero if the operation completes successfully.
257 */
258 void (*on_accept_complete)(pj_ioqueue_key_t *key,
259 pj_ioqueue_op_key_t *op_key,
260 pj_sock_t sock,
261 pj_status_t status);
262
263 /**
264 * This callback is called when #pj_ioqueue_connect completes.
265 *
266 * @param key The key.
267 * @param status PJ_SUCCESS if the operation completes successfully.
268 */
269 void (*on_connect_complete)(pj_ioqueue_key_t *key,
270 pj_status_t status);
271 } pj_ioqueue_callback;
272
273
274 /**
275 * Types of pending I/O Queue operation. This enumeration is only used
276 * internally within the ioqueue.
277 */
278 typedef enum pj_ioqueue_operation_e
279 {
280 PJ_IOQUEUE_OP_NONE = 0, /**< No operation. */
281 PJ_IOQUEUE_OP_READ = 1, /**< read() operation. */
282 PJ_IOQUEUE_OP_RECV = 2, /**< recv() operation. */
283 PJ_IOQUEUE_OP_RECV_FROM = 4, /**< recvfrom() operation. */
284 PJ_IOQUEUE_OP_WRITE = 8, /**< write() operation. */
285 PJ_IOQUEUE_OP_SEND = 16, /**< send() operation. */
286 PJ_IOQUEUE_OP_SEND_TO = 32, /**< sendto() operation. */
287 #if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0
288 PJ_IOQUEUE_OP_ACCEPT = 64, /**< accept() operation. */
289 PJ_IOQUEUE_OP_CONNECT = 128 /**< connect() operation. */
290 #endif /* PJ_HAS_TCP */
291 } pj_ioqueue_operation_e;
292
293
294 /**
295 * This macro specifies the maximum number of events that can be
296 * processed by the ioqueue on a single poll cycle, on implementation
297 * that supports it. The value is only meaningfull when specified
298 * during PJLIB build.
299 */
300 #ifndef PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL
301 # define PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL (16)
302 #endif
303
304
305 /**
306 * This macro specifies the maximum event candidates collected by each
307 * polling thread to be able to reach maximum number of processed events
308 * (i.e: PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL) in each poll cycle.
309 * An event candidate will be dispatched to application as event unless
310 * it is already being dispatched by other polling thread. So in order to
311 * anticipate such race condition, each poll operation should collects its
312 * event candidates more than PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL, the
313 * recommended value is (PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL *
314 * number of polling threads).
315 *
316 * The value is only meaningfull when specified during PJLIB build and
317 * is only effective on multiple polling threads environment.
318 */
319 #if !defined(PJ_IOQUEUE_MAX_CAND_EVENTS) || \
320 PJ_IOQUEUE_MAX_CAND_EVENTS < PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL
321 # undef PJ_IOQUEUE_MAX_CAND_EVENTS
322 # define PJ_IOQUEUE_MAX_CAND_EVENTS PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL
323 #endif
324
325
326 /**
327 * When this flag is specified in ioqueue's recv() or send() operations,
328 * the ioqueue will always mark the operation as asynchronous.
329 */
330 #define PJ_IOQUEUE_ALWAYS_ASYNC ((pj_uint32_t)1 << (pj_uint32_t)31)
331
332
333 /**
334 * Epoll flags.
335 */
336 typedef enum pj_ioqueue_epoll_flag
337 {
338 /** Use of EPOLLEXCLUSIVE.
339 */
340 PJ_IOQUEUE_EPOLL_EXCLUSIVE = 1,
341
342 /** Use of EPOLLONESHOT.
343 */
344 PJ_IOQUEUE_EPOLL_ONESHOT = 2,
345
346 /**
347 * Default flag to specify which epoll type to use, which mean to use
348 * EPOLLEXCLUSIVE if available, otherwise EPOLLONESHOT, otherwise "bare"
349 * epoll when neither are available.
350 */
351 PJ_IOQUEUE_EPOLL_AUTO = PJ_IOQUEUE_EPOLL_EXCLUSIVE |
352 PJ_IOQUEUE_EPOLL_ONESHOT,
353
354 } pj_ioqueue_epoll_flag;
355
356
357 /**
358 * Additional settings that can be given during ioqueue creation. Application
359 * MUST initialize this structure with #pj_ioqueue_cfg_default().
360 */
361 typedef struct pj_ioqueue_cfg
362 {
363 /**
364 * Specify flags to control e.g. how events are handled when epoll backend
365 * is used on Linux. The values are combination of pj_ioqueue_epoll_flag.
366 * The default value is PJ_IOQUEUE_DEFAULT_EPOLL_FLAGS, which by default
367 * is set to PJ_IOQUEUE_EPOLL_AUTO. This setting will be ignored for other
368 * ioqueue backends.
369 */
370 unsigned epoll_flags;
371
372 /**
373 * Default concurrency for the handles registered to this ioqueue. Setting
374 * this to non-zero enables a handle to process more than one operations
375 * at the same time using different threads. Default is
376 * PJ_IOQUEUE_DEFAULT_ALLOW_CONCURRENCY. This setting is equivalent to
377 * calling pj_ioqueue_set_default_concurrency() after creating the ioqueue.
378 */
379 pj_bool_t default_concurrency;
380
381 } pj_ioqueue_cfg;
382
383
384 /**
385 * Initialize the ioqueue configuration with the default values.
386 *
387 * @param cfg The configuration to be initialized.
388 */
389 PJ_DECL(void) pj_ioqueue_cfg_default(pj_ioqueue_cfg *cfg);
390
391
392 /**
393 * Return the name of the ioqueue implementation.
394 *
395 * @return Implementation name.
396 */
397 PJ_DECL(const char*) pj_ioqueue_name(void);
398
399
400 /**
401 * Create a new I/O Queue framework.
402 *
403 * @param pool The pool to allocate the I/O queue structure.
404 * @param max_fd The maximum number of handles to be supported, which
405 * should not exceed PJ_IOQUEUE_MAX_HANDLES.
406 * @param ioqueue Pointer to hold the newly created I/O Queue.
407 *
408 * @return PJ_SUCCESS on success.
409 */
410 PJ_DECL(pj_status_t) pj_ioqueue_create( pj_pool_t *pool,
411 pj_size_t max_fd,
412 pj_ioqueue_t **ioqueue);
413
414 /**
415 * Create a new I/O Queue framework.
416 *
417 * @param pool The pool to allocate the I/O queue structure.
418 * @param max_fd The maximum number of handles to be supported, which
419 * should not exceed PJ_IOQUEUE_MAX_HANDLES.
420 * @param cfg Optional ioqueue configuration. Application must
421 * initialize this structure with pj_ioqueue_cfg_default()
422 * first. If this is not specified, default config values
423 * as set pj_ioqueue_cfg_default() by will be used.
424 * @param ioqueue Pointer to hold the newly created I/O Queue.
425 *
426 * @return PJ_SUCCESS on success.
427 */
428 PJ_DECL(pj_status_t) pj_ioqueue_create2( pj_pool_t *pool,
429 pj_size_t max_fd,
430 const pj_ioqueue_cfg *cfg,
431 pj_ioqueue_t **ioqueue);
432
433 /**
434 * Destroy the I/O queue.
435 *
436 * @param ioque The I/O Queue to be destroyed.
437 *
438 * @return PJ_SUCCESS if success.
439 */
440 PJ_DECL(pj_status_t) pj_ioqueue_destroy( pj_ioqueue_t *ioque );
441
442 /**
443 * Set the lock object to be used by the I/O Queue. This function can only
444 * be called right after the I/O queue is created, before any handle is
445 * registered to the I/O queue.
446 *
447 * Initially the I/O queue is created with non-recursive mutex protection.
448 * Applications can supply alternative lock to be used by calling this
449 * function.
450 *
451 * @param ioque The ioqueue instance.
452 * @param lock The lock to be used by the ioqueue.
453 * @param auto_delete In non-zero, the lock will be deleted by the ioqueue.
454 *
455 * @return PJ_SUCCESS or the appropriate error code.
456 */
457 PJ_DECL(pj_status_t) pj_ioqueue_set_lock( pj_ioqueue_t *ioque,
458 pj_lock_t *lock,
459 pj_bool_t auto_delete );
460
461 /**
462 * Set default concurrency policy for this ioqueue. If this function is not
463 * called, the default concurrency policy for the ioqueue is controlled by
464 * compile time setting PJ_IOQUEUE_DEFAULT_ALLOW_CONCURRENCY.
465 *
466 * Note that changing the concurrency setting to the ioqueue will only affect
467 * subsequent key registrations. To modify the concurrency setting for
468 * individual key, use #pj_ioqueue_set_concurrency().
469 *
470 * @param ioqueue The ioqueue instance.
471 * @param allow Non-zero to allow concurrent callback calls, or
472 * PJ_FALSE to disallow it.
473 *
474 * @return PJ_SUCCESS on success or the appropriate error code.
475 */
476 PJ_DECL(pj_status_t) pj_ioqueue_set_default_concurrency(pj_ioqueue_t *ioqueue,
477 pj_bool_t allow);
478
479 /**
480 * Register a socket to the I/O queue framework.
481 * When a socket is registered to the IOQueue, it may be modified to use
482 * non-blocking IO. If it is modified, there is no guarantee that this
483 * modification will be restored after the socket is unregistered.
484 *
485 * @param pool To allocate the resource for the specified handle,
486 * which must be valid until the handle/key is unregistered
487 * from I/O Queue.
488 * @param ioque The I/O Queue.
489 * @param sock The socket.
490 * @param user_data User data to be associated with the key, which can be
491 * retrieved later.
492 * @param cb Callback to be called when I/O operation completes.
493 * @param key Pointer to receive the key to be associated with this
494 * socket. Subsequent I/O queue operation will need this
495 * key.
496 *
497 * @return PJ_SUCCESS on success, or the error code.
498 */
499 PJ_DECL(pj_status_t) pj_ioqueue_register_sock( pj_pool_t *pool,
500 pj_ioqueue_t *ioque,
501 pj_sock_t sock,
502 void *user_data,
503 const pj_ioqueue_callback *cb,
504 pj_ioqueue_key_t **key );
505
506 /**
507 * Variant of pj_ioqueue_register_sock() with additional group lock parameter.
508 * If group lock is set for the key, the key will add the reference counter
509 * when the socket is registered and decrease it when it is destroyed.
510 */
511 PJ_DECL(pj_status_t) pj_ioqueue_register_sock2(pj_pool_t *pool,
512 pj_ioqueue_t *ioque,
513 pj_sock_t sock,
514 pj_grp_lock_t *grp_lock,
515 void *user_data,
516 const pj_ioqueue_callback *cb,
517 pj_ioqueue_key_t **key );
518
519 /**
520 * Unregister from the I/O Queue framework. Caller must make sure that
521 * the key doesn't have any pending operations before calling this function,
522 * by calling #pj_ioqueue_is_pending() for all previously submitted
523 * operations except asynchronous connect, and if necessary call
524 * #pj_ioqueue_post_completion() to cancel the pending operations.
525 *
526 * Note that asynchronous connect operation will automatically be
527 * cancelled during the unregistration.
528 *
529 * Also note that when I/O Completion Port backend is used, application
530 * MUST close the handle immediately after unregistering the key. This is
531 * because there is no unregistering API for IOCP. The only way to
532 * unregister the handle from IOCP is to close the handle.
533 *
534 * @param key The key that was previously obtained from registration.
535 *
536 * @return PJ_SUCCESS on success or the error code.
537 *
538 * @see pj_ioqueue_is_pending
539 */
540 PJ_DECL(pj_status_t) pj_ioqueue_unregister( pj_ioqueue_key_t *key );
541
542
543 /**
544 * Get user data associated with an ioqueue key.
545 *
546 * @param key The key that was previously obtained from registration.
547 *
548 * @return The user data associated with the descriptor, or NULL
549 * on error or if no data is associated with the key during
550 * registration.
551 */
552 PJ_DECL(void*) pj_ioqueue_get_user_data( pj_ioqueue_key_t *key );
553
554 /**
555 * Set or change the user data to be associated with the file descriptor or
556 * handle or socket descriptor.
557 *
558 * @param key The key that was previously obtained from registration.
559 * @param user_data User data to be associated with the descriptor.
560 * @param old_data Optional parameter to retrieve the old user data.
561 *
562 * @return PJ_SUCCESS on success or the error code.
563 */
564 PJ_DECL(pj_status_t) pj_ioqueue_set_user_data( pj_ioqueue_key_t *key,
565 void *user_data,
566 void **old_data);
567
568 /**
569 * Configure whether the ioqueue is allowed to call the key's callback
570 * concurrently/in parallel. The default concurrency setting for the key
571 * is controlled by ioqueue's default concurrency value, which can be
572 * changed by calling #pj_ioqueue_set_default_concurrency().
573 *
574 * If concurrency is allowed for the key, it means that if there are more
575 * than one pending operations complete simultaneously, more than one
576 * threads may call the key's callback at the same time. This generally
577 * would promote good scalability for application, at the expense of more
578 * complexity to manage the concurrent accesses in application's code.
579 *
580 * Alternatively application may disable the concurrent access by
581 * setting the \a allow flag to false. With concurrency disabled, only
582 * one thread can call the key's callback at one time.
583 *
584 * @param key The key that was previously obtained from registration.
585 * @param allow Set this to non-zero to allow concurrent callback calls
586 * and zero (PJ_FALSE) to disallow it.
587 *
588 * @return PJ_SUCCESS on success or the appropriate error code.
589 */
590 PJ_DECL(pj_status_t) pj_ioqueue_set_concurrency(pj_ioqueue_key_t *key,
591 pj_bool_t allow);
592
593 /**
594 * Acquire the key's mutex. When the key's concurrency is disabled,
595 * application may call this function to synchronize its operation
596 * with the key's callback (i.e. this function will block until the
597 * key's callback returns).
598 *
599 * @param key The key that was previously obtained from registration.
600 *
601 * @return PJ_SUCCESS on success or the appropriate error code.
602 */
603 PJ_DECL(pj_status_t) pj_ioqueue_lock_key(pj_ioqueue_key_t *key);
604
605 /**
606 * Try to acquire the key's mutex. When the key's concurrency is disabled,
607 * application may call this function to synchronize its operation
608 * with the key's callback.
609 *
610 * @param key The key that was previously obtained from registration.
611 *
612 * @return PJ_SUCCESS on success or the appropriate error code.
613 */
614 PJ_DECL(pj_status_t) pj_ioqueue_trylock_key(pj_ioqueue_key_t *key);
615
616 /**
617 * Release the lock previously acquired with pj_ioqueue_lock_key().
618 *
619 * @param key The key that was previously obtained from registration.
620 *
621 * @return PJ_SUCCESS on success or the appropriate error code.
622 */
623 PJ_DECL(pj_status_t) pj_ioqueue_unlock_key(pj_ioqueue_key_t *key);
624
625 /**
626 * Initialize operation key.
627 *
628 * @param op_key The operation key to be initialied.
629 * @param size The size of the operation key.
630 */
631 PJ_DECL(void) pj_ioqueue_op_key_init( pj_ioqueue_op_key_t *op_key,
632 pj_size_t size );
633
634 /**
635 * Check if operation is pending on the specified operation key.
636 * The \c op_key must have been initialized with #pj_ioqueue_op_key_init()
637 * or submitted as pending operation before, or otherwise the result
638 * is undefined.
639 *
640 * @param key The key.
641 * @param op_key The operation key, previously submitted to any of
642 * the I/O functions and has returned PJ_EPENDING.
643 *
644 * @return Non-zero if operation is still pending.
645 */
646 PJ_DECL(pj_bool_t) pj_ioqueue_is_pending( pj_ioqueue_key_t *key,
647 pj_ioqueue_op_key_t *op_key );
648
649
650 /**
651 * Post completion status to the specified operation key and call the
652 * appropriate callback. When the callback is called, the number of bytes
653 * received in read/write callback or the status in accept/connect callback
654 * will be set from the \c bytes_status parameter.
655 *
656 * @param key The key.
657 * @param op_key Pending operation key.
658 * @param bytes_status Number of bytes or status to be set. A good value
659 * to put here is -PJ_ECANCELLED.
660 *
661 * @return PJ_SUCCESS if completion status has been successfully
662 * sent.
663 */
664 PJ_DECL(pj_status_t) pj_ioqueue_post_completion( pj_ioqueue_key_t *key,
665 pj_ioqueue_op_key_t *op_key,
666 pj_ssize_t bytes_status );
667
668
669 /**
670 * Clear ioqueue key states. This function will cancel any outstanding
671 * operations on that key, without invoking any completion callback.
672 * After calling this function, application should reinit its all operation
673 * keys, i.e: using pj_ioqueue_op_key_init(), before reusing them.
674 *
675 * @param key The key.
676 *
677 * @return PJ_SUCCESS on success or the appropriate error code.
678 */
679 PJ_DECL(pj_status_t) pj_ioqueue_clear_key( pj_ioqueue_key_t *key );
680
681
682
683 #if defined(PJ_HAS_TCP) && PJ_HAS_TCP != 0
684 /**
685 * Instruct I/O Queue to accept incoming connection on the specified
686 * listening socket. This function will return immediately (i.e. non-blocking)
687 * regardless whether a connection is immediately available. If the function
688 * can't complete immediately, the caller will be notified about the incoming
689 * connection when it calls pj_ioqueue_poll(). If a new connection is
690 * immediately available, the function returns PJ_SUCCESS with the new
691 * connection; in this case, the callback WILL NOT be called.
692 *
693 * @param key The key which registered to the server socket.
694 * @param op_key An operation specific key to be associated with the
695 * pending operation, so that application can keep track of
696 * which operation has been completed when the callback is
697 * called.
698 * @param new_sock Argument which contain pointer to receive the new socket
699 * for the incoming connection.
700 * @param local Optional argument which contain pointer to variable to
701 * receive local address.
702 * @param remote Optional argument which contain pointer to variable to
703 * receive the remote address.
704 * @param addrlen On input, contains the length of the buffer for the
705 * address, and on output, contains the actual length of the
706 * address. This argument is optional.
707 * @return
708 * - PJ_SUCCESS When connection is available immediately, and the
709 * parameters will be updated to contain information about
710 * the new connection. In this case, a completion callback
711 * WILL NOT be called.
712 * - PJ_EPENDING If no connection is available immediately. When a new
713 * connection arrives, the callback will be called.
714 * - non-zero which indicates the appropriate error code.
715 */
716 PJ_DECL(pj_status_t) pj_ioqueue_accept( pj_ioqueue_key_t *key,
717 pj_ioqueue_op_key_t *op_key,
718 pj_sock_t *new_sock,
719 pj_sockaddr_t *local,
720 pj_sockaddr_t *remote,
721 int *addrlen );
722
723 /**
724 * Initiate non-blocking socket connect. If the socket can NOT be connected
725 * immediately, asynchronous connect() will be scheduled and caller will be
726 * notified via completion callback when it calls pj_ioqueue_poll(). If
727 * socket is connected immediately, the function returns PJ_SUCCESS and
728 * completion callback WILL NOT be called.
729 *
730 * @param key The key associated with TCP socket
731 * @param addr The remote address.
732 * @param addrlen The remote address length.
733 *
734 * @return
735 * - PJ_SUCCESS If socket is connected immediately. In this case, the
736 * completion callback WILL NOT be called.
737 * - PJ_EPENDING If operation is queued, or
738 * - non-zero Indicates the error code.
739 */
740 PJ_DECL(pj_status_t) pj_ioqueue_connect( pj_ioqueue_key_t *key,
741 const pj_sockaddr_t *addr,
742 int addrlen );
743
744 #endif /* PJ_HAS_TCP */
745
746 /**
747 * Poll the I/O Queue for completed events.
748 *
749 * Note: polling the ioqueue is not necessary in Symbian. Please see
750 * @ref PJ_SYMBIAN_OS for more info.
751 *
752 * @param ioque the I/O Queue.
753 * @param timeout polling timeout, or NULL if the thread wishes to wait
754 * indefinetely for the event.
755 *
756 * @return
757 * - zero if timed out (no event).
758 * - (<0) if error occured during polling. Callback will NOT be called.
759 * - (>1) to indicate numbers of events. Callbacks have been called.
760 */
761 PJ_DECL(int) pj_ioqueue_poll( pj_ioqueue_t *ioque,
762 const pj_time_val *timeout);
763
764
765 /**
766 * Instruct the I/O Queue to read from the specified handle. This function
767 * returns immediately (i.e. non-blocking) regardless whether some data has
768 * been transferred. If the operation can't complete immediately, caller will
769 * be notified about the completion when it calls pj_ioqueue_poll(). If data
770 * is immediately available, the function will return PJ_SUCCESS and the
771 * callback WILL NOT be called.
772 *
773 * @param key The key that uniquely identifies the handle.
774 * @param op_key An operation specific key to be associated with the
775 * pending operation, so that application can keep track of
776 * which operation has been completed when the callback is
777 * called. Caller must make sure that this key remains
778 * valid until the function completes.
779 * @param buffer The buffer to hold the read data. The caller MUST make sure
780 * that this buffer remain valid until the framework completes
781 * reading the handle.
782 * @param length On input, it specifies the size of the buffer. If data is
783 * available to be read immediately, the function returns
784 * PJ_SUCCESS and this argument will be filled with the
785 * amount of data read. If the function is pending, caller
786 * will be notified about the amount of data read in the
787 * callback. This parameter can point to local variable in
788 * caller's stack and doesn't have to remain valid for the
789 * duration of pending operation.
790 * @param flags Recv flag. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
791 * the function will never return PJ_SUCCESS.
792 *
793 * @return
794 * - PJ_SUCCESS If immediate data has been received in the buffer. In this
795 * case, the callback WILL NOT be called.
796 * - PJ_EPENDING If the operation has been queued, and the callback will be
797 * called when data has been received.
798 * - non-zero The return value indicates the error code.
799 */
800 PJ_DECL(pj_status_t) pj_ioqueue_recv( pj_ioqueue_key_t *key,
801 pj_ioqueue_op_key_t *op_key,
802 void *buffer,
803 pj_ssize_t *length,
804 pj_uint32_t flags );
805
806 /**
807 * This function behaves similarly as #pj_ioqueue_recv(), except that it is
808 * normally called for socket, and the remote address will also be returned
809 * along with the data. Caller MUST make sure that both buffer and addr
810 * remain valid until the framework completes reading the data.
811 *
812 * @param key The key that uniquely identifies the handle.
813 * @param op_key An operation specific key to be associated with the
814 * pending operation, so that application can keep track of
815 * which operation has been completed when the callback is
816 * called.
817 * @param buffer The buffer to hold the read data. The caller MUST make sure
818 * that this buffer remain valid until the framework completes
819 * reading the handle.
820 * @param length On input, it specifies the size of the buffer. If data is
821 * available to be read immediately, the function returns
822 * PJ_SUCCESS and this argument will be filled with the
823 * amount of data read. If the function is pending, caller
824 * will be notified about the amount of data read in the
825 * callback. This parameter can point to local variable in
826 * caller's stack and doesn't have to remain valid for the
827 * duration of pending operation.
828 * @param flags Recv flag. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
829 * the function will never return PJ_SUCCESS.
830 * @param addr Optional Pointer to buffer to receive the address.
831 * @param addrlen On input, specifies the length of the address buffer.
832 * On output, it will be filled with the actual length of
833 * the address. This argument can be NULL if \c addr is not
834 * specified.
835 *
836 * @return
837 * - PJ_SUCCESS If immediate data has been received. In this case, the
838 * callback must have been called before this function
839 * returns, and no pending operation is scheduled.
840 * - PJ_EPENDING If the operation has been queued.
841 * - non-zero The return value indicates the error code.
842 */
843 PJ_DECL(pj_status_t) pj_ioqueue_recvfrom( pj_ioqueue_key_t *key,
844 pj_ioqueue_op_key_t *op_key,
845 void *buffer,
846 pj_ssize_t *length,
847 pj_uint32_t flags,
848 pj_sockaddr_t *addr,
849 int *addrlen);
850
851 /**
852 * Instruct the I/O Queue to write to the handle. This function will return
853 * immediately (i.e. non-blocking) regardless whether some data has been
854 * transferred. If the function can't complete immediately, the caller will
855 * be notified about the completion when it calls pj_ioqueue_poll(). If
856 * operation completes immediately and data has been transferred, the function
857 * returns PJ_SUCCESS and the callback will NOT be called.
858 *
859 * @param key The key that identifies the handle.
860 * @param op_key An operation specific key to be associated with the
861 * pending operation, so that application can keep track of
862 * which operation has been completed when the callback is
863 * called.
864 * @param data The data to send. Caller MUST make sure that this buffer
865 * remains valid until the write operation completes.
866 * @param length On input, it specifies the length of data to send. When
867 * data was sent immediately, this function returns PJ_SUCCESS
868 * and this parameter contains the length of data sent. If
869 * data can not be sent immediately, an asynchronous operation
870 * is scheduled and caller will be notified via callback the
871 * number of bytes sent. This parameter can point to local
872 * variable on caller's stack and doesn't have to remain
873 * valid until the operation has completed.
874 * @param flags Send flags. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
875 * the function will never return PJ_SUCCESS.
876 *
877 * @return
878 * - PJ_SUCCESS If data was immediately transferred. In this case, no
879 * pending operation has been scheduled and the callback
880 * WILL NOT be called.
881 * - PJ_EPENDING If the operation has been queued. Once data base been
882 * transferred, the callback will be called.
883 * - non-zero The return value indicates the error code.
884 */
885 PJ_DECL(pj_status_t) pj_ioqueue_send( pj_ioqueue_key_t *key,
886 pj_ioqueue_op_key_t *op_key,
887 const void *data,
888 pj_ssize_t *length,
889 pj_uint32_t flags );
890
891
892 /**
893 * Instruct the I/O Queue to write to the handle. This function will return
894 * immediately (i.e. non-blocking) regardless whether some data has been
895 * transferred. If the function can't complete immediately, the caller will
896 * be notified about the completion when it calls pj_ioqueue_poll(). If
897 * operation completes immediately and data has been transferred, the function
898 * returns PJ_SUCCESS and the callback will NOT be called.
899 *
900 * @param key the key that identifies the handle.
901 * @param op_key An operation specific key to be associated with the
902 * pending operation, so that application can keep track of
903 * which operation has been completed when the callback is
904 * called.
905 * @param data the data to send. Caller MUST make sure that this buffer
906 * remains valid until the write operation completes.
907 * @param length On input, it specifies the length of data to send. When
908 * data was sent immediately, this function returns PJ_SUCCESS
909 * and this parameter contains the length of data sent. If
910 * data can not be sent immediately, an asynchronous operation
911 * is scheduled and caller will be notified via callback the
912 * number of bytes sent. This parameter can point to local
913 * variable on caller's stack and doesn't have to remain
914 * valid until the operation has completed.
915 * @param flags send flags. If flags has PJ_IOQUEUE_ALWAYS_ASYNC then
916 * the function will never return PJ_SUCCESS.
917 * @param addr Optional remote address.
918 * @param addrlen Remote address length, \c addr is specified.
919 *
920 * @return
921 * - PJ_SUCCESS If data was immediately written.
922 * - PJ_EPENDING If the operation has been queued.
923 * - non-zero The return value indicates the error code.
924 */
925 PJ_DECL(pj_status_t) pj_ioqueue_sendto( pj_ioqueue_key_t *key,
926 pj_ioqueue_op_key_t *op_key,
927 const void *data,
928 pj_ssize_t *length,
929 pj_uint32_t flags,
930 const pj_sockaddr_t *addr,
931 int addrlen);
932
933
934 /**
935 * Get the underlying OS handle associated with an ioqueue instance.
936 *
937 * @param ioqueue The ioqueue instance.
938 *
939 * @return The OS handle associated with the instance.
940 * For epoll/kqueue this will be a pointer to the file
941 * descriptor. For all other platforms, this will be a pointer
942 * to a platform-specific handle.
943 * If no handle is available, NULL will be returned.
944 */
945 PJ_DECL(pj_oshandle_t) pj_ioqueue_get_os_handle( pj_ioqueue_t *ioqueue );
946
947
948 /**
949 * @}
950 */
951
952 PJ_END_DECL
953
954 #endif /* __PJ_IOQUEUE_H__ */
955