Apache Portable Runtime
apr_poll.h
Go to the documentation of this file.
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 The amount of time in microseconds to wait.  This is 
00161  *                a maximum, not a minimum.  If a descriptor is signalled, we 
00162  *                will wake up before this time.  A negative number means 
00163  *                wait until a descriptor is signalled.
00164  * @param num Number of signalled descriptors (output parameter)
00165  * @param descriptors Array of signalled descriptors (output parameter)
00166  */
00167 APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
00168                                            apr_interval_time_t timeout,
00169                                            apr_int32_t *num,
00170                                            const apr_pollfd_t **descriptors);
00171 
00172 
00173 /**
00174  * Poll the descriptors in the poll structure
00175  * @param aprset The poll structure we will be using. 
00176  * @param numsock The number of descriptors we are polling
00177  * @param nsds The number of descriptors signalled (output parameter)
00178  * @param timeout The amount of time in microseconds to wait.  This is 
00179  *                a maximum, not a minimum.  If a descriptor is signalled, we 
00180  *                will wake up before this time.  A negative number means 
00181  *                wait until a descriptor is signalled.
00182  * @remark The number of descriptors signalled is returned in the third argument. 
00183  *         This is a blocking call, and it will not return until either a 
00184  *         descriptor has been signalled, or the timeout has expired. 
00185  * @remark The rtnevents field in the apr_pollfd_t array will only be filled-
00186  *         in if the return value is APR_SUCCESS.
00187  */
00188 APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t numsock,
00189                                    apr_int32_t *nsds, 
00190                                    apr_interval_time_t timeout);
00191 
00192 /** Opaque structure used for pollset API */
00193 typedef struct apr_pollcb_t apr_pollcb_t;
00194 
00195 /**
00196  * Setup a pollcb object
00197  * @param pollcb  The pointer in which to return the newly created object 
00198  * @param size The maximum number of descriptors that a single _poll can return.
00199  * @param p The pool from which to allocate the pollcb
00200  * @param flags Optional flags to modify the operation of the pollcb.
00201  *
00202  * @remark Pollcb is only supported on some platforms; the apr_pollcb_create()
00203  * call will fail with APR_ENOTIMPL on platforms where it is not supported.
00204  */
00205 APR_DECLARE(apr_status_t) apr_pollcb_create(apr_pollcb_t **pollcb,
00206                                             apr_uint32_t size,
00207                                             apr_pool_t *pool,
00208                                             apr_uint32_t flags);
00209 
00210 /**
00211  * Add a socket or file descriptor to a pollcb
00212  * @param pollcb The pollcb to which to add the descriptor
00213  * @param descriptor The descriptor to add
00214  * @remark If you set client_data in the descriptor, that value
00215  *         will be returned in the client_data field whenever this
00216  *         descriptor is signalled in apr_pollcb_poll().
00217  * @remark Unlike the apr_pollset API, the descriptor is not copied, and users 
00218  *         must retain the memory used by descriptor, as the same pointer will be 
00219  *         returned to them from apr_pollcb_poll.
00220  */
00221 APR_DECLARE(apr_status_t) apr_pollcb_add(apr_pollcb_t *pollcb,
00222                                          apr_pollfd_t *descriptor);
00223 /**
00224  * Remove a descriptor from a pollcb
00225  * @param pollcb The pollcb from which to remove the descriptor
00226  * @param descriptor The descriptor to remove
00227  */
00228 APR_DECLARE(apr_status_t) apr_pollcb_remove(apr_pollcb_t *pollcb,
00229                                             apr_pollfd_t *descriptor);
00230 
00231 /** Function prototype for pollcb handlers 
00232  * @param baton Opaque baton passed into apr_pollcb_poll
00233  * @param descriptor Contains the notification for an active descriptor, 
00234  *                   the rtnevents member contains what events were triggered
00235  *                   for this descriptor.
00236  */
00237 typedef apr_status_t (*apr_pollcb_cb_t)(void *baton, apr_pollfd_t *descriptor);
00238 
00239 /**
00240  * Block for activity on the descriptor(s) in a pollcb
00241  * @param pollcb The pollcb to use
00242  * @param timeout The amount of time in microseconds to wait.  This is 
00243  *                a maximum, not a minimum.  If a descriptor is signalled, we 
00244  *                will wake up before this time.  A negative number means 
00245  *                wait until a descriptor is signalled.
00246  * @param func Callback function to call for each active socket
00247  * @param baton Opaque baton passed to the callback function.
00248  */
00249 APR_DECLARE(apr_status_t) apr_pollcb_poll(apr_pollcb_t *pollcb,
00250                                           apr_interval_time_t timeout,
00251                                           apr_pollcb_cb_t func,
00252                                           void *baton); 
00253 
00254 /** @} */
00255 
00256 #ifdef __cplusplus
00257 }
00258 #endif
00259 
00260 #endif  /* ! APR_POLL_H */
00261 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines