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