ioqueue.h 42 KB

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