|
|
1 | /* |
|
|
2 | * libev event processing core, watcher management |
|
|
3 | * |
|
|
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
5 | * All rights reserved. |
|
|
6 | * |
|
|
7 | * Redistribution and use in source and binary forms, with or without |
|
|
8 | * modification, are permitted provided that the following conditions are |
|
|
9 | * met: |
|
|
10 | * |
|
|
11 | * * Redistributions of source code must retain the above copyright |
|
|
12 | * notice, this list of conditions and the following disclaimer. |
|
|
13 | * |
|
|
14 | * * Redistributions in binary form must reproduce the above |
|
|
15 | * copyright notice, this list of conditions and the following |
|
|
16 | * disclaimer in the documentation and/or other materials provided |
|
|
17 | * with the distribution. |
|
|
18 | * |
|
|
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
30 | */ |
|
|
31 | #if EV_USE_CONFIG_H |
|
|
32 | # include "config.h" |
|
|
33 | #endif |
|
|
34 | |
1 | #include <math.h> |
35 | #include <math.h> |
2 | #include <stdlib.h> |
36 | #include <stdlib.h> |
3 | #include <unistd.h> |
37 | #include <unistd.h> |
4 | #include <fcntl.h> |
38 | #include <fcntl.h> |
5 | #include <signal.h> |
39 | #include <signal.h> |
|
|
40 | #include <stddef.h> |
6 | |
41 | |
7 | #include <stdio.h> |
42 | #include <stdio.h> |
8 | |
43 | |
9 | #include <assert.h> |
44 | #include <assert.h> |
10 | #include <errno.h> |
45 | #include <errno.h> |
|
|
46 | #include <sys/types.h> |
|
|
47 | #include <sys/wait.h> |
11 | #include <sys/time.h> |
48 | #include <sys/time.h> |
12 | #include <time.h> |
49 | #include <time.h> |
13 | |
50 | |
14 | #define HAVE_EPOLL 1 |
|
|
15 | |
|
|
16 | #ifndef HAVE_MONOTONIC |
51 | #ifndef EV_USE_MONOTONIC |
|
|
52 | # define EV_USE_MONOTONIC 1 |
|
|
53 | #endif |
|
|
54 | |
17 | # ifdef CLOCK_MONOTONIC |
55 | #ifndef CLOCK_MONOTONIC |
|
|
56 | # undef EV_USE_MONOTONIC |
18 | # define HAVE_MONOTONIC 1 |
57 | # define EV_USE_MONOTONIC 0 |
19 | # endif |
58 | #endif |
20 | #endif |
|
|
21 | |
59 | |
22 | #ifndef HAVE_SELECT |
60 | #ifndef EV_USE_SELECT |
23 | # define HAVE_SELECT 1 |
61 | # define EV_USE_SELECT 1 |
24 | #endif |
62 | #endif |
25 | |
63 | |
26 | #ifndef HAVE_EPOLL |
64 | #ifndef EV_USE_EPOLL |
27 | # define HAVE_EPOLL 0 |
65 | # define EV_USE_EPOLL 0 |
28 | #endif |
66 | #endif |
29 | |
67 | |
|
|
68 | #ifndef CLOCK_REALTIME |
|
|
69 | # define EV_USE_REALTIME 0 |
|
|
70 | #endif |
30 | #ifndef HAVE_REALTIME |
71 | #ifndef EV_USE_REALTIME |
31 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
72 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
32 | #endif |
73 | #endif |
33 | |
74 | |
34 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
75 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
35 | #define MAX_BLOCKTIME 60. |
76 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
|
|
77 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
78 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
36 | |
79 | |
37 | #include "ev.h" |
80 | #include "ev.h" |
38 | |
81 | |
39 | typedef struct ev_watcher *W; |
82 | typedef struct ev_watcher *W; |
40 | typedef struct ev_watcher_list *WL; |
83 | typedef struct ev_watcher_list *WL; |
… | |
… | |
53 | /*****************************************************************************/ |
96 | /*****************************************************************************/ |
54 | |
97 | |
55 | ev_tstamp |
98 | ev_tstamp |
56 | ev_time (void) |
99 | ev_time (void) |
57 | { |
100 | { |
58 | #if HAVE_REALTIME |
101 | #if EV_USE_REALTIME |
59 | struct timespec ts; |
102 | struct timespec ts; |
60 | clock_gettime (CLOCK_REALTIME, &ts); |
103 | clock_gettime (CLOCK_REALTIME, &ts); |
61 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
104 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
62 | #else |
105 | #else |
63 | struct timeval tv; |
106 | struct timeval tv; |
… | |
… | |
67 | } |
110 | } |
68 | |
111 | |
69 | static ev_tstamp |
112 | static ev_tstamp |
70 | get_clock (void) |
113 | get_clock (void) |
71 | { |
114 | { |
72 | #if HAVE_MONOTONIC |
115 | #if EV_USE_MONOTONIC |
73 | if (have_monotonic) |
116 | if (have_monotonic) |
74 | { |
117 | { |
75 | struct timespec ts; |
118 | struct timespec ts; |
76 | clock_gettime (CLOCK_MONOTONIC, &ts); |
119 | clock_gettime (CLOCK_MONOTONIC, &ts); |
77 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
120 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
… | |
… | |
79 | #endif |
122 | #endif |
80 | |
123 | |
81 | return ev_time (); |
124 | return ev_time (); |
82 | } |
125 | } |
83 | |
126 | |
|
|
127 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
|
|
128 | |
84 | #define array_needsize(base,cur,cnt,init) \ |
129 | #define array_needsize(base,cur,cnt,init) \ |
85 | if ((cnt) > cur) \ |
130 | if ((cnt) > cur) \ |
86 | { \ |
131 | { \ |
87 | int newcnt = cur ? cur << 1 : 16; \ |
132 | int newcnt = cur; \ |
88 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
133 | do \ |
|
|
134 | { \ |
|
|
135 | newcnt = array_roundsize (base, newcnt << 1); \ |
|
|
136 | } \ |
|
|
137 | while ((cnt) > newcnt); \ |
|
|
138 | \ |
89 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
139 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
90 | init (base + cur, newcnt - cur); \ |
140 | init (base + cur, newcnt - cur); \ |
91 | cur = newcnt; \ |
141 | cur = newcnt; \ |
92 | } |
142 | } |
93 | |
143 | |
94 | /*****************************************************************************/ |
144 | /*****************************************************************************/ |
95 | |
145 | |
96 | typedef struct |
146 | typedef struct |
97 | { |
147 | { |
98 | struct ev_io *head; |
148 | struct ev_io *head; |
99 | unsigned char wev, rev; /* want, received event set */ |
149 | unsigned char events; |
|
|
150 | unsigned char reify; |
100 | } ANFD; |
151 | } ANFD; |
101 | |
152 | |
102 | static ANFD *anfds; |
153 | static ANFD *anfds; |
103 | static int anfdmax; |
154 | static int anfdmax; |
104 | |
155 | |
105 | static int *fdchanges; |
|
|
106 | static int fdchangemax, fdchangecnt; |
|
|
107 | |
|
|
108 | static void |
156 | static void |
109 | anfds_init (ANFD *base, int count) |
157 | anfds_init (ANFD *base, int count) |
110 | { |
158 | { |
111 | while (count--) |
159 | while (count--) |
112 | { |
160 | { |
113 | base->head = 0; |
161 | base->head = 0; |
114 | base->wev = base->rev = EV_NONE; |
162 | base->events = EV_NONE; |
|
|
163 | base->reify = 0; |
|
|
164 | |
115 | ++base; |
165 | ++base; |
116 | } |
166 | } |
117 | } |
167 | } |
118 | |
168 | |
119 | typedef struct |
169 | typedef struct |
… | |
… | |
126 | static int pendingmax, pendingcnt; |
176 | static int pendingmax, pendingcnt; |
127 | |
177 | |
128 | static void |
178 | static void |
129 | event (W w, int events) |
179 | event (W w, int events) |
130 | { |
180 | { |
|
|
181 | if (w->pending) |
|
|
182 | { |
|
|
183 | pendings [w->pending - 1].events |= events; |
|
|
184 | return; |
|
|
185 | } |
|
|
186 | |
131 | w->pending = ++pendingcnt; |
187 | w->pending = ++pendingcnt; |
132 | array_needsize (pendings, pendingmax, pendingcnt, ); |
188 | array_needsize (pendings, pendingmax, pendingcnt, ); |
133 | pendings [pendingcnt - 1].w = w; |
189 | pendings [pendingcnt - 1].w = w; |
134 | pendings [pendingcnt - 1].events = events; |
190 | pendings [pendingcnt - 1].events = events; |
135 | } |
191 | } |
136 | |
192 | |
137 | static void |
193 | static void |
|
|
194 | queue_events (W *events, int eventcnt, int type) |
|
|
195 | { |
|
|
196 | int i; |
|
|
197 | |
|
|
198 | for (i = 0; i < eventcnt; ++i) |
|
|
199 | event (events [i], type); |
|
|
200 | } |
|
|
201 | |
|
|
202 | static void |
138 | fd_event (int fd, int events) |
203 | fd_event (int fd, int events) |
139 | { |
204 | { |
140 | ANFD *anfd = anfds + fd; |
205 | ANFD *anfd = anfds + fd; |
141 | struct ev_io *w; |
206 | struct ev_io *w; |
142 | |
207 | |
… | |
… | |
147 | if (ev) |
212 | if (ev) |
148 | event ((W)w, ev); |
213 | event ((W)w, ev); |
149 | } |
214 | } |
150 | } |
215 | } |
151 | |
216 | |
|
|
217 | /*****************************************************************************/ |
|
|
218 | |
|
|
219 | static int *fdchanges; |
|
|
220 | static int fdchangemax, fdchangecnt; |
|
|
221 | |
152 | static void |
222 | static void |
153 | queue_events (W *events, int eventcnt, int type) |
223 | fd_reify (void) |
154 | { |
224 | { |
155 | int i; |
225 | int i; |
156 | |
226 | |
157 | for (i = 0; i < eventcnt; ++i) |
227 | for (i = 0; i < fdchangecnt; ++i) |
158 | event (events [i], type); |
228 | { |
|
|
229 | int fd = fdchanges [i]; |
|
|
230 | ANFD *anfd = anfds + fd; |
|
|
231 | struct ev_io *w; |
|
|
232 | |
|
|
233 | int events = 0; |
|
|
234 | |
|
|
235 | for (w = anfd->head; w; w = w->next) |
|
|
236 | events |= w->events; |
|
|
237 | |
|
|
238 | anfd->reify = 0; |
|
|
239 | |
|
|
240 | if (anfd->events != events) |
|
|
241 | { |
|
|
242 | method_modify (fd, anfd->events, events); |
|
|
243 | anfd->events = events; |
|
|
244 | } |
|
|
245 | } |
|
|
246 | |
|
|
247 | fdchangecnt = 0; |
|
|
248 | } |
|
|
249 | |
|
|
250 | static void |
|
|
251 | fd_change (int fd) |
|
|
252 | { |
|
|
253 | if (anfds [fd].reify || fdchangecnt < 0) |
|
|
254 | return; |
|
|
255 | |
|
|
256 | anfds [fd].reify = 1; |
|
|
257 | |
|
|
258 | ++fdchangecnt; |
|
|
259 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
260 | fdchanges [fdchangecnt - 1] = fd; |
|
|
261 | } |
|
|
262 | |
|
|
263 | /* called on EBADF to verify fds */ |
|
|
264 | static void |
|
|
265 | fd_recheck (void) |
|
|
266 | { |
|
|
267 | int fd; |
|
|
268 | |
|
|
269 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
270 | if (anfds [fd].events) |
|
|
271 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
272 | while (anfds [fd].head) |
|
|
273 | { |
|
|
274 | ev_io_stop (anfds [fd].head); |
|
|
275 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
|
|
276 | } |
159 | } |
277 | } |
160 | |
278 | |
161 | /*****************************************************************************/ |
279 | /*****************************************************************************/ |
162 | |
280 | |
163 | static struct ev_timer **timers; |
281 | static struct ev_timer **timers; |
… | |
… | |
210 | /*****************************************************************************/ |
328 | /*****************************************************************************/ |
211 | |
329 | |
212 | typedef struct |
330 | typedef struct |
213 | { |
331 | { |
214 | struct ev_signal *head; |
332 | struct ev_signal *head; |
215 | sig_atomic_t gotsig; |
333 | sig_atomic_t volatile gotsig; |
216 | } ANSIG; |
334 | } ANSIG; |
217 | |
335 | |
218 | static ANSIG *signals; |
336 | static ANSIG *signals; |
219 | static int signalmax; |
337 | static int signalmax; |
220 | |
338 | |
221 | static int sigpipe [2]; |
339 | static int sigpipe [2]; |
222 | static sig_atomic_t gotsig; |
340 | static sig_atomic_t volatile gotsig; |
223 | static struct ev_io sigev; |
341 | static struct ev_io sigev; |
224 | |
342 | |
225 | static void |
343 | static void |
226 | signals_init (ANSIG *base, int count) |
344 | signals_init (ANSIG *base, int count) |
227 | { |
345 | { |
228 | while (count--) |
346 | while (count--) |
229 | { |
347 | { |
230 | base->head = 0; |
348 | base->head = 0; |
231 | base->gotsig = 0; |
349 | base->gotsig = 0; |
|
|
350 | |
232 | ++base; |
351 | ++base; |
233 | } |
352 | } |
234 | } |
353 | } |
235 | |
354 | |
236 | static void |
355 | static void |
… | |
… | |
239 | signals [signum - 1].gotsig = 1; |
358 | signals [signum - 1].gotsig = 1; |
240 | |
359 | |
241 | if (!gotsig) |
360 | if (!gotsig) |
242 | { |
361 | { |
243 | gotsig = 1; |
362 | gotsig = 1; |
244 | write (sigpipe [1], &gotsig, 1); |
363 | write (sigpipe [1], &signum, 1); |
245 | } |
364 | } |
246 | } |
365 | } |
247 | |
366 | |
248 | static void |
367 | static void |
249 | sigcb (struct ev_io *iow, int revents) |
368 | sigcb (struct ev_io *iow, int revents) |
250 | { |
369 | { |
251 | struct ev_signal *w; |
370 | struct ev_signal *w; |
252 | int sig; |
371 | int sig; |
253 | |
372 | |
|
|
373 | read (sigpipe [0], &revents, 1); |
254 | gotsig = 0; |
374 | gotsig = 0; |
255 | read (sigpipe [0], &revents, 1); |
|
|
256 | |
375 | |
257 | for (sig = signalmax; sig--; ) |
376 | for (sig = signalmax; sig--; ) |
258 | if (signals [sig].gotsig) |
377 | if (signals [sig].gotsig) |
259 | { |
378 | { |
260 | signals [sig].gotsig = 0; |
379 | signals [sig].gotsig = 0; |
… | |
… | |
272 | |
391 | |
273 | /* rather than sort out wether we really need nb, set it */ |
392 | /* rather than sort out wether we really need nb, set it */ |
274 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
393 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
275 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
394 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
276 | |
395 | |
277 | evio_set (&sigev, sigpipe [0], EV_READ); |
396 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
278 | evio_start (&sigev); |
397 | ev_io_start (&sigev); |
279 | } |
398 | } |
280 | |
399 | |
281 | /*****************************************************************************/ |
400 | /*****************************************************************************/ |
282 | |
401 | |
283 | static struct ev_idle **idles; |
402 | static struct ev_idle **idles; |
284 | static int idlemax, idlecnt; |
403 | static int idlemax, idlecnt; |
285 | |
404 | |
|
|
405 | static struct ev_prepare **prepares; |
|
|
406 | static int preparemax, preparecnt; |
|
|
407 | |
286 | static struct ev_check **checks; |
408 | static struct ev_check **checks; |
287 | static int checkmax, checkcnt; |
409 | static int checkmax, checkcnt; |
288 | |
410 | |
289 | /*****************************************************************************/ |
411 | /*****************************************************************************/ |
290 | |
412 | |
|
|
413 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
414 | static struct ev_signal childev; |
|
|
415 | |
|
|
416 | #ifndef WCONTINUED |
|
|
417 | # define WCONTINUED 0 |
|
|
418 | #endif |
|
|
419 | |
|
|
420 | static void |
|
|
421 | childcb (struct ev_signal *sw, int revents) |
|
|
422 | { |
|
|
423 | struct ev_child *w; |
|
|
424 | int pid, status; |
|
|
425 | |
|
|
426 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
427 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
428 | if (w->pid == pid || w->pid == -1) |
|
|
429 | { |
|
|
430 | w->status = status; |
|
|
431 | event ((W)w, EV_CHILD); |
|
|
432 | } |
|
|
433 | } |
|
|
434 | |
|
|
435 | /*****************************************************************************/ |
|
|
436 | |
291 | #if HAVE_EPOLL |
437 | #if EV_USE_EPOLL |
292 | # include "ev_epoll.c" |
438 | # include "ev_epoll.c" |
293 | #endif |
439 | #endif |
294 | #if HAVE_SELECT |
440 | #if EV_USE_SELECT |
295 | # include "ev_select.c" |
441 | # include "ev_select.c" |
296 | #endif |
442 | #endif |
297 | |
443 | |
|
|
444 | int |
|
|
445 | ev_version_major (void) |
|
|
446 | { |
|
|
447 | return EV_VERSION_MAJOR; |
|
|
448 | } |
|
|
449 | |
|
|
450 | int |
|
|
451 | ev_version_minor (void) |
|
|
452 | { |
|
|
453 | return EV_VERSION_MINOR; |
|
|
454 | } |
|
|
455 | |
298 | int ev_init (int flags) |
456 | int ev_init (int flags) |
299 | { |
457 | { |
300 | #if HAVE_MONOTONIC |
|
|
301 | { |
|
|
302 | struct timespec ts; |
|
|
303 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
304 | have_monotonic = 1; |
|
|
305 | } |
|
|
306 | #endif |
|
|
307 | |
|
|
308 | ev_now = ev_time (); |
|
|
309 | now = get_clock (); |
|
|
310 | diff = ev_now - now; |
|
|
311 | |
|
|
312 | if (pipe (sigpipe)) |
|
|
313 | return 0; |
|
|
314 | |
|
|
315 | ev_method = EVMETHOD_NONE; |
|
|
316 | #if HAVE_EPOLL |
|
|
317 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
318 | #endif |
|
|
319 | #if HAVE_SELECT |
|
|
320 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
321 | #endif |
|
|
322 | |
|
|
323 | if (ev_method) |
458 | if (!ev_method) |
|
|
459 | { |
|
|
460 | #if EV_USE_MONOTONIC |
324 | { |
461 | { |
|
|
462 | struct timespec ts; |
|
|
463 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
464 | have_monotonic = 1; |
|
|
465 | } |
|
|
466 | #endif |
|
|
467 | |
|
|
468 | ev_now = ev_time (); |
|
|
469 | now = get_clock (); |
|
|
470 | diff = ev_now - now; |
|
|
471 | |
|
|
472 | if (pipe (sigpipe)) |
|
|
473 | return 0; |
|
|
474 | |
|
|
475 | ev_method = EVMETHOD_NONE; |
|
|
476 | #if EV_USE_EPOLL |
|
|
477 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
478 | #endif |
|
|
479 | #if EV_USE_SELECT |
|
|
480 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
481 | #endif |
|
|
482 | |
|
|
483 | if (ev_method) |
|
|
484 | { |
325 | evw_init (&sigev, sigcb); |
485 | ev_watcher_init (&sigev, sigcb); |
326 | siginit (); |
486 | siginit (); |
|
|
487 | |
|
|
488 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
489 | ev_signal_start (&childev); |
|
|
490 | } |
327 | } |
491 | } |
328 | |
492 | |
329 | return ev_method; |
493 | return ev_method; |
330 | } |
494 | } |
331 | |
495 | |
332 | /*****************************************************************************/ |
496 | /*****************************************************************************/ |
333 | |
497 | |
334 | void ev_prefork (void) |
498 | void |
|
|
499 | ev_fork_prepare (void) |
335 | { |
500 | { |
336 | /* nop */ |
501 | /* nop */ |
337 | } |
502 | } |
338 | |
503 | |
|
|
504 | void |
339 | void ev_postfork_parent (void) |
505 | ev_fork_parent (void) |
340 | { |
506 | { |
341 | /* nop */ |
507 | /* nop */ |
342 | } |
508 | } |
343 | |
509 | |
|
|
510 | void |
344 | void ev_postfork_child (void) |
511 | ev_fork_child (void) |
345 | { |
512 | { |
346 | #if HAVE_EPOLL |
513 | #if EV_USE_EPOLL |
347 | if (ev_method == EVMETHOD_EPOLL) |
514 | if (ev_method == EVMETHOD_EPOLL) |
348 | epoll_postfork_child (); |
515 | epoll_postfork_child (); |
349 | #endif |
516 | #endif |
350 | |
517 | |
351 | evio_stop (&sigev); |
518 | ev_io_stop (&sigev); |
352 | close (sigpipe [0]); |
519 | close (sigpipe [0]); |
353 | close (sigpipe [1]); |
520 | close (sigpipe [1]); |
354 | pipe (sigpipe); |
521 | pipe (sigpipe); |
355 | siginit (); |
522 | siginit (); |
356 | } |
523 | } |
357 | |
524 | |
358 | /*****************************************************************************/ |
525 | /*****************************************************************************/ |
359 | |
526 | |
360 | static void |
527 | static void |
361 | fd_reify (void) |
|
|
362 | { |
|
|
363 | int i; |
|
|
364 | |
|
|
365 | for (i = 0; i < fdchangecnt; ++i) |
|
|
366 | { |
|
|
367 | int fd = fdchanges [i]; |
|
|
368 | ANFD *anfd = anfds + fd; |
|
|
369 | struct ev_io *w; |
|
|
370 | |
|
|
371 | int wev = 0; |
|
|
372 | |
|
|
373 | for (w = anfd->head; w; w = w->next) |
|
|
374 | wev |= w->events; |
|
|
375 | |
|
|
376 | if (anfd->wev != wev) |
|
|
377 | { |
|
|
378 | method_modify (fd, anfd->wev, wev); |
|
|
379 | anfd->wev = wev; |
|
|
380 | } |
|
|
381 | } |
|
|
382 | |
|
|
383 | fdchangecnt = 0; |
|
|
384 | } |
|
|
385 | |
|
|
386 | static void |
|
|
387 | call_pending () |
528 | call_pending (void) |
388 | { |
529 | { |
389 | int i; |
530 | while (pendingcnt) |
390 | |
|
|
391 | for (i = 0; i < pendingcnt; ++i) |
|
|
392 | { |
531 | { |
393 | ANPENDING *p = pendings + i; |
532 | ANPENDING *p = pendings + --pendingcnt; |
394 | |
533 | |
395 | if (p->w) |
534 | if (p->w) |
396 | { |
535 | { |
397 | p->w->pending = 0; |
536 | p->w->pending = 0; |
398 | p->w->cb (p->w, p->events); |
537 | p->w->cb (p->w, p->events); |
399 | } |
538 | } |
400 | } |
539 | } |
401 | |
|
|
402 | pendingcnt = 0; |
|
|
403 | } |
540 | } |
404 | |
541 | |
405 | static void |
542 | static void |
406 | timers_reify () |
543 | timers_reify (void) |
407 | { |
544 | { |
408 | while (timercnt && timers [0]->at <= now) |
545 | while (timercnt && timers [0]->at <= now) |
409 | { |
546 | { |
410 | struct ev_timer *w = timers [0]; |
547 | struct ev_timer *w = timers [0]; |
411 | |
548 | |
412 | /* first reschedule or stop timer */ |
549 | /* first reschedule or stop timer */ |
413 | if (w->repeat) |
550 | if (w->repeat) |
414 | { |
551 | { |
|
|
552 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
415 | w->at = now + w->repeat; |
553 | w->at = now + w->repeat; |
416 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
417 | downheap ((WT *)timers, timercnt, 0); |
554 | downheap ((WT *)timers, timercnt, 0); |
418 | } |
555 | } |
419 | else |
556 | else |
420 | evtimer_stop (w); /* nonrepeating: stop timer */ |
557 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
421 | |
558 | |
422 | event ((W)w, EV_TIMEOUT); |
559 | event ((W)w, EV_TIMEOUT); |
423 | } |
560 | } |
424 | } |
561 | } |
425 | |
562 | |
426 | static void |
563 | static void |
427 | periodics_reify () |
564 | periodics_reify (void) |
428 | { |
565 | { |
429 | while (periodiccnt && periodics [0]->at <= ev_now) |
566 | while (periodiccnt && periodics [0]->at <= ev_now) |
430 | { |
567 | { |
431 | struct ev_periodic *w = periodics [0]; |
568 | struct ev_periodic *w = periodics [0]; |
432 | |
569 | |
433 | /* first reschedule or stop timer */ |
570 | /* first reschedule or stop timer */ |
434 | if (w->interval) |
571 | if (w->interval) |
435 | { |
572 | { |
436 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
573 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
437 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
574 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
438 | downheap ((WT *)periodics, periodiccnt, 0); |
575 | downheap ((WT *)periodics, periodiccnt, 0); |
439 | } |
576 | } |
440 | else |
577 | else |
441 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
578 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
442 | |
579 | |
443 | event ((W)w, EV_TIMEOUT); |
580 | event ((W)w, EV_PERIODIC); |
444 | } |
581 | } |
445 | } |
582 | } |
446 | |
583 | |
447 | static void |
584 | static void |
448 | periodics_reschedule (ev_tstamp diff) |
585 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
458 | { |
595 | { |
459 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
596 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
460 | |
597 | |
461 | if (fabs (diff) >= 1e-4) |
598 | if (fabs (diff) >= 1e-4) |
462 | { |
599 | { |
463 | evperiodic_stop (w); |
600 | ev_periodic_stop (w); |
464 | evperiodic_start (w); |
601 | ev_periodic_start (w); |
465 | |
602 | |
466 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
603 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
467 | } |
604 | } |
468 | } |
605 | } |
469 | } |
606 | } |
470 | } |
607 | } |
471 | |
608 | |
472 | static void |
609 | static void |
473 | time_update () |
610 | time_update (void) |
474 | { |
611 | { |
475 | int i; |
612 | int i; |
476 | |
613 | |
477 | ev_now = ev_time (); |
614 | ev_now = ev_time (); |
478 | |
615 | |
… | |
… | |
512 | int ev_loop_done; |
649 | int ev_loop_done; |
513 | |
650 | |
514 | void ev_loop (int flags) |
651 | void ev_loop (int flags) |
515 | { |
652 | { |
516 | double block; |
653 | double block; |
517 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
654 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
518 | |
|
|
519 | if (checkcnt) |
|
|
520 | { |
|
|
521 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
522 | call_pending (); |
|
|
523 | } |
|
|
524 | |
655 | |
525 | do |
656 | do |
526 | { |
657 | { |
|
|
658 | /* queue check watchers (and execute them) */ |
|
|
659 | if (preparecnt) |
|
|
660 | { |
|
|
661 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
662 | call_pending (); |
|
|
663 | } |
|
|
664 | |
527 | /* update fd-related kernel structures */ |
665 | /* update fd-related kernel structures */ |
528 | fd_reify (); |
666 | fd_reify (); |
529 | |
667 | |
530 | /* calculate blocking time */ |
668 | /* calculate blocking time */ |
531 | |
669 | |
532 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
670 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
671 | always have timers, we just calculate it always */ |
533 | ev_now = ev_time (); |
672 | ev_now = ev_time (); |
534 | |
673 | |
535 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
674 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
536 | block = 0.; |
675 | block = 0.; |
537 | else |
676 | else |
… | |
… | |
557 | |
696 | |
558 | /* update ev_now, do magic */ |
697 | /* update ev_now, do magic */ |
559 | time_update (); |
698 | time_update (); |
560 | |
699 | |
561 | /* queue pending timers and reschedule them */ |
700 | /* queue pending timers and reschedule them */ |
|
|
701 | timers_reify (); /* relative timers called last */ |
562 | periodics_reify (); /* absolute timers first */ |
702 | periodics_reify (); /* absolute timers called first */ |
563 | timers_reify (); /* relative timers second */ |
|
|
564 | |
703 | |
565 | /* queue idle watchers unless io or timers are pending */ |
704 | /* queue idle watchers unless io or timers are pending */ |
566 | if (!pendingcnt) |
705 | if (!pendingcnt) |
567 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
706 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
568 | |
707 | |
569 | /* queue check and possibly idle watchers */ |
708 | /* queue check watchers, to be executed first */ |
|
|
709 | if (checkcnt) |
570 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
710 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
571 | |
711 | |
572 | call_pending (); |
712 | call_pending (); |
573 | } |
713 | } |
574 | while (!ev_loop_done); |
714 | while (!ev_loop_done); |
575 | |
715 | |
… | |
… | |
600 | head = &(*head)->next; |
740 | head = &(*head)->next; |
601 | } |
741 | } |
602 | } |
742 | } |
603 | |
743 | |
604 | static void |
744 | static void |
|
|
745 | ev_clear_pending (W w) |
|
|
746 | { |
|
|
747 | if (w->pending) |
|
|
748 | { |
|
|
749 | pendings [w->pending - 1].w = 0; |
|
|
750 | w->pending = 0; |
|
|
751 | } |
|
|
752 | } |
|
|
753 | |
|
|
754 | static void |
605 | ev_start (W w, int active) |
755 | ev_start (W w, int active) |
606 | { |
756 | { |
607 | w->pending = 0; |
|
|
608 | w->active = active; |
757 | w->active = active; |
609 | } |
758 | } |
610 | |
759 | |
611 | static void |
760 | static void |
612 | ev_stop (W w) |
761 | ev_stop (W w) |
613 | { |
762 | { |
614 | if (w->pending) |
|
|
615 | pendings [w->pending - 1].w = 0; |
|
|
616 | |
|
|
617 | w->active = 0; |
763 | w->active = 0; |
618 | } |
764 | } |
619 | |
765 | |
620 | /*****************************************************************************/ |
766 | /*****************************************************************************/ |
621 | |
767 | |
622 | void |
768 | void |
623 | evio_start (struct ev_io *w) |
769 | ev_io_start (struct ev_io *w) |
624 | { |
770 | { |
625 | if (ev_is_active (w)) |
|
|
626 | return; |
|
|
627 | |
|
|
628 | int fd = w->fd; |
771 | int fd = w->fd; |
|
|
772 | |
|
|
773 | if (ev_is_active (w)) |
|
|
774 | return; |
|
|
775 | |
|
|
776 | assert (("ev_io_start called with negative fd", fd >= 0)); |
629 | |
777 | |
630 | ev_start ((W)w, 1); |
778 | ev_start ((W)w, 1); |
631 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
779 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
632 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
780 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
633 | |
781 | |
634 | ++fdchangecnt; |
782 | fd_change (fd); |
635 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
636 | fdchanges [fdchangecnt - 1] = fd; |
|
|
637 | } |
783 | } |
638 | |
784 | |
639 | void |
785 | void |
640 | evio_stop (struct ev_io *w) |
786 | ev_io_stop (struct ev_io *w) |
641 | { |
787 | { |
|
|
788 | ev_clear_pending ((W)w); |
642 | if (!ev_is_active (w)) |
789 | if (!ev_is_active (w)) |
643 | return; |
790 | return; |
644 | |
791 | |
645 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
792 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
646 | ev_stop ((W)w); |
793 | ev_stop ((W)w); |
647 | |
794 | |
648 | ++fdchangecnt; |
795 | fd_change (w->fd); |
649 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
650 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
651 | } |
796 | } |
652 | |
797 | |
653 | |
|
|
654 | void |
798 | void |
655 | evtimer_start (struct ev_timer *w) |
799 | ev_timer_start (struct ev_timer *w) |
656 | { |
800 | { |
657 | if (ev_is_active (w)) |
801 | if (ev_is_active (w)) |
658 | return; |
802 | return; |
659 | |
803 | |
660 | w->at += now; |
804 | w->at += now; |
661 | |
805 | |
662 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
806 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
663 | |
807 | |
664 | ev_start ((W)w, ++timercnt); |
808 | ev_start ((W)w, ++timercnt); |
665 | array_needsize (timers, timermax, timercnt, ); |
809 | array_needsize (timers, timermax, timercnt, ); |
666 | timers [timercnt - 1] = w; |
810 | timers [timercnt - 1] = w; |
667 | upheap ((WT *)timers, timercnt - 1); |
811 | upheap ((WT *)timers, timercnt - 1); |
668 | } |
812 | } |
669 | |
813 | |
670 | void |
814 | void |
671 | evtimer_stop (struct ev_timer *w) |
815 | ev_timer_stop (struct ev_timer *w) |
672 | { |
816 | { |
|
|
817 | ev_clear_pending ((W)w); |
673 | if (!ev_is_active (w)) |
818 | if (!ev_is_active (w)) |
674 | return; |
819 | return; |
675 | |
820 | |
676 | if (w->active < timercnt--) |
821 | if (w->active < timercnt--) |
677 | { |
822 | { |
… | |
… | |
683 | |
828 | |
684 | ev_stop ((W)w); |
829 | ev_stop ((W)w); |
685 | } |
830 | } |
686 | |
831 | |
687 | void |
832 | void |
688 | evtimer_again (struct ev_timer *w) |
833 | ev_timer_again (struct ev_timer *w) |
689 | { |
834 | { |
690 | if (ev_is_active (w)) |
835 | if (ev_is_active (w)) |
691 | { |
836 | { |
692 | if (w->repeat) |
837 | if (w->repeat) |
693 | { |
838 | { |
694 | w->at = now + w->repeat; |
839 | w->at = now + w->repeat; |
695 | downheap ((WT *)timers, timercnt, w->active - 1); |
840 | downheap ((WT *)timers, timercnt, w->active - 1); |
696 | } |
841 | } |
697 | else |
842 | else |
698 | evtimer_stop (w); |
843 | ev_timer_stop (w); |
699 | } |
844 | } |
700 | else if (w->repeat) |
845 | else if (w->repeat) |
701 | evtimer_start (w); |
846 | ev_timer_start (w); |
702 | } |
847 | } |
703 | |
848 | |
704 | void |
849 | void |
705 | evperiodic_start (struct ev_periodic *w) |
850 | ev_periodic_start (struct ev_periodic *w) |
706 | { |
851 | { |
707 | if (ev_is_active (w)) |
852 | if (ev_is_active (w)) |
708 | return; |
853 | return; |
709 | |
854 | |
710 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
855 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
711 | |
856 | |
712 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
857 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
713 | if (w->interval) |
858 | if (w->interval) |
714 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
859 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
715 | |
860 | |
… | |
… | |
718 | periodics [periodiccnt - 1] = w; |
863 | periodics [periodiccnt - 1] = w; |
719 | upheap ((WT *)periodics, periodiccnt - 1); |
864 | upheap ((WT *)periodics, periodiccnt - 1); |
720 | } |
865 | } |
721 | |
866 | |
722 | void |
867 | void |
723 | evperiodic_stop (struct ev_periodic *w) |
868 | ev_periodic_stop (struct ev_periodic *w) |
724 | { |
869 | { |
|
|
870 | ev_clear_pending ((W)w); |
725 | if (!ev_is_active (w)) |
871 | if (!ev_is_active (w)) |
726 | return; |
872 | return; |
727 | |
873 | |
728 | if (w->active < periodiccnt--) |
874 | if (w->active < periodiccnt--) |
729 | { |
875 | { |
… | |
… | |
733 | |
879 | |
734 | ev_stop ((W)w); |
880 | ev_stop ((W)w); |
735 | } |
881 | } |
736 | |
882 | |
737 | void |
883 | void |
738 | evsignal_start (struct ev_signal *w) |
884 | ev_signal_start (struct ev_signal *w) |
739 | { |
885 | { |
740 | if (ev_is_active (w)) |
886 | if (ev_is_active (w)) |
741 | return; |
887 | return; |
|
|
888 | |
|
|
889 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
742 | |
890 | |
743 | ev_start ((W)w, 1); |
891 | ev_start ((W)w, 1); |
744 | array_needsize (signals, signalmax, w->signum, signals_init); |
892 | array_needsize (signals, signalmax, w->signum, signals_init); |
745 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
893 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
746 | |
894 | |
… | |
… | |
753 | sigaction (w->signum, &sa, 0); |
901 | sigaction (w->signum, &sa, 0); |
754 | } |
902 | } |
755 | } |
903 | } |
756 | |
904 | |
757 | void |
905 | void |
758 | evsignal_stop (struct ev_signal *w) |
906 | ev_signal_stop (struct ev_signal *w) |
759 | { |
907 | { |
|
|
908 | ev_clear_pending ((W)w); |
760 | if (!ev_is_active (w)) |
909 | if (!ev_is_active (w)) |
761 | return; |
910 | return; |
762 | |
911 | |
763 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
912 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
764 | ev_stop ((W)w); |
913 | ev_stop ((W)w); |
765 | |
914 | |
766 | if (!signals [w->signum - 1].head) |
915 | if (!signals [w->signum - 1].head) |
767 | signal (w->signum, SIG_DFL); |
916 | signal (w->signum, SIG_DFL); |
768 | } |
917 | } |
769 | |
918 | |
|
|
919 | void |
770 | void evidle_start (struct ev_idle *w) |
920 | ev_idle_start (struct ev_idle *w) |
771 | { |
921 | { |
772 | if (ev_is_active (w)) |
922 | if (ev_is_active (w)) |
773 | return; |
923 | return; |
774 | |
924 | |
775 | ev_start ((W)w, ++idlecnt); |
925 | ev_start ((W)w, ++idlecnt); |
776 | array_needsize (idles, idlemax, idlecnt, ); |
926 | array_needsize (idles, idlemax, idlecnt, ); |
777 | idles [idlecnt - 1] = w; |
927 | idles [idlecnt - 1] = w; |
778 | } |
928 | } |
779 | |
929 | |
|
|
930 | void |
780 | void evidle_stop (struct ev_idle *w) |
931 | ev_idle_stop (struct ev_idle *w) |
781 | { |
932 | { |
|
|
933 | ev_clear_pending ((W)w); |
|
|
934 | if (ev_is_active (w)) |
|
|
935 | return; |
|
|
936 | |
782 | idles [w->active - 1] = idles [--idlecnt]; |
937 | idles [w->active - 1] = idles [--idlecnt]; |
783 | ev_stop ((W)w); |
938 | ev_stop ((W)w); |
784 | } |
939 | } |
785 | |
940 | |
|
|
941 | void |
|
|
942 | ev_prepare_start (struct ev_prepare *w) |
|
|
943 | { |
|
|
944 | if (ev_is_active (w)) |
|
|
945 | return; |
|
|
946 | |
|
|
947 | ev_start ((W)w, ++preparecnt); |
|
|
948 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
949 | prepares [preparecnt - 1] = w; |
|
|
950 | } |
|
|
951 | |
|
|
952 | void |
|
|
953 | ev_prepare_stop (struct ev_prepare *w) |
|
|
954 | { |
|
|
955 | ev_clear_pending ((W)w); |
|
|
956 | if (ev_is_active (w)) |
|
|
957 | return; |
|
|
958 | |
|
|
959 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
960 | ev_stop ((W)w); |
|
|
961 | } |
|
|
962 | |
|
|
963 | void |
786 | void evcheck_start (struct ev_check *w) |
964 | ev_check_start (struct ev_check *w) |
787 | { |
965 | { |
788 | if (ev_is_active (w)) |
966 | if (ev_is_active (w)) |
789 | return; |
967 | return; |
790 | |
968 | |
791 | ev_start ((W)w, ++checkcnt); |
969 | ev_start ((W)w, ++checkcnt); |
792 | array_needsize (checks, checkmax, checkcnt, ); |
970 | array_needsize (checks, checkmax, checkcnt, ); |
793 | checks [checkcnt - 1] = w; |
971 | checks [checkcnt - 1] = w; |
794 | } |
972 | } |
795 | |
973 | |
|
|
974 | void |
796 | void evcheck_stop (struct ev_check *w) |
975 | ev_check_stop (struct ev_check *w) |
797 | { |
976 | { |
|
|
977 | ev_clear_pending ((W)w); |
|
|
978 | if (ev_is_active (w)) |
|
|
979 | return; |
|
|
980 | |
798 | checks [w->active - 1] = checks [--checkcnt]; |
981 | checks [w->active - 1] = checks [--checkcnt]; |
799 | ev_stop ((W)w); |
982 | ev_stop ((W)w); |
800 | } |
983 | } |
801 | |
984 | |
|
|
985 | void |
|
|
986 | ev_child_start (struct ev_child *w) |
|
|
987 | { |
|
|
988 | if (ev_is_active (w)) |
|
|
989 | return; |
|
|
990 | |
|
|
991 | ev_start ((W)w, 1); |
|
|
992 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
993 | } |
|
|
994 | |
|
|
995 | void |
|
|
996 | ev_child_stop (struct ev_child *w) |
|
|
997 | { |
|
|
998 | ev_clear_pending ((W)w); |
|
|
999 | if (ev_is_active (w)) |
|
|
1000 | return; |
|
|
1001 | |
|
|
1002 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
1003 | ev_stop ((W)w); |
|
|
1004 | } |
|
|
1005 | |
|
|
1006 | /*****************************************************************************/ |
|
|
1007 | |
|
|
1008 | struct ev_once |
|
|
1009 | { |
|
|
1010 | struct ev_io io; |
|
|
1011 | struct ev_timer to; |
|
|
1012 | void (*cb)(int revents, void *arg); |
|
|
1013 | void *arg; |
|
|
1014 | }; |
|
|
1015 | |
|
|
1016 | static void |
|
|
1017 | once_cb (struct ev_once *once, int revents) |
|
|
1018 | { |
|
|
1019 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
1020 | void *arg = once->arg; |
|
|
1021 | |
|
|
1022 | ev_io_stop (&once->io); |
|
|
1023 | ev_timer_stop (&once->to); |
|
|
1024 | free (once); |
|
|
1025 | |
|
|
1026 | cb (revents, arg); |
|
|
1027 | } |
|
|
1028 | |
|
|
1029 | static void |
|
|
1030 | once_cb_io (struct ev_io *w, int revents) |
|
|
1031 | { |
|
|
1032 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
1033 | } |
|
|
1034 | |
|
|
1035 | static void |
|
|
1036 | once_cb_to (struct ev_timer *w, int revents) |
|
|
1037 | { |
|
|
1038 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
1039 | } |
|
|
1040 | |
|
|
1041 | void |
|
|
1042 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
1043 | { |
|
|
1044 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
1045 | |
|
|
1046 | if (!once) |
|
|
1047 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
|
|
1048 | else |
|
|
1049 | { |
|
|
1050 | once->cb = cb; |
|
|
1051 | once->arg = arg; |
|
|
1052 | |
|
|
1053 | ev_watcher_init (&once->io, once_cb_io); |
|
|
1054 | if (fd >= 0) |
|
|
1055 | { |
|
|
1056 | ev_io_set (&once->io, fd, events); |
|
|
1057 | ev_io_start (&once->io); |
|
|
1058 | } |
|
|
1059 | |
|
|
1060 | ev_watcher_init (&once->to, once_cb_to); |
|
|
1061 | if (timeout >= 0.) |
|
|
1062 | { |
|
|
1063 | ev_timer_set (&once->to, timeout, 0.); |
|
|
1064 | ev_timer_start (&once->to); |
|
|
1065 | } |
|
|
1066 | } |
|
|
1067 | } |
|
|
1068 | |
802 | /*****************************************************************************/ |
1069 | /*****************************************************************************/ |
803 | |
1070 | |
804 | #if 0 |
1071 | #if 0 |
805 | |
1072 | |
806 | struct ev_io wio; |
1073 | struct ev_io wio; |
… | |
… | |
813 | |
1080 | |
814 | static void |
1081 | static void |
815 | ocb (struct ev_timer *w, int revents) |
1082 | ocb (struct ev_timer *w, int revents) |
816 | { |
1083 | { |
817 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1084 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
818 | evtimer_stop (w); |
1085 | ev_timer_stop (w); |
819 | evtimer_start (w); |
1086 | ev_timer_start (w); |
820 | } |
1087 | } |
821 | |
1088 | |
822 | static void |
1089 | static void |
823 | scb (struct ev_signal *w, int revents) |
1090 | scb (struct ev_signal *w, int revents) |
824 | { |
1091 | { |
825 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1092 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
826 | evio_stop (&wio); |
1093 | ev_io_stop (&wio); |
827 | evio_start (&wio); |
1094 | ev_io_start (&wio); |
828 | } |
1095 | } |
829 | |
1096 | |
830 | static void |
1097 | static void |
831 | gcb (struct ev_signal *w, int revents) |
1098 | gcb (struct ev_signal *w, int revents) |
832 | { |
1099 | { |
… | |
… | |
836 | |
1103 | |
837 | int main (void) |
1104 | int main (void) |
838 | { |
1105 | { |
839 | ev_init (0); |
1106 | ev_init (0); |
840 | |
1107 | |
841 | evio_init (&wio, sin_cb, 0, EV_READ); |
1108 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
842 | evio_start (&wio); |
1109 | ev_io_start (&wio); |
843 | |
1110 | |
844 | struct ev_timer t[10000]; |
1111 | struct ev_timer t[10000]; |
845 | |
1112 | |
846 | #if 0 |
1113 | #if 0 |
847 | int i; |
1114 | int i; |
848 | for (i = 0; i < 10000; ++i) |
1115 | for (i = 0; i < 10000; ++i) |
849 | { |
1116 | { |
850 | struct ev_timer *w = t + i; |
1117 | struct ev_timer *w = t + i; |
851 | evw_init (w, ocb, i); |
1118 | ev_watcher_init (w, ocb, i); |
852 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1119 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
853 | evtimer_start (w); |
1120 | ev_timer_start (w); |
854 | if (drand48 () < 0.5) |
1121 | if (drand48 () < 0.5) |
855 | evtimer_stop (w); |
1122 | ev_timer_stop (w); |
856 | } |
1123 | } |
857 | #endif |
1124 | #endif |
858 | |
1125 | |
859 | struct ev_timer t1; |
1126 | struct ev_timer t1; |
860 | evtimer_init (&t1, ocb, 5, 10); |
1127 | ev_timer_init (&t1, ocb, 5, 10); |
861 | evtimer_start (&t1); |
1128 | ev_timer_start (&t1); |
862 | |
1129 | |
863 | struct ev_signal sig; |
1130 | struct ev_signal sig; |
864 | evsignal_init (&sig, scb, SIGQUIT); |
1131 | ev_signal_init (&sig, scb, SIGQUIT); |
865 | evsignal_start (&sig); |
1132 | ev_signal_start (&sig); |
866 | |
1133 | |
867 | struct ev_check cw; |
1134 | struct ev_check cw; |
868 | evcheck_init (&cw, gcb); |
1135 | ev_check_init (&cw, gcb); |
869 | evcheck_start (&cw); |
1136 | ev_check_start (&cw); |
870 | |
1137 | |
871 | struct ev_idle iw; |
1138 | struct ev_idle iw; |
872 | evidle_init (&iw, gcb); |
1139 | ev_idle_init (&iw, gcb); |
873 | evidle_start (&iw); |
1140 | ev_idle_start (&iw); |
874 | |
1141 | |
875 | ev_loop (0); |
1142 | ev_loop (0); |
876 | |
1143 | |
877 | return 0; |
1144 | return 0; |
878 | } |
1145 | } |