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 | { |
173 | if (w->pending) |
211 | if (w->pending) |
174 | { |
212 | { |
175 | pendings [w->pending - 1].events |= events; |
213 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
176 | return; |
214 | return; |
177 | } |
215 | } |
178 | |
216 | |
179 | w->pending = ++pendingcnt; |
217 | w->pending = ++pendingcnt [ABSPRI (w)]; |
180 | array_needsize (pendings, pendingmax, pendingcnt, ); |
218 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
181 | pendings [pendingcnt - 1].w = w; |
219 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
182 | pendings [pendingcnt - 1].events = events; |
220 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
183 | } |
221 | } |
184 | |
222 | |
185 | static void |
223 | static void |
186 | queue_events (W *events, int eventcnt, int type) |
224 | queue_events (W *events, int eventcnt, int type) |
187 | { |
225 | { |
… | |
… | |
225 | int events = 0; |
263 | int events = 0; |
226 | |
264 | |
227 | for (w = anfd->head; w; w = w->next) |
265 | for (w = anfd->head; w; w = w->next) |
228 | events |= w->events; |
266 | events |= w->events; |
229 | |
267 | |
230 | anfd->events &= ~EV_REIFY; |
268 | anfd->reify = 0; |
231 | |
269 | |
232 | if (anfd->events != events) |
270 | if (anfd->events != events) |
233 | { |
271 | { |
234 | method_modify (fd, anfd->events, events); |
272 | method_modify (fd, anfd->events, events); |
235 | anfd->events = events; |
273 | anfd->events = events; |
… | |
… | |
240 | } |
278 | } |
241 | |
279 | |
242 | static void |
280 | static void |
243 | fd_change (int fd) |
281 | fd_change (int fd) |
244 | { |
282 | { |
245 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
283 | if (anfds [fd].reify || fdchangecnt < 0) |
246 | return; |
284 | return; |
247 | |
285 | |
248 | anfds [fd].events |= EV_REIFY; |
286 | anfds [fd].reify = 1; |
249 | |
287 | |
250 | ++fdchangecnt; |
288 | ++fdchangecnt; |
251 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
289 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
252 | fdchanges [fdchangecnt - 1] = fd; |
290 | fdchanges [fdchangecnt - 1] = fd; |
253 | } |
291 | } |
254 | |
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 | |
255 | /* called on EBADF to verify fds */ |
306 | /* called on EBADF to verify fds */ |
256 | static void |
307 | static void |
257 | fd_recheck (void) |
308 | fd_ebadf (void) |
258 | { |
309 | { |
259 | int fd; |
310 | int fd; |
260 | |
311 | |
261 | for (fd = 0; fd < anfdmax; ++fd) |
312 | for (fd = 0; fd < anfdmax; ++fd) |
262 | if (anfds [fd].events) |
313 | if (anfds [fd].events) |
263 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
314 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
264 | 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) |
265 | { |
326 | { |
266 | ev_io_stop (anfds [fd].head); |
327 | close (fd); |
267 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
328 | fd_kill (fd); |
|
|
329 | return; |
268 | } |
330 | } |
269 | } |
331 | } |
270 | |
332 | |
271 | /*****************************************************************************/ |
333 | /*****************************************************************************/ |
272 | |
334 | |
273 | static struct ev_timer **timers; |
335 | static struct ev_timer **timers; |
… | |
… | |
320 | /*****************************************************************************/ |
382 | /*****************************************************************************/ |
321 | |
383 | |
322 | typedef struct |
384 | typedef struct |
323 | { |
385 | { |
324 | struct ev_signal *head; |
386 | struct ev_signal *head; |
325 | sig_atomic_t gotsig; |
387 | sig_atomic_t volatile gotsig; |
326 | } ANSIG; |
388 | } ANSIG; |
327 | |
389 | |
328 | static ANSIG *signals; |
390 | static ANSIG *signals; |
329 | static int signalmax; |
391 | static int signalmax; |
330 | |
392 | |
331 | static int sigpipe [2]; |
393 | static int sigpipe [2]; |
332 | static sig_atomic_t gotsig; |
394 | static sig_atomic_t volatile gotsig; |
333 | static struct ev_io sigev; |
395 | static struct ev_io sigev; |
334 | |
396 | |
335 | static void |
397 | static void |
336 | signals_init (ANSIG *base, int count) |
398 | signals_init (ANSIG *base, int count) |
337 | { |
399 | { |
338 | while (count--) |
400 | while (count--) |
339 | { |
401 | { |
340 | base->head = 0; |
402 | base->head = 0; |
341 | base->gotsig = 0; |
403 | base->gotsig = 0; |
|
|
404 | |
342 | ++base; |
405 | ++base; |
343 | } |
406 | } |
344 | } |
407 | } |
345 | |
408 | |
346 | static void |
409 | static void |
… | |
… | |
349 | signals [signum - 1].gotsig = 1; |
412 | signals [signum - 1].gotsig = 1; |
350 | |
413 | |
351 | if (!gotsig) |
414 | if (!gotsig) |
352 | { |
415 | { |
353 | gotsig = 1; |
416 | gotsig = 1; |
354 | write (sigpipe [1], &gotsig, 1); |
417 | write (sigpipe [1], &signum, 1); |
355 | } |
418 | } |
356 | } |
419 | } |
357 | |
420 | |
358 | static void |
421 | static void |
359 | sigcb (struct ev_io *iow, int revents) |
422 | sigcb (struct ev_io *iow, int revents) |
360 | { |
423 | { |
361 | struct ev_signal *w; |
424 | struct ev_signal *w; |
362 | int sig; |
425 | int signum; |
363 | |
426 | |
|
|
427 | read (sigpipe [0], &revents, 1); |
364 | gotsig = 0; |
428 | gotsig = 0; |
365 | read (sigpipe [0], &revents, 1); |
|
|
366 | |
429 | |
367 | for (sig = signalmax; sig--; ) |
430 | for (signum = signalmax; signum--; ) |
368 | if (signals [sig].gotsig) |
431 | if (signals [signum].gotsig) |
369 | { |
432 | { |
370 | signals [sig].gotsig = 0; |
433 | signals [signum].gotsig = 0; |
371 | |
434 | |
372 | for (w = signals [sig].head; w; w = w->next) |
435 | for (w = signals [signum].head; w; w = w->next) |
373 | event ((W)w, EV_SIGNAL); |
436 | event ((W)w, EV_SIGNAL); |
374 | } |
437 | } |
375 | } |
438 | } |
376 | |
439 | |
377 | static void |
440 | static void |
… | |
… | |
414 | struct ev_child *w; |
477 | struct ev_child *w; |
415 | int pid, status; |
478 | int pid, status; |
416 | |
479 | |
417 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
480 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
418 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
481 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
419 | if (w->pid == pid || w->pid == -1) |
482 | if (w->pid == pid || !w->pid) |
420 | { |
483 | { |
421 | w->status = status; |
484 | w->status = status; |
422 | event ((W)w, EV_CHILD); |
485 | event ((W)w, EV_CHILD); |
423 | } |
486 | } |
424 | } |
487 | } |
425 | |
488 | |
426 | /*****************************************************************************/ |
489 | /*****************************************************************************/ |
427 | |
490 | |
|
|
491 | #if EV_USE_KQUEUE |
|
|
492 | # include "ev_kqueue.c" |
|
|
493 | #endif |
428 | #if EV_USE_EPOLL |
494 | #if EV_USE_EPOLL |
429 | # include "ev_epoll.c" |
495 | # include "ev_epoll.c" |
430 | #endif |
496 | #endif |
|
|
497 | #if EV_USE_POLL |
|
|
498 | # include "ev_poll.c" |
|
|
499 | #endif |
431 | #if EV_USE_SELECT |
500 | #if EV_USE_SELECT |
432 | # include "ev_select.c" |
501 | # include "ev_select.c" |
433 | #endif |
502 | #endif |
434 | |
503 | |
435 | int |
504 | int |
… | |
… | |
442 | ev_version_minor (void) |
511 | ev_version_minor (void) |
443 | { |
512 | { |
444 | return EV_VERSION_MINOR; |
513 | return EV_VERSION_MINOR; |
445 | } |
514 | } |
446 | |
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 | |
447 | int ev_init (int flags) |
524 | int ev_init (int methods) |
448 | { |
525 | { |
449 | if (!ev_method) |
526 | if (!ev_method) |
450 | { |
527 | { |
451 | #if EV_USE_MONOTONIC |
528 | #if EV_USE_MONOTONIC |
452 | { |
529 | { |
… | |
… | |
454 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
531 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
455 | have_monotonic = 1; |
532 | have_monotonic = 1; |
456 | } |
533 | } |
457 | #endif |
534 | #endif |
458 | |
535 | |
459 | ev_now = ev_time (); |
536 | ev_now = ev_time (); |
460 | now = get_clock (); |
537 | now = get_clock (); |
|
|
538 | now_floor = now; |
461 | diff = ev_now - now; |
539 | diff = ev_now - now; |
462 | |
540 | |
463 | if (pipe (sigpipe)) |
541 | if (pipe (sigpipe)) |
464 | return 0; |
542 | return 0; |
465 | |
543 | |
|
|
544 | if (methods == EVMETHOD_AUTO) |
|
|
545 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
546 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
547 | else |
466 | 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 |
467 | #if EV_USE_EPOLL |
554 | #if EV_USE_EPOLL |
468 | 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); |
469 | #endif |
559 | #endif |
470 | #if EV_USE_SELECT |
560 | #if EV_USE_SELECT |
471 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
561 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
472 | #endif |
562 | #endif |
473 | |
563 | |
474 | if (ev_method) |
564 | if (ev_method) |
475 | { |
565 | { |
476 | ev_watcher_init (&sigev, sigcb); |
566 | ev_watcher_init (&sigev, sigcb); |
… | |
… | |
485 | } |
575 | } |
486 | |
576 | |
487 | /*****************************************************************************/ |
577 | /*****************************************************************************/ |
488 | |
578 | |
489 | void |
579 | void |
490 | ev_prefork (void) |
580 | ev_fork_prepare (void) |
491 | { |
581 | { |
492 | /* nop */ |
582 | /* nop */ |
493 | } |
583 | } |
494 | |
584 | |
495 | void |
585 | void |
496 | ev_postfork_parent (void) |
586 | ev_fork_parent (void) |
497 | { |
587 | { |
498 | /* nop */ |
588 | /* nop */ |
499 | } |
589 | } |
500 | |
590 | |
501 | void |
591 | void |
502 | ev_postfork_child (void) |
592 | ev_fork_child (void) |
503 | { |
593 | { |
504 | #if EV_USE_EPOLL |
594 | #if EV_USE_EPOLL |
505 | if (ev_method == EVMETHOD_EPOLL) |
595 | if (ev_method == EVMETHOD_EPOLL) |
506 | epoll_postfork_child (); |
596 | epoll_postfork_child (); |
507 | #endif |
597 | #endif |
… | |
… | |
516 | /*****************************************************************************/ |
606 | /*****************************************************************************/ |
517 | |
607 | |
518 | static void |
608 | static void |
519 | call_pending (void) |
609 | call_pending (void) |
520 | { |
610 | { |
|
|
611 | int pri; |
|
|
612 | |
|
|
613 | for (pri = NUMPRI; pri--; ) |
521 | while (pendingcnt) |
614 | while (pendingcnt [pri]) |
522 | { |
615 | { |
523 | ANPENDING *p = pendings + --pendingcnt; |
616 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
524 | |
617 | |
525 | if (p->w) |
618 | if (p->w) |
526 | { |
619 | { |
527 | p->w->pending = 0; |
620 | p->w->pending = 0; |
528 | p->w->cb (p->w, p->events); |
621 | p->w->cb (p->w, p->events); |
529 | } |
622 | } |
530 | } |
623 | } |
531 | } |
624 | } |
532 | |
625 | |
533 | static void |
626 | static void |
534 | timers_reify (void) |
627 | timers_reify (void) |
535 | { |
628 | { |
… | |
… | |
538 | struct ev_timer *w = timers [0]; |
631 | struct ev_timer *w = timers [0]; |
539 | |
632 | |
540 | /* first reschedule or stop timer */ |
633 | /* first reschedule or stop timer */ |
541 | if (w->repeat) |
634 | if (w->repeat) |
542 | { |
635 | { |
|
|
636 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
543 | w->at = now + w->repeat; |
637 | w->at = now + w->repeat; |
544 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
545 | downheap ((WT *)timers, timercnt, 0); |
638 | downheap ((WT *)timers, timercnt, 0); |
546 | } |
639 | } |
547 | else |
640 | else |
548 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
641 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
549 | |
642 | |
… | |
… | |
560 | |
653 | |
561 | /* first reschedule or stop timer */ |
654 | /* first reschedule or stop timer */ |
562 | if (w->interval) |
655 | if (w->interval) |
563 | { |
656 | { |
564 | 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; |
565 | 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)); |
566 | downheap ((WT *)periodics, periodiccnt, 0); |
659 | downheap ((WT *)periodics, periodiccnt, 0); |
567 | } |
660 | } |
568 | else |
661 | else |
569 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
662 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
570 | |
663 | |
571 | event ((W)w, EV_TIMEOUT); |
664 | event ((W)w, EV_PERIODIC); |
572 | } |
665 | } |
573 | } |
666 | } |
574 | |
667 | |
575 | static void |
668 | static void |
576 | periodics_reschedule (ev_tstamp diff) |
669 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
595 | } |
688 | } |
596 | } |
689 | } |
597 | } |
690 | } |
598 | } |
691 | } |
599 | |
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 | |
600 | static void |
711 | static void |
601 | time_update (void) |
712 | time_update (void) |
602 | { |
713 | { |
603 | int i; |
714 | int i; |
604 | |
715 | |
605 | ev_now = ev_time (); |
716 | #if EV_USE_MONOTONIC |
606 | |
|
|
607 | if (have_monotonic) |
717 | if (expect_true (have_monotonic)) |
608 | { |
718 | { |
|
|
719 | if (time_update_monotonic ()) |
|
|
720 | { |
609 | ev_tstamp odiff = diff; |
721 | ev_tstamp odiff = diff; |
610 | |
722 | |
611 | 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 */ |
612 | { |
724 | { |
613 | now = get_clock (); |
|
|
614 | diff = ev_now - now; |
725 | diff = ev_now - now; |
615 | |
726 | |
616 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
727 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
617 | return; /* all is well */ |
728 | return; /* all is well */ |
618 | |
729 | |
619 | ev_now = ev_time (); |
730 | ev_now = ev_time (); |
|
|
731 | now = get_clock (); |
|
|
732 | now_floor = now; |
620 | } |
733 | } |
621 | |
734 | |
622 | periodics_reschedule (diff - odiff); |
735 | periodics_reschedule (diff - odiff); |
623 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
736 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
737 | } |
624 | } |
738 | } |
625 | else |
739 | else |
|
|
740 | #endif |
626 | { |
741 | { |
|
|
742 | ev_now = ev_time (); |
|
|
743 | |
627 | 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)) |
628 | { |
745 | { |
629 | periodics_reschedule (ev_now - now); |
746 | periodics_reschedule (ev_now - now); |
630 | |
747 | |
631 | /* 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 */ |
632 | for (i = 0; i < timercnt; ++i) |
749 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
645 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
762 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
646 | |
763 | |
647 | do |
764 | do |
648 | { |
765 | { |
649 | /* queue check watchers (and execute them) */ |
766 | /* queue check watchers (and execute them) */ |
650 | if (preparecnt) |
767 | if (expect_false (preparecnt)) |
651 | { |
768 | { |
652 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
769 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
653 | call_pending (); |
770 | call_pending (); |
654 | } |
771 | } |
655 | |
772 | |
… | |
… | |
658 | |
775 | |
659 | /* calculate blocking time */ |
776 | /* calculate blocking time */ |
660 | |
777 | |
661 | /* 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 |
662 | 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 | { |
663 | ev_now = ev_time (); |
786 | ev_now = ev_time (); |
|
|
787 | now = ev_now; |
|
|
788 | } |
664 | |
789 | |
665 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
790 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
666 | block = 0.; |
791 | block = 0.; |
667 | else |
792 | else |
668 | { |
793 | { |
669 | block = MAX_BLOCKTIME; |
794 | block = MAX_BLOCKTIME; |
670 | |
795 | |
671 | if (timercnt) |
796 | if (timercnt) |
672 | { |
797 | { |
673 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
798 | ev_tstamp to = timers [0]->at - now + method_fudge; |
674 | if (block > to) block = to; |
799 | if (block > to) block = to; |
675 | } |
800 | } |
676 | |
801 | |
677 | if (periodiccnt) |
802 | if (periodiccnt) |
678 | { |
803 | { |
… | |
… | |
731 | head = &(*head)->next; |
856 | head = &(*head)->next; |
732 | } |
857 | } |
733 | } |
858 | } |
734 | |
859 | |
735 | static void |
860 | static void |
736 | ev_clear (W w) |
861 | ev_clear_pending (W w) |
737 | { |
862 | { |
738 | if (w->pending) |
863 | if (w->pending) |
739 | { |
864 | { |
740 | pendings [w->pending - 1].w = 0; |
865 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
741 | w->pending = 0; |
866 | w->pending = 0; |
742 | } |
867 | } |
743 | } |
868 | } |
744 | |
869 | |
745 | static void |
870 | static void |
746 | ev_start (W w, int active) |
871 | ev_start (W w, int active) |
747 | { |
872 | { |
|
|
873 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
874 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
875 | |
748 | w->active = active; |
876 | w->active = active; |
749 | } |
877 | } |
750 | |
878 | |
751 | static void |
879 | static void |
752 | ev_stop (W w) |
880 | ev_stop (W w) |
… | |
… | |
757 | /*****************************************************************************/ |
885 | /*****************************************************************************/ |
758 | |
886 | |
759 | void |
887 | void |
760 | ev_io_start (struct ev_io *w) |
888 | ev_io_start (struct ev_io *w) |
761 | { |
889 | { |
|
|
890 | int fd = w->fd; |
|
|
891 | |
762 | if (ev_is_active (w)) |
892 | if (ev_is_active (w)) |
763 | return; |
893 | return; |
764 | |
894 | |
765 | int fd = w->fd; |
895 | assert (("ev_io_start called with negative fd", fd >= 0)); |
766 | |
896 | |
767 | ev_start ((W)w, 1); |
897 | ev_start ((W)w, 1); |
768 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
898 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
769 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
899 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
770 | |
900 | |
… | |
… | |
772 | } |
902 | } |
773 | |
903 | |
774 | void |
904 | void |
775 | ev_io_stop (struct ev_io *w) |
905 | ev_io_stop (struct ev_io *w) |
776 | { |
906 | { |
777 | ev_clear ((W)w); |
907 | ev_clear_pending ((W)w); |
778 | if (!ev_is_active (w)) |
908 | if (!ev_is_active (w)) |
779 | return; |
909 | return; |
780 | |
910 | |
781 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
911 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
782 | ev_stop ((W)w); |
912 | ev_stop ((W)w); |
… | |
… | |
790 | if (ev_is_active (w)) |
920 | if (ev_is_active (w)) |
791 | return; |
921 | return; |
792 | |
922 | |
793 | w->at += now; |
923 | w->at += now; |
794 | |
924 | |
795 | 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.)); |
796 | |
926 | |
797 | ev_start ((W)w, ++timercnt); |
927 | ev_start ((W)w, ++timercnt); |
798 | array_needsize (timers, timermax, timercnt, ); |
928 | array_needsize (timers, timermax, timercnt, ); |
799 | timers [timercnt - 1] = w; |
929 | timers [timercnt - 1] = w; |
800 | upheap ((WT *)timers, timercnt - 1); |
930 | upheap ((WT *)timers, timercnt - 1); |
801 | } |
931 | } |
802 | |
932 | |
803 | void |
933 | void |
804 | ev_timer_stop (struct ev_timer *w) |
934 | ev_timer_stop (struct ev_timer *w) |
805 | { |
935 | { |
806 | ev_clear ((W)w); |
936 | ev_clear_pending ((W)w); |
807 | if (!ev_is_active (w)) |
937 | if (!ev_is_active (w)) |
808 | return; |
938 | return; |
809 | |
939 | |
810 | if (w->active < timercnt--) |
940 | if (w->active < timercnt--) |
811 | { |
941 | { |
… | |
… | |
839 | ev_periodic_start (struct ev_periodic *w) |
969 | ev_periodic_start (struct ev_periodic *w) |
840 | { |
970 | { |
841 | if (ev_is_active (w)) |
971 | if (ev_is_active (w)) |
842 | return; |
972 | return; |
843 | |
973 | |
844 | 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.)); |
845 | |
975 | |
846 | /* 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 */ |
847 | if (w->interval) |
977 | if (w->interval) |
848 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
978 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
849 | |
979 | |
… | |
… | |
854 | } |
984 | } |
855 | |
985 | |
856 | void |
986 | void |
857 | ev_periodic_stop (struct ev_periodic *w) |
987 | ev_periodic_stop (struct ev_periodic *w) |
858 | { |
988 | { |
859 | ev_clear ((W)w); |
989 | ev_clear_pending ((W)w); |
860 | if (!ev_is_active (w)) |
990 | if (!ev_is_active (w)) |
861 | return; |
991 | return; |
862 | |
992 | |
863 | if (w->active < periodiccnt--) |
993 | if (w->active < periodiccnt--) |
864 | { |
994 | { |
… | |
… | |
872 | void |
1002 | void |
873 | ev_signal_start (struct ev_signal *w) |
1003 | ev_signal_start (struct ev_signal *w) |
874 | { |
1004 | { |
875 | if (ev_is_active (w)) |
1005 | if (ev_is_active (w)) |
876 | return; |
1006 | return; |
|
|
1007 | |
|
|
1008 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
877 | |
1009 | |
878 | ev_start ((W)w, 1); |
1010 | ev_start ((W)w, 1); |
879 | array_needsize (signals, signalmax, w->signum, signals_init); |
1011 | array_needsize (signals, signalmax, w->signum, signals_init); |
880 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1012 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
881 | |
1013 | |
… | |
… | |
890 | } |
1022 | } |
891 | |
1023 | |
892 | void |
1024 | void |
893 | ev_signal_stop (struct ev_signal *w) |
1025 | ev_signal_stop (struct ev_signal *w) |
894 | { |
1026 | { |
895 | ev_clear ((W)w); |
1027 | ev_clear_pending ((W)w); |
896 | if (!ev_is_active (w)) |
1028 | if (!ev_is_active (w)) |
897 | return; |
1029 | return; |
898 | |
1030 | |
899 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1031 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
900 | ev_stop ((W)w); |
1032 | ev_stop ((W)w); |
… | |
… | |
915 | } |
1047 | } |
916 | |
1048 | |
917 | void |
1049 | void |
918 | ev_idle_stop (struct ev_idle *w) |
1050 | ev_idle_stop (struct ev_idle *w) |
919 | { |
1051 | { |
920 | ev_clear ((W)w); |
1052 | ev_clear_pending ((W)w); |
921 | if (ev_is_active (w)) |
1053 | if (ev_is_active (w)) |
922 | return; |
1054 | return; |
923 | |
1055 | |
924 | idles [w->active - 1] = idles [--idlecnt]; |
1056 | idles [w->active - 1] = idles [--idlecnt]; |
925 | ev_stop ((W)w); |
1057 | ev_stop ((W)w); |
… | |
… | |
937 | } |
1069 | } |
938 | |
1070 | |
939 | void |
1071 | void |
940 | ev_prepare_stop (struct ev_prepare *w) |
1072 | ev_prepare_stop (struct ev_prepare *w) |
941 | { |
1073 | { |
942 | ev_clear ((W)w); |
1074 | ev_clear_pending ((W)w); |
943 | if (ev_is_active (w)) |
1075 | if (ev_is_active (w)) |
944 | return; |
1076 | return; |
945 | |
1077 | |
946 | prepares [w->active - 1] = prepares [--preparecnt]; |
1078 | prepares [w->active - 1] = prepares [--preparecnt]; |
947 | ev_stop ((W)w); |
1079 | ev_stop ((W)w); |
… | |
… | |
959 | } |
1091 | } |
960 | |
1092 | |
961 | void |
1093 | void |
962 | ev_check_stop (struct ev_check *w) |
1094 | ev_check_stop (struct ev_check *w) |
963 | { |
1095 | { |
964 | ev_clear ((W)w); |
1096 | ev_clear_pending ((W)w); |
965 | if (ev_is_active (w)) |
1097 | if (ev_is_active (w)) |
966 | return; |
1098 | return; |
967 | |
1099 | |
968 | checks [w->active - 1] = checks [--checkcnt]; |
1100 | checks [w->active - 1] = checks [--checkcnt]; |
969 | ev_stop ((W)w); |
1101 | ev_stop ((W)w); |
… | |
… | |
980 | } |
1112 | } |
981 | |
1113 | |
982 | void |
1114 | void |
983 | ev_child_stop (struct ev_child *w) |
1115 | ev_child_stop (struct ev_child *w) |
984 | { |
1116 | { |
985 | ev_clear ((W)w); |
1117 | ev_clear_pending ((W)w); |
986 | if (ev_is_active (w)) |
1118 | if (ev_is_active (w)) |
987 | return; |
1119 | return; |
988 | |
1120 | |
989 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1121 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
990 | ev_stop ((W)w); |
1122 | ev_stop ((W)w); |