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