… | |
… | |
32 | #ifdef __cplusplus |
32 | #ifdef __cplusplus |
33 | extern "C" { |
33 | extern "C" { |
34 | #endif |
34 | #endif |
35 | |
35 | |
36 | #ifndef EV_STANDALONE |
36 | #ifndef EV_STANDALONE |
|
|
37 | # ifdef EV_CONFIG_H |
|
|
38 | # include EV_CONFIG_H |
|
|
39 | # else |
37 | # include "config.h" |
40 | # include "config.h" |
|
|
41 | # endif |
38 | |
42 | |
39 | # if HAVE_CLOCK_GETTIME |
43 | # if HAVE_CLOCK_GETTIME |
40 | # ifndef EV_USE_MONOTONIC |
44 | # ifndef EV_USE_MONOTONIC |
41 | # define EV_USE_MONOTONIC 1 |
45 | # define EV_USE_MONOTONIC 1 |
42 | # endif |
46 | # endif |
43 | # ifndef EV_USE_REALTIME |
47 | # ifndef EV_USE_REALTIME |
44 | # define EV_USE_REALTIME 1 |
48 | # define EV_USE_REALTIME 1 |
45 | # endif |
49 | # endif |
|
|
50 | # else |
|
|
51 | # ifndef EV_USE_MONOTONIC |
|
|
52 | # define EV_USE_MONOTONIC 0 |
|
|
53 | # endif |
|
|
54 | # ifndef EV_USE_REALTIME |
|
|
55 | # define EV_USE_REALTIME 0 |
|
|
56 | # endif |
46 | # endif |
57 | # endif |
47 | |
58 | |
48 | # if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT) |
59 | # ifndef EV_USE_SELECT |
|
|
60 | # if HAVE_SELECT && HAVE_SYS_SELECT_H |
49 | # define EV_USE_SELECT 1 |
61 | # define EV_USE_SELECT 1 |
|
|
62 | # else |
|
|
63 | # define EV_USE_SELECT 0 |
|
|
64 | # endif |
50 | # endif |
65 | # endif |
51 | |
66 | |
52 | # if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL) |
67 | # ifndef EV_USE_POLL |
|
|
68 | # if HAVE_POLL && HAVE_POLL_H |
53 | # define EV_USE_POLL 1 |
69 | # define EV_USE_POLL 1 |
|
|
70 | # else |
|
|
71 | # define EV_USE_POLL 0 |
|
|
72 | # endif |
54 | # endif |
73 | # endif |
55 | |
74 | |
56 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL) |
75 | # ifndef EV_USE_EPOLL |
|
|
76 | # if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H |
57 | # define EV_USE_EPOLL 1 |
77 | # define EV_USE_EPOLL 1 |
|
|
78 | # else |
|
|
79 | # define EV_USE_EPOLL 0 |
|
|
80 | # endif |
58 | # endif |
81 | # endif |
59 | |
82 | |
|
|
83 | # ifndef EV_USE_KQUEUE |
60 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE) |
84 | # if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H |
61 | # define EV_USE_KQUEUE 1 |
85 | # define EV_USE_KQUEUE 1 |
|
|
86 | # else |
|
|
87 | # define EV_USE_KQUEUE 0 |
|
|
88 | # endif |
62 | # endif |
89 | # endif |
63 | |
90 | |
64 | # if HAVE_PORT_H && HAVE_PORT_CREATE && !defined (EV_USE_PORT) |
91 | # ifndef EV_USE_PORT |
|
|
92 | # if HAVE_PORT_H && HAVE_PORT_CREATE |
65 | # define EV_USE_PORT 1 |
93 | # define EV_USE_PORT 1 |
|
|
94 | # else |
|
|
95 | # define EV_USE_PORT 0 |
|
|
96 | # endif |
66 | # endif |
97 | # endif |
67 | |
98 | |
68 | #endif |
99 | #endif |
69 | |
100 | |
70 | #include <math.h> |
101 | #include <math.h> |
… | |
… | |
80 | #include <time.h> |
111 | #include <time.h> |
81 | |
112 | |
82 | #include <signal.h> |
113 | #include <signal.h> |
83 | |
114 | |
84 | #ifndef _WIN32 |
115 | #ifndef _WIN32 |
85 | # include <unistd.h> |
|
|
86 | # include <sys/time.h> |
116 | # include <sys/time.h> |
87 | # include <sys/wait.h> |
117 | # include <sys/wait.h> |
|
|
118 | # include <unistd.h> |
88 | #else |
119 | #else |
89 | # define WIN32_LEAN_AND_MEAN |
120 | # define WIN32_LEAN_AND_MEAN |
90 | # include <windows.h> |
121 | # include <windows.h> |
91 | # ifndef EV_SELECT_IS_WINSOCKET |
122 | # ifndef EV_SELECT_IS_WINSOCKET |
92 | # define EV_SELECT_IS_WINSOCKET 1 |
123 | # define EV_SELECT_IS_WINSOCKET 1 |
… | |
… | |
94 | #endif |
125 | #endif |
95 | |
126 | |
96 | /**/ |
127 | /**/ |
97 | |
128 | |
98 | #ifndef EV_USE_MONOTONIC |
129 | #ifndef EV_USE_MONOTONIC |
99 | # define EV_USE_MONOTONIC 1 |
130 | # define EV_USE_MONOTONIC 0 |
100 | #endif |
131 | #endif |
101 | |
132 | |
102 | #ifndef EV_USE_REALTIME |
133 | #ifndef EV_USE_REALTIME |
103 | # define EV_USE_REALTIME 1 |
134 | # define EV_USE_REALTIME 0 |
104 | #endif |
135 | #endif |
105 | |
136 | |
106 | #ifndef EV_USE_SELECT |
137 | #ifndef EV_USE_SELECT |
107 | # define EV_USE_SELECT 1 |
138 | # define EV_USE_SELECT 1 |
108 | # define EV_SELECT_USE_FD_SET 1 |
|
|
109 | #endif |
139 | #endif |
110 | |
140 | |
111 | #ifndef EV_USE_POLL |
141 | #ifndef EV_USE_POLL |
112 | # ifdef _WIN32 |
142 | # ifdef _WIN32 |
113 | # define EV_USE_POLL 0 |
143 | # define EV_USE_POLL 0 |
… | |
… | |
126 | |
156 | |
127 | #ifndef EV_USE_PORT |
157 | #ifndef EV_USE_PORT |
128 | # define EV_USE_PORT 0 |
158 | # define EV_USE_PORT 0 |
129 | #endif |
159 | #endif |
130 | |
160 | |
|
|
161 | #ifndef EV_PID_HASHSIZE |
|
|
162 | # if EV_MINIMAL |
|
|
163 | # define EV_PID_HASHSIZE 1 |
|
|
164 | # else |
|
|
165 | # define EV_PID_HASHSIZE 16 |
|
|
166 | # endif |
|
|
167 | #endif |
|
|
168 | |
131 | /**/ |
169 | /**/ |
132 | |
|
|
133 | /* darwin simply cannot be helped */ |
|
|
134 | #ifdef __APPLE__ |
|
|
135 | # undef EV_USE_POLL |
|
|
136 | # undef EV_USE_KQUEUE |
|
|
137 | #endif |
|
|
138 | |
170 | |
139 | #ifndef CLOCK_MONOTONIC |
171 | #ifndef CLOCK_MONOTONIC |
140 | # undef EV_USE_MONOTONIC |
172 | # undef EV_USE_MONOTONIC |
141 | # define EV_USE_MONOTONIC 0 |
173 | # define EV_USE_MONOTONIC 0 |
142 | #endif |
174 | #endif |
… | |
… | |
152 | |
184 | |
153 | /**/ |
185 | /**/ |
154 | |
186 | |
155 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
187 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
156 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
188 | #define MAX_BLOCKTIME 59.743 /* never wait longer than this time (to detect time jumps) */ |
157 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
158 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
189 | /*#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
159 | |
190 | |
160 | #ifdef EV_H |
191 | #ifdef EV_H |
161 | # include EV_H |
192 | # include EV_H |
162 | #else |
193 | #else |
163 | # include "ev.h" |
194 | # include "ev.h" |
164 | #endif |
195 | #endif |
165 | |
196 | |
166 | #if __GNUC__ >= 3 |
197 | #if __GNUC__ >= 3 |
167 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
198 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
199 | # define inline_size static inline /* inline for codesize */ |
|
|
200 | # if EV_MINIMAL |
168 | # define inline inline |
201 | # define noinline __attribute__ ((noinline)) |
|
|
202 | # define inline_speed static noinline |
|
|
203 | # else |
|
|
204 | # define noinline |
|
|
205 | # define inline_speed static inline |
|
|
206 | # endif |
169 | #else |
207 | #else |
170 | # define expect(expr,value) (expr) |
208 | # define expect(expr,value) (expr) |
|
|
209 | # define inline_speed static |
171 | # define inline static |
210 | # define inline_size static |
|
|
211 | # define noinline |
172 | #endif |
212 | #endif |
173 | |
213 | |
174 | #define expect_false(expr) expect ((expr) != 0, 0) |
214 | #define expect_false(expr) expect ((expr) != 0, 0) |
175 | #define expect_true(expr) expect ((expr) != 0, 1) |
215 | #define expect_true(expr) expect ((expr) != 0, 1) |
176 | |
216 | |
… | |
… | |
178 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
218 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
179 | |
219 | |
180 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
220 | #define EMPTY0 /* required for microsofts broken pseudo-c compiler */ |
181 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
221 | #define EMPTY2(a,b) /* used to suppress some warnings */ |
182 | |
222 | |
183 | typedef struct ev_watcher *W; |
223 | typedef ev_watcher *W; |
184 | typedef struct ev_watcher_list *WL; |
224 | typedef ev_watcher_list *WL; |
185 | typedef struct ev_watcher_time *WT; |
225 | typedef ev_watcher_time *WT; |
186 | |
226 | |
187 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
227 | static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ |
188 | |
228 | |
189 | #ifdef _WIN32 |
229 | #ifdef _WIN32 |
190 | # include "ev_win32.c" |
230 | # include "ev_win32.c" |
… | |
… | |
192 | |
232 | |
193 | /*****************************************************************************/ |
233 | /*****************************************************************************/ |
194 | |
234 | |
195 | static void (*syserr_cb)(const char *msg); |
235 | static void (*syserr_cb)(const char *msg); |
196 | |
236 | |
|
|
237 | void |
197 | void ev_set_syserr_cb (void (*cb)(const char *msg)) |
238 | ev_set_syserr_cb (void (*cb)(const char *msg)) |
198 | { |
239 | { |
199 | syserr_cb = cb; |
240 | syserr_cb = cb; |
200 | } |
241 | } |
201 | |
242 | |
202 | static void |
243 | static void noinline |
203 | syserr (const char *msg) |
244 | syserr (const char *msg) |
204 | { |
245 | { |
205 | if (!msg) |
246 | if (!msg) |
206 | msg = "(libev) system error"; |
247 | msg = "(libev) system error"; |
207 | |
248 | |
… | |
… | |
212 | perror (msg); |
253 | perror (msg); |
213 | abort (); |
254 | abort (); |
214 | } |
255 | } |
215 | } |
256 | } |
216 | |
257 | |
217 | static void *(*alloc)(void *ptr, long size); |
258 | static void *(*alloc)(void *ptr, size_t size) = realloc; |
218 | |
259 | |
|
|
260 | void |
219 | void ev_set_allocator (void *(*cb)(void *ptr, long size)) |
261 | ev_set_allocator (void *(*cb)(void *ptr, size_t size)) |
220 | { |
262 | { |
221 | alloc = cb; |
263 | alloc = cb; |
222 | } |
264 | } |
223 | |
265 | |
224 | static void * |
266 | inline_speed void * |
225 | ev_realloc (void *ptr, long size) |
267 | ev_realloc (void *ptr, size_t size) |
226 | { |
268 | { |
227 | ptr = alloc ? alloc (ptr, size) : realloc (ptr, size); |
269 | ptr = alloc (ptr, size); |
228 | |
270 | |
229 | if (!ptr && size) |
271 | if (!ptr && size) |
230 | { |
272 | { |
231 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); |
273 | fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", (long)size); |
232 | abort (); |
274 | abort (); |
233 | } |
275 | } |
234 | |
276 | |
235 | return ptr; |
277 | return ptr; |
236 | } |
278 | } |
… | |
… | |
296 | gettimeofday (&tv, 0); |
338 | gettimeofday (&tv, 0); |
297 | return tv.tv_sec + tv.tv_usec * 1e-6; |
339 | return tv.tv_sec + tv.tv_usec * 1e-6; |
298 | #endif |
340 | #endif |
299 | } |
341 | } |
300 | |
342 | |
301 | inline ev_tstamp |
343 | ev_tstamp inline_size |
302 | get_clock (void) |
344 | get_clock (void) |
303 | { |
345 | { |
304 | #if EV_USE_MONOTONIC |
346 | #if EV_USE_MONOTONIC |
305 | if (expect_true (have_monotonic)) |
347 | if (expect_true (have_monotonic)) |
306 | { |
348 | { |
… | |
… | |
349 | #define array_free(stem, idx) \ |
391 | #define array_free(stem, idx) \ |
350 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
392 | ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; |
351 | |
393 | |
352 | /*****************************************************************************/ |
394 | /*****************************************************************************/ |
353 | |
395 | |
354 | static void |
396 | void noinline |
355 | anfds_init (ANFD *base, int count) |
|
|
356 | { |
|
|
357 | while (count--) |
|
|
358 | { |
|
|
359 | base->head = 0; |
|
|
360 | base->events = EV_NONE; |
|
|
361 | base->reify = 0; |
|
|
362 | |
|
|
363 | ++base; |
|
|
364 | } |
|
|
365 | } |
|
|
366 | |
|
|
367 | void |
|
|
368 | ev_feed_event (EV_P_ void *w, int revents) |
397 | ev_feed_event (EV_P_ void *w, int revents) |
369 | { |
398 | { |
370 | W w_ = (W)w; |
399 | W w_ = (W)w; |
371 | |
400 | |
372 | if (w_->pending) |
401 | if (expect_false (w_->pending)) |
373 | { |
402 | { |
374 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
403 | pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; |
375 | return; |
404 | return; |
376 | } |
405 | } |
377 | |
406 | |
… | |
… | |
379 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
408 | array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); |
380 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
409 | pendings [ABSPRI (w_)][w_->pending - 1].w = w_; |
381 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
410 | pendings [ABSPRI (w_)][w_->pending - 1].events = revents; |
382 | } |
411 | } |
383 | |
412 | |
384 | static void |
413 | void inline_size |
385 | queue_events (EV_P_ W *events, int eventcnt, int type) |
414 | queue_events (EV_P_ W *events, int eventcnt, int type) |
386 | { |
415 | { |
387 | int i; |
416 | int i; |
388 | |
417 | |
389 | for (i = 0; i < eventcnt; ++i) |
418 | for (i = 0; i < eventcnt; ++i) |
390 | ev_feed_event (EV_A_ events [i], type); |
419 | ev_feed_event (EV_A_ events [i], type); |
391 | } |
420 | } |
392 | |
421 | |
393 | inline void |
422 | /*****************************************************************************/ |
|
|
423 | |
|
|
424 | void inline_size |
|
|
425 | anfds_init (ANFD *base, int count) |
|
|
426 | { |
|
|
427 | while (count--) |
|
|
428 | { |
|
|
429 | base->head = 0; |
|
|
430 | base->events = EV_NONE; |
|
|
431 | base->reify = 0; |
|
|
432 | |
|
|
433 | ++base; |
|
|
434 | } |
|
|
435 | } |
|
|
436 | |
|
|
437 | void inline_speed |
394 | fd_event (EV_P_ int fd, int revents) |
438 | fd_event (EV_P_ int fd, int revents) |
395 | { |
439 | { |
396 | ANFD *anfd = anfds + fd; |
440 | ANFD *anfd = anfds + fd; |
397 | struct ev_io *w; |
441 | ev_io *w; |
398 | |
442 | |
399 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
443 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
400 | { |
444 | { |
401 | int ev = w->events & revents; |
445 | int ev = w->events & revents; |
402 | |
446 | |
403 | if (ev) |
447 | if (ev) |
404 | ev_feed_event (EV_A_ (W)w, ev); |
448 | ev_feed_event (EV_A_ (W)w, ev); |
… | |
… | |
409 | ev_feed_fd_event (EV_P_ int fd, int revents) |
453 | ev_feed_fd_event (EV_P_ int fd, int revents) |
410 | { |
454 | { |
411 | fd_event (EV_A_ fd, revents); |
455 | fd_event (EV_A_ fd, revents); |
412 | } |
456 | } |
413 | |
457 | |
414 | /*****************************************************************************/ |
458 | void inline_size |
415 | |
|
|
416 | static void |
|
|
417 | fd_reify (EV_P) |
459 | fd_reify (EV_P) |
418 | { |
460 | { |
419 | int i; |
461 | int i; |
420 | |
462 | |
421 | for (i = 0; i < fdchangecnt; ++i) |
463 | for (i = 0; i < fdchangecnt; ++i) |
422 | { |
464 | { |
423 | int fd = fdchanges [i]; |
465 | int fd = fdchanges [i]; |
424 | ANFD *anfd = anfds + fd; |
466 | ANFD *anfd = anfds + fd; |
425 | struct ev_io *w; |
467 | ev_io *w; |
426 | |
468 | |
427 | int events = 0; |
469 | int events = 0; |
428 | |
470 | |
429 | for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) |
471 | for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) |
430 | events |= w->events; |
472 | events |= w->events; |
431 | |
473 | |
432 | #if EV_SELECT_IS_WINSOCKET |
474 | #if EV_SELECT_IS_WINSOCKET |
433 | if (events) |
475 | if (events) |
434 | { |
476 | { |
… | |
… | |
438 | } |
480 | } |
439 | #endif |
481 | #endif |
440 | |
482 | |
441 | anfd->reify = 0; |
483 | anfd->reify = 0; |
442 | |
484 | |
443 | method_modify (EV_A_ fd, anfd->events, events); |
485 | backend_modify (EV_A_ fd, anfd->events, events); |
444 | anfd->events = events; |
486 | anfd->events = events; |
445 | } |
487 | } |
446 | |
488 | |
447 | fdchangecnt = 0; |
489 | fdchangecnt = 0; |
448 | } |
490 | } |
449 | |
491 | |
450 | static void |
492 | void inline_size |
451 | fd_change (EV_P_ int fd) |
493 | fd_change (EV_P_ int fd) |
452 | { |
494 | { |
453 | if (anfds [fd].reify) |
495 | if (expect_false (anfds [fd].reify)) |
454 | return; |
496 | return; |
455 | |
497 | |
456 | anfds [fd].reify = 1; |
498 | anfds [fd].reify = 1; |
457 | |
499 | |
458 | ++fdchangecnt; |
500 | ++fdchangecnt; |
459 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
501 | array_needsize (int, fdchanges, fdchangemax, fdchangecnt, EMPTY2); |
460 | fdchanges [fdchangecnt - 1] = fd; |
502 | fdchanges [fdchangecnt - 1] = fd; |
461 | } |
503 | } |
462 | |
504 | |
463 | static void |
505 | void inline_speed |
464 | fd_kill (EV_P_ int fd) |
506 | fd_kill (EV_P_ int fd) |
465 | { |
507 | { |
466 | struct ev_io *w; |
508 | ev_io *w; |
467 | |
509 | |
468 | while ((w = (struct ev_io *)anfds [fd].head)) |
510 | while ((w = (ev_io *)anfds [fd].head)) |
469 | { |
511 | { |
470 | ev_io_stop (EV_A_ w); |
512 | ev_io_stop (EV_A_ w); |
471 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
513 | ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); |
472 | } |
514 | } |
473 | } |
515 | } |
474 | |
516 | |
475 | static int |
517 | int inline_size |
476 | fd_valid (int fd) |
518 | fd_valid (int fd) |
477 | { |
519 | { |
478 | #ifdef _WIN32 |
520 | #ifdef _WIN32 |
479 | return _get_osfhandle (fd) != -1; |
521 | return _get_osfhandle (fd) != -1; |
480 | #else |
522 | #else |
481 | return fcntl (fd, F_GETFD) != -1; |
523 | return fcntl (fd, F_GETFD) != -1; |
482 | #endif |
524 | #endif |
483 | } |
525 | } |
484 | |
526 | |
485 | /* called on EBADF to verify fds */ |
527 | /* called on EBADF to verify fds */ |
486 | static void |
528 | static void noinline |
487 | fd_ebadf (EV_P) |
529 | fd_ebadf (EV_P) |
488 | { |
530 | { |
489 | int fd; |
531 | int fd; |
490 | |
532 | |
491 | for (fd = 0; fd < anfdmax; ++fd) |
533 | for (fd = 0; fd < anfdmax; ++fd) |
… | |
… | |
493 | if (!fd_valid (fd) == -1 && errno == EBADF) |
535 | if (!fd_valid (fd) == -1 && errno == EBADF) |
494 | fd_kill (EV_A_ fd); |
536 | fd_kill (EV_A_ fd); |
495 | } |
537 | } |
496 | |
538 | |
497 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
539 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
498 | static void |
540 | static void noinline |
499 | fd_enomem (EV_P) |
541 | fd_enomem (EV_P) |
500 | { |
542 | { |
501 | int fd; |
543 | int fd; |
502 | |
544 | |
503 | for (fd = anfdmax; fd--; ) |
545 | for (fd = anfdmax; fd--; ) |
… | |
… | |
506 | fd_kill (EV_A_ fd); |
548 | fd_kill (EV_A_ fd); |
507 | return; |
549 | return; |
508 | } |
550 | } |
509 | } |
551 | } |
510 | |
552 | |
511 | /* usually called after fork if method needs to re-arm all fds from scratch */ |
553 | /* usually called after fork if backend needs to re-arm all fds from scratch */ |
512 | static void |
554 | static void noinline |
513 | fd_rearm_all (EV_P) |
555 | fd_rearm_all (EV_P) |
514 | { |
556 | { |
515 | int fd; |
557 | int fd; |
516 | |
558 | |
517 | /* this should be highly optimised to not do anything but set a flag */ |
559 | /* this should be highly optimised to not do anything but set a flag */ |
… | |
… | |
523 | } |
565 | } |
524 | } |
566 | } |
525 | |
567 | |
526 | /*****************************************************************************/ |
568 | /*****************************************************************************/ |
527 | |
569 | |
528 | static void |
570 | void inline_speed |
529 | upheap (WT *heap, int k) |
571 | upheap (WT *heap, int k) |
530 | { |
572 | { |
531 | WT w = heap [k]; |
573 | WT w = heap [k]; |
532 | |
574 | |
533 | while (k && heap [k >> 1]->at > w->at) |
575 | while (k && heap [k >> 1]->at > w->at) |
… | |
… | |
540 | heap [k] = w; |
582 | heap [k] = w; |
541 | ((W)heap [k])->active = k + 1; |
583 | ((W)heap [k])->active = k + 1; |
542 | |
584 | |
543 | } |
585 | } |
544 | |
586 | |
545 | static void |
587 | void inline_speed |
546 | downheap (WT *heap, int N, int k) |
588 | downheap (WT *heap, int N, int k) |
547 | { |
589 | { |
548 | WT w = heap [k]; |
590 | WT w = heap [k]; |
549 | |
591 | |
550 | while (k < (N >> 1)) |
592 | while (k < (N >> 1)) |
… | |
… | |
564 | |
606 | |
565 | heap [k] = w; |
607 | heap [k] = w; |
566 | ((W)heap [k])->active = k + 1; |
608 | ((W)heap [k])->active = k + 1; |
567 | } |
609 | } |
568 | |
610 | |
569 | inline void |
611 | void inline_size |
570 | adjustheap (WT *heap, int N, int k) |
612 | adjustheap (WT *heap, int N, int k) |
571 | { |
613 | { |
572 | upheap (heap, k); |
614 | upheap (heap, k); |
573 | downheap (heap, N, k); |
615 | downheap (heap, N, k); |
574 | } |
616 | } |
… | |
… | |
584 | static ANSIG *signals; |
626 | static ANSIG *signals; |
585 | static int signalmax; |
627 | static int signalmax; |
586 | |
628 | |
587 | static int sigpipe [2]; |
629 | static int sigpipe [2]; |
588 | static sig_atomic_t volatile gotsig; |
630 | static sig_atomic_t volatile gotsig; |
589 | static struct ev_io sigev; |
631 | static ev_io sigev; |
590 | |
632 | |
591 | static void |
633 | void inline_size |
592 | signals_init (ANSIG *base, int count) |
634 | signals_init (ANSIG *base, int count) |
593 | { |
635 | { |
594 | while (count--) |
636 | while (count--) |
595 | { |
637 | { |
596 | base->head = 0; |
638 | base->head = 0; |
… | |
… | |
616 | write (sigpipe [1], &signum, 1); |
658 | write (sigpipe [1], &signum, 1); |
617 | errno = old_errno; |
659 | errno = old_errno; |
618 | } |
660 | } |
619 | } |
661 | } |
620 | |
662 | |
621 | void |
663 | void noinline |
622 | ev_feed_signal_event (EV_P_ int signum) |
664 | ev_feed_signal_event (EV_P_ int signum) |
623 | { |
665 | { |
624 | WL w; |
666 | WL w; |
625 | |
667 | |
626 | #if EV_MULTIPLICITY |
668 | #if EV_MULTIPLICITY |
… | |
… | |
637 | for (w = signals [signum].head; w; w = w->next) |
679 | for (w = signals [signum].head; w; w = w->next) |
638 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
680 | ev_feed_event (EV_A_ (W)w, EV_SIGNAL); |
639 | } |
681 | } |
640 | |
682 | |
641 | static void |
683 | static void |
642 | sigcb (EV_P_ struct ev_io *iow, int revents) |
684 | sigcb (EV_P_ ev_io *iow, int revents) |
643 | { |
685 | { |
644 | int signum; |
686 | int signum; |
645 | |
687 | |
646 | read (sigpipe [0], &revents, 1); |
688 | read (sigpipe [0], &revents, 1); |
647 | gotsig = 0; |
689 | gotsig = 0; |
… | |
… | |
649 | for (signum = signalmax; signum--; ) |
691 | for (signum = signalmax; signum--; ) |
650 | if (signals [signum].gotsig) |
692 | if (signals [signum].gotsig) |
651 | ev_feed_signal_event (EV_A_ signum + 1); |
693 | ev_feed_signal_event (EV_A_ signum + 1); |
652 | } |
694 | } |
653 | |
695 | |
654 | inline void |
696 | void inline_size |
655 | fd_intern (int fd) |
697 | fd_intern (int fd) |
656 | { |
698 | { |
657 | #ifdef _WIN32 |
699 | #ifdef _WIN32 |
658 | int arg = 1; |
700 | int arg = 1; |
659 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
701 | ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); |
… | |
… | |
661 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
703 | fcntl (fd, F_SETFD, FD_CLOEXEC); |
662 | fcntl (fd, F_SETFL, O_NONBLOCK); |
704 | fcntl (fd, F_SETFL, O_NONBLOCK); |
663 | #endif |
705 | #endif |
664 | } |
706 | } |
665 | |
707 | |
666 | static void |
708 | static void noinline |
667 | siginit (EV_P) |
709 | siginit (EV_P) |
668 | { |
710 | { |
669 | fd_intern (sigpipe [0]); |
711 | fd_intern (sigpipe [0]); |
670 | fd_intern (sigpipe [1]); |
712 | fd_intern (sigpipe [1]); |
671 | |
713 | |
… | |
… | |
674 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
716 | ev_unref (EV_A); /* child watcher should not keep loop alive */ |
675 | } |
717 | } |
676 | |
718 | |
677 | /*****************************************************************************/ |
719 | /*****************************************************************************/ |
678 | |
720 | |
679 | static struct ev_child *childs [PID_HASHSIZE]; |
721 | static ev_child *childs [EV_PID_HASHSIZE]; |
680 | |
722 | |
681 | #ifndef _WIN32 |
723 | #ifndef _WIN32 |
682 | |
724 | |
683 | static struct ev_signal childev; |
725 | static ev_signal childev; |
684 | |
726 | |
685 | #ifndef WCONTINUED |
727 | void inline_speed |
686 | # define WCONTINUED 0 |
|
|
687 | #endif |
|
|
688 | |
|
|
689 | static void |
|
|
690 | child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) |
728 | child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status) |
691 | { |
729 | { |
692 | struct ev_child *w; |
730 | ev_child *w; |
693 | |
731 | |
694 | for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) |
732 | for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) |
695 | if (w->pid == pid || !w->pid) |
733 | if (w->pid == pid || !w->pid) |
696 | { |
734 | { |
697 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
735 | ev_priority (w) = ev_priority (sw); /* need to do it *now* */ |
698 | w->rpid = pid; |
736 | w->rpid = pid; |
699 | w->rstatus = status; |
737 | w->rstatus = status; |
700 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
738 | ev_feed_event (EV_A_ (W)w, EV_CHILD); |
701 | } |
739 | } |
702 | } |
740 | } |
703 | |
741 | |
|
|
742 | #ifndef WCONTINUED |
|
|
743 | # define WCONTINUED 0 |
|
|
744 | #endif |
|
|
745 | |
704 | static void |
746 | static void |
705 | childcb (EV_P_ struct ev_signal *sw, int revents) |
747 | childcb (EV_P_ ev_signal *sw, int revents) |
706 | { |
748 | { |
707 | int pid, status; |
749 | int pid, status; |
708 | |
750 | |
|
|
751 | /* some systems define WCONTINUED but then fail to support it (linux 2.4) */ |
709 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
752 | if (0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
710 | { |
753 | if (!WCONTINUED |
|
|
754 | || errno != EINVAL |
|
|
755 | || 0 >= (pid = waitpid (-1, &status, WNOHANG | WUNTRACED))) |
|
|
756 | return; |
|
|
757 | |
711 | /* make sure we are called again until all childs have been reaped */ |
758 | /* make sure we are called again until all childs have been reaped */ |
|
|
759 | /* we need to do it this way so that the callback gets called before we continue */ |
712 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
760 | ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); |
713 | |
761 | |
714 | child_reap (EV_A_ sw, pid, pid, status); |
762 | child_reap (EV_A_ sw, pid, pid, status); |
|
|
763 | if (EV_PID_HASHSIZE > 1) |
715 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
764 | child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */ |
716 | } |
|
|
717 | } |
765 | } |
718 | |
766 | |
719 | #endif |
767 | #endif |
720 | |
768 | |
721 | /*****************************************************************************/ |
769 | /*****************************************************************************/ |
… | |
… | |
747 | { |
795 | { |
748 | return EV_VERSION_MINOR; |
796 | return EV_VERSION_MINOR; |
749 | } |
797 | } |
750 | |
798 | |
751 | /* return true if we are running with elevated privileges and should ignore env variables */ |
799 | /* return true if we are running with elevated privileges and should ignore env variables */ |
752 | static int |
800 | int inline_size |
753 | enable_secure (void) |
801 | enable_secure (void) |
754 | { |
802 | { |
755 | #ifdef _WIN32 |
803 | #ifdef _WIN32 |
756 | return 0; |
804 | return 0; |
757 | #else |
805 | #else |
… | |
… | |
759 | || getgid () != getegid (); |
807 | || getgid () != getegid (); |
760 | #endif |
808 | #endif |
761 | } |
809 | } |
762 | |
810 | |
763 | unsigned int |
811 | unsigned int |
764 | ev_method (EV_P) |
812 | ev_supported_backends (void) |
765 | { |
813 | { |
766 | return method; |
814 | unsigned int flags = 0; |
|
|
815 | |
|
|
816 | if (EV_USE_PORT ) flags |= EVBACKEND_PORT; |
|
|
817 | if (EV_USE_KQUEUE) flags |= EVBACKEND_KQUEUE; |
|
|
818 | if (EV_USE_EPOLL ) flags |= EVBACKEND_EPOLL; |
|
|
819 | if (EV_USE_POLL ) flags |= EVBACKEND_POLL; |
|
|
820 | if (EV_USE_SELECT) flags |= EVBACKEND_SELECT; |
|
|
821 | |
|
|
822 | return flags; |
|
|
823 | } |
|
|
824 | |
|
|
825 | unsigned int |
|
|
826 | ev_recommended_backends (void) |
|
|
827 | { |
|
|
828 | unsigned int flags = ev_supported_backends (); |
|
|
829 | |
|
|
830 | #ifndef __NetBSD__ |
|
|
831 | /* kqueue is borked on everything but netbsd apparently */ |
|
|
832 | /* it usually doesn't work correctly on anything but sockets and pipes */ |
|
|
833 | flags &= ~EVBACKEND_KQUEUE; |
|
|
834 | #endif |
|
|
835 | #ifdef __APPLE__ |
|
|
836 | // flags &= ~EVBACKEND_KQUEUE; for documentation |
|
|
837 | flags &= ~EVBACKEND_POLL; |
|
|
838 | #endif |
|
|
839 | |
|
|
840 | return flags; |
|
|
841 | } |
|
|
842 | |
|
|
843 | unsigned int |
|
|
844 | ev_embeddable_backends (void) |
|
|
845 | { |
|
|
846 | return EVBACKEND_EPOLL |
|
|
847 | | EVBACKEND_KQUEUE |
|
|
848 | | EVBACKEND_PORT; |
|
|
849 | } |
|
|
850 | |
|
|
851 | unsigned int |
|
|
852 | ev_backend (EV_P) |
|
|
853 | { |
|
|
854 | return backend; |
767 | } |
855 | } |
768 | |
856 | |
769 | static void |
857 | static void |
770 | loop_init (EV_P_ unsigned int flags) |
858 | loop_init (EV_P_ unsigned int flags) |
771 | { |
859 | { |
772 | if (!method) |
860 | if (!backend) |
773 | { |
861 | { |
774 | #if EV_USE_MONOTONIC |
862 | #if EV_USE_MONOTONIC |
775 | { |
863 | { |
776 | struct timespec ts; |
864 | struct timespec ts; |
777 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
865 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
… | |
… | |
782 | ev_rt_now = ev_time (); |
870 | ev_rt_now = ev_time (); |
783 | mn_now = get_clock (); |
871 | mn_now = get_clock (); |
784 | now_floor = mn_now; |
872 | now_floor = mn_now; |
785 | rtmn_diff = ev_rt_now - mn_now; |
873 | rtmn_diff = ev_rt_now - mn_now; |
786 | |
874 | |
787 | if (!(flags & EVFLAG_NOENV) && !enable_secure () && getenv ("LIBEV_FLAGS")) |
875 | if (!(flags & EVFLAG_NOENV) |
|
|
876 | && !enable_secure () |
|
|
877 | && getenv ("LIBEV_FLAGS")) |
788 | flags = atoi (getenv ("LIBEV_FLAGS")); |
878 | flags = atoi (getenv ("LIBEV_FLAGS")); |
789 | |
879 | |
790 | if (!(flags & 0x0000ffff)) |
880 | if (!(flags & 0x0000ffffUL)) |
791 | flags |= 0x0000ffff; |
881 | flags |= ev_recommended_backends (); |
792 | |
882 | |
793 | method = 0; |
883 | backend = 0; |
794 | #if EV_USE_PORT |
884 | #if EV_USE_PORT |
795 | if (!method && (flags & EVMETHOD_PORT )) method = port_init (EV_A_ flags); |
885 | if (!backend && (flags & EVBACKEND_PORT )) backend = port_init (EV_A_ flags); |
796 | #endif |
886 | #endif |
797 | #if EV_USE_KQUEUE |
887 | #if EV_USE_KQUEUE |
798 | if (!method && (flags & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ flags); |
888 | if (!backend && (flags & EVBACKEND_KQUEUE)) backend = kqueue_init (EV_A_ flags); |
799 | #endif |
889 | #endif |
800 | #if EV_USE_EPOLL |
890 | #if EV_USE_EPOLL |
801 | if (!method && (flags & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ flags); |
891 | if (!backend && (flags & EVBACKEND_EPOLL )) backend = epoll_init (EV_A_ flags); |
802 | #endif |
892 | #endif |
803 | #if EV_USE_POLL |
893 | #if EV_USE_POLL |
804 | if (!method && (flags & EVMETHOD_POLL )) method = poll_init (EV_A_ flags); |
894 | if (!backend && (flags & EVBACKEND_POLL )) backend = poll_init (EV_A_ flags); |
805 | #endif |
895 | #endif |
806 | #if EV_USE_SELECT |
896 | #if EV_USE_SELECT |
807 | if (!method && (flags & EVMETHOD_SELECT)) method = select_init (EV_A_ flags); |
897 | if (!backend && (flags & EVBACKEND_SELECT)) backend = select_init (EV_A_ flags); |
808 | #endif |
898 | #endif |
809 | |
899 | |
810 | ev_init (&sigev, sigcb); |
900 | ev_init (&sigev, sigcb); |
811 | ev_set_priority (&sigev, EV_MAXPRI); |
901 | ev_set_priority (&sigev, EV_MAXPRI); |
812 | } |
902 | } |
813 | } |
903 | } |
814 | |
904 | |
815 | void |
905 | static void |
816 | loop_destroy (EV_P) |
906 | loop_destroy (EV_P) |
817 | { |
907 | { |
818 | int i; |
908 | int i; |
819 | |
909 | |
820 | #if EV_USE_PORT |
910 | #if EV_USE_PORT |
821 | if (method == EVMETHOD_PORT ) port_destroy (EV_A); |
911 | if (backend == EVBACKEND_PORT ) port_destroy (EV_A); |
822 | #endif |
912 | #endif |
823 | #if EV_USE_KQUEUE |
913 | #if EV_USE_KQUEUE |
824 | if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); |
914 | if (backend == EVBACKEND_KQUEUE) kqueue_destroy (EV_A); |
825 | #endif |
915 | #endif |
826 | #if EV_USE_EPOLL |
916 | #if EV_USE_EPOLL |
827 | if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); |
917 | if (backend == EVBACKEND_EPOLL ) epoll_destroy (EV_A); |
828 | #endif |
918 | #endif |
829 | #if EV_USE_POLL |
919 | #if EV_USE_POLL |
830 | if (method == EVMETHOD_POLL ) poll_destroy (EV_A); |
920 | if (backend == EVBACKEND_POLL ) poll_destroy (EV_A); |
831 | #endif |
921 | #endif |
832 | #if EV_USE_SELECT |
922 | #if EV_USE_SELECT |
833 | if (method == EVMETHOD_SELECT) select_destroy (EV_A); |
923 | if (backend == EVBACKEND_SELECT) select_destroy (EV_A); |
834 | #endif |
924 | #endif |
835 | |
925 | |
836 | for (i = NUMPRI; i--; ) |
926 | for (i = NUMPRI; i--; ) |
837 | array_free (pending, [i]); |
927 | array_free (pending, [i]); |
838 | |
928 | |
839 | /* have to use the microsoft-never-gets-it-right macro */ |
929 | /* have to use the microsoft-never-gets-it-right macro */ |
840 | array_free (fdchange, EMPTY0); |
930 | array_free (fdchange, EMPTY0); |
841 | array_free (timer, EMPTY0); |
931 | array_free (timer, EMPTY0); |
842 | #if EV_PERIODICS |
932 | #if EV_PERIODIC_ENABLE |
843 | array_free (periodic, EMPTY0); |
933 | array_free (periodic, EMPTY0); |
844 | #endif |
934 | #endif |
845 | array_free (idle, EMPTY0); |
935 | array_free (idle, EMPTY0); |
846 | array_free (prepare, EMPTY0); |
936 | array_free (prepare, EMPTY0); |
847 | array_free (check, EMPTY0); |
937 | array_free (check, EMPTY0); |
848 | |
938 | |
849 | method = 0; |
939 | backend = 0; |
850 | } |
940 | } |
851 | |
941 | |
852 | static void |
942 | static void |
853 | loop_fork (EV_P) |
943 | loop_fork (EV_P) |
854 | { |
944 | { |
855 | #if EV_USE_PORT |
945 | #if EV_USE_PORT |
856 | if (method == EVMETHOD_PORT ) port_fork (EV_A); |
946 | if (backend == EVBACKEND_PORT ) port_fork (EV_A); |
857 | #endif |
947 | #endif |
858 | #if EV_USE_KQUEUE |
948 | #if EV_USE_KQUEUE |
859 | if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A); |
949 | if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); |
860 | #endif |
950 | #endif |
861 | #if EV_USE_EPOLL |
951 | #if EV_USE_EPOLL |
862 | if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A); |
952 | if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); |
863 | #endif |
953 | #endif |
864 | |
954 | |
865 | if (ev_is_active (&sigev)) |
955 | if (ev_is_active (&sigev)) |
866 | { |
956 | { |
867 | /* default loop */ |
957 | /* default loop */ |
… | |
… | |
888 | |
978 | |
889 | memset (loop, 0, sizeof (struct ev_loop)); |
979 | memset (loop, 0, sizeof (struct ev_loop)); |
890 | |
980 | |
891 | loop_init (EV_A_ flags); |
981 | loop_init (EV_A_ flags); |
892 | |
982 | |
893 | if (ev_method (EV_A)) |
983 | if (ev_backend (EV_A)) |
894 | return loop; |
984 | return loop; |
895 | |
985 | |
896 | return 0; |
986 | return 0; |
897 | } |
987 | } |
898 | |
988 | |
… | |
… | |
911 | |
1001 | |
912 | #endif |
1002 | #endif |
913 | |
1003 | |
914 | #if EV_MULTIPLICITY |
1004 | #if EV_MULTIPLICITY |
915 | struct ev_loop * |
1005 | struct ev_loop * |
916 | ev_default_loop_ (unsigned int flags) |
1006 | ev_default_loop_init (unsigned int flags) |
917 | #else |
1007 | #else |
918 | int |
1008 | int |
919 | ev_default_loop (unsigned int flags) |
1009 | ev_default_loop (unsigned int flags) |
920 | #endif |
1010 | #endif |
921 | { |
1011 | { |
… | |
… | |
931 | ev_default_loop_ptr = 1; |
1021 | ev_default_loop_ptr = 1; |
932 | #endif |
1022 | #endif |
933 | |
1023 | |
934 | loop_init (EV_A_ flags); |
1024 | loop_init (EV_A_ flags); |
935 | |
1025 | |
936 | if (ev_method (EV_A)) |
1026 | if (ev_backend (EV_A)) |
937 | { |
1027 | { |
938 | siginit (EV_A); |
1028 | siginit (EV_A); |
939 | |
1029 | |
940 | #ifndef _WIN32 |
1030 | #ifndef _WIN32 |
941 | ev_signal_init (&childev, childcb, SIGCHLD); |
1031 | ev_signal_init (&childev, childcb, SIGCHLD); |
… | |
… | |
977 | { |
1067 | { |
978 | #if EV_MULTIPLICITY |
1068 | #if EV_MULTIPLICITY |
979 | struct ev_loop *loop = ev_default_loop_ptr; |
1069 | struct ev_loop *loop = ev_default_loop_ptr; |
980 | #endif |
1070 | #endif |
981 | |
1071 | |
982 | if (method) |
1072 | if (backend) |
983 | postfork = 1; |
1073 | postfork = 1; |
984 | } |
1074 | } |
985 | |
1075 | |
986 | /*****************************************************************************/ |
1076 | /*****************************************************************************/ |
987 | |
1077 | |
988 | static int |
1078 | int inline_size |
989 | any_pending (EV_P) |
1079 | any_pending (EV_P) |
990 | { |
1080 | { |
991 | int pri; |
1081 | int pri; |
992 | |
1082 | |
993 | for (pri = NUMPRI; pri--; ) |
1083 | for (pri = NUMPRI; pri--; ) |
… | |
… | |
995 | return 1; |
1085 | return 1; |
996 | |
1086 | |
997 | return 0; |
1087 | return 0; |
998 | } |
1088 | } |
999 | |
1089 | |
1000 | static void |
1090 | void inline_speed |
1001 | call_pending (EV_P) |
1091 | call_pending (EV_P) |
1002 | { |
1092 | { |
1003 | int pri; |
1093 | int pri; |
1004 | |
1094 | |
1005 | for (pri = NUMPRI; pri--; ) |
1095 | for (pri = NUMPRI; pri--; ) |
1006 | while (pendingcnt [pri]) |
1096 | while (pendingcnt [pri]) |
1007 | { |
1097 | { |
1008 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1098 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
1009 | |
1099 | |
1010 | if (p->w) |
1100 | if (expect_true (p->w)) |
1011 | { |
1101 | { |
|
|
1102 | assert (("non-pending watcher on pending list", p->w->pending)); |
|
|
1103 | |
1012 | p->w->pending = 0; |
1104 | p->w->pending = 0; |
1013 | EV_CB_INVOKE (p->w, p->events); |
1105 | EV_CB_INVOKE (p->w, p->events); |
1014 | } |
1106 | } |
1015 | } |
1107 | } |
1016 | } |
1108 | } |
1017 | |
1109 | |
1018 | static void |
1110 | void inline_size |
1019 | timers_reify (EV_P) |
1111 | timers_reify (EV_P) |
1020 | { |
1112 | { |
1021 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1113 | while (timercnt && ((WT)timers [0])->at <= mn_now) |
1022 | { |
1114 | { |
1023 | struct ev_timer *w = timers [0]; |
1115 | ev_timer *w = timers [0]; |
1024 | |
1116 | |
1025 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1117 | assert (("inactive timer on timer heap detected", ev_is_active (w))); |
1026 | |
1118 | |
1027 | /* first reschedule or stop timer */ |
1119 | /* first reschedule or stop timer */ |
1028 | if (w->repeat) |
1120 | if (w->repeat) |
… | |
… | |
1040 | |
1132 | |
1041 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1133 | ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); |
1042 | } |
1134 | } |
1043 | } |
1135 | } |
1044 | |
1136 | |
1045 | #if EV_PERIODICS |
1137 | #if EV_PERIODIC_ENABLE |
1046 | static void |
1138 | void inline_size |
1047 | periodics_reify (EV_P) |
1139 | periodics_reify (EV_P) |
1048 | { |
1140 | { |
1049 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1141 | while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) |
1050 | { |
1142 | { |
1051 | struct ev_periodic *w = periodics [0]; |
1143 | ev_periodic *w = periodics [0]; |
1052 | |
1144 | |
1053 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1145 | assert (("inactive timer on periodic heap detected", ev_is_active (w))); |
1054 | |
1146 | |
1055 | /* first reschedule or stop timer */ |
1147 | /* first reschedule or stop timer */ |
1056 | if (w->reschedule_cb) |
1148 | if (w->reschedule_cb) |
… | |
… | |
1070 | |
1162 | |
1071 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1163 | ev_feed_event (EV_A_ (W)w, EV_PERIODIC); |
1072 | } |
1164 | } |
1073 | } |
1165 | } |
1074 | |
1166 | |
1075 | static void |
1167 | static void noinline |
1076 | periodics_reschedule (EV_P) |
1168 | periodics_reschedule (EV_P) |
1077 | { |
1169 | { |
1078 | int i; |
1170 | int i; |
1079 | |
1171 | |
1080 | /* adjust periodics after time jump */ |
1172 | /* adjust periodics after time jump */ |
1081 | for (i = 0; i < periodiccnt; ++i) |
1173 | for (i = 0; i < periodiccnt; ++i) |
1082 | { |
1174 | { |
1083 | struct ev_periodic *w = periodics [i]; |
1175 | ev_periodic *w = periodics [i]; |
1084 | |
1176 | |
1085 | if (w->reschedule_cb) |
1177 | if (w->reschedule_cb) |
1086 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1178 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1087 | else if (w->interval) |
1179 | else if (w->interval) |
1088 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1180 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
… | |
… | |
1092 | for (i = periodiccnt >> 1; i--; ) |
1184 | for (i = periodiccnt >> 1; i--; ) |
1093 | downheap ((WT *)periodics, periodiccnt, i); |
1185 | downheap ((WT *)periodics, periodiccnt, i); |
1094 | } |
1186 | } |
1095 | #endif |
1187 | #endif |
1096 | |
1188 | |
1097 | inline int |
1189 | int inline_size |
1098 | time_update_monotonic (EV_P) |
1190 | time_update_monotonic (EV_P) |
1099 | { |
1191 | { |
1100 | mn_now = get_clock (); |
1192 | mn_now = get_clock (); |
1101 | |
1193 | |
1102 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
1194 | if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) |
… | |
… | |
1110 | ev_rt_now = ev_time (); |
1202 | ev_rt_now = ev_time (); |
1111 | return 1; |
1203 | return 1; |
1112 | } |
1204 | } |
1113 | } |
1205 | } |
1114 | |
1206 | |
1115 | static void |
1207 | void inline_size |
1116 | time_update (EV_P) |
1208 | time_update (EV_P) |
1117 | { |
1209 | { |
1118 | int i; |
1210 | int i; |
1119 | |
1211 | |
1120 | #if EV_USE_MONOTONIC |
1212 | #if EV_USE_MONOTONIC |
… | |
… | |
1122 | { |
1214 | { |
1123 | if (time_update_monotonic (EV_A)) |
1215 | if (time_update_monotonic (EV_A)) |
1124 | { |
1216 | { |
1125 | ev_tstamp odiff = rtmn_diff; |
1217 | ev_tstamp odiff = rtmn_diff; |
1126 | |
1218 | |
1127 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
1219 | /* loop a few times, before making important decisions. |
|
|
1220 | * on the choice of "4": one iteration isn't enough, |
|
|
1221 | * in case we get preempted during the calls to |
|
|
1222 | * ev_time and get_clock. a second call is almost guarenteed |
|
|
1223 | * to succeed in that case, though. and looping a few more times |
|
|
1224 | * doesn't hurt either as we only do this on time-jumps or |
|
|
1225 | * in the unlikely event of getting preempted here. |
|
|
1226 | */ |
|
|
1227 | for (i = 4; --i; ) |
1128 | { |
1228 | { |
1129 | rtmn_diff = ev_rt_now - mn_now; |
1229 | rtmn_diff = ev_rt_now - mn_now; |
1130 | |
1230 | |
1131 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1231 | if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) |
1132 | return; /* all is well */ |
1232 | return; /* all is well */ |
… | |
… | |
1134 | ev_rt_now = ev_time (); |
1234 | ev_rt_now = ev_time (); |
1135 | mn_now = get_clock (); |
1235 | mn_now = get_clock (); |
1136 | now_floor = mn_now; |
1236 | now_floor = mn_now; |
1137 | } |
1237 | } |
1138 | |
1238 | |
1139 | # if EV_PERIODICS |
1239 | # if EV_PERIODIC_ENABLE |
1140 | periodics_reschedule (EV_A); |
1240 | periodics_reschedule (EV_A); |
1141 | # endif |
1241 | # endif |
1142 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1242 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
1143 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1243 | /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ |
1144 | } |
1244 | } |
… | |
… | |
1148 | { |
1248 | { |
1149 | ev_rt_now = ev_time (); |
1249 | ev_rt_now = ev_time (); |
1150 | |
1250 | |
1151 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1251 | if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
1152 | { |
1252 | { |
1153 | #if EV_PERIODICS |
1253 | #if EV_PERIODIC_ENABLE |
1154 | periodics_reschedule (EV_A); |
1254 | periodics_reschedule (EV_A); |
1155 | #endif |
1255 | #endif |
1156 | |
1256 | |
1157 | /* adjust timers. this is easy, as the offset is the same for all */ |
1257 | /* adjust timers. this is easy, as the offset is the same for all */ |
1158 | for (i = 0; i < timercnt; ++i) |
1258 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
1178 | static int loop_done; |
1278 | static int loop_done; |
1179 | |
1279 | |
1180 | void |
1280 | void |
1181 | ev_loop (EV_P_ int flags) |
1281 | ev_loop (EV_P_ int flags) |
1182 | { |
1282 | { |
1183 | double block; |
|
|
1184 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
1283 | loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) |
|
|
1284 | ? EVUNLOOP_ONE |
|
|
1285 | : EVUNLOOP_CANCEL; |
1185 | |
1286 | |
1186 | while (activecnt) |
1287 | while (activecnt) |
1187 | { |
1288 | { |
|
|
1289 | /* we might have forked, so reify kernel state if necessary */ |
|
|
1290 | #if EV_FORK_ENABLE |
|
|
1291 | if (expect_false (postfork)) |
|
|
1292 | if (forkcnt) |
|
|
1293 | { |
|
|
1294 | queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); |
|
|
1295 | call_pending (EV_A); |
|
|
1296 | } |
|
|
1297 | #endif |
|
|
1298 | |
1188 | /* queue check watchers (and execute them) */ |
1299 | /* queue check watchers (and execute them) */ |
1189 | if (expect_false (preparecnt)) |
1300 | if (expect_false (preparecnt)) |
1190 | { |
1301 | { |
1191 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1302 | queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); |
1192 | call_pending (EV_A); |
1303 | call_pending (EV_A); |
… | |
… | |
1198 | |
1309 | |
1199 | /* update fd-related kernel structures */ |
1310 | /* update fd-related kernel structures */ |
1200 | fd_reify (EV_A); |
1311 | fd_reify (EV_A); |
1201 | |
1312 | |
1202 | /* calculate blocking time */ |
1313 | /* calculate blocking time */ |
|
|
1314 | { |
|
|
1315 | double block; |
1203 | |
1316 | |
1204 | /* we only need this for !monotonic clock or timers, but as we basically |
1317 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
1205 | always have timers, we just calculate it always */ |
1318 | block = 0.; /* do not block at all */ |
|
|
1319 | else |
|
|
1320 | { |
|
|
1321 | /* update time to cancel out callback processing overhead */ |
1206 | #if EV_USE_MONOTONIC |
1322 | #if EV_USE_MONOTONIC |
1207 | if (expect_true (have_monotonic)) |
1323 | if (expect_true (have_monotonic)) |
1208 | time_update_monotonic (EV_A); |
1324 | time_update_monotonic (EV_A); |
1209 | else |
1325 | else |
1210 | #endif |
1326 | #endif |
1211 | { |
1327 | { |
1212 | ev_rt_now = ev_time (); |
1328 | ev_rt_now = ev_time (); |
1213 | mn_now = ev_rt_now; |
1329 | mn_now = ev_rt_now; |
1214 | } |
1330 | } |
1215 | |
1331 | |
1216 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
|
|
1217 | block = 0.; |
|
|
1218 | else |
|
|
1219 | { |
|
|
1220 | block = MAX_BLOCKTIME; |
1332 | block = MAX_BLOCKTIME; |
1221 | |
1333 | |
1222 | if (timercnt) |
1334 | if (timercnt) |
1223 | { |
1335 | { |
1224 | ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; |
1336 | ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; |
1225 | if (block > to) block = to; |
1337 | if (block > to) block = to; |
1226 | } |
1338 | } |
1227 | |
1339 | |
1228 | #if EV_PERIODICS |
1340 | #if EV_PERIODIC_ENABLE |
1229 | if (periodiccnt) |
1341 | if (periodiccnt) |
1230 | { |
1342 | { |
1231 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; |
1343 | ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; |
1232 | if (block > to) block = to; |
1344 | if (block > to) block = to; |
1233 | } |
1345 | } |
1234 | #endif |
1346 | #endif |
1235 | |
1347 | |
1236 | if (block < 0.) block = 0.; |
1348 | if (expect_false (block < 0.)) block = 0.; |
1237 | } |
1349 | } |
1238 | |
1350 | |
1239 | method_poll (EV_A_ block); |
1351 | backend_poll (EV_A_ block); |
|
|
1352 | } |
1240 | |
1353 | |
1241 | /* update ev_rt_now, do magic */ |
1354 | /* update ev_rt_now, do magic */ |
1242 | time_update (EV_A); |
1355 | time_update (EV_A); |
1243 | |
1356 | |
1244 | /* queue pending timers and reschedule them */ |
1357 | /* queue pending timers and reschedule them */ |
1245 | timers_reify (EV_A); /* relative timers called last */ |
1358 | timers_reify (EV_A); /* relative timers called last */ |
1246 | #if EV_PERIODICS |
1359 | #if EV_PERIODIC_ENABLE |
1247 | periodics_reify (EV_A); /* absolute timers called first */ |
1360 | periodics_reify (EV_A); /* absolute timers called first */ |
1248 | #endif |
1361 | #endif |
1249 | |
1362 | |
1250 | /* queue idle watchers unless io or timers are pending */ |
1363 | /* queue idle watchers unless other events are pending */ |
1251 | if (idlecnt && !any_pending (EV_A)) |
1364 | if (idlecnt && !any_pending (EV_A)) |
1252 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1365 | queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); |
1253 | |
1366 | |
1254 | /* queue check watchers, to be executed first */ |
1367 | /* queue check watchers, to be executed first */ |
1255 | if (checkcnt) |
1368 | if (expect_false (checkcnt)) |
1256 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1369 | queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); |
1257 | |
1370 | |
1258 | call_pending (EV_A); |
1371 | call_pending (EV_A); |
1259 | |
1372 | |
1260 | if (loop_done) |
1373 | if (expect_false (loop_done)) |
1261 | break; |
1374 | break; |
1262 | } |
1375 | } |
1263 | |
1376 | |
1264 | if (loop_done != 2) |
1377 | if (loop_done == EVUNLOOP_ONE) |
1265 | loop_done = 0; |
1378 | loop_done = EVUNLOOP_CANCEL; |
1266 | } |
1379 | } |
1267 | |
1380 | |
1268 | void |
1381 | void |
1269 | ev_unloop (EV_P_ int how) |
1382 | ev_unloop (EV_P_ int how) |
1270 | { |
1383 | { |
1271 | loop_done = how; |
1384 | loop_done = how; |
1272 | } |
1385 | } |
1273 | |
1386 | |
1274 | /*****************************************************************************/ |
1387 | /*****************************************************************************/ |
1275 | |
1388 | |
1276 | inline void |
1389 | void inline_size |
1277 | wlist_add (WL *head, WL elem) |
1390 | wlist_add (WL *head, WL elem) |
1278 | { |
1391 | { |
1279 | elem->next = *head; |
1392 | elem->next = *head; |
1280 | *head = elem; |
1393 | *head = elem; |
1281 | } |
1394 | } |
1282 | |
1395 | |
1283 | inline void |
1396 | void inline_size |
1284 | wlist_del (WL *head, WL elem) |
1397 | wlist_del (WL *head, WL elem) |
1285 | { |
1398 | { |
1286 | while (*head) |
1399 | while (*head) |
1287 | { |
1400 | { |
1288 | if (*head == elem) |
1401 | if (*head == elem) |
… | |
… | |
1293 | |
1406 | |
1294 | head = &(*head)->next; |
1407 | head = &(*head)->next; |
1295 | } |
1408 | } |
1296 | } |
1409 | } |
1297 | |
1410 | |
1298 | inline void |
1411 | void inline_speed |
1299 | ev_clear_pending (EV_P_ W w) |
1412 | ev_clear_pending (EV_P_ W w) |
1300 | { |
1413 | { |
1301 | if (w->pending) |
1414 | if (w->pending) |
1302 | { |
1415 | { |
1303 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1416 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
1304 | w->pending = 0; |
1417 | w->pending = 0; |
1305 | } |
1418 | } |
1306 | } |
1419 | } |
1307 | |
1420 | |
1308 | inline void |
1421 | void inline_speed |
1309 | ev_start (EV_P_ W w, int active) |
1422 | ev_start (EV_P_ W w, int active) |
1310 | { |
1423 | { |
1311 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1424 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
1312 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1425 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
1313 | |
1426 | |
1314 | w->active = active; |
1427 | w->active = active; |
1315 | ev_ref (EV_A); |
1428 | ev_ref (EV_A); |
1316 | } |
1429 | } |
1317 | |
1430 | |
1318 | inline void |
1431 | void inline_size |
1319 | ev_stop (EV_P_ W w) |
1432 | ev_stop (EV_P_ W w) |
1320 | { |
1433 | { |
1321 | ev_unref (EV_A); |
1434 | ev_unref (EV_A); |
1322 | w->active = 0; |
1435 | w->active = 0; |
1323 | } |
1436 | } |
1324 | |
1437 | |
1325 | /*****************************************************************************/ |
1438 | /*****************************************************************************/ |
1326 | |
1439 | |
1327 | void |
1440 | void |
1328 | ev_io_start (EV_P_ struct ev_io *w) |
1441 | ev_io_start (EV_P_ ev_io *w) |
1329 | { |
1442 | { |
1330 | int fd = w->fd; |
1443 | int fd = w->fd; |
1331 | |
1444 | |
1332 | if (ev_is_active (w)) |
1445 | if (expect_false (ev_is_active (w))) |
1333 | return; |
1446 | return; |
1334 | |
1447 | |
1335 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1448 | assert (("ev_io_start called with negative fd", fd >= 0)); |
1336 | |
1449 | |
1337 | ev_start (EV_A_ (W)w, 1); |
1450 | ev_start (EV_A_ (W)w, 1); |
… | |
… | |
1340 | |
1453 | |
1341 | fd_change (EV_A_ fd); |
1454 | fd_change (EV_A_ fd); |
1342 | } |
1455 | } |
1343 | |
1456 | |
1344 | void |
1457 | void |
1345 | ev_io_stop (EV_P_ struct ev_io *w) |
1458 | ev_io_stop (EV_P_ ev_io *w) |
1346 | { |
1459 | { |
1347 | ev_clear_pending (EV_A_ (W)w); |
1460 | ev_clear_pending (EV_A_ (W)w); |
1348 | if (!ev_is_active (w)) |
1461 | if (expect_false (!ev_is_active (w))) |
1349 | return; |
1462 | return; |
1350 | |
1463 | |
1351 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1464 | assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); |
1352 | |
1465 | |
1353 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
1466 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
… | |
… | |
1355 | |
1468 | |
1356 | fd_change (EV_A_ w->fd); |
1469 | fd_change (EV_A_ w->fd); |
1357 | } |
1470 | } |
1358 | |
1471 | |
1359 | void |
1472 | void |
1360 | ev_timer_start (EV_P_ struct ev_timer *w) |
1473 | ev_timer_start (EV_P_ ev_timer *w) |
1361 | { |
1474 | { |
1362 | if (ev_is_active (w)) |
1475 | if (expect_false (ev_is_active (w))) |
1363 | return; |
1476 | return; |
1364 | |
1477 | |
1365 | ((WT)w)->at += mn_now; |
1478 | ((WT)w)->at += mn_now; |
1366 | |
1479 | |
1367 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1480 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
1368 | |
1481 | |
1369 | ev_start (EV_A_ (W)w, ++timercnt); |
1482 | ev_start (EV_A_ (W)w, ++timercnt); |
1370 | array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); |
1483 | array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2); |
1371 | timers [timercnt - 1] = w; |
1484 | timers [timercnt - 1] = w; |
1372 | upheap ((WT *)timers, timercnt - 1); |
1485 | upheap ((WT *)timers, timercnt - 1); |
1373 | |
1486 | |
1374 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1487 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1375 | } |
1488 | } |
1376 | |
1489 | |
1377 | void |
1490 | void |
1378 | ev_timer_stop (EV_P_ struct ev_timer *w) |
1491 | ev_timer_stop (EV_P_ ev_timer *w) |
1379 | { |
1492 | { |
1380 | ev_clear_pending (EV_A_ (W)w); |
1493 | ev_clear_pending (EV_A_ (W)w); |
1381 | if (!ev_is_active (w)) |
1494 | if (expect_false (!ev_is_active (w))) |
1382 | return; |
1495 | return; |
1383 | |
1496 | |
1384 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1497 | assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); |
1385 | |
1498 | |
1386 | if (((W)w)->active < timercnt--) |
1499 | if (expect_true (((W)w)->active < timercnt--)) |
1387 | { |
1500 | { |
1388 | timers [((W)w)->active - 1] = timers [timercnt]; |
1501 | timers [((W)w)->active - 1] = timers [timercnt]; |
1389 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1502 | adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); |
1390 | } |
1503 | } |
1391 | |
1504 | |
… | |
… | |
1393 | |
1506 | |
1394 | ev_stop (EV_A_ (W)w); |
1507 | ev_stop (EV_A_ (W)w); |
1395 | } |
1508 | } |
1396 | |
1509 | |
1397 | void |
1510 | void |
1398 | ev_timer_again (EV_P_ struct ev_timer *w) |
1511 | ev_timer_again (EV_P_ ev_timer *w) |
1399 | { |
1512 | { |
1400 | if (ev_is_active (w)) |
1513 | if (ev_is_active (w)) |
1401 | { |
1514 | { |
1402 | if (w->repeat) |
1515 | if (w->repeat) |
1403 | { |
1516 | { |
… | |
… | |
1412 | w->at = w->repeat; |
1525 | w->at = w->repeat; |
1413 | ev_timer_start (EV_A_ w); |
1526 | ev_timer_start (EV_A_ w); |
1414 | } |
1527 | } |
1415 | } |
1528 | } |
1416 | |
1529 | |
1417 | #if EV_PERIODICS |
1530 | #if EV_PERIODIC_ENABLE |
1418 | void |
1531 | void |
1419 | ev_periodic_start (EV_P_ struct ev_periodic *w) |
1532 | ev_periodic_start (EV_P_ ev_periodic *w) |
1420 | { |
1533 | { |
1421 | if (ev_is_active (w)) |
1534 | if (expect_false (ev_is_active (w))) |
1422 | return; |
1535 | return; |
1423 | |
1536 | |
1424 | if (w->reschedule_cb) |
1537 | if (w->reschedule_cb) |
1425 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1538 | ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); |
1426 | else if (w->interval) |
1539 | else if (w->interval) |
… | |
… | |
1429 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1542 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1430 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1543 | ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; |
1431 | } |
1544 | } |
1432 | |
1545 | |
1433 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1546 | ev_start (EV_A_ (W)w, ++periodiccnt); |
1434 | array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1547 | array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); |
1435 | periodics [periodiccnt - 1] = w; |
1548 | periodics [periodiccnt - 1] = w; |
1436 | upheap ((WT *)periodics, periodiccnt - 1); |
1549 | upheap ((WT *)periodics, periodiccnt - 1); |
1437 | |
1550 | |
1438 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1551 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1439 | } |
1552 | } |
1440 | |
1553 | |
1441 | void |
1554 | void |
1442 | ev_periodic_stop (EV_P_ struct ev_periodic *w) |
1555 | ev_periodic_stop (EV_P_ ev_periodic *w) |
1443 | { |
1556 | { |
1444 | ev_clear_pending (EV_A_ (W)w); |
1557 | ev_clear_pending (EV_A_ (W)w); |
1445 | if (!ev_is_active (w)) |
1558 | if (expect_false (!ev_is_active (w))) |
1446 | return; |
1559 | return; |
1447 | |
1560 | |
1448 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1561 | assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); |
1449 | |
1562 | |
1450 | if (((W)w)->active < periodiccnt--) |
1563 | if (expect_true (((W)w)->active < periodiccnt--)) |
1451 | { |
1564 | { |
1452 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1565 | periodics [((W)w)->active - 1] = periodics [periodiccnt]; |
1453 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1566 | adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); |
1454 | } |
1567 | } |
1455 | |
1568 | |
1456 | ev_stop (EV_A_ (W)w); |
1569 | ev_stop (EV_A_ (W)w); |
1457 | } |
1570 | } |
1458 | |
1571 | |
1459 | void |
1572 | void |
1460 | ev_periodic_again (EV_P_ struct ev_periodic *w) |
1573 | ev_periodic_again (EV_P_ ev_periodic *w) |
1461 | { |
1574 | { |
1462 | /* TODO: use adjustheap and recalculation */ |
1575 | /* TODO: use adjustheap and recalculation */ |
1463 | ev_periodic_stop (EV_A_ w); |
1576 | ev_periodic_stop (EV_A_ w); |
1464 | ev_periodic_start (EV_A_ w); |
1577 | ev_periodic_start (EV_A_ w); |
1465 | } |
1578 | } |
1466 | #endif |
1579 | #endif |
1467 | |
1580 | |
1468 | void |
|
|
1469 | ev_idle_start (EV_P_ struct ev_idle *w) |
|
|
1470 | { |
|
|
1471 | if (ev_is_active (w)) |
|
|
1472 | return; |
|
|
1473 | |
|
|
1474 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1475 | array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
|
|
1476 | idles [idlecnt - 1] = w; |
|
|
1477 | } |
|
|
1478 | |
|
|
1479 | void |
|
|
1480 | ev_idle_stop (EV_P_ struct ev_idle *w) |
|
|
1481 | { |
|
|
1482 | ev_clear_pending (EV_A_ (W)w); |
|
|
1483 | if (!ev_is_active (w)) |
|
|
1484 | return; |
|
|
1485 | |
|
|
1486 | idles [((W)w)->active - 1] = idles [--idlecnt]; |
|
|
1487 | ev_stop (EV_A_ (W)w); |
|
|
1488 | } |
|
|
1489 | |
|
|
1490 | void |
|
|
1491 | ev_prepare_start (EV_P_ struct ev_prepare *w) |
|
|
1492 | { |
|
|
1493 | if (ev_is_active (w)) |
|
|
1494 | return; |
|
|
1495 | |
|
|
1496 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1497 | array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
|
|
1498 | prepares [preparecnt - 1] = w; |
|
|
1499 | } |
|
|
1500 | |
|
|
1501 | void |
|
|
1502 | ev_prepare_stop (EV_P_ struct ev_prepare *w) |
|
|
1503 | { |
|
|
1504 | ev_clear_pending (EV_A_ (W)w); |
|
|
1505 | if (!ev_is_active (w)) |
|
|
1506 | return; |
|
|
1507 | |
|
|
1508 | prepares [((W)w)->active - 1] = prepares [--preparecnt]; |
|
|
1509 | ev_stop (EV_A_ (W)w); |
|
|
1510 | } |
|
|
1511 | |
|
|
1512 | void |
|
|
1513 | ev_check_start (EV_P_ struct ev_check *w) |
|
|
1514 | { |
|
|
1515 | if (ev_is_active (w)) |
|
|
1516 | return; |
|
|
1517 | |
|
|
1518 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1519 | array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); |
|
|
1520 | checks [checkcnt - 1] = w; |
|
|
1521 | } |
|
|
1522 | |
|
|
1523 | void |
|
|
1524 | ev_check_stop (EV_P_ struct ev_check *w) |
|
|
1525 | { |
|
|
1526 | ev_clear_pending (EV_A_ (W)w); |
|
|
1527 | if (!ev_is_active (w)) |
|
|
1528 | return; |
|
|
1529 | |
|
|
1530 | checks [((W)w)->active - 1] = checks [--checkcnt]; |
|
|
1531 | ev_stop (EV_A_ (W)w); |
|
|
1532 | } |
|
|
1533 | |
|
|
1534 | #ifndef SA_RESTART |
1581 | #ifndef SA_RESTART |
1535 | # define SA_RESTART 0 |
1582 | # define SA_RESTART 0 |
1536 | #endif |
1583 | #endif |
1537 | |
1584 | |
1538 | void |
1585 | void |
1539 | ev_signal_start (EV_P_ struct ev_signal *w) |
1586 | ev_signal_start (EV_P_ ev_signal *w) |
1540 | { |
1587 | { |
1541 | #if EV_MULTIPLICITY |
1588 | #if EV_MULTIPLICITY |
1542 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1589 | assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1543 | #endif |
1590 | #endif |
1544 | if (ev_is_active (w)) |
1591 | if (expect_false (ev_is_active (w))) |
1545 | return; |
1592 | return; |
1546 | |
1593 | |
1547 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1594 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
1548 | |
1595 | |
1549 | ev_start (EV_A_ (W)w, 1); |
1596 | ev_start (EV_A_ (W)w, 1); |
… | |
… | |
1563 | #endif |
1610 | #endif |
1564 | } |
1611 | } |
1565 | } |
1612 | } |
1566 | |
1613 | |
1567 | void |
1614 | void |
1568 | ev_signal_stop (EV_P_ struct ev_signal *w) |
1615 | ev_signal_stop (EV_P_ ev_signal *w) |
1569 | { |
1616 | { |
1570 | ev_clear_pending (EV_A_ (W)w); |
1617 | ev_clear_pending (EV_A_ (W)w); |
1571 | if (!ev_is_active (w)) |
1618 | if (expect_false (!ev_is_active (w))) |
1572 | return; |
1619 | return; |
1573 | |
1620 | |
1574 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1621 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1575 | ev_stop (EV_A_ (W)w); |
1622 | ev_stop (EV_A_ (W)w); |
1576 | |
1623 | |
1577 | if (!signals [w->signum - 1].head) |
1624 | if (!signals [w->signum - 1].head) |
1578 | signal (w->signum, SIG_DFL); |
1625 | signal (w->signum, SIG_DFL); |
1579 | } |
1626 | } |
1580 | |
1627 | |
1581 | void |
1628 | void |
1582 | ev_child_start (EV_P_ struct ev_child *w) |
1629 | ev_child_start (EV_P_ ev_child *w) |
1583 | { |
1630 | { |
1584 | #if EV_MULTIPLICITY |
1631 | #if EV_MULTIPLICITY |
1585 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1632 | assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); |
1586 | #endif |
1633 | #endif |
1587 | if (ev_is_active (w)) |
1634 | if (expect_false (ev_is_active (w))) |
1588 | return; |
1635 | return; |
1589 | |
1636 | |
1590 | ev_start (EV_A_ (W)w, 1); |
1637 | ev_start (EV_A_ (W)w, 1); |
1591 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1638 | wlist_add ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1592 | } |
1639 | } |
1593 | |
1640 | |
1594 | void |
1641 | void |
1595 | ev_child_stop (EV_P_ struct ev_child *w) |
1642 | ev_child_stop (EV_P_ ev_child *w) |
1596 | { |
1643 | { |
1597 | ev_clear_pending (EV_A_ (W)w); |
1644 | ev_clear_pending (EV_A_ (W)w); |
1598 | if (!ev_is_active (w)) |
1645 | if (expect_false (!ev_is_active (w))) |
1599 | return; |
1646 | return; |
1600 | |
1647 | |
1601 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1648 | wlist_del ((WL *)&childs [w->pid & (EV_PID_HASHSIZE - 1)], (WL)w); |
1602 | ev_stop (EV_A_ (W)w); |
1649 | ev_stop (EV_A_ (W)w); |
1603 | } |
1650 | } |
1604 | |
1651 | |
|
|
1652 | #if EV_STAT_ENABLE |
|
|
1653 | |
|
|
1654 | # ifdef _WIN32 |
|
|
1655 | # undef lstat |
|
|
1656 | # define lstat(a,b) _stati64 (a,b) |
|
|
1657 | # endif |
|
|
1658 | |
|
|
1659 | #define DEF_STAT_INTERVAL 5.0074891 |
|
|
1660 | #define MIN_STAT_INTERVAL 0.1074891 |
|
|
1661 | |
|
|
1662 | void |
|
|
1663 | ev_stat_stat (EV_P_ ev_stat *w) |
|
|
1664 | { |
|
|
1665 | if (lstat (w->path, &w->attr) < 0) |
|
|
1666 | w->attr.st_nlink = 0; |
|
|
1667 | else if (!w->attr.st_nlink) |
|
|
1668 | w->attr.st_nlink = 1; |
|
|
1669 | } |
|
|
1670 | |
|
|
1671 | static void |
|
|
1672 | stat_timer_cb (EV_P_ ev_timer *w_, int revents) |
|
|
1673 | { |
|
|
1674 | ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); |
|
|
1675 | |
|
|
1676 | /* we copy this here each the time so that */ |
|
|
1677 | /* prev has the old value when the callback gets invoked */ |
|
|
1678 | w->prev = w->attr; |
|
|
1679 | ev_stat_stat (EV_A_ w); |
|
|
1680 | |
|
|
1681 | if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) |
|
|
1682 | ev_feed_event (EV_A_ w, EV_STAT); |
|
|
1683 | } |
|
|
1684 | |
|
|
1685 | void |
|
|
1686 | ev_stat_start (EV_P_ ev_stat *w) |
|
|
1687 | { |
|
|
1688 | if (expect_false (ev_is_active (w))) |
|
|
1689 | return; |
|
|
1690 | |
|
|
1691 | /* since we use memcmp, we need to clear any padding data etc. */ |
|
|
1692 | memset (&w->prev, 0, sizeof (ev_statdata)); |
|
|
1693 | memset (&w->attr, 0, sizeof (ev_statdata)); |
|
|
1694 | |
|
|
1695 | ev_stat_stat (EV_A_ w); |
|
|
1696 | |
|
|
1697 | if (w->interval < MIN_STAT_INTERVAL) |
|
|
1698 | w->interval = w->interval ? MIN_STAT_INTERVAL : DEF_STAT_INTERVAL; |
|
|
1699 | |
|
|
1700 | ev_timer_init (&w->timer, stat_timer_cb, w->interval, w->interval); |
|
|
1701 | ev_set_priority (&w->timer, ev_priority (w)); |
|
|
1702 | ev_timer_start (EV_A_ &w->timer); |
|
|
1703 | |
|
|
1704 | ev_start (EV_A_ (W)w, 1); |
|
|
1705 | } |
|
|
1706 | |
|
|
1707 | void |
|
|
1708 | ev_stat_stop (EV_P_ ev_stat *w) |
|
|
1709 | { |
|
|
1710 | ev_clear_pending (EV_A_ (W)w); |
|
|
1711 | if (expect_false (!ev_is_active (w))) |
|
|
1712 | return; |
|
|
1713 | |
|
|
1714 | ev_timer_stop (EV_A_ &w->timer); |
|
|
1715 | |
|
|
1716 | ev_stop (EV_A_ (W)w); |
|
|
1717 | } |
|
|
1718 | #endif |
|
|
1719 | |
|
|
1720 | void |
|
|
1721 | ev_idle_start (EV_P_ ev_idle *w) |
|
|
1722 | { |
|
|
1723 | if (expect_false (ev_is_active (w))) |
|
|
1724 | return; |
|
|
1725 | |
|
|
1726 | ev_start (EV_A_ (W)w, ++idlecnt); |
|
|
1727 | array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2); |
|
|
1728 | idles [idlecnt - 1] = w; |
|
|
1729 | } |
|
|
1730 | |
|
|
1731 | void |
|
|
1732 | ev_idle_stop (EV_P_ ev_idle *w) |
|
|
1733 | { |
|
|
1734 | ev_clear_pending (EV_A_ (W)w); |
|
|
1735 | if (expect_false (!ev_is_active (w))) |
|
|
1736 | return; |
|
|
1737 | |
|
|
1738 | { |
|
|
1739 | int active = ((W)w)->active; |
|
|
1740 | idles [active - 1] = idles [--idlecnt]; |
|
|
1741 | ((W)idles [active - 1])->active = active; |
|
|
1742 | } |
|
|
1743 | |
|
|
1744 | ev_stop (EV_A_ (W)w); |
|
|
1745 | } |
|
|
1746 | |
|
|
1747 | void |
|
|
1748 | ev_prepare_start (EV_P_ ev_prepare *w) |
|
|
1749 | { |
|
|
1750 | if (expect_false (ev_is_active (w))) |
|
|
1751 | return; |
|
|
1752 | |
|
|
1753 | ev_start (EV_A_ (W)w, ++preparecnt); |
|
|
1754 | array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); |
|
|
1755 | prepares [preparecnt - 1] = w; |
|
|
1756 | } |
|
|
1757 | |
|
|
1758 | void |
|
|
1759 | ev_prepare_stop (EV_P_ ev_prepare *w) |
|
|
1760 | { |
|
|
1761 | ev_clear_pending (EV_A_ (W)w); |
|
|
1762 | if (expect_false (!ev_is_active (w))) |
|
|
1763 | return; |
|
|
1764 | |
|
|
1765 | { |
|
|
1766 | int active = ((W)w)->active; |
|
|
1767 | prepares [active - 1] = prepares [--preparecnt]; |
|
|
1768 | ((W)prepares [active - 1])->active = active; |
|
|
1769 | } |
|
|
1770 | |
|
|
1771 | ev_stop (EV_A_ (W)w); |
|
|
1772 | } |
|
|
1773 | |
|
|
1774 | void |
|
|
1775 | ev_check_start (EV_P_ ev_check *w) |
|
|
1776 | { |
|
|
1777 | if (expect_false (ev_is_active (w))) |
|
|
1778 | return; |
|
|
1779 | |
|
|
1780 | ev_start (EV_A_ (W)w, ++checkcnt); |
|
|
1781 | array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2); |
|
|
1782 | checks [checkcnt - 1] = w; |
|
|
1783 | } |
|
|
1784 | |
|
|
1785 | void |
|
|
1786 | ev_check_stop (EV_P_ ev_check *w) |
|
|
1787 | { |
|
|
1788 | ev_clear_pending (EV_A_ (W)w); |
|
|
1789 | if (expect_false (!ev_is_active (w))) |
|
|
1790 | return; |
|
|
1791 | |
|
|
1792 | { |
|
|
1793 | int active = ((W)w)->active; |
|
|
1794 | checks [active - 1] = checks [--checkcnt]; |
|
|
1795 | ((W)checks [active - 1])->active = active; |
|
|
1796 | } |
|
|
1797 | |
|
|
1798 | ev_stop (EV_A_ (W)w); |
|
|
1799 | } |
|
|
1800 | |
|
|
1801 | #if EV_EMBED_ENABLE |
|
|
1802 | void noinline |
|
|
1803 | ev_embed_sweep (EV_P_ ev_embed *w) |
|
|
1804 | { |
|
|
1805 | ev_loop (w->loop, EVLOOP_NONBLOCK); |
|
|
1806 | } |
|
|
1807 | |
|
|
1808 | static void |
|
|
1809 | embed_cb (EV_P_ ev_io *io, int revents) |
|
|
1810 | { |
|
|
1811 | ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); |
|
|
1812 | |
|
|
1813 | if (ev_cb (w)) |
|
|
1814 | ev_feed_event (EV_A_ (W)w, EV_EMBED); |
|
|
1815 | else |
|
|
1816 | ev_embed_sweep (loop, w); |
|
|
1817 | } |
|
|
1818 | |
|
|
1819 | void |
|
|
1820 | ev_embed_start (EV_P_ ev_embed *w) |
|
|
1821 | { |
|
|
1822 | if (expect_false (ev_is_active (w))) |
|
|
1823 | return; |
|
|
1824 | |
|
|
1825 | { |
|
|
1826 | struct ev_loop *loop = w->loop; |
|
|
1827 | assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); |
|
|
1828 | ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); |
|
|
1829 | } |
|
|
1830 | |
|
|
1831 | ev_set_priority (&w->io, ev_priority (w)); |
|
|
1832 | ev_io_start (EV_A_ &w->io); |
|
|
1833 | |
|
|
1834 | ev_start (EV_A_ (W)w, 1); |
|
|
1835 | } |
|
|
1836 | |
|
|
1837 | void |
|
|
1838 | ev_embed_stop (EV_P_ ev_embed *w) |
|
|
1839 | { |
|
|
1840 | ev_clear_pending (EV_A_ (W)w); |
|
|
1841 | if (expect_false (!ev_is_active (w))) |
|
|
1842 | return; |
|
|
1843 | |
|
|
1844 | ev_io_stop (EV_A_ &w->io); |
|
|
1845 | |
|
|
1846 | ev_stop (EV_A_ (W)w); |
|
|
1847 | } |
|
|
1848 | #endif |
|
|
1849 | |
|
|
1850 | #if EV_FORK_ENABLE |
|
|
1851 | void |
|
|
1852 | ev_fork_start (EV_P_ ev_fork *w) |
|
|
1853 | { |
|
|
1854 | if (expect_false (ev_is_active (w))) |
|
|
1855 | return; |
|
|
1856 | |
|
|
1857 | ev_start (EV_A_ (W)w, ++forkcnt); |
|
|
1858 | array_needsize (ev_fork *, forks, forkmax, forkcnt, EMPTY2); |
|
|
1859 | forks [forkcnt - 1] = w; |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | void |
|
|
1863 | ev_fork_stop (EV_P_ ev_fork *w) |
|
|
1864 | { |
|
|
1865 | ev_clear_pending (EV_A_ (W)w); |
|
|
1866 | if (expect_false (!ev_is_active (w))) |
|
|
1867 | return; |
|
|
1868 | |
|
|
1869 | { |
|
|
1870 | int active = ((W)w)->active; |
|
|
1871 | forks [active - 1] = forks [--forkcnt]; |
|
|
1872 | ((W)forks [active - 1])->active = active; |
|
|
1873 | } |
|
|
1874 | |
|
|
1875 | ev_stop (EV_A_ (W)w); |
|
|
1876 | } |
|
|
1877 | #endif |
|
|
1878 | |
1605 | /*****************************************************************************/ |
1879 | /*****************************************************************************/ |
1606 | |
1880 | |
1607 | struct ev_once |
1881 | struct ev_once |
1608 | { |
1882 | { |
1609 | struct ev_io io; |
1883 | ev_io io; |
1610 | struct ev_timer to; |
1884 | ev_timer to; |
1611 | void (*cb)(int revents, void *arg); |
1885 | void (*cb)(int revents, void *arg); |
1612 | void *arg; |
1886 | void *arg; |
1613 | }; |
1887 | }; |
1614 | |
1888 | |
1615 | static void |
1889 | static void |
… | |
… | |
1624 | |
1898 | |
1625 | cb (revents, arg); |
1899 | cb (revents, arg); |
1626 | } |
1900 | } |
1627 | |
1901 | |
1628 | static void |
1902 | static void |
1629 | once_cb_io (EV_P_ struct ev_io *w, int revents) |
1903 | once_cb_io (EV_P_ ev_io *w, int revents) |
1630 | { |
1904 | { |
1631 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1905 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
1632 | } |
1906 | } |
1633 | |
1907 | |
1634 | static void |
1908 | static void |
1635 | once_cb_to (EV_P_ struct ev_timer *w, int revents) |
1909 | once_cb_to (EV_P_ ev_timer *w, int revents) |
1636 | { |
1910 | { |
1637 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1911 | once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
1638 | } |
1912 | } |
1639 | |
1913 | |
1640 | void |
1914 | void |
1641 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1915 | ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1642 | { |
1916 | { |
1643 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1917 | struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); |
1644 | |
1918 | |
1645 | if (!once) |
1919 | if (expect_false (!once)) |
|
|
1920 | { |
1646 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1921 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1647 | else |
1922 | return; |
1648 | { |
1923 | } |
|
|
1924 | |
1649 | once->cb = cb; |
1925 | once->cb = cb; |
1650 | once->arg = arg; |
1926 | once->arg = arg; |
1651 | |
1927 | |
1652 | ev_init (&once->io, once_cb_io); |
1928 | ev_init (&once->io, once_cb_io); |
1653 | if (fd >= 0) |
1929 | if (fd >= 0) |
1654 | { |
1930 | { |
1655 | ev_io_set (&once->io, fd, events); |
1931 | ev_io_set (&once->io, fd, events); |
1656 | ev_io_start (EV_A_ &once->io); |
1932 | ev_io_start (EV_A_ &once->io); |
1657 | } |
1933 | } |
1658 | |
1934 | |
1659 | ev_init (&once->to, once_cb_to); |
1935 | ev_init (&once->to, once_cb_to); |
1660 | if (timeout >= 0.) |
1936 | if (timeout >= 0.) |
1661 | { |
1937 | { |
1662 | ev_timer_set (&once->to, timeout, 0.); |
1938 | ev_timer_set (&once->to, timeout, 0.); |
1663 | ev_timer_start (EV_A_ &once->to); |
1939 | ev_timer_start (EV_A_ &once->to); |
1664 | } |
|
|
1665 | } |
1940 | } |
1666 | } |
1941 | } |
1667 | |
1942 | |
1668 | #ifdef __cplusplus |
1943 | #ifdef __cplusplus |
1669 | } |
1944 | } |