… | |
… | |
54 | |
54 | |
55 | #endif |
55 | #endif |
56 | |
56 | |
57 | #include <math.h> |
57 | #include <math.h> |
58 | #include <stdlib.h> |
58 | #include <stdlib.h> |
59 | #include <unistd.h> |
|
|
60 | #include <fcntl.h> |
59 | #include <fcntl.h> |
61 | #include <signal.h> |
|
|
62 | #include <stddef.h> |
60 | #include <stddef.h> |
63 | |
61 | |
64 | #include <stdio.h> |
62 | #include <stdio.h> |
65 | |
63 | |
66 | #include <assert.h> |
64 | #include <assert.h> |
67 | #include <errno.h> |
65 | #include <errno.h> |
68 | #include <sys/types.h> |
66 | #include <sys/types.h> |
|
|
67 | #include <time.h> |
|
|
68 | |
|
|
69 | #include <signal.h> |
|
|
70 | |
69 | #ifndef WIN32 |
71 | #ifndef WIN32 |
|
|
72 | # include <unistd.h> |
|
|
73 | # include <sys/time.h> |
70 | # include <sys/wait.h> |
74 | # include <sys/wait.h> |
71 | #endif |
75 | #endif |
72 | #include <sys/time.h> |
|
|
73 | #include <time.h> |
|
|
74 | |
|
|
75 | /**/ |
76 | /**/ |
76 | |
77 | |
77 | #ifndef EV_USE_MONOTONIC |
78 | #ifndef EV_USE_MONOTONIC |
78 | # define EV_USE_MONOTONIC 1 |
79 | # define EV_USE_MONOTONIC 1 |
79 | #endif |
80 | #endif |
… | |
… | |
90 | # define EV_USE_EPOLL 0 |
91 | # define EV_USE_EPOLL 0 |
91 | #endif |
92 | #endif |
92 | |
93 | |
93 | #ifndef EV_USE_KQUEUE |
94 | #ifndef EV_USE_KQUEUE |
94 | # define EV_USE_KQUEUE 0 |
95 | # define EV_USE_KQUEUE 0 |
|
|
96 | #endif |
|
|
97 | |
|
|
98 | #ifndef EV_USE_WIN32 |
|
|
99 | # ifdef WIN32 |
|
|
100 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
101 | # undef EV_USE_SELECT |
|
|
102 | # define EV_USE_SELECT 1 |
|
|
103 | # else |
|
|
104 | # define EV_USE_WIN32 0 |
|
|
105 | # endif |
95 | #endif |
106 | #endif |
96 | |
107 | |
97 | #ifndef EV_USE_REALTIME |
108 | #ifndef EV_USE_REALTIME |
98 | # define EV_USE_REALTIME 1 |
109 | # define EV_USE_REALTIME 1 |
99 | #endif |
110 | #endif |
… | |
… | |
137 | typedef struct ev_watcher_list *WL; |
148 | typedef struct ev_watcher_list *WL; |
138 | typedef struct ev_watcher_time *WT; |
149 | typedef struct ev_watcher_time *WT; |
139 | |
150 | |
140 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
141 | |
152 | |
|
|
153 | #include "ev_win32.c" |
|
|
154 | |
142 | /*****************************************************************************/ |
155 | /*****************************************************************************/ |
143 | |
156 | |
|
|
157 | static void (*syserr_cb)(const char *msg); |
|
|
158 | |
|
|
159 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
|
|
160 | { |
|
|
161 | syserr_cb = cb; |
|
|
162 | } |
|
|
163 | |
|
|
164 | static void |
|
|
165 | syserr (const char *msg) |
|
|
166 | { |
|
|
167 | if (!msg) |
|
|
168 | msg = "(libev) system error"; |
|
|
169 | |
|
|
170 | if (syserr_cb) |
|
|
171 | syserr_cb (msg); |
|
|
172 | else |
|
|
173 | { |
|
|
174 | perror (msg); |
|
|
175 | abort (); |
|
|
176 | } |
|
|
177 | } |
|
|
178 | |
|
|
179 | static void *(*alloc)(void *ptr, long size); |
|
|
180 | |
|
|
181 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
182 | { |
|
|
183 | alloc = cb; |
|
|
184 | } |
|
|
185 | |
|
|
186 | static void * |
|
|
187 | ev_realloc (void *ptr, long size) |
|
|
188 | { |
|
|
189 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
|
|
190 | |
|
|
191 | if (!ptr && size) |
|
|
192 | { |
|
|
193 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
194 | abort (); |
|
|
195 | } |
|
|
196 | |
|
|
197 | return ptr; |
|
|
198 | } |
|
|
199 | |
|
|
200 | #define ev_malloc(size) ev_realloc (0, (size)) |
|
|
201 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
|
|
202 | |
|
|
203 | /*****************************************************************************/ |
|
|
204 | |
144 | typedef struct |
205 | typedef struct |
145 | { |
206 | { |
146 | struct ev_watcher_list *head; |
207 | WL head; |
147 | unsigned char events; |
208 | unsigned char events; |
148 | unsigned char reify; |
209 | unsigned char reify; |
149 | } ANFD; |
210 | } ANFD; |
150 | |
211 | |
151 | typedef struct |
212 | typedef struct |
… | |
… | |
154 | int events; |
215 | int events; |
155 | } ANPENDING; |
216 | } ANPENDING; |
156 | |
217 | |
157 | #if EV_MULTIPLICITY |
218 | #if EV_MULTIPLICITY |
158 | |
219 | |
159 | struct ev_loop |
220 | struct ev_loop |
160 | { |
221 | { |
161 | # define VAR(name,decl) decl; |
222 | #define VAR(name,decl) decl; |
162 | # include "ev_vars.h" |
223 | #include "ev_vars.h" |
163 | }; |
|
|
164 | # undef VAR |
224 | #undef VAR |
|
|
225 | }; |
165 | # include "ev_wrap.h" |
226 | #include "ev_wrap.h" |
|
|
227 | |
|
|
228 | struct ev_loop default_loop_struct; |
|
|
229 | static struct ev_loop *default_loop; |
166 | |
230 | |
167 | #else |
231 | #else |
168 | |
232 | |
169 | # define VAR(name,decl) static decl; |
233 | #define VAR(name,decl) static decl; |
170 | # include "ev_vars.h" |
234 | #include "ev_vars.h" |
171 | # undef VAR |
235 | #undef VAR |
|
|
236 | |
|
|
237 | static int default_loop; |
172 | |
238 | |
173 | #endif |
239 | #endif |
174 | |
240 | |
175 | /*****************************************************************************/ |
241 | /*****************************************************************************/ |
176 | |
242 | |
… | |
… | |
207 | ev_now (EV_P) |
273 | ev_now (EV_P) |
208 | { |
274 | { |
209 | return rt_now; |
275 | return rt_now; |
210 | } |
276 | } |
211 | |
277 | |
212 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
278 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
213 | |
279 | |
214 | #define array_needsize(base,cur,cnt,init) \ |
280 | #define array_needsize(type,base,cur,cnt,init) \ |
215 | if (expect_false ((cnt) > cur)) \ |
281 | if (expect_false ((cnt) > cur)) \ |
216 | { \ |
282 | { \ |
217 | int newcnt = cur; \ |
283 | int newcnt = cur; \ |
218 | do \ |
284 | do \ |
219 | { \ |
285 | { \ |
220 | newcnt = array_roundsize (base, newcnt << 1); \ |
286 | newcnt = array_roundsize (type, newcnt << 1); \ |
221 | } \ |
287 | } \ |
222 | while ((cnt) > newcnt); \ |
288 | while ((cnt) > newcnt); \ |
223 | \ |
289 | \ |
224 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
290 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
225 | init (base + cur, newcnt - cur); \ |
291 | init (base + cur, newcnt - cur); \ |
226 | cur = newcnt; \ |
292 | cur = newcnt; \ |
227 | } |
293 | } |
|
|
294 | |
|
|
295 | #define array_slim(type,stem) \ |
|
|
296 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
297 | { \ |
|
|
298 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
299 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
|
|
300 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
301 | } |
|
|
302 | |
|
|
303 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
304 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
305 | #define array_free_microshit(stem) \ |
|
|
306 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
|
|
307 | |
|
|
308 | #define array_free(stem, idx) \ |
|
|
309 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
228 | |
310 | |
229 | /*****************************************************************************/ |
311 | /*****************************************************************************/ |
230 | |
312 | |
231 | static void |
313 | static void |
232 | anfds_init (ANFD *base, int count) |
314 | anfds_init (ANFD *base, int count) |
… | |
… | |
239 | |
321 | |
240 | ++base; |
322 | ++base; |
241 | } |
323 | } |
242 | } |
324 | } |
243 | |
325 | |
244 | static void |
326 | void |
245 | event (EV_P_ W w, int events) |
327 | ev_feed_event (EV_P_ void *w, int revents) |
246 | { |
328 | { |
|
|
329 | W w_ = (W)w; |
|
|
330 | |
247 | if (w->pending) |
331 | if (w_->pending) |
248 | { |
332 | { |
249 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
333 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
250 | return; |
334 | return; |
251 | } |
335 | } |
252 | |
336 | |
253 | w->pending = ++pendingcnt [ABSPRI (w)]; |
337 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
254 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
338 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
255 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
339 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
256 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
340 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
257 | } |
341 | } |
258 | |
342 | |
259 | static void |
343 | static void |
260 | queue_events (EV_P_ W *events, int eventcnt, int type) |
344 | queue_events (EV_P_ W *events, int eventcnt, int type) |
261 | { |
345 | { |
262 | int i; |
346 | int i; |
263 | |
347 | |
264 | for (i = 0; i < eventcnt; ++i) |
348 | for (i = 0; i < eventcnt; ++i) |
265 | event (EV_A_ events [i], type); |
349 | ev_feed_event (EV_A_ events [i], type); |
266 | } |
350 | } |
267 | |
351 | |
268 | static void |
352 | inline void |
269 | fd_event (EV_P_ int fd, int events) |
353 | fd_event (EV_P_ int fd, int revents) |
270 | { |
354 | { |
271 | ANFD *anfd = anfds + fd; |
355 | ANFD *anfd = anfds + fd; |
272 | struct ev_io *w; |
356 | struct ev_io *w; |
273 | |
357 | |
274 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
358 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
275 | { |
359 | { |
276 | int ev = w->events & events; |
360 | int ev = w->events & revents; |
277 | |
361 | |
278 | if (ev) |
362 | if (ev) |
279 | event (EV_A_ (W)w, ev); |
363 | ev_feed_event (EV_A_ (W)w, ev); |
280 | } |
364 | } |
|
|
365 | } |
|
|
366 | |
|
|
367 | void |
|
|
368 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
369 | { |
|
|
370 | fd_event (EV_A_ fd, revents); |
281 | } |
371 | } |
282 | |
372 | |
283 | /*****************************************************************************/ |
373 | /*****************************************************************************/ |
284 | |
374 | |
285 | static void |
375 | static void |
… | |
… | |
298 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
388 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
299 | events |= w->events; |
389 | events |= w->events; |
300 | |
390 | |
301 | anfd->reify = 0; |
391 | anfd->reify = 0; |
302 | |
392 | |
303 | if (anfd->events != events) |
|
|
304 | { |
|
|
305 | method_modify (EV_A_ fd, anfd->events, events); |
393 | method_modify (EV_A_ fd, anfd->events, events); |
306 | anfd->events = events; |
394 | anfd->events = events; |
307 | } |
|
|
308 | } |
395 | } |
309 | |
396 | |
310 | fdchangecnt = 0; |
397 | fdchangecnt = 0; |
311 | } |
398 | } |
312 | |
399 | |
313 | static void |
400 | static void |
314 | fd_change (EV_P_ int fd) |
401 | fd_change (EV_P_ int fd) |
315 | { |
402 | { |
316 | if (anfds [fd].reify || fdchangecnt < 0) |
403 | if (anfds [fd].reify) |
317 | return; |
404 | return; |
318 | |
405 | |
319 | anfds [fd].reify = 1; |
406 | anfds [fd].reify = 1; |
320 | |
407 | |
321 | ++fdchangecnt; |
408 | ++fdchangecnt; |
322 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
409 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
323 | fdchanges [fdchangecnt - 1] = fd; |
410 | fdchanges [fdchangecnt - 1] = fd; |
324 | } |
411 | } |
325 | |
412 | |
326 | static void |
413 | static void |
327 | fd_kill (EV_P_ int fd) |
414 | fd_kill (EV_P_ int fd) |
… | |
… | |
329 | struct ev_io *w; |
416 | struct ev_io *w; |
330 | |
417 | |
331 | while ((w = (struct ev_io *)anfds [fd].head)) |
418 | while ((w = (struct ev_io *)anfds [fd].head)) |
332 | { |
419 | { |
333 | ev_io_stop (EV_A_ w); |
420 | ev_io_stop (EV_A_ w); |
334 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
421 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
335 | } |
422 | } |
|
|
423 | } |
|
|
424 | |
|
|
425 | static int |
|
|
426 | fd_valid (int fd) |
|
|
427 | { |
|
|
428 | #ifdef WIN32 |
|
|
429 | return !!win32_get_osfhandle (fd); |
|
|
430 | #else |
|
|
431 | return fcntl (fd, F_GETFD) != -1; |
|
|
432 | #endif |
336 | } |
433 | } |
337 | |
434 | |
338 | /* called on EBADF to verify fds */ |
435 | /* called on EBADF to verify fds */ |
339 | static void |
436 | static void |
340 | fd_ebadf (EV_P) |
437 | fd_ebadf (EV_P) |
341 | { |
438 | { |
342 | int fd; |
439 | int fd; |
343 | |
440 | |
344 | for (fd = 0; fd < anfdmax; ++fd) |
441 | for (fd = 0; fd < anfdmax; ++fd) |
345 | if (anfds [fd].events) |
442 | if (anfds [fd].events) |
346 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
443 | if (!fd_valid (fd) == -1 && errno == EBADF) |
347 | fd_kill (EV_A_ fd); |
444 | fd_kill (EV_A_ fd); |
348 | } |
445 | } |
349 | |
446 | |
350 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
447 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
351 | static void |
448 | static void |
352 | fd_enomem (EV_P) |
449 | fd_enomem (EV_P) |
353 | { |
450 | { |
354 | int fd = anfdmax; |
451 | int fd; |
355 | |
452 | |
356 | while (fd--) |
453 | for (fd = anfdmax; fd--; ) |
357 | if (anfds [fd].events) |
454 | if (anfds [fd].events) |
358 | { |
455 | { |
359 | close (fd); |
|
|
360 | fd_kill (EV_A_ fd); |
456 | fd_kill (EV_A_ fd); |
361 | return; |
457 | return; |
362 | } |
458 | } |
363 | } |
459 | } |
364 | |
460 | |
365 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
461 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
366 | static void |
462 | static void |
367 | fd_rearm_all (EV_P) |
463 | fd_rearm_all (EV_P) |
368 | { |
464 | { |
369 | int fd; |
465 | int fd; |
370 | |
466 | |
… | |
… | |
385 | WT w = heap [k]; |
481 | WT w = heap [k]; |
386 | |
482 | |
387 | while (k && heap [k >> 1]->at > w->at) |
483 | while (k && heap [k >> 1]->at > w->at) |
388 | { |
484 | { |
389 | heap [k] = heap [k >> 1]; |
485 | heap [k] = heap [k >> 1]; |
390 | heap [k]->active = k + 1; |
486 | ((W)heap [k])->active = k + 1; |
391 | k >>= 1; |
487 | k >>= 1; |
392 | } |
488 | } |
393 | |
489 | |
394 | heap [k] = w; |
490 | heap [k] = w; |
395 | heap [k]->active = k + 1; |
491 | ((W)heap [k])->active = k + 1; |
396 | |
492 | |
397 | } |
493 | } |
398 | |
494 | |
399 | static void |
495 | static void |
400 | downheap (WT *heap, int N, int k) |
496 | downheap (WT *heap, int N, int k) |
… | |
… | |
410 | |
506 | |
411 | if (w->at <= heap [j]->at) |
507 | if (w->at <= heap [j]->at) |
412 | break; |
508 | break; |
413 | |
509 | |
414 | heap [k] = heap [j]; |
510 | heap [k] = heap [j]; |
415 | heap [k]->active = k + 1; |
511 | ((W)heap [k])->active = k + 1; |
416 | k = j; |
512 | k = j; |
417 | } |
513 | } |
418 | |
514 | |
419 | heap [k] = w; |
515 | heap [k] = w; |
420 | heap [k]->active = k + 1; |
516 | ((W)heap [k])->active = k + 1; |
421 | } |
517 | } |
422 | |
518 | |
423 | /*****************************************************************************/ |
519 | /*****************************************************************************/ |
424 | |
520 | |
425 | typedef struct |
521 | typedef struct |
426 | { |
522 | { |
427 | struct ev_watcher_list *head; |
523 | WL head; |
428 | sig_atomic_t volatile gotsig; |
524 | sig_atomic_t volatile gotsig; |
429 | } ANSIG; |
525 | } ANSIG; |
430 | |
526 | |
431 | static ANSIG *signals; |
527 | static ANSIG *signals; |
432 | static int signalmax; |
528 | static int signalmax; |
… | |
… | |
448 | } |
544 | } |
449 | |
545 | |
450 | static void |
546 | static void |
451 | sighandler (int signum) |
547 | sighandler (int signum) |
452 | { |
548 | { |
|
|
549 | #if WIN32 |
|
|
550 | signal (signum, sighandler); |
|
|
551 | #endif |
|
|
552 | |
453 | signals [signum - 1].gotsig = 1; |
553 | signals [signum - 1].gotsig = 1; |
454 | |
554 | |
455 | if (!gotsig) |
555 | if (!gotsig) |
456 | { |
556 | { |
457 | int old_errno = errno; |
557 | int old_errno = errno; |
458 | gotsig = 1; |
558 | gotsig = 1; |
|
|
559 | #ifdef WIN32 |
|
|
560 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
561 | #else |
459 | write (sigpipe [1], &signum, 1); |
562 | write (sigpipe [1], &signum, 1); |
|
|
563 | #endif |
460 | errno = old_errno; |
564 | errno = old_errno; |
461 | } |
565 | } |
462 | } |
566 | } |
463 | |
567 | |
|
|
568 | void |
|
|
569 | ev_feed_signal_event (EV_P_ int signum) |
|
|
570 | { |
|
|
571 | WL w; |
|
|
572 | |
|
|
573 | #if EV_MULTIPLICITY |
|
|
574 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
575 | #endif |
|
|
576 | |
|
|
577 | --signum; |
|
|
578 | |
|
|
579 | if (signum < 0 || signum >= signalmax) |
|
|
580 | return; |
|
|
581 | |
|
|
582 | signals [signum].gotsig = 0; |
|
|
583 | |
|
|
584 | for (w = signals [signum].head; w; w = w->next) |
|
|
585 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
586 | } |
|
|
587 | |
464 | static void |
588 | static void |
465 | sigcb (EV_P_ struct ev_io *iow, int revents) |
589 | sigcb (EV_P_ struct ev_io *iow, int revents) |
466 | { |
590 | { |
467 | struct ev_watcher_list *w; |
|
|
468 | int signum; |
591 | int signum; |
469 | |
592 | |
|
|
593 | #ifdef WIN32 |
|
|
594 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
595 | #else |
470 | read (sigpipe [0], &revents, 1); |
596 | read (sigpipe [0], &revents, 1); |
|
|
597 | #endif |
471 | gotsig = 0; |
598 | gotsig = 0; |
472 | |
599 | |
473 | for (signum = signalmax; signum--; ) |
600 | for (signum = signalmax; signum--; ) |
474 | if (signals [signum].gotsig) |
601 | if (signals [signum].gotsig) |
475 | { |
602 | ev_feed_signal_event (EV_A_ signum + 1); |
476 | signals [signum].gotsig = 0; |
|
|
477 | |
|
|
478 | for (w = signals [signum].head; w; w = w->next) |
|
|
479 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
480 | } |
|
|
481 | } |
603 | } |
482 | |
604 | |
483 | static void |
605 | static void |
484 | siginit (EV_P) |
606 | siginit (EV_P) |
485 | { |
607 | { |
… | |
… | |
497 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
619 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
498 | } |
620 | } |
499 | |
621 | |
500 | /*****************************************************************************/ |
622 | /*****************************************************************************/ |
501 | |
623 | |
|
|
624 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
625 | |
502 | #ifndef WIN32 |
626 | #ifndef WIN32 |
503 | |
627 | |
504 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
505 | static struct ev_signal childev; |
628 | static struct ev_signal childev; |
506 | |
629 | |
507 | #ifndef WCONTINUED |
630 | #ifndef WCONTINUED |
508 | # define WCONTINUED 0 |
631 | # define WCONTINUED 0 |
509 | #endif |
632 | #endif |
… | |
… | |
514 | struct ev_child *w; |
637 | struct ev_child *w; |
515 | |
638 | |
516 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
639 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
517 | if (w->pid == pid || !w->pid) |
640 | if (w->pid == pid || !w->pid) |
518 | { |
641 | { |
519 | w->priority = sw->priority; /* need to do it *now* */ |
642 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
520 | w->rpid = pid; |
643 | w->rpid = pid; |
521 | w->rstatus = status; |
644 | w->rstatus = status; |
522 | event (EV_A_ (W)w, EV_CHILD); |
645 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
523 | } |
646 | } |
524 | } |
647 | } |
525 | |
648 | |
526 | static void |
649 | static void |
527 | childcb (EV_P_ struct ev_signal *sw, int revents) |
650 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
529 | int pid, status; |
652 | int pid, status; |
530 | |
653 | |
531 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
654 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
532 | { |
655 | { |
533 | /* make sure we are called again until all childs have been reaped */ |
656 | /* make sure we are called again until all childs have been reaped */ |
534 | event (EV_A_ (W)sw, EV_SIGNAL); |
657 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
535 | |
658 | |
536 | child_reap (EV_A_ sw, pid, pid, status); |
659 | child_reap (EV_A_ sw, pid, pid, status); |
537 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
660 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
538 | } |
661 | } |
539 | } |
662 | } |
… | |
… | |
608 | methods = atoi (getenv ("LIBEV_METHODS")); |
731 | methods = atoi (getenv ("LIBEV_METHODS")); |
609 | else |
732 | else |
610 | methods = EVMETHOD_ANY; |
733 | methods = EVMETHOD_ANY; |
611 | |
734 | |
612 | method = 0; |
735 | method = 0; |
|
|
736 | #if EV_USE_WIN32 |
|
|
737 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
738 | #endif |
613 | #if EV_USE_KQUEUE |
739 | #if EV_USE_KQUEUE |
614 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
740 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
615 | #endif |
741 | #endif |
616 | #if EV_USE_EPOLL |
742 | #if EV_USE_EPOLL |
617 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
743 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
… | |
… | |
620 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
746 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
621 | #endif |
747 | #endif |
622 | #if EV_USE_SELECT |
748 | #if EV_USE_SELECT |
623 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
749 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
624 | #endif |
750 | #endif |
|
|
751 | |
|
|
752 | ev_watcher_init (&sigev, sigcb); |
|
|
753 | ev_set_priority (&sigev, EV_MAXPRI); |
625 | } |
754 | } |
626 | } |
755 | } |
627 | |
756 | |
628 | void |
757 | void |
629 | loop_destroy (EV_P) |
758 | loop_destroy (EV_P) |
630 | { |
759 | { |
|
|
760 | int i; |
|
|
761 | |
|
|
762 | #if EV_USE_WIN32 |
|
|
763 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
764 | #endif |
631 | #if EV_USE_KQUEUE |
765 | #if EV_USE_KQUEUE |
632 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
766 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
633 | #endif |
767 | #endif |
634 | #if EV_USE_EPOLL |
768 | #if EV_USE_EPOLL |
635 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
769 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
… | |
… | |
639 | #endif |
773 | #endif |
640 | #if EV_USE_SELECT |
774 | #if EV_USE_SELECT |
641 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
775 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
642 | #endif |
776 | #endif |
643 | |
777 | |
|
|
778 | for (i = NUMPRI; i--; ) |
|
|
779 | array_free (pending, [i]); |
|
|
780 | |
|
|
781 | /* have to use the microsoft-never-gets-it-right macro */ |
|
|
782 | array_free_microshit (fdchange); |
|
|
783 | array_free_microshit (timer); |
|
|
784 | array_free_microshit (periodic); |
|
|
785 | array_free_microshit (idle); |
|
|
786 | array_free_microshit (prepare); |
|
|
787 | array_free_microshit (check); |
|
|
788 | |
644 | method = 0; |
789 | method = 0; |
645 | /*TODO*/ |
|
|
646 | } |
790 | } |
647 | |
791 | |
648 | void |
792 | static void |
649 | loop_fork (EV_P) |
793 | loop_fork (EV_P) |
650 | { |
794 | { |
651 | /*TODO*/ |
|
|
652 | #if EV_USE_EPOLL |
795 | #if EV_USE_EPOLL |
653 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
796 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
654 | #endif |
797 | #endif |
655 | #if EV_USE_KQUEUE |
798 | #if EV_USE_KQUEUE |
656 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
799 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
657 | #endif |
800 | #endif |
|
|
801 | |
|
|
802 | if (ev_is_active (&sigev)) |
|
|
803 | { |
|
|
804 | /* default loop */ |
|
|
805 | |
|
|
806 | ev_ref (EV_A); |
|
|
807 | ev_io_stop (EV_A_ &sigev); |
|
|
808 | close (sigpipe [0]); |
|
|
809 | close (sigpipe [1]); |
|
|
810 | |
|
|
811 | while (pipe (sigpipe)) |
|
|
812 | syserr ("(libev) error creating pipe"); |
|
|
813 | |
|
|
814 | siginit (EV_A); |
|
|
815 | } |
|
|
816 | |
|
|
817 | postfork = 0; |
658 | } |
818 | } |
659 | |
819 | |
660 | #if EV_MULTIPLICITY |
820 | #if EV_MULTIPLICITY |
661 | struct ev_loop * |
821 | struct ev_loop * |
662 | ev_loop_new (int methods) |
822 | ev_loop_new (int methods) |
663 | { |
823 | { |
664 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
824 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
|
|
825 | |
|
|
826 | memset (loop, 0, sizeof (struct ev_loop)); |
665 | |
827 | |
666 | loop_init (EV_A_ methods); |
828 | loop_init (EV_A_ methods); |
667 | |
829 | |
668 | if (ev_method (EV_A)) |
830 | if (ev_method (EV_A)) |
669 | return loop; |
831 | return loop; |
… | |
… | |
673 | |
835 | |
674 | void |
836 | void |
675 | ev_loop_destroy (EV_P) |
837 | ev_loop_destroy (EV_P) |
676 | { |
838 | { |
677 | loop_destroy (EV_A); |
839 | loop_destroy (EV_A); |
678 | free (loop); |
840 | ev_free (loop); |
679 | } |
841 | } |
680 | |
842 | |
681 | void |
843 | void |
682 | ev_loop_fork (EV_P) |
844 | ev_loop_fork (EV_P) |
683 | { |
845 | { |
684 | loop_fork (EV_A); |
846 | postfork = 1; |
685 | } |
847 | } |
686 | |
848 | |
687 | #endif |
849 | #endif |
688 | |
850 | |
689 | #if EV_MULTIPLICITY |
851 | #if EV_MULTIPLICITY |
690 | struct ev_loop default_loop_struct; |
|
|
691 | static struct ev_loop *default_loop; |
|
|
692 | |
|
|
693 | struct ev_loop * |
852 | struct ev_loop * |
694 | #else |
853 | #else |
695 | static int default_loop; |
|
|
696 | |
|
|
697 | int |
854 | int |
698 | #endif |
855 | #endif |
699 | ev_default_loop (int methods) |
856 | ev_default_loop (int methods) |
700 | { |
857 | { |
701 | if (sigpipe [0] == sigpipe [1]) |
858 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
712 | |
869 | |
713 | loop_init (EV_A_ methods); |
870 | loop_init (EV_A_ methods); |
714 | |
871 | |
715 | if (ev_method (EV_A)) |
872 | if (ev_method (EV_A)) |
716 | { |
873 | { |
717 | ev_watcher_init (&sigev, sigcb); |
|
|
718 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
719 | siginit (EV_A); |
874 | siginit (EV_A); |
720 | |
875 | |
721 | #ifndef WIN32 |
876 | #ifndef WIN32 |
722 | ev_signal_init (&childev, childcb, SIGCHLD); |
877 | ev_signal_init (&childev, childcb, SIGCHLD); |
723 | ev_set_priority (&childev, EV_MAXPRI); |
878 | ev_set_priority (&childev, EV_MAXPRI); |
… | |
… | |
737 | { |
892 | { |
738 | #if EV_MULTIPLICITY |
893 | #if EV_MULTIPLICITY |
739 | struct ev_loop *loop = default_loop; |
894 | struct ev_loop *loop = default_loop; |
740 | #endif |
895 | #endif |
741 | |
896 | |
|
|
897 | #ifndef WIN32 |
742 | ev_ref (EV_A); /* child watcher */ |
898 | ev_ref (EV_A); /* child watcher */ |
743 | ev_signal_stop (EV_A_ &childev); |
899 | ev_signal_stop (EV_A_ &childev); |
|
|
900 | #endif |
744 | |
901 | |
745 | ev_ref (EV_A); /* signal watcher */ |
902 | ev_ref (EV_A); /* signal watcher */ |
746 | ev_io_stop (EV_A_ &sigev); |
903 | ev_io_stop (EV_A_ &sigev); |
747 | |
904 | |
748 | close (sigpipe [0]); sigpipe [0] = 0; |
905 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
756 | { |
913 | { |
757 | #if EV_MULTIPLICITY |
914 | #if EV_MULTIPLICITY |
758 | struct ev_loop *loop = default_loop; |
915 | struct ev_loop *loop = default_loop; |
759 | #endif |
916 | #endif |
760 | |
917 | |
761 | loop_fork (EV_A); |
918 | if (method) |
762 | |
919 | postfork = 1; |
763 | ev_io_stop (EV_A_ &sigev); |
|
|
764 | close (sigpipe [0]); |
|
|
765 | close (sigpipe [1]); |
|
|
766 | pipe (sigpipe); |
|
|
767 | |
|
|
768 | ev_ref (EV_A); /* signal watcher */ |
|
|
769 | siginit (EV_A); |
|
|
770 | } |
920 | } |
771 | |
921 | |
772 | /*****************************************************************************/ |
922 | /*****************************************************************************/ |
|
|
923 | |
|
|
924 | static int |
|
|
925 | any_pending (EV_P) |
|
|
926 | { |
|
|
927 | int pri; |
|
|
928 | |
|
|
929 | for (pri = NUMPRI; pri--; ) |
|
|
930 | if (pendingcnt [pri]) |
|
|
931 | return 1; |
|
|
932 | |
|
|
933 | return 0; |
|
|
934 | } |
773 | |
935 | |
774 | static void |
936 | static void |
775 | call_pending (EV_P) |
937 | call_pending (EV_P) |
776 | { |
938 | { |
777 | int pri; |
939 | int pri; |
… | |
… | |
790 | } |
952 | } |
791 | |
953 | |
792 | static void |
954 | static void |
793 | timers_reify (EV_P) |
955 | timers_reify (EV_P) |
794 | { |
956 | { |
795 | while (timercnt && timers [0]->at <= mn_now) |
957 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
796 | { |
958 | { |
797 | struct ev_timer *w = timers [0]; |
959 | struct ev_timer *w = timers [0]; |
|
|
960 | |
|
|
961 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
798 | |
962 | |
799 | /* first reschedule or stop timer */ |
963 | /* first reschedule or stop timer */ |
800 | if (w->repeat) |
964 | if (w->repeat) |
801 | { |
965 | { |
802 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
966 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
803 | w->at = mn_now + w->repeat; |
967 | ((WT)w)->at = mn_now + w->repeat; |
804 | downheap ((WT *)timers, timercnt, 0); |
968 | downheap ((WT *)timers, timercnt, 0); |
805 | } |
969 | } |
806 | else |
970 | else |
807 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
971 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
808 | |
972 | |
809 | event (EV_A_ (W)w, EV_TIMEOUT); |
973 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
810 | } |
974 | } |
811 | } |
975 | } |
812 | |
976 | |
813 | static void |
977 | static void |
814 | periodics_reify (EV_P) |
978 | periodics_reify (EV_P) |
815 | { |
979 | { |
816 | while (periodiccnt && periodics [0]->at <= rt_now) |
980 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
817 | { |
981 | { |
818 | struct ev_periodic *w = periodics [0]; |
982 | struct ev_periodic *w = periodics [0]; |
819 | |
983 | |
|
|
984 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
|
|
985 | |
820 | /* first reschedule or stop timer */ |
986 | /* first reschedule or stop timer */ |
821 | if (w->interval) |
987 | if (w->reschedule_cb) |
822 | { |
988 | { |
|
|
989 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
|
|
990 | |
|
|
991 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
|
|
992 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
993 | } |
|
|
994 | else if (w->interval) |
|
|
995 | { |
823 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
996 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
824 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
997 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
825 | downheap ((WT *)periodics, periodiccnt, 0); |
998 | downheap ((WT *)periodics, periodiccnt, 0); |
826 | } |
999 | } |
827 | else |
1000 | else |
828 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1001 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
829 | |
1002 | |
830 | event (EV_A_ (W)w, EV_PERIODIC); |
1003 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
831 | } |
1004 | } |
832 | } |
1005 | } |
833 | |
1006 | |
834 | static void |
1007 | static void |
835 | periodics_reschedule (EV_P) |
1008 | periodics_reschedule (EV_P) |
… | |
… | |
839 | /* adjust periodics after time jump */ |
1012 | /* adjust periodics after time jump */ |
840 | for (i = 0; i < periodiccnt; ++i) |
1013 | for (i = 0; i < periodiccnt; ++i) |
841 | { |
1014 | { |
842 | struct ev_periodic *w = periodics [i]; |
1015 | struct ev_periodic *w = periodics [i]; |
843 | |
1016 | |
|
|
1017 | if (w->reschedule_cb) |
|
|
1018 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
844 | if (w->interval) |
1019 | else if (w->interval) |
845 | { |
|
|
846 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
1020 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
847 | |
|
|
848 | if (fabs (diff) >= 1e-4) |
|
|
849 | { |
|
|
850 | ev_periodic_stop (EV_A_ w); |
|
|
851 | ev_periodic_start (EV_A_ w); |
|
|
852 | |
|
|
853 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
854 | } |
|
|
855 | } |
|
|
856 | } |
1021 | } |
|
|
1022 | |
|
|
1023 | /* now rebuild the heap */ |
|
|
1024 | for (i = periodiccnt >> 1; i--; ) |
|
|
1025 | downheap ((WT *)periodics, periodiccnt, i); |
857 | } |
1026 | } |
858 | |
1027 | |
859 | inline int |
1028 | inline int |
860 | time_update_monotonic (EV_P) |
1029 | time_update_monotonic (EV_P) |
861 | { |
1030 | { |
… | |
… | |
912 | { |
1081 | { |
913 | periodics_reschedule (EV_A); |
1082 | periodics_reschedule (EV_A); |
914 | |
1083 | |
915 | /* adjust timers. this is easy, as the offset is the same for all */ |
1084 | /* adjust timers. this is easy, as the offset is the same for all */ |
916 | for (i = 0; i < timercnt; ++i) |
1085 | for (i = 0; i < timercnt; ++i) |
917 | timers [i]->at += rt_now - mn_now; |
1086 | ((WT)timers [i])->at += rt_now - mn_now; |
918 | } |
1087 | } |
919 | |
1088 | |
920 | mn_now = rt_now; |
1089 | mn_now = rt_now; |
921 | } |
1090 | } |
922 | } |
1091 | } |
… | |
… | |
948 | { |
1117 | { |
949 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1118 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
950 | call_pending (EV_A); |
1119 | call_pending (EV_A); |
951 | } |
1120 | } |
952 | |
1121 | |
|
|
1122 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1123 | if (expect_false (postfork)) |
|
|
1124 | loop_fork (EV_A); |
|
|
1125 | |
953 | /* update fd-related kernel structures */ |
1126 | /* update fd-related kernel structures */ |
954 | fd_reify (EV_A); |
1127 | fd_reify (EV_A); |
955 | |
1128 | |
956 | /* calculate blocking time */ |
1129 | /* calculate blocking time */ |
957 | |
1130 | |
958 | /* we only need this for !monotonic clockor timers, but as we basically |
1131 | /* we only need this for !monotonic clock or timers, but as we basically |
959 | always have timers, we just calculate it always */ |
1132 | always have timers, we just calculate it always */ |
960 | #if EV_USE_MONOTONIC |
1133 | #if EV_USE_MONOTONIC |
961 | if (expect_true (have_monotonic)) |
1134 | if (expect_true (have_monotonic)) |
962 | time_update_monotonic (EV_A); |
1135 | time_update_monotonic (EV_A); |
963 | else |
1136 | else |
… | |
… | |
973 | { |
1146 | { |
974 | block = MAX_BLOCKTIME; |
1147 | block = MAX_BLOCKTIME; |
975 | |
1148 | |
976 | if (timercnt) |
1149 | if (timercnt) |
977 | { |
1150 | { |
978 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1151 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
979 | if (block > to) block = to; |
1152 | if (block > to) block = to; |
980 | } |
1153 | } |
981 | |
1154 | |
982 | if (periodiccnt) |
1155 | if (periodiccnt) |
983 | { |
1156 | { |
984 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1157 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
985 | if (block > to) block = to; |
1158 | if (block > to) block = to; |
986 | } |
1159 | } |
987 | |
1160 | |
988 | if (block < 0.) block = 0.; |
1161 | if (block < 0.) block = 0.; |
989 | } |
1162 | } |
… | |
… | |
996 | /* queue pending timers and reschedule them */ |
1169 | /* queue pending timers and reschedule them */ |
997 | timers_reify (EV_A); /* relative timers called last */ |
1170 | timers_reify (EV_A); /* relative timers called last */ |
998 | periodics_reify (EV_A); /* absolute timers called first */ |
1171 | periodics_reify (EV_A); /* absolute timers called first */ |
999 | |
1172 | |
1000 | /* queue idle watchers unless io or timers are pending */ |
1173 | /* queue idle watchers unless io or timers are pending */ |
1001 | if (!pendingcnt) |
1174 | if (idlecnt && !any_pending (EV_A)) |
1002 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1175 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1003 | |
1176 | |
1004 | /* queue check watchers, to be executed first */ |
1177 | /* queue check watchers, to be executed first */ |
1005 | if (checkcnt) |
1178 | if (checkcnt) |
1006 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1179 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1081 | return; |
1254 | return; |
1082 | |
1255 | |
1083 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1256 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1084 | |
1257 | |
1085 | ev_start (EV_A_ (W)w, 1); |
1258 | ev_start (EV_A_ (W)w, 1); |
1086 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1259 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1087 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1260 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1088 | |
1261 | |
1089 | fd_change (EV_A_ fd); |
1262 | fd_change (EV_A_ fd); |
1090 | } |
1263 | } |
1091 | |
1264 | |
… | |
… | |
1106 | ev_timer_start (EV_P_ struct ev_timer *w) |
1279 | ev_timer_start (EV_P_ struct ev_timer *w) |
1107 | { |
1280 | { |
1108 | if (ev_is_active (w)) |
1281 | if (ev_is_active (w)) |
1109 | return; |
1282 | return; |
1110 | |
1283 | |
1111 | w->at += mn_now; |
1284 | ((WT)w)->at += mn_now; |
1112 | |
1285 | |
1113 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1286 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1114 | |
1287 | |
1115 | ev_start (EV_A_ (W)w, ++timercnt); |
1288 | ev_start (EV_A_ (W)w, ++timercnt); |
1116 | array_needsize (timers, timermax, timercnt, ); |
1289 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1117 | timers [timercnt - 1] = w; |
1290 | timers [timercnt - 1] = w; |
1118 | upheap ((WT *)timers, timercnt - 1); |
1291 | upheap ((WT *)timers, timercnt - 1); |
|
|
1292 | |
|
|
1293 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1119 | } |
1294 | } |
1120 | |
1295 | |
1121 | void |
1296 | void |
1122 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1297 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1123 | { |
1298 | { |
1124 | ev_clear_pending (EV_A_ (W)w); |
1299 | ev_clear_pending (EV_A_ (W)w); |
1125 | if (!ev_is_active (w)) |
1300 | if (!ev_is_active (w)) |
1126 | return; |
1301 | return; |
1127 | |
1302 | |
|
|
1303 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1304 | |
1128 | if (w->active < timercnt--) |
1305 | if (((W)w)->active < timercnt--) |
1129 | { |
1306 | { |
1130 | timers [w->active - 1] = timers [timercnt]; |
1307 | timers [((W)w)->active - 1] = timers [timercnt]; |
1131 | downheap ((WT *)timers, timercnt, w->active - 1); |
1308 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1132 | } |
1309 | } |
1133 | |
1310 | |
1134 | w->at = w->repeat; |
1311 | ((WT)w)->at = w->repeat; |
1135 | |
1312 | |
1136 | ev_stop (EV_A_ (W)w); |
1313 | ev_stop (EV_A_ (W)w); |
1137 | } |
1314 | } |
1138 | |
1315 | |
1139 | void |
1316 | void |
… | |
… | |
1141 | { |
1318 | { |
1142 | if (ev_is_active (w)) |
1319 | if (ev_is_active (w)) |
1143 | { |
1320 | { |
1144 | if (w->repeat) |
1321 | if (w->repeat) |
1145 | { |
1322 | { |
1146 | w->at = mn_now + w->repeat; |
1323 | ((WT)w)->at = mn_now + w->repeat; |
1147 | downheap ((WT *)timers, timercnt, w->active - 1); |
1324 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1148 | } |
1325 | } |
1149 | else |
1326 | else |
1150 | ev_timer_stop (EV_A_ w); |
1327 | ev_timer_stop (EV_A_ w); |
1151 | } |
1328 | } |
1152 | else if (w->repeat) |
1329 | else if (w->repeat) |
… | |
… | |
1157 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1334 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1158 | { |
1335 | { |
1159 | if (ev_is_active (w)) |
1336 | if (ev_is_active (w)) |
1160 | return; |
1337 | return; |
1161 | |
1338 | |
|
|
1339 | if (w->reschedule_cb) |
|
|
1340 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
|
|
1341 | else if (w->interval) |
|
|
1342 | { |
1162 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1343 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1163 | |
|
|
1164 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1344 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1165 | if (w->interval) |
|
|
1166 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1345 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1346 | } |
1167 | |
1347 | |
1168 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1348 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1169 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1349 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1170 | periodics [periodiccnt - 1] = w; |
1350 | periodics [periodiccnt - 1] = w; |
1171 | upheap ((WT *)periodics, periodiccnt - 1); |
1351 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1352 | |
|
|
1353 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1172 | } |
1354 | } |
1173 | |
1355 | |
1174 | void |
1356 | void |
1175 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1357 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1176 | { |
1358 | { |
1177 | ev_clear_pending (EV_A_ (W)w); |
1359 | ev_clear_pending (EV_A_ (W)w); |
1178 | if (!ev_is_active (w)) |
1360 | if (!ev_is_active (w)) |
1179 | return; |
1361 | return; |
1180 | |
1362 | |
|
|
1363 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1364 | |
1181 | if (w->active < periodiccnt--) |
1365 | if (((W)w)->active < periodiccnt--) |
1182 | { |
1366 | { |
1183 | periodics [w->active - 1] = periodics [periodiccnt]; |
1367 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1184 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1368 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1185 | } |
1369 | } |
1186 | |
1370 | |
1187 | ev_stop (EV_A_ (W)w); |
1371 | ev_stop (EV_A_ (W)w); |
1188 | } |
1372 | } |
1189 | |
1373 | |
1190 | void |
1374 | void |
|
|
1375 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1376 | { |
|
|
1377 | ev_periodic_stop (EV_A_ w); |
|
|
1378 | ev_periodic_start (EV_A_ w); |
|
|
1379 | } |
|
|
1380 | |
|
|
1381 | void |
1191 | ev_idle_start (EV_P_ struct ev_idle *w) |
1382 | ev_idle_start (EV_P_ struct ev_idle *w) |
1192 | { |
1383 | { |
1193 | if (ev_is_active (w)) |
1384 | if (ev_is_active (w)) |
1194 | return; |
1385 | return; |
1195 | |
1386 | |
1196 | ev_start (EV_A_ (W)w, ++idlecnt); |
1387 | ev_start (EV_A_ (W)w, ++idlecnt); |
1197 | array_needsize (idles, idlemax, idlecnt, ); |
1388 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1198 | idles [idlecnt - 1] = w; |
1389 | idles [idlecnt - 1] = w; |
1199 | } |
1390 | } |
1200 | |
1391 | |
1201 | void |
1392 | void |
1202 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1393 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1203 | { |
1394 | { |
1204 | ev_clear_pending (EV_A_ (W)w); |
1395 | ev_clear_pending (EV_A_ (W)w); |
1205 | if (ev_is_active (w)) |
1396 | if (ev_is_active (w)) |
1206 | return; |
1397 | return; |
1207 | |
1398 | |
1208 | idles [w->active - 1] = idles [--idlecnt]; |
1399 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1209 | ev_stop (EV_A_ (W)w); |
1400 | ev_stop (EV_A_ (W)w); |
1210 | } |
1401 | } |
1211 | |
1402 | |
1212 | void |
1403 | void |
1213 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1404 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1214 | { |
1405 | { |
1215 | if (ev_is_active (w)) |
1406 | if (ev_is_active (w)) |
1216 | return; |
1407 | return; |
1217 | |
1408 | |
1218 | ev_start (EV_A_ (W)w, ++preparecnt); |
1409 | ev_start (EV_A_ (W)w, ++preparecnt); |
1219 | array_needsize (prepares, preparemax, preparecnt, ); |
1410 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1220 | prepares [preparecnt - 1] = w; |
1411 | prepares [preparecnt - 1] = w; |
1221 | } |
1412 | } |
1222 | |
1413 | |
1223 | void |
1414 | void |
1224 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1415 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1225 | { |
1416 | { |
1226 | ev_clear_pending (EV_A_ (W)w); |
1417 | ev_clear_pending (EV_A_ (W)w); |
1227 | if (ev_is_active (w)) |
1418 | if (ev_is_active (w)) |
1228 | return; |
1419 | return; |
1229 | |
1420 | |
1230 | prepares [w->active - 1] = prepares [--preparecnt]; |
1421 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1231 | ev_stop (EV_A_ (W)w); |
1422 | ev_stop (EV_A_ (W)w); |
1232 | } |
1423 | } |
1233 | |
1424 | |
1234 | void |
1425 | void |
1235 | ev_check_start (EV_P_ struct ev_check *w) |
1426 | ev_check_start (EV_P_ struct ev_check *w) |
1236 | { |
1427 | { |
1237 | if (ev_is_active (w)) |
1428 | if (ev_is_active (w)) |
1238 | return; |
1429 | return; |
1239 | |
1430 | |
1240 | ev_start (EV_A_ (W)w, ++checkcnt); |
1431 | ev_start (EV_A_ (W)w, ++checkcnt); |
1241 | array_needsize (checks, checkmax, checkcnt, ); |
1432 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1242 | checks [checkcnt - 1] = w; |
1433 | checks [checkcnt - 1] = w; |
1243 | } |
1434 | } |
1244 | |
1435 | |
1245 | void |
1436 | void |
1246 | ev_check_stop (EV_P_ struct ev_check *w) |
1437 | ev_check_stop (EV_P_ struct ev_check *w) |
1247 | { |
1438 | { |
1248 | ev_clear_pending (EV_A_ (W)w); |
1439 | ev_clear_pending (EV_A_ (W)w); |
1249 | if (ev_is_active (w)) |
1440 | if (ev_is_active (w)) |
1250 | return; |
1441 | return; |
1251 | |
1442 | |
1252 | checks [w->active - 1] = checks [--checkcnt]; |
1443 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1253 | ev_stop (EV_A_ (W)w); |
1444 | ev_stop (EV_A_ (W)w); |
1254 | } |
1445 | } |
1255 | |
1446 | |
1256 | #ifndef SA_RESTART |
1447 | #ifndef SA_RESTART |
1257 | # define SA_RESTART 0 |
1448 | # define SA_RESTART 0 |
… | |
… | |
1267 | return; |
1458 | return; |
1268 | |
1459 | |
1269 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1460 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1270 | |
1461 | |
1271 | ev_start (EV_A_ (W)w, 1); |
1462 | ev_start (EV_A_ (W)w, 1); |
1272 | array_needsize (signals, signalmax, w->signum, signals_init); |
1463 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1273 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1464 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1274 | |
1465 | |
1275 | if (!w->next) |
1466 | if (!((WL)w)->next) |
1276 | { |
1467 | { |
|
|
1468 | #if WIN32 |
|
|
1469 | signal (w->signum, sighandler); |
|
|
1470 | #else |
1277 | struct sigaction sa; |
1471 | struct sigaction sa; |
1278 | sa.sa_handler = sighandler; |
1472 | sa.sa_handler = sighandler; |
1279 | sigfillset (&sa.sa_mask); |
1473 | sigfillset (&sa.sa_mask); |
1280 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1474 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1281 | sigaction (w->signum, &sa, 0); |
1475 | sigaction (w->signum, &sa, 0); |
|
|
1476 | #endif |
1282 | } |
1477 | } |
1283 | } |
1478 | } |
1284 | |
1479 | |
1285 | void |
1480 | void |
1286 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1481 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1336 | void (*cb)(int revents, void *arg) = once->cb; |
1531 | void (*cb)(int revents, void *arg) = once->cb; |
1337 | void *arg = once->arg; |
1532 | void *arg = once->arg; |
1338 | |
1533 | |
1339 | ev_io_stop (EV_A_ &once->io); |
1534 | ev_io_stop (EV_A_ &once->io); |
1340 | ev_timer_stop (EV_A_ &once->to); |
1535 | ev_timer_stop (EV_A_ &once->to); |
1341 | free (once); |
1536 | ev_free (once); |
1342 | |
1537 | |
1343 | cb (revents, arg); |
1538 | cb (revents, arg); |
1344 | } |
1539 | } |
1345 | |
1540 | |
1346 | static void |
1541 | static void |
… | |
… | |
1356 | } |
1551 | } |
1357 | |
1552 | |
1358 | void |
1553 | void |
1359 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1554 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1360 | { |
1555 | { |
1361 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1556 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1362 | |
1557 | |
1363 | if (!once) |
1558 | if (!once) |
1364 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1559 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1365 | else |
1560 | else |
1366 | { |
1561 | { |