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