1 | /* |
1 | /* |
|
|
2 | * libev event processing core, watcher management |
|
|
3 | * |
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
8 | * modification, are permitted provided that the following conditions are |
… | |
… | |
44 | #include <sys/types.h> |
46 | #include <sys/types.h> |
45 | #include <sys/wait.h> |
47 | #include <sys/wait.h> |
46 | #include <sys/time.h> |
48 | #include <sys/time.h> |
47 | #include <time.h> |
49 | #include <time.h> |
48 | |
50 | |
|
|
51 | /**/ |
|
|
52 | |
49 | #ifndef EV_USE_MONOTONIC |
53 | #ifndef EV_USE_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
|
|
51 | # define EV_USE_MONOTONIC 1 |
54 | # define EV_USE_MONOTONIC 1 |
52 | # endif |
|
|
53 | #endif |
55 | #endif |
54 | |
56 | |
55 | #ifndef EV_USE_SELECT |
57 | #ifndef EV_USE_SELECT |
56 | # define EV_USE_SELECT 1 |
58 | # define EV_USE_SELECT 1 |
57 | #endif |
59 | #endif |
58 | |
60 | |
|
|
61 | #ifndef EV_USE_POLL |
|
|
62 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
63 | #endif |
|
|
64 | |
59 | #ifndef EV_USE_EPOLL |
65 | #ifndef EV_USE_EPOLL |
60 | # define EV_USE_EPOLL 0 |
66 | # define EV_USE_EPOLL 0 |
61 | #endif |
67 | #endif |
62 | |
68 | |
|
|
69 | #ifndef EV_USE_KQUEUE |
|
|
70 | # define EV_USE_KQUEUE 0 |
|
|
71 | #endif |
|
|
72 | |
|
|
73 | #ifndef EV_USE_REALTIME |
|
|
74 | # define EV_USE_REALTIME 1 |
|
|
75 | #endif |
|
|
76 | |
|
|
77 | /**/ |
|
|
78 | |
|
|
79 | #ifndef CLOCK_MONOTONIC |
|
|
80 | # undef EV_USE_MONOTONIC |
|
|
81 | # define EV_USE_MONOTONIC 0 |
|
|
82 | #endif |
|
|
83 | |
63 | #ifndef CLOCK_REALTIME |
84 | #ifndef CLOCK_REALTIME |
|
|
85 | # undef EV_USE_REALTIME |
64 | # define EV_USE_REALTIME 0 |
86 | # define EV_USE_REALTIME 0 |
65 | #endif |
87 | #endif |
66 | #ifndef EV_USE_REALTIME |
88 | |
67 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
89 | /**/ |
68 | #endif |
|
|
69 | |
90 | |
70 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
91 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
71 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
92 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
72 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
93 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
73 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
94 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
74 | |
95 | |
75 | #include "ev.h" |
96 | #include "ev.h" |
|
|
97 | |
|
|
98 | #if __GNUC__ >= 3 |
|
|
99 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
100 | # define inline inline |
|
|
101 | #else |
|
|
102 | # define expect(expr,value) (expr) |
|
|
103 | # define inline static |
|
|
104 | #endif |
|
|
105 | |
|
|
106 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
107 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
108 | |
|
|
109 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
110 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
76 | |
111 | |
77 | typedef struct ev_watcher *W; |
112 | typedef struct ev_watcher *W; |
78 | typedef struct ev_watcher_list *WL; |
113 | typedef struct ev_watcher_list *WL; |
79 | typedef struct ev_watcher_time *WT; |
114 | typedef struct ev_watcher_time *WT; |
80 | |
115 | |
81 | static ev_tstamp now, diff; /* monotonic clock */ |
116 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
82 | ev_tstamp ev_now; |
117 | ev_tstamp ev_now; |
83 | int ev_method; |
118 | int ev_method; |
84 | |
119 | |
85 | static int have_monotonic; /* runtime */ |
120 | static int have_monotonic; /* runtime */ |
86 | |
121 | |
… | |
… | |
106 | |
141 | |
107 | static ev_tstamp |
142 | static ev_tstamp |
108 | get_clock (void) |
143 | get_clock (void) |
109 | { |
144 | { |
110 | #if EV_USE_MONOTONIC |
145 | #if EV_USE_MONOTONIC |
111 | if (have_monotonic) |
146 | if (expect_true (have_monotonic)) |
112 | { |
147 | { |
113 | struct timespec ts; |
148 | struct timespec ts; |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
149 | clock_gettime (CLOCK_MONOTONIC, &ts); |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
150 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
116 | } |
151 | } |
… | |
… | |
120 | } |
155 | } |
121 | |
156 | |
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
157 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
123 | |
158 | |
124 | #define array_needsize(base,cur,cnt,init) \ |
159 | #define array_needsize(base,cur,cnt,init) \ |
125 | if ((cnt) > cur) \ |
160 | if (expect_false ((cnt) > cur)) \ |
126 | { \ |
161 | { \ |
127 | int newcnt = cur; \ |
162 | int newcnt = cur; \ |
128 | do \ |
163 | do \ |
129 | { \ |
164 | { \ |
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
165 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
139 | /*****************************************************************************/ |
174 | /*****************************************************************************/ |
140 | |
175 | |
141 | typedef struct |
176 | typedef struct |
142 | { |
177 | { |
143 | struct ev_io *head; |
178 | struct ev_io *head; |
144 | int events; |
179 | unsigned char events; |
|
|
180 | unsigned char reify; |
145 | } ANFD; |
181 | } ANFD; |
146 | |
182 | |
147 | static ANFD *anfds; |
183 | static ANFD *anfds; |
148 | static int anfdmax; |
184 | static int anfdmax; |
149 | |
185 | |
… | |
… | |
152 | { |
188 | { |
153 | while (count--) |
189 | while (count--) |
154 | { |
190 | { |
155 | base->head = 0; |
191 | base->head = 0; |
156 | base->events = EV_NONE; |
192 | base->events = EV_NONE; |
|
|
193 | base->reify = 0; |
|
|
194 | |
157 | ++base; |
195 | ++base; |
158 | } |
196 | } |
159 | } |
197 | } |
160 | |
198 | |
161 | typedef struct |
199 | typedef struct |
162 | { |
200 | { |
163 | W w; |
201 | W w; |
164 | int events; |
202 | int events; |
165 | } ANPENDING; |
203 | } ANPENDING; |
166 | |
204 | |
167 | static ANPENDING *pendings; |
205 | static ANPENDING *pendings [NUMPRI]; |
168 | static int pendingmax, pendingcnt; |
206 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
169 | |
207 | |
170 | static void |
208 | static void |
171 | event (W w, int events) |
209 | event (W w, int events) |
172 | { |
210 | { |
|
|
211 | if (w->pending) |
|
|
212 | { |
|
|
213 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
|
|
214 | return; |
|
|
215 | } |
|
|
216 | |
173 | w->pending = ++pendingcnt; |
217 | w->pending = ++pendingcnt [ABSPRI (w)]; |
174 | array_needsize (pendings, pendingmax, pendingcnt, ); |
218 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
175 | pendings [pendingcnt - 1].w = w; |
219 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
176 | pendings [pendingcnt - 1].events = events; |
220 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
177 | } |
221 | } |
178 | |
222 | |
179 | static void |
223 | static void |
180 | queue_events (W *events, int eventcnt, int type) |
224 | queue_events (W *events, int eventcnt, int type) |
181 | { |
225 | { |
… | |
… | |
219 | int events = 0; |
263 | int events = 0; |
220 | |
264 | |
221 | for (w = anfd->head; w; w = w->next) |
265 | for (w = anfd->head; w; w = w->next) |
222 | events |= w->events; |
266 | events |= w->events; |
223 | |
267 | |
224 | anfd->events &= ~EV_REIFY; |
268 | anfd->reify = 0; |
225 | |
269 | |
226 | if (anfd->events != events) |
270 | if (anfd->events != events) |
227 | { |
271 | { |
228 | method_modify (fd, anfd->events, events); |
272 | method_modify (fd, anfd->events, events); |
229 | anfd->events = events; |
273 | anfd->events = events; |
… | |
… | |
234 | } |
278 | } |
235 | |
279 | |
236 | static void |
280 | static void |
237 | fd_change (int fd) |
281 | fd_change (int fd) |
238 | { |
282 | { |
239 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
283 | if (anfds [fd].reify || fdchangecnt < 0) |
240 | return; |
284 | return; |
241 | |
285 | |
242 | anfds [fd].events |= EV_REIFY; |
286 | anfds [fd].reify = 1; |
243 | |
287 | |
244 | ++fdchangecnt; |
288 | ++fdchangecnt; |
245 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
289 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
246 | fdchanges [fdchangecnt - 1] = fd; |
290 | fdchanges [fdchangecnt - 1] = fd; |
247 | } |
291 | } |
248 | |
292 | |
|
|
293 | static void |
|
|
294 | fd_kill (int fd) |
|
|
295 | { |
|
|
296 | struct ev_io *w; |
|
|
297 | |
|
|
298 | printf ("killing fd %d\n", fd);//D |
|
|
299 | while ((w = anfds [fd].head)) |
|
|
300 | { |
|
|
301 | ev_io_stop (w); |
|
|
302 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
303 | } |
|
|
304 | } |
|
|
305 | |
249 | /* called on EBADF to verify fds */ |
306 | /* called on EBADF to verify fds */ |
250 | static void |
307 | static void |
251 | fd_recheck (void) |
308 | fd_ebadf (void) |
252 | { |
309 | { |
253 | int fd; |
310 | int fd; |
254 | |
311 | |
255 | for (fd = 0; fd < anfdmax; ++fd) |
312 | for (fd = 0; fd < anfdmax; ++fd) |
256 | if (anfds [fd].events) |
313 | if (anfds [fd].events) |
257 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
314 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
258 | while (anfds [fd].head) |
315 | fd_kill (fd); |
|
|
316 | } |
|
|
317 | |
|
|
318 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
319 | static void |
|
|
320 | fd_enomem (void) |
|
|
321 | { |
|
|
322 | int fd = anfdmax; |
|
|
323 | |
|
|
324 | while (fd--) |
|
|
325 | if (anfds [fd].events) |
259 | { |
326 | { |
260 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
327 | close (fd); |
261 | ev_io_stop (anfds [fd].head); |
328 | fd_kill (fd); |
|
|
329 | return; |
262 | } |
330 | } |
263 | } |
331 | } |
264 | |
332 | |
265 | /*****************************************************************************/ |
333 | /*****************************************************************************/ |
266 | |
334 | |
267 | static struct ev_timer **timers; |
335 | static struct ev_timer **timers; |
… | |
… | |
314 | /*****************************************************************************/ |
382 | /*****************************************************************************/ |
315 | |
383 | |
316 | typedef struct |
384 | typedef struct |
317 | { |
385 | { |
318 | struct ev_signal *head; |
386 | struct ev_signal *head; |
319 | sig_atomic_t gotsig; |
387 | sig_atomic_t volatile gotsig; |
320 | } ANSIG; |
388 | } ANSIG; |
321 | |
389 | |
322 | static ANSIG *signals; |
390 | static ANSIG *signals; |
323 | static int signalmax; |
391 | static int signalmax; |
324 | |
392 | |
325 | static int sigpipe [2]; |
393 | static int sigpipe [2]; |
326 | static sig_atomic_t gotsig; |
394 | static sig_atomic_t volatile gotsig; |
327 | static struct ev_io sigev; |
395 | static struct ev_io sigev; |
328 | |
396 | |
329 | static void |
397 | static void |
330 | signals_init (ANSIG *base, int count) |
398 | signals_init (ANSIG *base, int count) |
331 | { |
399 | { |
332 | while (count--) |
400 | while (count--) |
333 | { |
401 | { |
334 | base->head = 0; |
402 | base->head = 0; |
335 | base->gotsig = 0; |
403 | base->gotsig = 0; |
|
|
404 | |
336 | ++base; |
405 | ++base; |
337 | } |
406 | } |
338 | } |
407 | } |
339 | |
408 | |
340 | static void |
409 | static void |
… | |
… | |
343 | signals [signum - 1].gotsig = 1; |
412 | signals [signum - 1].gotsig = 1; |
344 | |
413 | |
345 | if (!gotsig) |
414 | if (!gotsig) |
346 | { |
415 | { |
347 | gotsig = 1; |
416 | gotsig = 1; |
348 | write (sigpipe [1], &gotsig, 1); |
417 | write (sigpipe [1], &signum, 1); |
349 | } |
418 | } |
350 | } |
419 | } |
351 | |
420 | |
352 | static void |
421 | static void |
353 | sigcb (struct ev_io *iow, int revents) |
422 | sigcb (struct ev_io *iow, int revents) |
354 | { |
423 | { |
355 | struct ev_signal *w; |
424 | struct ev_signal *w; |
356 | int sig; |
425 | int signum; |
357 | |
426 | |
|
|
427 | read (sigpipe [0], &revents, 1); |
358 | gotsig = 0; |
428 | gotsig = 0; |
359 | read (sigpipe [0], &revents, 1); |
|
|
360 | |
429 | |
361 | for (sig = signalmax; sig--; ) |
430 | for (signum = signalmax; signum--; ) |
362 | if (signals [sig].gotsig) |
431 | if (signals [signum].gotsig) |
363 | { |
432 | { |
364 | signals [sig].gotsig = 0; |
433 | signals [signum].gotsig = 0; |
365 | |
434 | |
366 | for (w = signals [sig].head; w; w = w->next) |
435 | for (w = signals [signum].head; w; w = w->next) |
367 | event ((W)w, EV_SIGNAL); |
436 | event ((W)w, EV_SIGNAL); |
368 | } |
437 | } |
369 | } |
438 | } |
370 | |
439 | |
371 | static void |
440 | static void |
… | |
… | |
408 | struct ev_child *w; |
477 | struct ev_child *w; |
409 | int pid, status; |
478 | int pid, status; |
410 | |
479 | |
411 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
480 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
412 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
481 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
413 | if (w->pid == pid || w->pid == -1) |
482 | if (w->pid == pid || !w->pid) |
414 | { |
483 | { |
415 | w->status = status; |
484 | w->status = status; |
416 | event ((W)w, EV_CHILD); |
485 | event ((W)w, EV_CHILD); |
417 | } |
486 | } |
418 | } |
487 | } |
419 | |
488 | |
420 | /*****************************************************************************/ |
489 | /*****************************************************************************/ |
421 | |
490 | |
|
|
491 | #if EV_USE_KQUEUE |
|
|
492 | # include "ev_kqueue.c" |
|
|
493 | #endif |
422 | #if EV_USE_EPOLL |
494 | #if EV_USE_EPOLL |
423 | # include "ev_epoll.c" |
495 | # include "ev_epoll.c" |
424 | #endif |
496 | #endif |
|
|
497 | #if EV_USE_POLL |
|
|
498 | # include "ev_poll.c" |
|
|
499 | #endif |
425 | #if EV_USE_SELECT |
500 | #if EV_USE_SELECT |
426 | # include "ev_select.c" |
501 | # include "ev_select.c" |
427 | #endif |
502 | #endif |
428 | |
503 | |
429 | int |
504 | int |
… | |
… | |
436 | ev_version_minor (void) |
511 | ev_version_minor (void) |
437 | { |
512 | { |
438 | return EV_VERSION_MINOR; |
513 | return EV_VERSION_MINOR; |
439 | } |
514 | } |
440 | |
515 | |
|
|
516 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
517 | static int |
|
|
518 | enable_secure () |
|
|
519 | { |
|
|
520 | return getuid () != geteuid () |
|
|
521 | || getgid () != getegid (); |
|
|
522 | } |
|
|
523 | |
441 | int ev_init (int flags) |
524 | int ev_init (int methods) |
442 | { |
525 | { |
443 | if (!ev_method) |
526 | if (!ev_method) |
444 | { |
527 | { |
445 | #if EV_USE_MONOTONIC |
528 | #if EV_USE_MONOTONIC |
446 | { |
529 | { |
… | |
… | |
448 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
531 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
449 | have_monotonic = 1; |
532 | have_monotonic = 1; |
450 | } |
533 | } |
451 | #endif |
534 | #endif |
452 | |
535 | |
453 | ev_now = ev_time (); |
536 | ev_now = ev_time (); |
454 | now = get_clock (); |
537 | now = get_clock (); |
|
|
538 | now_floor = now; |
455 | diff = ev_now - now; |
539 | diff = ev_now - now; |
456 | |
540 | |
457 | if (pipe (sigpipe)) |
541 | if (pipe (sigpipe)) |
458 | return 0; |
542 | return 0; |
459 | |
543 | |
|
|
544 | if (methods == EVMETHOD_AUTO) |
|
|
545 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
546 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
547 | else |
460 | ev_method = EVMETHOD_NONE; |
548 | methods = EVMETHOD_ANY; |
|
|
549 | |
|
|
550 | ev_method = 0; |
|
|
551 | #if EV_USE_KQUEUE |
|
|
552 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
553 | #endif |
461 | #if EV_USE_EPOLL |
554 | #if EV_USE_EPOLL |
462 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
555 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
556 | #endif |
|
|
557 | #if EV_USE_POLL |
|
|
558 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
463 | #endif |
559 | #endif |
464 | #if EV_USE_SELECT |
560 | #if EV_USE_SELECT |
465 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
561 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
466 | #endif |
562 | #endif |
467 | |
563 | |
468 | if (ev_method) |
564 | if (ev_method) |
469 | { |
565 | { |
470 | ev_watcher_init (&sigev, sigcb); |
566 | ev_watcher_init (&sigev, sigcb); |
… | |
… | |
479 | } |
575 | } |
480 | |
576 | |
481 | /*****************************************************************************/ |
577 | /*****************************************************************************/ |
482 | |
578 | |
483 | void |
579 | void |
484 | ev_prefork (void) |
580 | ev_fork_prepare (void) |
485 | { |
581 | { |
486 | /* nop */ |
582 | /* nop */ |
487 | } |
583 | } |
488 | |
584 | |
489 | void |
585 | void |
490 | ev_postfork_parent (void) |
586 | ev_fork_parent (void) |
491 | { |
587 | { |
492 | /* nop */ |
588 | /* nop */ |
493 | } |
589 | } |
494 | |
590 | |
495 | void |
591 | void |
496 | ev_postfork_child (void) |
592 | ev_fork_child (void) |
497 | { |
593 | { |
498 | #if EV_USE_EPOLL |
594 | #if EV_USE_EPOLL |
499 | if (ev_method == EVMETHOD_EPOLL) |
595 | if (ev_method == EVMETHOD_EPOLL) |
500 | epoll_postfork_child (); |
596 | epoll_postfork_child (); |
501 | #endif |
597 | #endif |
… | |
… | |
510 | /*****************************************************************************/ |
606 | /*****************************************************************************/ |
511 | |
607 | |
512 | static void |
608 | static void |
513 | call_pending (void) |
609 | call_pending (void) |
514 | { |
610 | { |
|
|
611 | int pri; |
|
|
612 | |
|
|
613 | for (pri = NUMPRI; pri--; ) |
515 | while (pendingcnt) |
614 | while (pendingcnt [pri]) |
516 | { |
615 | { |
517 | ANPENDING *p = pendings + --pendingcnt; |
616 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
518 | |
617 | |
519 | if (p->w) |
618 | if (p->w) |
520 | { |
619 | { |
521 | p->w->pending = 0; |
620 | p->w->pending = 0; |
522 | p->w->cb (p->w, p->events); |
621 | p->w->cb (p->w, p->events); |
523 | } |
622 | } |
524 | } |
623 | } |
525 | } |
624 | } |
526 | |
625 | |
527 | static void |
626 | static void |
528 | timers_reify (void) |
627 | timers_reify (void) |
529 | { |
628 | { |
… | |
… | |
532 | struct ev_timer *w = timers [0]; |
631 | struct ev_timer *w = timers [0]; |
533 | |
632 | |
534 | /* first reschedule or stop timer */ |
633 | /* first reschedule or stop timer */ |
535 | if (w->repeat) |
634 | if (w->repeat) |
536 | { |
635 | { |
|
|
636 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
537 | w->at = now + w->repeat; |
637 | w->at = now + w->repeat; |
538 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
539 | downheap ((WT *)timers, timercnt, 0); |
638 | downheap ((WT *)timers, timercnt, 0); |
540 | } |
639 | } |
541 | else |
640 | else |
542 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
641 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
543 | |
642 | |
… | |
… | |
554 | |
653 | |
555 | /* first reschedule or stop timer */ |
654 | /* first reschedule or stop timer */ |
556 | if (w->interval) |
655 | if (w->interval) |
557 | { |
656 | { |
558 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
657 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
559 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
658 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
560 | downheap ((WT *)periodics, periodiccnt, 0); |
659 | downheap ((WT *)periodics, periodiccnt, 0); |
561 | } |
660 | } |
562 | else |
661 | else |
563 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
662 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
564 | |
663 | |
565 | event ((W)w, EV_TIMEOUT); |
664 | event ((W)w, EV_PERIODIC); |
566 | } |
665 | } |
567 | } |
666 | } |
568 | |
667 | |
569 | static void |
668 | static void |
570 | periodics_reschedule (ev_tstamp diff) |
669 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
589 | } |
688 | } |
590 | } |
689 | } |
591 | } |
690 | } |
592 | } |
691 | } |
593 | |
692 | |
|
|
693 | static int |
|
|
694 | time_update_monotonic (void) |
|
|
695 | { |
|
|
696 | now = get_clock (); |
|
|
697 | |
|
|
698 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
699 | { |
|
|
700 | ev_now = now + diff; |
|
|
701 | return 0; |
|
|
702 | } |
|
|
703 | else |
|
|
704 | { |
|
|
705 | now_floor = now; |
|
|
706 | ev_now = ev_time (); |
|
|
707 | return 1; |
|
|
708 | } |
|
|
709 | } |
|
|
710 | |
594 | static void |
711 | static void |
595 | time_update (void) |
712 | time_update (void) |
596 | { |
713 | { |
597 | int i; |
714 | int i; |
598 | |
715 | |
599 | ev_now = ev_time (); |
716 | #if EV_USE_MONOTONIC |
600 | |
|
|
601 | if (have_monotonic) |
717 | if (expect_true (have_monotonic)) |
602 | { |
718 | { |
|
|
719 | if (time_update_monotonic ()) |
|
|
720 | { |
603 | ev_tstamp odiff = diff; |
721 | ev_tstamp odiff = diff; |
604 | |
722 | |
605 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
723 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
606 | { |
724 | { |
607 | now = get_clock (); |
|
|
608 | diff = ev_now - now; |
725 | diff = ev_now - now; |
609 | |
726 | |
610 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
727 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
611 | return; /* all is well */ |
728 | return; /* all is well */ |
612 | |
729 | |
613 | ev_now = ev_time (); |
730 | ev_now = ev_time (); |
|
|
731 | now = get_clock (); |
|
|
732 | now_floor = now; |
614 | } |
733 | } |
615 | |
734 | |
616 | periodics_reschedule (diff - odiff); |
735 | periodics_reschedule (diff - odiff); |
617 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
736 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
737 | } |
618 | } |
738 | } |
619 | else |
739 | else |
|
|
740 | #endif |
620 | { |
741 | { |
|
|
742 | ev_now = ev_time (); |
|
|
743 | |
621 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
744 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
622 | { |
745 | { |
623 | periodics_reschedule (ev_now - now); |
746 | periodics_reschedule (ev_now - now); |
624 | |
747 | |
625 | /* adjust timers. this is easy, as the offset is the same for all */ |
748 | /* adjust timers. this is easy, as the offset is the same for all */ |
626 | for (i = 0; i < timercnt; ++i) |
749 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
639 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
762 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
640 | |
763 | |
641 | do |
764 | do |
642 | { |
765 | { |
643 | /* queue check watchers (and execute them) */ |
766 | /* queue check watchers (and execute them) */ |
644 | if (preparecnt) |
767 | if (expect_false (preparecnt)) |
645 | { |
768 | { |
646 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
769 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
647 | call_pending (); |
770 | call_pending (); |
648 | } |
771 | } |
649 | |
772 | |
… | |
… | |
652 | |
775 | |
653 | /* calculate blocking time */ |
776 | /* calculate blocking time */ |
654 | |
777 | |
655 | /* we only need this for !monotonic clockor timers, but as we basically |
778 | /* we only need this for !monotonic clockor timers, but as we basically |
656 | always have timers, we just calculate it always */ |
779 | always have timers, we just calculate it always */ |
|
|
780 | #if EV_USE_MONOTONIC |
|
|
781 | if (expect_true (have_monotonic)) |
|
|
782 | time_update_monotonic (); |
|
|
783 | else |
|
|
784 | #endif |
|
|
785 | { |
657 | ev_now = ev_time (); |
786 | ev_now = ev_time (); |
|
|
787 | now = ev_now; |
|
|
788 | } |
658 | |
789 | |
659 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
790 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
660 | block = 0.; |
791 | block = 0.; |
661 | else |
792 | else |
662 | { |
793 | { |
663 | block = MAX_BLOCKTIME; |
794 | block = MAX_BLOCKTIME; |
664 | |
795 | |
665 | if (timercnt) |
796 | if (timercnt) |
666 | { |
797 | { |
667 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
798 | ev_tstamp to = timers [0]->at - now + method_fudge; |
668 | if (block > to) block = to; |
799 | if (block > to) block = to; |
669 | } |
800 | } |
670 | |
801 | |
671 | if (periodiccnt) |
802 | if (periodiccnt) |
672 | { |
803 | { |
… | |
… | |
725 | head = &(*head)->next; |
856 | head = &(*head)->next; |
726 | } |
857 | } |
727 | } |
858 | } |
728 | |
859 | |
729 | static void |
860 | static void |
730 | ev_clear (W w) |
861 | ev_clear_pending (W w) |
731 | { |
862 | { |
732 | if (w->pending) |
863 | if (w->pending) |
733 | { |
864 | { |
734 | pendings [w->pending - 1].w = 0; |
865 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
735 | w->pending = 0; |
866 | w->pending = 0; |
736 | } |
867 | } |
737 | } |
868 | } |
738 | |
869 | |
739 | static void |
870 | static void |
740 | ev_start (W w, int active) |
871 | ev_start (W w, int active) |
741 | { |
872 | { |
|
|
873 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
874 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
875 | |
742 | w->active = active; |
876 | w->active = active; |
743 | } |
877 | } |
744 | |
878 | |
745 | static void |
879 | static void |
746 | ev_stop (W w) |
880 | ev_stop (W w) |
… | |
… | |
751 | /*****************************************************************************/ |
885 | /*****************************************************************************/ |
752 | |
886 | |
753 | void |
887 | void |
754 | ev_io_start (struct ev_io *w) |
888 | ev_io_start (struct ev_io *w) |
755 | { |
889 | { |
|
|
890 | int fd = w->fd; |
|
|
891 | |
756 | if (ev_is_active (w)) |
892 | if (ev_is_active (w)) |
757 | return; |
893 | return; |
758 | |
894 | |
759 | int fd = w->fd; |
895 | assert (("ev_io_start called with negative fd", fd >= 0)); |
760 | |
896 | |
761 | ev_start ((W)w, 1); |
897 | ev_start ((W)w, 1); |
762 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
898 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
763 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
899 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
764 | |
900 | |
… | |
… | |
766 | } |
902 | } |
767 | |
903 | |
768 | void |
904 | void |
769 | ev_io_stop (struct ev_io *w) |
905 | ev_io_stop (struct ev_io *w) |
770 | { |
906 | { |
771 | ev_clear ((W)w); |
907 | ev_clear_pending ((W)w); |
772 | if (!ev_is_active (w)) |
908 | if (!ev_is_active (w)) |
773 | return; |
909 | return; |
774 | |
910 | |
775 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
911 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
776 | ev_stop ((W)w); |
912 | ev_stop ((W)w); |
… | |
… | |
784 | if (ev_is_active (w)) |
920 | if (ev_is_active (w)) |
785 | return; |
921 | return; |
786 | |
922 | |
787 | w->at += now; |
923 | w->at += now; |
788 | |
924 | |
789 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
925 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
790 | |
926 | |
791 | ev_start ((W)w, ++timercnt); |
927 | ev_start ((W)w, ++timercnt); |
792 | array_needsize (timers, timermax, timercnt, ); |
928 | array_needsize (timers, timermax, timercnt, ); |
793 | timers [timercnt - 1] = w; |
929 | timers [timercnt - 1] = w; |
794 | upheap ((WT *)timers, timercnt - 1); |
930 | upheap ((WT *)timers, timercnt - 1); |
795 | } |
931 | } |
796 | |
932 | |
797 | void |
933 | void |
798 | ev_timer_stop (struct ev_timer *w) |
934 | ev_timer_stop (struct ev_timer *w) |
799 | { |
935 | { |
800 | ev_clear ((W)w); |
936 | ev_clear_pending ((W)w); |
801 | if (!ev_is_active (w)) |
937 | if (!ev_is_active (w)) |
802 | return; |
938 | return; |
803 | |
939 | |
804 | if (w->active < timercnt--) |
940 | if (w->active < timercnt--) |
805 | { |
941 | { |
… | |
… | |
833 | ev_periodic_start (struct ev_periodic *w) |
969 | ev_periodic_start (struct ev_periodic *w) |
834 | { |
970 | { |
835 | if (ev_is_active (w)) |
971 | if (ev_is_active (w)) |
836 | return; |
972 | return; |
837 | |
973 | |
838 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
974 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
839 | |
975 | |
840 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
976 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
841 | if (w->interval) |
977 | if (w->interval) |
842 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
978 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
843 | |
979 | |
… | |
… | |
848 | } |
984 | } |
849 | |
985 | |
850 | void |
986 | void |
851 | ev_periodic_stop (struct ev_periodic *w) |
987 | ev_periodic_stop (struct ev_periodic *w) |
852 | { |
988 | { |
853 | ev_clear ((W)w); |
989 | ev_clear_pending ((W)w); |
854 | if (!ev_is_active (w)) |
990 | if (!ev_is_active (w)) |
855 | return; |
991 | return; |
856 | |
992 | |
857 | if (w->active < periodiccnt--) |
993 | if (w->active < periodiccnt--) |
858 | { |
994 | { |
… | |
… | |
866 | void |
1002 | void |
867 | ev_signal_start (struct ev_signal *w) |
1003 | ev_signal_start (struct ev_signal *w) |
868 | { |
1004 | { |
869 | if (ev_is_active (w)) |
1005 | if (ev_is_active (w)) |
870 | return; |
1006 | return; |
|
|
1007 | |
|
|
1008 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
871 | |
1009 | |
872 | ev_start ((W)w, 1); |
1010 | ev_start ((W)w, 1); |
873 | array_needsize (signals, signalmax, w->signum, signals_init); |
1011 | array_needsize (signals, signalmax, w->signum, signals_init); |
874 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1012 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
875 | |
1013 | |
… | |
… | |
884 | } |
1022 | } |
885 | |
1023 | |
886 | void |
1024 | void |
887 | ev_signal_stop (struct ev_signal *w) |
1025 | ev_signal_stop (struct ev_signal *w) |
888 | { |
1026 | { |
889 | ev_clear ((W)w); |
1027 | ev_clear_pending ((W)w); |
890 | if (!ev_is_active (w)) |
1028 | if (!ev_is_active (w)) |
891 | return; |
1029 | return; |
892 | |
1030 | |
893 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1031 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
894 | ev_stop ((W)w); |
1032 | ev_stop ((W)w); |
… | |
… | |
909 | } |
1047 | } |
910 | |
1048 | |
911 | void |
1049 | void |
912 | ev_idle_stop (struct ev_idle *w) |
1050 | ev_idle_stop (struct ev_idle *w) |
913 | { |
1051 | { |
914 | ev_clear ((W)w); |
1052 | ev_clear_pending ((W)w); |
915 | if (ev_is_active (w)) |
1053 | if (ev_is_active (w)) |
916 | return; |
1054 | return; |
917 | |
1055 | |
918 | idles [w->active - 1] = idles [--idlecnt]; |
1056 | idles [w->active - 1] = idles [--idlecnt]; |
919 | ev_stop ((W)w); |
1057 | ev_stop ((W)w); |
… | |
… | |
931 | } |
1069 | } |
932 | |
1070 | |
933 | void |
1071 | void |
934 | ev_prepare_stop (struct ev_prepare *w) |
1072 | ev_prepare_stop (struct ev_prepare *w) |
935 | { |
1073 | { |
936 | ev_clear ((W)w); |
1074 | ev_clear_pending ((W)w); |
937 | if (ev_is_active (w)) |
1075 | if (ev_is_active (w)) |
938 | return; |
1076 | return; |
939 | |
1077 | |
940 | prepares [w->active - 1] = prepares [--preparecnt]; |
1078 | prepares [w->active - 1] = prepares [--preparecnt]; |
941 | ev_stop ((W)w); |
1079 | ev_stop ((W)w); |
… | |
… | |
953 | } |
1091 | } |
954 | |
1092 | |
955 | void |
1093 | void |
956 | ev_check_stop (struct ev_check *w) |
1094 | ev_check_stop (struct ev_check *w) |
957 | { |
1095 | { |
958 | ev_clear ((W)w); |
1096 | ev_clear_pending ((W)w); |
959 | if (ev_is_active (w)) |
1097 | if (ev_is_active (w)) |
960 | return; |
1098 | return; |
961 | |
1099 | |
962 | checks [w->active - 1] = checks [--checkcnt]; |
1100 | checks [w->active - 1] = checks [--checkcnt]; |
963 | ev_stop ((W)w); |
1101 | ev_stop ((W)w); |
… | |
… | |
974 | } |
1112 | } |
975 | |
1113 | |
976 | void |
1114 | void |
977 | ev_child_stop (struct ev_child *w) |
1115 | ev_child_stop (struct ev_child *w) |
978 | { |
1116 | { |
979 | ev_clear ((W)w); |
1117 | ev_clear_pending ((W)w); |
980 | if (ev_is_active (w)) |
1118 | if (ev_is_active (w)) |
981 | return; |
1119 | return; |
982 | |
1120 | |
983 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1121 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
984 | ev_stop ((W)w); |
1122 | ev_stop ((W)w); |