… | |
… | |
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 | |
118 | static ev_tstamp now_floor, mn_now, diff; /* monotonic clock */ |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
119 | static ev_tstamp rt_now; |
|
|
120 | static int method; |
|
|
121 | |
149 | |
122 | static int have_monotonic; /* runtime */ |
150 | #if WIN32 |
123 | |
151 | /* note: the comment below could not be substantiated, but what would I care */ |
124 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
152 | /* MSDN says this is required to handle SIGFPE */ |
125 | static void (*method_modify)(EV_P_ int fd, int oev, int nev); |
153 | volatile double SIGFPE_REQ = 0.0f; |
126 | static void (*method_poll)(EV_P_ ev_tstamp timeout); |
|
|
127 | |
|
|
128 | static int activecnt; /* number of active events */ |
|
|
129 | |
|
|
130 | #if EV_USE_SELECT |
|
|
131 | static unsigned char *vec_ri, *vec_ro, *vec_wi, *vec_wo; |
|
|
132 | static int vec_max; |
|
|
133 | #endif |
154 | #endif |
134 | |
155 | |
135 | #if EV_USEV_POLL |
156 | /*****************************************************************************/ |
136 | static struct pollfd *polls; |
|
|
137 | static int pollmax, pollcnt; |
|
|
138 | static int *pollidxs; /* maps fds into structure indices */ |
|
|
139 | static int pollidxmax; |
|
|
140 | #endif |
|
|
141 | |
157 | |
142 | #if EV_USE_EPOLL |
158 | typedef struct |
143 | static int epoll_fd = -1; |
159 | { |
|
|
160 | WL head; |
|
|
161 | unsigned char events; |
|
|
162 | unsigned char reify; |
|
|
163 | } ANFD; |
144 | |
164 | |
145 | static struct epoll_event *events; |
165 | typedef struct |
146 | static int eventmax; |
166 | { |
147 | #endif |
167 | W w; |
|
|
168 | int events; |
|
|
169 | } ANPENDING; |
148 | |
170 | |
149 | #if EV_USE_KQUEUE |
171 | #if EV_MULTIPLICITY |
150 | static int kqueue_fd; |
172 | |
151 | static struct kevent *kqueue_changes; |
173 | struct ev_loop |
152 | static int kqueue_changemax, kqueue_changecnt; |
174 | { |
153 | static struct kevent *kqueue_events; |
175 | # define VAR(name,decl) decl; |
154 | static int kqueue_eventmax; |
176 | # include "ev_vars.h" |
|
|
177 | }; |
|
|
178 | # undef VAR |
|
|
179 | # include "ev_wrap.h" |
|
|
180 | |
|
|
181 | #else |
|
|
182 | |
|
|
183 | # define VAR(name,decl) static decl; |
|
|
184 | # include "ev_vars.h" |
|
|
185 | # undef VAR |
|
|
186 | |
155 | #endif |
187 | #endif |
156 | |
188 | |
157 | /*****************************************************************************/ |
189 | /*****************************************************************************/ |
158 | |
190 | |
159 | inline ev_tstamp |
191 | inline ev_tstamp |
… | |
… | |
206 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
238 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
207 | init (base + cur, newcnt - cur); \ |
239 | init (base + cur, newcnt - cur); \ |
208 | cur = newcnt; \ |
240 | cur = newcnt; \ |
209 | } |
241 | } |
210 | |
242 | |
|
|
243 | #define array_slim(stem) \ |
|
|
244 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
245 | { \ |
|
|
246 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
247 | base = realloc (base, sizeof (*base) * (stem ## max)); \ |
|
|
248 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
249 | } |
|
|
250 | |
|
|
251 | #define array_free(stem, idx) \ |
|
|
252 | free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
|
|
253 | |
211 | /*****************************************************************************/ |
254 | /*****************************************************************************/ |
212 | |
|
|
213 | typedef struct |
|
|
214 | { |
|
|
215 | struct ev_watcher_list *head; |
|
|
216 | unsigned char events; |
|
|
217 | unsigned char reify; |
|
|
218 | } ANFD; |
|
|
219 | |
|
|
220 | static ANFD *anfds; |
|
|
221 | static int anfdmax; |
|
|
222 | |
255 | |
223 | static void |
256 | static void |
224 | anfds_init (ANFD *base, int count) |
257 | anfds_init (ANFD *base, int count) |
225 | { |
258 | { |
226 | while (count--) |
259 | while (count--) |
… | |
… | |
230 | base->reify = 0; |
263 | base->reify = 0; |
231 | |
264 | |
232 | ++base; |
265 | ++base; |
233 | } |
266 | } |
234 | } |
267 | } |
235 | |
|
|
236 | typedef struct |
|
|
237 | { |
|
|
238 | W w; |
|
|
239 | int events; |
|
|
240 | } ANPENDING; |
|
|
241 | |
|
|
242 | static ANPENDING *pendings [NUMPRI]; |
|
|
243 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
|
|
244 | |
268 | |
245 | static void |
269 | static void |
246 | event (EV_P_ W w, int events) |
270 | event (EV_P_ W w, int events) |
247 | { |
271 | { |
248 | if (w->pending) |
272 | if (w->pending) |
… | |
… | |
281 | } |
305 | } |
282 | } |
306 | } |
283 | |
307 | |
284 | /*****************************************************************************/ |
308 | /*****************************************************************************/ |
285 | |
309 | |
286 | static int *fdchanges; |
|
|
287 | static int fdchangemax, fdchangecnt; |
|
|
288 | |
|
|
289 | static void |
310 | static void |
290 | fd_reify (EV_P) |
311 | fd_reify (EV_P) |
291 | { |
312 | { |
292 | int i; |
313 | int i; |
293 | |
314 | |
… | |
… | |
302 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
323 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
303 | events |= w->events; |
324 | events |= w->events; |
304 | |
325 | |
305 | anfd->reify = 0; |
326 | anfd->reify = 0; |
306 | |
327 | |
307 | if (anfd->events != events) |
|
|
308 | { |
|
|
309 | method_modify (EV_A_ fd, anfd->events, events); |
328 | method_modify (EV_A_ fd, anfd->events, events); |
310 | anfd->events = events; |
329 | anfd->events = events; |
311 | } |
|
|
312 | } |
330 | } |
313 | |
331 | |
314 | fdchangecnt = 0; |
332 | fdchangecnt = 0; |
315 | } |
333 | } |
316 | |
334 | |
… | |
… | |
353 | |
371 | |
354 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
372 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
355 | static void |
373 | static void |
356 | fd_enomem (EV_P) |
374 | fd_enomem (EV_P) |
357 | { |
375 | { |
358 | int fd = anfdmax; |
376 | int fd; |
359 | |
377 | |
360 | while (fd--) |
378 | for (fd = anfdmax; fd--; ) |
361 | if (anfds [fd].events) |
379 | if (anfds [fd].events) |
362 | { |
380 | { |
363 | close (fd); |
|
|
364 | fd_kill (EV_A_ fd); |
381 | fd_kill (EV_A_ fd); |
365 | return; |
382 | return; |
366 | } |
383 | } |
367 | } |
384 | } |
368 | |
385 | |
|
|
386 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
|
|
387 | static void |
|
|
388 | fd_rearm_all (EV_P) |
|
|
389 | { |
|
|
390 | int fd; |
|
|
391 | |
|
|
392 | /* this should be highly optimised to not do anything but set a flag */ |
|
|
393 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
394 | if (anfds [fd].events) |
|
|
395 | { |
|
|
396 | anfds [fd].events = 0; |
|
|
397 | fd_change (EV_A_ fd); |
|
|
398 | } |
|
|
399 | } |
|
|
400 | |
369 | /*****************************************************************************/ |
401 | /*****************************************************************************/ |
370 | |
402 | |
371 | static struct ev_timer **timers; |
|
|
372 | static int timermax, timercnt; |
|
|
373 | |
|
|
374 | static struct ev_periodic **periodics; |
|
|
375 | static int periodicmax, periodiccnt; |
|
|
376 | |
|
|
377 | static void |
403 | static void |
378 | upheap (WT *timers, int k) |
404 | upheap (WT *heap, int k) |
379 | { |
405 | { |
380 | WT w = timers [k]; |
406 | WT w = heap [k]; |
381 | |
407 | |
382 | while (k && timers [k >> 1]->at > w->at) |
408 | while (k && heap [k >> 1]->at > w->at) |
383 | { |
409 | { |
384 | timers [k] = timers [k >> 1]; |
410 | heap [k] = heap [k >> 1]; |
385 | timers [k]->active = k + 1; |
411 | ((W)heap [k])->active = k + 1; |
386 | k >>= 1; |
412 | k >>= 1; |
387 | } |
413 | } |
388 | |
414 | |
389 | timers [k] = w; |
415 | heap [k] = w; |
390 | timers [k]->active = k + 1; |
416 | ((W)heap [k])->active = k + 1; |
391 | |
417 | |
392 | } |
418 | } |
393 | |
419 | |
394 | static void |
420 | static void |
395 | downheap (WT *timers, int N, int k) |
421 | downheap (WT *heap, int N, int k) |
396 | { |
422 | { |
397 | WT w = timers [k]; |
423 | WT w = heap [k]; |
398 | |
424 | |
399 | while (k < (N >> 1)) |
425 | while (k < (N >> 1)) |
400 | { |
426 | { |
401 | int j = k << 1; |
427 | int j = k << 1; |
402 | |
428 | |
403 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
429 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
404 | ++j; |
430 | ++j; |
405 | |
431 | |
406 | if (w->at <= timers [j]->at) |
432 | if (w->at <= heap [j]->at) |
407 | break; |
433 | break; |
408 | |
434 | |
409 | timers [k] = timers [j]; |
435 | heap [k] = heap [j]; |
410 | timers [k]->active = k + 1; |
436 | ((W)heap [k])->active = k + 1; |
411 | k = j; |
437 | k = j; |
412 | } |
438 | } |
413 | |
439 | |
414 | timers [k] = w; |
440 | heap [k] = w; |
415 | timers [k]->active = k + 1; |
441 | ((W)heap [k])->active = k + 1; |
416 | } |
442 | } |
417 | |
443 | |
418 | /*****************************************************************************/ |
444 | /*****************************************************************************/ |
419 | |
445 | |
420 | typedef struct |
446 | typedef struct |
421 | { |
447 | { |
422 | struct ev_watcher_list *head; |
448 | WL head; |
423 | sig_atomic_t volatile gotsig; |
449 | sig_atomic_t volatile gotsig; |
424 | } ANSIG; |
450 | } ANSIG; |
425 | |
451 | |
426 | static ANSIG *signals; |
452 | static ANSIG *signals; |
427 | static int signalmax; |
453 | static int signalmax; |
… | |
… | |
443 | } |
469 | } |
444 | |
470 | |
445 | static void |
471 | static void |
446 | sighandler (int signum) |
472 | sighandler (int signum) |
447 | { |
473 | { |
|
|
474 | #if WIN32 |
|
|
475 | signal (signum, sighandler); |
|
|
476 | #endif |
|
|
477 | |
448 | signals [signum - 1].gotsig = 1; |
478 | signals [signum - 1].gotsig = 1; |
449 | |
479 | |
450 | if (!gotsig) |
480 | if (!gotsig) |
451 | { |
481 | { |
452 | int old_errno = errno; |
482 | int old_errno = errno; |
… | |
… | |
457 | } |
487 | } |
458 | |
488 | |
459 | static void |
489 | static void |
460 | sigcb (EV_P_ struct ev_io *iow, int revents) |
490 | sigcb (EV_P_ struct ev_io *iow, int revents) |
461 | { |
491 | { |
462 | struct ev_watcher_list *w; |
492 | WL w; |
463 | int signum; |
493 | int signum; |
464 | |
494 | |
465 | read (sigpipe [0], &revents, 1); |
495 | read (sigpipe [0], &revents, 1); |
466 | gotsig = 0; |
496 | gotsig = 0; |
467 | |
497 | |
… | |
… | |
486 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
516 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
487 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
517 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
488 | #endif |
518 | #endif |
489 | |
519 | |
490 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
520 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
491 | ev_io_start (&sigev); |
521 | ev_io_start (EV_A_ &sigev); |
492 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
522 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
493 | } |
523 | } |
494 | |
524 | |
495 | /*****************************************************************************/ |
525 | /*****************************************************************************/ |
496 | |
526 | |
497 | static struct ev_idle **idles; |
527 | #ifndef WIN32 |
498 | static int idlemax, idlecnt; |
|
|
499 | |
|
|
500 | static struct ev_prepare **prepares; |
|
|
501 | static int preparemax, preparecnt; |
|
|
502 | |
|
|
503 | static struct ev_check **checks; |
|
|
504 | static int checkmax, checkcnt; |
|
|
505 | |
|
|
506 | /*****************************************************************************/ |
|
|
507 | |
528 | |
508 | static struct ev_child *childs [PID_HASHSIZE]; |
529 | static struct ev_child *childs [PID_HASHSIZE]; |
509 | static struct ev_signal childev; |
530 | static struct ev_signal childev; |
510 | |
|
|
511 | #ifndef WIN32 |
|
|
512 | |
531 | |
513 | #ifndef WCONTINUED |
532 | #ifndef WCONTINUED |
514 | # define WCONTINUED 0 |
533 | # define WCONTINUED 0 |
515 | #endif |
534 | #endif |
516 | |
535 | |
… | |
… | |
520 | struct ev_child *w; |
539 | struct ev_child *w; |
521 | |
540 | |
522 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
541 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
523 | if (w->pid == pid || !w->pid) |
542 | if (w->pid == pid || !w->pid) |
524 | { |
543 | { |
525 | w->priority = sw->priority; /* need to do it *now* */ |
544 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
526 | w->rpid = pid; |
545 | w->rpid = pid; |
527 | w->rstatus = status; |
546 | w->rstatus = status; |
528 | event (EV_A_ (W)w, EV_CHILD); |
547 | event (EV_A_ (W)w, EV_CHILD); |
529 | } |
548 | } |
530 | } |
549 | } |
531 | |
550 | |
532 | static void |
551 | static void |
… | |
… | |
552 | # include "ev_kqueue.c" |
571 | # include "ev_kqueue.c" |
553 | #endif |
572 | #endif |
554 | #if EV_USE_EPOLL |
573 | #if EV_USE_EPOLL |
555 | # include "ev_epoll.c" |
574 | # include "ev_epoll.c" |
556 | #endif |
575 | #endif |
557 | #if EV_USEV_POLL |
576 | #if EV_USE_POLL |
558 | # include "ev_poll.c" |
577 | # include "ev_poll.c" |
559 | #endif |
578 | #endif |
560 | #if EV_USE_SELECT |
579 | #if EV_USE_SELECT |
561 | # include "ev_select.c" |
580 | # include "ev_select.c" |
562 | #endif |
581 | #endif |
… | |
… | |
589 | ev_method (EV_P) |
608 | ev_method (EV_P) |
590 | { |
609 | { |
591 | return method; |
610 | return method; |
592 | } |
611 | } |
593 | |
612 | |
594 | int |
613 | static void |
595 | ev_init (EV_P_ int methods) |
614 | loop_init (EV_P_ int methods) |
596 | { |
615 | { |
597 | if (!method) |
616 | if (!method) |
598 | { |
617 | { |
599 | #if EV_USE_MONOTONIC |
618 | #if EV_USE_MONOTONIC |
600 | { |
619 | { |
… | |
… | |
605 | #endif |
624 | #endif |
606 | |
625 | |
607 | rt_now = ev_time (); |
626 | rt_now = ev_time (); |
608 | mn_now = get_clock (); |
627 | mn_now = get_clock (); |
609 | now_floor = mn_now; |
628 | now_floor = mn_now; |
610 | diff = rt_now - mn_now; |
629 | rtmn_diff = rt_now - mn_now; |
611 | |
|
|
612 | if (pipe (sigpipe)) |
|
|
613 | return 0; |
|
|
614 | |
630 | |
615 | if (methods == EVMETHOD_AUTO) |
631 | if (methods == EVMETHOD_AUTO) |
616 | if (!enable_secure () && getenv ("LIBmethodS")) |
632 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
617 | methods = atoi (getenv ("LIBmethodS")); |
633 | methods = atoi (getenv ("LIBEV_METHODS")); |
618 | else |
634 | else |
619 | methods = EVMETHOD_ANY; |
635 | methods = EVMETHOD_ANY; |
620 | |
636 | |
621 | method = 0; |
637 | method = 0; |
|
|
638 | #if EV_USE_WIN32 |
|
|
639 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
640 | #endif |
622 | #if EV_USE_KQUEUE |
641 | #if EV_USE_KQUEUE |
623 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
642 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
624 | #endif |
643 | #endif |
625 | #if EV_USE_EPOLL |
644 | #if EV_USE_EPOLL |
626 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
645 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
627 | #endif |
646 | #endif |
628 | #if EV_USEV_POLL |
647 | #if EV_USE_POLL |
629 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
648 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
630 | #endif |
649 | #endif |
631 | #if EV_USE_SELECT |
650 | #if EV_USE_SELECT |
632 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
651 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
633 | #endif |
652 | #endif |
|
|
653 | } |
|
|
654 | } |
634 | |
655 | |
|
|
656 | void |
|
|
657 | loop_destroy (EV_P) |
|
|
658 | { |
|
|
659 | int i; |
|
|
660 | |
|
|
661 | #if EV_USE_WIN32 |
|
|
662 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
663 | #endif |
|
|
664 | #if EV_USE_KQUEUE |
|
|
665 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
|
|
666 | #endif |
|
|
667 | #if EV_USE_EPOLL |
|
|
668 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
|
|
669 | #endif |
|
|
670 | #if EV_USE_POLL |
|
|
671 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
|
|
672 | #endif |
|
|
673 | #if EV_USE_SELECT |
|
|
674 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
|
|
675 | #endif |
|
|
676 | |
|
|
677 | for (i = NUMPRI; i--; ) |
|
|
678 | array_free (pending, [i]); |
|
|
679 | |
|
|
680 | array_free (fdchange, ); |
|
|
681 | array_free (timer, ); |
|
|
682 | array_free (periodic, ); |
|
|
683 | array_free (idle, ); |
|
|
684 | array_free (prepare, ); |
|
|
685 | array_free (check, ); |
|
|
686 | |
|
|
687 | method = 0; |
|
|
688 | /*TODO*/ |
|
|
689 | } |
|
|
690 | |
|
|
691 | void |
|
|
692 | loop_fork (EV_P) |
|
|
693 | { |
|
|
694 | /*TODO*/ |
|
|
695 | #if EV_USE_EPOLL |
|
|
696 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
|
|
697 | #endif |
|
|
698 | #if EV_USE_KQUEUE |
|
|
699 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
|
|
700 | #endif |
|
|
701 | } |
|
|
702 | |
|
|
703 | #if EV_MULTIPLICITY |
|
|
704 | struct ev_loop * |
|
|
705 | ev_loop_new (int methods) |
|
|
706 | { |
|
|
707 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
|
|
708 | |
|
|
709 | loop_init (EV_A_ methods); |
|
|
710 | |
|
|
711 | if (ev_method (EV_A)) |
|
|
712 | return loop; |
|
|
713 | |
|
|
714 | return 0; |
|
|
715 | } |
|
|
716 | |
|
|
717 | void |
|
|
718 | ev_loop_destroy (EV_P) |
|
|
719 | { |
|
|
720 | loop_destroy (EV_A); |
|
|
721 | free (loop); |
|
|
722 | } |
|
|
723 | |
|
|
724 | void |
|
|
725 | ev_loop_fork (EV_P) |
|
|
726 | { |
|
|
727 | loop_fork (EV_A); |
|
|
728 | } |
|
|
729 | |
|
|
730 | #endif |
|
|
731 | |
|
|
732 | #if EV_MULTIPLICITY |
|
|
733 | struct ev_loop default_loop_struct; |
|
|
734 | static struct ev_loop *default_loop; |
|
|
735 | |
|
|
736 | struct ev_loop * |
|
|
737 | #else |
|
|
738 | static int default_loop; |
|
|
739 | |
|
|
740 | int |
|
|
741 | #endif |
|
|
742 | ev_default_loop (int methods) |
|
|
743 | { |
|
|
744 | if (sigpipe [0] == sigpipe [1]) |
|
|
745 | if (pipe (sigpipe)) |
|
|
746 | return 0; |
|
|
747 | |
|
|
748 | if (!default_loop) |
|
|
749 | { |
|
|
750 | #if EV_MULTIPLICITY |
|
|
751 | struct ev_loop *loop = default_loop = &default_loop_struct; |
|
|
752 | #else |
|
|
753 | default_loop = 1; |
|
|
754 | #endif |
|
|
755 | |
|
|
756 | loop_init (EV_A_ methods); |
|
|
757 | |
635 | if (method) |
758 | if (ev_method (EV_A)) |
636 | { |
759 | { |
637 | ev_watcher_init (&sigev, sigcb); |
760 | ev_watcher_init (&sigev, sigcb); |
638 | ev_set_priority (&sigev, EV_MAXPRI); |
761 | ev_set_priority (&sigev, EV_MAXPRI); |
639 | siginit (EV_A); |
762 | siginit (EV_A); |
640 | |
763 | |
… | |
… | |
643 | ev_set_priority (&childev, EV_MAXPRI); |
766 | ev_set_priority (&childev, EV_MAXPRI); |
644 | ev_signal_start (EV_A_ &childev); |
767 | ev_signal_start (EV_A_ &childev); |
645 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
768 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
646 | #endif |
769 | #endif |
647 | } |
770 | } |
|
|
771 | else |
|
|
772 | default_loop = 0; |
648 | } |
773 | } |
649 | |
774 | |
650 | return method; |
775 | return default_loop; |
651 | } |
776 | } |
652 | |
777 | |
653 | /*****************************************************************************/ |
|
|
654 | |
|
|
655 | void |
778 | void |
656 | ev_fork_prepare (void) |
779 | ev_default_destroy (void) |
657 | { |
780 | { |
658 | /* nop */ |
781 | #if EV_MULTIPLICITY |
659 | } |
782 | struct ev_loop *loop = default_loop; |
660 | |
|
|
661 | void |
|
|
662 | ev_fork_parent (void) |
|
|
663 | { |
|
|
664 | /* nop */ |
|
|
665 | } |
|
|
666 | |
|
|
667 | void |
|
|
668 | ev_fork_child (void) |
|
|
669 | { |
|
|
670 | #if EV_USE_EPOLL |
|
|
671 | if (method == EVMETHOD_EPOLL) |
|
|
672 | epoll_postfork_child (); |
|
|
673 | #endif |
783 | #endif |
674 | |
784 | |
|
|
785 | ev_ref (EV_A); /* child watcher */ |
|
|
786 | ev_signal_stop (EV_A_ &childev); |
|
|
787 | |
|
|
788 | ev_ref (EV_A); /* signal watcher */ |
675 | ev_io_stop (&sigev); |
789 | ev_io_stop (EV_A_ &sigev); |
|
|
790 | |
|
|
791 | close (sigpipe [0]); sigpipe [0] = 0; |
|
|
792 | close (sigpipe [1]); sigpipe [1] = 0; |
|
|
793 | |
|
|
794 | loop_destroy (EV_A); |
|
|
795 | } |
|
|
796 | |
|
|
797 | void |
|
|
798 | ev_default_fork (void) |
|
|
799 | { |
|
|
800 | #if EV_MULTIPLICITY |
|
|
801 | struct ev_loop *loop = default_loop; |
|
|
802 | #endif |
|
|
803 | |
|
|
804 | loop_fork (EV_A); |
|
|
805 | |
|
|
806 | ev_io_stop (EV_A_ &sigev); |
676 | close (sigpipe [0]); |
807 | close (sigpipe [0]); |
677 | close (sigpipe [1]); |
808 | close (sigpipe [1]); |
678 | pipe (sigpipe); |
809 | pipe (sigpipe); |
|
|
810 | |
|
|
811 | ev_ref (EV_A); /* signal watcher */ |
679 | siginit (); |
812 | siginit (EV_A); |
680 | } |
813 | } |
681 | |
814 | |
682 | /*****************************************************************************/ |
815 | /*****************************************************************************/ |
683 | |
816 | |
684 | static void |
817 | static void |
… | |
… | |
700 | } |
833 | } |
701 | |
834 | |
702 | static void |
835 | static void |
703 | timers_reify (EV_P) |
836 | timers_reify (EV_P) |
704 | { |
837 | { |
705 | while (timercnt && timers [0]->at <= mn_now) |
838 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
706 | { |
839 | { |
707 | struct ev_timer *w = timers [0]; |
840 | struct ev_timer *w = timers [0]; |
|
|
841 | |
|
|
842 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
708 | |
843 | |
709 | /* first reschedule or stop timer */ |
844 | /* first reschedule or stop timer */ |
710 | if (w->repeat) |
845 | if (w->repeat) |
711 | { |
846 | { |
712 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
847 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
713 | w->at = mn_now + w->repeat; |
848 | ((WT)w)->at = mn_now + w->repeat; |
714 | downheap ((WT *)timers, timercnt, 0); |
849 | downheap ((WT *)timers, timercnt, 0); |
715 | } |
850 | } |
716 | else |
851 | else |
717 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
852 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
718 | |
853 | |
719 | event ((W)w, EV_TIMEOUT); |
854 | event (EV_A_ (W)w, EV_TIMEOUT); |
720 | } |
855 | } |
721 | } |
856 | } |
722 | |
857 | |
723 | static void |
858 | static void |
724 | periodics_reify (EV_P) |
859 | periodics_reify (EV_P) |
725 | { |
860 | { |
726 | while (periodiccnt && periodics [0]->at <= rt_now) |
861 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
727 | { |
862 | { |
728 | struct ev_periodic *w = periodics [0]; |
863 | struct ev_periodic *w = periodics [0]; |
|
|
864 | |
|
|
865 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
729 | |
866 | |
730 | /* first reschedule or stop timer */ |
867 | /* first reschedule or stop timer */ |
731 | if (w->interval) |
868 | if (w->interval) |
732 | { |
869 | { |
733 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
870 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
734 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
871 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
735 | downheap ((WT *)periodics, periodiccnt, 0); |
872 | downheap ((WT *)periodics, periodiccnt, 0); |
736 | } |
873 | } |
737 | else |
874 | else |
738 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
875 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
739 | |
876 | |
740 | event (EV_A_ (W)w, EV_PERIODIC); |
877 | event (EV_A_ (W)w, EV_PERIODIC); |
741 | } |
878 | } |
742 | } |
879 | } |
743 | |
880 | |
744 | static void |
881 | static void |
745 | periodics_reschedule (EV_P_ ev_tstamp diff) |
882 | periodics_reschedule (EV_P) |
746 | { |
883 | { |
747 | int i; |
884 | int i; |
748 | |
885 | |
749 | /* adjust periodics after time jump */ |
886 | /* adjust periodics after time jump */ |
750 | for (i = 0; i < periodiccnt; ++i) |
887 | for (i = 0; i < periodiccnt; ++i) |
751 | { |
888 | { |
752 | struct ev_periodic *w = periodics [i]; |
889 | struct ev_periodic *w = periodics [i]; |
753 | |
890 | |
754 | if (w->interval) |
891 | if (w->interval) |
755 | { |
892 | { |
756 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
893 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
757 | |
894 | |
758 | if (fabs (diff) >= 1e-4) |
895 | if (fabs (diff) >= 1e-4) |
759 | { |
896 | { |
760 | ev_periodic_stop (EV_A_ w); |
897 | ev_periodic_stop (EV_A_ w); |
761 | ev_periodic_start (EV_A_ w); |
898 | ev_periodic_start (EV_A_ w); |
… | |
… | |
771 | { |
908 | { |
772 | mn_now = get_clock (); |
909 | mn_now = get_clock (); |
773 | |
910 | |
774 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
911 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
775 | { |
912 | { |
776 | rt_now = mn_now + diff; |
913 | rt_now = rtmn_diff + mn_now; |
777 | return 0; |
914 | return 0; |
778 | } |
915 | } |
779 | else |
916 | else |
780 | { |
917 | { |
781 | now_floor = mn_now; |
918 | now_floor = mn_now; |
… | |
… | |
792 | #if EV_USE_MONOTONIC |
929 | #if EV_USE_MONOTONIC |
793 | if (expect_true (have_monotonic)) |
930 | if (expect_true (have_monotonic)) |
794 | { |
931 | { |
795 | if (time_update_monotonic (EV_A)) |
932 | if (time_update_monotonic (EV_A)) |
796 | { |
933 | { |
797 | ev_tstamp odiff = diff; |
934 | ev_tstamp odiff = rtmn_diff; |
798 | |
935 | |
799 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
936 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
800 | { |
937 | { |
801 | diff = rt_now - mn_now; |
938 | rtmn_diff = rt_now - mn_now; |
802 | |
939 | |
803 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
940 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
804 | return; /* all is well */ |
941 | return; /* all is well */ |
805 | |
942 | |
806 | rt_now = ev_time (); |
943 | rt_now = ev_time (); |
807 | mn_now = get_clock (); |
944 | mn_now = get_clock (); |
808 | now_floor = mn_now; |
945 | now_floor = mn_now; |
809 | } |
946 | } |
810 | |
947 | |
811 | periodics_reschedule (EV_A_ diff - odiff); |
948 | periodics_reschedule (EV_A); |
812 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
949 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
950 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
813 | } |
951 | } |
814 | } |
952 | } |
815 | else |
953 | else |
816 | #endif |
954 | #endif |
817 | { |
955 | { |
818 | rt_now = ev_time (); |
956 | rt_now = ev_time (); |
819 | |
957 | |
820 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
958 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
821 | { |
959 | { |
822 | periodics_reschedule (EV_A_ rt_now - mn_now); |
960 | periodics_reschedule (EV_A); |
823 | |
961 | |
824 | /* adjust timers. this is easy, as the offset is the same for all */ |
962 | /* adjust timers. this is easy, as the offset is the same for all */ |
825 | for (i = 0; i < timercnt; ++i) |
963 | for (i = 0; i < timercnt; ++i) |
826 | timers [i]->at += diff; |
964 | ((WT)timers [i])->at += rt_now - mn_now; |
827 | } |
965 | } |
828 | |
966 | |
829 | mn_now = rt_now; |
967 | mn_now = rt_now; |
830 | } |
968 | } |
831 | } |
969 | } |
… | |
… | |
882 | { |
1020 | { |
883 | block = MAX_BLOCKTIME; |
1021 | block = MAX_BLOCKTIME; |
884 | |
1022 | |
885 | if (timercnt) |
1023 | if (timercnt) |
886 | { |
1024 | { |
887 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1025 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
888 | if (block > to) block = to; |
1026 | if (block > to) block = to; |
889 | } |
1027 | } |
890 | |
1028 | |
891 | if (periodiccnt) |
1029 | if (periodiccnt) |
892 | { |
1030 | { |
893 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1031 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
894 | if (block > to) block = to; |
1032 | if (block > to) block = to; |
895 | } |
1033 | } |
896 | |
1034 | |
897 | if (block < 0.) block = 0.; |
1035 | if (block < 0.) block = 0.; |
898 | } |
1036 | } |
… | |
… | |
1015 | ev_timer_start (EV_P_ struct ev_timer *w) |
1153 | ev_timer_start (EV_P_ struct ev_timer *w) |
1016 | { |
1154 | { |
1017 | if (ev_is_active (w)) |
1155 | if (ev_is_active (w)) |
1018 | return; |
1156 | return; |
1019 | |
1157 | |
1020 | w->at += mn_now; |
1158 | ((WT)w)->at += mn_now; |
1021 | |
1159 | |
1022 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1160 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1023 | |
1161 | |
1024 | ev_start (EV_A_ (W)w, ++timercnt); |
1162 | ev_start (EV_A_ (W)w, ++timercnt); |
1025 | array_needsize (timers, timermax, timercnt, ); |
1163 | array_needsize (timers, timermax, timercnt, ); |
1026 | timers [timercnt - 1] = w; |
1164 | timers [timercnt - 1] = w; |
1027 | upheap ((WT *)timers, timercnt - 1); |
1165 | upheap ((WT *)timers, timercnt - 1); |
|
|
1166 | |
|
|
1167 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1028 | } |
1168 | } |
1029 | |
1169 | |
1030 | void |
1170 | void |
1031 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1171 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1032 | { |
1172 | { |
1033 | ev_clear_pending (EV_A_ (W)w); |
1173 | ev_clear_pending (EV_A_ (W)w); |
1034 | if (!ev_is_active (w)) |
1174 | if (!ev_is_active (w)) |
1035 | return; |
1175 | return; |
1036 | |
1176 | |
|
|
1177 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1178 | |
1037 | if (w->active < timercnt--) |
1179 | if (((W)w)->active < timercnt--) |
1038 | { |
1180 | { |
1039 | timers [w->active - 1] = timers [timercnt]; |
1181 | timers [((W)w)->active - 1] = timers [timercnt]; |
1040 | downheap ((WT *)timers, timercnt, w->active - 1); |
1182 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1041 | } |
1183 | } |
1042 | |
1184 | |
1043 | w->at = w->repeat; |
1185 | ((WT)w)->at = w->repeat; |
1044 | |
1186 | |
1045 | ev_stop (EV_A_ (W)w); |
1187 | ev_stop (EV_A_ (W)w); |
1046 | } |
1188 | } |
1047 | |
1189 | |
1048 | void |
1190 | void |
… | |
… | |
1050 | { |
1192 | { |
1051 | if (ev_is_active (w)) |
1193 | if (ev_is_active (w)) |
1052 | { |
1194 | { |
1053 | if (w->repeat) |
1195 | if (w->repeat) |
1054 | { |
1196 | { |
1055 | w->at = mn_now + w->repeat; |
1197 | ((WT)w)->at = mn_now + w->repeat; |
1056 | downheap ((WT *)timers, timercnt, w->active - 1); |
1198 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1057 | } |
1199 | } |
1058 | else |
1200 | else |
1059 | ev_timer_stop (EV_A_ w); |
1201 | ev_timer_stop (EV_A_ w); |
1060 | } |
1202 | } |
1061 | else if (w->repeat) |
1203 | else if (w->repeat) |
… | |
… | |
1070 | |
1212 | |
1071 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1213 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1072 | |
1214 | |
1073 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1215 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1074 | if (w->interval) |
1216 | if (w->interval) |
1075 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1217 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1076 | |
1218 | |
1077 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1219 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1078 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1220 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1079 | periodics [periodiccnt - 1] = w; |
1221 | periodics [periodiccnt - 1] = w; |
1080 | upheap ((WT *)periodics, periodiccnt - 1); |
1222 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1223 | |
|
|
1224 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1081 | } |
1225 | } |
1082 | |
1226 | |
1083 | void |
1227 | void |
1084 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1228 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1085 | { |
1229 | { |
1086 | ev_clear_pending (EV_A_ (W)w); |
1230 | ev_clear_pending (EV_A_ (W)w); |
1087 | if (!ev_is_active (w)) |
1231 | if (!ev_is_active (w)) |
1088 | return; |
1232 | return; |
1089 | |
1233 | |
|
|
1234 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1235 | |
1090 | if (w->active < periodiccnt--) |
1236 | if (((W)w)->active < periodiccnt--) |
1091 | { |
1237 | { |
1092 | periodics [w->active - 1] = periodics [periodiccnt]; |
1238 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1093 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1239 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1094 | } |
1240 | } |
1095 | |
1241 | |
|
|
1242 | ev_stop (EV_A_ (W)w); |
|
|
1243 | } |
|
|
1244 | |
|
|
1245 | void |
|
|
1246 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1247 | { |
|
|
1248 | if (ev_is_active (w)) |
|
|
1249 | return; |
|
|
1250 | |
|
|
1251 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1252 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1253 | idles [idlecnt - 1] = w; |
|
|
1254 | } |
|
|
1255 | |
|
|
1256 | void |
|
|
1257 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1258 | { |
|
|
1259 | ev_clear_pending (EV_A_ (W)w); |
|
|
1260 | if (ev_is_active (w)) |
|
|
1261 | return; |
|
|
1262 | |
|
|
1263 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
|
|
1264 | ev_stop (EV_A_ (W)w); |
|
|
1265 | } |
|
|
1266 | |
|
|
1267 | void |
|
|
1268 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1269 | { |
|
|
1270 | if (ev_is_active (w)) |
|
|
1271 | return; |
|
|
1272 | |
|
|
1273 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1274 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1275 | prepares [preparecnt - 1] = w; |
|
|
1276 | } |
|
|
1277 | |
|
|
1278 | void |
|
|
1279 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1280 | { |
|
|
1281 | ev_clear_pending (EV_A_ (W)w); |
|
|
1282 | if (ev_is_active (w)) |
|
|
1283 | return; |
|
|
1284 | |
|
|
1285 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
|
|
1286 | ev_stop (EV_A_ (W)w); |
|
|
1287 | } |
|
|
1288 | |
|
|
1289 | void |
|
|
1290 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1291 | { |
|
|
1292 | if (ev_is_active (w)) |
|
|
1293 | return; |
|
|
1294 | |
|
|
1295 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1296 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1297 | checks [checkcnt - 1] = w; |
|
|
1298 | } |
|
|
1299 | |
|
|
1300 | void |
|
|
1301 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1302 | { |
|
|
1303 | ev_clear_pending (EV_A_ (W)w); |
|
|
1304 | if (ev_is_active (w)) |
|
|
1305 | return; |
|
|
1306 | |
|
|
1307 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1096 | ev_stop (EV_A_ (W)w); |
1308 | ev_stop (EV_A_ (W)w); |
1097 | } |
1309 | } |
1098 | |
1310 | |
1099 | #ifndef SA_RESTART |
1311 | #ifndef SA_RESTART |
1100 | # define SA_RESTART 0 |
1312 | # define SA_RESTART 0 |
1101 | #endif |
1313 | #endif |
1102 | |
1314 | |
1103 | void |
1315 | void |
1104 | ev_signal_start (EV_P_ struct ev_signal *w) |
1316 | ev_signal_start (EV_P_ struct ev_signal *w) |
1105 | { |
1317 | { |
|
|
1318 | #if EV_MULTIPLICITY |
|
|
1319 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1320 | #endif |
1106 | if (ev_is_active (w)) |
1321 | if (ev_is_active (w)) |
1107 | return; |
1322 | return; |
1108 | |
1323 | |
1109 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1324 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1110 | |
1325 | |
1111 | ev_start (EV_A_ (W)w, 1); |
1326 | ev_start (EV_A_ (W)w, 1); |
1112 | array_needsize (signals, signalmax, w->signum, signals_init); |
1327 | array_needsize (signals, signalmax, w->signum, signals_init); |
1113 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1328 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1114 | |
1329 | |
1115 | if (!w->next) |
1330 | if (!((WL)w)->next) |
1116 | { |
1331 | { |
|
|
1332 | #if WIN32 |
|
|
1333 | signal (w->signum, sighandler); |
|
|
1334 | #else |
1117 | struct sigaction sa; |
1335 | struct sigaction sa; |
1118 | sa.sa_handler = sighandler; |
1336 | sa.sa_handler = sighandler; |
1119 | sigfillset (&sa.sa_mask); |
1337 | sigfillset (&sa.sa_mask); |
1120 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1338 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1121 | sigaction (w->signum, &sa, 0); |
1339 | sigaction (w->signum, &sa, 0); |
|
|
1340 | #endif |
1122 | } |
1341 | } |
1123 | } |
1342 | } |
1124 | |
1343 | |
1125 | void |
1344 | void |
1126 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1345 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1135 | if (!signals [w->signum - 1].head) |
1354 | if (!signals [w->signum - 1].head) |
1136 | signal (w->signum, SIG_DFL); |
1355 | signal (w->signum, SIG_DFL); |
1137 | } |
1356 | } |
1138 | |
1357 | |
1139 | void |
1358 | void |
1140 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1141 | { |
|
|
1142 | if (ev_is_active (w)) |
|
|
1143 | return; |
|
|
1144 | |
|
|
1145 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1146 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
1147 | idles [idlecnt - 1] = w; |
|
|
1148 | } |
|
|
1149 | |
|
|
1150 | void |
|
|
1151 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1152 | { |
|
|
1153 | ev_clear_pending (EV_A_ (W)w); |
|
|
1154 | if (ev_is_active (w)) |
|
|
1155 | return; |
|
|
1156 | |
|
|
1157 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
1158 | ev_stop (EV_A_ (W)w); |
|
|
1159 | } |
|
|
1160 | |
|
|
1161 | void |
|
|
1162 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1163 | { |
|
|
1164 | if (ev_is_active (w)) |
|
|
1165 | return; |
|
|
1166 | |
|
|
1167 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1168 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
1169 | prepares [preparecnt - 1] = w; |
|
|
1170 | } |
|
|
1171 | |
|
|
1172 | void |
|
|
1173 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1174 | { |
|
|
1175 | ev_clear_pending (EV_A_ (W)w); |
|
|
1176 | if (ev_is_active (w)) |
|
|
1177 | return; |
|
|
1178 | |
|
|
1179 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
1180 | ev_stop (EV_A_ (W)w); |
|
|
1181 | } |
|
|
1182 | |
|
|
1183 | void |
|
|
1184 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1185 | { |
|
|
1186 | if (ev_is_active (w)) |
|
|
1187 | return; |
|
|
1188 | |
|
|
1189 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1190 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
1191 | checks [checkcnt - 1] = w; |
|
|
1192 | } |
|
|
1193 | |
|
|
1194 | void |
|
|
1195 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1196 | { |
|
|
1197 | ev_clear_pending (EV_A_ (W)w); |
|
|
1198 | if (ev_is_active (w)) |
|
|
1199 | return; |
|
|
1200 | |
|
|
1201 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
1202 | ev_stop (EV_A_ (W)w); |
|
|
1203 | } |
|
|
1204 | |
|
|
1205 | void |
|
|
1206 | ev_child_start (EV_P_ struct ev_child *w) |
1359 | ev_child_start (EV_P_ struct ev_child *w) |
1207 | { |
1360 | { |
|
|
1361 | #if EV_MULTIPLICITY |
|
|
1362 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1363 | #endif |
1208 | if (ev_is_active (w)) |
1364 | if (ev_is_active (w)) |
1209 | return; |
1365 | return; |
1210 | |
1366 | |
1211 | ev_start (EV_A_ (W)w, 1); |
1367 | ev_start (EV_A_ (W)w, 1); |
1212 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1368 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
… | |
… | |
1284 | ev_timer_start (EV_A_ &once->to); |
1440 | ev_timer_start (EV_A_ &once->to); |
1285 | } |
1441 | } |
1286 | } |
1442 | } |
1287 | } |
1443 | } |
1288 | |
1444 | |
1289 | /*****************************************************************************/ |
|
|
1290 | |
|
|
1291 | #if 0 |
|
|
1292 | |
|
|
1293 | struct ev_io wio; |
|
|
1294 | |
|
|
1295 | static void |
|
|
1296 | sin_cb (struct ev_io *w, int revents) |
|
|
1297 | { |
|
|
1298 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
|
|
1299 | } |
|
|
1300 | |
|
|
1301 | static void |
|
|
1302 | ocb (struct ev_timer *w, int revents) |
|
|
1303 | { |
|
|
1304 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
1305 | ev_timer_stop (w); |
|
|
1306 | ev_timer_start (w); |
|
|
1307 | } |
|
|
1308 | |
|
|
1309 | static void |
|
|
1310 | scb (struct ev_signal *w, int revents) |
|
|
1311 | { |
|
|
1312 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1313 | ev_io_stop (&wio); |
|
|
1314 | ev_io_start (&wio); |
|
|
1315 | } |
|
|
1316 | |
|
|
1317 | static void |
|
|
1318 | gcb (struct ev_signal *w, int revents) |
|
|
1319 | { |
|
|
1320 | fprintf (stderr, "generic %x\n", revents); |
|
|
1321 | |
|
|
1322 | } |
|
|
1323 | |
|
|
1324 | int main (void) |
|
|
1325 | { |
|
|
1326 | ev_init (0); |
|
|
1327 | |
|
|
1328 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
|
|
1329 | ev_io_start (&wio); |
|
|
1330 | |
|
|
1331 | struct ev_timer t[10000]; |
|
|
1332 | |
|
|
1333 | #if 0 |
|
|
1334 | int i; |
|
|
1335 | for (i = 0; i < 10000; ++i) |
|
|
1336 | { |
|
|
1337 | struct ev_timer *w = t + i; |
|
|
1338 | ev_watcher_init (w, ocb, i); |
|
|
1339 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
1340 | ev_timer_start (w); |
|
|
1341 | if (drand48 () < 0.5) |
|
|
1342 | ev_timer_stop (w); |
|
|
1343 | } |
|
|
1344 | #endif |
|
|
1345 | |
|
|
1346 | struct ev_timer t1; |
|
|
1347 | ev_timer_init (&t1, ocb, 5, 10); |
|
|
1348 | ev_timer_start (&t1); |
|
|
1349 | |
|
|
1350 | struct ev_signal sig; |
|
|
1351 | ev_signal_init (&sig, scb, SIGQUIT); |
|
|
1352 | ev_signal_start (&sig); |
|
|
1353 | |
|
|
1354 | struct ev_check cw; |
|
|
1355 | ev_check_init (&cw, gcb); |
|
|
1356 | ev_check_start (&cw); |
|
|
1357 | |
|
|
1358 | struct ev_idle iw; |
|
|
1359 | ev_idle_init (&iw, gcb); |
|
|
1360 | ev_idle_start (&iw); |
|
|
1361 | |
|
|
1362 | ev_loop (0); |
|
|
1363 | |
|
|
1364 | return 0; |
|
|
1365 | } |
|
|
1366 | |
|
|
1367 | #endif |
|
|
1368 | |
|
|
1369 | |
|
|
1370 | |
|
|
1371 | |
|
|