1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef APR_ARCH_POLL_PRIVATE_H
18#define APR_ARCH_POLL_PRIVATE_H
19
20#if HAVE_POLL_H
21#include <poll.h>
22#endif
23
24#if HAVE_SYS_POLL_H
25#include <sys/poll.h>
26#endif
27
28#ifdef HAVE_PORT_CREATE
29#include <port.h>
30#include <sys/port_impl.h>
31#endif
32
33#ifdef HAVE_KQUEUE
34#include <sys/types.h>
35#include <sys/event.h>
36#include <sys/time.h>
37#endif
38
39#ifdef HAVE_EPOLL
40#include <sys/epoll.h>
41#endif
42
43#ifdef NETWARE
44#define HAS_SOCKETS(dt) (dt == APR_POLL_SOCKET) ? 1 : 0
45#define HAS_PIPES(dt) (dt == APR_POLL_FILE) ? 1 : 0
46#endif
47
48/* Choose the best method platform specific to use in apr_pollset */
49#ifdef HAVE_KQUEUE
50#define POLLSET_USES_KQUEUE
51#define POLLSET_DEFAULT_METHOD APR_POLLSET_KQUEUE
52#elif defined(HAVE_PORT_CREATE)
53#define POLLSET_USES_PORT
54#define POLLSET_DEFAULT_METHOD APR_POLLSET_PORT
55#elif defined(HAVE_EPOLL)
56#define POLLSET_USES_EPOLL
57#define POLLSET_DEFAULT_METHOD APR_POLLSET_EPOLL
58#elif defined(HAVE_POLL)
59#define POLLSET_USES_POLL
60#define POLLSET_DEFAULT_METHOD APR_POLLSET_POLL
61#else
62#define POLLSET_USES_SELECT
63#define POLLSET_DEFAULT_METHOD APR_POLLSET_SELECT
64#endif
65
66#ifdef WIN32
67#define POLL_USES_SELECT
68#undef POLLSET_DEFAULT_METHOD
69#define POLLSET_DEFAULT_METHOD APR_POLLSET_SELECT
70#else
71#ifdef HAVE_POLL
72#define POLL_USES_POLL
73#else
74#define POLL_USES_SELECT
75#endif
76#endif
77
78#if defined(POLLSET_USES_KQUEUE) || defined(POLLSET_USES_EPOLL) || defined(POLLSET_USES_PORT)
79
80#include "apr_ring.h"
81
82#if APR_HAS_THREADS
83#include "apr_thread_mutex.h"
84#define pollset_lock_rings() \
85    if (pollset->flags & APR_POLLSET_THREADSAFE) \
86        apr_thread_mutex_lock(pollset->p->ring_lock);
87#define pollset_unlock_rings() \
88    if (pollset->flags & APR_POLLSET_THREADSAFE) \
89        apr_thread_mutex_unlock(pollset->p->ring_lock);
90#else
91#define pollset_lock_rings()
92#define pollset_unlock_rings()
93#endif
94
95typedef struct pfd_elem_t pfd_elem_t;
96
97struct pfd_elem_t {
98    APR_RING_ENTRY(pfd_elem_t) link;
99    apr_pollfd_t pfd;
100#ifdef HAVE_PORT_CREATE
101   int on_query_ring;
102#endif
103};
104
105#endif
106
107typedef struct apr_pollset_private_t apr_pollset_private_t;
108typedef struct apr_pollset_provider_t apr_pollset_provider_t;
109typedef struct apr_pollcb_provider_t apr_pollcb_provider_t;
110struct apr_pollset_t
111{
112    apr_pool_t *pool;
113    apr_uint32_t nelts;
114    apr_uint32_t nalloc;
115    apr_uint32_t flags;
116    /* Pipe descriptors used for wakeup */
117    apr_file_t *wakeup_pipe[2];
118    apr_pollfd_t wakeup_pfd;
119    apr_pollset_private_t *p;
120    apr_pollset_provider_t *provider;
121};
122
123typedef union {
124#if defined(HAVE_EPOLL)
125    struct epoll_event *epoll;
126#endif
127#if defined(HAVE_PORT_CREATE)
128    port_event_t *port;
129#endif
130#if defined(HAVE_KQUEUE)
131    struct kevent *ke;
132#endif
133#if defined(HAVE_POLL)
134    struct pollfd *ps;
135#endif
136    void *undef;
137} apr_pollcb_pset;
138
139struct apr_pollcb_t {
140    apr_pool_t *pool;
141    apr_uint32_t nelts;
142    apr_uint32_t nalloc;
143    int fd;
144    apr_pollcb_pset pollset;
145    apr_pollfd_t **copyset;
146    apr_pollcb_provider_t *provider;
147};
148
149struct apr_pollset_provider_t {
150    apr_status_t (*create)(apr_pollset_t *, apr_uint32_t, apr_pool_t *, apr_uint32_t);
151    apr_status_t (*add)(apr_pollset_t *, const apr_pollfd_t *);
152    apr_status_t (*remove)(apr_pollset_t *, const apr_pollfd_t *);
153    apr_status_t (*poll)(apr_pollset_t *, apr_interval_time_t, apr_int32_t *, const apr_pollfd_t **);
154    apr_status_t (*cleanup)(apr_pollset_t *);
155    const char *name;
156};
157
158struct apr_pollcb_provider_t {
159    apr_status_t (*create)(apr_pollcb_t *, apr_uint32_t, apr_pool_t *, apr_uint32_t);
160    apr_status_t (*add)(apr_pollcb_t *, apr_pollfd_t *);
161    apr_status_t (*remove)(apr_pollcb_t *, apr_pollfd_t *);
162    apr_status_t (*poll)(apr_pollcb_t *, apr_interval_time_t, apr_pollcb_cb_t, void *);
163    const char *name;
164};
165
166/* Private functions */
167void apr_pollset_drain_wakeup_pipe(apr_pollset_t *pollset);
168
169#endif /* APR_ARCH_POLL_PRIVATE_H */
170