… | |
… | |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 | |
|
|
32 | #ifdef __cplusplus |
|
|
33 | extern "C" { |
|
|
34 | #endif |
|
|
35 | |
31 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
37 | # include "config.h" |
33 | |
38 | |
34 | # if HAVE_CLOCK_GETTIME |
39 | # if HAVE_CLOCK_GETTIME |
35 | # define EV_USE_MONOTONIC 1 |
40 | # define EV_USE_MONOTONIC 1 |
… | |
… | |
46 | |
51 | |
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
52 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
48 | # define EV_USE_EPOLL 1 |
53 | # define EV_USE_EPOLL 1 |
49 | # endif |
54 | # endif |
50 | |
55 | |
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
56 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
52 | # define EV_USE_KQUEUE 1 |
57 | # define EV_USE_KQUEUE 1 |
53 | # endif |
58 | # endif |
54 | |
59 | |
55 | #endif |
60 | #endif |
56 | |
61 | |
57 | #include <math.h> |
62 | #include <math.h> |
58 | #include <stdlib.h> |
63 | #include <stdlib.h> |
59 | #include <unistd.h> |
|
|
60 | #include <fcntl.h> |
64 | #include <fcntl.h> |
61 | #include <signal.h> |
|
|
62 | #include <stddef.h> |
65 | #include <stddef.h> |
63 | |
66 | |
64 | #include <stdio.h> |
67 | #include <stdio.h> |
65 | |
68 | |
66 | #include <assert.h> |
69 | #include <assert.h> |
67 | #include <errno.h> |
70 | #include <errno.h> |
68 | #include <sys/types.h> |
71 | #include <sys/types.h> |
|
|
72 | #include <time.h> |
|
|
73 | |
|
|
74 | #include <signal.h> |
|
|
75 | |
69 | #ifndef WIN32 |
76 | #ifndef WIN32 |
|
|
77 | # include <unistd.h> |
|
|
78 | # include <sys/time.h> |
70 | # include <sys/wait.h> |
79 | # include <sys/wait.h> |
71 | #endif |
80 | #endif |
72 | #include <sys/time.h> |
|
|
73 | #include <time.h> |
|
|
74 | |
|
|
75 | /**/ |
81 | /**/ |
76 | |
82 | |
77 | #ifndef EV_USE_MONOTONIC |
83 | #ifndef EV_USE_MONOTONIC |
78 | # define EV_USE_MONOTONIC 1 |
84 | # define EV_USE_MONOTONIC 1 |
79 | #endif |
85 | #endif |
… | |
… | |
94 | # define EV_USE_KQUEUE 0 |
100 | # define EV_USE_KQUEUE 0 |
95 | #endif |
101 | #endif |
96 | |
102 | |
97 | #ifndef EV_USE_WIN32 |
103 | #ifndef EV_USE_WIN32 |
98 | # ifdef WIN32 |
104 | # ifdef WIN32 |
|
|
105 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
106 | # undef EV_USE_SELECT |
99 | # define EV_USE_WIN32 1 |
107 | # define EV_USE_SELECT 1 |
100 | # else |
108 | # else |
101 | # define EV_USE_WIN32 0 |
109 | # define EV_USE_WIN32 0 |
102 | # endif |
110 | # endif |
103 | #endif |
111 | #endif |
104 | |
112 | |
… | |
… | |
123 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
131 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
124 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
132 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
125 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
133 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
126 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
134 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
127 | |
135 | |
|
|
136 | #ifdef EV_H |
|
|
137 | # include EV_H |
|
|
138 | #else |
128 | #include "ev.h" |
139 | # include "ev.h" |
|
|
140 | #endif |
129 | |
141 | |
130 | #if __GNUC__ >= 3 |
142 | #if __GNUC__ >= 3 |
131 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
143 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
132 | # define inline inline |
144 | # define inline inline |
133 | #else |
145 | #else |
… | |
… | |
145 | typedef struct ev_watcher_list *WL; |
157 | typedef struct ev_watcher_list *WL; |
146 | typedef struct ev_watcher_time *WT; |
158 | typedef struct ev_watcher_time *WT; |
147 | |
159 | |
148 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
160 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
149 | |
161 | |
150 | #if WIN32 |
162 | #include "ev_win32.c" |
151 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
152 | /* MSDN says this is required to handle SIGFPE */ |
|
|
153 | volatile double SIGFPE_REQ = 0.0f; |
|
|
154 | #endif |
|
|
155 | |
163 | |
156 | /*****************************************************************************/ |
164 | /*****************************************************************************/ |
157 | |
165 | |
158 | static void (*syserr_cb)(void); |
166 | static void (*syserr_cb)(const char *msg); |
159 | |
167 | |
160 | void ev_set_syserr_cb (void (*cb)(void)) |
168 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
161 | { |
169 | { |
162 | syserr_cb = cb; |
170 | syserr_cb = cb; |
163 | } |
171 | } |
164 | |
172 | |
165 | static void |
173 | static void |
166 | syserr (void) |
174 | syserr (const char *msg) |
167 | { |
175 | { |
|
|
176 | if (!msg) |
|
|
177 | msg = "(libev) system error"; |
|
|
178 | |
168 | if (syserr_cb) |
179 | if (syserr_cb) |
169 | syserr_cb (); |
180 | syserr_cb (msg); |
170 | else |
181 | else |
171 | { |
182 | { |
172 | perror ("libev"); |
183 | perror (msg); |
173 | abort (); |
184 | abort (); |
174 | } |
185 | } |
175 | } |
186 | } |
176 | |
187 | |
177 | static void *(*alloc)(void *ptr, long size); |
188 | static void *(*alloc)(void *ptr, long size); |
… | |
… | |
213 | int events; |
224 | int events; |
214 | } ANPENDING; |
225 | } ANPENDING; |
215 | |
226 | |
216 | #if EV_MULTIPLICITY |
227 | #if EV_MULTIPLICITY |
217 | |
228 | |
218 | struct ev_loop |
229 | struct ev_loop |
219 | { |
230 | { |
|
|
231 | ev_tstamp ev_rt_now; |
220 | # define VAR(name,decl) decl; |
232 | #define VAR(name,decl) decl; |
221 | # include "ev_vars.h" |
233 | #include "ev_vars.h" |
222 | }; |
|
|
223 | # undef VAR |
234 | #undef VAR |
|
|
235 | }; |
224 | # include "ev_wrap.h" |
236 | #include "ev_wrap.h" |
|
|
237 | |
|
|
238 | struct ev_loop default_loop_struct; |
|
|
239 | static struct ev_loop *default_loop; |
225 | |
240 | |
226 | #else |
241 | #else |
227 | |
242 | |
|
|
243 | ev_tstamp ev_rt_now; |
228 | # define VAR(name,decl) static decl; |
244 | #define VAR(name,decl) static decl; |
229 | # include "ev_vars.h" |
245 | #include "ev_vars.h" |
230 | # undef VAR |
246 | #undef VAR |
|
|
247 | |
|
|
248 | static int default_loop; |
231 | |
249 | |
232 | #endif |
250 | #endif |
233 | |
251 | |
234 | /*****************************************************************************/ |
252 | /*****************************************************************************/ |
235 | |
253 | |
236 | inline ev_tstamp |
254 | ev_tstamp |
237 | ev_time (void) |
255 | ev_time (void) |
238 | { |
256 | { |
239 | #if EV_USE_REALTIME |
257 | #if EV_USE_REALTIME |
240 | struct timespec ts; |
258 | struct timespec ts; |
241 | clock_gettime (CLOCK_REALTIME, &ts); |
259 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
260 | #endif |
278 | #endif |
261 | |
279 | |
262 | return ev_time (); |
280 | return ev_time (); |
263 | } |
281 | } |
264 | |
282 | |
|
|
283 | #if EV_MULTIPLICITY |
265 | ev_tstamp |
284 | ev_tstamp |
266 | ev_now (EV_P) |
285 | ev_now (EV_P) |
267 | { |
286 | { |
268 | return rt_now; |
287 | return ev_rt_now; |
269 | } |
288 | } |
|
|
289 | #endif |
270 | |
290 | |
271 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
291 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
272 | |
292 | |
273 | #define array_needsize(base,cur,cnt,init) \ |
293 | #define array_needsize(type,base,cur,cnt,init) \ |
274 | if (expect_false ((cnt) > cur)) \ |
294 | if (expect_false ((cnt) > cur)) \ |
275 | { \ |
295 | { \ |
276 | int newcnt = cur; \ |
296 | int newcnt = cur; \ |
277 | do \ |
297 | do \ |
278 | { \ |
298 | { \ |
279 | newcnt = array_roundsize (base, newcnt << 1); \ |
299 | newcnt = array_roundsize (type, newcnt << 1); \ |
280 | } \ |
300 | } \ |
281 | while ((cnt) > newcnt); \ |
301 | while ((cnt) > newcnt); \ |
282 | \ |
302 | \ |
283 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
303 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
284 | init (base + cur, newcnt - cur); \ |
304 | init (base + cur, newcnt - cur); \ |
285 | cur = newcnt; \ |
305 | cur = newcnt; \ |
286 | } |
306 | } |
287 | |
307 | |
288 | #define array_slim(stem) \ |
308 | #define array_slim(type,stem) \ |
289 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
309 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
290 | { \ |
310 | { \ |
291 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
311 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
292 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
312 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
293 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
313 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
294 | } |
314 | } |
|
|
315 | |
|
|
316 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
317 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
318 | #define array_free_microshit(stem) \ |
|
|
319 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
295 | |
320 | |
296 | #define array_free(stem, idx) \ |
321 | #define array_free(stem, idx) \ |
297 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
322 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
298 | |
323 | |
299 | /*****************************************************************************/ |
324 | /*****************************************************************************/ |
… | |
… | |
309 | |
334 | |
310 | ++base; |
335 | ++base; |
311 | } |
336 | } |
312 | } |
337 | } |
313 | |
338 | |
314 | static void |
339 | void |
315 | event (EV_P_ W w, int events) |
340 | ev_feed_event (EV_P_ void *w, int revents) |
316 | { |
341 | { |
|
|
342 | W w_ = (W)w; |
|
|
343 | |
317 | if (w->pending) |
344 | if (w_->pending) |
318 | { |
345 | { |
319 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
346 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
320 | return; |
347 | return; |
321 | } |
348 | } |
322 | |
349 | |
323 | w->pending = ++pendingcnt [ABSPRI (w)]; |
350 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
324 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
351 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
325 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
352 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
326 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
353 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
327 | } |
354 | } |
328 | |
355 | |
329 | static void |
356 | static void |
330 | queue_events (EV_P_ W *events, int eventcnt, int type) |
357 | queue_events (EV_P_ W *events, int eventcnt, int type) |
331 | { |
358 | { |
332 | int i; |
359 | int i; |
333 | |
360 | |
334 | for (i = 0; i < eventcnt; ++i) |
361 | for (i = 0; i < eventcnt; ++i) |
335 | event (EV_A_ events [i], type); |
362 | ev_feed_event (EV_A_ events [i], type); |
336 | } |
363 | } |
337 | |
364 | |
338 | static void |
365 | inline void |
339 | fd_event (EV_P_ int fd, int events) |
366 | fd_event (EV_P_ int fd, int revents) |
340 | { |
367 | { |
341 | ANFD *anfd = anfds + fd; |
368 | ANFD *anfd = anfds + fd; |
342 | struct ev_io *w; |
369 | struct ev_io *w; |
343 | |
370 | |
344 | 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) |
345 | { |
372 | { |
346 | int ev = w->events & events; |
373 | int ev = w->events & revents; |
347 | |
374 | |
348 | if (ev) |
375 | if (ev) |
349 | event (EV_A_ (W)w, ev); |
376 | ev_feed_event (EV_A_ (W)w, ev); |
350 | } |
377 | } |
|
|
378 | } |
|
|
379 | |
|
|
380 | void |
|
|
381 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
382 | { |
|
|
383 | fd_event (EV_A_ fd, revents); |
351 | } |
384 | } |
352 | |
385 | |
353 | /*****************************************************************************/ |
386 | /*****************************************************************************/ |
354 | |
387 | |
355 | static void |
388 | static void |
… | |
… | |
378 | } |
411 | } |
379 | |
412 | |
380 | static void |
413 | static void |
381 | fd_change (EV_P_ int fd) |
414 | fd_change (EV_P_ int fd) |
382 | { |
415 | { |
383 | if (anfds [fd].reify || fdchangecnt < 0) |
416 | if (anfds [fd].reify) |
384 | return; |
417 | return; |
385 | |
418 | |
386 | anfds [fd].reify = 1; |
419 | anfds [fd].reify = 1; |
387 | |
420 | |
388 | ++fdchangecnt; |
421 | ++fdchangecnt; |
389 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
422 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
390 | fdchanges [fdchangecnt - 1] = fd; |
423 | fdchanges [fdchangecnt - 1] = fd; |
391 | } |
424 | } |
392 | |
425 | |
393 | static void |
426 | static void |
394 | fd_kill (EV_P_ int fd) |
427 | fd_kill (EV_P_ int fd) |
… | |
… | |
396 | struct ev_io *w; |
429 | struct ev_io *w; |
397 | |
430 | |
398 | while ((w = (struct ev_io *)anfds [fd].head)) |
431 | while ((w = (struct ev_io *)anfds [fd].head)) |
399 | { |
432 | { |
400 | ev_io_stop (EV_A_ w); |
433 | ev_io_stop (EV_A_ w); |
401 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
434 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
402 | } |
435 | } |
|
|
436 | } |
|
|
437 | |
|
|
438 | static int |
|
|
439 | fd_valid (int fd) |
|
|
440 | { |
|
|
441 | #ifdef WIN32 |
|
|
442 | return !!win32_get_osfhandle (fd); |
|
|
443 | #else |
|
|
444 | return fcntl (fd, F_GETFD) != -1; |
|
|
445 | #endif |
403 | } |
446 | } |
404 | |
447 | |
405 | /* called on EBADF to verify fds */ |
448 | /* called on EBADF to verify fds */ |
406 | static void |
449 | static void |
407 | fd_ebadf (EV_P) |
450 | fd_ebadf (EV_P) |
408 | { |
451 | { |
409 | int fd; |
452 | int fd; |
410 | |
453 | |
411 | for (fd = 0; fd < anfdmax; ++fd) |
454 | for (fd = 0; fd < anfdmax; ++fd) |
412 | if (anfds [fd].events) |
455 | if (anfds [fd].events) |
413 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
456 | if (!fd_valid (fd) == -1 && errno == EBADF) |
414 | fd_kill (EV_A_ fd); |
457 | fd_kill (EV_A_ fd); |
415 | } |
458 | } |
416 | |
459 | |
417 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
460 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
418 | static void |
461 | static void |
… | |
… | |
426 | fd_kill (EV_A_ fd); |
469 | fd_kill (EV_A_ fd); |
427 | return; |
470 | return; |
428 | } |
471 | } |
429 | } |
472 | } |
430 | |
473 | |
431 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
474 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
432 | static void |
475 | static void |
433 | fd_rearm_all (EV_P) |
476 | fd_rearm_all (EV_P) |
434 | { |
477 | { |
435 | int fd; |
478 | int fd; |
436 | |
479 | |
… | |
… | |
484 | |
527 | |
485 | heap [k] = w; |
528 | heap [k] = w; |
486 | ((W)heap [k])->active = k + 1; |
529 | ((W)heap [k])->active = k + 1; |
487 | } |
530 | } |
488 | |
531 | |
|
|
532 | inline void |
|
|
533 | adjustheap (WT *heap, int N, int k, ev_tstamp at) |
|
|
534 | { |
|
|
535 | ev_tstamp old_at = heap [k]->at; |
|
|
536 | heap [k]->at = at; |
|
|
537 | |
|
|
538 | if (old_at < at) |
|
|
539 | downheap (heap, N, k); |
|
|
540 | else |
|
|
541 | upheap (heap, k); |
|
|
542 | } |
|
|
543 | |
489 | /*****************************************************************************/ |
544 | /*****************************************************************************/ |
490 | |
545 | |
491 | typedef struct |
546 | typedef struct |
492 | { |
547 | { |
493 | WL head; |
548 | WL head; |
… | |
… | |
524 | |
579 | |
525 | if (!gotsig) |
580 | if (!gotsig) |
526 | { |
581 | { |
527 | int old_errno = errno; |
582 | int old_errno = errno; |
528 | gotsig = 1; |
583 | gotsig = 1; |
|
|
584 | #ifdef WIN32 |
|
|
585 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
586 | #else |
529 | write (sigpipe [1], &signum, 1); |
587 | write (sigpipe [1], &signum, 1); |
|
|
588 | #endif |
530 | errno = old_errno; |
589 | errno = old_errno; |
531 | } |
590 | } |
532 | } |
591 | } |
533 | |
592 | |
|
|
593 | void |
|
|
594 | ev_feed_signal_event (EV_P_ int signum) |
|
|
595 | { |
|
|
596 | WL w; |
|
|
597 | |
|
|
598 | #if EV_MULTIPLICITY |
|
|
599 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
600 | #endif |
|
|
601 | |
|
|
602 | --signum; |
|
|
603 | |
|
|
604 | if (signum < 0 || signum >= signalmax) |
|
|
605 | return; |
|
|
606 | |
|
|
607 | signals [signum].gotsig = 0; |
|
|
608 | |
|
|
609 | for (w = signals [signum].head; w; w = w->next) |
|
|
610 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
611 | } |
|
|
612 | |
534 | static void |
613 | static void |
535 | sigcb (EV_P_ struct ev_io *iow, int revents) |
614 | sigcb (EV_P_ struct ev_io *iow, int revents) |
536 | { |
615 | { |
537 | WL w; |
|
|
538 | int signum; |
616 | int signum; |
539 | |
617 | |
|
|
618 | #ifdef WIN32 |
|
|
619 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
620 | #else |
540 | read (sigpipe [0], &revents, 1); |
621 | read (sigpipe [0], &revents, 1); |
|
|
622 | #endif |
541 | gotsig = 0; |
623 | gotsig = 0; |
542 | |
624 | |
543 | for (signum = signalmax; signum--; ) |
625 | for (signum = signalmax; signum--; ) |
544 | if (signals [signum].gotsig) |
626 | if (signals [signum].gotsig) |
545 | { |
627 | ev_feed_signal_event (EV_A_ signum + 1); |
546 | signals [signum].gotsig = 0; |
|
|
547 | |
|
|
548 | for (w = signals [signum].head; w; w = w->next) |
|
|
549 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
550 | } |
|
|
551 | } |
628 | } |
552 | |
629 | |
553 | static void |
630 | static void |
554 | siginit (EV_P) |
631 | siginit (EV_P) |
555 | { |
632 | { |
… | |
… | |
567 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
644 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
568 | } |
645 | } |
569 | |
646 | |
570 | /*****************************************************************************/ |
647 | /*****************************************************************************/ |
571 | |
648 | |
|
|
649 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
650 | |
572 | #ifndef WIN32 |
651 | #ifndef WIN32 |
573 | |
652 | |
574 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
575 | static struct ev_signal childev; |
653 | static struct ev_signal childev; |
576 | |
654 | |
577 | #ifndef WCONTINUED |
655 | #ifndef WCONTINUED |
578 | # define WCONTINUED 0 |
656 | # define WCONTINUED 0 |
579 | #endif |
657 | #endif |
… | |
… | |
587 | if (w->pid == pid || !w->pid) |
665 | if (w->pid == pid || !w->pid) |
588 | { |
666 | { |
589 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
667 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
590 | w->rpid = pid; |
668 | w->rpid = pid; |
591 | w->rstatus = status; |
669 | w->rstatus = status; |
592 | event (EV_A_ (W)w, EV_CHILD); |
670 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
593 | } |
671 | } |
594 | } |
672 | } |
595 | |
673 | |
596 | static void |
674 | static void |
597 | childcb (EV_P_ struct ev_signal *sw, int revents) |
675 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
599 | int pid, status; |
677 | int pid, status; |
600 | |
678 | |
601 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
679 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
602 | { |
680 | { |
603 | /* make sure we are called again until all childs have been reaped */ |
681 | /* make sure we are called again until all childs have been reaped */ |
604 | event (EV_A_ (W)sw, EV_SIGNAL); |
682 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
605 | |
683 | |
606 | child_reap (EV_A_ sw, pid, pid, status); |
684 | child_reap (EV_A_ sw, pid, pid, status); |
607 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
685 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
608 | } |
686 | } |
609 | } |
687 | } |
… | |
… | |
666 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
744 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
667 | have_monotonic = 1; |
745 | have_monotonic = 1; |
668 | } |
746 | } |
669 | #endif |
747 | #endif |
670 | |
748 | |
671 | rt_now = ev_time (); |
749 | ev_rt_now = ev_time (); |
672 | mn_now = get_clock (); |
750 | mn_now = get_clock (); |
673 | now_floor = mn_now; |
751 | now_floor = mn_now; |
674 | rtmn_diff = rt_now - mn_now; |
752 | rtmn_diff = ev_rt_now - mn_now; |
675 | |
753 | |
676 | if (methods == EVMETHOD_AUTO) |
754 | if (methods == EVMETHOD_AUTO) |
677 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
755 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
678 | methods = atoi (getenv ("LIBEV_METHODS")); |
756 | methods = atoi (getenv ("LIBEV_METHODS")); |
679 | else |
757 | else |
… | |
… | |
693 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
771 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
694 | #endif |
772 | #endif |
695 | #if EV_USE_SELECT |
773 | #if EV_USE_SELECT |
696 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
774 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
697 | #endif |
775 | #endif |
|
|
776 | |
|
|
777 | ev_init (&sigev, sigcb); |
|
|
778 | ev_set_priority (&sigev, EV_MAXPRI); |
698 | } |
779 | } |
699 | } |
780 | } |
700 | |
781 | |
701 | void |
782 | void |
702 | loop_destroy (EV_P) |
783 | loop_destroy (EV_P) |
… | |
… | |
720 | #endif |
801 | #endif |
721 | |
802 | |
722 | for (i = NUMPRI; i--; ) |
803 | for (i = NUMPRI; i--; ) |
723 | array_free (pending, [i]); |
804 | array_free (pending, [i]); |
724 | |
805 | |
|
|
806 | /* have to use the microsoft-never-gets-it-right macro */ |
725 | array_free (fdchange, ); |
807 | array_free_microshit (fdchange); |
726 | array_free (timer, ); |
808 | array_free_microshit (timer); |
|
|
809 | #if EV_PERIODICS |
727 | array_free (periodic, ); |
810 | array_free_microshit (periodic); |
|
|
811 | #endif |
728 | array_free (idle, ); |
812 | array_free_microshit (idle); |
729 | array_free (prepare, ); |
813 | array_free_microshit (prepare); |
730 | array_free (check, ); |
814 | array_free_microshit (check); |
731 | |
815 | |
732 | method = 0; |
816 | method = 0; |
733 | /*TODO*/ |
|
|
734 | } |
817 | } |
735 | |
818 | |
736 | void |
819 | static void |
737 | loop_fork (EV_P) |
820 | loop_fork (EV_P) |
738 | { |
821 | { |
739 | /*TODO*/ |
|
|
740 | #if EV_USE_EPOLL |
822 | #if EV_USE_EPOLL |
741 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
823 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
742 | #endif |
824 | #endif |
743 | #if EV_USE_KQUEUE |
825 | #if EV_USE_KQUEUE |
744 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
826 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
745 | #endif |
827 | #endif |
|
|
828 | |
|
|
829 | if (ev_is_active (&sigev)) |
|
|
830 | { |
|
|
831 | /* default loop */ |
|
|
832 | |
|
|
833 | ev_ref (EV_A); |
|
|
834 | ev_io_stop (EV_A_ &sigev); |
|
|
835 | close (sigpipe [0]); |
|
|
836 | close (sigpipe [1]); |
|
|
837 | |
|
|
838 | while (pipe (sigpipe)) |
|
|
839 | syserr ("(libev) error creating pipe"); |
|
|
840 | |
|
|
841 | siginit (EV_A); |
|
|
842 | } |
|
|
843 | |
|
|
844 | postfork = 0; |
746 | } |
845 | } |
747 | |
846 | |
748 | #if EV_MULTIPLICITY |
847 | #if EV_MULTIPLICITY |
749 | struct ev_loop * |
848 | struct ev_loop * |
750 | ev_loop_new (int methods) |
849 | ev_loop_new (int methods) |
… | |
… | |
769 | } |
868 | } |
770 | |
869 | |
771 | void |
870 | void |
772 | ev_loop_fork (EV_P) |
871 | ev_loop_fork (EV_P) |
773 | { |
872 | { |
774 | loop_fork (EV_A); |
873 | postfork = 1; |
775 | } |
874 | } |
776 | |
875 | |
777 | #endif |
876 | #endif |
778 | |
877 | |
779 | #if EV_MULTIPLICITY |
878 | #if EV_MULTIPLICITY |
780 | struct ev_loop default_loop_struct; |
|
|
781 | static struct ev_loop *default_loop; |
|
|
782 | |
|
|
783 | struct ev_loop * |
879 | struct ev_loop * |
784 | #else |
880 | #else |
785 | static int default_loop; |
|
|
786 | |
|
|
787 | int |
881 | int |
788 | #endif |
882 | #endif |
789 | ev_default_loop (int methods) |
883 | ev_default_loop (int methods) |
790 | { |
884 | { |
791 | if (sigpipe [0] == sigpipe [1]) |
885 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
802 | |
896 | |
803 | loop_init (EV_A_ methods); |
897 | loop_init (EV_A_ methods); |
804 | |
898 | |
805 | if (ev_method (EV_A)) |
899 | if (ev_method (EV_A)) |
806 | { |
900 | { |
807 | ev_watcher_init (&sigev, sigcb); |
|
|
808 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
809 | siginit (EV_A); |
901 | siginit (EV_A); |
810 | |
902 | |
811 | #ifndef WIN32 |
903 | #ifndef WIN32 |
812 | ev_signal_init (&childev, childcb, SIGCHLD); |
904 | ev_signal_init (&childev, childcb, SIGCHLD); |
813 | ev_set_priority (&childev, EV_MAXPRI); |
905 | ev_set_priority (&childev, EV_MAXPRI); |
… | |
… | |
827 | { |
919 | { |
828 | #if EV_MULTIPLICITY |
920 | #if EV_MULTIPLICITY |
829 | struct ev_loop *loop = default_loop; |
921 | struct ev_loop *loop = default_loop; |
830 | #endif |
922 | #endif |
831 | |
923 | |
|
|
924 | #ifndef WIN32 |
832 | ev_ref (EV_A); /* child watcher */ |
925 | ev_ref (EV_A); /* child watcher */ |
833 | ev_signal_stop (EV_A_ &childev); |
926 | ev_signal_stop (EV_A_ &childev); |
|
|
927 | #endif |
834 | |
928 | |
835 | ev_ref (EV_A); /* signal watcher */ |
929 | ev_ref (EV_A); /* signal watcher */ |
836 | ev_io_stop (EV_A_ &sigev); |
930 | ev_io_stop (EV_A_ &sigev); |
837 | |
931 | |
838 | close (sigpipe [0]); sigpipe [0] = 0; |
932 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
846 | { |
940 | { |
847 | #if EV_MULTIPLICITY |
941 | #if EV_MULTIPLICITY |
848 | struct ev_loop *loop = default_loop; |
942 | struct ev_loop *loop = default_loop; |
849 | #endif |
943 | #endif |
850 | |
944 | |
851 | loop_fork (EV_A); |
945 | if (method) |
852 | |
946 | postfork = 1; |
853 | ev_io_stop (EV_A_ &sigev); |
|
|
854 | close (sigpipe [0]); |
|
|
855 | close (sigpipe [1]); |
|
|
856 | pipe (sigpipe); |
|
|
857 | |
|
|
858 | ev_ref (EV_A); /* signal watcher */ |
|
|
859 | siginit (EV_A); |
|
|
860 | } |
947 | } |
861 | |
948 | |
862 | /*****************************************************************************/ |
949 | /*****************************************************************************/ |
|
|
950 | |
|
|
951 | static int |
|
|
952 | any_pending (EV_P) |
|
|
953 | { |
|
|
954 | int pri; |
|
|
955 | |
|
|
956 | for (pri = NUMPRI; pri--; ) |
|
|
957 | if (pendingcnt [pri]) |
|
|
958 | return 1; |
|
|
959 | |
|
|
960 | return 0; |
|
|
961 | } |
863 | |
962 | |
864 | static void |
963 | static void |
865 | call_pending (EV_P) |
964 | call_pending (EV_P) |
866 | { |
965 | { |
867 | int pri; |
966 | int pri; |
… | |
… | |
872 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
971 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
873 | |
972 | |
874 | if (p->w) |
973 | if (p->w) |
875 | { |
974 | { |
876 | p->w->pending = 0; |
975 | p->w->pending = 0; |
877 | p->w->cb (EV_A_ p->w, p->events); |
976 | EV_CB_INVOKE (p->w, p->events); |
878 | } |
977 | } |
879 | } |
978 | } |
880 | } |
979 | } |
881 | |
980 | |
882 | static void |
981 | static void |
… | |
… | |
890 | |
989 | |
891 | /* first reschedule or stop timer */ |
990 | /* first reschedule or stop timer */ |
892 | if (w->repeat) |
991 | if (w->repeat) |
893 | { |
992 | { |
894 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
993 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
994 | |
895 | ((WT)w)->at = mn_now + w->repeat; |
995 | ((WT)w)->at += w->repeat; |
|
|
996 | if (((WT)w)->at < mn_now) |
|
|
997 | ((WT)w)->at = mn_now; |
|
|
998 | |
896 | downheap ((WT *)timers, timercnt, 0); |
999 | downheap ((WT *)timers, timercnt, 0); |
897 | } |
1000 | } |
898 | else |
1001 | else |
899 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1002 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
900 | |
1003 | |
901 | event (EV_A_ (W)w, EV_TIMEOUT); |
1004 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
902 | } |
1005 | } |
903 | } |
1006 | } |
904 | |
1007 | |
|
|
1008 | #if EV_PERIODICS |
905 | static void |
1009 | static void |
906 | periodics_reify (EV_P) |
1010 | periodics_reify (EV_P) |
907 | { |
1011 | { |
908 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1012 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
909 | { |
1013 | { |
910 | struct ev_periodic *w = periodics [0]; |
1014 | struct ev_periodic *w = periodics [0]; |
911 | |
1015 | |
912 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1016 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
913 | |
1017 | |
914 | /* first reschedule or stop timer */ |
1018 | /* first reschedule or stop timer */ |
915 | if (w->interval) |
1019 | if (w->reschedule_cb) |
916 | { |
1020 | { |
|
|
1021 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
|
|
1022 | |
|
|
1023 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
|
|
1024 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
1025 | } |
|
|
1026 | else if (w->interval) |
|
|
1027 | { |
917 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1028 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
918 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1029 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
919 | downheap ((WT *)periodics, periodiccnt, 0); |
1030 | downheap ((WT *)periodics, periodiccnt, 0); |
920 | } |
1031 | } |
921 | else |
1032 | else |
922 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1033 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
923 | |
1034 | |
924 | event (EV_A_ (W)w, EV_PERIODIC); |
1035 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
925 | } |
1036 | } |
926 | } |
1037 | } |
927 | |
1038 | |
928 | static void |
1039 | static void |
929 | periodics_reschedule (EV_P) |
1040 | periodics_reschedule (EV_P) |
… | |
… | |
933 | /* adjust periodics after time jump */ |
1044 | /* adjust periodics after time jump */ |
934 | for (i = 0; i < periodiccnt; ++i) |
1045 | for (i = 0; i < periodiccnt; ++i) |
935 | { |
1046 | { |
936 | struct ev_periodic *w = periodics [i]; |
1047 | struct ev_periodic *w = periodics [i]; |
937 | |
1048 | |
|
|
1049 | if (w->reschedule_cb) |
|
|
1050 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
938 | if (w->interval) |
1051 | else if (w->interval) |
939 | { |
|
|
940 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1052 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
941 | |
|
|
942 | if (fabs (diff) >= 1e-4) |
|
|
943 | { |
|
|
944 | ev_periodic_stop (EV_A_ w); |
|
|
945 | ev_periodic_start (EV_A_ w); |
|
|
946 | |
|
|
947 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
948 | } |
|
|
949 | } |
|
|
950 | } |
1053 | } |
|
|
1054 | |
|
|
1055 | /* now rebuild the heap */ |
|
|
1056 | for (i = periodiccnt >> 1; i--; ) |
|
|
1057 | downheap ((WT *)periodics, periodiccnt, i); |
951 | } |
1058 | } |
|
|
1059 | #endif |
952 | |
1060 | |
953 | inline int |
1061 | inline int |
954 | time_update_monotonic (EV_P) |
1062 | time_update_monotonic (EV_P) |
955 | { |
1063 | { |
956 | mn_now = get_clock (); |
1064 | mn_now = get_clock (); |
957 | |
1065 | |
958 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1066 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
959 | { |
1067 | { |
960 | rt_now = rtmn_diff + mn_now; |
1068 | ev_rt_now = rtmn_diff + mn_now; |
961 | return 0; |
1069 | return 0; |
962 | } |
1070 | } |
963 | else |
1071 | else |
964 | { |
1072 | { |
965 | now_floor = mn_now; |
1073 | now_floor = mn_now; |
966 | rt_now = ev_time (); |
1074 | ev_rt_now = ev_time (); |
967 | return 1; |
1075 | return 1; |
968 | } |
1076 | } |
969 | } |
1077 | } |
970 | |
1078 | |
971 | static void |
1079 | static void |
… | |
… | |
980 | { |
1088 | { |
981 | ev_tstamp odiff = rtmn_diff; |
1089 | ev_tstamp odiff = rtmn_diff; |
982 | |
1090 | |
983 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1091 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
984 | { |
1092 | { |
985 | rtmn_diff = rt_now - mn_now; |
1093 | rtmn_diff = ev_rt_now - mn_now; |
986 | |
1094 | |
987 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1095 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
988 | return; /* all is well */ |
1096 | return; /* all is well */ |
989 | |
1097 | |
990 | rt_now = ev_time (); |
1098 | ev_rt_now = ev_time (); |
991 | mn_now = get_clock (); |
1099 | mn_now = get_clock (); |
992 | now_floor = mn_now; |
1100 | now_floor = mn_now; |
993 | } |
1101 | } |
994 | |
1102 | |
|
|
1103 | # if EV_PERIODICS |
995 | periodics_reschedule (EV_A); |
1104 | periodics_reschedule (EV_A); |
|
|
1105 | # endif |
996 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1106 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
997 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1107 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
998 | } |
1108 | } |
999 | } |
1109 | } |
1000 | else |
1110 | else |
1001 | #endif |
1111 | #endif |
1002 | { |
1112 | { |
1003 | rt_now = ev_time (); |
1113 | ev_rt_now = ev_time (); |
1004 | |
1114 | |
1005 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1115 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1006 | { |
1116 | { |
|
|
1117 | #if EV_PERIODICS |
1007 | periodics_reschedule (EV_A); |
1118 | periodics_reschedule (EV_A); |
|
|
1119 | #endif |
1008 | |
1120 | |
1009 | /* adjust timers. this is easy, as the offset is the same for all */ |
1121 | /* adjust timers. this is easy, as the offset is the same for all */ |
1010 | for (i = 0; i < timercnt; ++i) |
1122 | for (i = 0; i < timercnt; ++i) |
1011 | ((WT)timers [i])->at += rt_now - mn_now; |
1123 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1012 | } |
1124 | } |
1013 | |
1125 | |
1014 | mn_now = rt_now; |
1126 | mn_now = ev_rt_now; |
1015 | } |
1127 | } |
1016 | } |
1128 | } |
1017 | |
1129 | |
1018 | void |
1130 | void |
1019 | ev_ref (EV_P) |
1131 | ev_ref (EV_P) |
… | |
… | |
1042 | { |
1154 | { |
1043 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1155 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1044 | call_pending (EV_A); |
1156 | call_pending (EV_A); |
1045 | } |
1157 | } |
1046 | |
1158 | |
|
|
1159 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1160 | if (expect_false (postfork)) |
|
|
1161 | loop_fork (EV_A); |
|
|
1162 | |
1047 | /* update fd-related kernel structures */ |
1163 | /* update fd-related kernel structures */ |
1048 | fd_reify (EV_A); |
1164 | fd_reify (EV_A); |
1049 | |
1165 | |
1050 | /* calculate blocking time */ |
1166 | /* calculate blocking time */ |
1051 | |
1167 | |
1052 | /* we only need this for !monotonic clockor timers, but as we basically |
1168 | /* we only need this for !monotonic clock or timers, but as we basically |
1053 | always have timers, we just calculate it always */ |
1169 | always have timers, we just calculate it always */ |
1054 | #if EV_USE_MONOTONIC |
1170 | #if EV_USE_MONOTONIC |
1055 | if (expect_true (have_monotonic)) |
1171 | if (expect_true (have_monotonic)) |
1056 | time_update_monotonic (EV_A); |
1172 | time_update_monotonic (EV_A); |
1057 | else |
1173 | else |
1058 | #endif |
1174 | #endif |
1059 | { |
1175 | { |
1060 | rt_now = ev_time (); |
1176 | ev_rt_now = ev_time (); |
1061 | mn_now = rt_now; |
1177 | mn_now = ev_rt_now; |
1062 | } |
1178 | } |
1063 | |
1179 | |
1064 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1180 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1065 | block = 0.; |
1181 | block = 0.; |
1066 | else |
1182 | else |
… | |
… | |
1071 | { |
1187 | { |
1072 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1188 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1073 | if (block > to) block = to; |
1189 | if (block > to) block = to; |
1074 | } |
1190 | } |
1075 | |
1191 | |
|
|
1192 | #if EV_PERIODICS |
1076 | if (periodiccnt) |
1193 | if (periodiccnt) |
1077 | { |
1194 | { |
1078 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1195 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1079 | if (block > to) block = to; |
1196 | if (block > to) block = to; |
1080 | } |
1197 | } |
|
|
1198 | #endif |
1081 | |
1199 | |
1082 | if (block < 0.) block = 0.; |
1200 | if (block < 0.) block = 0.; |
1083 | } |
1201 | } |
1084 | |
1202 | |
1085 | method_poll (EV_A_ block); |
1203 | method_poll (EV_A_ block); |
1086 | |
1204 | |
1087 | /* update rt_now, do magic */ |
1205 | /* update ev_rt_now, do magic */ |
1088 | time_update (EV_A); |
1206 | time_update (EV_A); |
1089 | |
1207 | |
1090 | /* queue pending timers and reschedule them */ |
1208 | /* queue pending timers and reschedule them */ |
1091 | timers_reify (EV_A); /* relative timers called last */ |
1209 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1210 | #if EV_PERIODICS |
1092 | periodics_reify (EV_A); /* absolute timers called first */ |
1211 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1212 | #endif |
1093 | |
1213 | |
1094 | /* queue idle watchers unless io or timers are pending */ |
1214 | /* queue idle watchers unless io or timers are pending */ |
1095 | if (!pendingcnt) |
1215 | if (idlecnt && !any_pending (EV_A)) |
1096 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1216 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1097 | |
1217 | |
1098 | /* queue check watchers, to be executed first */ |
1218 | /* queue check watchers, to be executed first */ |
1099 | if (checkcnt) |
1219 | if (checkcnt) |
1100 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1220 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1175 | return; |
1295 | return; |
1176 | |
1296 | |
1177 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1297 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1178 | |
1298 | |
1179 | ev_start (EV_A_ (W)w, 1); |
1299 | ev_start (EV_A_ (W)w, 1); |
1180 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1300 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1181 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1301 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1182 | |
1302 | |
1183 | fd_change (EV_A_ fd); |
1303 | fd_change (EV_A_ fd); |
1184 | } |
1304 | } |
1185 | |
1305 | |
… | |
… | |
1188 | { |
1308 | { |
1189 | ev_clear_pending (EV_A_ (W)w); |
1309 | ev_clear_pending (EV_A_ (W)w); |
1190 | if (!ev_is_active (w)) |
1310 | if (!ev_is_active (w)) |
1191 | return; |
1311 | return; |
1192 | |
1312 | |
|
|
1313 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1314 | |
1193 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1315 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1194 | ev_stop (EV_A_ (W)w); |
1316 | ev_stop (EV_A_ (W)w); |
1195 | |
1317 | |
1196 | fd_change (EV_A_ w->fd); |
1318 | fd_change (EV_A_ w->fd); |
1197 | } |
1319 | } |
… | |
… | |
1205 | ((WT)w)->at += mn_now; |
1327 | ((WT)w)->at += mn_now; |
1206 | |
1328 | |
1207 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1329 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1208 | |
1330 | |
1209 | ev_start (EV_A_ (W)w, ++timercnt); |
1331 | ev_start (EV_A_ (W)w, ++timercnt); |
1210 | array_needsize (timers, timermax, timercnt, ); |
1332 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1211 | timers [timercnt - 1] = w; |
1333 | timers [timercnt - 1] = w; |
1212 | upheap ((WT *)timers, timercnt - 1); |
1334 | upheap ((WT *)timers, timercnt - 1); |
1213 | |
1335 | |
1214 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1336 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1215 | } |
1337 | } |
… | |
… | |
1227 | { |
1349 | { |
1228 | timers [((W)w)->active - 1] = timers [timercnt]; |
1350 | timers [((W)w)->active - 1] = timers [timercnt]; |
1229 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1351 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1230 | } |
1352 | } |
1231 | |
1353 | |
1232 | ((WT)w)->at = w->repeat; |
1354 | ((WT)w)->at -= mn_now; |
1233 | |
1355 | |
1234 | ev_stop (EV_A_ (W)w); |
1356 | ev_stop (EV_A_ (W)w); |
1235 | } |
1357 | } |
1236 | |
1358 | |
1237 | void |
1359 | void |
1238 | ev_timer_again (EV_P_ struct ev_timer *w) |
1360 | ev_timer_again (EV_P_ struct ev_timer *w) |
1239 | { |
1361 | { |
1240 | if (ev_is_active (w)) |
1362 | if (ev_is_active (w)) |
1241 | { |
1363 | { |
1242 | if (w->repeat) |
1364 | if (w->repeat) |
1243 | { |
|
|
1244 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1245 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1365 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1246 | } |
|
|
1247 | else |
1366 | else |
1248 | ev_timer_stop (EV_A_ w); |
1367 | ev_timer_stop (EV_A_ w); |
1249 | } |
1368 | } |
1250 | else if (w->repeat) |
1369 | else if (w->repeat) |
1251 | ev_timer_start (EV_A_ w); |
1370 | ev_timer_start (EV_A_ w); |
1252 | } |
1371 | } |
1253 | |
1372 | |
|
|
1373 | #if EV_PERIODICS |
1254 | void |
1374 | void |
1255 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1375 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1256 | { |
1376 | { |
1257 | if (ev_is_active (w)) |
1377 | if (ev_is_active (w)) |
1258 | return; |
1378 | return; |
1259 | |
1379 | |
|
|
1380 | if (w->reschedule_cb) |
|
|
1381 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
|
|
1382 | else if (w->interval) |
|
|
1383 | { |
1260 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1384 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1261 | |
|
|
1262 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1385 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1263 | if (w->interval) |
|
|
1264 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1386 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1387 | } |
1265 | |
1388 | |
1266 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1389 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1267 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1390 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1268 | periodics [periodiccnt - 1] = w; |
1391 | periodics [periodiccnt - 1] = w; |
1269 | upheap ((WT *)periodics, periodiccnt - 1); |
1392 | upheap ((WT *)periodics, periodiccnt - 1); |
1270 | |
1393 | |
1271 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1394 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1272 | } |
1395 | } |
… | |
… | |
1288 | |
1411 | |
1289 | ev_stop (EV_A_ (W)w); |
1412 | ev_stop (EV_A_ (W)w); |
1290 | } |
1413 | } |
1291 | |
1414 | |
1292 | void |
1415 | void |
|
|
1416 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1417 | { |
|
|
1418 | /* TODO: use adjustheap and recalculation */ |
|
|
1419 | ev_periodic_stop (EV_A_ w); |
|
|
1420 | ev_periodic_start (EV_A_ w); |
|
|
1421 | } |
|
|
1422 | #endif |
|
|
1423 | |
|
|
1424 | void |
1293 | ev_idle_start (EV_P_ struct ev_idle *w) |
1425 | ev_idle_start (EV_P_ struct ev_idle *w) |
1294 | { |
1426 | { |
1295 | if (ev_is_active (w)) |
1427 | if (ev_is_active (w)) |
1296 | return; |
1428 | return; |
1297 | |
1429 | |
1298 | ev_start (EV_A_ (W)w, ++idlecnt); |
1430 | ev_start (EV_A_ (W)w, ++idlecnt); |
1299 | array_needsize (idles, idlemax, idlecnt, ); |
1431 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1300 | idles [idlecnt - 1] = w; |
1432 | idles [idlecnt - 1] = w; |
1301 | } |
1433 | } |
1302 | |
1434 | |
1303 | void |
1435 | void |
1304 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1436 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1305 | { |
1437 | { |
1306 | ev_clear_pending (EV_A_ (W)w); |
1438 | ev_clear_pending (EV_A_ (W)w); |
1307 | if (ev_is_active (w)) |
1439 | if (!ev_is_active (w)) |
1308 | return; |
1440 | return; |
1309 | |
1441 | |
1310 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1442 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1311 | ev_stop (EV_A_ (W)w); |
1443 | ev_stop (EV_A_ (W)w); |
1312 | } |
1444 | } |
… | |
… | |
1316 | { |
1448 | { |
1317 | if (ev_is_active (w)) |
1449 | if (ev_is_active (w)) |
1318 | return; |
1450 | return; |
1319 | |
1451 | |
1320 | ev_start (EV_A_ (W)w, ++preparecnt); |
1452 | ev_start (EV_A_ (W)w, ++preparecnt); |
1321 | array_needsize (prepares, preparemax, preparecnt, ); |
1453 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1322 | prepares [preparecnt - 1] = w; |
1454 | prepares [preparecnt - 1] = w; |
1323 | } |
1455 | } |
1324 | |
1456 | |
1325 | void |
1457 | void |
1326 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1458 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1327 | { |
1459 | { |
1328 | ev_clear_pending (EV_A_ (W)w); |
1460 | ev_clear_pending (EV_A_ (W)w); |
1329 | if (ev_is_active (w)) |
1461 | if (!ev_is_active (w)) |
1330 | return; |
1462 | return; |
1331 | |
1463 | |
1332 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1464 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1333 | ev_stop (EV_A_ (W)w); |
1465 | ev_stop (EV_A_ (W)w); |
1334 | } |
1466 | } |
… | |
… | |
1338 | { |
1470 | { |
1339 | if (ev_is_active (w)) |
1471 | if (ev_is_active (w)) |
1340 | return; |
1472 | return; |
1341 | |
1473 | |
1342 | ev_start (EV_A_ (W)w, ++checkcnt); |
1474 | ev_start (EV_A_ (W)w, ++checkcnt); |
1343 | array_needsize (checks, checkmax, checkcnt, ); |
1475 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1344 | checks [checkcnt - 1] = w; |
1476 | checks [checkcnt - 1] = w; |
1345 | } |
1477 | } |
1346 | |
1478 | |
1347 | void |
1479 | void |
1348 | ev_check_stop (EV_P_ struct ev_check *w) |
1480 | ev_check_stop (EV_P_ struct ev_check *w) |
1349 | { |
1481 | { |
1350 | ev_clear_pending (EV_A_ (W)w); |
1482 | ev_clear_pending (EV_A_ (W)w); |
1351 | if (ev_is_active (w)) |
1483 | if (!ev_is_active (w)) |
1352 | return; |
1484 | return; |
1353 | |
1485 | |
1354 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1486 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1355 | ev_stop (EV_A_ (W)w); |
1487 | ev_stop (EV_A_ (W)w); |
1356 | } |
1488 | } |
… | |
… | |
1369 | return; |
1501 | return; |
1370 | |
1502 | |
1371 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1503 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1372 | |
1504 | |
1373 | ev_start (EV_A_ (W)w, 1); |
1505 | ev_start (EV_A_ (W)w, 1); |
1374 | array_needsize (signals, signalmax, w->signum, signals_init); |
1506 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1375 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1507 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1376 | |
1508 | |
1377 | if (!((WL)w)->next) |
1509 | if (!((WL)w)->next) |
1378 | { |
1510 | { |
1379 | #if WIN32 |
1511 | #if WIN32 |
… | |
… | |
1462 | } |
1594 | } |
1463 | |
1595 | |
1464 | void |
1596 | void |
1465 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1597 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1466 | { |
1598 | { |
1467 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1599 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1468 | |
1600 | |
1469 | if (!once) |
1601 | if (!once) |
1470 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1602 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1471 | else |
1603 | else |
1472 | { |
1604 | { |
1473 | once->cb = cb; |
1605 | once->cb = cb; |
1474 | once->arg = arg; |
1606 | once->arg = arg; |
1475 | |
1607 | |
1476 | ev_watcher_init (&once->io, once_cb_io); |
1608 | ev_init (&once->io, once_cb_io); |
1477 | if (fd >= 0) |
1609 | if (fd >= 0) |
1478 | { |
1610 | { |
1479 | ev_io_set (&once->io, fd, events); |
1611 | ev_io_set (&once->io, fd, events); |
1480 | ev_io_start (EV_A_ &once->io); |
1612 | ev_io_start (EV_A_ &once->io); |
1481 | } |
1613 | } |
1482 | |
1614 | |
1483 | ev_watcher_init (&once->to, once_cb_to); |
1615 | ev_init (&once->to, once_cb_to); |
1484 | if (timeout >= 0.) |
1616 | if (timeout >= 0.) |
1485 | { |
1617 | { |
1486 | ev_timer_set (&once->to, timeout, 0.); |
1618 | ev_timer_set (&once->to, timeout, 0.); |
1487 | ev_timer_start (EV_A_ &once->to); |
1619 | ev_timer_start (EV_A_ &once->to); |
1488 | } |
1620 | } |
1489 | } |
1621 | } |
1490 | } |
1622 | } |
1491 | |
1623 | |
|
|
1624 | #ifdef __cplusplus |
|
|
1625 | } |
|
|
1626 | #endif |
|
|
1627 | |