… | |
… | |
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 |
|
|
40 | # ifndef EV_USE_MONOTONIC |
35 | # define EV_USE_MONOTONIC 1 |
41 | # define EV_USE_MONOTONIC 1 |
|
|
42 | # endif |
|
|
43 | # ifndef EV_USE_REALTIME |
36 | # define EV_USE_REALTIME 1 |
44 | # define EV_USE_REALTIME 1 |
|
|
45 | # endif |
37 | # endif |
46 | # endif |
38 | |
47 | |
39 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
48 | # if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) |
40 | # define EV_USE_SELECT 1 |
49 | # define EV_USE_SELECT 1 |
41 | # endif |
50 | # endif |
42 | |
51 | |
43 | # if HAVE_POLL && HAVE_POLL_H |
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
44 | # define EV_USE_POLL 1 |
53 | # define EV_USE_POLL 1 |
45 | # endif |
54 | # endif |
46 | |
55 | |
47 | # if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
56 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
48 | # define EV_USE_EPOLL 1 |
57 | # define EV_USE_EPOLL 1 |
49 | # endif |
58 | # endif |
50 | |
59 | |
51 | # if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
60 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) |
52 | # define EV_USE_KQUEUE 1 |
61 | # define EV_USE_KQUEUE 1 |
53 | # endif |
62 | # endif |
54 | |
63 | |
55 | #endif |
64 | #endif |
56 | |
65 | |
… | |
… | |
66 | #include <sys/types.h> |
75 | #include <sys/types.h> |
67 | #include <time.h> |
76 | #include <time.h> |
68 | |
77 | |
69 | #include <signal.h> |
78 | #include <signal.h> |
70 | |
79 | |
71 | #ifndef WIN32 |
80 | #ifndef _WIN32 |
72 | # include <unistd.h> |
81 | # include <unistd.h> |
73 | # include <sys/time.h> |
82 | # include <sys/time.h> |
74 | # include <sys/wait.h> |
83 | # include <sys/wait.h> |
|
|
84 | #else |
|
|
85 | # define WIN32_LEAN_AND_MEAN |
|
|
86 | # include <windows.h> |
|
|
87 | # ifndef EV_SELECT_IS_WINSOCKET |
|
|
88 | # define EV_SELECT_IS_WINSOCKET 1 |
75 | #endif |
89 | # endif |
|
|
90 | #endif |
|
|
91 | |
76 | /**/ |
92 | /**/ |
77 | |
93 | |
78 | #ifndef EV_USE_MONOTONIC |
94 | #ifndef EV_USE_MONOTONIC |
79 | # define EV_USE_MONOTONIC 1 |
95 | # define EV_USE_MONOTONIC 1 |
80 | #endif |
96 | #endif |
… | |
… | |
91 | # define EV_USE_EPOLL 0 |
107 | # define EV_USE_EPOLL 0 |
92 | #endif |
108 | #endif |
93 | |
109 | |
94 | #ifndef EV_USE_KQUEUE |
110 | #ifndef EV_USE_KQUEUE |
95 | # define EV_USE_KQUEUE 0 |
111 | # 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 |
|
|
106 | #endif |
112 | #endif |
107 | |
113 | |
108 | #ifndef EV_USE_REALTIME |
114 | #ifndef EV_USE_REALTIME |
109 | # define EV_USE_REALTIME 1 |
115 | # define EV_USE_REALTIME 1 |
110 | #endif |
116 | #endif |
… | |
… | |
117 | #endif |
123 | #endif |
118 | |
124 | |
119 | #ifndef CLOCK_REALTIME |
125 | #ifndef CLOCK_REALTIME |
120 | # undef EV_USE_REALTIME |
126 | # undef EV_USE_REALTIME |
121 | # define EV_USE_REALTIME 0 |
127 | # define EV_USE_REALTIME 0 |
|
|
128 | #endif |
|
|
129 | |
|
|
130 | #if EV_SELECT_IS_WINSOCKET |
|
|
131 | # include <winsock.h> |
122 | #endif |
132 | #endif |
123 | |
133 | |
124 | /**/ |
134 | /**/ |
125 | |
135 | |
126 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
136 | #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) */ |
137 | #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 */ |
138 | #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 */ |
139 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
130 | |
140 | |
|
|
141 | #ifdef EV_H |
|
|
142 | # include EV_H |
|
|
143 | #else |
131 | #include "ev.h" |
144 | # include "ev.h" |
|
|
145 | #endif |
132 | |
146 | |
133 | #if __GNUC__ >= 3 |
147 | #if __GNUC__ >= 3 |
134 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
148 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
135 | # define inline inline |
149 | # define inline inline |
136 | #else |
150 | #else |
… | |
… | |
142 | #define expect_true(expr) expect ((expr) != 0, 1) |
156 | #define expect_true(expr) expect ((expr) != 0, 1) |
143 | |
157 | |
144 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
158 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
145 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
159 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
146 | |
160 | |
|
|
161 | #define EMPTY /* required for microsofts broken pseudo-c compiler */ |
|
|
162 | |
147 | typedef struct ev_watcher *W; |
163 | typedef struct ev_watcher *W; |
148 | typedef struct ev_watcher_list *WL; |
164 | typedef struct ev_watcher_list *WL; |
149 | typedef struct ev_watcher_time *WT; |
165 | typedef struct ev_watcher_time *WT; |
150 | |
166 | |
151 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
167 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
152 | |
168 | |
|
|
169 | #ifdef _WIN32 |
153 | #include "ev_win32.c" |
170 | # include "ev_win32.c" |
|
|
171 | #endif |
154 | |
172 | |
155 | /*****************************************************************************/ |
173 | /*****************************************************************************/ |
156 | |
174 | |
157 | static void (*syserr_cb)(const char *msg); |
175 | static void (*syserr_cb)(const char *msg); |
158 | |
176 | |
… | |
… | |
205 | typedef struct |
223 | typedef struct |
206 | { |
224 | { |
207 | WL head; |
225 | WL head; |
208 | unsigned char events; |
226 | unsigned char events; |
209 | unsigned char reify; |
227 | unsigned char reify; |
|
|
228 | #if EV_SELECT_IS_WINSOCKET |
|
|
229 | SOCKET handle; |
|
|
230 | #endif |
210 | } ANFD; |
231 | } ANFD; |
211 | |
232 | |
212 | typedef struct |
233 | typedef struct |
213 | { |
234 | { |
214 | W w; |
235 | W w; |
215 | int events; |
236 | int events; |
216 | } ANPENDING; |
237 | } ANPENDING; |
217 | |
238 | |
218 | #if EV_MULTIPLICITY |
239 | #if EV_MULTIPLICITY |
219 | |
240 | |
220 | struct ev_loop |
241 | struct ev_loop |
221 | { |
242 | { |
|
|
243 | ev_tstamp ev_rt_now; |
|
|
244 | #define ev_rt_now ((loop)->ev_rt_now) |
222 | # define VAR(name,decl) decl; |
245 | #define VAR(name,decl) decl; |
223 | # include "ev_vars.h" |
246 | #include "ev_vars.h" |
224 | }; |
|
|
225 | # undef VAR |
247 | #undef VAR |
|
|
248 | }; |
226 | # include "ev_wrap.h" |
249 | #include "ev_wrap.h" |
|
|
250 | |
|
|
251 | struct ev_loop default_loop_struct; |
|
|
252 | static struct ev_loop *default_loop; |
227 | |
253 | |
228 | #else |
254 | #else |
229 | |
255 | |
|
|
256 | ev_tstamp ev_rt_now; |
230 | # define VAR(name,decl) static decl; |
257 | #define VAR(name,decl) static decl; |
231 | # include "ev_vars.h" |
258 | #include "ev_vars.h" |
232 | # undef VAR |
259 | #undef VAR |
|
|
260 | |
|
|
261 | static int default_loop; |
233 | |
262 | |
234 | #endif |
263 | #endif |
235 | |
264 | |
236 | /*****************************************************************************/ |
265 | /*****************************************************************************/ |
237 | |
266 | |
238 | inline ev_tstamp |
267 | ev_tstamp |
239 | ev_time (void) |
268 | ev_time (void) |
240 | { |
269 | { |
241 | #if EV_USE_REALTIME |
270 | #if EV_USE_REALTIME |
242 | struct timespec ts; |
271 | struct timespec ts; |
243 | clock_gettime (CLOCK_REALTIME, &ts); |
272 | clock_gettime (CLOCK_REALTIME, &ts); |
… | |
… | |
262 | #endif |
291 | #endif |
263 | |
292 | |
264 | return ev_time (); |
293 | return ev_time (); |
265 | } |
294 | } |
266 | |
295 | |
|
|
296 | #if EV_MULTIPLICITY |
267 | ev_tstamp |
297 | ev_tstamp |
268 | ev_now (EV_P) |
298 | ev_now (EV_P) |
269 | { |
299 | { |
270 | return rt_now; |
300 | return ev_rt_now; |
271 | } |
301 | } |
|
|
302 | #endif |
272 | |
303 | |
273 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
304 | #define array_roundsize(type,n) ((n) | 4 & ~3) |
274 | |
305 | |
275 | #define array_needsize(type,base,cur,cnt,init) \ |
306 | #define array_needsize(type,base,cur,cnt,init) \ |
276 | if (expect_false ((cnt) > cur)) \ |
307 | if (expect_false ((cnt) > cur)) \ |
… | |
… | |
293 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
324 | stem ## max = array_roundsize (stem ## cnt >> 1); \ |
294 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
325 | base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ |
295 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
326 | fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ |
296 | } |
327 | } |
297 | |
328 | |
298 | /* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ |
|
|
299 | /* bringing us everlasting joy in form of stupid extra macros that are not required in C */ |
|
|
300 | #define array_free_microshit(stem) \ |
|
|
301 | ev_free (stem ## s); stem ## cnt = stem ## max = 0; |
|
|
302 | |
|
|
303 | #define array_free(stem, idx) \ |
329 | #define array_free(stem, idx) \ |
304 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
330 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
305 | |
331 | |
306 | /*****************************************************************************/ |
332 | /*****************************************************************************/ |
307 | |
333 | |
… | |
… | |
316 | |
342 | |
317 | ++base; |
343 | ++base; |
318 | } |
344 | } |
319 | } |
345 | } |
320 | |
346 | |
321 | static void |
347 | void |
322 | event (EV_P_ W w, int events) |
348 | ev_feed_event (EV_P_ void *w, int revents) |
323 | { |
349 | { |
|
|
350 | W w_ = (W)w; |
|
|
351 | |
324 | if (w->pending) |
352 | if (w_->pending) |
325 | { |
353 | { |
326 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
354 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
327 | return; |
355 | return; |
328 | } |
356 | } |
329 | |
357 | |
330 | w->pending = ++pendingcnt [ABSPRI (w)]; |
358 | w_->pending = ++pendingcnt [ABSPRI (w_)]; |
331 | array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); |
359 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void)); |
332 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
360 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
333 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
361 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
334 | } |
362 | } |
335 | |
363 | |
336 | static void |
364 | static void |
337 | queue_events (EV_P_ W *events, int eventcnt, int type) |
365 | queue_events (EV_P_ W *events, int eventcnt, int type) |
338 | { |
366 | { |
339 | int i; |
367 | int i; |
340 | |
368 | |
341 | for (i = 0; i < eventcnt; ++i) |
369 | for (i = 0; i < eventcnt; ++i) |
342 | event (EV_A_ events [i], type); |
370 | ev_feed_event (EV_A_ events [i], type); |
343 | } |
371 | } |
344 | |
372 | |
345 | static void |
373 | inline void |
346 | fd_event (EV_P_ int fd, int events) |
374 | fd_event (EV_P_ int fd, int revents) |
347 | { |
375 | { |
348 | ANFD *anfd = anfds + fd; |
376 | ANFD *anfd = anfds + fd; |
349 | struct ev_io *w; |
377 | struct ev_io *w; |
350 | |
378 | |
351 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
379 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
352 | { |
380 | { |
353 | int ev = w->events & events; |
381 | int ev = w->events & revents; |
354 | |
382 | |
355 | if (ev) |
383 | if (ev) |
356 | event (EV_A_ (W)w, ev); |
384 | ev_feed_event (EV_A_ (W)w, ev); |
357 | } |
385 | } |
|
|
386 | } |
|
|
387 | |
|
|
388 | void |
|
|
389 | ev_feed_fd_event (EV_P_ int fd, int revents) |
|
|
390 | { |
|
|
391 | fd_event (EV_A_ fd, revents); |
358 | } |
392 | } |
359 | |
393 | |
360 | /*****************************************************************************/ |
394 | /*****************************************************************************/ |
361 | |
395 | |
362 | static void |
396 | static void |
… | |
… | |
373 | int events = 0; |
407 | int events = 0; |
374 | |
408 | |
375 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
409 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
376 | events |= w->events; |
410 | events |= w->events; |
377 | |
411 | |
|
|
412 | #if EV_SELECT_IS_WINSOCKET |
|
|
413 | if (events) |
|
|
414 | { |
|
|
415 | unsigned long argp; |
|
|
416 | anfd->handle = _get_osfhandle (fd); |
|
|
417 | assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); |
|
|
418 | } |
|
|
419 | #endif |
|
|
420 | |
378 | anfd->reify = 0; |
421 | anfd->reify = 0; |
379 | |
422 | |
380 | method_modify (EV_A_ fd, anfd->events, events); |
423 | method_modify (EV_A_ fd, anfd->events, events); |
381 | anfd->events = events; |
424 | anfd->events = events; |
382 | } |
425 | } |
… | |
… | |
403 | struct ev_io *w; |
446 | struct ev_io *w; |
404 | |
447 | |
405 | while ((w = (struct ev_io *)anfds [fd].head)) |
448 | while ((w = (struct ev_io *)anfds [fd].head)) |
406 | { |
449 | { |
407 | ev_io_stop (EV_A_ w); |
450 | ev_io_stop (EV_A_ w); |
408 | event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
451 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
409 | } |
452 | } |
410 | } |
453 | } |
411 | |
454 | |
412 | static int |
455 | static int |
413 | fd_valid (int fd) |
456 | fd_valid (int fd) |
414 | { |
457 | { |
415 | #ifdef WIN32 |
458 | #ifdef _WIN32 |
416 | return !!win32_get_osfhandle (fd); |
459 | return _get_osfhandle (fd) != -1; |
417 | #else |
460 | #else |
418 | return fcntl (fd, F_GETFD) != -1; |
461 | return fcntl (fd, F_GETFD) != -1; |
419 | #endif |
462 | #endif |
420 | } |
463 | } |
421 | |
464 | |
… | |
… | |
501 | |
544 | |
502 | heap [k] = w; |
545 | heap [k] = w; |
503 | ((W)heap [k])->active = k + 1; |
546 | ((W)heap [k])->active = k + 1; |
504 | } |
547 | } |
505 | |
548 | |
|
|
549 | inline void |
|
|
550 | adjustheap (WT *heap, int N, int k) |
|
|
551 | { |
|
|
552 | upheap (heap, k); |
|
|
553 | downheap (heap, N, k); |
|
|
554 | } |
|
|
555 | |
506 | /*****************************************************************************/ |
556 | /*****************************************************************************/ |
507 | |
557 | |
508 | typedef struct |
558 | typedef struct |
509 | { |
559 | { |
510 | WL head; |
560 | WL head; |
… | |
… | |
531 | } |
581 | } |
532 | |
582 | |
533 | static void |
583 | static void |
534 | sighandler (int signum) |
584 | sighandler (int signum) |
535 | { |
585 | { |
536 | #if WIN32 |
586 | #if _WIN32 |
537 | signal (signum, sighandler); |
587 | signal (signum, sighandler); |
538 | #endif |
588 | #endif |
539 | |
589 | |
540 | signals [signum - 1].gotsig = 1; |
590 | signals [signum - 1].gotsig = 1; |
541 | |
591 | |
542 | if (!gotsig) |
592 | if (!gotsig) |
543 | { |
593 | { |
544 | int old_errno = errno; |
594 | int old_errno = errno; |
545 | gotsig = 1; |
595 | gotsig = 1; |
546 | #ifdef WIN32 |
|
|
547 | send (sigpipe [1], &signum, 1, MSG_DONTWAIT); |
|
|
548 | #else |
|
|
549 | write (sigpipe [1], &signum, 1); |
596 | write (sigpipe [1], &signum, 1); |
550 | #endif |
|
|
551 | errno = old_errno; |
597 | errno = old_errno; |
552 | } |
598 | } |
553 | } |
599 | } |
554 | |
600 | |
|
|
601 | void |
|
|
602 | ev_feed_signal_event (EV_P_ int signum) |
|
|
603 | { |
|
|
604 | WL w; |
|
|
605 | |
|
|
606 | #if EV_MULTIPLICITY |
|
|
607 | assert (("feeding signal events is only supported in the default loop", loop == default_loop)); |
|
|
608 | #endif |
|
|
609 | |
|
|
610 | --signum; |
|
|
611 | |
|
|
612 | if (signum < 0 || signum >= signalmax) |
|
|
613 | return; |
|
|
614 | |
|
|
615 | signals [signum].gotsig = 0; |
|
|
616 | |
|
|
617 | for (w = signals [signum].head; w; w = w->next) |
|
|
618 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
|
|
619 | } |
|
|
620 | |
555 | static void |
621 | static void |
556 | sigcb (EV_P_ struct ev_io *iow, int revents) |
622 | sigcb (EV_P_ struct ev_io *iow, int revents) |
557 | { |
623 | { |
558 | WL w; |
|
|
559 | int signum; |
624 | int signum; |
560 | |
625 | |
561 | #ifdef WIN32 |
|
|
562 | recv (sigpipe [0], &revents, 1, MSG_DONTWAIT); |
|
|
563 | #else |
|
|
564 | read (sigpipe [0], &revents, 1); |
626 | read (sigpipe [0], &revents, 1); |
565 | #endif |
|
|
566 | gotsig = 0; |
627 | gotsig = 0; |
567 | |
628 | |
568 | for (signum = signalmax; signum--; ) |
629 | for (signum = signalmax; signum--; ) |
569 | if (signals [signum].gotsig) |
630 | if (signals [signum].gotsig) |
570 | { |
631 | ev_feed_signal_event (EV_A_ signum + 1); |
571 | signals [signum].gotsig = 0; |
632 | } |
572 | |
633 | |
573 | for (w = signals [signum].head; w; w = w->next) |
634 | inline void |
574 | event (EV_A_ (W)w, EV_SIGNAL); |
635 | fd_intern (int fd) |
575 | } |
636 | { |
|
|
637 | #ifdef _WIN32 |
|
|
638 | int arg = 1; |
|
|
639 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
|
|
640 | #else |
|
|
641 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
|
|
642 | fcntl (fd, F_SETFL, O_NONBLOCK); |
|
|
643 | #endif |
576 | } |
644 | } |
577 | |
645 | |
578 | static void |
646 | static void |
579 | siginit (EV_P) |
647 | siginit (EV_P) |
580 | { |
648 | { |
581 | #ifndef WIN32 |
649 | fd_intern (sigpipe [0]); |
582 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
650 | fd_intern (sigpipe [1]); |
583 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
|
|
584 | |
|
|
585 | /* rather than sort out wether we really need nb, set it */ |
|
|
586 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
|
|
587 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
588 | #endif |
|
|
589 | |
651 | |
590 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
652 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
591 | ev_io_start (EV_A_ &sigev); |
653 | ev_io_start (EV_A_ &sigev); |
592 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
654 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
593 | } |
655 | } |
594 | |
656 | |
595 | /*****************************************************************************/ |
657 | /*****************************************************************************/ |
596 | |
658 | |
597 | static struct ev_child *childs [PID_HASHSIZE]; |
659 | static struct ev_child *childs [PID_HASHSIZE]; |
598 | |
660 | |
599 | #ifndef WIN32 |
661 | #ifndef _WIN32 |
600 | |
662 | |
601 | static struct ev_signal childev; |
663 | static struct ev_signal childev; |
602 | |
664 | |
603 | #ifndef WCONTINUED |
665 | #ifndef WCONTINUED |
604 | # define WCONTINUED 0 |
666 | # define WCONTINUED 0 |
… | |
… | |
613 | if (w->pid == pid || !w->pid) |
675 | if (w->pid == pid || !w->pid) |
614 | { |
676 | { |
615 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
677 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
616 | w->rpid = pid; |
678 | w->rpid = pid; |
617 | w->rstatus = status; |
679 | w->rstatus = status; |
618 | event (EV_A_ (W)w, EV_CHILD); |
680 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
619 | } |
681 | } |
620 | } |
682 | } |
621 | |
683 | |
622 | static void |
684 | static void |
623 | childcb (EV_P_ struct ev_signal *sw, int revents) |
685 | childcb (EV_P_ struct ev_signal *sw, int revents) |
… | |
… | |
625 | int pid, status; |
687 | int pid, status; |
626 | |
688 | |
627 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
689 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
628 | { |
690 | { |
629 | /* make sure we are called again until all childs have been reaped */ |
691 | /* make sure we are called again until all childs have been reaped */ |
630 | event (EV_A_ (W)sw, EV_SIGNAL); |
692 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
631 | |
693 | |
632 | child_reap (EV_A_ sw, pid, pid, status); |
694 | child_reap (EV_A_ sw, pid, pid, status); |
633 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
695 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
634 | } |
696 | } |
635 | } |
697 | } |
… | |
… | |
665 | |
727 | |
666 | /* return true if we are running with elevated privileges and should ignore env variables */ |
728 | /* return true if we are running with elevated privileges and should ignore env variables */ |
667 | static int |
729 | static int |
668 | enable_secure (void) |
730 | enable_secure (void) |
669 | { |
731 | { |
670 | #ifdef WIN32 |
732 | #ifdef _WIN32 |
671 | return 0; |
733 | return 0; |
672 | #else |
734 | #else |
673 | return getuid () != geteuid () |
735 | return getuid () != geteuid () |
674 | || getgid () != getegid (); |
736 | || getgid () != getegid (); |
675 | #endif |
737 | #endif |
… | |
… | |
692 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
754 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
693 | have_monotonic = 1; |
755 | have_monotonic = 1; |
694 | } |
756 | } |
695 | #endif |
757 | #endif |
696 | |
758 | |
697 | rt_now = ev_time (); |
759 | ev_rt_now = ev_time (); |
698 | mn_now = get_clock (); |
760 | mn_now = get_clock (); |
699 | now_floor = mn_now; |
761 | now_floor = mn_now; |
700 | rtmn_diff = rt_now - mn_now; |
762 | rtmn_diff = ev_rt_now - mn_now; |
701 | |
763 | |
702 | if (methods == EVMETHOD_AUTO) |
764 | if (methods == EVMETHOD_AUTO) |
703 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
765 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
704 | methods = atoi (getenv ("LIBEV_METHODS")); |
766 | methods = atoi (getenv ("LIBEV_METHODS")); |
705 | else |
767 | else |
706 | methods = EVMETHOD_ANY; |
768 | methods = EVMETHOD_ANY; |
707 | |
769 | |
708 | method = 0; |
770 | method = 0; |
709 | #if EV_USE_WIN32 |
|
|
710 | if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods); |
|
|
711 | #endif |
|
|
712 | #if EV_USE_KQUEUE |
771 | #if EV_USE_KQUEUE |
713 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
772 | if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); |
714 | #endif |
773 | #endif |
715 | #if EV_USE_EPOLL |
774 | #if EV_USE_EPOLL |
716 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
775 | if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); |
… | |
… | |
720 | #endif |
779 | #endif |
721 | #if EV_USE_SELECT |
780 | #if EV_USE_SELECT |
722 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
781 | if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); |
723 | #endif |
782 | #endif |
724 | |
783 | |
725 | ev_watcher_init (&sigev, sigcb); |
784 | ev_init (&sigev, sigcb); |
726 | ev_set_priority (&sigev, EV_MAXPRI); |
785 | ev_set_priority (&sigev, EV_MAXPRI); |
727 | } |
786 | } |
728 | } |
787 | } |
729 | |
788 | |
730 | void |
789 | void |
731 | loop_destroy (EV_P) |
790 | loop_destroy (EV_P) |
732 | { |
791 | { |
733 | int i; |
792 | int i; |
734 | |
793 | |
735 | #if EV_USE_WIN32 |
|
|
736 | if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); |
|
|
737 | #endif |
|
|
738 | #if EV_USE_KQUEUE |
794 | #if EV_USE_KQUEUE |
739 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
795 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
740 | #endif |
796 | #endif |
741 | #if EV_USE_EPOLL |
797 | #if EV_USE_EPOLL |
742 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
798 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
… | |
… | |
750 | |
806 | |
751 | for (i = NUMPRI; i--; ) |
807 | for (i = NUMPRI; i--; ) |
752 | array_free (pending, [i]); |
808 | array_free (pending, [i]); |
753 | |
809 | |
754 | /* have to use the microsoft-never-gets-it-right macro */ |
810 | /* have to use the microsoft-never-gets-it-right macro */ |
755 | array_free_microshit (fdchange); |
811 | array_free (fdchange, EMPTY); |
756 | array_free_microshit (timer); |
812 | array_free (timer, EMPTY); |
757 | array_free_microshit (periodic); |
813 | #if EV_PERIODICS |
758 | array_free_microshit (idle); |
814 | array_free (periodic, EMPTY); |
759 | array_free_microshit (prepare); |
815 | #endif |
760 | array_free_microshit (check); |
816 | array_free (idle, EMPTY); |
|
|
817 | array_free (prepare, EMPTY); |
|
|
818 | array_free (check, EMPTY); |
761 | |
819 | |
762 | method = 0; |
820 | method = 0; |
763 | } |
821 | } |
764 | |
822 | |
765 | static void |
823 | static void |
… | |
… | |
820 | } |
878 | } |
821 | |
879 | |
822 | #endif |
880 | #endif |
823 | |
881 | |
824 | #if EV_MULTIPLICITY |
882 | #if EV_MULTIPLICITY |
825 | struct ev_loop default_loop_struct; |
|
|
826 | static struct ev_loop *default_loop; |
|
|
827 | |
|
|
828 | struct ev_loop * |
883 | struct ev_loop * |
829 | #else |
884 | #else |
830 | static int default_loop; |
|
|
831 | |
|
|
832 | int |
885 | int |
833 | #endif |
886 | #endif |
834 | ev_default_loop (int methods) |
887 | ev_default_loop (int methods) |
835 | { |
888 | { |
836 | if (sigpipe [0] == sigpipe [1]) |
889 | if (sigpipe [0] == sigpipe [1]) |
… | |
… | |
849 | |
902 | |
850 | if (ev_method (EV_A)) |
903 | if (ev_method (EV_A)) |
851 | { |
904 | { |
852 | siginit (EV_A); |
905 | siginit (EV_A); |
853 | |
906 | |
854 | #ifndef WIN32 |
907 | #ifndef _WIN32 |
855 | ev_signal_init (&childev, childcb, SIGCHLD); |
908 | ev_signal_init (&childev, childcb, SIGCHLD); |
856 | ev_set_priority (&childev, EV_MAXPRI); |
909 | ev_set_priority (&childev, EV_MAXPRI); |
857 | ev_signal_start (EV_A_ &childev); |
910 | ev_signal_start (EV_A_ &childev); |
858 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
911 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
859 | #endif |
912 | #endif |
… | |
… | |
870 | { |
923 | { |
871 | #if EV_MULTIPLICITY |
924 | #if EV_MULTIPLICITY |
872 | struct ev_loop *loop = default_loop; |
925 | struct ev_loop *loop = default_loop; |
873 | #endif |
926 | #endif |
874 | |
927 | |
875 | #ifndef WIN32 |
928 | #ifndef _WIN32 |
876 | ev_ref (EV_A); /* child watcher */ |
929 | ev_ref (EV_A); /* child watcher */ |
877 | ev_signal_stop (EV_A_ &childev); |
930 | ev_signal_stop (EV_A_ &childev); |
878 | #endif |
931 | #endif |
879 | |
932 | |
880 | ev_ref (EV_A); /* signal watcher */ |
933 | ev_ref (EV_A); /* signal watcher */ |
… | |
… | |
922 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
975 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
923 | |
976 | |
924 | if (p->w) |
977 | if (p->w) |
925 | { |
978 | { |
926 | p->w->pending = 0; |
979 | p->w->pending = 0; |
927 | p->w->cb (EV_A_ p->w, p->events); |
980 | EV_CB_INVOKE (p->w, p->events); |
928 | } |
981 | } |
929 | } |
982 | } |
930 | } |
983 | } |
931 | |
984 | |
932 | static void |
985 | static void |
… | |
… | |
940 | |
993 | |
941 | /* first reschedule or stop timer */ |
994 | /* first reschedule or stop timer */ |
942 | if (w->repeat) |
995 | if (w->repeat) |
943 | { |
996 | { |
944 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
997 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
|
|
998 | |
945 | ((WT)w)->at = mn_now + w->repeat; |
999 | ((WT)w)->at += w->repeat; |
|
|
1000 | if (((WT)w)->at < mn_now) |
|
|
1001 | ((WT)w)->at = mn_now; |
|
|
1002 | |
946 | downheap ((WT *)timers, timercnt, 0); |
1003 | downheap ((WT *)timers, timercnt, 0); |
947 | } |
1004 | } |
948 | else |
1005 | else |
949 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1006 | ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ |
950 | |
1007 | |
951 | event (EV_A_ (W)w, EV_TIMEOUT); |
1008 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
952 | } |
1009 | } |
953 | } |
1010 | } |
954 | |
1011 | |
|
|
1012 | #if EV_PERIODICS |
955 | static void |
1013 | static void |
956 | periodics_reify (EV_P) |
1014 | periodics_reify (EV_P) |
957 | { |
1015 | { |
958 | while (periodiccnt && ((WT)periodics [0])->at <= rt_now) |
1016 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
959 | { |
1017 | { |
960 | struct ev_periodic *w = periodics [0]; |
1018 | struct ev_periodic *w = periodics [0]; |
961 | |
1019 | |
962 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1020 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
963 | |
1021 | |
964 | /* first reschedule or stop timer */ |
1022 | /* first reschedule or stop timer */ |
965 | if (w->reschedule_cb) |
1023 | if (w->reschedule_cb) |
966 | { |
1024 | { |
967 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001); |
1025 | ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001); |
968 | |
1026 | |
969 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now)); |
1027 | assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now)); |
970 | downheap ((WT *)periodics, periodiccnt, 0); |
1028 | downheap ((WT *)periodics, periodiccnt, 0); |
971 | } |
1029 | } |
972 | else if (w->interval) |
1030 | else if (w->interval) |
973 | { |
1031 | { |
974 | ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
1032 | ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; |
975 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); |
1033 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now)); |
976 | downheap ((WT *)periodics, periodiccnt, 0); |
1034 | downheap ((WT *)periodics, periodiccnt, 0); |
977 | } |
1035 | } |
978 | else |
1036 | else |
979 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
1037 | ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ |
980 | |
1038 | |
981 | event (EV_A_ (W)w, EV_PERIODIC); |
1039 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
982 | } |
1040 | } |
983 | } |
1041 | } |
984 | |
1042 | |
985 | static void |
1043 | static void |
986 | periodics_reschedule (EV_P) |
1044 | periodics_reschedule (EV_P) |
… | |
… | |
991 | for (i = 0; i < periodiccnt; ++i) |
1049 | for (i = 0; i < periodiccnt; ++i) |
992 | { |
1050 | { |
993 | struct ev_periodic *w = periodics [i]; |
1051 | struct ev_periodic *w = periodics [i]; |
994 | |
1052 | |
995 | if (w->reschedule_cb) |
1053 | if (w->reschedule_cb) |
996 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1054 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
997 | else if (w->interval) |
1055 | else if (w->interval) |
998 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1056 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
999 | } |
1057 | } |
1000 | |
1058 | |
1001 | /* now rebuild the heap */ |
1059 | /* now rebuild the heap */ |
1002 | for (i = periodiccnt >> 1; i--; ) |
1060 | for (i = periodiccnt >> 1; i--; ) |
1003 | downheap ((WT *)periodics, periodiccnt, i); |
1061 | downheap ((WT *)periodics, periodiccnt, i); |
1004 | } |
1062 | } |
|
|
1063 | #endif |
1005 | |
1064 | |
1006 | inline int |
1065 | inline int |
1007 | time_update_monotonic (EV_P) |
1066 | time_update_monotonic (EV_P) |
1008 | { |
1067 | { |
1009 | mn_now = get_clock (); |
1068 | mn_now = get_clock (); |
1010 | |
1069 | |
1011 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1070 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1012 | { |
1071 | { |
1013 | rt_now = rtmn_diff + mn_now; |
1072 | ev_rt_now = rtmn_diff + mn_now; |
1014 | return 0; |
1073 | return 0; |
1015 | } |
1074 | } |
1016 | else |
1075 | else |
1017 | { |
1076 | { |
1018 | now_floor = mn_now; |
1077 | now_floor = mn_now; |
1019 | rt_now = ev_time (); |
1078 | ev_rt_now = ev_time (); |
1020 | return 1; |
1079 | return 1; |
1021 | } |
1080 | } |
1022 | } |
1081 | } |
1023 | |
1082 | |
1024 | static void |
1083 | static void |
… | |
… | |
1033 | { |
1092 | { |
1034 | ev_tstamp odiff = rtmn_diff; |
1093 | ev_tstamp odiff = rtmn_diff; |
1035 | |
1094 | |
1036 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1095 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1037 | { |
1096 | { |
1038 | rtmn_diff = rt_now - mn_now; |
1097 | rtmn_diff = ev_rt_now - mn_now; |
1039 | |
1098 | |
1040 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1099 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1041 | return; /* all is well */ |
1100 | return; /* all is well */ |
1042 | |
1101 | |
1043 | rt_now = ev_time (); |
1102 | ev_rt_now = ev_time (); |
1044 | mn_now = get_clock (); |
1103 | mn_now = get_clock (); |
1045 | now_floor = mn_now; |
1104 | now_floor = mn_now; |
1046 | } |
1105 | } |
1047 | |
1106 | |
|
|
1107 | # if EV_PERIODICS |
1048 | periodics_reschedule (EV_A); |
1108 | periodics_reschedule (EV_A); |
|
|
1109 | # endif |
1049 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1110 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1050 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1111 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1051 | } |
1112 | } |
1052 | } |
1113 | } |
1053 | else |
1114 | else |
1054 | #endif |
1115 | #endif |
1055 | { |
1116 | { |
1056 | rt_now = ev_time (); |
1117 | ev_rt_now = ev_time (); |
1057 | |
1118 | |
1058 | if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1119 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1059 | { |
1120 | { |
|
|
1121 | #if EV_PERIODICS |
1060 | periodics_reschedule (EV_A); |
1122 | periodics_reschedule (EV_A); |
|
|
1123 | #endif |
1061 | |
1124 | |
1062 | /* adjust timers. this is easy, as the offset is the same for all */ |
1125 | /* adjust timers. this is easy, as the offset is the same for all */ |
1063 | for (i = 0; i < timercnt; ++i) |
1126 | for (i = 0; i < timercnt; ++i) |
1064 | ((WT)timers [i])->at += rt_now - mn_now; |
1127 | ((WT)timers [i])->at += ev_rt_now - mn_now; |
1065 | } |
1128 | } |
1066 | |
1129 | |
1067 | mn_now = rt_now; |
1130 | mn_now = ev_rt_now; |
1068 | } |
1131 | } |
1069 | } |
1132 | } |
1070 | |
1133 | |
1071 | void |
1134 | void |
1072 | ev_ref (EV_P) |
1135 | ev_ref (EV_P) |
… | |
… | |
1112 | if (expect_true (have_monotonic)) |
1175 | if (expect_true (have_monotonic)) |
1113 | time_update_monotonic (EV_A); |
1176 | time_update_monotonic (EV_A); |
1114 | else |
1177 | else |
1115 | #endif |
1178 | #endif |
1116 | { |
1179 | { |
1117 | rt_now = ev_time (); |
1180 | ev_rt_now = ev_time (); |
1118 | mn_now = rt_now; |
1181 | mn_now = ev_rt_now; |
1119 | } |
1182 | } |
1120 | |
1183 | |
1121 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1184 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1122 | block = 0.; |
1185 | block = 0.; |
1123 | else |
1186 | else |
… | |
… | |
1128 | { |
1191 | { |
1129 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1192 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1130 | if (block > to) block = to; |
1193 | if (block > to) block = to; |
1131 | } |
1194 | } |
1132 | |
1195 | |
|
|
1196 | #if EV_PERIODICS |
1133 | if (periodiccnt) |
1197 | if (periodiccnt) |
1134 | { |
1198 | { |
1135 | ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge; |
1199 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1136 | if (block > to) block = to; |
1200 | if (block > to) block = to; |
1137 | } |
1201 | } |
|
|
1202 | #endif |
1138 | |
1203 | |
1139 | if (block < 0.) block = 0.; |
1204 | if (block < 0.) block = 0.; |
1140 | } |
1205 | } |
1141 | |
1206 | |
1142 | method_poll (EV_A_ block); |
1207 | method_poll (EV_A_ block); |
1143 | |
1208 | |
1144 | /* update rt_now, do magic */ |
1209 | /* update ev_rt_now, do magic */ |
1145 | time_update (EV_A); |
1210 | time_update (EV_A); |
1146 | |
1211 | |
1147 | /* queue pending timers and reschedule them */ |
1212 | /* queue pending timers and reschedule them */ |
1148 | timers_reify (EV_A); /* relative timers called last */ |
1213 | timers_reify (EV_A); /* relative timers called last */ |
|
|
1214 | #if EV_PERIODICS |
1149 | periodics_reify (EV_A); /* absolute timers called first */ |
1215 | periodics_reify (EV_A); /* absolute timers called first */ |
|
|
1216 | #endif |
1150 | |
1217 | |
1151 | /* queue idle watchers unless io or timers are pending */ |
1218 | /* queue idle watchers unless io or timers are pending */ |
1152 | if (idlecnt && !any_pending (EV_A)) |
1219 | if (idlecnt && !any_pending (EV_A)) |
1153 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1220 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1154 | |
1221 | |
… | |
… | |
1245 | { |
1312 | { |
1246 | ev_clear_pending (EV_A_ (W)w); |
1313 | ev_clear_pending (EV_A_ (W)w); |
1247 | if (!ev_is_active (w)) |
1314 | if (!ev_is_active (w)) |
1248 | return; |
1315 | return; |
1249 | |
1316 | |
|
|
1317 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
|
|
1318 | |
1250 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1319 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1251 | ev_stop (EV_A_ (W)w); |
1320 | ev_stop (EV_A_ (W)w); |
1252 | |
1321 | |
1253 | fd_change (EV_A_ w->fd); |
1322 | fd_change (EV_A_ w->fd); |
1254 | } |
1323 | } |
… | |
… | |
1281 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1350 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1282 | |
1351 | |
1283 | if (((W)w)->active < timercnt--) |
1352 | if (((W)w)->active < timercnt--) |
1284 | { |
1353 | { |
1285 | timers [((W)w)->active - 1] = timers [timercnt]; |
1354 | timers [((W)w)->active - 1] = timers [timercnt]; |
1286 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1355 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1287 | } |
1356 | } |
1288 | |
1357 | |
1289 | ((WT)w)->at = w->repeat; |
1358 | ((WT)w)->at -= mn_now; |
1290 | |
1359 | |
1291 | ev_stop (EV_A_ (W)w); |
1360 | ev_stop (EV_A_ (W)w); |
1292 | } |
1361 | } |
1293 | |
1362 | |
1294 | void |
1363 | void |
… | |
… | |
1297 | if (ev_is_active (w)) |
1366 | if (ev_is_active (w)) |
1298 | { |
1367 | { |
1299 | if (w->repeat) |
1368 | if (w->repeat) |
1300 | { |
1369 | { |
1301 | ((WT)w)->at = mn_now + w->repeat; |
1370 | ((WT)w)->at = mn_now + w->repeat; |
1302 | downheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1371 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1303 | } |
1372 | } |
1304 | else |
1373 | else |
1305 | ev_timer_stop (EV_A_ w); |
1374 | ev_timer_stop (EV_A_ w); |
1306 | } |
1375 | } |
1307 | else if (w->repeat) |
1376 | else if (w->repeat) |
1308 | ev_timer_start (EV_A_ w); |
1377 | ev_timer_start (EV_A_ w); |
1309 | } |
1378 | } |
1310 | |
1379 | |
|
|
1380 | #if EV_PERIODICS |
1311 | void |
1381 | void |
1312 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1382 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1313 | { |
1383 | { |
1314 | if (ev_is_active (w)) |
1384 | if (ev_is_active (w)) |
1315 | return; |
1385 | return; |
1316 | |
1386 | |
1317 | if (w->reschedule_cb) |
1387 | if (w->reschedule_cb) |
1318 | ((WT)w)->at = w->reschedule_cb (w, rt_now); |
1388 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1319 | else if (w->interval) |
1389 | else if (w->interval) |
1320 | { |
1390 | { |
1321 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1391 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
1322 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1392 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1323 | ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1393 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1324 | } |
1394 | } |
1325 | |
1395 | |
1326 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1396 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1327 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1397 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void)); |
1328 | periodics [periodiccnt - 1] = w; |
1398 | periodics [periodiccnt - 1] = w; |
… | |
… | |
1341 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1411 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1342 | |
1412 | |
1343 | if (((W)w)->active < periodiccnt--) |
1413 | if (((W)w)->active < periodiccnt--) |
1344 | { |
1414 | { |
1345 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1415 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1346 | downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1416 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1347 | } |
1417 | } |
1348 | |
1418 | |
1349 | ev_stop (EV_A_ (W)w); |
1419 | ev_stop (EV_A_ (W)w); |
1350 | } |
1420 | } |
1351 | |
1421 | |
1352 | void |
1422 | void |
1353 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1423 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1354 | { |
1424 | { |
|
|
1425 | /* TODO: use adjustheap and recalculation */ |
1355 | ev_periodic_stop (EV_A_ w); |
1426 | ev_periodic_stop (EV_A_ w); |
1356 | ev_periodic_start (EV_A_ w); |
1427 | ev_periodic_start (EV_A_ w); |
1357 | } |
1428 | } |
|
|
1429 | #endif |
1358 | |
1430 | |
1359 | void |
1431 | void |
1360 | ev_idle_start (EV_P_ struct ev_idle *w) |
1432 | ev_idle_start (EV_P_ struct ev_idle *w) |
1361 | { |
1433 | { |
1362 | if (ev_is_active (w)) |
1434 | if (ev_is_active (w)) |
… | |
… | |
1369 | |
1441 | |
1370 | void |
1442 | void |
1371 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1443 | ev_idle_stop (EV_P_ struct ev_idle *w) |
1372 | { |
1444 | { |
1373 | ev_clear_pending (EV_A_ (W)w); |
1445 | ev_clear_pending (EV_A_ (W)w); |
1374 | if (ev_is_active (w)) |
1446 | if (!ev_is_active (w)) |
1375 | return; |
1447 | return; |
1376 | |
1448 | |
1377 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1449 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
1378 | ev_stop (EV_A_ (W)w); |
1450 | ev_stop (EV_A_ (W)w); |
1379 | } |
1451 | } |
… | |
… | |
1391 | |
1463 | |
1392 | void |
1464 | void |
1393 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1465 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
1394 | { |
1466 | { |
1395 | ev_clear_pending (EV_A_ (W)w); |
1467 | ev_clear_pending (EV_A_ (W)w); |
1396 | if (ev_is_active (w)) |
1468 | if (!ev_is_active (w)) |
1397 | return; |
1469 | return; |
1398 | |
1470 | |
1399 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1471 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
1400 | ev_stop (EV_A_ (W)w); |
1472 | ev_stop (EV_A_ (W)w); |
1401 | } |
1473 | } |
… | |
… | |
1413 | |
1485 | |
1414 | void |
1486 | void |
1415 | ev_check_stop (EV_P_ struct ev_check *w) |
1487 | ev_check_stop (EV_P_ struct ev_check *w) |
1416 | { |
1488 | { |
1417 | ev_clear_pending (EV_A_ (W)w); |
1489 | ev_clear_pending (EV_A_ (W)w); |
1418 | if (ev_is_active (w)) |
1490 | if (!ev_is_active (w)) |
1419 | return; |
1491 | return; |
1420 | |
1492 | |
1421 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1493 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
1422 | ev_stop (EV_A_ (W)w); |
1494 | ev_stop (EV_A_ (W)w); |
1423 | } |
1495 | } |
… | |
… | |
1441 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1513 | array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); |
1442 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1514 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1443 | |
1515 | |
1444 | if (!((WL)w)->next) |
1516 | if (!((WL)w)->next) |
1445 | { |
1517 | { |
1446 | #if WIN32 |
1518 | #if _WIN32 |
1447 | signal (w->signum, sighandler); |
1519 | signal (w->signum, sighandler); |
1448 | #else |
1520 | #else |
1449 | struct sigaction sa; |
1521 | struct sigaction sa; |
1450 | sa.sa_handler = sighandler; |
1522 | sa.sa_handler = sighandler; |
1451 | sigfillset (&sa.sa_mask); |
1523 | sigfillset (&sa.sa_mask); |
… | |
… | |
1484 | |
1556 | |
1485 | void |
1557 | void |
1486 | ev_child_stop (EV_P_ struct ev_child *w) |
1558 | ev_child_stop (EV_P_ struct ev_child *w) |
1487 | { |
1559 | { |
1488 | ev_clear_pending (EV_A_ (W)w); |
1560 | ev_clear_pending (EV_A_ (W)w); |
1489 | if (ev_is_active (w)) |
1561 | if (!ev_is_active (w)) |
1490 | return; |
1562 | return; |
1491 | |
1563 | |
1492 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1564 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1493 | ev_stop (EV_A_ (W)w); |
1565 | ev_stop (EV_A_ (W)w); |
1494 | } |
1566 | } |
… | |
… | |
1538 | else |
1610 | else |
1539 | { |
1611 | { |
1540 | once->cb = cb; |
1612 | once->cb = cb; |
1541 | once->arg = arg; |
1613 | once->arg = arg; |
1542 | |
1614 | |
1543 | ev_watcher_init (&once->io, once_cb_io); |
1615 | ev_init (&once->io, once_cb_io); |
1544 | if (fd >= 0) |
1616 | if (fd >= 0) |
1545 | { |
1617 | { |
1546 | ev_io_set (&once->io, fd, events); |
1618 | ev_io_set (&once->io, fd, events); |
1547 | ev_io_start (EV_A_ &once->io); |
1619 | ev_io_start (EV_A_ &once->io); |
1548 | } |
1620 | } |
1549 | |
1621 | |
1550 | ev_watcher_init (&once->to, once_cb_to); |
1622 | ev_init (&once->to, once_cb_to); |
1551 | if (timeout >= 0.) |
1623 | if (timeout >= 0.) |
1552 | { |
1624 | { |
1553 | ev_timer_set (&once->to, timeout, 0.); |
1625 | ev_timer_set (&once->to, timeout, 0.); |
1554 | ev_timer_start (EV_A_ &once->to); |
1626 | ev_timer_start (EV_A_ &once->to); |
1555 | } |
1627 | } |
1556 | } |
1628 | } |
1557 | } |
1629 | } |
1558 | |
1630 | |
|
|
1631 | #ifdef __cplusplus |
|
|
1632 | } |
|
|
1633 | #endif |
|
|
1634 | |