… | |
… | |
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 | static void (*syserr_cb)(const char *msg); |
143 | static int epoll_fd = -1; |
|
|
144 | |
159 | |
145 | static struct epoll_event *events; |
160 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
146 | static int eventmax; |
161 | { |
147 | #endif |
162 | syserr_cb = cb; |
|
|
163 | } |
148 | |
164 | |
149 | #if EV_USE_KQUEUE |
165 | static void |
150 | static int kqueue_fd; |
166 | syserr (const char *msg) |
151 | static struct kevent *kqueue_changes; |
167 | { |
152 | static int kqueue_changemax, kqueue_changecnt; |
168 | if (!msg) |
153 | static struct kevent *kqueue_events; |
169 | msg = "(libev) system error"; |
154 | static int kqueue_eventmax; |
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 | |
|
|
206 | typedef struct |
|
|
207 | { |
|
|
208 | WL head; |
|
|
209 | unsigned char events; |
|
|
210 | unsigned char reify; |
|
|
211 | } ANFD; |
|
|
212 | |
|
|
213 | typedef struct |
|
|
214 | { |
|
|
215 | W w; |
|
|
216 | int events; |
|
|
217 | } ANPENDING; |
|
|
218 | |
|
|
219 | #if EV_MULTIPLICITY |
|
|
220 | |
|
|
221 | struct ev_loop |
|
|
222 | { |
|
|
223 | # define VAR(name,decl) decl; |
|
|
224 | # include "ev_vars.h" |
|
|
225 | }; |
|
|
226 | # undef VAR |
|
|
227 | # include "ev_wrap.h" |
|
|
228 | |
|
|
229 | #else |
|
|
230 | |
|
|
231 | # define VAR(name,decl) static decl; |
|
|
232 | # include "ev_vars.h" |
|
|
233 | # undef VAR |
|
|
234 | |
155 | #endif |
235 | #endif |
156 | |
236 | |
157 | /*****************************************************************************/ |
237 | /*****************************************************************************/ |
158 | |
238 | |
159 | inline ev_tstamp |
239 | inline ev_tstamp |
… | |
… | |
191 | return rt_now; |
271 | return rt_now; |
192 | } |
272 | } |
193 | |
273 | |
194 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
274 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
195 | |
275 | |
196 | #define array_needsize(base,cur,cnt,init) \ |
276 | #define array_needsize(base,cur,cnt,init) \ |
197 | if (expect_false ((cnt) > cur)) \ |
277 | if (expect_false ((cnt) > cur)) \ |
198 | { \ |
278 | { \ |
199 | int newcnt = cur; \ |
279 | int newcnt = cur; \ |
200 | do \ |
280 | do \ |
201 | { \ |
281 | { \ |
202 | newcnt = array_roundsize (base, newcnt << 1); \ |
282 | newcnt = array_roundsize (base, newcnt << 1); \ |
203 | } \ |
283 | } \ |
204 | while ((cnt) > newcnt); \ |
284 | while ((cnt) > newcnt); \ |
205 | \ |
285 | \ |
206 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
286 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
207 | init (base + cur, newcnt - cur); \ |
287 | init (base + cur, newcnt - cur); \ |
208 | cur = newcnt; \ |
288 | cur = newcnt; \ |
209 | } |
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; |
210 | |
301 | |
211 | /*****************************************************************************/ |
302 | /*****************************************************************************/ |
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 | |
303 | |
223 | static void |
304 | static void |
224 | anfds_init (ANFD *base, int count) |
305 | anfds_init (ANFD *base, int count) |
225 | { |
306 | { |
226 | while (count--) |
307 | while (count--) |
… | |
… | |
230 | base->reify = 0; |
311 | base->reify = 0; |
231 | |
312 | |
232 | ++base; |
313 | ++base; |
233 | } |
314 | } |
234 | } |
315 | } |
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 | |
316 | |
245 | static void |
317 | static void |
246 | event (EV_P_ W w, int events) |
318 | event (EV_P_ W w, int events) |
247 | { |
319 | { |
248 | if (w->pending) |
320 | if (w->pending) |
… | |
… | |
281 | } |
353 | } |
282 | } |
354 | } |
283 | |
355 | |
284 | /*****************************************************************************/ |
356 | /*****************************************************************************/ |
285 | |
357 | |
286 | static int *fdchanges; |
|
|
287 | static int fdchangemax, fdchangecnt; |
|
|
288 | |
|
|
289 | static void |
358 | static void |
290 | fd_reify (EV_P) |
359 | fd_reify (EV_P) |
291 | { |
360 | { |
292 | int i; |
361 | int i; |
293 | |
362 | |
… | |
… | |
302 | 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) |
303 | events |= w->events; |
372 | events |= w->events; |
304 | |
373 | |
305 | anfd->reify = 0; |
374 | anfd->reify = 0; |
306 | |
375 | |
307 | if (anfd->events != events) |
|
|
308 | { |
|
|
309 | method_modify (EV_A_ fd, anfd->events, events); |
376 | method_modify (EV_A_ fd, anfd->events, events); |
310 | anfd->events = events; |
377 | anfd->events = events; |
311 | } |
|
|
312 | } |
378 | } |
313 | |
379 | |
314 | fdchangecnt = 0; |
380 | fdchangecnt = 0; |
315 | } |
381 | } |
316 | |
382 | |
317 | static void |
383 | static void |
318 | fd_change (EV_P_ int fd) |
384 | fd_change (EV_P_ int fd) |
319 | { |
385 | { |
320 | if (anfds [fd].reify || fdchangecnt < 0) |
386 | if (anfds [fd].reify) |
321 | return; |
387 | return; |
322 | |
388 | |
323 | anfds [fd].reify = 1; |
389 | anfds [fd].reify = 1; |
324 | |
390 | |
325 | ++fdchangecnt; |
391 | ++fdchangecnt; |
… | |
… | |
353 | |
419 | |
354 | /* 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 */ |
355 | static void |
421 | static void |
356 | fd_enomem (EV_P) |
422 | fd_enomem (EV_P) |
357 | { |
423 | { |
358 | int fd = anfdmax; |
424 | int fd; |
359 | |
425 | |
360 | while (fd--) |
426 | for (fd = anfdmax; fd--; ) |
361 | if (anfds [fd].events) |
427 | if (anfds [fd].events) |
362 | { |
428 | { |
363 | close (fd); |
|
|
364 | fd_kill (EV_A_ fd); |
429 | fd_kill (EV_A_ fd); |
365 | return; |
430 | return; |
366 | } |
431 | } |
367 | } |
432 | } |
368 | |
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 | |
369 | /*****************************************************************************/ |
449 | /*****************************************************************************/ |
370 | |
450 | |
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 |
451 | static void |
378 | upheap (WT *timers, int k) |
452 | upheap (WT *heap, int k) |
379 | { |
453 | { |
380 | WT w = timers [k]; |
454 | WT w = heap [k]; |
381 | |
455 | |
382 | while (k && timers [k >> 1]->at > w->at) |
456 | while (k && heap [k >> 1]->at > w->at) |
383 | { |
457 | { |
384 | timers [k] = timers [k >> 1]; |
458 | heap [k] = heap [k >> 1]; |
385 | timers [k]->active = k + 1; |
459 | ((W)heap [k])->active = k + 1; |
386 | k >>= 1; |
460 | k >>= 1; |
387 | } |
461 | } |
388 | |
462 | |
389 | timers [k] = w; |
463 | heap [k] = w; |
390 | timers [k]->active = k + 1; |
464 | ((W)heap [k])->active = k + 1; |
391 | |
465 | |
392 | } |
466 | } |
393 | |
467 | |
394 | static void |
468 | static void |
395 | downheap (WT *timers, int N, int k) |
469 | downheap (WT *heap, int N, int k) |
396 | { |
470 | { |
397 | WT w = timers [k]; |
471 | WT w = heap [k]; |
398 | |
472 | |
399 | while (k < (N >> 1)) |
473 | while (k < (N >> 1)) |
400 | { |
474 | { |
401 | int j = k << 1; |
475 | int j = k << 1; |
402 | |
476 | |
403 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
477 | if (j + 1 < N && heap [j]->at > heap [j + 1]->at) |
404 | ++j; |
478 | ++j; |
405 | |
479 | |
406 | if (w->at <= timers [j]->at) |
480 | if (w->at <= heap [j]->at) |
407 | break; |
481 | break; |
408 | |
482 | |
409 | timers [k] = timers [j]; |
483 | heap [k] = heap [j]; |
410 | timers [k]->active = k + 1; |
484 | ((W)heap [k])->active = k + 1; |
411 | k = j; |
485 | k = j; |
412 | } |
486 | } |
413 | |
487 | |
414 | timers [k] = w; |
488 | heap [k] = w; |
415 | timers [k]->active = k + 1; |
489 | ((W)heap [k])->active = k + 1; |
416 | } |
490 | } |
417 | |
491 | |
418 | /*****************************************************************************/ |
492 | /*****************************************************************************/ |
419 | |
493 | |
420 | typedef struct |
494 | typedef struct |
421 | { |
495 | { |
422 | struct ev_watcher_list *head; |
496 | WL head; |
423 | sig_atomic_t volatile gotsig; |
497 | sig_atomic_t volatile gotsig; |
424 | } ANSIG; |
498 | } ANSIG; |
425 | |
499 | |
426 | static ANSIG *signals; |
500 | static ANSIG *signals; |
427 | static int signalmax; |
501 | static int signalmax; |
… | |
… | |
443 | } |
517 | } |
444 | |
518 | |
445 | static void |
519 | static void |
446 | sighandler (int signum) |
520 | sighandler (int signum) |
447 | { |
521 | { |
|
|
522 | #if WIN32 |
|
|
523 | signal (signum, sighandler); |
|
|
524 | #endif |
|
|
525 | |
448 | signals [signum - 1].gotsig = 1; |
526 | signals [signum - 1].gotsig = 1; |
449 | |
527 | |
450 | if (!gotsig) |
528 | if (!gotsig) |
451 | { |
529 | { |
452 | int old_errno = errno; |
530 | int old_errno = errno; |
… | |
… | |
457 | } |
535 | } |
458 | |
536 | |
459 | static void |
537 | static void |
460 | sigcb (EV_P_ struct ev_io *iow, int revents) |
538 | sigcb (EV_P_ struct ev_io *iow, int revents) |
461 | { |
539 | { |
462 | struct ev_watcher_list *w; |
540 | WL w; |
463 | int signum; |
541 | int signum; |
464 | |
542 | |
465 | read (sigpipe [0], &revents, 1); |
543 | read (sigpipe [0], &revents, 1); |
466 | gotsig = 0; |
544 | gotsig = 0; |
467 | |
545 | |
… | |
… | |
486 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
564 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
487 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
565 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
488 | #endif |
566 | #endif |
489 | |
567 | |
490 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
568 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
491 | ev_io_start (&sigev); |
569 | ev_io_start (EV_A_ &sigev); |
492 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
570 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
493 | } |
571 | } |
494 | |
572 | |
495 | /*****************************************************************************/ |
573 | /*****************************************************************************/ |
496 | |
574 | |
497 | static struct ev_idle **idles; |
575 | #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 | |
576 | |
508 | static struct ev_child *childs [PID_HASHSIZE]; |
577 | static struct ev_child *childs [PID_HASHSIZE]; |
509 | static struct ev_signal childev; |
578 | static struct ev_signal childev; |
510 | |
|
|
511 | #ifndef WIN32 |
|
|
512 | |
579 | |
513 | #ifndef WCONTINUED |
580 | #ifndef WCONTINUED |
514 | # define WCONTINUED 0 |
581 | # define WCONTINUED 0 |
515 | #endif |
582 | #endif |
516 | |
583 | |
… | |
… | |
520 | struct ev_child *w; |
587 | struct ev_child *w; |
521 | |
588 | |
522 | 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) |
523 | if (w->pid == pid || !w->pid) |
590 | if (w->pid == pid || !w->pid) |
524 | { |
591 | { |
525 | w->priority = sw->priority; /* need to do it *now* */ |
592 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
526 | w->rpid = pid; |
593 | w->rpid = pid; |
527 | w->rstatus = status; |
594 | w->rstatus = status; |
528 | event (EV_A_ (W)w, EV_CHILD); |
595 | event (EV_A_ (W)w, EV_CHILD); |
529 | } |
596 | } |
530 | } |
597 | } |
531 | |
598 | |
532 | static void |
599 | static void |
… | |
… | |
552 | # include "ev_kqueue.c" |
619 | # include "ev_kqueue.c" |
553 | #endif |
620 | #endif |
554 | #if EV_USE_EPOLL |
621 | #if EV_USE_EPOLL |
555 | # include "ev_epoll.c" |
622 | # include "ev_epoll.c" |
556 | #endif |
623 | #endif |
557 | #if EV_USEV_POLL |
624 | #if EV_USE_POLL |
558 | # include "ev_poll.c" |
625 | # include "ev_poll.c" |
559 | #endif |
626 | #endif |
560 | #if EV_USE_SELECT |
627 | #if EV_USE_SELECT |
561 | # include "ev_select.c" |
628 | # include "ev_select.c" |
562 | #endif |
629 | #endif |
… | |
… | |
589 | ev_method (EV_P) |
656 | ev_method (EV_P) |
590 | { |
657 | { |
591 | return method; |
658 | return method; |
592 | } |
659 | } |
593 | |
660 | |
594 | int |
661 | static void |
595 | ev_init (EV_P_ int methods) |
662 | loop_init (EV_P_ int methods) |
596 | { |
663 | { |
597 | if (!method) |
664 | if (!method) |
598 | { |
665 | { |
599 | #if EV_USE_MONOTONIC |
666 | #if EV_USE_MONOTONIC |
600 | { |
667 | { |
… | |
… | |
605 | #endif |
672 | #endif |
606 | |
673 | |
607 | rt_now = ev_time (); |
674 | rt_now = ev_time (); |
608 | mn_now = get_clock (); |
675 | mn_now = get_clock (); |
609 | now_floor = mn_now; |
676 | now_floor = mn_now; |
610 | diff = rt_now - mn_now; |
677 | rtmn_diff = rt_now - mn_now; |
611 | |
|
|
612 | if (pipe (sigpipe)) |
|
|
613 | return 0; |
|
|
614 | |
678 | |
615 | if (methods == EVMETHOD_AUTO) |
679 | if (methods == EVMETHOD_AUTO) |
616 | if (!enable_secure () && getenv ("LIBmethodS")) |
680 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
617 | methods = atoi (getenv ("LIBmethodS")); |
681 | methods = atoi (getenv ("LIBEV_METHODS")); |
618 | else |
682 | else |
619 | methods = EVMETHOD_ANY; |
683 | methods = EVMETHOD_ANY; |
620 | |
684 | |
621 | method = 0; |
685 | method = 0; |
|
|
686 | #if EV_USE_WIN32 |
|
|
687 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
688 | #endif |
622 | #if EV_USE_KQUEUE |
689 | #if EV_USE_KQUEUE |
623 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
690 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
624 | #endif |
691 | #endif |
625 | #if EV_USE_EPOLL |
692 | #if EV_USE_EPOLL |
626 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
693 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
627 | #endif |
694 | #endif |
628 | #if EV_USEV_POLL |
695 | #if EV_USE_POLL |
629 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
696 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
630 | #endif |
697 | #endif |
631 | #if EV_USE_SELECT |
698 | #if EV_USE_SELECT |
632 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
699 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
633 | #endif |
700 | #endif |
634 | |
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 | |
635 | if (method) |
826 | if (ev_method (EV_A)) |
636 | { |
827 | { |
637 | ev_watcher_init (&sigev, sigcb); |
|
|
638 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
639 | siginit (EV_A); |
828 | siginit (EV_A); |
640 | |
829 | |
641 | #ifndef WIN32 |
830 | #ifndef WIN32 |
642 | ev_signal_init (&childev, childcb, SIGCHLD); |
831 | ev_signal_init (&childev, childcb, SIGCHLD); |
643 | ev_set_priority (&childev, EV_MAXPRI); |
832 | ev_set_priority (&childev, EV_MAXPRI); |
644 | ev_signal_start (EV_A_ &childev); |
833 | ev_signal_start (EV_A_ &childev); |
645 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
834 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
646 | #endif |
835 | #endif |
647 | } |
836 | } |
|
|
837 | else |
|
|
838 | default_loop = 0; |
648 | } |
839 | } |
649 | |
840 | |
650 | return method; |
841 | return default_loop; |
651 | } |
842 | } |
652 | |
843 | |
653 | /*****************************************************************************/ |
|
|
654 | |
|
|
655 | void |
844 | void |
656 | ev_fork_prepare (void) |
845 | ev_default_destroy (void) |
657 | { |
846 | { |
658 | /* nop */ |
847 | #if EV_MULTIPLICITY |
659 | } |
848 | 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 |
849 | #endif |
674 | |
850 | |
|
|
851 | ev_ref (EV_A); /* child watcher */ |
|
|
852 | ev_signal_stop (EV_A_ &childev); |
|
|
853 | |
|
|
854 | ev_ref (EV_A); /* signal watcher */ |
675 | ev_io_stop (&sigev); |
855 | ev_io_stop (EV_A_ &sigev); |
676 | close (sigpipe [0]); |
856 | |
677 | close (sigpipe [1]); |
857 | close (sigpipe [0]); sigpipe [0] = 0; |
678 | pipe (sigpipe); |
858 | close (sigpipe [1]); sigpipe [1] = 0; |
679 | 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; |
680 | } |
872 | } |
681 | |
873 | |
682 | /*****************************************************************************/ |
874 | /*****************************************************************************/ |
683 | |
875 | |
684 | static void |
876 | static void |
… | |
… | |
700 | } |
892 | } |
701 | |
893 | |
702 | static void |
894 | static void |
703 | timers_reify (EV_P) |
895 | timers_reify (EV_P) |
704 | { |
896 | { |
705 | while (timercnt && timers [0]->at <= mn_now) |
897 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
706 | { |
898 | { |
707 | 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))); |
708 | |
902 | |
709 | /* first reschedule or stop timer */ |
903 | /* first reschedule or stop timer */ |
710 | if (w->repeat) |
904 | if (w->repeat) |
711 | { |
905 | { |
712 | 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.)); |
713 | w->at = mn_now + w->repeat; |
907 | ((WT)w)->at = mn_now + w->repeat; |
714 | downheap ((WT *)timers, timercnt, 0); |
908 | downheap ((WT *)timers, timercnt, 0); |
715 | } |
909 | } |
716 | else |
910 | else |
717 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
911 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
718 | |
912 | |
719 | event ((W)w, EV_TIMEOUT); |
913 | event (EV_A_ (W)w, EV_TIMEOUT); |
720 | } |
914 | } |
721 | } |
915 | } |
722 | |
916 | |
723 | static void |
917 | static void |
724 | periodics_reify (EV_P) |
918 | periodics_reify (EV_P) |
725 | { |
919 | { |
726 | while (periodiccnt && periodics [0]->at <= rt_now) |
920 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
727 | { |
921 | { |
728 | 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))); |
729 | |
925 | |
730 | /* first reschedule or stop timer */ |
926 | /* first reschedule or stop timer */ |
731 | if (w->interval) |
927 | if (w->interval) |
732 | { |
928 | { |
733 | 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; |
734 | 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)); |
735 | downheap ((WT *)periodics, periodiccnt, 0); |
931 | downheap ((WT *)periodics, periodiccnt, 0); |
736 | } |
932 | } |
737 | else |
933 | else |
738 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
934 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
739 | |
935 | |
740 | event (EV_A_ (W)w, EV_PERIODIC); |
936 | event (EV_A_ (W)w, EV_PERIODIC); |
741 | } |
937 | } |
742 | } |
938 | } |
743 | |
939 | |
744 | static void |
940 | static void |
745 | periodics_reschedule (EV_P_ ev_tstamp diff) |
941 | periodics_reschedule (EV_P) |
746 | { |
942 | { |
747 | int i; |
943 | int i; |
748 | |
944 | |
749 | /* adjust periodics after time jump */ |
945 | /* adjust periodics after time jump */ |
750 | for (i = 0; i < periodiccnt; ++i) |
946 | for (i = 0; i < periodiccnt; ++i) |
751 | { |
947 | { |
752 | struct ev_periodic *w = periodics [i]; |
948 | struct ev_periodic *w = periodics [i]; |
753 | |
949 | |
754 | if (w->interval) |
950 | if (w->interval) |
755 | { |
951 | { |
756 | 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; |
757 | |
953 | |
758 | if (fabs (diff) >= 1e-4) |
954 | if (fabs (diff) >= 1e-4) |
759 | { |
955 | { |
760 | ev_periodic_stop (EV_A_ w); |
956 | ev_periodic_stop (EV_A_ w); |
761 | ev_periodic_start (EV_A_ w); |
957 | ev_periodic_start (EV_A_ w); |
… | |
… | |
771 | { |
967 | { |
772 | mn_now = get_clock (); |
968 | mn_now = get_clock (); |
773 | |
969 | |
774 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
970 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
775 | { |
971 | { |
776 | rt_now = mn_now + diff; |
972 | rt_now = rtmn_diff + mn_now; |
777 | return 0; |
973 | return 0; |
778 | } |
974 | } |
779 | else |
975 | else |
780 | { |
976 | { |
781 | now_floor = mn_now; |
977 | now_floor = mn_now; |
… | |
… | |
792 | #if EV_USE_MONOTONIC |
988 | #if EV_USE_MONOTONIC |
793 | if (expect_true (have_monotonic)) |
989 | if (expect_true (have_monotonic)) |
794 | { |
990 | { |
795 | if (time_update_monotonic (EV_A)) |
991 | if (time_update_monotonic (EV_A)) |
796 | { |
992 | { |
797 | ev_tstamp odiff = diff; |
993 | ev_tstamp odiff = rtmn_diff; |
798 | |
994 | |
799 | 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 */ |
800 | { |
996 | { |
801 | diff = rt_now - mn_now; |
997 | rtmn_diff = rt_now - mn_now; |
802 | |
998 | |
803 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
999 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
804 | return; /* all is well */ |
1000 | return; /* all is well */ |
805 | |
1001 | |
806 | rt_now = ev_time (); |
1002 | rt_now = ev_time (); |
807 | mn_now = get_clock (); |
1003 | mn_now = get_clock (); |
808 | now_floor = mn_now; |
1004 | now_floor = mn_now; |
809 | } |
1005 | } |
810 | |
1006 | |
811 | periodics_reschedule (EV_A_ diff - odiff); |
1007 | periodics_reschedule (EV_A); |
812 | /* 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) */ |
813 | } |
1010 | } |
814 | } |
1011 | } |
815 | else |
1012 | else |
816 | #endif |
1013 | #endif |
817 | { |
1014 | { |
818 | rt_now = ev_time (); |
1015 | rt_now = ev_time (); |
819 | |
1016 | |
820 | 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)) |
821 | { |
1018 | { |
822 | periodics_reschedule (EV_A_ rt_now - mn_now); |
1019 | periodics_reschedule (EV_A); |
823 | |
1020 | |
824 | /* 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 */ |
825 | for (i = 0; i < timercnt; ++i) |
1022 | for (i = 0; i < timercnt; ++i) |
826 | timers [i]->at += diff; |
1023 | ((WT)timers [i])->at += rt_now - mn_now; |
827 | } |
1024 | } |
828 | |
1025 | |
829 | mn_now = rt_now; |
1026 | mn_now = rt_now; |
830 | } |
1027 | } |
831 | } |
1028 | } |
… | |
… | |
857 | { |
1054 | { |
858 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1055 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
859 | call_pending (EV_A); |
1056 | call_pending (EV_A); |
860 | } |
1057 | } |
861 | |
1058 | |
|
|
1059 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1060 | if (expect_false (postfork)) |
|
|
1061 | loop_fork (EV_A); |
|
|
1062 | |
862 | /* update fd-related kernel structures */ |
1063 | /* update fd-related kernel structures */ |
863 | fd_reify (EV_A); |
1064 | fd_reify (EV_A); |
864 | |
1065 | |
865 | /* calculate blocking time */ |
1066 | /* calculate blocking time */ |
866 | |
1067 | |
… | |
… | |
882 | { |
1083 | { |
883 | block = MAX_BLOCKTIME; |
1084 | block = MAX_BLOCKTIME; |
884 | |
1085 | |
885 | if (timercnt) |
1086 | if (timercnt) |
886 | { |
1087 | { |
887 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1088 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
888 | if (block > to) block = to; |
1089 | if (block > to) block = to; |
889 | } |
1090 | } |
890 | |
1091 | |
891 | if (periodiccnt) |
1092 | if (periodiccnt) |
892 | { |
1093 | { |
893 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1094 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
894 | if (block > to) block = to; |
1095 | if (block > to) block = to; |
895 | } |
1096 | } |
896 | |
1097 | |
897 | if (block < 0.) block = 0.; |
1098 | if (block < 0.) block = 0.; |
898 | } |
1099 | } |
… | |
… | |
1015 | ev_timer_start (EV_P_ struct ev_timer *w) |
1216 | ev_timer_start (EV_P_ struct ev_timer *w) |
1016 | { |
1217 | { |
1017 | if (ev_is_active (w)) |
1218 | if (ev_is_active (w)) |
1018 | return; |
1219 | return; |
1019 | |
1220 | |
1020 | w->at += mn_now; |
1221 | ((WT)w)->at += mn_now; |
1021 | |
1222 | |
1022 | 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.)); |
1023 | |
1224 | |
1024 | ev_start (EV_A_ (W)w, ++timercnt); |
1225 | ev_start (EV_A_ (W)w, ++timercnt); |
1025 | array_needsize (timers, timermax, timercnt, ); |
1226 | array_needsize (timers, timermax, timercnt, ); |
1026 | timers [timercnt - 1] = w; |
1227 | timers [timercnt - 1] = w; |
1027 | upheap ((WT *)timers, timercnt - 1); |
1228 | upheap ((WT *)timers, timercnt - 1); |
|
|
1229 | |
|
|
1230 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1028 | } |
1231 | } |
1029 | |
1232 | |
1030 | void |
1233 | void |
1031 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1234 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1032 | { |
1235 | { |
1033 | ev_clear_pending (EV_A_ (W)w); |
1236 | ev_clear_pending (EV_A_ (W)w); |
1034 | if (!ev_is_active (w)) |
1237 | if (!ev_is_active (w)) |
1035 | return; |
1238 | return; |
1036 | |
1239 | |
|
|
1240 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1241 | |
1037 | if (w->active < timercnt--) |
1242 | if (((W)w)->active < timercnt--) |
1038 | { |
1243 | { |
1039 | timers [w->active - 1] = timers [timercnt]; |
1244 | timers [((W)w)->active - 1] = timers [timercnt]; |
1040 | downheap ((WT *)timers, timercnt, w->active - 1); |
1245 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1041 | } |
1246 | } |
1042 | |
1247 | |
1043 | w->at = w->repeat; |
1248 | ((WT)w)->at = w->repeat; |
1044 | |
1249 | |
1045 | ev_stop (EV_A_ (W)w); |
1250 | ev_stop (EV_A_ (W)w); |
1046 | } |
1251 | } |
1047 | |
1252 | |
1048 | void |
1253 | void |
… | |
… | |
1050 | { |
1255 | { |
1051 | if (ev_is_active (w)) |
1256 | if (ev_is_active (w)) |
1052 | { |
1257 | { |
1053 | if (w->repeat) |
1258 | if (w->repeat) |
1054 | { |
1259 | { |
1055 | w->at = mn_now + w->repeat; |
1260 | ((WT)w)->at = mn_now + w->repeat; |
1056 | downheap ((WT *)timers, timercnt, w->active - 1); |
1261 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1057 | } |
1262 | } |
1058 | else |
1263 | else |
1059 | ev_timer_stop (EV_A_ w); |
1264 | ev_timer_stop (EV_A_ w); |
1060 | } |
1265 | } |
1061 | else if (w->repeat) |
1266 | else if (w->repeat) |
… | |
… | |
1070 | |
1275 | |
1071 | 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.)); |
1072 | |
1277 | |
1073 | /* 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 */ |
1074 | if (w->interval) |
1279 | if (w->interval) |
1075 | 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; |
1076 | |
1281 | |
1077 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1282 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1078 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1283 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1079 | periodics [periodiccnt - 1] = w; |
1284 | periodics [periodiccnt - 1] = w; |
1080 | upheap ((WT *)periodics, periodiccnt - 1); |
1285 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1286 | |
|
|
1287 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1081 | } |
1288 | } |
1082 | |
1289 | |
1083 | void |
1290 | void |
1084 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1291 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1085 | { |
1292 | { |
1086 | ev_clear_pending (EV_A_ (W)w); |
1293 | ev_clear_pending (EV_A_ (W)w); |
1087 | if (!ev_is_active (w)) |
1294 | if (!ev_is_active (w)) |
1088 | return; |
1295 | return; |
1089 | |
1296 | |
|
|
1297 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1298 | |
1090 | if (w->active < periodiccnt--) |
1299 | if (((W)w)->active < periodiccnt--) |
1091 | { |
1300 | { |
1092 | periodics [w->active - 1] = periodics [periodiccnt]; |
1301 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1093 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1302 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1094 | } |
1303 | } |
1095 | |
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]; |
1096 | ev_stop (EV_A_ (W)w); |
1371 | ev_stop (EV_A_ (W)w); |
1097 | } |
1372 | } |
1098 | |
1373 | |
1099 | #ifndef SA_RESTART |
1374 | #ifndef SA_RESTART |
1100 | # define SA_RESTART 0 |
1375 | # define SA_RESTART 0 |
1101 | #endif |
1376 | #endif |
1102 | |
1377 | |
1103 | void |
1378 | void |
1104 | ev_signal_start (EV_P_ struct ev_signal *w) |
1379 | ev_signal_start (EV_P_ struct ev_signal *w) |
1105 | { |
1380 | { |
|
|
1381 | #if EV_MULTIPLICITY |
|
|
1382 | assert (("signal watchers are only supported in the default loop", loop == default_loop)); |
|
|
1383 | #endif |
1106 | if (ev_is_active (w)) |
1384 | if (ev_is_active (w)) |
1107 | return; |
1385 | return; |
1108 | |
1386 | |
1109 | 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)); |
1110 | |
1388 | |
1111 | ev_start (EV_A_ (W)w, 1); |
1389 | ev_start (EV_A_ (W)w, 1); |
1112 | array_needsize (signals, signalmax, w->signum, signals_init); |
1390 | array_needsize (signals, signalmax, w->signum, signals_init); |
1113 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1391 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1114 | |
1392 | |
1115 | if (!w->next) |
1393 | if (!((WL)w)->next) |
1116 | { |
1394 | { |
|
|
1395 | #if WIN32 |
|
|
1396 | signal (w->signum, sighandler); |
|
|
1397 | #else |
1117 | struct sigaction sa; |
1398 | struct sigaction sa; |
1118 | sa.sa_handler = sighandler; |
1399 | sa.sa_handler = sighandler; |
1119 | sigfillset (&sa.sa_mask); |
1400 | sigfillset (&sa.sa_mask); |
1120 | 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 */ |
1121 | sigaction (w->signum, &sa, 0); |
1402 | sigaction (w->signum, &sa, 0); |
|
|
1403 | #endif |
1122 | } |
1404 | } |
1123 | } |
1405 | } |
1124 | |
1406 | |
1125 | void |
1407 | void |
1126 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1408 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1135 | if (!signals [w->signum - 1].head) |
1417 | if (!signals [w->signum - 1].head) |
1136 | signal (w->signum, SIG_DFL); |
1418 | signal (w->signum, SIG_DFL); |
1137 | } |
1419 | } |
1138 | |
1420 | |
1139 | void |
1421 | 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) |
1422 | ev_child_start (EV_P_ struct ev_child *w) |
1207 | { |
1423 | { |
|
|
1424 | #if EV_MULTIPLICITY |
|
|
1425 | assert (("child watchers are only supported in the default loop", loop == default_loop)); |
|
|
1426 | #endif |
1208 | if (ev_is_active (w)) |
1427 | if (ev_is_active (w)) |
1209 | return; |
1428 | return; |
1210 | |
1429 | |
1211 | ev_start (EV_A_ (W)w, 1); |
1430 | ev_start (EV_A_ (W)w, 1); |
1212 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1431 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
… | |
… | |
1239 | void (*cb)(int revents, void *arg) = once->cb; |
1458 | void (*cb)(int revents, void *arg) = once->cb; |
1240 | void *arg = once->arg; |
1459 | void *arg = once->arg; |
1241 | |
1460 | |
1242 | ev_io_stop (EV_A_ &once->io); |
1461 | ev_io_stop (EV_A_ &once->io); |
1243 | ev_timer_stop (EV_A_ &once->to); |
1462 | ev_timer_stop (EV_A_ &once->to); |
1244 | free (once); |
1463 | ev_free (once); |
1245 | |
1464 | |
1246 | cb (revents, arg); |
1465 | cb (revents, arg); |
1247 | } |
1466 | } |
1248 | |
1467 | |
1249 | static void |
1468 | static void |
… | |
… | |
1259 | } |
1478 | } |
1260 | |
1479 | |
1261 | void |
1480 | void |
1262 | 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) |
1263 | { |
1482 | { |
1264 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1483 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1265 | |
1484 | |
1266 | if (!once) |
1485 | if (!once) |
1267 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1486 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1268 | else |
1487 | else |
1269 | { |
1488 | { |
… | |
… | |
1284 | ev_timer_start (EV_A_ &once->to); |
1503 | ev_timer_start (EV_A_ &once->to); |
1285 | } |
1504 | } |
1286 | } |
1505 | } |
1287 | } |
1506 | } |
1288 | |
1507 | |
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 | |
|
|