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 00059 /** Used in apr_pollfd_t to determine what the apr_descriptor is */ 00060 typedef enum { 00061 APR_NO_DESC, /**< nothing here */ 00062 APR_POLL_SOCKET, /**< descriptor refers to a socket */ 00063 APR_POLL_FILE, /**< descriptor refers to a file */ 00064 APR_POLL_LASTDESC /**< descriptor is the last one in the list */ 00065 } apr_datatype_e ; 00066 00067 /** Union of either an APR file or socket. */ 00068 typedef union { 00069 apr_file_t *f; /**< file */ 00070 apr_socket_t *s; /**< socket */ 00071 } apr_descriptor; 00072 00073 /** @see apr_pollfd_t */ 00074 typedef struct apr_pollfd_t apr_pollfd_t; 00075 00076 /** Poll descriptor set. */ 00077 struct apr_pollfd_t { 00078 apr_pool_t *p; /**< associated pool */ 00079 apr_datatype_e desc_type; /**< descriptor type */ 00080 apr_int16_t reqevents; /**< requested events */ 00081 apr_int16_t rtnevents; /**< returned events */ 00082 apr_descriptor desc; /**< @see apr_descriptor */ 00083 void *client_data; /**< allows app to associate context */ 00084 }; 00085 00086 00087 /* General-purpose poll API for arbitrarily large numbers of 00088 * file descriptors 00089 */ 00090 00091 /** Opaque structure used for pollset API */ 00092 typedef struct apr_pollset_t apr_pollset_t; 00093 00094 /** 00095 * Setup a pollset object 00096 * @param pollset The pointer in which to return the newly created object 00097 * @param size The maximum number of descriptors that this pollset can hold 00098 * @param p The pool from which to allocate the pollset 00099 * @param flags Optional flags to modify the operation of the pollset. 00100 * 00101 * @remark If flags equals APR_POLLSET_THREADSAFE, then a pollset is 00102 * created on which it is safe to make concurrent calls to 00103 * apr_pollset_add(), apr_pollset_remove() and apr_pollset_poll() from 00104 * separate threads. This feature is only supported on some 00105 * platforms; the apr_pollset_create() call will fail with 00106 * APR_ENOTIMPL on platforms where it is not supported. 00107 */ 00108 APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset, 00109 apr_uint32_t size, 00110 apr_pool_t *p, 00111 apr_uint32_t flags); 00112 00113 /** 00114 * Destroy a pollset object 00115 * @param pollset The pollset to destroy 00116 */ 00117 APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset); 00118 00119 /** 00120 * Add a socket or file descriptor to a pollset 00121 * @param pollset The pollset to which to add the descriptor 00122 * @param descriptor The descriptor to add 00123 * @remark If you set client_data in the descriptor, that value 00124 * will be returned in the client_data field whenever this 00125 * descriptor is signalled in apr_pollset_poll(). 00126 * @remark If the pollset has been created with APR_POLLSET_THREADSAFE 00127 * and thread T1 is blocked in a call to apr_pollset_poll() for 00128 * this same pollset that is being modified via apr_pollset_add() 00129 * in thread T2, the currently executing apr_pollset_poll() call in 00130 * T1 will either: (1) automatically include the newly added descriptor 00131 * in the set of descriptors it is watching or (2) return immediately 00132 * with APR_EINTR. Option (1) is recommended, but option (2) is 00133 * allowed for implementations where option (1) is impossible 00134 * or impractical. 00135 */ 00136 APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset, 00137 const apr_pollfd_t *descriptor); 00138 00139 /** 00140 * Remove a descriptor from a pollset 00141 * @param pollset The pollset from which to remove the descriptor 00142 * @param descriptor The descriptor to remove 00143 * @remark If the pollset has been created with APR_POLLSET_THREADSAFE 00144 * and thread T1 is blocked in a call to apr_pollset_poll() for 00145 * this same pollset that is being modified via apr_pollset_remove() 00146 * in thread T2, the currently executing apr_pollset_poll() call in 00147 * T1 will either: (1) automatically exclude the newly added descriptor 00148 * in the set of descriptors it is watching or (2) return immediately 00149 * with APR_EINTR. Option (1) is recommended, but option (2) is 00150 * allowed for implementations where option (1) is impossible 00151 * or impractical. 00152 */ 00153 APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset, 00154 const apr_pollfd_t *descriptor); 00155 00156 /** 00157 * Block for activity on the descriptor(s) in a pollset 00158 * @param pollset The pollset to use 00159 * @param timeout Timeout in microseconds 00160 * @param num Number of signalled descriptors (output parameter) 00161 * @param descriptors Array of signalled descriptors (output parameter) 00162 */ 00163 APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset, 00164 apr_interval_time_t timeout, 00165 apr_int32_t *num, 00166 const apr_pollfd_t **descriptors); 00167 00168 00169 /** 00170 * Poll the descriptors in the poll structure 00171 * @param aprset The poll structure we will be using. 00172 * @param numsock The number of descriptors we are polling 00173 * @param nsds The number of descriptors signalled. 00174 * @param timeout The amount of time in microseconds to wait. This is 00175 * a maximum, not a minimum. If a descriptor is signalled, we 00176 * will wake up before this time. A negative number means 00177 * wait until a descriptor is signalled. 00178 * @remark The number of descriptors signalled is returned in the third argument. 00179 * This is a blocking call, and it will not return until either a 00180 * descriptor has been signalled, or the timeout has expired. 00181 * @remark The rtnevents field in the apr_pollfd_t array will only be filled- 00182 * in if the return value is APR_SUCCESS. 00183 */ 00184 APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t numsock, 00185 apr_int32_t *nsds, 00186 apr_interval_time_t timeout); 00187 00188 /** @} */ 00189 00190 00191 #ifdef __cplusplus 00192 } 00193 #endif 00194 00195 #endif /* ! APR_POLL_H */ 00196