00001 /* Licensed to the Apache Software Foundation (ASF) under one or more 00002 * contributor license agreements. See the NOTICE file distributed with 00003 * this work for additional information regarding copyright ownership. 00004 * The ASF licenses this file to You under the Apache License, Version 2.0 00005 * (the "License"); you may not use this file except in compliance with 00006 * the License. You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef APR_POLL_H 00018 #define APR_POLL_H 00019 /** 00020 * @file apr_poll.h 00021 * @brief APR Poll interface 00022 */ 00023 #include "apr.h" 00024 #include "apr_pools.h" 00025 #include "apr_errno.h" 00026 #include "apr_inherit.h" 00027 #include "apr_file_io.h" 00028 #include "apr_network_io.h" 00029 00030 #if APR_HAVE_NETINET_IN_H 00031 #include <netinet/in.h> 00032 #endif 00033 00034 #ifdef __cplusplus 00035 extern "C" { 00036 #endif /* __cplusplus */ 00037 00038 /** 00039 * @defgroup apr_poll Poll Routines 00040 * @ingroup APR 00041 * @{ 00042 */ 00043 00044 /** 00045 * Poll options 00046 */ 00047 #define APR_POLLIN 0x001 /**< Can read without blocking */ 00048 #define APR_POLLPRI 0x002 /**< Priority data available */ 00049 #define APR_POLLOUT 0x004 /**< Can write without blocking */ 00050 #define APR_POLLERR 0x010 /**< Pending error */ 00051 #define APR_POLLHUP 0x020 /**< Hangup occurred */ 00052 #define APR_POLLNVAL 0x040 /**< Descriptior invalid */ 00053 00054 /** 00055 * Pollset Flags 00056 */ 00057 #define APR_POLLSET_THREADSAFE 0x001 /**< Adding or Removing a Descriptor is thread safe */ 00058 #define APR_POLLSET_NOCOPY 0x002 /**< Descriptors passed to apr_pollset_create() are not copied */ 00059 00060 /** Used in apr_pollfd_t to determine what the apr_descriptor is */ 00061 typedef enum { 00062 APR_NO_DESC, /**< nothing here */ 00063 APR_POLL_SOCKET, /**< descriptor refers to a socket */ 00064 APR_POLL_FILE, /**< descriptor refers to a file */ 00065 APR_POLL_LASTDESC /**< @deprecated descriptor is the last one in the list */ 00066 } apr_datatype_e ; 00067 00068 /** Union of either an APR file or socket. */ 00069 typedef union { 00070 apr_file_t *f; /**< file */ 00071 apr_socket_t *s; /**< socket */ 00072 } apr_descriptor; 00073 00074 /** @see apr_pollfd_t */ 00075 typedef struct apr_pollfd_t apr_pollfd_t; 00076 00077 /** Poll descriptor set. */ 00078 struct apr_pollfd_t { 00079 apr_pool_t *p; /**< associated pool */ 00080 apr_datatype_e desc_type; /**< descriptor type */ 00081 apr_int16_t reqevents; /**< requested events */ 00082 apr_int16_t rtnevents; /**< returned events */ 00083 apr_descriptor desc; /**< @see apr_descriptor */ 00084 void *client_data; /**< allows app to associate context */ 00085 }; 00086 00087 00088 /* General-purpose poll API for arbitrarily large numbers of 00089 * file descriptors 00090 */ 00091 00092 /** Opaque structure used for pollset API */ 00093 typedef struct apr_pollset_t apr_pollset_t; 00094 00095 /** 00096 * Setup a pollset object 00097 * @param pollset The pointer in which to return the newly created object 00098 * @param size The maximum number of descriptors that this pollset can hold 00099 * @param p The pool from which to allocate the pollset 00100 * @param flags Optional flags to modify the operation of the pollset. 00101 * 00102 * @remark If flags equals APR_POLLSET_THREADSAFE, then a pollset is 00103 * created on which it is safe to make concurrent calls to 00104 * apr_pollset_add(), apr_pollset_remove() and apr_pollset_poll() from 00105 * separate threads. This feature is only supported on some 00106 * platforms; the apr_pollset_create() call will fail with 00107 * APR_ENOTIMPL on platforms where it is not supported. 00108 */ 00109 APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset, 00110 apr_uint32_t size, 00111 apr_pool_t *p, 00112 apr_uint32_t flags); 00113 00114 /** 00115 * Destroy a pollset object 00116 * @param pollset The pollset to destroy 00117 */ 00118 APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset); 00119 00120 /** 00121 * Add a socket or file descriptor to a pollset 00122 * @param pollset The pollset to which to add the descriptor 00123 * @param descriptor The descriptor to add 00124 * @remark If you set client_data in the descriptor, that value 00125 * will be returned in the client_data field whenever this 00126 * descriptor is signalled in apr_pollset_poll(). 00127 * @remark If the pollset has been created with APR_POLLSET_THREADSAFE 00128 * and thread T1 is blocked in a call to apr_pollset_poll() for 00129 * this same pollset that is being modified via apr_pollset_add() 00130 * in thread T2, the currently executing apr_pollset_poll() call in 00131 * T1 will either: (1) automatically include the newly added descriptor 00132 * in the set of descriptors it is watching or (2) return immediately 00133 * with APR_EINTR. Option (1) is recommended, but option (2) is 00134 * allowed for implementations where option (1) is impossible 00135 * or impractical. 00136 */ 00137 APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset, 00138 const apr_pollfd_t *descriptor); 00139 00140 /** 00141 * Remove a descriptor from a pollset 00142 * @param pollset The pollset from which to remove the descriptor 00143 * @param descriptor The descriptor to remove 00144 * @remark If the pollset has been created with APR_POLLSET_THREADSAFE 00145 * and thread T1 is blocked in a call to apr_pollset_poll() for 00146 * this same pollset that is being modified via apr_pollset_remove() 00147 * in thread T2, the currently executing apr_pollset_poll() call in 00148 * T1 will either: (1) automatically exclude the newly added descriptor 00149 * in the set of descriptors it is watching or (2) return immediately 00150 * with APR_EINTR. Option (1) is recommended, but option (2) is 00151 * allowed for implementations where option (1) is impossible 00152 * or impractical. 00153 */ 00154 APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset, 00155 const apr_pollfd_t *descriptor); 00156 00157 /** 00158 * Block for activity on the descriptor(s) in a pollset 00159 * @param pollset The pollset to use 00160 * @param timeout Timeout in microseconds 00161 * @param num Number of signalled descriptors (output parameter) 00162 * @param descriptors Array of signalled descriptors (output parameter) 00163 */ 00164 APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset, 00165 apr_interval_time_t timeout, 00166 apr_int32_t *num, 00167 const apr_pollfd_t **descriptors); 00168 00169 00170 /** 00171 * Poll the descriptors in the poll structure 00172 * @param aprset The poll structure we will be using. 00173 * @param numsock The number of descriptors we are polling 00174 * @param nsds The number of descriptors signalled (output parameter) 00175 * @param timeout The amount of time in microseconds to wait. This is 00176 * a maximum, not a minimum. If a descriptor is signalled, we 00177 * will wake up before this time. A negative number means 00178 * wait until a descriptor is signalled. 00179 * @remark The number of descriptors signalled is returned in the third argument. 00180 * This is a blocking call, and it will not return until either a 00181 * descriptor has been signalled, or the timeout has expired. 00182 * @remark The rtnevents field in the apr_pollfd_t array will only be filled- 00183 * in if the return value is APR_SUCCESS. 00184 */ 00185 APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t numsock, 00186 apr_int32_t *nsds, 00187 apr_interval_time_t timeout); 00188 00189 /** Opaque structure used for pollset API */ 00190 typedef struct apr_pollcb_t apr_pollcb_t; 00191 00192 /** 00193 * Setup a pollcb object 00194 * @param pollcb The pointer in which to return the newly created object 00195 * @param size The maximum number of descriptors that a single _poll can return. 00196 * @param p The pool from which to allocate the pollcb 00197 * @param flags Optional flags to modify the operation of the pollcb. 00198 * 00199 * @remark Pollcb is only supported on some platforms; the apr_pollcb_create() 00200 * call will fail with APR_ENOTIMPL on platforms where it is not supported. 00201 */ 00202 APR_DECLARE(apr_status_t) apr_pollcb_create(apr_pollcb_t **pollcb, 00203 apr_uint32_t size, 00204 apr_pool_t *pool, 00205 apr_uint32_t flags); 00206 00207 /** 00208 * Add a socket or file descriptor to a pollcb 00209 * @param pollcb The pollcb to which to add the descriptor 00210 * @param descriptor The descriptor to add 00211 * @remark If you set client_data in the descriptor, that value 00212 * will be returned in the client_data field whenever this 00213 * descriptor is signalled in apr_pollcb_poll(). 00214 * @remark Unlike the apr_pollset API, the descriptor is not copied, and users 00215 * must retain the memory used by descriptor, as the same pointer will be 00216 * returned to them from apr_pollcb_poll. 00217 */ 00218 APR_DECLARE(apr_status_t) apr_pollcb_add(apr_pollcb_t *pollcb, 00219 apr_pollfd_t *descriptor); 00220 /** 00221 * Remove a descriptor from a pollcb 00222 * @param pollcb The pollcb from which to remove the descriptor 00223 * @param descriptor The descriptor to remove 00224 */ 00225 APR_DECLARE(apr_status_t) apr_pollcb_remove(apr_pollcb_t *pollcb, 00226 apr_pollfd_t *descriptor); 00227 00228 /** Function prototype for pollcb handlers 00229 * @param baton Opaque baton passed into apr_pollcb_poll 00230 * @param descriptor Contains the notification for an active descriptor, 00231 * the rtnevents member contains what events were triggered 00232 * for this descriptor. 00233 */ 00234 typedef apr_status_t (*apr_pollcb_cb_t)(void *baton, apr_pollfd_t *descriptor); 00235 00236 /** 00237 * Block for activity on the descriptor(s) in a pollcb 00238 * @param pollcb The pollcb to use 00239 * @param timeout Timeout in microseconds 00240 * @param func Callback function to call for each active socket 00241 * @param baton Opaque baton passed to the callback function. 00242 */ 00243 APR_DECLARE(apr_status_t) apr_pollcb_poll(apr_pollcb_t *pollcb, 00244 apr_interval_time_t timeout, 00245 apr_pollcb_cb_t func, 00246 void *baton); 00247 00248 /** @} */ 00249 00250 #ifdef __cplusplus 00251 } 00252 #endif 00253 00254 #endif /* ! APR_POLL_H */ 00255