… | |
… | |
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 | |
… | |
… | |
126 | #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) */ |
127 | #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) */ |
128 | #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 */ |
129 | /*#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 */ |
130 | |
135 | |
|
|
136 | #ifdef EV_H |
|
|
137 | # include EV_H |
|
|
138 | #else |
131 | #include "ev.h" |
139 | # include "ev.h" |
|
|
140 | #endif |
132 | |
141 | |
133 | #if __GNUC__ >= 3 |
142 | #if __GNUC__ >= 3 |
134 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
143 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
135 | # define inline inline |
144 | # define inline inline |
136 | #else |
145 | #else |
… | |
… | |
148 | typedef struct ev_watcher_list *WL; |
157 | typedef struct ev_watcher_list *WL; |
149 | typedef struct ev_watcher_time *WT; |
158 | typedef struct ev_watcher_time *WT; |
150 | |
159 | |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
160 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
152 | |
161 | |
153 | #if WIN32 |
162 | #include "ev_win32.c" |
154 | /* note: the comment below could not be substantiated, but what would I care */ |
|
|
155 | /* MSDN says this is required to handle SIGFPE */ |
|
|
156 | volatile double SIGFPE_REQ = 0.0f; |
|
|
157 | |
|
|
158 | static int |
|
|
159 | ev_socketpair_tcp (int filedes [2]) |
|
|
160 | { |
|
|
161 | struct sockaddr_in addr = { 0 }; |
|
|
162 | int addr_size = sizeof (addr); |
|
|
163 | SOCKET listener; |
|
|
164 | SOCKET sock [2] = { -1, -1 }; |
|
|
165 | |
|
|
166 | if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|
|
167 | return -1; |
|
|
168 | |
|
|
169 | addr.sin_family = AF_INET; |
|
|
170 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
|
|
171 | addr.sin_port = 0; |
|
|
172 | |
|
|
173 | if (bind (listener, (struct sockaddr *)&addr, addr_size)) |
|
|
174 | goto fail; |
|
|
175 | |
|
|
176 | if (getsockname(listener, (struct sockaddr *)&addr, &addr_size)) |
|
|
177 | goto fail; |
|
|
178 | |
|
|
179 | if (listen (listener, 1)) |
|
|
180 | goto fail; |
|
|
181 | |
|
|
182 | if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) |
|
|
183 | goto fail; |
|
|
184 | |
|
|
185 | if (connect (sock[0], (struct sockaddr *)&addr, addr_size)) |
|
|
186 | goto fail; |
|
|
187 | |
|
|
188 | if ((sock[1] = accept (listener, 0, 0)) < 0) |
|
|
189 | goto fail; |
|
|
190 | |
|
|
191 | closesocket (listener); |
|
|
192 | |
|
|
193 | filedes [0] = sock [0]; |
|
|
194 | filedes [1] = sock [1]; |
|
|
195 | |
|
|
196 | return 0; |
|
|
197 | |
|
|
198 | fail: |
|
|
199 | closesocket (listener); |
|
|
200 | |
|
|
201 | if (sock [0] != INVALID_SOCKET) closesocket (sock [0]); |
|
|
202 | if (sock [1] != INVALID_SOCKET) closesocket (sock [1]); |
|
|
203 | |
|
|
204 | return -1; |
|
|
205 | } |
|
|
206 | |
|
|
207 | # define ev_pipe(filedes) ev_socketpair_tcp (filedes) |
|
|
208 | #else |
|
|
209 | # define ev_pipe(filedes) pipe (filedes) |
|
|
210 | #endif |
|
|
211 | |
163 | |
212 | /*****************************************************************************/ |
164 | /*****************************************************************************/ |
213 | |
165 | |
214 | static void (*syserr_cb)(const char *msg); |
166 | static void (*syserr_cb)(const char *msg); |
215 | |
167 | |
… | |
… | |
272 | int events; |
224 | int events; |
273 | } ANPENDING; |
225 | } ANPENDING; |
274 | |
226 | |
275 | #if EV_MULTIPLICITY |
227 | #if EV_MULTIPLICITY |
276 | |
228 | |
277 | struct ev_loop |
229 | struct ev_loop |
278 | { |
230 | { |
|
|
231 | ev_tstamp ev_rt_now; |
279 | # define VAR(name,decl) decl; |
232 | #define VAR(name,decl) decl; |
280 | # include "ev_vars.h" |
233 | #include "ev_vars.h" |
281 | }; |
|
|
282 | # undef VAR |
234 | #undef VAR |
|
|
235 | }; |
283 | # include "ev_wrap.h" |
236 | #include "ev_wrap.h" |
|
|
237 | |
|
|
238 | struct ev_loop default_loop_struct; |
|
|
239 | static struct ev_loop *default_loop; |
284 | |
240 | |
285 | #else |
241 | #else |
286 | |
242 | |
|
|
243 | ev_tstamp ev_rt_now; |
287 | # define VAR(name,decl) static decl; |
244 | #define VAR(name,decl) static decl; |
288 | # include "ev_vars.h" |
245 | #include "ev_vars.h" |
289 | # undef VAR |
246 | #undef VAR |
|
|
247 | |
|
|
248 | static int default_loop; |
290 | |
249 | |
291 | #endif |
250 | #endif |
292 | |
251 | |
293 | /*****************************************************************************/ |
252 | /*****************************************************************************/ |
294 | |
253 | |
… | |
… | |
319 | #endif |
278 | #endif |
320 | |
279 | |
321 | return ev_time (); |
280 | return ev_time (); |
322 | } |
281 | } |
323 | |
282 | |
|
|
283 | #if EV_MULTIPLICITY |
324 | ev_tstamp |
284 | ev_tstamp |
325 | ev_now (EV_P) |
285 | ev_now (EV_P) |
326 | { |
286 | { |
327 | return rt_now; |
287 | return ev_rt_now; |
328 | } |
288 | } |
|
|
289 | #endif |
329 | |
290 | |
330 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
291 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
331 | |
292 | |
332 | #define array_needsize(base,cur,cnt,init) \ |
293 | #define array_needsize(type,base,cur,cnt,init) \ |
333 | if (expect_false ((cnt) > cur)) \ |
294 | if (expect_false ((cnt) > cur)) \ |
334 | { \ |
295 | { \ |
335 | int newcnt = cur; \ |
296 | int newcnt = cur; \ |
336 | do \ |
297 | do \ |
337 | { \ |
298 | { \ |
338 | newcnt = array_roundsize (base, newcnt << 1); \ |
299 | newcnt = array_roundsize (type, newcnt << 1); \ |
339 | } \ |
300 | } \ |
340 | while ((cnt) > newcnt); \ |
301 | while ((cnt) > newcnt); \ |
341 | \ |
302 | \ |
342 | base = ev_realloc (base, sizeof (*base) * (newcnt)); \ |
303 | base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\ |
343 | init (base + cur, newcnt - cur); \ |
304 | init (base + cur, newcnt - cur); \ |
344 | cur = newcnt; \ |
305 | cur = newcnt; \ |
345 | } |
306 | } |
346 | |
307 | |
347 | #define array_slim(stem) \ |
308 | #define array_slim(type,stem) \ |
348 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
309 | if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ |
349 | { \ |
310 | { \ |
350 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
311 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
351 | base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ |
312 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
352 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
313 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
353 | } |
314 | } |
354 | |
315 | |
355 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
316 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
356 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
317 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
… | |
… | |
373 | |
334 | |
374 | ++base; |
335 | ++base; |
375 | } |
336 | } |
376 | } |
337 | } |
377 | |
338 | |
378 | static void |
339 | void |
379 | event (EV_P_ W w, int events) |
340 | ev_feed_event (EV_P_ void *w, int revents) |
380 | { |
341 | { |
|
|
342 | W w_ = (W)w; |
|
|
343 | |
381 | if (w->pending) |
344 | if (w_->pending) |
382 | { |
345 | { |
383 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
346 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
384 | return; |
347 | return; |
385 | } |
348 | } |
386 | |
349 | |
387 | w->pending = ++pendingcnt [ABSPRI (w)]; |
350 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
388 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
351 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
389 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
352 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
390 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
353 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
391 | } |
354 | } |
392 | |
355 | |
393 | static void |
356 | static void |
394 | queue_events (EV_P_ W *events, int eventcnt, int type) |
357 | queue_events (EV_P_ W *events, int eventcnt, int type) |
395 | { |
358 | { |
396 | int i; |
359 | int i; |
397 | |
360 | |
398 | for (i = 0; i < eventcnt; ++i) |
361 | for (i = 0; i < eventcnt; ++i) |
399 | event (EV_A_ events [i], type); |
362 | ev_feed_event (EV_A_ events [i], type); |
400 | } |
363 | } |
401 | |
364 | |
402 | static void |
365 | inline void |
403 | fd_event (EV_P_ int fd, int events) |
366 | fd_event (EV_P_ int fd, int revents) |
404 | { |
367 | { |
405 | ANFD *anfd = anfds + fd; |
368 | ANFD *anfd = anfds + fd; |
406 | struct ev_io *w; |
369 | struct ev_io *w; |
407 | |
370 | |
408 | 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) |
409 | { |
372 | { |
410 | int ev = w->events & events; |
373 | int ev = w->events & revents; |
411 | |
374 | |
412 | if (ev) |
375 | if (ev) |
413 | event (EV_A_ (W)w, ev); |
376 | ev_feed_event (EV_A_ (W)w, ev); |
414 | } |
377 | } |
|
|
378 | } |
|
|
379 | |
|
|
380 | void |
|
|
381 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
382 | { |
|
|
383 | fd_event (EV_A_ fd, revents); |
415 | } |
384 | } |
416 | |
385 | |
417 | /*****************************************************************************/ |
386 | /*****************************************************************************/ |
418 | |
387 | |
419 | static void |
388 | static void |
… | |
… | |
448 | return; |
417 | return; |
449 | |
418 | |
450 | anfds [fd].reify = 1; |
419 | anfds [fd].reify = 1; |
451 | |
420 | |
452 | ++fdchangecnt; |
421 | ++fdchangecnt; |
453 | array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); |
422 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void)); |
454 | fdchanges [fdchangecnt - 1] = fd; |
423 | fdchanges [fdchangecnt - 1] = fd; |
455 | } |
424 | } |
456 | |
425 | |
457 | static void |
426 | static void |
458 | fd_kill (EV_P_ int fd) |
427 | fd_kill (EV_P_ int fd) |
… | |
… | |
460 | struct ev_io *w; |
429 | struct ev_io *w; |
461 | |
430 | |
462 | while ((w = (struct ev_io *)anfds [fd].head)) |
431 | while ((w = (struct ev_io *)anfds [fd].head)) |
463 | { |
432 | { |
464 | ev_io_stop (EV_A_ w); |
433 | ev_io_stop (EV_A_ w); |
465 | 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); |
466 | } |
435 | } |
467 | } |
436 | } |
468 | |
437 | |
469 | static int |
438 | static int |
470 | fd_valid (int fd) |
439 | fd_valid (int fd) |
… | |
… | |
558 | |
527 | |
559 | heap [k] = w; |
528 | heap [k] = w; |
560 | ((W)heap [k])->active = k + 1; |
529 | ((W)heap [k])->active = k + 1; |
561 | } |
530 | } |
562 | |
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 | |
563 | /*****************************************************************************/ |
544 | /*****************************************************************************/ |
564 | |
545 | |
565 | typedef struct |
546 | typedef struct |
566 | { |
547 | { |
567 | WL head; |
548 | WL head; |
… | |
… | |
598 | |
579 | |
599 | if (!gotsig) |
580 | if (!gotsig) |
600 | { |
581 | { |
601 | int old_errno = errno; |
582 | int old_errno = errno; |
602 | gotsig = 1; |
583 | gotsig = 1; |
|
|
584 | #ifdef WIN32 |
|
|
585 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
586 | #else |
603 | write (sigpipe [1], &signum, 1); |
587 | write (sigpipe [1], &signum, 1); |
|
|
588 | #endif |
604 | errno = old_errno; |
589 | errno = old_errno; |
605 | } |
590 | } |
606 | } |
591 | } |
607 | |
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 | |
608 | static void |
613 | static void |
609 | sigcb (EV_P_ struct ev_io *iow, int revents) |
614 | sigcb (EV_P_ struct ev_io *iow, int revents) |
610 | { |
615 | { |
611 | WL w; |
|
|
612 | int signum; |
616 | int signum; |
613 | |
617 | |
|
|
618 | #ifdef WIN32 |
|
|
619 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
620 | #else |
614 | read (sigpipe [0], &revents, 1); |
621 | read (sigpipe [0], &revents, 1); |
|
|
622 | #endif |
615 | gotsig = 0; |
623 | gotsig = 0; |
616 | |
624 | |
617 | for (signum = signalmax; signum--; ) |
625 | for (signum = signalmax; signum--; ) |
618 | if (signals [signum].gotsig) |
626 | if (signals [signum].gotsig) |
619 | { |
627 | ev_feed_signal_event (EV_A_ signum + 1); |
620 | signals [signum].gotsig = 0; |
|
|
621 | |
|
|
622 | for (w = signals [signum].head; w; w = w->next) |
|
|
623 | event (EV_A_ (W)w, EV_SIGNAL); |
|
|
624 | } |
|
|
625 | } |
628 | } |
626 | |
629 | |
627 | static void |
630 | static void |
628 | siginit (EV_P) |
631 | siginit (EV_P) |
629 | { |
632 | { |
… | |
… | |
662 | if (w->pid == pid || !w->pid) |
665 | if (w->pid == pid || !w->pid) |
663 | { |
666 | { |
664 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
667 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
665 | w->rpid = pid; |
668 | w->rpid = pid; |
666 | w->rstatus = status; |
669 | w->rstatus = status; |
667 | event (EV_A_ (W)w, EV_CHILD); |
670 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
668 | } |
671 | } |
669 | } |
672 | } |
670 | |
673 | |
671 | static void |
674 | static void |
672 | childcb (EV_P_ struct ev_signal *sw, int revents) |
675 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
674 | int pid, status; |
677 | int pid, status; |
675 | |
678 | |
676 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
679 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
677 | { |
680 | { |
678 | /* 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 */ |
679 | event (EV_A_ (W)sw, EV_SIGNAL); |
682 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
680 | |
683 | |
681 | child_reap (EV_A_ sw, pid, pid, status); |
684 | child_reap (EV_A_ sw, pid, pid, status); |
682 | 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 */ |
683 | } |
686 | } |
684 | } |
687 | } |
… | |
… | |
741 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
744 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
742 | have_monotonic = 1; |
745 | have_monotonic = 1; |
743 | } |
746 | } |
744 | #endif |
747 | #endif |
745 | |
748 | |
746 | rt_now = ev_time (); |
749 | ev_rt_now = ev_time (); |
747 | mn_now = get_clock (); |
750 | mn_now = get_clock (); |
748 | now_floor = mn_now; |
751 | now_floor = mn_now; |
749 | rtmn_diff = rt_now - mn_now; |
752 | rtmn_diff = ev_rt_now - mn_now; |
750 | |
753 | |
751 | if (methods == EVMETHOD_AUTO) |
754 | if (methods == EVMETHOD_AUTO) |
752 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
755 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
753 | methods = atoi (getenv ("LIBEV_METHODS")); |
756 | methods = atoi (getenv ("LIBEV_METHODS")); |
754 | else |
757 | else |
… | |
… | |
769 | #endif |
772 | #endif |
770 | #if EV_USE_SELECT |
773 | #if EV_USE_SELECT |
771 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
774 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
772 | #endif |
775 | #endif |
773 | |
776 | |
774 | ev_watcher_init (&sigev, sigcb); |
777 | ev_init (&sigev, sigcb); |
775 | ev_set_priority (&sigev, EV_MAXPRI); |
778 | ev_set_priority (&sigev, EV_MAXPRI); |
776 | } |
779 | } |
777 | } |
780 | } |
778 | |
781 | |
779 | void |
782 | void |
… | |
… | |
828 | ev_ref (EV_A); |
831 | ev_ref (EV_A); |
829 | ev_io_stop (EV_A_ &sigev); |
832 | ev_io_stop (EV_A_ &sigev); |
830 | close (sigpipe [0]); |
833 | close (sigpipe [0]); |
831 | close (sigpipe [1]); |
834 | close (sigpipe [1]); |
832 | |
835 | |
833 | while (ev_pipe (sigpipe)) |
836 | while (pipe (sigpipe)) |
834 | syserr ("(libev) error creating pipe"); |
837 | syserr ("(libev) error creating pipe"); |
835 | |
838 | |
836 | siginit (EV_A); |
839 | siginit (EV_A); |
837 | } |
840 | } |
838 | |
841 | |
… | |
… | |
869 | } |
872 | } |
870 | |
873 | |
871 | #endif |
874 | #endif |
872 | |
875 | |
873 | #if EV_MULTIPLICITY |
876 | #if EV_MULTIPLICITY |
874 | struct ev_loop default_loop_struct; |
|
|
875 | static struct ev_loop *default_loop; |
|
|
876 | |
|
|
877 | struct ev_loop * |
877 | struct ev_loop * |
878 | #else |
878 | #else |
879 | static int default_loop; |
|
|
880 | |
|
|
881 | int |
879 | int |
882 | #endif |
880 | #endif |
883 | ev_default_loop (int methods) |
881 | ev_default_loop (int methods) |
884 | { |
882 | { |
885 | if (sigpipe [0] == sigpipe [1]) |
883 | if (sigpipe [0] == sigpipe [1]) |
886 | if (ev_pipe (sigpipe)) |
884 | if (pipe (sigpipe)) |
887 | return 0; |
885 | return 0; |
888 | |
886 | |
889 | if (!default_loop) |
887 | if (!default_loop) |
890 | { |
888 | { |
891 | #if EV_MULTIPLICITY |
889 | #if EV_MULTIPLICITY |
… | |
… | |
946 | postfork = 1; |
944 | postfork = 1; |
947 | } |
945 | } |
948 | |
946 | |
949 | /*****************************************************************************/ |
947 | /*****************************************************************************/ |
950 | |
948 | |
|
|
949 | static int |
|
|
950 | any_pending (EV_P) |
|
|
951 | { |
|
|
952 | int pri; |
|
|
953 | |
|
|
954 | for (pri = NUMPRI; pri--; ) |
|
|
955 | if (pendingcnt [pri]) |
|
|
956 | return 1; |
|
|
957 | |
|
|
958 | return 0; |
|
|
959 | } |
|
|
960 | |
951 | static void |
961 | static void |
952 | call_pending (EV_P) |
962 | call_pending (EV_P) |
953 | { |
963 | { |
954 | int pri; |
964 | int pri; |
955 | |
965 | |
… | |
… | |
959 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
969 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
960 | |
970 | |
961 | if (p->w) |
971 | if (p->w) |
962 | { |
972 | { |
963 | p->w->pending = 0; |
973 | p->w->pending = 0; |
964 | p->w->cb (EV_A_ p->w, p->events); |
974 | EV_CB_INVOKE (p->w, p->events); |
965 | } |
975 | } |
966 | } |
976 | } |
967 | } |
977 | } |
968 | |
978 | |
969 | static void |
979 | static void |
… | |
… | |
983 | downheap ((WT *)timers, timercnt, 0); |
993 | downheap ((WT *)timers, timercnt, 0); |
984 | } |
994 | } |
985 | else |
995 | else |
986 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
996 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
987 | |
997 | |
988 | event (EV_A_ (W)w, EV_TIMEOUT); |
998 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
989 | } |
999 | } |
990 | } |
1000 | } |
991 | |
1001 | |
992 | static void |
1002 | static void |
993 | periodics_reify (EV_P) |
1003 | periodics_reify (EV_P) |
994 | { |
1004 | { |
995 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1005 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
996 | { |
1006 | { |
997 | struct ev_periodic *w = periodics [0]; |
1007 | struct ev_periodic *w = periodics [0]; |
998 | |
1008 | |
999 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1009 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1000 | |
1010 | |
1001 | /* first reschedule or stop timer */ |
1011 | /* first reschedule or stop timer */ |
1002 | if (w->interval) |
1012 | if (w->reschedule_cb) |
1003 | { |
1013 | { |
|
|
1014 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
|
|
1015 | |
|
|
1016 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
|
|
1017 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
1018 | } |
|
|
1019 | else if (w->interval) |
|
|
1020 | { |
1004 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1021 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1005 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1022 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
1006 | downheap ((WT *)periodics, periodiccnt, 0); |
1023 | downheap ((WT *)periodics, periodiccnt, 0); |
1007 | } |
1024 | } |
1008 | else |
1025 | else |
1009 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1026 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1010 | |
1027 | |
1011 | event (EV_A_ (W)w, EV_PERIODIC); |
1028 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1012 | } |
1029 | } |
1013 | } |
1030 | } |
1014 | |
1031 | |
1015 | static void |
1032 | static void |
1016 | periodics_reschedule (EV_P) |
1033 | periodics_reschedule (EV_P) |
… | |
… | |
1020 | /* adjust periodics after time jump */ |
1037 | /* adjust periodics after time jump */ |
1021 | for (i = 0; i < periodiccnt; ++i) |
1038 | for (i = 0; i < periodiccnt; ++i) |
1022 | { |
1039 | { |
1023 | struct ev_periodic *w = periodics [i]; |
1040 | struct ev_periodic *w = periodics [i]; |
1024 | |
1041 | |
|
|
1042 | if (w->reschedule_cb) |
|
|
1043 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1025 | if (w->interval) |
1044 | else if (w->interval) |
1026 | { |
|
|
1027 | ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1045 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1028 | |
|
|
1029 | if (fabs (diff) >= 1e-4) |
|
|
1030 | { |
|
|
1031 | ev_periodic_stop (EV_A_ w); |
|
|
1032 | ev_periodic_start (EV_A_ w); |
|
|
1033 | |
|
|
1034 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
1035 | } |
|
|
1036 | } |
|
|
1037 | } |
1046 | } |
|
|
1047 | |
|
|
1048 | /* now rebuild the heap */ |
|
|
1049 | for (i = periodiccnt >> 1; i--; ) |
|
|
1050 | downheap ((WT *)periodics, periodiccnt, i); |
1038 | } |
1051 | } |
1039 | |
1052 | |
1040 | inline int |
1053 | inline int |
1041 | time_update_monotonic (EV_P) |
1054 | time_update_monotonic (EV_P) |
1042 | { |
1055 | { |
1043 | mn_now = get_clock (); |
1056 | mn_now = get_clock (); |
1044 | |
1057 | |
1045 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1058 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1046 | { |
1059 | { |
1047 | rt_now = rtmn_diff + mn_now; |
1060 | ev_rt_now = rtmn_diff + mn_now; |
1048 | return 0; |
1061 | return 0; |
1049 | } |
1062 | } |
1050 | else |
1063 | else |
1051 | { |
1064 | { |
1052 | now_floor = mn_now; |
1065 | now_floor = mn_now; |
1053 | rt_now = ev_time (); |
1066 | ev_rt_now = ev_time (); |
1054 | return 1; |
1067 | return 1; |
1055 | } |
1068 | } |
1056 | } |
1069 | } |
1057 | |
1070 | |
1058 | static void |
1071 | static void |
… | |
… | |
1067 | { |
1080 | { |
1068 | ev_tstamp odiff = rtmn_diff; |
1081 | ev_tstamp odiff = rtmn_diff; |
1069 | |
1082 | |
1070 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1083 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1071 | { |
1084 | { |
1072 | rtmn_diff = rt_now - mn_now; |
1085 | rtmn_diff = ev_rt_now - mn_now; |
1073 | |
1086 | |
1074 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1087 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1075 | return; /* all is well */ |
1088 | return; /* all is well */ |
1076 | |
1089 | |
1077 | rt_now = ev_time (); |
1090 | ev_rt_now = ev_time (); |
1078 | mn_now = get_clock (); |
1091 | mn_now = get_clock (); |
1079 | now_floor = mn_now; |
1092 | now_floor = mn_now; |
1080 | } |
1093 | } |
1081 | |
1094 | |
1082 | periodics_reschedule (EV_A); |
1095 | periodics_reschedule (EV_A); |
… | |
… | |
1085 | } |
1098 | } |
1086 | } |
1099 | } |
1087 | else |
1100 | else |
1088 | #endif |
1101 | #endif |
1089 | { |
1102 | { |
1090 | rt_now = ev_time (); |
1103 | ev_rt_now = ev_time (); |
1091 | |
1104 | |
1092 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1105 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1093 | { |
1106 | { |
1094 | periodics_reschedule (EV_A); |
1107 | periodics_reschedule (EV_A); |
1095 | |
1108 | |
1096 | /* adjust timers. this is easy, as the offset is the same for all */ |
1109 | /* adjust timers. this is easy, as the offset is the same for all */ |
1097 | for (i = 0; i < timercnt; ++i) |
1110 | for (i = 0; i < timercnt; ++i) |
1098 | ((WT)timers [i])->at += rt_now - mn_now; |
1111 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1099 | } |
1112 | } |
1100 | |
1113 | |
1101 | mn_now = rt_now; |
1114 | mn_now = ev_rt_now; |
1102 | } |
1115 | } |
1103 | } |
1116 | } |
1104 | |
1117 | |
1105 | void |
1118 | void |
1106 | ev_ref (EV_P) |
1119 | ev_ref (EV_P) |
… | |
… | |
1138 | /* update fd-related kernel structures */ |
1151 | /* update fd-related kernel structures */ |
1139 | fd_reify (EV_A); |
1152 | fd_reify (EV_A); |
1140 | |
1153 | |
1141 | /* calculate blocking time */ |
1154 | /* calculate blocking time */ |
1142 | |
1155 | |
1143 | /* we only need this for !monotonic clockor timers, but as we basically |
1156 | /* we only need this for !monotonic clock or timers, but as we basically |
1144 | always have timers, we just calculate it always */ |
1157 | always have timers, we just calculate it always */ |
1145 | #if EV_USE_MONOTONIC |
1158 | #if EV_USE_MONOTONIC |
1146 | if (expect_true (have_monotonic)) |
1159 | if (expect_true (have_monotonic)) |
1147 | time_update_monotonic (EV_A); |
1160 | time_update_monotonic (EV_A); |
1148 | else |
1161 | else |
1149 | #endif |
1162 | #endif |
1150 | { |
1163 | { |
1151 | rt_now = ev_time (); |
1164 | ev_rt_now = ev_time (); |
1152 | mn_now = rt_now; |
1165 | mn_now = ev_rt_now; |
1153 | } |
1166 | } |
1154 | |
1167 | |
1155 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1168 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1156 | block = 0.; |
1169 | block = 0.; |
1157 | else |
1170 | else |
… | |
… | |
1164 | if (block > to) block = to; |
1177 | if (block > to) block = to; |
1165 | } |
1178 | } |
1166 | |
1179 | |
1167 | if (periodiccnt) |
1180 | if (periodiccnt) |
1168 | { |
1181 | { |
1169 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1182 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1170 | if (block > to) block = to; |
1183 | if (block > to) block = to; |
1171 | } |
1184 | } |
1172 | |
1185 | |
1173 | if (block < 0.) block = 0.; |
1186 | if (block < 0.) block = 0.; |
1174 | } |
1187 | } |
1175 | |
1188 | |
1176 | method_poll (EV_A_ block); |
1189 | method_poll (EV_A_ block); |
1177 | |
1190 | |
1178 | /* update rt_now, do magic */ |
1191 | /* update ev_rt_now, do magic */ |
1179 | time_update (EV_A); |
1192 | time_update (EV_A); |
1180 | |
1193 | |
1181 | /* queue pending timers and reschedule them */ |
1194 | /* queue pending timers and reschedule them */ |
1182 | timers_reify (EV_A); /* relative timers called last */ |
1195 | timers_reify (EV_A); /* relative timers called last */ |
1183 | periodics_reify (EV_A); /* absolute timers called first */ |
1196 | periodics_reify (EV_A); /* absolute timers called first */ |
1184 | |
1197 | |
1185 | /* queue idle watchers unless io or timers are pending */ |
1198 | /* queue idle watchers unless io or timers are pending */ |
1186 | if (!pendingcnt) |
1199 | if (idlecnt && !any_pending (EV_A)) |
1187 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1200 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1188 | |
1201 | |
1189 | /* queue check watchers, to be executed first */ |
1202 | /* queue check watchers, to be executed first */ |
1190 | if (checkcnt) |
1203 | if (checkcnt) |
1191 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1204 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
… | |
… | |
1266 | return; |
1279 | return; |
1267 | |
1280 | |
1268 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1281 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1269 | |
1282 | |
1270 | ev_start (EV_A_ (W)w, 1); |
1283 | ev_start (EV_A_ (W)w, 1); |
1271 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
1284 | array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); |
1272 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1285 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
1273 | |
1286 | |
1274 | fd_change (EV_A_ fd); |
1287 | fd_change (EV_A_ fd); |
1275 | } |
1288 | } |
1276 | |
1289 | |
… | |
… | |
1279 | { |
1292 | { |
1280 | ev_clear_pending (EV_A_ (W)w); |
1293 | ev_clear_pending (EV_A_ (W)w); |
1281 | if (!ev_is_active (w)) |
1294 | if (!ev_is_active (w)) |
1282 | return; |
1295 | return; |
1283 | |
1296 | |
|
|
1297 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1298 | |
1284 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1299 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1285 | ev_stop (EV_A_ (W)w); |
1300 | ev_stop (EV_A_ (W)w); |
1286 | |
1301 | |
1287 | fd_change (EV_A_ w->fd); |
1302 | fd_change (EV_A_ w->fd); |
1288 | } |
1303 | } |
… | |
… | |
1296 | ((WT)w)->at += mn_now; |
1311 | ((WT)w)->at += mn_now; |
1297 | |
1312 | |
1298 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1313 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1299 | |
1314 | |
1300 | ev_start (EV_A_ (W)w, ++timercnt); |
1315 | ev_start (EV_A_ (W)w, ++timercnt); |
1301 | array_needsize (timers, timermax, timercnt, (void)); |
1316 | array_needsize (struct ev_timer *, timers, timermax, timercnt, (void)); |
1302 | timers [timercnt - 1] = w; |
1317 | timers [timercnt - 1] = w; |
1303 | upheap ((WT *)timers, timercnt - 1); |
1318 | upheap ((WT *)timers, timercnt - 1); |
1304 | |
1319 | |
1305 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1320 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1306 | } |
1321 | } |
… | |
… | |
1329 | ev_timer_again (EV_P_ struct ev_timer *w) |
1344 | ev_timer_again (EV_P_ struct ev_timer *w) |
1330 | { |
1345 | { |
1331 | if (ev_is_active (w)) |
1346 | if (ev_is_active (w)) |
1332 | { |
1347 | { |
1333 | if (w->repeat) |
1348 | if (w->repeat) |
1334 | { |
|
|
1335 | ((WT)w)->at = mn_now + w->repeat; |
|
|
1336 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1349 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); |
1337 | } |
|
|
1338 | else |
1350 | else |
1339 | ev_timer_stop (EV_A_ w); |
1351 | ev_timer_stop (EV_A_ w); |
1340 | } |
1352 | } |
1341 | else if (w->repeat) |
1353 | else if (w->repeat) |
1342 | ev_timer_start (EV_A_ w); |
1354 | ev_timer_start (EV_A_ w); |
… | |
… | |
1346 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1358 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1347 | { |
1359 | { |
1348 | if (ev_is_active (w)) |
1360 | if (ev_is_active (w)) |
1349 | return; |
1361 | return; |
1350 | |
1362 | |
|
|
1363 | if (w->reschedule_cb) |
|
|
1364 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
|
|
1365 | else if (w->interval) |
|
|
1366 | { |
1351 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1367 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1352 | |
|
|
1353 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1368 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1354 | if (w->interval) |
|
|
1355 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1369 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
|
|
1370 | } |
1356 | |
1371 | |
1357 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1372 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1358 | array_needsize (periodics, periodicmax, periodiccnt, (void)); |
1373 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1359 | periodics [periodiccnt - 1] = w; |
1374 | periodics [periodiccnt - 1] = w; |
1360 | upheap ((WT *)periodics, periodiccnt - 1); |
1375 | upheap ((WT *)periodics, periodiccnt - 1); |
1361 | |
1376 | |
1362 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1377 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1363 | } |
1378 | } |
… | |
… | |
1379 | |
1394 | |
1380 | ev_stop (EV_A_ (W)w); |
1395 | ev_stop (EV_A_ (W)w); |
1381 | } |
1396 | } |
1382 | |
1397 | |
1383 | void |
1398 | void |
|
|
1399 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
|
|
1400 | { |
|
|
1401 | /* TODO: use adjustheap and recalculation */ |
|
|
1402 | ev_periodic_stop (EV_A_ w); |
|
|
1403 | ev_periodic_start (EV_A_ w); |
|
|
1404 | } |
|
|
1405 | |
|
|
1406 | void |
1384 | ev_idle_start (EV_P_ struct ev_idle *w) |
1407 | ev_idle_start (EV_P_ struct ev_idle *w) |
1385 | { |
1408 | { |
1386 | if (ev_is_active (w)) |
1409 | if (ev_is_active (w)) |
1387 | return; |
1410 | return; |
1388 | |
1411 | |
1389 | ev_start (EV_A_ (W)w, ++idlecnt); |
1412 | ev_start (EV_A_ (W)w, ++idlecnt); |
1390 | array_needsize (idles, idlemax, idlecnt, (void)); |
1413 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void)); |
1391 | idles [idlecnt - 1] = w; |
1414 | idles [idlecnt - 1] = w; |
1392 | } |
1415 | } |
1393 | |
1416 | |
1394 | void |
1417 | void |
1395 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1418 | ev_idle_stop (EV_P_ struct ev_idle *w) |
… | |
… | |
1407 | { |
1430 | { |
1408 | if (ev_is_active (w)) |
1431 | if (ev_is_active (w)) |
1409 | return; |
1432 | return; |
1410 | |
1433 | |
1411 | ev_start (EV_A_ (W)w, ++preparecnt); |
1434 | ev_start (EV_A_ (W)w, ++preparecnt); |
1412 | array_needsize (prepares, preparemax, preparecnt, (void)); |
1435 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void)); |
1413 | prepares [preparecnt - 1] = w; |
1436 | prepares [preparecnt - 1] = w; |
1414 | } |
1437 | } |
1415 | |
1438 | |
1416 | void |
1439 | void |
1417 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1440 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
… | |
… | |
1429 | { |
1452 | { |
1430 | if (ev_is_active (w)) |
1453 | if (ev_is_active (w)) |
1431 | return; |
1454 | return; |
1432 | |
1455 | |
1433 | ev_start (EV_A_ (W)w, ++checkcnt); |
1456 | ev_start (EV_A_ (W)w, ++checkcnt); |
1434 | array_needsize (checks, checkmax, checkcnt, (void)); |
1457 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void)); |
1435 | checks [checkcnt - 1] = w; |
1458 | checks [checkcnt - 1] = w; |
1436 | } |
1459 | } |
1437 | |
1460 | |
1438 | void |
1461 | void |
1439 | ev_check_stop (EV_P_ struct ev_check *w) |
1462 | ev_check_stop (EV_P_ struct ev_check *w) |
… | |
… | |
1460 | return; |
1483 | return; |
1461 | |
1484 | |
1462 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1485 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1463 | |
1486 | |
1464 | ev_start (EV_A_ (W)w, 1); |
1487 | ev_start (EV_A_ (W)w, 1); |
1465 | array_needsize (signals, signalmax, w->signum, signals_init); |
1488 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1466 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1489 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1467 | |
1490 | |
1468 | if (!((WL)w)->next) |
1491 | if (!((WL)w)->next) |
1469 | { |
1492 | { |
1470 | #if WIN32 |
1493 | #if WIN32 |
… | |
… | |
1553 | } |
1576 | } |
1554 | |
1577 | |
1555 | void |
1578 | void |
1556 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1579 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1557 | { |
1580 | { |
1558 | struct ev_once *once = ev_malloc (sizeof (struct ev_once)); |
1581 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1559 | |
1582 | |
1560 | if (!once) |
1583 | if (!once) |
1561 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1584 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1562 | else |
1585 | else |
1563 | { |
1586 | { |
1564 | once->cb = cb; |
1587 | once->cb = cb; |
1565 | once->arg = arg; |
1588 | once->arg = arg; |
1566 | |
1589 | |
1567 | ev_watcher_init (&once->io, once_cb_io); |
1590 | ev_init (&once->io, once_cb_io); |
1568 | if (fd >= 0) |
1591 | if (fd >= 0) |
1569 | { |
1592 | { |
1570 | ev_io_set (&once->io, fd, events); |
1593 | ev_io_set (&once->io, fd, events); |
1571 | ev_io_start (EV_A_ &once->io); |
1594 | ev_io_start (EV_A_ &once->io); |
1572 | } |
1595 | } |
1573 | |
1596 | |
1574 | ev_watcher_init (&once->to, once_cb_to); |
1597 | ev_init (&once->to, once_cb_to); |
1575 | if (timeout >= 0.) |
1598 | if (timeout >= 0.) |
1576 | { |
1599 | { |
1577 | ev_timer_set (&once->to, timeout, 0.); |
1600 | ev_timer_set (&once->to, timeout, 0.); |
1578 | ev_timer_start (EV_A_ &once->to); |
1601 | ev_timer_start (EV_A_ &once->to); |
1579 | } |
1602 | } |
1580 | } |
1603 | } |
1581 | } |
1604 | } |
1582 | |
1605 | |
|
|
1606 | #ifdef __cplusplus |
|
|
1607 | } |
|
|
1608 | #endif |
|
|
1609 | |