… | |
… | |
28 | * (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 |
29 | * 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. |
30 | */ |
30 | */ |
31 | #ifndef EV_STANDALONE |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
|
|
33 | |
|
|
34 | # if HAVE_CLOCK_GETTIME |
|
|
35 | # define EV_USE_MONOTONIC 1 |
|
|
36 | # define EV_USE_REALTIME 1 |
|
|
37 | # endif |
|
|
38 | |
|
|
39 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
|
|
40 | # define EV_USE_SELECT 1 |
|
|
41 | # endif |
|
|
42 | |
|
|
43 | # if HAVE_POLL && HAVE_POLL_H |
|
|
44 | # define EV_USE_POLL 1 |
|
|
45 | # endif |
|
|
46 | |
|
|
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
|
|
48 | # define EV_USE_EPOLL 1 |
|
|
49 | # endif |
|
|
50 | |
|
|
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
|
|
52 | # define EV_USE_KQUEUE 1 |
|
|
53 | # endif |
|
|
54 | |
33 | #endif |
55 | #endif |
34 | |
56 | |
35 | #include <math.h> |
57 | #include <math.h> |
36 | #include <stdlib.h> |
58 | #include <stdlib.h> |
37 | #include <unistd.h> |
59 | #include <unistd.h> |
… | |
… | |
58 | |
80 | |
59 | #ifndef EV_USE_SELECT |
81 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
82 | # define EV_USE_SELECT 1 |
61 | #endif |
83 | #endif |
62 | |
84 | |
63 | #ifndef EV_USEV_POLL |
85 | #ifndef EV_USE_POLL |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
86 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
65 | #endif |
87 | #endif |
66 | |
88 | |
67 | #ifndef EV_USE_EPOLL |
89 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
90 | # define EV_USE_EPOLL 0 |
69 | #endif |
91 | #endif |
70 | |
92 | |
71 | #ifndef EV_USE_KQUEUE |
93 | #ifndef EV_USE_KQUEUE |
72 | # define EV_USE_KQUEUE 0 |
94 | # define EV_USE_KQUEUE 0 |
|
|
95 | #endif |
|
|
96 | |
|
|
97 | #ifndef EV_USE_WIN32 |
|
|
98 | # ifdef WIN32 |
|
|
99 | # define EV_USE_WIN32 1 |
|
|
100 | # else |
|
|
101 | # define EV_USE_WIN32 0 |
|
|
102 | # endif |
73 | #endif |
103 | #endif |
74 | |
104 | |
75 | #ifndef EV_USE_REALTIME |
105 | #ifndef EV_USE_REALTIME |
76 | # define EV_USE_REALTIME 1 |
106 | # define EV_USE_REALTIME 1 |
77 | #endif |
107 | #endif |
… | |
… | |
113 | |
143 | |
114 | typedef struct ev_watcher *W; |
144 | typedef struct ev_watcher *W; |
115 | typedef struct ev_watcher_list *WL; |
145 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
146 | typedef struct ev_watcher_time *WT; |
117 | |
147 | |
|
|
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
|
|
149 | |
|
|
150 | #if WIN32 |
|
|
151 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
152 | /* MSDN says this is required to handle SIGFPE */ |
|
|
153 | volatile double SIGFPE_REQ = 0.0f; |
|
|
154 | #endif |
|
|
155 | |
118 | /*****************************************************************************/ |
156 | /*****************************************************************************/ |
119 | |
157 | |
|
|
158 | static void (*syserr_cb)(const char *msg); |
|
|
159 | |
|
|
160 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
|
|
161 | { |
|
|
162 | syserr_cb = cb; |
|
|
163 | } |
|
|
164 | |
|
|
165 | static void |
|
|
166 | syserr (const char *msg) |
|
|
167 | { |
|
|
168 | if (!msg) |
|
|
169 | msg = "(libev) system error"; |
|
|
170 | |
|
|
171 | if (syserr_cb) |
|
|
172 | syserr_cb (msg); |
|
|
173 | else |
|
|
174 | { |
|
|
175 | perror (msg); |
|
|
176 | abort (); |
|
|
177 | } |
|
|
178 | } |
|
|
179 | |
|
|
180 | static void *(*alloc)(void *ptr, long size); |
|
|
181 | |
|
|
182 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
183 | { |
|
|
184 | alloc = cb; |
|
|
185 | } |
|
|
186 | |
|
|
187 | static void * |
|
|
188 | ev_realloc (void *ptr, long size) |
|
|
189 | { |
|
|
190 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
|
|
191 | |
|
|
192 | if (!ptr && size) |
|
|
193 | { |
|
|
194 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
195 | abort (); |
|
|
196 | } |
|
|
197 | |
|
|
198 | return ptr; |
|
|
199 | } |
|
|
200 | |
|
|
201 | #define ev_malloc(size) ev_realloc (0, (size)) |
|
|
202 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
|
|
203 | |
|
|
204 | /*****************************************************************************/ |
|
|
205 | |
120 | typedef struct |
206 | typedef struct |
121 | { |
207 | { |
122 | struct ev_watcher_list *head; |
208 | WL head; |
123 | unsigned char events; |
209 | unsigned char events; |
124 | unsigned char reify; |
210 | unsigned char reify; |
125 | } ANFD; |
211 | } ANFD; |
126 | |
212 | |
127 | typedef struct |
213 | typedef struct |
128 | { |
214 | { |
129 | W w; |
215 | W w; |
130 | int events; |
216 | int events; |
131 | } ANPENDING; |
217 | } ANPENDING; |
132 | |
218 | |
133 | #ifdef EV_MULTIPLICITY |
219 | #if EV_MULTIPLICITY |
|
|
220 | |
134 | struct ev_loop |
221 | struct ev_loop |
135 | { |
222 | { |
136 | # define VAR(name,decl) decl |
223 | # define VAR(name,decl) decl; |
137 | # include "ev_vars.h" |
224 | # include "ev_vars.h" |
138 | }; |
225 | }; |
|
|
226 | # undef VAR |
|
|
227 | # include "ev_wrap.h" |
|
|
228 | |
139 | #else |
229 | #else |
|
|
230 | |
140 | # define VAR(name,decl) static decl |
231 | # define VAR(name,decl) static decl; |
141 | # include "ev_vars.h" |
232 | # include "ev_vars.h" |
142 | #endif |
|
|
143 | #undef VAR |
233 | # undef VAR |
|
|
234 | |
|
|
235 | #endif |
144 | |
236 | |
145 | /*****************************************************************************/ |
237 | /*****************************************************************************/ |
146 | |
238 | |
147 | inline ev_tstamp |
239 | inline ev_tstamp |
148 | ev_time (void) |
240 | ev_time (void) |
… | |
… | |
179 | return rt_now; |
271 | return rt_now; |
180 | } |
272 | } |
181 | |
273 | |
182 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
274 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
183 | |
275 | |
184 | #define array_needsize(base,cur,cnt,init) \ |
276 | #define array_needsize(base,cur,cnt,init) \ |
185 | if (expect_false ((cnt) > cur)) \ |
277 | if (expect_false ((cnt) > cur)) \ |
186 | { \ |
278 | { \ |
187 | int newcnt = cur; \ |
279 | int newcnt = cur; \ |
188 | do \ |
280 | do \ |
189 | { \ |
281 | { \ |
190 | newcnt = array_roundsize (base, newcnt << 1); \ |
282 | newcnt = array_roundsize (base, newcnt << 1); \ |
191 | } \ |
283 | } \ |
192 | while ((cnt) > newcnt); \ |
284 | while ((cnt) > newcnt); \ |
193 | \ |
285 | \ |
194 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
286 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
195 | init (base + cur, newcnt - cur); \ |
287 | init (base + cur, newcnt - cur); \ |
196 | cur = newcnt; \ |
288 | cur = newcnt; \ |
197 | } |
289 | } |
|
|
290 | |
|
|
291 | #define array_slim(stem) \ |
|
|
292 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
293 | { \ |
|
|
294 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
295 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
|
|
296 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
297 | } |
|
|
298 | |
|
|
299 | #define array_free(stem, idx) \ |
|
|
300 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
198 | |
301 | |
199 | /*****************************************************************************/ |
302 | /*****************************************************************************/ |
200 | |
303 | |
201 | static void |
304 | static void |
202 | anfds_init (ANFD *base, int count) |
305 | anfds_init (ANFD *base, int count) |
… | |
… | |
268 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
371 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
269 | events |= w->events; |
372 | events |= w->events; |
270 | |
373 | |
271 | anfd->reify = 0; |
374 | anfd->reify = 0; |
272 | |
375 | |
273 | if (anfd->events != events) |
|
|
274 | { |
|
|
275 | method_modify (EV_A_ fd, anfd->events, events); |
376 | method_modify (EV_A_ fd, anfd->events, events); |
276 | anfd->events = events; |
377 | anfd->events = events; |
277 | } |
|
|
278 | } |
378 | } |
279 | |
379 | |
280 | fdchangecnt = 0; |
380 | fdchangecnt = 0; |
281 | } |
381 | } |
282 | |
382 | |
283 | static void |
383 | static void |
284 | fd_change (EV_P_ int fd) |
384 | fd_change (EV_P_ int fd) |
285 | { |
385 | { |
286 | if (anfds [fd].reify || fdchangecnt < 0) |
386 | if (anfds [fd].reify) |
287 | return; |
387 | return; |
288 | |
388 | |
289 | anfds [fd].reify = 1; |
389 | anfds [fd].reify = 1; |
290 | |
390 | |
291 | ++fdchangecnt; |
391 | ++fdchangecnt; |
… | |
… | |
319 | |
419 | |
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
420 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
321 | static void |
421 | static void |
322 | fd_enomem (EV_P) |
422 | fd_enomem (EV_P) |
323 | { |
423 | { |
324 | int fd = anfdmax; |
424 | int fd; |
325 | |
425 | |
326 | while (fd--) |
426 | for (fd = anfdmax; fd--; ) |
327 | if (anfds [fd].events) |
427 | if (anfds [fd].events) |
328 | { |
428 | { |
329 | close (fd); |
|
|
330 | fd_kill (EV_A_ fd); |
429 | fd_kill (EV_A_ fd); |
331 | return; |
430 | return; |
332 | } |
431 | } |
333 | } |
432 | } |
334 | |
433 | |
|
|
434 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
|
|
435 | static void |
|
|
436 | fd_rearm_all (EV_P) |
|
|
437 | { |
|
|
438 | int fd; |
|
|
439 | |
|
|
440 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
441 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
442 | if (anfds [fd].events) |
|
|
443 | { |
|
|
444 | anfds [fd].events = 0; |
|
|
445 | fd_change (EV_A_ fd); |
|
|
446 | } |
|
|
447 | } |
|
|
448 | |
335 | /*****************************************************************************/ |
449 | /*****************************************************************************/ |
336 | |
450 | |
337 | static void |
451 | static void |
338 | upheap (WT *timers, int k) |
452 | upheap (WT *heap, int k) |
339 | { |
453 | { |
340 | WT w = timers [k]; |
454 | WT w = heap [k]; |
341 | |
455 | |
342 | while (k && timers [k >> 1]->at > w->at) |
456 | while (k && heap [k >> 1]->at > w->at) |
343 | { |
457 | { |
344 | timers [k] = timers [k >> 1]; |
458 | heap [k] = heap [k >> 1]; |
345 | timers [k]->active = k + 1; |
459 | ((W)heap [k])->active = k + 1; |
346 | k >>= 1; |
460 | k >>= 1; |
347 | } |
461 | } |
348 | |
462 | |
349 | timers [k] = w; |
463 | heap [k] = w; |
350 | timers [k]->active = k + 1; |
464 | ((W)heap [k])->active = k + 1; |
351 | |
465 | |
352 | } |
466 | } |
353 | |
467 | |
354 | static void |
468 | static void |
355 | downheap (WT *timers, int N, int k) |
469 | downheap (WT *heap, int N, int k) |
356 | { |
470 | { |
357 | WT w = timers [k]; |
471 | WT w = heap [k]; |
358 | |
472 | |
359 | while (k < (N >> 1)) |
473 | while (k < (N >> 1)) |
360 | { |
474 | { |
361 | int j = k << 1; |
475 | int j = k << 1; |
362 | |
476 | |
363 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
477 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
364 | ++j; |
478 | ++j; |
365 | |
479 | |
366 | if (w->at <= timers [j]->at) |
480 | if (w->at <= heap [j]->at) |
367 | break; |
481 | break; |
368 | |
482 | |
369 | timers [k] = timers [j]; |
483 | heap [k] = heap [j]; |
370 | timers [k]->active = k + 1; |
484 | ((W)heap [k])->active = k + 1; |
371 | k = j; |
485 | k = j; |
372 | } |
486 | } |
373 | |
487 | |
374 | timers [k] = w; |
488 | heap [k] = w; |
375 | timers [k]->active = k + 1; |
489 | ((W)heap [k])->active = k + 1; |
376 | } |
490 | } |
377 | |
491 | |
378 | /*****************************************************************************/ |
492 | /*****************************************************************************/ |
379 | |
493 | |
380 | typedef struct |
494 | typedef struct |
381 | { |
495 | { |
382 | struct ev_watcher_list *head; |
496 | WL head; |
383 | sig_atomic_t volatile gotsig; |
497 | sig_atomic_t volatile gotsig; |
384 | } ANSIG; |
498 | } ANSIG; |
385 | |
499 | |
386 | static ANSIG *signals; |
500 | static ANSIG *signals; |
387 | static int signalmax; |
501 | static int signalmax; |
388 | |
502 | |
389 | static int sigpipe [2]; |
503 | static int sigpipe [2]; |
390 | static sig_atomic_t volatile gotsig; |
504 | static sig_atomic_t volatile gotsig; |
|
|
505 | static struct ev_io sigev; |
391 | |
506 | |
392 | static void |
507 | static void |
393 | signals_init (ANSIG *base, int count) |
508 | signals_init (ANSIG *base, int count) |
394 | { |
509 | { |
395 | while (count--) |
510 | while (count--) |
… | |
… | |
402 | } |
517 | } |
403 | |
518 | |
404 | static void |
519 | static void |
405 | sighandler (int signum) |
520 | sighandler (int signum) |
406 | { |
521 | { |
|
|
522 | #if WIN32 |
|
|
523 | signal (signum, sighandler); |
|
|
524 | #endif |
|
|
525 | |
407 | signals [signum - 1].gotsig = 1; |
526 | signals [signum - 1].gotsig = 1; |
408 | |
527 | |
409 | if (!gotsig) |
528 | if (!gotsig) |
410 | { |
529 | { |
411 | int old_errno = errno; |
530 | int old_errno = errno; |
… | |
… | |
416 | } |
535 | } |
417 | |
536 | |
418 | static void |
537 | static void |
419 | sigcb (EV_P_ struct ev_io *iow, int revents) |
538 | sigcb (EV_P_ struct ev_io *iow, int revents) |
420 | { |
539 | { |
421 | struct ev_watcher_list *w; |
540 | WL w; |
422 | int signum; |
541 | int signum; |
423 | |
542 | |
424 | read (sigpipe [0], &revents, 1); |
543 | read (sigpipe [0], &revents, 1); |
425 | gotsig = 0; |
544 | gotsig = 0; |
426 | |
545 | |
… | |
… | |
445 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
564 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
446 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
565 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
447 | #endif |
566 | #endif |
448 | |
567 | |
449 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
568 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
450 | ev_io_start (&sigev); |
569 | ev_io_start (EV_A_ &sigev); |
451 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
570 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
452 | } |
571 | } |
453 | |
572 | |
454 | /*****************************************************************************/ |
573 | /*****************************************************************************/ |
455 | |
574 | |
456 | #ifndef WIN32 |
575 | #ifndef WIN32 |
|
|
576 | |
|
|
577 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
578 | static struct ev_signal childev; |
457 | |
579 | |
458 | #ifndef WCONTINUED |
580 | #ifndef WCONTINUED |
459 | # define WCONTINUED 0 |
581 | # define WCONTINUED 0 |
460 | #endif |
582 | #endif |
461 | |
583 | |
… | |
… | |
465 | struct ev_child *w; |
587 | struct ev_child *w; |
466 | |
588 | |
467 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
589 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
468 | if (w->pid == pid || !w->pid) |
590 | if (w->pid == pid || !w->pid) |
469 | { |
591 | { |
470 | w->priority = sw->priority; /* need to do it *now* */ |
592 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
471 | w->rpid = pid; |
593 | w->rpid = pid; |
472 | w->rstatus = status; |
594 | w->rstatus = status; |
473 | event (EV_A_ (W)w, EV_CHILD); |
595 | event (EV_A_ (W)w, EV_CHILD); |
474 | } |
596 | } |
475 | } |
597 | } |
476 | |
598 | |
477 | static void |
599 | static void |
… | |
… | |
497 | # include "ev_kqueue.c" |
619 | # include "ev_kqueue.c" |
498 | #endif |
620 | #endif |
499 | #if EV_USE_EPOLL |
621 | #if EV_USE_EPOLL |
500 | # include "ev_epoll.c" |
622 | # include "ev_epoll.c" |
501 | #endif |
623 | #endif |
502 | #if EV_USEV_POLL |
624 | #if EV_USE_POLL |
503 | # include "ev_poll.c" |
625 | # include "ev_poll.c" |
504 | #endif |
626 | #endif |
505 | #if EV_USE_SELECT |
627 | #if EV_USE_SELECT |
506 | # include "ev_select.c" |
628 | # include "ev_select.c" |
507 | #endif |
629 | #endif |
… | |
… | |
534 | ev_method (EV_P) |
656 | ev_method (EV_P) |
535 | { |
657 | { |
536 | return method; |
658 | return method; |
537 | } |
659 | } |
538 | |
660 | |
539 | int |
661 | static void |
540 | ev_init (EV_P_ int methods) |
662 | loop_init (EV_P_ int methods) |
541 | { |
663 | { |
542 | #ifdef EV_MULTIPLICITY |
|
|
543 | memset (loop, 0, sizeof (struct ev_loop)); |
|
|
544 | #endif |
|
|
545 | |
|
|
546 | if (!method) |
664 | if (!method) |
547 | { |
665 | { |
548 | #if EV_USE_MONOTONIC |
666 | #if EV_USE_MONOTONIC |
549 | { |
667 | { |
550 | struct timespec ts; |
668 | struct timespec ts; |
… | |
… | |
554 | #endif |
672 | #endif |
555 | |
673 | |
556 | rt_now = ev_time (); |
674 | rt_now = ev_time (); |
557 | mn_now = get_clock (); |
675 | mn_now = get_clock (); |
558 | now_floor = mn_now; |
676 | now_floor = mn_now; |
559 | diff = rt_now - mn_now; |
677 | rtmn_diff = rt_now - mn_now; |
560 | |
|
|
561 | if (pipe (sigpipe)) |
|
|
562 | return 0; |
|
|
563 | |
678 | |
564 | if (methods == EVMETHOD_AUTO) |
679 | if (methods == EVMETHOD_AUTO) |
565 | if (!enable_secure () && getenv ("LIBmethodS")) |
680 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
566 | methods = atoi (getenv ("LIBmethodS")); |
681 | methods = atoi (getenv ("LIBEV_METHODS")); |
567 | else |
682 | else |
568 | methods = EVMETHOD_ANY; |
683 | methods = EVMETHOD_ANY; |
569 | |
684 | |
570 | method = 0; |
685 | method = 0; |
|
|
686 | #if EV_USE_WIN32 |
|
|
687 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
688 | #endif |
571 | #if EV_USE_KQUEUE |
689 | #if EV_USE_KQUEUE |
572 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
690 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
573 | #endif |
691 | #endif |
574 | #if EV_USE_EPOLL |
692 | #if EV_USE_EPOLL |
575 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
693 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
576 | #endif |
694 | #endif |
577 | #if EV_USEV_POLL |
695 | #if EV_USE_POLL |
578 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
696 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
579 | #endif |
697 | #endif |
580 | #if EV_USE_SELECT |
698 | #if EV_USE_SELECT |
581 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
699 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
582 | #endif |
700 | #endif |
583 | |
701 | |
|
|
702 | ev_watcher_init (&sigev, sigcb); |
|
|
703 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
704 | } |
|
|
705 | } |
|
|
706 | |
|
|
707 | void |
|
|
708 | loop_destroy (EV_P) |
|
|
709 | { |
|
|
710 | int i; |
|
|
711 | |
|
|
712 | #if EV_USE_WIN32 |
|
|
713 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
714 | #endif |
|
|
715 | #if EV_USE_KQUEUE |
|
|
716 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
|
|
717 | #endif |
|
|
718 | #if EV_USE_EPOLL |
|
|
719 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
|
|
720 | #endif |
|
|
721 | #if EV_USE_POLL |
|
|
722 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
|
|
723 | #endif |
|
|
724 | #if EV_USE_SELECT |
|
|
725 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
|
|
726 | #endif |
|
|
727 | |
|
|
728 | for (i = NUMPRI; i--; ) |
|
|
729 | array_free (pending, [i]); |
|
|
730 | |
|
|
731 | array_free (fdchange, ); |
|
|
732 | array_free (timer, ); |
|
|
733 | array_free (periodic, ); |
|
|
734 | array_free (idle, ); |
|
|
735 | array_free (prepare, ); |
|
|
736 | array_free (check, ); |
|
|
737 | |
|
|
738 | method = 0; |
|
|
739 | } |
|
|
740 | |
|
|
741 | static void |
|
|
742 | loop_fork (EV_P) |
|
|
743 | { |
|
|
744 | #if EV_USE_EPOLL |
|
|
745 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
|
|
746 | #endif |
|
|
747 | #if EV_USE_KQUEUE |
|
|
748 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
749 | #endif |
|
|
750 | |
|
|
751 | if (ev_is_active (&sigev)) |
|
|
752 | { |
|
|
753 | /* default loop */ |
|
|
754 | |
|
|
755 | ev_ref (EV_A); |
|
|
756 | ev_io_stop (EV_A_ &sigev); |
|
|
757 | close (sigpipe [0]); |
|
|
758 | close (sigpipe [1]); |
|
|
759 | |
|
|
760 | while (pipe (sigpipe)) |
|
|
761 | syserr ("(libev) error creating pipe"); |
|
|
762 | |
|
|
763 | siginit (EV_A); |
|
|
764 | } |
|
|
765 | |
|
|
766 | postfork = 0; |
|
|
767 | } |
|
|
768 | |
|
|
769 | #if EV_MULTIPLICITY |
|
|
770 | struct ev_loop * |
|
|
771 | ev_loop_new (int methods) |
|
|
772 | { |
|
|
773 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
|
|
774 | |
|
|
775 | memset (loop, 0, sizeof (struct ev_loop)); |
|
|
776 | |
|
|
777 | loop_init (EV_A_ methods); |
|
|
778 | |
|
|
779 | if (ev_method (EV_A)) |
|
|
780 | return loop; |
|
|
781 | |
|
|
782 | return 0; |
|
|
783 | } |
|
|
784 | |
|
|
785 | void |
|
|
786 | ev_loop_destroy (EV_P) |
|
|
787 | { |
|
|
788 | loop_destroy (EV_A); |
|
|
789 | ev_free (loop); |
|
|
790 | } |
|
|
791 | |
|
|
792 | void |
|
|
793 | ev_loop_fork (EV_P) |
|
|
794 | { |
|
|
795 | postfork = 1; |
|
|
796 | } |
|
|
797 | |
|
|
798 | #endif |
|
|
799 | |
|
|
800 | #if EV_MULTIPLICITY |
|
|
801 | struct ev_loop default_loop_struct; |
|
|
802 | static struct ev_loop *default_loop; |
|
|
803 | |
|
|
804 | struct ev_loop * |
|
|
805 | #else |
|
|
806 | static int default_loop; |
|
|
807 | |
|
|
808 | int |
|
|
809 | #endif |
|
|
810 | ev_default_loop (int methods) |
|
|
811 | { |
|
|
812 | if (sigpipe [0] == sigpipe [1]) |
|
|
813 | if (pipe (sigpipe)) |
|
|
814 | return 0; |
|
|
815 | |
|
|
816 | if (!default_loop) |
|
|
817 | { |
|
|
818 | #if EV_MULTIPLICITY |
|
|
819 | struct ev_loop *loop = default_loop = &default_loop_struct; |
|
|
820 | #else |
|
|
821 | default_loop = 1; |
|
|
822 | #endif |
|
|
823 | |
|
|
824 | loop_init (EV_A_ methods); |
|
|
825 | |
584 | if (method) |
826 | if (ev_method (EV_A)) |
585 | { |
827 | { |
586 | ev_watcher_init (&sigev, sigcb); |
|
|
587 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
588 | siginit (EV_A); |
828 | siginit (EV_A); |
589 | |
829 | |
590 | #ifndef WIN32 |
830 | #ifndef WIN32 |
591 | ev_signal_init (&childev, childcb, SIGCHLD); |
831 | ev_signal_init (&childev, childcb, SIGCHLD); |
592 | ev_set_priority (&childev, EV_MAXPRI); |
832 | ev_set_priority (&childev, EV_MAXPRI); |
593 | ev_signal_start (EV_A_ &childev); |
833 | ev_signal_start (EV_A_ &childev); |
594 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
834 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
595 | #endif |
835 | #endif |
596 | } |
836 | } |
|
|
837 | else |
|
|
838 | default_loop = 0; |
597 | } |
839 | } |
598 | |
840 | |
599 | return method; |
841 | return default_loop; |
600 | } |
842 | } |
601 | |
843 | |
602 | /*****************************************************************************/ |
|
|
603 | |
|
|
604 | void |
844 | void |
605 | ev_fork_prepare (void) |
845 | ev_default_destroy (void) |
606 | { |
846 | { |
607 | /* nop */ |
847 | #if EV_MULTIPLICITY |
608 | } |
848 | struct ev_loop *loop = default_loop; |
609 | |
|
|
610 | void |
|
|
611 | ev_fork_parent (void) |
|
|
612 | { |
|
|
613 | /* nop */ |
|
|
614 | } |
|
|
615 | |
|
|
616 | void |
|
|
617 | ev_fork_child (void) |
|
|
618 | { |
|
|
619 | #if EV_USE_EPOLL |
|
|
620 | if (method == EVMETHOD_EPOLL) |
|
|
621 | epoll_postfork_child (); |
|
|
622 | #endif |
849 | #endif |
623 | |
850 | |
|
|
851 | ev_ref (EV_A); /* child watcher */ |
|
|
852 | ev_signal_stop (EV_A_ &childev); |
|
|
853 | |
|
|
854 | ev_ref (EV_A); /* signal watcher */ |
624 | ev_io_stop (&sigev); |
855 | ev_io_stop (EV_A_ &sigev); |
625 | close (sigpipe [0]); |
856 | |
626 | close (sigpipe [1]); |
857 | close (sigpipe [0]); sigpipe [0] = 0; |
627 | pipe (sigpipe); |
858 | close (sigpipe [1]); sigpipe [1] = 0; |
628 | siginit (); |
859 | |
|
|
860 | loop_destroy (EV_A); |
|
|
861 | } |
|
|
862 | |
|
|
863 | void |
|
|
864 | ev_default_fork (void) |
|
|
865 | { |
|
|
866 | #if EV_MULTIPLICITY |
|
|
867 | struct ev_loop *loop = default_loop; |
|
|
868 | #endif |
|
|
869 | |
|
|
870 | if (method) |
|
|
871 | postfork = 1; |
629 | } |
872 | } |
630 | |
873 | |
631 | /*****************************************************************************/ |
874 | /*****************************************************************************/ |
632 | |
875 | |
633 | static void |
876 | static void |
… | |
… | |
649 | } |
892 | } |
650 | |
893 | |
651 | static void |
894 | static void |
652 | timers_reify (EV_P) |
895 | timers_reify (EV_P) |
653 | { |
896 | { |
654 | while (timercnt && timers [0]->at <= mn_now) |
897 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
655 | { |
898 | { |
656 | struct ev_timer *w = timers [0]; |
899 | struct ev_timer *w = timers [0]; |
|
|
900 | |
|
|
901 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
657 | |
902 | |
658 | /* first reschedule or stop timer */ |
903 | /* first reschedule or stop timer */ |
659 | if (w->repeat) |
904 | if (w->repeat) |
660 | { |
905 | { |
661 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
906 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
662 | w->at = mn_now + w->repeat; |
907 | ((WT)w)->at = mn_now + w->repeat; |
663 | downheap ((WT *)timers, timercnt, 0); |
908 | downheap ((WT *)timers, timercnt, 0); |
664 | } |
909 | } |
665 | else |
910 | else |
666 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
911 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
667 | |
912 | |
668 | event ((W)w, EV_TIMEOUT); |
913 | event (EV_A_ (W)w, EV_TIMEOUT); |
669 | } |
914 | } |
670 | } |
915 | } |
671 | |
916 | |
672 | static void |
917 | static void |
673 | periodics_reify (EV_P) |
918 | periodics_reify (EV_P) |
674 | { |
919 | { |
675 | while (periodiccnt && periodics [0]->at <= rt_now) |
920 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
676 | { |
921 | { |
677 | struct ev_periodic *w = periodics [0]; |
922 | struct ev_periodic *w = periodics [0]; |
|
|
923 | |
|
|
924 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
678 | |
925 | |
679 | /* first reschedule or stop timer */ |
926 | /* first reschedule or stop timer */ |
680 | if (w->interval) |
927 | if (w->interval) |
681 | { |
928 | { |
682 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
929 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
683 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
930 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
684 | downheap ((WT *)periodics, periodiccnt, 0); |
931 | downheap ((WT *)periodics, periodiccnt, 0); |
685 | } |
932 | } |
686 | else |
933 | else |
687 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
934 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
688 | |
935 | |
689 | event (EV_A_ (W)w, EV_PERIODIC); |
936 | event (EV_A_ (W)w, EV_PERIODIC); |
690 | } |
937 | } |
691 | } |
938 | } |
692 | |
939 | |
693 | static void |
940 | static void |
694 | periodics_reschedule (EV_P_ ev_tstamp diff) |
941 | periodics_reschedule (EV_P) |
695 | { |
942 | { |
696 | int i; |
943 | int i; |
697 | |
944 | |
698 | /* adjust periodics after time jump */ |
945 | /* adjust periodics after time jump */ |
699 | for (i = 0; i < periodiccnt; ++i) |
946 | for (i = 0; i < periodiccnt; ++i) |
700 | { |
947 | { |
701 | struct ev_periodic *w = periodics [i]; |
948 | struct ev_periodic *w = periodics [i]; |
702 | |
949 | |
703 | if (w->interval) |
950 | if (w->interval) |
704 | { |
951 | { |
705 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
952 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
706 | |
953 | |
707 | if (fabs (diff) >= 1e-4) |
954 | if (fabs (diff) >= 1e-4) |
708 | { |
955 | { |
709 | ev_periodic_stop (EV_A_ w); |
956 | ev_periodic_stop (EV_A_ w); |
710 | ev_periodic_start (EV_A_ w); |
957 | ev_periodic_start (EV_A_ w); |
… | |
… | |
720 | { |
967 | { |
721 | mn_now = get_clock (); |
968 | mn_now = get_clock (); |
722 | |
969 | |
723 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
970 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
724 | { |
971 | { |
725 | rt_now = mn_now + diff; |
972 | rt_now = rtmn_diff + mn_now; |
726 | return 0; |
973 | return 0; |
727 | } |
974 | } |
728 | else |
975 | else |
729 | { |
976 | { |
730 | now_floor = mn_now; |
977 | now_floor = mn_now; |
… | |
… | |
741 | #if EV_USE_MONOTONIC |
988 | #if EV_USE_MONOTONIC |
742 | if (expect_true (have_monotonic)) |
989 | if (expect_true (have_monotonic)) |
743 | { |
990 | { |
744 | if (time_update_monotonic (EV_A)) |
991 | if (time_update_monotonic (EV_A)) |
745 | { |
992 | { |
746 | ev_tstamp odiff = diff; |
993 | ev_tstamp odiff = rtmn_diff; |
747 | |
994 | |
748 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
995 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
749 | { |
996 | { |
750 | diff = rt_now - mn_now; |
997 | rtmn_diff = rt_now - mn_now; |
751 | |
998 | |
752 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
999 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
753 | return; /* all is well */ |
1000 | return; /* all is well */ |
754 | |
1001 | |
755 | rt_now = ev_time (); |
1002 | rt_now = ev_time (); |
756 | mn_now = get_clock (); |
1003 | mn_now = get_clock (); |
757 | now_floor = mn_now; |
1004 | now_floor = mn_now; |
758 | } |
1005 | } |
759 | |
1006 | |
760 | periodics_reschedule (EV_A_ diff - odiff); |
1007 | periodics_reschedule (EV_A); |
761 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1008 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
1009 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
762 | } |
1010 | } |
763 | } |
1011 | } |
764 | else |
1012 | else |
765 | #endif |
1013 | #endif |
766 | { |
1014 | { |
767 | rt_now = ev_time (); |
1015 | rt_now = ev_time (); |
768 | |
1016 | |
769 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1017 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
770 | { |
1018 | { |
771 | periodics_reschedule (EV_A_ rt_now - mn_now); |
1019 | periodics_reschedule (EV_A); |
772 | |
1020 | |
773 | /* adjust timers. this is easy, as the offset is the same for all */ |
1021 | /* adjust timers. this is easy, as the offset is the same for all */ |
774 | for (i = 0; i < timercnt; ++i) |
1022 | for (i = 0; i < timercnt; ++i) |
775 | timers [i]->at += diff; |
1023 | ((WT)timers [i])->at += rt_now - mn_now; |
776 | } |
1024 | } |
777 | |
1025 | |
778 | mn_now = rt_now; |
1026 | mn_now = rt_now; |
779 | } |
1027 | } |
780 | } |
1028 | } |
… | |
… | |
806 | { |
1054 | { |
807 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1055 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
808 | call_pending (EV_A); |
1056 | call_pending (EV_A); |
809 | } |
1057 | } |
810 | |
1058 | |
|
|
1059 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1060 | if (expect_false (postfork)) |
|
|
1061 | loop_fork (EV_A); |
|
|
1062 | |
811 | /* update fd-related kernel structures */ |
1063 | /* update fd-related kernel structures */ |
812 | fd_reify (EV_A); |
1064 | fd_reify (EV_A); |
813 | |
1065 | |
814 | /* calculate blocking time */ |
1066 | /* calculate blocking time */ |
815 | |
1067 | |
… | |
… | |
831 | { |
1083 | { |
832 | block = MAX_BLOCKTIME; |
1084 | block = MAX_BLOCKTIME; |
833 | |
1085 | |
834 | if (timercnt) |
1086 | if (timercnt) |
835 | { |
1087 | { |
836 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1088 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
837 | if (block > to) block = to; |
1089 | if (block > to) block = to; |
838 | } |
1090 | } |
839 | |
1091 | |
840 | if (periodiccnt) |
1092 | if (periodiccnt) |
841 | { |
1093 | { |
842 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1094 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
843 | if (block > to) block = to; |
1095 | if (block > to) block = to; |
844 | } |
1096 | } |
845 | |
1097 | |
846 | if (block < 0.) block = 0.; |
1098 | if (block < 0.) block = 0.; |
847 | } |
1099 | } |
… | |
… | |
964 | ev_timer_start (EV_P_ struct ev_timer *w) |
1216 | ev_timer_start (EV_P_ struct ev_timer *w) |
965 | { |
1217 | { |
966 | if (ev_is_active (w)) |
1218 | if (ev_is_active (w)) |
967 | return; |
1219 | return; |
968 | |
1220 | |
969 | w->at += mn_now; |
1221 | ((WT)w)->at += mn_now; |
970 | |
1222 | |
971 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1223 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
972 | |
1224 | |
973 | ev_start (EV_A_ (W)w, ++timercnt); |
1225 | ev_start (EV_A_ (W)w, ++timercnt); |
974 | array_needsize (timers, timermax, timercnt, ); |
1226 | array_needsize (timers, timermax, timercnt, ); |
975 | timers [timercnt - 1] = w; |
1227 | timers [timercnt - 1] = w; |
976 | upheap ((WT *)timers, timercnt - 1); |
1228 | upheap ((WT *)timers, timercnt - 1); |
|
|
1229 | |
|
|
1230 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
977 | } |
1231 | } |
978 | |
1232 | |
979 | void |
1233 | void |
980 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1234 | ev_timer_stop (EV_P_ struct ev_timer *w) |
981 | { |
1235 | { |
982 | ev_clear_pending (EV_A_ (W)w); |
1236 | ev_clear_pending (EV_A_ (W)w); |
983 | if (!ev_is_active (w)) |
1237 | if (!ev_is_active (w)) |
984 | return; |
1238 | return; |
985 | |
1239 | |
|
|
1240 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1241 | |
986 | if (w->active < timercnt--) |
1242 | if (((W)w)->active < timercnt--) |
987 | { |
1243 | { |
988 | timers [w->active - 1] = timers [timercnt]; |
1244 | timers [((W)w)->active - 1] = timers [timercnt]; |
989 | downheap ((WT *)timers, timercnt, w->active - 1); |
1245 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
990 | } |
1246 | } |
991 | |
1247 | |
992 | w->at = w->repeat; |
1248 | ((WT)w)->at = w->repeat; |
993 | |
1249 | |
994 | ev_stop (EV_A_ (W)w); |
1250 | ev_stop (EV_A_ (W)w); |
995 | } |
1251 | } |
996 | |
1252 | |
997 | void |
1253 | void |
… | |
… | |
999 | { |
1255 | { |
1000 | if (ev_is_active (w)) |
1256 | if (ev_is_active (w)) |
1001 | { |
1257 | { |
1002 | if (w->repeat) |
1258 | if (w->repeat) |
1003 | { |
1259 | { |
1004 | w->at = mn_now + w->repeat; |
1260 | ((WT)w)->at = mn_now + w->repeat; |
1005 | downheap ((WT *)timers, timercnt, w->active - 1); |
1261 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1006 | } |
1262 | } |
1007 | else |
1263 | else |
1008 | ev_timer_stop (EV_A_ w); |
1264 | ev_timer_stop (EV_A_ w); |
1009 | } |
1265 | } |
1010 | else if (w->repeat) |
1266 | else if (w->repeat) |
… | |
… | |
1019 | |
1275 | |
1020 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1276 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1021 | |
1277 | |
1022 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1278 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1023 | if (w->interval) |
1279 | if (w->interval) |
1024 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1280 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1025 | |
1281 | |
1026 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1282 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1027 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1283 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1028 | periodics [periodiccnt - 1] = w; |
1284 | periodics [periodiccnt - 1] = w; |
1029 | upheap ((WT *)periodics, periodiccnt - 1); |
1285 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1286 | |
|
|
1287 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1030 | } |
1288 | } |
1031 | |
1289 | |
1032 | void |
1290 | void |
1033 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1291 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1034 | { |
1292 | { |
1035 | ev_clear_pending (EV_A_ (W)w); |
1293 | ev_clear_pending (EV_A_ (W)w); |
1036 | if (!ev_is_active (w)) |
1294 | if (!ev_is_active (w)) |
1037 | return; |
1295 | return; |
1038 | |
1296 | |
|
|
1297 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1298 | |
1039 | if (w->active < periodiccnt--) |
1299 | if (((W)w)->active < periodiccnt--) |
1040 | { |
1300 | { |
1041 | periodics [w->active - 1] = periodics [periodiccnt]; |
1301 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1042 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1302 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1043 | } |
1303 | } |
1044 | |
1304 | |
|
|
1305 | ev_stop (EV_A_ (W)w); |
|
|
1306 | } |
|
|
1307 | |
|
|
1308 | void |
|
|
1309 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1310 | { |
|
|
1311 | if (ev_is_active (w)) |
|
|
1312 | return; |
|
|
1313 | |
|
|
1314 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1315 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1316 | idles [idlecnt - 1] = w; |
|
|
1317 | } |
|
|
1318 | |
|
|
1319 | void |
|
|
1320 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1321 | { |
|
|
1322 | ev_clear_pending (EV_A_ (W)w); |
|
|
1323 | if (ev_is_active (w)) |
|
|
1324 | return; |
|
|
1325 | |
|
|
1326 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
|
|
1327 | ev_stop (EV_A_ (W)w); |
|
|
1328 | } |
|
|
1329 | |
|
|
1330 | void |
|
|
1331 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1332 | { |
|
|
1333 | if (ev_is_active (w)) |
|
|
1334 | return; |
|
|
1335 | |
|
|
1336 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1337 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1338 | prepares [preparecnt - 1] = w; |
|
|
1339 | } |
|
|
1340 | |
|
|
1341 | void |
|
|
1342 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1343 | { |
|
|
1344 | ev_clear_pending (EV_A_ (W)w); |
|
|
1345 | if (ev_is_active (w)) |
|
|
1346 | return; |
|
|
1347 | |
|
|
1348 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
|
|
1349 | ev_stop (EV_A_ (W)w); |
|
|
1350 | } |
|
|
1351 | |
|
|
1352 | void |
|
|
1353 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1354 | { |
|
|
1355 | if (ev_is_active (w)) |
|
|
1356 | return; |
|
|
1357 | |
|
|
1358 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1359 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1360 | checks [checkcnt - 1] = w; |
|
|
1361 | } |
|
|
1362 | |
|
|
1363 | void |
|
|
1364 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1365 | { |
|
|
1366 | ev_clear_pending (EV_A_ (W)w); |
|
|
1367 | if (ev_is_active (w)) |
|
|
1368 | return; |
|
|
1369 | |
|
|
1370 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1045 | ev_stop (EV_A_ (W)w); |
1371 | ev_stop (EV_A_ (W)w); |
1046 | } |
1372 | } |
1047 | |
1373 | |
1048 | #ifndef SA_RESTART |
1374 | #ifndef SA_RESTART |
1049 | # define SA_RESTART 0 |
1375 | # define SA_RESTART 0 |
1050 | #endif |
1376 | #endif |
1051 | |
1377 | |
1052 | void |
1378 | void |
1053 | ev_signal_start (EV_P_ struct ev_signal *w) |
1379 | ev_signal_start (EV_P_ struct ev_signal *w) |
1054 | { |
1380 | { |
|
|
1381 | #if EV_MULTIPLICITY |
|
|
1382 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1383 | #endif |
1055 | if (ev_is_active (w)) |
1384 | if (ev_is_active (w)) |
1056 | return; |
1385 | return; |
1057 | |
1386 | |
1058 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1387 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1059 | |
1388 | |
1060 | ev_start (EV_A_ (W)w, 1); |
1389 | ev_start (EV_A_ (W)w, 1); |
1061 | array_needsize (signals, signalmax, w->signum, signals_init); |
1390 | array_needsize (signals, signalmax, w->signum, signals_init); |
1062 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1391 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1063 | |
1392 | |
1064 | if (!w->next) |
1393 | if (!((WL)w)->next) |
1065 | { |
1394 | { |
|
|
1395 | #if WIN32 |
|
|
1396 | signal (w->signum, sighandler); |
|
|
1397 | #else |
1066 | struct sigaction sa; |
1398 | struct sigaction sa; |
1067 | sa.sa_handler = sighandler; |
1399 | sa.sa_handler = sighandler; |
1068 | sigfillset (&sa.sa_mask); |
1400 | sigfillset (&sa.sa_mask); |
1069 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1401 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1070 | sigaction (w->signum, &sa, 0); |
1402 | sigaction (w->signum, &sa, 0); |
|
|
1403 | #endif |
1071 | } |
1404 | } |
1072 | } |
1405 | } |
1073 | |
1406 | |
1074 | void |
1407 | void |
1075 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1408 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1084 | if (!signals [w->signum - 1].head) |
1417 | if (!signals [w->signum - 1].head) |
1085 | signal (w->signum, SIG_DFL); |
1418 | signal (w->signum, SIG_DFL); |
1086 | } |
1419 | } |
1087 | |
1420 | |
1088 | void |
1421 | void |
1089 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1090 | { |
|
|
1091 | if (ev_is_active (w)) |
|
|
1092 | return; |
|
|
1093 | |
|
|
1094 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1095 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1096 | idles [idlecnt - 1] = w; |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | void |
|
|
1100 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1101 | { |
|
|
1102 | ev_clear_pending (EV_A_ (W)w); |
|
|
1103 | if (ev_is_active (w)) |
|
|
1104 | return; |
|
|
1105 | |
|
|
1106 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1107 | ev_stop (EV_A_ (W)w); |
|
|
1108 | } |
|
|
1109 | |
|
|
1110 | void |
|
|
1111 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1112 | { |
|
|
1113 | if (ev_is_active (w)) |
|
|
1114 | return; |
|
|
1115 | |
|
|
1116 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1117 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1118 | prepares [preparecnt - 1] = w; |
|
|
1119 | } |
|
|
1120 | |
|
|
1121 | void |
|
|
1122 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1123 | { |
|
|
1124 | ev_clear_pending (EV_A_ (W)w); |
|
|
1125 | if (ev_is_active (w)) |
|
|
1126 | return; |
|
|
1127 | |
|
|
1128 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1129 | ev_stop (EV_A_ (W)w); |
|
|
1130 | } |
|
|
1131 | |
|
|
1132 | void |
|
|
1133 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1134 | { |
|
|
1135 | if (ev_is_active (w)) |
|
|
1136 | return; |
|
|
1137 | |
|
|
1138 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1139 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1140 | checks [checkcnt - 1] = w; |
|
|
1141 | } |
|
|
1142 | |
|
|
1143 | void |
|
|
1144 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1145 | { |
|
|
1146 | ev_clear_pending (EV_A_ (W)w); |
|
|
1147 | if (ev_is_active (w)) |
|
|
1148 | return; |
|
|
1149 | |
|
|
1150 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1151 | ev_stop (EV_A_ (W)w); |
|
|
1152 | } |
|
|
1153 | |
|
|
1154 | void |
|
|
1155 | ev_child_start (EV_P_ struct ev_child *w) |
1422 | ev_child_start (EV_P_ struct ev_child *w) |
1156 | { |
1423 | { |
|
|
1424 | #if EV_MULTIPLICITY |
|
|
1425 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1426 | #endif |
1157 | if (ev_is_active (w)) |
1427 | if (ev_is_active (w)) |
1158 | return; |
1428 | return; |
1159 | |
1429 | |
1160 | ev_start (EV_A_ (W)w, 1); |
1430 | ev_start (EV_A_ (W)w, 1); |
1161 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1431 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
… | |
… | |
1188 | void (*cb)(int revents, void *arg) = once->cb; |
1458 | void (*cb)(int revents, void *arg) = once->cb; |
1189 | void *arg = once->arg; |
1459 | void *arg = once->arg; |
1190 | |
1460 | |
1191 | ev_io_stop (EV_A_ &once->io); |
1461 | ev_io_stop (EV_A_ &once->io); |
1192 | ev_timer_stop (EV_A_ &once->to); |
1462 | ev_timer_stop (EV_A_ &once->to); |
1193 | free (once); |
1463 | ev_free (once); |
1194 | |
1464 | |
1195 | cb (revents, arg); |
1465 | cb (revents, arg); |
1196 | } |
1466 | } |
1197 | |
1467 | |
1198 | static void |
1468 | static void |
… | |
… | |
1208 | } |
1478 | } |
1209 | |
1479 | |
1210 | void |
1480 | void |
1211 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1481 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1212 | { |
1482 | { |
1213 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1483 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1214 | |
1484 | |
1215 | if (!once) |
1485 | if (!once) |
1216 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1486 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1217 | else |
1487 | else |
1218 | { |
1488 | { |
… | |
… | |
1233 | ev_timer_start (EV_A_ &once->to); |
1503 | ev_timer_start (EV_A_ &once->to); |
1234 | } |
1504 | } |
1235 | } |
1505 | } |
1236 | } |
1506 | } |
1237 | |
1507 | |
1238 | /*****************************************************************************/ |
|
|
1239 | |
|
|
1240 | #if 0 |
|
|
1241 | |
|
|
1242 | struct ev_io wio; |
|
|
1243 | |
|
|
1244 | static void |
|
|
1245 | sin_cb (struct ev_io *w, int revents) |
|
|
1246 | { |
|
|
1247 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1248 | } |
|
|
1249 | |
|
|
1250 | static void |
|
|
1251 | ocb (struct ev_timer *w, int revents) |
|
|
1252 | { |
|
|
1253 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1254 | ev_timer_stop (w); |
|
|
1255 | ev_timer_start (w); |
|
|
1256 | } |
|
|
1257 | |
|
|
1258 | static void |
|
|
1259 | scb (struct ev_signal *w, int revents) |
|
|
1260 | { |
|
|
1261 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1262 | ev_io_stop (&wio); |
|
|
1263 | ev_io_start (&wio); |
|
|
1264 | } |
|
|
1265 | |
|
|
1266 | static void |
|
|
1267 | gcb (struct ev_signal *w, int revents) |
|
|
1268 | { |
|
|
1269 | fprintf (stderr, "generic %x\n", revents); |
|
|
1270 | |
|
|
1271 | } |
|
|
1272 | |
|
|
1273 | int main (void) |
|
|
1274 | { |
|
|
1275 | ev_init (0); |
|
|
1276 | |
|
|
1277 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1278 | ev_io_start (&wio); |
|
|
1279 | |
|
|
1280 | struct ev_timer t[10000]; |
|
|
1281 | |
|
|
1282 | #if 0 |
|
|
1283 | int i; |
|
|
1284 | for (i = 0; i < 10000; ++i) |
|
|
1285 | { |
|
|
1286 | struct ev_timer *w = t + i; |
|
|
1287 | ev_watcher_init (w, ocb, i); |
|
|
1288 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1289 | ev_timer_start (w); |
|
|
1290 | if (drand48 () < 0.5) |
|
|
1291 | ev_timer_stop (w); |
|
|
1292 | } |
|
|
1293 | #endif |
|
|
1294 | |
|
|
1295 | struct ev_timer t1; |
|
|
1296 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1297 | ev_timer_start (&t1); |
|
|
1298 | |
|
|
1299 | struct ev_signal sig; |
|
|
1300 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1301 | ev_signal_start (&sig); |
|
|
1302 | |
|
|
1303 | struct ev_check cw; |
|
|
1304 | ev_check_init (&cw, gcb); |
|
|
1305 | ev_check_start (&cw); |
|
|
1306 | |
|
|
1307 | struct ev_idle iw; |
|
|
1308 | ev_idle_init (&iw, gcb); |
|
|
1309 | ev_idle_start (&iw); |
|
|
1310 | |
|
|
1311 | ev_loop (0); |
|
|
1312 | |
|
|
1313 | return 0; |
|
|
1314 | } |
|
|
1315 | |
|
|
1316 | #endif |
|
|
1317 | |
|
|
1318 | |
|
|
1319 | |
|
|
1320 | |
|
|