apr_poll.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef APR_POLL_H
  17. #define APR_POLL_H
  18. /**
  19. * @file apr_poll.h
  20. * @brief APR Poll interface
  21. */
  22. #include "apr.h"
  23. #include "apr_pools.h"
  24. #include "apr_errno.h"
  25. #include "apr_inherit.h"
  26. #include "apr_file_io.h"
  27. #include "apr_network_io.h"
  28. #if APR_HAVE_NETINET_IN_H
  29. #include <netinet/in.h>
  30. #endif
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif /* __cplusplus */
  34. /**
  35. * @defgroup apr_poll Poll Routines
  36. * @ingroup APR
  37. * @{
  38. */
  39. /**
  40. * @defgroup pollopts Poll options
  41. * @ingroup apr_poll
  42. * @{
  43. */
  44. #define APR_POLLIN 0x001 /**< Can read without blocking */
  45. #define APR_POLLPRI 0x002 /**< Priority data available */
  46. #define APR_POLLOUT 0x004 /**< Can write without blocking */
  47. #define APR_POLLERR 0x010 /**< Pending error */
  48. #define APR_POLLHUP 0x020 /**< Hangup occurred */
  49. #define APR_POLLNVAL 0x040 /**< Descriptor invalid */
  50. /** @} */
  51. /**
  52. * @defgroup pollflags Pollset Flags
  53. * @ingroup apr_poll
  54. * @{
  55. */
  56. #define APR_POLLSET_THREADSAFE 0x001 /**< Adding or removing a descriptor is
  57. * thread-safe
  58. */
  59. #define APR_POLLSET_NOCOPY 0x002 /**< Descriptors passed to apr_pollset_add()
  60. * are not copied
  61. */
  62. #define APR_POLLSET_WAKEABLE 0x004 /**< Poll operations are interruptable by
  63. * apr_pollset_wakeup() or apr_pollcb_wakeup()
  64. */
  65. #define APR_POLLSET_NODEFAULT 0x010 /**< Do not try to use the default method if
  66. * the specified non-default method cannot be
  67. * used
  68. */
  69. /** @} */
  70. /**
  71. * Pollset Methods
  72. */
  73. typedef enum {
  74. APR_POLLSET_DEFAULT, /**< Platform default poll method */
  75. APR_POLLSET_SELECT, /**< Poll uses select method */
  76. APR_POLLSET_KQUEUE, /**< Poll uses kqueue method */
  77. APR_POLLSET_PORT, /**< Poll uses Solaris event port method */
  78. APR_POLLSET_EPOLL, /**< Poll uses epoll method */
  79. APR_POLLSET_POLL, /**< Poll uses poll method */
  80. APR_POLLSET_AIO_MSGQ /**< Poll uses z/OS asio method */
  81. } apr_pollset_method_e;
  82. /** Used in apr_pollfd_t to determine what the apr_descriptor is */
  83. typedef enum {
  84. APR_NO_DESC, /**< nothing here */
  85. APR_POLL_SOCKET, /**< descriptor refers to a socket */
  86. APR_POLL_FILE, /**< descriptor refers to a file */
  87. APR_POLL_LASTDESC /**< @deprecated descriptor is the last one in the list */
  88. } apr_datatype_e ;
  89. /** Union of either an APR file or socket. */
  90. typedef union {
  91. apr_file_t *f; /**< file */
  92. apr_socket_t *s; /**< socket */
  93. } apr_descriptor;
  94. /** @see apr_pollfd_t */
  95. typedef struct apr_pollfd_t apr_pollfd_t;
  96. /** Poll descriptor set. */
  97. struct apr_pollfd_t {
  98. apr_pool_t *p; /**< associated pool */
  99. apr_datatype_e desc_type; /**< descriptor type */
  100. apr_int16_t reqevents; /**< requested events */
  101. apr_int16_t rtnevents; /**< returned events */
  102. apr_descriptor desc; /**< @see apr_descriptor */
  103. void *client_data; /**< allows app to associate context */
  104. };
  105. /* General-purpose poll API for arbitrarily large numbers of
  106. * file descriptors
  107. */
  108. /** Opaque structure used for pollset API */
  109. typedef struct apr_pollset_t apr_pollset_t;
  110. /**
  111. * Set up a pollset object
  112. * @param pollset The pointer in which to return the newly created object
  113. * @param size The maximum number of descriptors that this pollset can hold
  114. * @param p The pool from which to allocate the pollset
  115. * @param flags Optional flags to modify the operation of the pollset.
  116. *
  117. * @remark If flags contains APR_POLLSET_THREADSAFE, then a pollset is
  118. * created on which it is safe to make concurrent calls to
  119. * apr_pollset_add(), apr_pollset_remove() and apr_pollset_poll()
  120. * from separate threads. This feature is only supported on some
  121. * platforms; the apr_pollset_create() call will fail with
  122. * APR_ENOTIMPL on platforms where it is not supported.
  123. * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollset is
  124. * created with an additional internal pipe object used for the
  125. * apr_pollset_wakeup() call. The actual size of pollset is
  126. * in that case @a size + 1. This feature is only supported on some
  127. * platforms; the apr_pollset_create() call will fail with
  128. * APR_ENOTIMPL on platforms where it is not supported.
  129. * @remark If flags contains APR_POLLSET_NOCOPY, then the apr_pollfd_t
  130. * structures passed to apr_pollset_add() are not copied and
  131. * must have a lifetime at least as long as the pollset.
  132. * @remark Some poll methods (including APR_POLLSET_KQUEUE,
  133. * APR_POLLSET_PORT, and APR_POLLSET_EPOLL) do not have a
  134. * fixed limit on the size of the pollset. For these methods,
  135. * the size parameter controls the maximum number of
  136. * descriptors that will be returned by a single call to
  137. * apr_pollset_poll().
  138. */
  139. APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
  140. apr_uint32_t size,
  141. apr_pool_t *p,
  142. apr_uint32_t flags);
  143. /**
  144. * Set up a pollset object
  145. * @param pollset The pointer in which to return the newly created object
  146. * @param size The maximum number of descriptors that this pollset can hold
  147. * @param p The pool from which to allocate the pollset
  148. * @param flags Optional flags to modify the operation of the pollset.
  149. * @param method Poll method to use. See #apr_pollset_method_e. If this
  150. * method cannot be used, the default method will be used unless the
  151. * APR_POLLSET_NODEFAULT flag has been specified.
  152. *
  153. * @remark If flags contains APR_POLLSET_THREADSAFE, then a pollset is
  154. * created on which it is safe to make concurrent calls to
  155. * apr_pollset_add(), apr_pollset_remove() and apr_pollset_poll()
  156. * from separate threads. This feature is only supported on some
  157. * platforms; the apr_pollset_create_ex() call will fail with
  158. * APR_ENOTIMPL on platforms where it is not supported.
  159. * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollset is
  160. * created with additional internal pipe object used for the
  161. * apr_pollset_wakeup() call. The actual size of pollset is
  162. * in that case size + 1. This feature is only supported on some
  163. * platforms; the apr_pollset_create_ex() call will fail with
  164. * APR_ENOTIMPL on platforms where it is not supported.
  165. * @remark If flags contains APR_POLLSET_NOCOPY, then the apr_pollfd_t
  166. * structures passed to apr_pollset_add() are not copied and
  167. * must have a lifetime at least as long as the pollset.
  168. * @remark Some poll methods (including APR_POLLSET_KQUEUE,
  169. * APR_POLLSET_PORT, and APR_POLLSET_EPOLL) do not have a
  170. * fixed limit on the size of the pollset. For these methods,
  171. * the size parameter controls the maximum number of
  172. * descriptors that will be returned by a single call to
  173. * apr_pollset_poll().
  174. */
  175. APR_DECLARE(apr_status_t) apr_pollset_create_ex(apr_pollset_t **pollset,
  176. apr_uint32_t size,
  177. apr_pool_t *p,
  178. apr_uint32_t flags,
  179. apr_pollset_method_e method);
  180. /**
  181. * Destroy a pollset object
  182. * @param pollset The pollset to destroy
  183. */
  184. APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset);
  185. /**
  186. * Add a socket or file descriptor to a pollset
  187. * @param pollset The pollset to which to add the descriptor
  188. * @param descriptor The descriptor to add
  189. * @remark If you set client_data in the descriptor, that value
  190. * will be returned in the client_data field whenever this
  191. * descriptor is signalled in apr_pollset_poll().
  192. * @remark If the pollset has been created with APR_POLLSET_THREADSAFE
  193. * and thread T1 is blocked in a call to apr_pollset_poll() for
  194. * this same pollset that is being modified via apr_pollset_add()
  195. * in thread T2, the currently executing apr_pollset_poll() call in
  196. * T1 will either: (1) automatically include the newly added descriptor
  197. * in the set of descriptors it is watching or (2) return immediately
  198. * with APR_EINTR. Option (1) is recommended, but option (2) is
  199. * allowed for implementations where option (1) is impossible
  200. * or impractical.
  201. * @remark If the pollset has been created with APR_POLLSET_NOCOPY, the
  202. * apr_pollfd_t structure referenced by descriptor will not be copied
  203. * and must have a lifetime at least as long as the pollset.
  204. * @remark Do not add the same socket or file descriptor to the same pollset
  205. * multiple times, even if the requested events differ for the
  206. * different calls to apr_pollset_add(). If the events of interest
  207. * for a descriptor change, you must first remove the descriptor
  208. * from the pollset with apr_pollset_remove(), then add it again
  209. * specifying all requested events.
  210. */
  211. APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
  212. const apr_pollfd_t *descriptor);
  213. /**
  214. * Remove a descriptor from a pollset
  215. * @param pollset The pollset from which to remove the descriptor
  216. * @param descriptor The descriptor to remove
  217. * @remark If the descriptor is not found, APR_NOTFOUND is returned.
  218. * @remark If the pollset has been created with APR_POLLSET_THREADSAFE
  219. * and thread T1 is blocked in a call to apr_pollset_poll() for
  220. * this same pollset that is being modified via apr_pollset_remove()
  221. * in thread T2, the currently executing apr_pollset_poll() call in
  222. * T1 will either: (1) automatically exclude the newly added descriptor
  223. * in the set of descriptors it is watching or (2) return immediately
  224. * with APR_EINTR. Option (1) is recommended, but option (2) is
  225. * allowed for implementations where option (1) is impossible
  226. * or impractical.
  227. * @remark apr_pollset_remove() cannot be used to remove a subset of requested
  228. * events for a descriptor. The reqevents field in the apr_pollfd_t
  229. * parameter must contain the same value when removing as when adding.
  230. */
  231. APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
  232. const apr_pollfd_t *descriptor);
  233. /**
  234. * Block for activity on the descriptor(s) in a pollset
  235. * @param pollset The pollset to use
  236. * @param timeout The amount of time in microseconds to wait. This is a
  237. * maximum, not a minimum. If a descriptor is signalled, the
  238. * function will return before this time. If timeout is
  239. * negative, the function will block until a descriptor is
  240. * signalled or until apr_pollset_wakeup() has been called.
  241. * @param num Number of signalled descriptors (output parameter)
  242. * @param descriptors Array of signalled descriptors (output parameter)
  243. * @remark APR_EINTR will be returned if the pollset has been created with
  244. * APR_POLLSET_WAKEABLE, apr_pollset_wakeup() has been called while
  245. * waiting for activity, and there were no signalled descriptors at the
  246. * time of the wakeup call.
  247. * @remark Multiple signalled conditions for the same descriptor may be reported
  248. * in one or more returned apr_pollfd_t structures, depending on the
  249. * implementation.
  250. */
  251. APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
  252. apr_interval_time_t timeout,
  253. apr_int32_t *num,
  254. const apr_pollfd_t **descriptors);
  255. /**
  256. * Interrupt the blocked apr_pollset_poll() call.
  257. * @param pollset The pollset to use
  258. * @remark If the pollset was not created with APR_POLLSET_WAKEABLE the
  259. * return value is APR_EINIT.
  260. */
  261. APR_DECLARE(apr_status_t) apr_pollset_wakeup(apr_pollset_t *pollset);
  262. /**
  263. * Poll the descriptors in the poll structure
  264. * @param aprset The poll structure we will be using.
  265. * @param numsock The number of descriptors we are polling
  266. * @param nsds The number of descriptors signalled (output parameter)
  267. * @param timeout The amount of time in microseconds to wait. This is a
  268. * maximum, not a minimum. If a descriptor is signalled, the
  269. * function will return before this time. If timeout is
  270. * negative, the function will block until a descriptor is
  271. * signalled or until apr_pollset_wakeup() has been called.
  272. * @remark The number of descriptors signalled is returned in the third argument.
  273. * This is a blocking call, and it will not return until either a
  274. * descriptor has been signalled or the timeout has expired.
  275. * @remark The rtnevents field in the apr_pollfd_t array will only be filled-
  276. * in if the return value is APR_SUCCESS.
  277. */
  278. APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t numsock,
  279. apr_int32_t *nsds,
  280. apr_interval_time_t timeout);
  281. /**
  282. * Return a printable representation of the pollset method.
  283. * @param pollset The pollset to use
  284. */
  285. APR_DECLARE(const char *) apr_pollset_method_name(apr_pollset_t *pollset);
  286. /**
  287. * Return a printable representation of the default pollset method
  288. * (APR_POLLSET_DEFAULT).
  289. */
  290. APR_DECLARE(const char *) apr_poll_method_defname(void);
  291. /** Opaque structure used for pollcb API */
  292. typedef struct apr_pollcb_t apr_pollcb_t;
  293. /**
  294. * Set up a pollcb object
  295. * @param pollcb The pointer in which to return the newly created object
  296. * @param size The maximum number of descriptors that a single _poll can return.
  297. * @param p The pool from which to allocate the pollcb
  298. * @param flags Optional flags to modify the operation of the pollcb.
  299. *
  300. * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollcb is
  301. * created with an additional internal pipe object used for the
  302. * apr_pollcb_wakeup() call. The actual size of pollcb is
  303. * in that case @a size + 1.
  304. * @remark Pollcb is only supported on some platforms; the apr_pollcb_create()
  305. * call will fail with APR_ENOTIMPL on platforms where it is not supported.
  306. */
  307. APR_DECLARE(apr_status_t) apr_pollcb_create(apr_pollcb_t **pollcb,
  308. apr_uint32_t size,
  309. apr_pool_t *p,
  310. apr_uint32_t flags);
  311. /**
  312. * Set up a pollcb object
  313. * @param pollcb The pointer in which to return the newly created object
  314. * @param size The maximum number of descriptors that a single _poll can return.
  315. * @param p The pool from which to allocate the pollcb
  316. * @param flags Optional flags to modify the operation of the pollcb.
  317. * @param method Poll method to use. See #apr_pollset_method_e. If this
  318. * method cannot be used, the default method will be used unless the
  319. * APR_POLLSET_NODEFAULT flag has been specified.
  320. *
  321. * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollcb is
  322. * created with an additional internal pipe object used for the
  323. * apr_pollcb_wakeup() call. The actual size of pollcb is
  324. * in that case @a size + 1.
  325. * @remark Pollcb is only supported on some platforms; the apr_pollcb_create_ex()
  326. * call will fail with APR_ENOTIMPL on platforms where it is not supported.
  327. */
  328. APR_DECLARE(apr_status_t) apr_pollcb_create_ex(apr_pollcb_t **pollcb,
  329. apr_uint32_t size,
  330. apr_pool_t *p,
  331. apr_uint32_t flags,
  332. apr_pollset_method_e method);
  333. /**
  334. * Add a socket or file descriptor to a pollcb
  335. * @param pollcb The pollcb to which to add the descriptor
  336. * @param descriptor The descriptor to add
  337. * @remark If you set client_data in the descriptor, that value will be
  338. * returned in the client_data field whenever this descriptor is
  339. * signalled in apr_pollcb_poll().
  340. * @remark Unlike the apr_pollset API, the descriptor is not copied, and users
  341. * must retain the memory used by descriptor, as the same pointer will
  342. * be returned to them from apr_pollcb_poll.
  343. * @remark Do not add the same socket or file descriptor to the same pollcb
  344. * multiple times, even if the requested events differ for the
  345. * different calls to apr_pollcb_add(). If the events of interest
  346. * for a descriptor change, you must first remove the descriptor
  347. * from the pollcb with apr_pollcb_remove(), then add it again
  348. * specifying all requested events.
  349. */
  350. APR_DECLARE(apr_status_t) apr_pollcb_add(apr_pollcb_t *pollcb,
  351. apr_pollfd_t *descriptor);
  352. /**
  353. * Remove a descriptor from a pollcb
  354. * @param pollcb The pollcb from which to remove the descriptor
  355. * @param descriptor The descriptor to remove
  356. * @remark If the descriptor is not found, APR_NOTFOUND is returned.
  357. * @remark apr_pollcb_remove() cannot be used to remove a subset of requested
  358. * events for a descriptor. The reqevents field in the apr_pollfd_t
  359. * parameter must contain the same value when removing as when adding.
  360. */
  361. APR_DECLARE(apr_status_t) apr_pollcb_remove(apr_pollcb_t *pollcb,
  362. apr_pollfd_t *descriptor);
  363. /**
  364. * Function prototype for pollcb handlers
  365. * @param baton Opaque baton passed into apr_pollcb_poll()
  366. * @param descriptor Contains the notification for an active descriptor.
  367. * The @a rtnevents member describes which events were triggered
  368. * for this descriptor.
  369. * @remark If the pollcb handler does not return APR_SUCCESS, the apr_pollcb_poll()
  370. * call returns with the handler's return value.
  371. */
  372. typedef apr_status_t (*apr_pollcb_cb_t)(void *baton, apr_pollfd_t *descriptor);
  373. /**
  374. * Block for activity on the descriptor(s) in a pollcb
  375. * @param pollcb The pollcb to use
  376. * @param timeout The amount of time in microseconds to wait. This is a
  377. * maximum, not a minimum. If a descriptor is signalled, the
  378. * function will return before this time. If timeout is
  379. * negative, the function will block until a descriptor is
  380. * signalled or until apr_pollcb_wakeup() has been called.
  381. * @param func Callback function to call for each active descriptor.
  382. * @param baton Opaque baton passed to the callback function.
  383. * @remark Multiple signalled conditions for the same descriptor may be reported
  384. * in one or more calls to the callback function, depending on the
  385. * implementation.
  386. * @remark APR_EINTR will be returned if the pollset has been created with
  387. * APR_POLLSET_WAKEABLE and apr_pollcb_wakeup() has been called while
  388. * waiting for activity.
  389. */
  390. APR_DECLARE(apr_status_t) apr_pollcb_poll(apr_pollcb_t *pollcb,
  391. apr_interval_time_t timeout,
  392. apr_pollcb_cb_t func,
  393. void *baton);
  394. /**
  395. * Interrupt the blocked apr_pollcb_poll() call.
  396. * @param pollcb The pollcb to use
  397. * @remark If the pollcb was not created with APR_POLLSET_WAKEABLE the
  398. * return value is APR_EINIT.
  399. */
  400. APR_DECLARE(apr_status_t) apr_pollcb_wakeup(apr_pollcb_t *pollcb);
  401. /**
  402. * Return a printable representation of the pollcb method.
  403. * @param pollcb The pollcb to use
  404. */
  405. APR_DECLARE(const char *) apr_pollcb_method_name(apr_pollcb_t *pollcb);
  406. /** @} */
  407. #ifdef __cplusplus
  408. }
  409. #endif
  410. #endif /* ! APR_POLL_H */