fspr_poll.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 fspr_poll.h
  20. * @brief APR Poll interface
  21. */
  22. #include "fspr.h"
  23. #include "fspr_pools.h"
  24. #include "fspr_errno.h"
  25. #include "fspr_inherit.h"
  26. #include "fspr_file_io.h"
  27. #include "fspr_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 fspr_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 /**< Descriptior invalid */
  48. /**
  49. * Pollset Flags
  50. */
  51. #define APR_POLLSET_THREADSAFE 0x001 /**< Adding or Removing a Descriptor is thread safe */
  52. /** Used in fspr_pollfd_t to determine what the fspr_descriptor is */
  53. typedef enum {
  54. APR_NO_DESC, /**< nothing here */
  55. APR_POLL_SOCKET, /**< descriptor refers to a socket */
  56. APR_POLL_FILE, /**< descriptor refers to a file */
  57. APR_POLL_LASTDESC /**< descriptor is the last one in the list */
  58. } fspr_datatype_e ;
  59. /** Union of either an APR file or socket. */
  60. typedef union {
  61. fspr_file_t *f; /**< file */
  62. fspr_socket_t *s; /**< socket */
  63. } fspr_descriptor;
  64. /** @see fspr_pollfd_t */
  65. typedef struct fspr_pollfd_t fspr_pollfd_t;
  66. /** Poll descriptor set. */
  67. struct fspr_pollfd_t {
  68. fspr_pool_t *p; /**< associated pool */
  69. fspr_datatype_e desc_type; /**< descriptor type */
  70. fspr_int16_t reqevents; /**< requested events */
  71. fspr_int16_t rtnevents; /**< returned events */
  72. fspr_descriptor desc; /**< @see fspr_descriptor */
  73. void *client_data; /**< allows app to associate context */
  74. };
  75. /* General-purpose poll API for arbitrarily large numbers of
  76. * file descriptors
  77. */
  78. /** Opaque structure used for pollset API */
  79. typedef struct fspr_pollset_t fspr_pollset_t;
  80. /**
  81. * Setup a pollset object
  82. * @param pollset The pointer in which to return the newly created object
  83. * @param size The maximum number of descriptors that this pollset can hold
  84. * @param p The pool from which to allocate the pollset
  85. * @param flags Optional flags to modify the operation of the pollset.
  86. *
  87. * @remark If flags equals APR_POLLSET_THREADSAFE, then a pollset is
  88. * created on which it is safe to make concurrent calls to
  89. * fspr_pollset_add(), fspr_pollset_remove() and fspr_pollset_poll() from
  90. * separate threads. This feature is only supported on some
  91. * platforms; the fspr_pollset_create() call will fail with
  92. * APR_ENOTIMPL on platforms where it is not supported.
  93. */
  94. APR_DECLARE(fspr_status_t) fspr_pollset_create(fspr_pollset_t **pollset,
  95. fspr_uint32_t size,
  96. fspr_pool_t *p,
  97. fspr_uint32_t flags);
  98. /**
  99. * Destroy a pollset object
  100. * @param pollset The pollset to destroy
  101. */
  102. APR_DECLARE(fspr_status_t) fspr_pollset_destroy(fspr_pollset_t *pollset);
  103. /**
  104. * Add a socket or file descriptor to a pollset
  105. * @param pollset The pollset to which to add the descriptor
  106. * @param descriptor The descriptor to add
  107. * @remark If you set client_data in the descriptor, that value
  108. * will be returned in the client_data field whenever this
  109. * descriptor is signalled in fspr_pollset_poll().
  110. * @remark If the pollset has been created with APR_POLLSET_THREADSAFE
  111. * and thread T1 is blocked in a call to fspr_pollset_poll() for
  112. * this same pollset that is being modified via fspr_pollset_add()
  113. * in thread T2, the currently executing fspr_pollset_poll() call in
  114. * T1 will either: (1) automatically include the newly added descriptor
  115. * in the set of descriptors it is watching or (2) return immediately
  116. * with APR_EINTR. Option (1) is recommended, but option (2) is
  117. * allowed for implementations where option (1) is impossible
  118. * or impractical.
  119. */
  120. APR_DECLARE(fspr_status_t) fspr_pollset_add(fspr_pollset_t *pollset,
  121. const fspr_pollfd_t *descriptor);
  122. /**
  123. * Remove a descriptor from a pollset
  124. * @param pollset The pollset from which to remove the descriptor
  125. * @param descriptor The descriptor to remove
  126. * @remark If the pollset has been created with APR_POLLSET_THREADSAFE
  127. * and thread T1 is blocked in a call to fspr_pollset_poll() for
  128. * this same pollset that is being modified via fspr_pollset_remove()
  129. * in thread T2, the currently executing fspr_pollset_poll() call in
  130. * T1 will either: (1) automatically exclude the newly added descriptor
  131. * in the set of descriptors it is watching or (2) return immediately
  132. * with APR_EINTR. Option (1) is recommended, but option (2) is
  133. * allowed for implementations where option (1) is impossible
  134. * or impractical.
  135. */
  136. APR_DECLARE(fspr_status_t) fspr_pollset_remove(fspr_pollset_t *pollset,
  137. const fspr_pollfd_t *descriptor);
  138. /**
  139. * Block for activity on the descriptor(s) in a pollset
  140. * @param pollset The pollset to use
  141. * @param timeout Timeout in microseconds
  142. * @param num Number of signalled descriptors (output parameter)
  143. * @param descriptors Array of signalled descriptors (output parameter)
  144. */
  145. APR_DECLARE(fspr_status_t) fspr_pollset_poll(fspr_pollset_t *pollset,
  146. fspr_interval_time_t timeout,
  147. fspr_int32_t *num,
  148. const fspr_pollfd_t **descriptors);
  149. /**
  150. * Poll the descriptors in the poll structure
  151. * @param aprset The poll structure we will be using.
  152. * @param numsock The number of descriptors we are polling
  153. * @param nsds The number of descriptors signalled.
  154. * @param timeout The amount of time in microseconds to wait. This is
  155. * a maximum, not a minimum. If a descriptor is signalled, we
  156. * will wake up before this time. A negative number means
  157. * wait until a descriptor is signalled.
  158. * @remark The number of descriptors signalled is returned in the third argument.
  159. * This is a blocking call, and it will not return until either a
  160. * descriptor has been signalled, or the timeout has expired.
  161. * @remark The rtnevents field in the fspr_pollfd_t array will only be filled-
  162. * in if the return value is APR_SUCCESS.
  163. */
  164. APR_DECLARE(fspr_status_t) fspr_poll(fspr_pollfd_t *aprset, fspr_int32_t numsock,
  165. fspr_int32_t *nsds,
  166. fspr_interval_time_t timeout);
  167. /** @} */
  168. #ifdef __cplusplus
  169. }
  170. #endif
  171. #endif /* ! APR_POLL_H */