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_AIO_MSGQ)
167 extern apr_pollset_provider_t *apr_pollset_provider_aio_msgq;
169 #if defined(HAVE_POLL)
170 extern apr_pollset_provider_t *apr_pollset_provider_poll;
172 extern apr_pollset_provider_t *apr_pollset_provider_select;
174 static apr_pollset_provider_t *pollset_provider(apr_pollset_method_e method)
176 apr_pollset_provider_t *provider = NULL;
178 case APR_POLLSET_KQUEUE:
179 #if defined(HAVE_KQUEUE)
180 provider = apr_pollset_provider_kqueue;
183 case APR_POLLSET_PORT:
184 #if defined(HAVE_PORT_CREATE)
185 provider = apr_pollset_provider_port;
188 case APR_POLLSET_EPOLL:
189 #if defined(HAVE_EPOLL)
190 provider = apr_pollset_provider_epoll;
193 case APR_POLLSET_AIO_MSGQ:
194 #if defined(HAVE_AIO_MSGQ)
195 provider = apr_pollset_provider_aio_msgq;
198 case APR_POLLSET_POLL:
199 #if defined(HAVE_POLL)
200 provider = apr_pollset_provider_poll;
203 case APR_POLLSET_SELECT:
204 provider = apr_pollset_provider_select;
206 case APR_POLLSET_DEFAULT:
212 APR_DECLARE(apr_status_t) apr_pollset_create_ex(apr_pollset_t **ret_pollset,
216 apr_pollset_method_e method)
219 apr_pollset_t *pollset;
220 apr_pollset_provider_t *provider = NULL;
225 /* Favor WSAPoll if supported.
226 * This will work only if ws2_32.dll has WSAPoll funtion.
227 * In other cases it will fall back to select() method unless
228 * the APR_POLLSET_NODEFAULT is added to the flags.
230 if (method == APR_POLLSET_DEFAULT) {
231 method = APR_POLLSET_POLL;
235 if (method == APR_POLLSET_DEFAULT)
236 method = pollset_default_method;
237 while (provider == NULL) {
238 provider = pollset_provider(method);
240 if ((flags & APR_POLLSET_NODEFAULT) == APR_POLLSET_NODEFAULT)
242 if (method == pollset_default_method)
244 method = pollset_default_method;
247 if (flags & APR_POLLSET_WAKEABLE) {
248 /* Add room for wakeup descriptor */
252 pollset = apr_palloc(p, sizeof(*pollset));
254 pollset->nalloc = size;
256 pollset->flags = flags;
257 pollset->provider = provider;
259 rv = (*provider->create)(pollset, size, p, flags);
260 if (rv == APR_ENOTIMPL) {
261 if (method == pollset_default_method) {
264 provider = pollset_provider(pollset_default_method);
268 rv = (*provider->create)(pollset, size, p, flags);
269 if (rv != APR_SUCCESS) {
272 pollset->provider = provider;
274 else if (rv != APR_SUCCESS) {
277 if (flags & APR_POLLSET_WAKEABLE) {
278 /* Create wakeup pipe */
279 if ((rv = create_wakeup_pipe(pollset)) != APR_SUCCESS) {
283 if ((flags & APR_POLLSET_WAKEABLE) || provider->cleanup)
284 apr_pool_cleanup_register(p, pollset, pollset_cleanup,
285 apr_pool_cleanup_null);
287 *ret_pollset = pollset;
291 APR_DECLARE(const char *) apr_pollset_method_name(apr_pollset_t *pollset)
293 return pollset->provider->name;
296 APR_DECLARE(const char *) apr_poll_method_defname()
298 apr_pollset_provider_t *provider = NULL;
300 provider = pollset_provider(pollset_default_method);
302 return provider->name;
307 APR_DECLARE(apr_status_t) apr_pollset_create(apr_pollset_t **pollset,
312 apr_pollset_method_e method = APR_POLLSET_DEFAULT;
313 return apr_pollset_create_ex(pollset, size, p, flags, method);
316 APR_DECLARE(apr_status_t) apr_pollset_destroy(apr_pollset_t * pollset)
318 if (pollset->flags & APR_POLLSET_WAKEABLE ||
319 pollset->provider->cleanup)
320 return apr_pool_cleanup_run(pollset->pool, pollset,
326 APR_DECLARE(apr_status_t) apr_pollset_wakeup(apr_pollset_t *pollset)
328 if (pollset->flags & APR_POLLSET_WAKEABLE)
329 return apr_file_putc(1, pollset->wakeup_pipe[1]);
334 APR_DECLARE(apr_status_t) apr_pollset_add(apr_pollset_t *pollset,
335 const apr_pollfd_t *descriptor)
337 return (*pollset->provider->add)(pollset, descriptor);
340 APR_DECLARE(apr_status_t) apr_pollset_remove(apr_pollset_t *pollset,
341 const apr_pollfd_t *descriptor)
343 return (*pollset->provider->remove)(pollset, descriptor);
346 APR_DECLARE(apr_status_t) apr_pollset_poll(apr_pollset_t *pollset,
347 apr_interval_time_t timeout,
349 const apr_pollfd_t **descriptors)
351 return (*pollset->provider->poll)(pollset, timeout, num, descriptors);