apr_poll.h 19 KB

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