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"
30 static apr_pollset_method_e pollset_default_method = POLLSET_DEFAULT_METHOD;
31 #if defined(HAVE_KQUEUE)
32 extern apr_pollcb_provider_t *apr_pollcb_provider_kqueue;
34 #if defined(HAVE_PORT_CREATE)
35 extern apr_pollcb_provider_t *apr_pollcb_provider_port;
37 #if defined(HAVE_EPOLL)
38 extern apr_pollcb_provider_t *apr_pollcb_provider_epoll;
40 #if defined(HAVE_POLL)
41 extern apr_pollcb_provider_t *apr_pollcb_provider_poll;
44 static apr_pollcb_provider_t *pollcb_provider(apr_pollset_method_e method)
46 apr_pollcb_provider_t *provider = NULL;
48 case APR_POLLSET_KQUEUE:
49 #if defined(HAVE_KQUEUE)
50 provider = apr_pollcb_provider_kqueue;
53 case APR_POLLSET_PORT:
54 #if defined(HAVE_PORT_CREATE)
55 provider = apr_pollcb_provider_port;
58 case APR_POLLSET_EPOLL:
59 #if defined(HAVE_EPOLL)
60 provider = apr_pollcb_provider_epoll;
63 case APR_POLLSET_POLL:
64 #if defined(HAVE_POLL)
65 provider = apr_pollcb_provider_poll;
68 case APR_POLLSET_SELECT:
69 case APR_POLLSET_AIO_MSGQ:
70 case APR_POLLSET_DEFAULT:
76 APR_DECLARE(apr_status_t) apr_pollcb_create_ex(apr_pollcb_t **ret_pollcb,
80 apr_pollset_method_e method)
84 apr_pollcb_provider_t *provider = NULL;
89 /* This will work only if ws2_32.dll has WSAPoll funtion.
90 * We could check the presence of the function here,
91 * but someone might implement other pollcb method in
94 if (method == APR_POLLSET_DEFAULT) {
95 method = APR_POLLSET_POLL;
99 if (method == APR_POLLSET_DEFAULT)
100 method = pollset_default_method;
101 while (provider == NULL) {
102 provider = pollcb_provider(method);
104 if ((flags & APR_POLLSET_NODEFAULT) == APR_POLLSET_NODEFAULT)
106 if (method == pollset_default_method)
108 method = pollset_default_method;
112 pollcb = apr_palloc(p, sizeof(*pollcb));
114 pollcb->nalloc = size;
116 pollcb->provider = provider;
118 rv = (*provider->create)(pollcb, size, p, flags);
119 if (rv == APR_ENOTIMPL) {
120 if (method == pollset_default_method) {
124 if ((flags & APR_POLLSET_NODEFAULT) == APR_POLLSET_NODEFAULT) {
128 /* Try with default provider */
129 provider = pollcb_provider(pollset_default_method);
133 rv = (*provider->create)(pollcb, size, p, flags);
134 if (rv != APR_SUCCESS) {
137 pollcb->provider = provider;
139 else if (rv != APR_SUCCESS) {
143 *ret_pollcb = pollcb;
147 APR_DECLARE(apr_status_t) apr_pollcb_create(apr_pollcb_t **pollcb,
152 apr_pollset_method_e method = APR_POLLSET_DEFAULT;
153 return apr_pollcb_create_ex(pollcb, size, p, flags, method);
156 APR_DECLARE(apr_status_t) apr_pollcb_add(apr_pollcb_t *pollcb,
157 apr_pollfd_t *descriptor)
159 return (*pollcb->provider->add)(pollcb, descriptor);
162 APR_DECLARE(apr_status_t) apr_pollcb_remove(apr_pollcb_t *pollcb,
163 apr_pollfd_t *descriptor)
165 return (*pollcb->provider->remove)(pollcb, descriptor);
169 APR_DECLARE(apr_status_t) apr_pollcb_poll(apr_pollcb_t *pollcb,
170 apr_interval_time_t timeout,
171 apr_pollcb_cb_t func,
174 return (*pollcb->provider->poll)(pollcb, timeout, func, baton);