1 /* $NetBSD: poll.c,v 1.1.1.2 2013/04/11 16:43:26 christos Exp $ */
2 /* $OpenBSD: poll.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
5 * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
6 * Copyright 2007-2012 Niels Provos and Nick Mathewson
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "event2/event-config.h"
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: poll.c,v 1.1.1.2 2013/04/11 16:43:26 christos Exp $");
34 #include <sys/types.h>
35 #ifdef _EVENT_HAVE_SYS_TIME_H
38 #include <sys/queue.h>
48 #include "event-internal.h"
49 #include "evsignal-internal.h"
50 #include "log-internal.h"
51 #include "evmap-internal.h"
52 #include "event2/thread.h"
53 #include "evthread-internal.h"
60 int event_count
; /* Highest number alloc */
61 int nfds
; /* Highest number used */
62 int realloc_copy
; /* True iff we must realloc
64 struct pollfd
*event_set
;
65 struct pollfd
*event_set_copy
;
68 static void *poll_init(struct event_base
*);
69 static int poll_add(struct event_base
*, int, short old
, short events
, void *_idx
);
70 static int poll_del(struct event_base
*, int, short old
, short events
, void *_idx
);
71 static int poll_dispatch(struct event_base
*, struct timeval
*);
72 static void poll_dealloc(struct event_base
*);
74 const struct eventop pollops
= {
81 0, /* doesn't need_reinit */
83 sizeof(struct pollidx
),
87 poll_init(struct event_base
*base
)
89 struct pollop
*pollop
;
91 if (!(pollop
= mm_calloc(1, sizeof(struct pollop
))))
99 #ifdef CHECK_INVARIANTS
101 poll_check_ok(struct pollop
*pop
)
106 for (i
= 0; i
< pop
->fd_count
; ++i
) {
107 idx
= pop
->idxplus1_by_fd
[i
]-1;
110 EVUTIL_ASSERT(pop
->event_set
[idx
].fd
== i
);
112 for (i
= 0; i
< pop
->nfds
; ++i
) {
113 struct pollfd
*pfd
= &pop
->event_set
[i
];
114 EVUTIL_ASSERT(pop
->idxplus1_by_fd
[pfd
->fd
] == i
+1);
118 #define poll_check_ok(pop)
122 poll_dispatch(struct event_base
*base
, struct timeval
*tv
)
126 struct pollop
*pop
= base
->evbase
;
127 struct pollfd
*event_set
;
133 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
134 if (base
->th_base_lock
) {
135 /* If we're using this backend in a multithreaded setting,
136 * then we need to work on a copy of event_set, so that we can
137 * let other threads modify the main event_set while we're
138 * polling. If we're not multithreaded, then we'll skip the
139 * copy step here to save memory and time. */
140 if (pop
->realloc_copy
) {
141 struct pollfd
*tmp
= mm_realloc(pop
->event_set_copy
,
142 pop
->event_count
* sizeof(struct pollfd
));
144 event_warn("realloc");
147 pop
->event_set_copy
= tmp
;
148 pop
->realloc_copy
= 0;
150 memcpy(pop
->event_set_copy
, pop
->event_set
,
151 sizeof(struct pollfd
)*nfds
);
152 event_set
= pop
->event_set_copy
;
154 event_set
= pop
->event_set
;
157 event_set
= pop
->event_set
;
161 msec
= evutil_tv_to_msec(tv
);
162 if (msec
< 0 || msec
> INT_MAX
)
166 EVBASE_RELEASE_LOCK(base
, th_base_lock
);
168 res
= poll(event_set
, nfds
, msec
);
170 EVBASE_ACQUIRE_LOCK(base
, th_base_lock
);
173 if (errno
!= EINTR
) {
181 event_debug(("%s: poll reports %d", __func__
, res
));
183 if (res
== 0 || nfds
== 0)
187 for (j
= 0; j
< nfds
; j
++) {
191 what
= event_set
[i
].revents
;
197 /* If the file gets closed notify */
198 if (what
& (POLLHUP
|POLLERR
))
199 what
|= POLLIN
|POLLOUT
;
207 evmap_io_active(base
, event_set
[i
].fd
, res
);
214 poll_add(struct event_base
*base
, int fd
, short old
, short events
, void *_idx
)
216 struct pollop
*pop
= base
->evbase
;
217 struct pollfd
*pfd
= NULL
;
218 struct pollidx
*idx
= _idx
;
221 EVUTIL_ASSERT((events
& EV_SIGNAL
) == 0);
222 if (!(events
& (EV_READ
|EV_WRITE
)))
226 if (pop
->nfds
+ 1 >= pop
->event_count
) {
227 struct pollfd
*tmp_event_set
;
230 if (pop
->event_count
< 32)
231 tmp_event_count
= 32;
233 tmp_event_count
= pop
->event_count
* 2;
235 /* We need more file descriptors */
236 tmp_event_set
= mm_realloc(pop
->event_set
,
237 tmp_event_count
* sizeof(struct pollfd
));
238 if (tmp_event_set
== NULL
) {
239 event_warn("realloc");
242 pop
->event_set
= tmp_event_set
;
244 pop
->event_count
= tmp_event_count
;
245 pop
->realloc_copy
= 1;
248 i
= idx
->idxplus1
- 1;
251 pfd
= &pop
->event_set
[i
];
254 pfd
= &pop
->event_set
[i
];
257 idx
->idxplus1
= i
+ 1;
261 if (events
& EV_WRITE
)
262 pfd
->events
|= POLLOUT
;
263 if (events
& EV_READ
)
264 pfd
->events
|= POLLIN
;
271 * Nothing to be done here.
275 poll_del(struct event_base
*base
, int fd
, short old
, short events
, void *_idx
)
277 struct pollop
*pop
= base
->evbase
;
278 struct pollfd
*pfd
= NULL
;
279 struct pollidx
*idx
= _idx
;
282 EVUTIL_ASSERT((events
& EV_SIGNAL
) == 0);
283 if (!(events
& (EV_READ
|EV_WRITE
)))
287 i
= idx
->idxplus1
- 1;
291 /* Do we still want to read or write? */
292 pfd
= &pop
->event_set
[i
];
293 if (events
& EV_READ
)
294 pfd
->events
&= ~POLLIN
;
295 if (events
& EV_WRITE
)
296 pfd
->events
&= ~POLLOUT
;
299 /* Another event cares about that fd. */
302 /* Okay, so we aren't interested in that fd anymore. */
306 if (i
!= pop
->nfds
) {
308 * Shift the last pollfd down into the now-unoccupied
311 memcpy(&pop
->event_set
[i
], &pop
->event_set
[pop
->nfds
],
312 sizeof(struct pollfd
));
313 idx
= evmap_io_get_fdinfo(&base
->io
, pop
->event_set
[i
].fd
);
315 EVUTIL_ASSERT(idx
->idxplus1
== pop
->nfds
+ 1);
316 idx
->idxplus1
= i
+ 1;
324 poll_dealloc(struct event_base
*base
)
326 struct pollop
*pop
= base
->evbase
;
330 mm_free(pop
->event_set
);
331 if (pop
->event_set_copy
)
332 mm_free(pop
->event_set_copy
);
334 memset(pop
, 0, sizeof(struct pollop
));