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()
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 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 pollset has been created with APR_POLLSET_THREADSAFE
00230  *         and thread T1 is blocked in a call to apr_pollset_poll() for
00231  *         this same pollset that is being modified via apr_pollset_remove()
00232  *         in thread T2, the currently executing apr_pollset_poll() call in
00233  *         T1 will either: (1) automatically exclude the newly added descriptor
00234  *         in the set of descriptors it is watching or (2) return immediately
00235  *         with APR_EINTR.  Option (1) is recommended, but option (2) is
00236  *         allowed for implementations where option (1) is impossible
00237  *         or impractical.
00238  * @remark apr_pollset_remove() cannot be used to remove a subset of requested
00239  *         events for a descriptor.  The reqevents field in the apr_pollfd_t
00240  *         parameter must contain the same value when removing as when adding.
00241  */
00242 APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
00243                                              const apr_pollfd_t *descriptor);
00244 
00245 /**
00246  * Block for activity on the descriptor(s) in a pollset
00247  * @param pollset The pollset to use
00248  * @param timeout The amount of time in microseconds to wait.  This is a
00249  *                maximum, not a minimum.  If a descriptor is signalled, the
00250  *                function will return before this time.  If timeout is
00251  *                negative, the function will block until a descriptor is
00252  *                signalled or until apr_pollset_wakeup() has been called.
00253  * @param num Number of signalled descriptors (output parameter)
00254  * @param descriptors Array of signalled descriptors (output parameter)
00255  * @remark APR_EINTR will be returned if the pollset has been created with
00256  *         APR_POLLSET_WAKEABLE, apr_pollset_wakeup() has been called while
00257  *         waiting for activity, and there were no signalled descriptors at the
00258  *         time of the wakeup call.
00259  * @remark Multiple signalled conditions for the same descriptor may be reported
00260  *         in one or more returned apr_pollfd_t structures, depending on the
00261  *         implementation.
00262  * @bug With versions 1.4.2 and prior on Windows, a call with no descriptors
00263  *      and timeout will return immediately with the wrong error code.
00264  */
00265 APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
00266                                            apr_interval_time_t timeout,
00267                                            apr_int32_t *num,
00268                                            const apr_pollfd_t **descriptors);
00269 
00270 /**
00271  * Interrupt the blocked apr_pollset_poll() call.
00272  * @param pollset The pollset to use
00273  * @remark If the pollset was not created with APR_POLLSET_WAKEABLE the
00274  *         return value is APR_EINIT.
00275  */
00276 APR_DECLARE(apr_status_t) apr_pollset_wakeup(apr_pollset_t *pollset);
00277 
00278 /**
00279  * Poll the descriptors in the poll structure
00280  * @param aprset The poll structure we will be using. 
00281  * @param numsock The number of descriptors we are polling
00282  * @param nsds The number of descriptors signalled (output parameter)
00283  * @param timeout The amount of time in microseconds to wait.  This is a
00284  *                maximum, not a minimum.  If a descriptor is signalled, the
00285  *                function will return before this time.  If timeout is
00286  *                negative, the function will block until a descriptor is
00287  *                signalled or until apr_pollset_wakeup() has been called.
00288  * @remark The number of descriptors signalled is returned in the third argument. 
00289  *         This is a blocking call, and it will not return until either a 
00290  *         descriptor has been signalled or the timeout has expired. 
00291  * @remark The rtnevents field in the apr_pollfd_t array will only be filled-
00292  *         in if the return value is APR_SUCCESS.
00293  * @bug With versions 1.4.2 and prior on Windows, a call with no descriptors
00294  *      and timeout will return immediately with the wrong error code.
00295  */
00296 APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t numsock,
00297                                    apr_int32_t *nsds, 
00298                                    apr_interval_time_t timeout);
00299 
00300 /**
00301  * Return a printable representation of the pollset method.
00302  * @param pollset The pollset to use
00303  */
00304 APR_DECLARE(const char *) apr_pollset_method_name(apr_pollset_t *pollset);
00305 
00306 /**
00307  * Return a printable representation of the default pollset method
00308  * (APR_POLLSET_DEFAULT).
00309  */
00310 APR_DECLARE(const char *) apr_poll_method_defname(void);
00311 
00312 /** Opaque structure used for pollset API */
00313 typedef struct apr_pollcb_t apr_pollcb_t;
00314 
00315 /**
00316  * Set up a pollcb object
00317  * @param pollcb  The pointer in which to return the newly created object 
00318  * @param size The maximum number of descriptors that a single _poll can return.
00319  * @param p The pool from which to allocate the pollcb
00320  * @param flags Optional flags to modify the operation of the pollcb.
00321  *
00322  * @remark Pollcb is only supported on some platforms; the apr_pollcb_create()
00323  * call will fail with APR_ENOTIMPL on platforms where it is not supported.
00324  */
00325 APR_DECLARE(apr_status_t) apr_pollcb_create(apr_pollcb_t **pollcb,
00326                                             apr_uint32_t size,
00327                                             apr_pool_t *p,
00328                                             apr_uint32_t flags);
00329 
00330 /**
00331  * Set up a pollcb object
00332  * @param pollcb  The pointer in which to return the newly created object 
00333  * @param size The maximum number of descriptors that a single _poll can return.
00334  * @param p The pool from which to allocate the pollcb
00335  * @param flags Optional flags to modify the operation of the pollcb.
00336  * @param method Poll method to use. See #apr_pollset_method_e.  If this
00337  *         method cannot be used, the default method will be used unless the
00338  *         APR_POLLSET_NODEFAULT flag has been specified.
00339  *
00340  * @remark Pollcb is only supported on some platforms; the apr_pollcb_create_ex()
00341  * call will fail with APR_ENOTIMPL on platforms where it is not supported.
00342  */
00343 APR_DECLARE(apr_status_t) apr_pollcb_create_ex(apr_pollcb_t **pollcb,
00344                                                apr_uint32_t size,
00345                                                apr_pool_t *p,
00346                                                apr_uint32_t flags,
00347                                                apr_pollset_method_e method);
00348 
00349 /**
00350  * Add a socket or file descriptor to a pollcb
00351  * @param pollcb The pollcb to which to add the descriptor
00352  * @param descriptor The descriptor to add
00353  * @remark If you set client_data in the descriptor, that value will be
00354  *         returned in the client_data field whenever this descriptor is
00355  *         signalled in apr_pollcb_poll().
00356  * @remark Unlike the apr_pollset API, the descriptor is not copied, and users 
00357  *         must retain the memory used by descriptor, as the same pointer will
00358  *         be returned to them from apr_pollcb_poll.
00359  * @remark Do not add the same socket or file descriptor to the same pollcb
00360  *         multiple times, even if the requested events differ for the 
00361  *         different calls to apr_pollcb_add().  If the events of interest
00362  *         for a descriptor change, you must first remove the descriptor 
00363  *         from the pollcb with apr_pollcb_remove(), then add it again 
00364  *         specifying all requested events.
00365  */
00366 APR_DECLARE(apr_status_t) apr_pollcb_add(apr_pollcb_t *pollcb,
00367                                          apr_pollfd_t *descriptor);
00368 /**
00369  * Remove a descriptor from a pollcb
00370  * @param pollcb The pollcb from which to remove the descriptor
00371  * @param descriptor The descriptor to remove
00372  * @remark apr_pollcb_remove() cannot be used to remove a subset of requested
00373  *         events for a descriptor.  The reqevents field in the apr_pollfd_t
00374  *         parameter must contain the same value when removing as when adding.
00375  */
00376 APR_DECLARE(apr_status_t) apr_pollcb_remove(apr_pollcb_t *pollcb,
00377                                             apr_pollfd_t *descriptor);
00378 
00379 /** Function prototype for pollcb handlers 
00380  * @param baton Opaque baton passed into apr_pollcb_poll()
00381  * @param descriptor Contains the notification for an active descriptor, 
00382  *                   the rtnevents member contains what events were triggered
00383  *                   for this descriptor.
00384  */
00385 typedef apr_status_t (*apr_pollcb_cb_t)(void *baton, apr_pollfd_t *descriptor);
00386 
00387 /**
00388  * Block for activity on the descriptor(s) in a pollcb
00389  * @param pollcb The pollcb to use
00390  * @param timeout The amount of time in microseconds to wait.  This is a
00391  *                maximum, not a minimum.  If a descriptor is signalled, the
00392  *                function will return before this time.  If timeout is
00393  *                negative, the function will block until a descriptor is
00394  *                signalled.
00395  * @param func Callback function to call for each active descriptor.
00396  * @param baton Opaque baton passed to the callback function.
00397  * @remark Multiple signalled conditions for the same descriptor may be reported
00398  *         in one or more calls to the callback function, depending on the
00399  *         implementation.
00400  * @bug With versions 1.4.2 and prior on Windows, a call with no descriptors
00401  *      and timeout will return immediately with the wrong error code.
00402  */
00403 APR_DECLARE(apr_status_t) apr_pollcb_poll(apr_pollcb_t *pollcb,
00404                                           apr_interval_time_t timeout,
00405                                           apr_pollcb_cb_t func,
00406                                           void *baton); 
00407 
00408 /** @} */
00409 
00410 #ifdef __cplusplus
00411 }
00412 #endif
00413 
00414 #endif  /* ! APR_POLL_H */
00415 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines