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