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     /**< Descriptor invalid */
00053 
00054 /**
00055  * Pollset Flags
00056  */
00057 #define APR_POLLSET_THREADSAFE 0x001 /**< Adding or removing a descriptor is
00058                                       * thread-safe
00059                                       */
00060 #define APR_POLLSET_NOCOPY     0x002 /**< Descriptors passed to apr_pollset_add()
00061                                       * are not copied
00062                                       */
00063 #define APR_POLLSET_WAKEABLE   0x004 /**< Poll operations are interruptable by
00064                                       * apr_pollset_wakeup() or apr_pollcb_wakeup()
00065                                       */
00066 #define APR_POLLSET_NODEFAULT  0x010 /**< Do not try to use the default method if
00067                                       * the specified non-default method cannot be
00068                                       * used
00069                                       */
00070 
00071 /**
00072  * Pollset Methods
00073  */
00074 typedef enum {
00075     APR_POLLSET_DEFAULT,        /**< Platform default poll method */
00076     APR_POLLSET_SELECT,         /**< Poll uses select method */
00077     APR_POLLSET_KQUEUE,         /**< Poll uses kqueue method */
00078     APR_POLLSET_PORT,           /**< Poll uses Solaris event port method */
00079     APR_POLLSET_EPOLL,          /**< Poll uses epoll method */
00080     APR_POLLSET_POLL            /**< Poll uses poll method */
00081 } apr_pollset_method_e;
00082 
00083 /** Used in apr_pollfd_t to determine what the apr_descriptor is */
00084 typedef enum { 
00085     APR_NO_DESC,                /**< nothing here */
00086     APR_POLL_SOCKET,            /**< descriptor refers to a socket */
00087     APR_POLL_FILE,              /**< descriptor refers to a file */
00088     APR_POLL_LASTDESC           /**< @deprecated descriptor is the last one in the list */
00089 } apr_datatype_e ;
00090 
00091 /** Union of either an APR file or socket. */
00092 typedef union {
00093     apr_file_t *f;              /**< file */
00094     apr_socket_t *s;            /**< socket */
00095 } apr_descriptor;
00096 
00097 /** @see apr_pollfd_t */
00098 typedef struct apr_pollfd_t apr_pollfd_t;
00099 
00100 /** Poll descriptor set. */
00101 struct apr_pollfd_t {
00102     apr_pool_t *p;              /**< associated pool */
00103     apr_datatype_e desc_type;   /**< descriptor type */
00104     apr_int16_t reqevents;      /**< requested events */
00105     apr_int16_t rtnevents;      /**< returned events */
00106     apr_descriptor desc;        /**< @see apr_descriptor */
00107     void *client_data;          /**< allows app to associate context */
00108 };
00109 
00110 
00111 /* General-purpose poll API for arbitrarily large numbers of
00112  * file descriptors
00113  */
00114 
00115 /** Opaque structure used for pollset API */
00116 typedef struct apr_pollset_t apr_pollset_t;
00117 
00118 /**
00119  * Set up a pollset object
00120  * @param pollset  The pointer in which to return the newly created object 
00121  * @param size The maximum number of descriptors that this pollset can hold
00122  * @param p The pool from which to allocate the pollset
00123  * @param flags Optional flags to modify the operation of the pollset.
00124  *
00125  * @remark If flags contains APR_POLLSET_THREADSAFE, then a pollset is
00126  *         created on which it is safe to make concurrent calls to
00127  *         apr_pollset_add(), apr_pollset_remove() and apr_pollset_poll()
00128  *         from separate threads.  This feature is only supported on some
00129  *         platforms; the apr_pollset_create() call will fail with
00130  *         APR_ENOTIMPL on platforms where it is not supported.
00131  * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollset is
00132  *         created with an additional internal pipe object used for the
00133  *         apr_pollset_wakeup() call. The actual size of pollset is
00134  *         in that case @a size + 1. This feature is only supported on some
00135  *         platforms; the apr_pollset_create() call will fail with
00136  *         APR_ENOTIMPL on platforms where it is not supported.
00137  * @remark If flags contains APR_POLLSET_NOCOPY, then the apr_pollfd_t
00138  *         structures passed to apr_pollset_add() are not copied and
00139  *         must have a lifetime at least as long as the pollset.
00140  * @remark Some poll methods (including APR_POLLSET_KQUEUE,
00141  *         APR_POLLSET_PORT, and APR_POLLSET_EPOLL) do not have a
00142  *         fixed limit on the size of the pollset. For these methods,
00143  *         the size parameter controls the maximum number of
00144  *         descriptors that will be returned by a single call to
00145  *         apr_pollset_poll().
00146  */
00147 APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
00148                                              apr_uint32_t size,
00149                                              apr_pool_t *p,
00150                                              apr_uint32_t flags);
00151 
00152 /**
00153  * Set up a pollset object
00154  * @param pollset  The pointer in which to return the newly created object 
00155  * @param size The maximum number of descriptors that this pollset can hold
00156  * @param p The pool from which to allocate the pollset
00157  * @param flags Optional flags to modify the operation of the pollset.
00158  * @param method Poll method to use. See #apr_pollset_method_e.  If this
00159  *         method cannot be used, the default method will be used unless the
00160  *         APR_POLLSET_NODEFAULT flag has been specified.
00161  *
00162  * @remark If flags contains APR_POLLSET_THREADSAFE, then a pollset is
00163  *         created on which it is safe to make concurrent calls to
00164  *         apr_pollset_add(), apr_pollset_remove() and apr_pollset_poll()
00165  *         from separate threads.  This feature is only supported on some
00166  *         platforms; the apr_pollset_create_ex() call will fail with
00167  *         APR_ENOTIMPL on platforms where it is not supported.
00168  * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollset is
00169  *         created with additional internal pipe object used for the
00170  *         apr_pollset_wakeup() call. The actual size of pollset is
00171  *         in that case size + 1. This feature is only supported on some
00172  *         platforms; the apr_pollset_create_ex() call will fail with
00173  *         APR_ENOTIMPL on platforms where it is not supported.
00174  * @remark If flags contains APR_POLLSET_NOCOPY, then the apr_pollfd_t
00175  *         structures passed to apr_pollset_add() are not copied and
00176  *         must have a lifetime at least as long as the pollset.
00177  * @remark Some poll methods (including APR_POLLSET_KQUEUE,
00178  *         APR_POLLSET_PORT, and APR_POLLSET_EPOLL) do not have a
00179  *         fixed limit on the size of the pollset. For these methods,
00180  *         the size parameter controls the maximum number of
00181  *         descriptors that will be returned by a single call to
00182  *         apr_pollset_poll().
00183  */
00184 APR_DECLARE(apr_status_t) apr_pollset_create_ex(apr_pollset_t **pollset,
00185                                                 apr_uint32_t size,
00186                                                 apr_pool_t *p,
00187                                                 apr_uint32_t flags,
00188                                                 apr_pollset_method_e method);
00189 
00190 /**
00191  * Destroy a pollset object
00192  * @param pollset The pollset to destroy
00193  */
00194 APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t *pollset);
00195 
00196 /**
00197  * Add a socket or file descriptor to a pollset
00198  * @param pollset The pollset to which to add the descriptor
00199  * @param descriptor The descriptor to add
00200  * @remark If you set client_data in the descriptor, that value
00201  *         will be returned in the client_data field whenever this
00202  *         descriptor is signalled in apr_pollset_poll().
00203  * @remark If the pollset has been created with APR_POLLSET_THREADSAFE
00204  *         and thread T1 is blocked in a call to apr_pollset_poll() for
00205  *         this same pollset that is being modified via apr_pollset_add()
00206  *         in thread T2, the currently executing apr_pollset_poll() call in
00207  *         T1 will either: (1) automatically include the newly added descriptor
00208  *         in the set of descriptors it is watching or (2) return immediately
00209  *         with APR_EINTR.  Option (1) is recommended, but option (2) is
00210  *         allowed for implementations where option (1) is impossible
00211  *         or impractical.
00212  * @remark If the pollset has been created with APR_POLLSET_NOCOPY, the 
00213  *         apr_pollfd_t structure referenced by descriptor will not be copied
00214  *         and must have a lifetime at least as long as the pollset.
00215  * @remark Do not add the same socket or file descriptor to the same pollset
00216  *         multiple times, even if the requested events differ for the 
00217  *         different calls to apr_pollset_add().  If the events of interest
00218  *         for a descriptor change, you must first remove the descriptor 
00219  *         from the pollset with apr_pollset_remove(), then add it again 
00220  *         specifying all requested events.
00221  */
00222 APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
00223                                           const apr_pollfd_t *descriptor);
00224 
00225 /**
00226  * Remove a descriptor from a pollset
00227  * @param pollset The pollset from which to remove the descriptor
00228  * @param descriptor The descriptor to remove
00229  * @remark If the descriptor is not found, APR_NOTFOUND is returned.
00230  * @remark If the pollset has been created with APR_POLLSET_THREADSAFE
00231  *         and thread T1 is blocked in a call to apr_pollset_poll() for
00232  *         this same pollset that is being modified via apr_pollset_remove()
00233  *         in thread T2, the currently executing apr_pollset_poll() call in
00234  *         T1 will either: (1) automatically exclude the newly added descriptor
00235  *         in the set of descriptors it is watching or (2) return immediately
00236  *         with APR_EINTR.  Option (1) is recommended, but option (2) is
00237  *         allowed for implementations where option (1) is impossible
00238  *         or impractical.
00239  * @remark apr_pollset_remove() cannot be used to remove a subset of requested
00240  *         events for a descriptor.  The reqevents field in the apr_pollfd_t
00241  *         parameter must contain the same value when removing as when adding.
00242  */
00243 APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
00244                                              const apr_pollfd_t *descriptor);
00245 
00246 /**
00247  * Block for activity on the descriptor(s) in a pollset
00248  * @param pollset The pollset to use
00249  * @param timeout The amount of time in microseconds to wait.  This is a
00250  *                maximum, not a minimum.  If a descriptor is signalled, the
00251  *                function will return before this time.  If timeout is
00252  *                negative, the function will block until a descriptor is
00253  *                signalled or until apr_pollset_wakeup() has been called.
00254  * @param num Number of signalled descriptors (output parameter)
00255  * @param descriptors Array of signalled descriptors (output parameter)
00256  * @remark APR_EINTR will be returned if the pollset has been created with
00257  *         APR_POLLSET_WAKEABLE, apr_pollset_wakeup() has been called while
00258  *         waiting for activity, and there were no signalled descriptors at the
00259  *         time of the wakeup call.
00260  * @remark Multiple signalled conditions for the same descriptor may be reported
00261  *         in one or more returned apr_pollfd_t structures, depending on the
00262  *         implementation.
00263  */
00264 APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
00265                                            apr_interval_time_t timeout,
00266                                            apr_int32_t *num,
00267                                            const apr_pollfd_t **descriptors);
00268 
00269 /**
00270  * Interrupt the blocked apr_pollset_poll() call.
00271  * @param pollset The pollset to use
00272  * @remark If the pollset was not created with APR_POLLSET_WAKEABLE the
00273  *         return value is APR_EINIT.
00274  */
00275 APR_DECLARE(apr_status_t) apr_pollset_wakeup(apr_pollset_t *pollset);
00276 
00277 /**
00278  * Poll the descriptors in the poll structure
00279  * @param aprset The poll structure we will be using. 
00280  * @param numsock The number of descriptors we are polling
00281  * @param nsds The number of descriptors signalled (output parameter)
00282  * @param timeout The amount of time in microseconds to wait.  This is a
00283  *                maximum, not a minimum.  If a descriptor is signalled, the
00284  *                function will return before this time.  If timeout is
00285  *                negative, the function will block until a descriptor is
00286  *                signalled or until apr_pollset_wakeup() has been called.
00287  * @remark The number of descriptors signalled is returned in the third argument. 
00288  *         This is a blocking call, and it will not return until either a 
00289  *         descriptor has been signalled or the timeout has expired. 
00290  * @remark The rtnevents field in the apr_pollfd_t array will only be filled-
00291  *         in if the return value is APR_SUCCESS.
00292  */
00293 APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t numsock,
00294                                    apr_int32_t *nsds, 
00295                                    apr_interval_time_t timeout);
00296 
00297 /**
00298  * Return a printable representation of the pollset method.
00299  * @param pollset The pollset to use
00300  */
00301 APR_DECLARE(const char *) apr_pollset_method_name(apr_pollset_t *pollset);
00302 
00303 /**
00304  * Return a printable representation of the default pollset method
00305  * (APR_POLLSET_DEFAULT).
00306  */
00307 APR_DECLARE(const char *) apr_poll_method_defname(void);
00308 
00309 /** Opaque structure used for pollcb API */
00310 typedef struct apr_pollcb_t apr_pollcb_t;
00311 
00312 /**
00313  * Set up a pollcb object
00314  * @param pollcb  The pointer in which to return the newly created object 
00315  * @param size The maximum number of descriptors that a single _poll can return.
00316  * @param p The pool from which to allocate the pollcb
00317  * @param flags Optional flags to modify the operation of the pollcb.
00318  *
00319  * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollcb is
00320  *         created with an additional internal pipe object used for the
00321  *         apr_pollcb_wakeup() call. The actual size of pollcb is
00322  *         in that case @a size + 1.
00323  * @remark Pollcb is only supported on some platforms; the apr_pollcb_create()
00324  *         call will fail with APR_ENOTIMPL on platforms where it is not supported.
00325  */
00326 APR_DECLARE(apr_status_t) apr_pollcb_create(apr_pollcb_t **pollcb,
00327                                             apr_uint32_t size,
00328                                             apr_pool_t *p,
00329                                             apr_uint32_t flags);
00330 
00331 /**
00332  * Set up a pollcb object
00333  * @param pollcb  The pointer in which to return the newly created object 
00334  * @param size The maximum number of descriptors that a single _poll can return.
00335  * @param p The pool from which to allocate the pollcb
00336  * @param flags Optional flags to modify the operation of the pollcb.
00337  * @param method Poll method to use. See #apr_pollset_method_e.  If this
00338  *         method cannot be used, the default method will be used unless the
00339  *         APR_POLLSET_NODEFAULT flag has been specified.
00340  *
00341  * @remark If flags contains APR_POLLSET_WAKEABLE, then a pollcb is
00342  *         created with an additional internal pipe object used for the
00343  *         apr_pollcb_wakeup() call. The actual size of pollcb is
00344  *         in that case @a size + 1.
00345  * @remark Pollcb is only supported on some platforms; the apr_pollcb_create_ex()
00346  *         call will fail with APR_ENOTIMPL on platforms where it is not supported.
00347  */
00348 APR_DECLARE(apr_status_t) apr_pollcb_create_ex(apr_pollcb_t **pollcb,
00349                                                apr_uint32_t size,
00350                                                apr_pool_t *p,
00351                                                apr_uint32_t flags,
00352                                                apr_pollset_method_e method);
00353 
00354 /**
00355  * Add a socket or file descriptor to a pollcb
00356  * @param pollcb The pollcb to which to add the descriptor
00357  * @param descriptor The descriptor to add
00358  * @remark If you set client_data in the descriptor, that value will be
00359  *         returned in the client_data field whenever this descriptor is
00360  *         signalled in apr_pollcb_poll().
00361  * @remark Unlike the apr_pollset API, the descriptor is not copied, and users 
00362  *         must retain the memory used by descriptor, as the same pointer will
00363  *         be returned to them from apr_pollcb_poll.
00364  * @remark Do not add the same socket or file descriptor to the same pollcb
00365  *         multiple times, even if the requested events differ for the 
00366  *         different calls to apr_pollcb_add().  If the events of interest
00367  *         for a descriptor change, you must first remove the descriptor 
00368  *         from the pollcb with apr_pollcb_remove(), then add it again 
00369  *         specifying all requested events.
00370  */
00371 APR_DECLARE(apr_status_t) apr_pollcb_add(apr_pollcb_t *pollcb,
00372                                          apr_pollfd_t *descriptor);
00373 /**
00374  * Remove a descriptor from a pollcb
00375  * @param pollcb The pollcb from which to remove the descriptor
00376  * @param descriptor The descriptor to remove
00377  * @remark If the descriptor is not found, APR_NOTFOUND is returned.
00378  * @remark apr_pollcb_remove() cannot be used to remove a subset of requested
00379  *         events for a descriptor.  The reqevents field in the apr_pollfd_t
00380  *         parameter must contain the same value when removing as when adding.
00381  */
00382 APR_DECLARE(apr_status_t) apr_pollcb_remove(apr_pollcb_t *pollcb,
00383                                             apr_pollfd_t *descriptor);
00384 
00385 /**
00386  * Function prototype for pollcb handlers 
00387  * @param baton Opaque baton passed into apr_pollcb_poll()
00388  * @param descriptor Contains the notification for an active descriptor. 
00389  *                   The @a rtnevents member describes which events were triggered
00390  *                   for this descriptor.
00391  * @remark If the pollcb handler does not return APR_SUCCESS, the apr_pollcb_poll()
00392  *         call returns with the handler's return value.
00393  */
00394 typedef apr_status_t (*apr_pollcb_cb_t)(void *baton, apr_pollfd_t *descriptor);
00395 
00396 /**
00397  * Block for activity on the descriptor(s) in a pollcb
00398  * @param pollcb The pollcb to use
00399  * @param timeout The amount of time in microseconds to wait.  This is a
00400  *                maximum, not a minimum.  If a descriptor is signalled, the
00401  *                function will return before this time.  If timeout is
00402  *                negative, the function will block until a descriptor is
00403  *                signalled or until apr_pollcb_wakeup() has been called.
00404  * @param func Callback function to call for each active descriptor.
00405  * @param baton Opaque baton passed to the callback function.
00406  * @remark Multiple signalled conditions for the same descriptor may be reported
00407  *         in one or more calls to the callback function, depending on the
00408  *         implementation.
00409  * @remark APR_EINTR will be returned if the pollset has been created with
00410  *         APR_POLLSET_WAKEABLE and apr_pollcb_wakeup() has been called while
00411  *         waiting for activity.
00412  */
00413 APR_DECLARE(apr_status_t) apr_pollcb_poll(apr_pollcb_t *pollcb,
00414                                           apr_interval_time_t timeout,
00415                                           apr_pollcb_cb_t func,
00416                                           void *baton);
00417 
00418 /**
00419  * Interrupt the blocked apr_pollcb_poll() call.
00420  * @param pollcb The pollcb to use
00421  * @remark If the pollcb was not created with APR_POLLSET_WAKEABLE the
00422  *         return value is APR_EINIT.
00423  */
00424 APR_DECLARE(apr_status_t) apr_pollcb_wakeup(apr_pollcb_t *pollcb);
00425 
00426 /**
00427  * Return a printable representation of the pollcb method.
00428  * @param pollcb The pollcb to use
00429  */
00430 APR_DECLARE(const char *) apr_pollcb_method_name(apr_pollcb_t *pollcb);
00431 
00432 /** @} */
00433 
00434 #ifdef __cplusplus
00435 }
00436 #endif
00437 
00438 #endif  /* ! APR_POLL_H */
00439 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines