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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 /* POSIX defines 1024 for the FD_SETSIZE */
19 #define FD_SETSIZE 1024
25 #include "apr_portable.h"
26 #include "apr_arch_file_io.h"
27 #include "apr_arch_networkio.h"
28 #include "apr_arch_poll_private.h"
29 #include "apr_arch_inherit.h"
31 static apr_pollset_method_e pollset_default_method = POLLSET_DEFAULT_METHOD;
33 #if !APR_FILES_AS_SOCKETS
36 /* Create a dummy wakeup socket pipe for interrupting the poller
38 static apr_status_t create_wakeup_pipe(apr_pollset_t *pollset)
42 if ((rv = apr_file_socket_pipe_create(&pollset->wakeup_pipe[0],
43 &pollset->wakeup_pipe[1],
44 pollset->pool)) != APR_SUCCESS)
47 pollset->wakeup_pfd.p = pollset->pool;
48 pollset->wakeup_pfd.reqevents = APR_POLLIN;
49 pollset->wakeup_pfd.desc_type = APR_POLL_FILE;
50 pollset->wakeup_pfd.desc.f = pollset->wakeup_pipe[0];
52 return apr_pollset_add(pollset, &pollset->wakeup_pfd);
56 static apr_status_t create_wakeup_pipe(apr_pollset_t *pollset)
61 static apr_status_t apr_file_socket_pipe_close(apr_file_t *file)
67 #else /* APR_FILES_AS_SOCKETS */
69 /* Create a dummy wakeup pipe for interrupting the poller
71 static apr_status_t create_wakeup_pipe(apr_pollset_t *pollset)
75 if ((rv = apr_file_pipe_create(&pollset->wakeup_pipe[0],
76 &pollset->wakeup_pipe[1],
77 pollset->pool)) != APR_SUCCESS)
80 pollset->wakeup_pfd.p = pollset->pool;
81 pollset->wakeup_pfd.reqevents = APR_POLLIN;
82 pollset->wakeup_pfd.desc_type = APR_POLL_FILE;
83 pollset->wakeup_pfd.desc.f = pollset->wakeup_pipe[0];
88 if ((flags = fcntl(pollset->wakeup_pipe[0]->filedes, F_GETFD)) == -1)
92 if (fcntl(pollset->wakeup_pipe[0]->filedes, F_SETFD, flags) == -1)
98 if ((flags = fcntl(pollset->wakeup_pipe[1]->filedes, F_GETFD)) == -1)
102 if (fcntl(pollset->wakeup_pipe[1]->filedes, F_SETFD, flags) == -1)
106 return apr_pollset_add(pollset, &pollset->wakeup_pfd);
108 #endif /* !APR_FILES_AS_SOCKETS */
110 /* Read and discard what's ever in the wakeup pipe.
112 void apr_pollset_drain_wakeup_pipe(apr_pollset_t *pollset)
115 apr_size_t nr = sizeof(rb);
117 while (apr_file_read(pollset->wakeup_pipe[0], rb, &nr) == APR_SUCCESS) {
118 /* Although we write just one byte to the other end of the pipe
119 * during wakeup, multiple threads could call the wakeup.
120 * So simply drain out from the input side of the pipe all
123 if (nr != sizeof(rb))
128 static apr_status_t pollset_cleanup(void *p)
130 apr_pollset_t *pollset = (apr_pollset_t *) p;
131 if (pollset->provider->cleanup) {
132 (*pollset->provider->cleanup)(pollset);
134 if (pollset->flags & APR_POLLSET_WAKEABLE) {
135 /* Close both sides of the wakeup pipe */
136 if (pollset->wakeup_pipe[0]) {
137 #if APR_FILES_AS_SOCKETS
138 apr_file_close(pollset->wakeup_pipe[0]);
140 apr_file_socket_pipe_close(pollset->wakeup_pipe[0]);
142 pollset->wakeup_pipe[0] = NULL;
144 if (pollset->wakeup_pipe[1]) {
145 #if APR_FILES_AS_SOCKETS
146 apr_file_close(pollset->wakeup_pipe[1]);
148 apr_file_socket_pipe_close(pollset->wakeup_pipe[1]);
150 pollset->wakeup_pipe[1] = NULL;
157 #if defined(HAVE_KQUEUE)
158 extern apr_pollset_provider_t *apr_pollset_provider_kqueue;
160 #if defined(HAVE_PORT_CREATE)
161 extern apr_pollset_provider_t *apr_pollset_provider_port;
163 #if defined(HAVE_EPOLL)
164 extern apr_pollset_provider_t *apr_pollset_provider_epoll;
166 #if defined(HAVE_POLL)
167 extern apr_pollset_provider_t *apr_pollset_provider_poll;
169 extern apr_pollset_provider_t *apr_pollset_provider_select;
171 static apr_pollset_provider_t *pollset_provider(apr_pollset_method_e method)
173 apr_pollset_provider_t *provider = NULL;
175 case APR_POLLSET_KQUEUE:
176 #if defined(HAVE_KQUEUE)
177 provider = apr_pollset_provider_kqueue;
180 case APR_POLLSET_PORT:
181 #if defined(HAVE_PORT_CREATE)
182 provider = apr_pollset_provider_port;
185 case APR_POLLSET_EPOLL:
186 #if defined(HAVE_EPOLL)
187 provider = apr_pollset_provider_epoll;
190 case APR_POLLSET_POLL:
191 #if defined(HAVE_POLL)
192 provider = apr_pollset_provider_poll;
195 case APR_POLLSET_SELECT:
196 provider = apr_pollset_provider_select;
198 case APR_POLLSET_DEFAULT:
204 APR_DECLARE(apr_status_t) apr_pollset_create_ex(apr_pollset_t **ret_pollset,
208 apr_pollset_method_e method)
211 apr_pollset_t *pollset;
212 apr_pollset_provider_t *provider = NULL;
217 /* Favor WSAPoll if supported.
218 * This will work only if ws2_32.dll has WSAPoll funtion.
219 * In other cases it will fall back to select() method unless
220 * the APR_POLLSET_NODEFAULT is added to the flags.
222 if (method == APR_POLLSET_DEFAULT) {
223 method = APR_POLLSET_POLL;
227 if (method == APR_POLLSET_DEFAULT)
228 method = pollset_default_method;
229 while (provider == NULL) {
230 provider = pollset_provider(method);
232 if ((flags & APR_POLLSET_NODEFAULT) == APR_POLLSET_NODEFAULT)
234 if (method == pollset_default_method)
236 method = pollset_default_method;
239 if (flags & APR_POLLSET_WAKEABLE) {
240 /* Add room for wakeup descriptor */
244 pollset = apr_palloc(p, sizeof(*pollset));
246 pollset->nalloc = size;
248 pollset->flags = flags;
249 pollset->provider = provider;
251 rv = (*provider->create)(pollset, size, p, flags);
252 if (rv == APR_ENOTIMPL) {
253 if (method == pollset_default_method) {
256 provider = pollset_provider(pollset_default_method);
260 rv = (*provider->create)(pollset, size, p, flags);
261 if (rv != APR_SUCCESS) {
264 pollset->provider = provider;
266 else if (rv != APR_SUCCESS) {
269 if (flags & APR_POLLSET_WAKEABLE) {
270 /* Create wakeup pipe */
271 if ((rv = create_wakeup_pipe(pollset)) != APR_SUCCESS) {
275 if ((flags & APR_POLLSET_WAKEABLE) || provider->cleanup)
276 apr_pool_cleanup_register(p, pollset, pollset_cleanup,
277 apr_pool_cleanup_null);
279 *ret_pollset = pollset;
283 APR_DECLARE(const char *) apr_pollset_method_name(apr_pollset_t *pollset)
285 return pollset->provider->name;
288 APR_DECLARE(const char *) apr_poll_method_defname()
290 apr_pollset_provider_t *provider = NULL;
292 provider = pollset_provider(pollset_default_method);
294 return provider->name;
299 APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
304 apr_pollset_method_e method = APR_POLLSET_DEFAULT;
305 return apr_pollset_create_ex(pollset, size, p, flags, method);
308 APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t * pollset)
310 if (pollset->flags & APR_POLLSET_WAKEABLE ||
311 pollset->provider->cleanup)
312 return apr_pool_cleanup_run(pollset->pool, pollset,
318 APR_DECLARE(apr_status_t) apr_pollset_wakeup(apr_pollset_t *pollset)
320 if (pollset->flags & APR_POLLSET_WAKEABLE)
321 return apr_file_putc(1, pollset->wakeup_pipe[1]);
326 APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
327 const apr_pollfd_t *descriptor)
329 return (*pollset->provider->add)(pollset, descriptor);
332 APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
333 const apr_pollfd_t *descriptor)
335 return (*pollset->provider->remove)(pollset, descriptor);
338 APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
339 apr_interval_time_t timeout,
341 const apr_pollfd_t **descriptors)
343 return (*pollset->provider->poll)(pollset, timeout, num, descriptors);