apr_poll.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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()
  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 Pollcb is only supported on some platforms; the apr_pollcb_create()
  301. * call will fail with APR_ENOTIMPL on platforms where it is not supported.
  302. */
  303. APR_DECLARE(apr_status_t) apr_pollcb_create(apr_pollcb_t **pollcb,
  304. apr_uint32_t size,
  305. apr_pool_t *p,
  306. apr_uint32_t flags);
  307. /**
  308. * Set up a pollcb object
  309. * @param pollcb The pointer in which to return the newly created object
  310. * @param size The maximum number of descriptors that a single _poll can return.
  311. * @param p The pool from which to allocate the pollcb
  312. * @param flags Optional flags to modify the operation of the pollcb.
  313. * @param method Poll method to use. See #apr_pollset_method_e. If this
  314. * method cannot be used, the default method will be used unless the
  315. * APR_POLLSET_NODEFAULT flag has been specified.
  316. *
  317. * @remark Pollcb is only supported on some platforms; the apr_pollcb_create_ex()
  318. * call will fail with APR_ENOTIMPL on platforms where it is not supported.
  319. */
  320. APR_DECLARE(apr_status_t) apr_pollcb_create_ex(apr_pollcb_t **pollcb,
  321. apr_uint32_t size,
  322. apr_pool_t *p,
  323. apr_uint32_t flags,
  324. apr_pollset_method_e method);
  325. /**
  326. * Add a socket or file descriptor to a pollcb
  327. * @param pollcb The pollcb to which to add the descriptor
  328. * @param descriptor The descriptor to add
  329. * @remark If you set client_data in the descriptor, that value will be
  330. * returned in the client_data field whenever this descriptor is
  331. * signalled in apr_pollcb_poll().
  332. * @remark Unlike the apr_pollset API, the descriptor is not copied, and users
  333. * must retain the memory used by descriptor, as the same pointer will
  334. * be returned to them from apr_pollcb_poll.
  335. * @remark Do not add the same socket or file descriptor to the same pollcb
  336. * multiple times, even if the requested events differ for the
  337. * different calls to apr_pollcb_add(). If the events of interest
  338. * for a descriptor change, you must first remove the descriptor
  339. * from the pollcb with apr_pollcb_remove(), then add it again
  340. * specifying all requested events.
  341. */
  342. APR_DECLARE(apr_status_t) apr_pollcb_add(apr_pollcb_t *pollcb,
  343. apr_pollfd_t *descriptor);
  344. /**
  345. * Remove a descriptor from a pollcb
  346. * @param pollcb The pollcb from which to remove the descriptor
  347. * @param descriptor The descriptor to remove
  348. * @remark apr_pollcb_remove() cannot be used to remove a subset of requested
  349. * events for a descriptor. The reqevents field in the apr_pollfd_t
  350. * parameter must contain the same value when removing as when adding.
  351. */
  352. APR_DECLARE(apr_status_t) apr_pollcb_remove(apr_pollcb_t *pollcb,
  353. apr_pollfd_t *descriptor);
  354. /** Function prototype for pollcb handlers
  355. * @param baton Opaque baton passed into apr_pollcb_poll()
  356. * @param descriptor Contains the notification for an active descriptor,
  357. * the rtnevents member contains what events were triggered
  358. * for this descriptor.
  359. */
  360. typedef apr_status_t (*apr_pollcb_cb_t)(void *baton, apr_pollfd_t *descriptor);
  361. /**
  362. * Block for activity on the descriptor(s) in a pollcb
  363. * @param pollcb The pollcb to use
  364. * @param timeout The amount of time in microseconds to wait. This is a
  365. * maximum, not a minimum. If a descriptor is signalled, the
  366. * function will return before this time. If timeout is
  367. * negative, the function will block until a descriptor is
  368. * signalled.
  369. * @param func Callback function to call for each active descriptor.
  370. * @param baton Opaque baton passed to the callback function.
  371. * @remark Multiple signalled conditions for the same descriptor may be reported
  372. * in one or more calls to the callback function, depending on the
  373. * implementation.
  374. */
  375. APR_DECLARE(apr_status_t) apr_pollcb_poll(apr_pollcb_t *pollcb,
  376. apr_interval_time_t timeout,
  377. apr_pollcb_cb_t func,
  378. void *baton);
  379. /** @} */
  380. #ifdef __cplusplus
  381. }
  382. #endif
  383. #endif /* ! APR_POLL_H */