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.
19 #include "apr_arch_networkio.h"
21 APR_DECLARE(apr_status_t) apr_poll(apr_pollfd_t *aprset, apr_int32_t num,
22 apr_int32_t *nsds, apr_interval_time_t timeout)
26 int num_read = 0, num_write = 0, num_except = 0, num_total;
27 int pos_read, pos_write, pos_except;
29 for (i = 0; i < num; i++) {
30 if (aprset[i].desc_type == APR_POLL_SOCKET) {
31 num_read += (aprset[i].reqevents & APR_POLLIN) != 0;
32 num_write += (aprset[i].reqevents & APR_POLLOUT) != 0;
33 num_except += (aprset[i].reqevents & APR_POLLPRI) != 0;
37 num_total = num_read + num_write + num_except;
38 pollset = alloca(sizeof(int) * num_total);
39 memset(pollset, 0, sizeof(int) * num_total);
43 pos_except = pos_write + num_write;
45 for (i = 0; i < num; i++) {
46 if (aprset[i].desc_type == APR_POLL_SOCKET) {
47 if (aprset[i].reqevents & APR_POLLIN) {
48 pollset[pos_read++] = aprset[i].desc.s->socketdes;
51 if (aprset[i].reqevents & APR_POLLOUT) {
52 pollset[pos_write++] = aprset[i].desc.s->socketdes;
55 if (aprset[i].reqevents & APR_POLLPRI) {
56 pollset[pos_except++] = aprset[i].desc.s->socketdes;
59 aprset[i].rtnevents = 0;
64 timeout /= 1000; /* convert microseconds to milliseconds */
67 i = select(pollset, num_read, num_write, num_except, timeout);
71 return APR_FROM_OS_ERROR(sock_errno());
80 pos_except = pos_write + num_write;
82 for (i = 0; i < num; i++) {
83 if (aprset[i].desc_type == APR_POLL_SOCKET) {
84 if (aprset[i].reqevents & APR_POLLIN) {
85 if (pollset[pos_read++] > 0) {
86 aprset[i].rtnevents |= APR_POLLIN;
90 if (aprset[i].reqevents & APR_POLLOUT) {
91 if (pollset[pos_write++] > 0) {
92 aprset[i].rtnevents |= APR_POLLOUT;
96 if (aprset[i].reqevents & APR_POLLPRI) {
97 if (pollset[pos_except++] > 0) {
98 aprset[i].rtnevents |= APR_POLLPRI;