… | |
… | |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | #ifndef EV_STANDALONE |
31 | #ifndef EV_STANDALONE |
32 | # include "config.h" |
32 | # include "config.h" |
|
|
33 | |
|
|
34 | # if HAVE_CLOCK_GETTIME |
|
|
35 | # define EV_USE_MONOTONIC 1 |
|
|
36 | # define EV_USE_REALTIME 1 |
|
|
37 | # endif |
|
|
38 | |
|
|
39 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
|
|
40 | # define EV_USE_SELECT 1 |
|
|
41 | # endif |
|
|
42 | |
|
|
43 | # if HAVE_POLL && HAVE_POLL_H |
|
|
44 | # define EV_USE_POLL 1 |
|
|
45 | # endif |
|
|
46 | |
|
|
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
|
|
48 | # define EV_USE_EPOLL 1 |
|
|
49 | # endif |
|
|
50 | |
|
|
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
|
|
52 | # define EV_USE_KQUEUE 1 |
|
|
53 | # endif |
|
|
54 | |
33 | #endif |
55 | #endif |
34 | |
56 | |
35 | #include <math.h> |
57 | #include <math.h> |
36 | #include <stdlib.h> |
58 | #include <stdlib.h> |
37 | #include <unistd.h> |
|
|
38 | #include <fcntl.h> |
59 | #include <fcntl.h> |
39 | #include <signal.h> |
|
|
40 | #include <stddef.h> |
60 | #include <stddef.h> |
41 | |
61 | |
42 | #include <stdio.h> |
62 | #include <stdio.h> |
43 | |
63 | |
44 | #include <assert.h> |
64 | #include <assert.h> |
45 | #include <errno.h> |
65 | #include <errno.h> |
46 | #include <sys/types.h> |
66 | #include <sys/types.h> |
|
|
67 | #include <time.h> |
|
|
68 | |
|
|
69 | #ifndef PERL |
|
|
70 | # include <signal.h> |
|
|
71 | #endif |
|
|
72 | |
47 | #ifndef WIN32 |
73 | #ifndef WIN32 |
|
|
74 | # include <unistd.h> |
|
|
75 | # include <sys/time.h> |
48 | # include <sys/wait.h> |
76 | # include <sys/wait.h> |
49 | #endif |
77 | #endif |
50 | #include <sys/time.h> |
|
|
51 | #include <time.h> |
|
|
52 | |
|
|
53 | /**/ |
78 | /**/ |
54 | |
79 | |
55 | #ifndef EV_USE_MONOTONIC |
80 | #ifndef EV_USE_MONOTONIC |
56 | # define EV_USE_MONOTONIC 1 |
81 | # define EV_USE_MONOTONIC 1 |
57 | #endif |
82 | #endif |
58 | |
83 | |
59 | #ifndef EV_USE_SELECT |
84 | #ifndef EV_USE_SELECT |
60 | # define EV_USE_SELECT 1 |
85 | # define EV_USE_SELECT 1 |
61 | #endif |
86 | #endif |
62 | |
87 | |
63 | #ifndef EV_USEV_POLL |
88 | #ifndef EV_USE_POLL |
64 | # define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ |
89 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
65 | #endif |
90 | #endif |
66 | |
91 | |
67 | #ifndef EV_USE_EPOLL |
92 | #ifndef EV_USE_EPOLL |
68 | # define EV_USE_EPOLL 0 |
93 | # define EV_USE_EPOLL 0 |
69 | #endif |
94 | #endif |
70 | |
95 | |
71 | #ifndef EV_USE_KQUEUE |
96 | #ifndef EV_USE_KQUEUE |
72 | # define EV_USE_KQUEUE 0 |
97 | # define EV_USE_KQUEUE 0 |
|
|
98 | #endif |
|
|
99 | |
|
|
100 | #ifndef EV_USE_WIN32 |
|
|
101 | # ifdef WIN32 |
|
|
102 | # define EV_USE_WIN32 0 /* it does not exist, use select */ |
|
|
103 | # undef EV_USE_SELECT |
|
|
104 | # define EV_USE_SELECT 1 |
|
|
105 | # else |
|
|
106 | # define EV_USE_WIN32 0 |
|
|
107 | # endif |
73 | #endif |
108 | #endif |
74 | |
109 | |
75 | #ifndef EV_USE_REALTIME |
110 | #ifndef EV_USE_REALTIME |
76 | # define EV_USE_REALTIME 1 |
111 | # define EV_USE_REALTIME 1 |
77 | #endif |
112 | #endif |
… | |
… | |
115 | typedef struct ev_watcher_list *WL; |
150 | typedef struct ev_watcher_list *WL; |
116 | typedef struct ev_watcher_time *WT; |
151 | typedef struct ev_watcher_time *WT; |
117 | |
152 | |
118 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
153 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
119 | |
154 | |
|
|
155 | #if WIN32 |
|
|
156 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
157 | /* MSDN says this is required to handle SIGFPE */ |
|
|
158 | volatile double SIGFPE_REQ = 0.0f; |
|
|
159 | #endif |
|
|
160 | |
120 | /*****************************************************************************/ |
161 | /*****************************************************************************/ |
121 | |
162 | |
|
|
163 | static void (*syserr_cb)(const char *msg); |
|
|
164 | |
|
|
165 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
|
|
166 | { |
|
|
167 | syserr_cb = cb; |
|
|
168 | } |
|
|
169 | |
|
|
170 | static void |
|
|
171 | syserr (const char *msg) |
|
|
172 | { |
|
|
173 | if (!msg) |
|
|
174 | msg = "(libev) system error"; |
|
|
175 | |
|
|
176 | if (syserr_cb) |
|
|
177 | syserr_cb (msg); |
|
|
178 | else |
|
|
179 | { |
|
|
180 | perror (msg); |
|
|
181 | abort (); |
|
|
182 | } |
|
|
183 | } |
|
|
184 | |
|
|
185 | static void *(*alloc)(void *ptr, long size); |
|
|
186 | |
|
|
187 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
188 | { |
|
|
189 | alloc = cb; |
|
|
190 | } |
|
|
191 | |
|
|
192 | static void * |
|
|
193 | ev_realloc (void *ptr, long size) |
|
|
194 | { |
|
|
195 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
|
|
196 | |
|
|
197 | if (!ptr && size) |
|
|
198 | { |
|
|
199 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
|
|
200 | abort (); |
|
|
201 | } |
|
|
202 | |
|
|
203 | return ptr; |
|
|
204 | } |
|
|
205 | |
|
|
206 | #define ev_malloc(size) ev_realloc (0, (size)) |
|
|
207 | #define ev_free(ptr) ev_realloc ((ptr), 0) |
|
|
208 | |
|
|
209 | /*****************************************************************************/ |
|
|
210 | |
122 | typedef struct |
211 | typedef struct |
123 | { |
212 | { |
124 | struct ev_watcher_list *head; |
213 | WL head; |
125 | unsigned char events; |
214 | unsigned char events; |
126 | unsigned char reify; |
215 | unsigned char reify; |
127 | } ANFD; |
216 | } ANFD; |
128 | |
217 | |
129 | typedef struct |
218 | typedef struct |
… | |
… | |
187 | return rt_now; |
276 | return rt_now; |
188 | } |
277 | } |
189 | |
278 | |
190 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
279 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
191 | |
280 | |
192 | #define array_needsize(base,cur,cnt,init) \ |
281 | #define array_needsize(base,cur,cnt,init) \ |
193 | if (expect_false ((cnt) > cur)) \ |
282 | if (expect_false ((cnt) > cur)) \ |
194 | { \ |
283 | { \ |
195 | int newcnt = cur; \ |
284 | int newcnt = cur; \ |
196 | do \ |
285 | do \ |
197 | { \ |
286 | { \ |
198 | newcnt = array_roundsize (base, newcnt << 1); \ |
287 | newcnt = array_roundsize (base, newcnt << 1); \ |
199 | } \ |
288 | } \ |
200 | while ((cnt) > newcnt); \ |
289 | while ((cnt) > newcnt); \ |
201 | \ |
290 | \ |
202 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
291 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
203 | init (base + cur, newcnt - cur); \ |
292 | init (base + cur, newcnt - cur); \ |
204 | cur = newcnt; \ |
293 | cur = newcnt; \ |
205 | } |
294 | } |
|
|
295 | |
|
|
296 | #define array_slim(stem) \ |
|
|
297 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
|
|
298 | { \ |
|
|
299 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
|
|
300 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
|
|
301 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
|
|
302 | } |
|
|
303 | |
|
|
304 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
305 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
306 | #define array_free_microshit(stem) \ |
|
|
307 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
|
|
308 | |
|
|
309 | #define array_free(stem, idx) \ |
|
|
310 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
206 | |
311 | |
207 | /*****************************************************************************/ |
312 | /*****************************************************************************/ |
208 | |
313 | |
209 | static void |
314 | static void |
210 | anfds_init (ANFD *base, int count) |
315 | anfds_init (ANFD *base, int count) |
… | |
… | |
227 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
332 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
228 | return; |
333 | return; |
229 | } |
334 | } |
230 | |
335 | |
231 | w->pending = ++pendingcnt [ABSPRI (w)]; |
336 | w->pending = ++pendingcnt [ABSPRI (w)]; |
232 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
337 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
233 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
338 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
234 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
339 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
235 | } |
340 | } |
236 | |
341 | |
237 | static void |
342 | static void |
… | |
… | |
276 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
381 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
277 | events |= w->events; |
382 | events |= w->events; |
278 | |
383 | |
279 | anfd->reify = 0; |
384 | anfd->reify = 0; |
280 | |
385 | |
281 | if (anfd->events != events) |
|
|
282 | { |
|
|
283 | method_modify (EV_A_ fd, anfd->events, events); |
386 | method_modify (EV_A_ fd, anfd->events, events); |
284 | anfd->events = events; |
387 | anfd->events = events; |
285 | } |
|
|
286 | } |
388 | } |
287 | |
389 | |
288 | fdchangecnt = 0; |
390 | fdchangecnt = 0; |
289 | } |
391 | } |
290 | |
392 | |
291 | static void |
393 | static void |
292 | fd_change (EV_P_ int fd) |
394 | fd_change (EV_P_ int fd) |
293 | { |
395 | { |
294 | if (anfds [fd].reify || fdchangecnt < 0) |
396 | if (anfds [fd].reify) |
295 | return; |
397 | return; |
296 | |
398 | |
297 | anfds [fd].reify = 1; |
399 | anfds [fd].reify = 1; |
298 | |
400 | |
299 | ++fdchangecnt; |
401 | ++fdchangecnt; |
300 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
402 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
301 | fdchanges [fdchangecnt - 1] = fd; |
403 | fdchanges [fdchangecnt - 1] = fd; |
302 | } |
404 | } |
303 | |
405 | |
304 | static void |
406 | static void |
305 | fd_kill (EV_P_ int fd) |
407 | fd_kill (EV_P_ int fd) |
… | |
… | |
311 | ev_io_stop (EV_A_ w); |
413 | ev_io_stop (EV_A_ w); |
312 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
414 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
313 | } |
415 | } |
314 | } |
416 | } |
315 | |
417 | |
|
|
418 | static int |
|
|
419 | fd_valid (int fd) |
|
|
420 | { |
|
|
421 | #ifdef WIN32 |
|
|
422 | return !!win32_get_osfhandle (fd); |
|
|
423 | #else |
|
|
424 | return fcntl (fd, F_GETFD) != -1; |
|
|
425 | #endif |
|
|
426 | } |
|
|
427 | |
316 | /* called on EBADF to verify fds */ |
428 | /* called on EBADF to verify fds */ |
317 | static void |
429 | static void |
318 | fd_ebadf (EV_P) |
430 | fd_ebadf (EV_P) |
319 | { |
431 | { |
320 | int fd; |
432 | int fd; |
321 | |
433 | |
322 | for (fd = 0; fd < anfdmax; ++fd) |
434 | for (fd = 0; fd < anfdmax; ++fd) |
323 | if (anfds [fd].events) |
435 | if (anfds [fd].events) |
324 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
436 | if (!fd_valid (fd) == -1 && errno == EBADF) |
325 | fd_kill (EV_A_ fd); |
437 | fd_kill (EV_A_ fd); |
326 | } |
438 | } |
327 | |
439 | |
328 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
440 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
329 | static void |
441 | static void |
330 | fd_enomem (EV_P) |
442 | fd_enomem (EV_P) |
331 | { |
443 | { |
332 | int fd = anfdmax; |
444 | int fd; |
333 | |
445 | |
334 | while (fd--) |
446 | for (fd = anfdmax; fd--; ) |
335 | if (anfds [fd].events) |
447 | if (anfds [fd].events) |
336 | { |
448 | { |
337 | close (fd); |
|
|
338 | fd_kill (EV_A_ fd); |
449 | fd_kill (EV_A_ fd); |
339 | return; |
450 | return; |
340 | } |
451 | } |
341 | } |
452 | } |
342 | |
453 | |
343 | /* susually called after fork if method needs to re-arm all fds from scratch */ |
454 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
344 | static void |
455 | static void |
345 | fd_rearm_all (EV_P) |
456 | fd_rearm_all (EV_P) |
346 | { |
457 | { |
347 | int fd; |
458 | int fd; |
348 | |
459 | |
349 | /* this should be highly optimised to not do anything but set a flag */ |
460 | /* this should be highly optimised to not do anything but set a flag */ |
350 | for (fd = 0; fd < anfdmax; ++fd) |
461 | for (fd = 0; fd < anfdmax; ++fd) |
351 | if (anfds [fd].events) |
462 | if (anfds [fd].events) |
352 | { |
463 | { |
353 | anfds [fd].events = 0; |
464 | anfds [fd].events = 0; |
354 | fd_change (fd); |
465 | fd_change (EV_A_ fd); |
355 | } |
466 | } |
356 | } |
467 | } |
357 | |
468 | |
358 | /*****************************************************************************/ |
469 | /*****************************************************************************/ |
359 | |
470 | |
… | |
… | |
363 | WT w = heap [k]; |
474 | WT w = heap [k]; |
364 | |
475 | |
365 | while (k && heap [k >> 1]->at > w->at) |
476 | while (k && heap [k >> 1]->at > w->at) |
366 | { |
477 | { |
367 | heap [k] = heap [k >> 1]; |
478 | heap [k] = heap [k >> 1]; |
368 | heap [k]->active = k + 1; |
479 | ((W)heap [k])->active = k + 1; |
369 | k >>= 1; |
480 | k >>= 1; |
370 | } |
481 | } |
371 | |
482 | |
372 | heap [k] = w; |
483 | heap [k] = w; |
373 | heap [k]->active = k + 1; |
484 | ((W)heap [k])->active = k + 1; |
374 | |
485 | |
375 | } |
486 | } |
376 | |
487 | |
377 | static void |
488 | static void |
378 | downheap (WT *heap, int N, int k) |
489 | downheap (WT *heap, int N, int k) |
… | |
… | |
388 | |
499 | |
389 | if (w->at <= heap [j]->at) |
500 | if (w->at <= heap [j]->at) |
390 | break; |
501 | break; |
391 | |
502 | |
392 | heap [k] = heap [j]; |
503 | heap [k] = heap [j]; |
393 | heap [k]->active = k + 1; |
504 | ((W)heap [k])->active = k + 1; |
394 | k = j; |
505 | k = j; |
395 | } |
506 | } |
396 | |
507 | |
397 | heap [k] = w; |
508 | heap [k] = w; |
398 | heap [k]->active = k + 1; |
509 | ((W)heap [k])->active = k + 1; |
399 | } |
510 | } |
400 | |
511 | |
401 | /*****************************************************************************/ |
512 | /*****************************************************************************/ |
402 | |
513 | |
403 | typedef struct |
514 | typedef struct |
404 | { |
515 | { |
405 | struct ev_watcher_list *head; |
516 | WL head; |
406 | sig_atomic_t volatile gotsig; |
517 | sig_atomic_t volatile gotsig; |
407 | } ANSIG; |
518 | } ANSIG; |
408 | |
519 | |
409 | static ANSIG *signals; |
520 | static ANSIG *signals; |
410 | static int signalmax; |
521 | static int signalmax; |
411 | |
522 | |
412 | static int sigpipe [2]; |
523 | static int sigpipe [2]; |
413 | static sig_atomic_t volatile gotsig; |
524 | static sig_atomic_t volatile gotsig; |
|
|
525 | static struct ev_io sigev; |
414 | |
526 | |
415 | static void |
527 | static void |
416 | signals_init (ANSIG *base, int count) |
528 | signals_init (ANSIG *base, int count) |
417 | { |
529 | { |
418 | while (count--) |
530 | while (count--) |
… | |
… | |
425 | } |
537 | } |
426 | |
538 | |
427 | static void |
539 | static void |
428 | sighandler (int signum) |
540 | sighandler (int signum) |
429 | { |
541 | { |
|
|
542 | #if WIN32 |
|
|
543 | signal (signum, sighandler); |
|
|
544 | #endif |
|
|
545 | |
430 | signals [signum - 1].gotsig = 1; |
546 | signals [signum - 1].gotsig = 1; |
431 | |
547 | |
432 | if (!gotsig) |
548 | if (!gotsig) |
433 | { |
549 | { |
434 | int old_errno = errno; |
550 | int old_errno = errno; |
… | |
… | |
439 | } |
555 | } |
440 | |
556 | |
441 | static void |
557 | static void |
442 | sigcb (EV_P_ struct ev_io *iow, int revents) |
558 | sigcb (EV_P_ struct ev_io *iow, int revents) |
443 | { |
559 | { |
444 | struct ev_watcher_list *w; |
560 | WL w; |
445 | int signum; |
561 | int signum; |
446 | |
562 | |
447 | read (sigpipe [0], &revents, 1); |
563 | read (sigpipe [0], &revents, 1); |
448 | gotsig = 0; |
564 | gotsig = 0; |
449 | |
565 | |
… | |
… | |
474 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
590 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
475 | } |
591 | } |
476 | |
592 | |
477 | /*****************************************************************************/ |
593 | /*****************************************************************************/ |
478 | |
594 | |
|
|
595 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
596 | |
479 | #ifndef WIN32 |
597 | #ifndef WIN32 |
|
|
598 | |
|
|
599 | static struct ev_signal childev; |
480 | |
600 | |
481 | #ifndef WCONTINUED |
601 | #ifndef WCONTINUED |
482 | # define WCONTINUED 0 |
602 | # define WCONTINUED 0 |
483 | #endif |
603 | #endif |
484 | |
604 | |
… | |
… | |
488 | struct ev_child *w; |
608 | struct ev_child *w; |
489 | |
609 | |
490 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
610 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
491 | if (w->pid == pid || !w->pid) |
611 | if (w->pid == pid || !w->pid) |
492 | { |
612 | { |
493 | w->priority = sw->priority; /* need to do it *now* */ |
613 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
494 | w->rpid = pid; |
614 | w->rpid = pid; |
495 | w->rstatus = status; |
615 | w->rstatus = status; |
496 | event (EV_A_ (W)w, EV_CHILD); |
616 | event (EV_A_ (W)w, EV_CHILD); |
497 | } |
617 | } |
498 | } |
618 | } |
499 | |
619 | |
500 | static void |
620 | static void |
… | |
… | |
520 | # include "ev_kqueue.c" |
640 | # include "ev_kqueue.c" |
521 | #endif |
641 | #endif |
522 | #if EV_USE_EPOLL |
642 | #if EV_USE_EPOLL |
523 | # include "ev_epoll.c" |
643 | # include "ev_epoll.c" |
524 | #endif |
644 | #endif |
525 | #if EV_USEV_POLL |
645 | #if EV_USE_POLL |
526 | # include "ev_poll.c" |
646 | # include "ev_poll.c" |
527 | #endif |
647 | #endif |
528 | #if EV_USE_SELECT |
648 | #if EV_USE_SELECT |
529 | # include "ev_select.c" |
649 | # include "ev_select.c" |
530 | #endif |
650 | #endif |
… | |
… | |
582 | methods = atoi (getenv ("LIBEV_METHODS")); |
702 | methods = atoi (getenv ("LIBEV_METHODS")); |
583 | else |
703 | else |
584 | methods = EVMETHOD_ANY; |
704 | methods = EVMETHOD_ANY; |
585 | |
705 | |
586 | method = 0; |
706 | method = 0; |
|
|
707 | #if EV_USE_WIN32 |
|
|
708 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
709 | #endif |
587 | #if EV_USE_KQUEUE |
710 | #if EV_USE_KQUEUE |
588 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
711 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
589 | #endif |
712 | #endif |
590 | #if EV_USE_EPOLL |
713 | #if EV_USE_EPOLL |
591 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
714 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
592 | #endif |
715 | #endif |
593 | #if EV_USEV_POLL |
716 | #if EV_USE_POLL |
594 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
717 | if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); |
595 | #endif |
718 | #endif |
596 | #if EV_USE_SELECT |
719 | #if EV_USE_SELECT |
597 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
720 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
598 | #endif |
721 | #endif |
|
|
722 | |
|
|
723 | ev_watcher_init (&sigev, sigcb); |
|
|
724 | ev_set_priority (&sigev, EV_MAXPRI); |
599 | } |
725 | } |
600 | } |
726 | } |
601 | |
727 | |
602 | void |
728 | void |
603 | loop_destroy (EV_P) |
729 | loop_destroy (EV_P) |
604 | { |
730 | { |
|
|
731 | int i; |
|
|
732 | |
|
|
733 | #if EV_USE_WIN32 |
|
|
734 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
735 | #endif |
605 | #if EV_USE_KQUEUE |
736 | #if EV_USE_KQUEUE |
606 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
737 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
607 | #endif |
738 | #endif |
608 | #if EV_USE_EPOLL |
739 | #if EV_USE_EPOLL |
609 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
740 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
610 | #endif |
741 | #endif |
611 | #if EV_USEV_POLL |
742 | #if EV_USE_POLL |
612 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
743 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
613 | #endif |
744 | #endif |
614 | #if EV_USE_SELECT |
745 | #if EV_USE_SELECT |
615 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
746 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
616 | #endif |
747 | #endif |
617 | |
748 | |
|
|
749 | for (i = NUMPRI; i--; ) |
|
|
750 | array_free (pending, [i]); |
|
|
751 | |
|
|
752 | /* have to use the microsoft-never-gets-it-right macro */ |
|
|
753 | array_free_microshit (fdchange); |
|
|
754 | array_free_microshit (timer); |
|
|
755 | array_free_microshit (periodic); |
|
|
756 | array_free_microshit (idle); |
|
|
757 | array_free_microshit (prepare); |
|
|
758 | array_free_microshit (check); |
|
|
759 | |
618 | method = 0; |
760 | method = 0; |
619 | /*TODO*/ |
|
|
620 | } |
761 | } |
621 | |
762 | |
622 | void |
763 | static void |
623 | loop_fork (EV_P) |
764 | loop_fork (EV_P) |
624 | { |
765 | { |
625 | /*TODO*/ |
|
|
626 | #if EV_USE_EPOLL |
766 | #if EV_USE_EPOLL |
627 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
767 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
628 | #endif |
768 | #endif |
629 | #if EV_USE_KQUEUE |
769 | #if EV_USE_KQUEUE |
630 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
770 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
631 | #endif |
771 | #endif |
|
|
772 | |
|
|
773 | if (ev_is_active (&sigev)) |
|
|
774 | { |
|
|
775 | /* default loop */ |
|
|
776 | |
|
|
777 | ev_ref (EV_A); |
|
|
778 | ev_io_stop (EV_A_ &sigev); |
|
|
779 | close (sigpipe [0]); |
|
|
780 | close (sigpipe [1]); |
|
|
781 | |
|
|
782 | while (pipe (sigpipe)) |
|
|
783 | syserr ("(libev) error creating pipe"); |
|
|
784 | |
|
|
785 | siginit (EV_A); |
|
|
786 | } |
|
|
787 | |
|
|
788 | postfork = 0; |
632 | } |
789 | } |
633 | |
790 | |
634 | #if EV_MULTIPLICITY |
791 | #if EV_MULTIPLICITY |
635 | struct ev_loop * |
792 | struct ev_loop * |
636 | ev_loop_new (int methods) |
793 | ev_loop_new (int methods) |
637 | { |
794 | { |
638 | struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); |
795 | struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop)); |
|
|
796 | |
|
|
797 | memset (loop, 0, sizeof (struct ev_loop)); |
639 | |
798 | |
640 | loop_init (EV_A_ methods); |
799 | loop_init (EV_A_ methods); |
641 | |
800 | |
642 | if (ev_methods (EV_A)) |
801 | if (ev_method (EV_A)) |
643 | return loop; |
802 | return loop; |
644 | |
803 | |
645 | return 0; |
804 | return 0; |
646 | } |
805 | } |
647 | |
806 | |
648 | void |
807 | void |
649 | ev_loop_destroy (EV_P) |
808 | ev_loop_destroy (EV_P) |
650 | { |
809 | { |
651 | loop_destroy (EV_A); |
810 | loop_destroy (EV_A); |
652 | free (loop); |
811 | ev_free (loop); |
653 | } |
812 | } |
654 | |
813 | |
655 | void |
814 | void |
656 | ev_loop_fork (EV_P) |
815 | ev_loop_fork (EV_P) |
657 | { |
816 | { |
658 | loop_fork (EV_A); |
817 | postfork = 1; |
659 | } |
818 | } |
660 | |
819 | |
661 | #endif |
820 | #endif |
662 | |
821 | |
663 | #if EV_MULTIPLICITY |
822 | #if EV_MULTIPLICITY |
… | |
… | |
686 | |
845 | |
687 | loop_init (EV_A_ methods); |
846 | loop_init (EV_A_ methods); |
688 | |
847 | |
689 | if (ev_method (EV_A)) |
848 | if (ev_method (EV_A)) |
690 | { |
849 | { |
691 | ev_watcher_init (&sigev, sigcb); |
|
|
692 | ev_set_priority (&sigev, EV_MAXPRI); |
|
|
693 | siginit (EV_A); |
850 | siginit (EV_A); |
694 | |
851 | |
695 | #ifndef WIN32 |
852 | #ifndef WIN32 |
696 | ev_signal_init (&childev, childcb, SIGCHLD); |
853 | ev_signal_init (&childev, childcb, SIGCHLD); |
697 | ev_set_priority (&childev, EV_MAXPRI); |
854 | ev_set_priority (&childev, EV_MAXPRI); |
… | |
… | |
707 | } |
864 | } |
708 | |
865 | |
709 | void |
866 | void |
710 | ev_default_destroy (void) |
867 | ev_default_destroy (void) |
711 | { |
868 | { |
|
|
869 | #if EV_MULTIPLICITY |
712 | struct ev_loop *loop = default_loop; |
870 | struct ev_loop *loop = default_loop; |
|
|
871 | #endif |
713 | |
872 | |
|
|
873 | #ifndef WIN32 |
714 | ev_ref (EV_A); /* child watcher */ |
874 | ev_ref (EV_A); /* child watcher */ |
715 | ev_signal_stop (EV_A_ &childev); |
875 | ev_signal_stop (EV_A_ &childev); |
|
|
876 | #endif |
716 | |
877 | |
717 | ev_ref (EV_A); /* signal watcher */ |
878 | ev_ref (EV_A); /* signal watcher */ |
718 | ev_io_stop (EV_A_ &sigev); |
879 | ev_io_stop (EV_A_ &sigev); |
719 | |
880 | |
720 | close (sigpipe [0]); sigpipe [0] = 0; |
881 | close (sigpipe [0]); sigpipe [0] = 0; |
… | |
… | |
722 | |
883 | |
723 | loop_destroy (EV_A); |
884 | loop_destroy (EV_A); |
724 | } |
885 | } |
725 | |
886 | |
726 | void |
887 | void |
727 | ev_default_fork (EV_P) |
888 | ev_default_fork (void) |
728 | { |
889 | { |
729 | loop_fork (EV_A); |
890 | #if EV_MULTIPLICITY |
|
|
891 | struct ev_loop *loop = default_loop; |
|
|
892 | #endif |
730 | |
893 | |
731 | ev_io_stop (EV_A_ &sigev); |
894 | if (method) |
732 | close (sigpipe [0]); |
895 | postfork = 1; |
733 | close (sigpipe [1]); |
|
|
734 | pipe (sigpipe); |
|
|
735 | |
|
|
736 | ev_ref (EV_A); /* signal watcher */ |
|
|
737 | siginit (EV_A); |
|
|
738 | } |
896 | } |
739 | |
897 | |
740 | /*****************************************************************************/ |
898 | /*****************************************************************************/ |
741 | |
899 | |
742 | static void |
900 | static void |
… | |
… | |
758 | } |
916 | } |
759 | |
917 | |
760 | static void |
918 | static void |
761 | timers_reify (EV_P) |
919 | timers_reify (EV_P) |
762 | { |
920 | { |
763 | while (timercnt && timers [0]->at <= mn_now) |
921 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
764 | { |
922 | { |
765 | struct ev_timer *w = timers [0]; |
923 | struct ev_timer *w = timers [0]; |
|
|
924 | |
|
|
925 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
766 | |
926 | |
767 | /* first reschedule or stop timer */ |
927 | /* first reschedule or stop timer */ |
768 | if (w->repeat) |
928 | if (w->repeat) |
769 | { |
929 | { |
770 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
930 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
771 | w->at = mn_now + w->repeat; |
931 | ((WT)w)->at = mn_now + w->repeat; |
772 | downheap ((WT *)timers, timercnt, 0); |
932 | downheap ((WT *)timers, timercnt, 0); |
773 | } |
933 | } |
774 | else |
934 | else |
775 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
935 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
776 | |
936 | |
… | |
… | |
779 | } |
939 | } |
780 | |
940 | |
781 | static void |
941 | static void |
782 | periodics_reify (EV_P) |
942 | periodics_reify (EV_P) |
783 | { |
943 | { |
784 | while (periodiccnt && periodics [0]->at <= rt_now) |
944 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
785 | { |
945 | { |
786 | struct ev_periodic *w = periodics [0]; |
946 | struct ev_periodic *w = periodics [0]; |
|
|
947 | |
|
|
948 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
787 | |
949 | |
788 | /* first reschedule or stop timer */ |
950 | /* first reschedule or stop timer */ |
789 | if (w->interval) |
951 | if (w->interval) |
790 | { |
952 | { |
791 | w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; |
953 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
792 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); |
954 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
793 | downheap ((WT *)periodics, periodiccnt, 0); |
955 | downheap ((WT *)periodics, periodiccnt, 0); |
794 | } |
956 | } |
795 | else |
957 | else |
796 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
958 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
797 | |
959 | |
… | |
… | |
809 | { |
971 | { |
810 | struct ev_periodic *w = periodics [i]; |
972 | struct ev_periodic *w = periodics [i]; |
811 | |
973 | |
812 | if (w->interval) |
974 | if (w->interval) |
813 | { |
975 | { |
814 | ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; |
976 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
815 | |
977 | |
816 | if (fabs (diff) >= 1e-4) |
978 | if (fabs (diff) >= 1e-4) |
817 | { |
979 | { |
818 | ev_periodic_stop (EV_A_ w); |
980 | ev_periodic_stop (EV_A_ w); |
819 | ev_periodic_start (EV_A_ w); |
981 | ev_periodic_start (EV_A_ w); |
… | |
… | |
880 | { |
1042 | { |
881 | periodics_reschedule (EV_A); |
1043 | periodics_reschedule (EV_A); |
882 | |
1044 | |
883 | /* adjust timers. this is easy, as the offset is the same for all */ |
1045 | /* adjust timers. this is easy, as the offset is the same for all */ |
884 | for (i = 0; i < timercnt; ++i) |
1046 | for (i = 0; i < timercnt; ++i) |
885 | timers [i]->at += rt_now - mn_now; |
1047 | ((WT)timers [i])->at += rt_now - mn_now; |
886 | } |
1048 | } |
887 | |
1049 | |
888 | mn_now = rt_now; |
1050 | mn_now = rt_now; |
889 | } |
1051 | } |
890 | } |
1052 | } |
… | |
… | |
916 | { |
1078 | { |
917 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1079 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
918 | call_pending (EV_A); |
1080 | call_pending (EV_A); |
919 | } |
1081 | } |
920 | |
1082 | |
|
|
1083 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1084 | if (expect_false (postfork)) |
|
|
1085 | loop_fork (EV_A); |
|
|
1086 | |
921 | /* update fd-related kernel structures */ |
1087 | /* update fd-related kernel structures */ |
922 | fd_reify (EV_A); |
1088 | fd_reify (EV_A); |
923 | |
1089 | |
924 | /* calculate blocking time */ |
1090 | /* calculate blocking time */ |
925 | |
1091 | |
… | |
… | |
941 | { |
1107 | { |
942 | block = MAX_BLOCKTIME; |
1108 | block = MAX_BLOCKTIME; |
943 | |
1109 | |
944 | if (timercnt) |
1110 | if (timercnt) |
945 | { |
1111 | { |
946 | ev_tstamp to = timers [0]->at - mn_now + method_fudge; |
1112 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
947 | if (block > to) block = to; |
1113 | if (block > to) block = to; |
948 | } |
1114 | } |
949 | |
1115 | |
950 | if (periodiccnt) |
1116 | if (periodiccnt) |
951 | { |
1117 | { |
952 | ev_tstamp to = periodics [0]->at - rt_now + method_fudge; |
1118 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
953 | if (block > to) block = to; |
1119 | if (block > to) block = to; |
954 | } |
1120 | } |
955 | |
1121 | |
956 | if (block < 0.) block = 0.; |
1122 | if (block < 0.) block = 0.; |
957 | } |
1123 | } |
… | |
… | |
1074 | ev_timer_start (EV_P_ struct ev_timer *w) |
1240 | ev_timer_start (EV_P_ struct ev_timer *w) |
1075 | { |
1241 | { |
1076 | if (ev_is_active (w)) |
1242 | if (ev_is_active (w)) |
1077 | return; |
1243 | return; |
1078 | |
1244 | |
1079 | w->at += mn_now; |
1245 | ((WT)w)->at += mn_now; |
1080 | |
1246 | |
1081 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1247 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1082 | |
1248 | |
1083 | ev_start (EV_A_ (W)w, ++timercnt); |
1249 | ev_start (EV_A_ (W)w, ++timercnt); |
1084 | array_needsize (timers, timermax, timercnt, ); |
1250 | array_needsize (timers, timermax, timercnt, (void)); |
1085 | timers [timercnt - 1] = w; |
1251 | timers [timercnt - 1] = w; |
1086 | upheap ((WT *)timers, timercnt - 1); |
1252 | upheap ((WT *)timers, timercnt - 1); |
|
|
1253 | |
|
|
1254 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1087 | } |
1255 | } |
1088 | |
1256 | |
1089 | void |
1257 | void |
1090 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1258 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1091 | { |
1259 | { |
1092 | ev_clear_pending (EV_A_ (W)w); |
1260 | ev_clear_pending (EV_A_ (W)w); |
1093 | if (!ev_is_active (w)) |
1261 | if (!ev_is_active (w)) |
1094 | return; |
1262 | return; |
1095 | |
1263 | |
|
|
1264 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
|
|
1265 | |
1096 | if (w->active < timercnt--) |
1266 | if (((W)w)->active < timercnt--) |
1097 | { |
1267 | { |
1098 | timers [w->active - 1] = timers [timercnt]; |
1268 | timers [((W)w)->active - 1] = timers [timercnt]; |
1099 | downheap ((WT *)timers, timercnt, w->active - 1); |
1269 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1100 | } |
1270 | } |
1101 | |
1271 | |
1102 | w->at = w->repeat; |
1272 | ((WT)w)->at = w->repeat; |
1103 | |
1273 | |
1104 | ev_stop (EV_A_ (W)w); |
1274 | ev_stop (EV_A_ (W)w); |
1105 | } |
1275 | } |
1106 | |
1276 | |
1107 | void |
1277 | void |
… | |
… | |
1109 | { |
1279 | { |
1110 | if (ev_is_active (w)) |
1280 | if (ev_is_active (w)) |
1111 | { |
1281 | { |
1112 | if (w->repeat) |
1282 | if (w->repeat) |
1113 | { |
1283 | { |
1114 | w->at = mn_now + w->repeat; |
1284 | ((WT)w)->at = mn_now + w->repeat; |
1115 | downheap ((WT *)timers, timercnt, w->active - 1); |
1285 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1116 | } |
1286 | } |
1117 | else |
1287 | else |
1118 | ev_timer_stop (EV_A_ w); |
1288 | ev_timer_stop (EV_A_ w); |
1119 | } |
1289 | } |
1120 | else if (w->repeat) |
1290 | else if (w->repeat) |
… | |
… | |
1129 | |
1299 | |
1130 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1300 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1131 | |
1301 | |
1132 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1302 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1133 | if (w->interval) |
1303 | if (w->interval) |
1134 | w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; |
1304 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1135 | |
1305 | |
1136 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1306 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1137 | array_needsize (periodics, periodicmax, periodiccnt, ); |
1307 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1138 | periodics [periodiccnt - 1] = w; |
1308 | periodics [periodiccnt - 1] = w; |
1139 | upheap ((WT *)periodics, periodiccnt - 1); |
1309 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
1310 | |
|
|
1311 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1140 | } |
1312 | } |
1141 | |
1313 | |
1142 | void |
1314 | void |
1143 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1315 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1144 | { |
1316 | { |
1145 | ev_clear_pending (EV_A_ (W)w); |
1317 | ev_clear_pending (EV_A_ (W)w); |
1146 | if (!ev_is_active (w)) |
1318 | if (!ev_is_active (w)) |
1147 | return; |
1319 | return; |
1148 | |
1320 | |
|
|
1321 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
|
|
1322 | |
1149 | if (w->active < periodiccnt--) |
1323 | if (((W)w)->active < periodiccnt--) |
1150 | { |
1324 | { |
1151 | periodics [w->active - 1] = periodics [periodiccnt]; |
1325 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1152 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
1326 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1153 | } |
1327 | } |
1154 | |
1328 | |
1155 | ev_stop (EV_A_ (W)w); |
1329 | ev_stop (EV_A_ (W)w); |
1156 | } |
1330 | } |
1157 | |
1331 | |
… | |
… | |
1160 | { |
1334 | { |
1161 | if (ev_is_active (w)) |
1335 | if (ev_is_active (w)) |
1162 | return; |
1336 | return; |
1163 | |
1337 | |
1164 | ev_start (EV_A_ (W)w, ++idlecnt); |
1338 | ev_start (EV_A_ (W)w, ++idlecnt); |
1165 | array_needsize (idles, idlemax, idlecnt, ); |
1339 | array_needsize (idles, idlemax, idlecnt, (void)); |
1166 | idles [idlecnt - 1] = w; |
1340 | idles [idlecnt - 1] = w; |
1167 | } |
1341 | } |
1168 | |
1342 | |
1169 | void |
1343 | void |
1170 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1344 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1171 | { |
1345 | { |
1172 | ev_clear_pending (EV_A_ (W)w); |
1346 | ev_clear_pending (EV_A_ (W)w); |
1173 | if (ev_is_active (w)) |
1347 | if (ev_is_active (w)) |
1174 | return; |
1348 | return; |
1175 | |
1349 | |
1176 | idles [w->active - 1] = idles [--idlecnt]; |
1350 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1177 | ev_stop (EV_A_ (W)w); |
1351 | ev_stop (EV_A_ (W)w); |
1178 | } |
1352 | } |
1179 | |
1353 | |
1180 | void |
1354 | void |
1181 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1355 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
1182 | { |
1356 | { |
1183 | if (ev_is_active (w)) |
1357 | if (ev_is_active (w)) |
1184 | return; |
1358 | return; |
1185 | |
1359 | |
1186 | ev_start (EV_A_ (W)w, ++preparecnt); |
1360 | ev_start (EV_A_ (W)w, ++preparecnt); |
1187 | array_needsize (prepares, preparemax, preparecnt, ); |
1361 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1188 | prepares [preparecnt - 1] = w; |
1362 | prepares [preparecnt - 1] = w; |
1189 | } |
1363 | } |
1190 | |
1364 | |
1191 | void |
1365 | void |
1192 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1366 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1193 | { |
1367 | { |
1194 | ev_clear_pending (EV_A_ (W)w); |
1368 | ev_clear_pending (EV_A_ (W)w); |
1195 | if (ev_is_active (w)) |
1369 | if (ev_is_active (w)) |
1196 | return; |
1370 | return; |
1197 | |
1371 | |
1198 | prepares [w->active - 1] = prepares [--preparecnt]; |
1372 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1199 | ev_stop (EV_A_ (W)w); |
1373 | ev_stop (EV_A_ (W)w); |
1200 | } |
1374 | } |
1201 | |
1375 | |
1202 | void |
1376 | void |
1203 | ev_check_start (EV_P_ struct ev_check *w) |
1377 | ev_check_start (EV_P_ struct ev_check *w) |
1204 | { |
1378 | { |
1205 | if (ev_is_active (w)) |
1379 | if (ev_is_active (w)) |
1206 | return; |
1380 | return; |
1207 | |
1381 | |
1208 | ev_start (EV_A_ (W)w, ++checkcnt); |
1382 | ev_start (EV_A_ (W)w, ++checkcnt); |
1209 | array_needsize (checks, checkmax, checkcnt, ); |
1383 | array_needsize (checks, checkmax, checkcnt, (void)); |
1210 | checks [checkcnt - 1] = w; |
1384 | checks [checkcnt - 1] = w; |
1211 | } |
1385 | } |
1212 | |
1386 | |
1213 | void |
1387 | void |
1214 | ev_check_stop (EV_P_ struct ev_check *w) |
1388 | ev_check_stop (EV_P_ struct ev_check *w) |
1215 | { |
1389 | { |
1216 | ev_clear_pending (EV_A_ (W)w); |
1390 | ev_clear_pending (EV_A_ (W)w); |
1217 | if (ev_is_active (w)) |
1391 | if (ev_is_active (w)) |
1218 | return; |
1392 | return; |
1219 | |
1393 | |
1220 | checks [w->active - 1] = checks [--checkcnt]; |
1394 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1221 | ev_stop (EV_A_ (W)w); |
1395 | ev_stop (EV_A_ (W)w); |
1222 | } |
1396 | } |
1223 | |
1397 | |
1224 | #ifndef SA_RESTART |
1398 | #ifndef SA_RESTART |
1225 | # define SA_RESTART 0 |
1399 | # define SA_RESTART 0 |
… | |
… | |
1238 | |
1412 | |
1239 | ev_start (EV_A_ (W)w, 1); |
1413 | ev_start (EV_A_ (W)w, 1); |
1240 | array_needsize (signals, signalmax, w->signum, signals_init); |
1414 | array_needsize (signals, signalmax, w->signum, signals_init); |
1241 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1415 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1242 | |
1416 | |
1243 | if (!w->next) |
1417 | if (!((WL)w)->next) |
1244 | { |
1418 | { |
|
|
1419 | #if WIN32 |
|
|
1420 | signal (w->signum, sighandler); |
|
|
1421 | #else |
1245 | struct sigaction sa; |
1422 | struct sigaction sa; |
1246 | sa.sa_handler = sighandler; |
1423 | sa.sa_handler = sighandler; |
1247 | sigfillset (&sa.sa_mask); |
1424 | sigfillset (&sa.sa_mask); |
1248 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1425 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
1249 | sigaction (w->signum, &sa, 0); |
1426 | sigaction (w->signum, &sa, 0); |
|
|
1427 | #endif |
1250 | } |
1428 | } |
1251 | } |
1429 | } |
1252 | |
1430 | |
1253 | void |
1431 | void |
1254 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1432 | ev_signal_stop (EV_P_ struct ev_signal *w) |
… | |
… | |
1304 | void (*cb)(int revents, void *arg) = once->cb; |
1482 | void (*cb)(int revents, void *arg) = once->cb; |
1305 | void *arg = once->arg; |
1483 | void *arg = once->arg; |
1306 | |
1484 | |
1307 | ev_io_stop (EV_A_ &once->io); |
1485 | ev_io_stop (EV_A_ &once->io); |
1308 | ev_timer_stop (EV_A_ &once->to); |
1486 | ev_timer_stop (EV_A_ &once->to); |
1309 | free (once); |
1487 | ev_free (once); |
1310 | |
1488 | |
1311 | cb (revents, arg); |
1489 | cb (revents, arg); |
1312 | } |
1490 | } |
1313 | |
1491 | |
1314 | static void |
1492 | static void |
… | |
… | |
1324 | } |
1502 | } |
1325 | |
1503 | |
1326 | void |
1504 | void |
1327 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1505 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1328 | { |
1506 | { |
1329 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1507 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1330 | |
1508 | |
1331 | if (!once) |
1509 | if (!once) |
1332 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1510 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1333 | else |
1511 | else |
1334 | { |
1512 | { |