|
|
1 | /* |
|
|
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
3 | * All rights reserved. |
|
|
4 | * |
|
|
5 | * Redistribution and use in source and binary forms, with or without |
|
|
6 | * modification, are permitted provided that the following conditions are |
|
|
7 | * met: |
|
|
8 | * |
|
|
9 | * * Redistributions of source code must retain the above copyright |
|
|
10 | * notice, this list of conditions and the following disclaimer. |
|
|
11 | * |
|
|
12 | * * Redistributions in binary form must reproduce the above |
|
|
13 | * copyright notice, this list of conditions and the following |
|
|
14 | * disclaimer in the documentation and/or other materials provided |
|
|
15 | * with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
28 | */ |
|
|
29 | |
1 | #include <math.h> |
30 | #include <math.h> |
2 | #include <stdlib.h> |
31 | #include <stdlib.h> |
3 | #include <unistd.h> |
32 | #include <unistd.h> |
4 | #include <fcntl.h> |
33 | #include <fcntl.h> |
5 | #include <signal.h> |
34 | #include <signal.h> |
|
|
35 | #include <stddef.h> |
6 | |
36 | |
7 | #include <stdio.h> |
37 | #include <stdio.h> |
8 | |
38 | |
9 | #include <assert.h> |
39 | #include <assert.h> |
10 | #include <errno.h> |
40 | #include <errno.h> |
|
|
41 | #include <sys/types.h> |
|
|
42 | #include <sys/wait.h> |
11 | #include <sys/time.h> |
43 | #include <sys/time.h> |
12 | #include <time.h> |
44 | #include <time.h> |
13 | |
45 | |
14 | #ifndef HAVE_MONOTONIC |
46 | #ifndef HAVE_MONOTONIC |
15 | # ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
… | |
… | |
29 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
30 | #endif |
62 | #endif |
31 | |
63 | |
32 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
33 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
|
|
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
34 | |
67 | |
35 | #include "ev.h" |
68 | #include "ev.h" |
36 | |
|
|
37 | struct ev_watcher { |
|
|
38 | EV_WATCHER (ev_watcher); |
|
|
39 | }; |
|
|
40 | |
|
|
41 | struct ev_watcher_list { |
|
|
42 | EV_WATCHER_LIST (ev_watcher_list); |
|
|
43 | }; |
|
|
44 | |
69 | |
45 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher *W; |
46 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_list *WL; |
|
|
72 | typedef struct ev_watcher_time *WT; |
47 | |
73 | |
48 | static ev_tstamp now, diff; /* monotonic clock */ |
74 | static ev_tstamp now, diff; /* monotonic clock */ |
49 | ev_tstamp ev_now; |
75 | ev_tstamp ev_now; |
50 | int ev_method; |
76 | int ev_method; |
51 | |
77 | |
… | |
… | |
87 | } |
113 | } |
88 | |
114 | |
89 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
90 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
91 | { \ |
117 | { \ |
92 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
93 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
94 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
95 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
96 | cur = newcnt; \ |
127 | cur = newcnt; \ |
97 | } |
128 | } |
98 | |
129 | |
99 | /*****************************************************************************/ |
130 | /*****************************************************************************/ |
100 | |
131 | |
101 | typedef struct |
132 | typedef struct |
102 | { |
133 | { |
103 | struct ev_io *head; |
134 | struct ev_io *head; |
104 | unsigned char wev, rev; /* want, received event set */ |
135 | int events; |
105 | } ANFD; |
136 | } ANFD; |
106 | |
137 | |
107 | static ANFD *anfds; |
138 | static ANFD *anfds; |
108 | static int anfdmax; |
139 | static int anfdmax; |
109 | |
140 | |
110 | static int *fdchanges; |
|
|
111 | static int fdchangemax, fdchangecnt; |
|
|
112 | |
|
|
113 | static void |
141 | static void |
114 | anfds_init (ANFD *base, int count) |
142 | anfds_init (ANFD *base, int count) |
115 | { |
143 | { |
116 | while (count--) |
144 | while (count--) |
117 | { |
145 | { |
118 | base->head = 0; |
146 | base->head = 0; |
119 | base->wev = base->rev = EV_NONE; |
147 | base->events = EV_NONE; |
120 | ++base; |
148 | ++base; |
121 | } |
149 | } |
122 | } |
150 | } |
123 | |
151 | |
124 | typedef struct |
152 | typedef struct |
… | |
… | |
131 | static int pendingmax, pendingcnt; |
159 | static int pendingmax, pendingcnt; |
132 | |
160 | |
133 | static void |
161 | static void |
134 | event (W w, int events) |
162 | event (W w, int events) |
135 | { |
163 | { |
|
|
164 | if (w->active) |
|
|
165 | { |
136 | w->pending = ++pendingcnt; |
166 | w->pending = ++pendingcnt; |
137 | array_needsize (pendings, pendingmax, pendingcnt, ); |
167 | array_needsize (pendings, pendingmax, pendingcnt, ); |
138 | pendings [pendingcnt - 1].w = w; |
168 | pendings [pendingcnt - 1].w = w; |
139 | pendings [pendingcnt - 1].events = events; |
169 | pendings [pendingcnt - 1].events = events; |
|
|
170 | } |
|
|
171 | } |
|
|
172 | |
|
|
173 | static void |
|
|
174 | queue_events (W *events, int eventcnt, int type) |
|
|
175 | { |
|
|
176 | int i; |
|
|
177 | |
|
|
178 | for (i = 0; i < eventcnt; ++i) |
|
|
179 | event (events [i], type); |
140 | } |
180 | } |
141 | |
181 | |
142 | static void |
182 | static void |
143 | fd_event (int fd, int events) |
183 | fd_event (int fd, int events) |
144 | { |
184 | { |
… | |
… | |
152 | if (ev) |
192 | if (ev) |
153 | event ((W)w, ev); |
193 | event ((W)w, ev); |
154 | } |
194 | } |
155 | } |
195 | } |
156 | |
196 | |
|
|
197 | /*****************************************************************************/ |
|
|
198 | |
|
|
199 | static int *fdchanges; |
|
|
200 | static int fdchangemax, fdchangecnt; |
|
|
201 | |
157 | static void |
202 | static void |
158 | queue_events (W *events, int eventcnt, int type) |
203 | fd_reify (void) |
159 | { |
204 | { |
160 | int i; |
205 | int i; |
161 | |
206 | |
162 | for (i = 0; i < eventcnt; ++i) |
207 | for (i = 0; i < fdchangecnt; ++i) |
163 | event (events [i], type); |
208 | { |
164 | } |
209 | int fd = fdchanges [i]; |
|
|
210 | ANFD *anfd = anfds + fd; |
|
|
211 | struct ev_io *w; |
165 | |
212 | |
166 | /*****************************************************************************/ |
213 | int events = 0; |
167 | |
214 | |
|
|
215 | for (w = anfd->head; w; w = w->next) |
|
|
216 | events |= w->events; |
|
|
217 | |
|
|
218 | anfd->events &= ~EV_REIFY; |
|
|
219 | |
|
|
220 | if (anfd->events != events) |
|
|
221 | { |
|
|
222 | method_modify (fd, anfd->events, events); |
|
|
223 | anfd->events = events; |
|
|
224 | } |
|
|
225 | } |
|
|
226 | |
|
|
227 | fdchangecnt = 0; |
|
|
228 | } |
|
|
229 | |
|
|
230 | static void |
|
|
231 | fd_change (int fd) |
|
|
232 | { |
|
|
233 | if (anfds [fd].events & EV_REIFY) |
|
|
234 | return; |
|
|
235 | |
|
|
236 | anfds [fd].events |= EV_REIFY; |
|
|
237 | |
|
|
238 | ++fdchangecnt; |
|
|
239 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
240 | fdchanges [fdchangecnt - 1] = fd; |
|
|
241 | } |
|
|
242 | |
|
|
243 | /* called on EBADF to verify fds */ |
|
|
244 | static void |
|
|
245 | fd_recheck (void) |
|
|
246 | { |
|
|
247 | int fd; |
|
|
248 | |
|
|
249 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
250 | if (anfds [fd].events) |
|
|
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
252 | while (anfds [fd].head) |
|
|
253 | { |
|
|
254 | event ((W)anfds [fd].head, EV_ERROR); |
|
|
255 | ev_io_stop (anfds [fd].head); |
|
|
256 | } |
|
|
257 | } |
|
|
258 | |
|
|
259 | /*****************************************************************************/ |
|
|
260 | |
168 | static struct ev_timer **atimers; |
261 | static struct ev_timer **timers; |
169 | static int atimermax, atimercnt; |
262 | static int timermax, timercnt; |
170 | |
263 | |
171 | static struct ev_timer **rtimers; |
264 | static struct ev_periodic **periodics; |
172 | static int rtimermax, rtimercnt; |
265 | static int periodicmax, periodiccnt; |
173 | |
266 | |
174 | static void |
267 | static void |
175 | upheap (struct ev_timer **timers, int k) |
268 | upheap (WT *timers, int k) |
176 | { |
269 | { |
177 | struct ev_timer *w = timers [k]; |
270 | WT w = timers [k]; |
178 | |
271 | |
179 | while (k && timers [k >> 1]->at > w->at) |
272 | while (k && timers [k >> 1]->at > w->at) |
180 | { |
273 | { |
181 | timers [k] = timers [k >> 1]; |
274 | timers [k] = timers [k >> 1]; |
182 | timers [k]->active = k + 1; |
275 | timers [k]->active = k + 1; |
… | |
… | |
187 | timers [k]->active = k + 1; |
280 | timers [k]->active = k + 1; |
188 | |
281 | |
189 | } |
282 | } |
190 | |
283 | |
191 | static void |
284 | static void |
192 | downheap (struct ev_timer **timers, int N, int k) |
285 | downheap (WT *timers, int N, int k) |
193 | { |
286 | { |
194 | struct ev_timer *w = timers [k]; |
287 | WT w = timers [k]; |
195 | |
288 | |
196 | while (k < (N >> 1)) |
289 | while (k < (N >> 1)) |
197 | { |
290 | { |
198 | int j = k << 1; |
291 | int j = k << 1; |
199 | |
292 | |
… | |
… | |
277 | |
370 | |
278 | /* rather than sort out wether we really need nb, set it */ |
371 | /* rather than sort out wether we really need nb, set it */ |
279 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
372 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
280 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
373 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
281 | |
374 | |
282 | evio_set (&sigev, sigpipe [0], EV_READ); |
375 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
283 | evio_start (&sigev); |
376 | ev_io_start (&sigev); |
284 | } |
377 | } |
285 | |
378 | |
286 | /*****************************************************************************/ |
379 | /*****************************************************************************/ |
287 | |
380 | |
288 | static struct ev_idle **idles; |
381 | static struct ev_idle **idles; |
289 | static int idlemax, idlecnt; |
382 | static int idlemax, idlecnt; |
290 | |
383 | |
|
|
384 | static struct ev_prepare **prepares; |
|
|
385 | static int preparemax, preparecnt; |
|
|
386 | |
291 | static struct ev_check **checks; |
387 | static struct ev_check **checks; |
292 | static int checkmax, checkcnt; |
388 | static int checkmax, checkcnt; |
293 | |
389 | |
294 | /*****************************************************************************/ |
390 | /*****************************************************************************/ |
295 | |
391 | |
|
|
392 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
393 | static struct ev_signal childev; |
|
|
394 | |
|
|
395 | #ifndef WCONTINUED |
|
|
396 | # define WCONTINUED 0 |
|
|
397 | #endif |
|
|
398 | |
|
|
399 | static void |
|
|
400 | childcb (struct ev_signal *sw, int revents) |
|
|
401 | { |
|
|
402 | struct ev_child *w; |
|
|
403 | int pid, status; |
|
|
404 | |
|
|
405 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
406 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
407 | if (w->pid == pid || w->pid == -1) |
|
|
408 | { |
|
|
409 | w->status = status; |
|
|
410 | event ((W)w, EV_CHILD); |
|
|
411 | } |
|
|
412 | } |
|
|
413 | |
|
|
414 | /*****************************************************************************/ |
|
|
415 | |
296 | #if HAVE_EPOLL |
416 | #if HAVE_EPOLL |
297 | # include "ev_epoll.c" |
417 | # include "ev_epoll.c" |
298 | #endif |
418 | #endif |
299 | #if HAVE_SELECT |
419 | #if HAVE_SELECT |
300 | # include "ev_select.c" |
420 | # include "ev_select.c" |
301 | #endif |
421 | #endif |
302 | |
422 | |
|
|
423 | int |
|
|
424 | ev_version_major (void) |
|
|
425 | { |
|
|
426 | return EV_VERSION_MAJOR; |
|
|
427 | } |
|
|
428 | |
|
|
429 | int |
|
|
430 | ev_version_minor (void) |
|
|
431 | { |
|
|
432 | return EV_VERSION_MINOR; |
|
|
433 | } |
|
|
434 | |
303 | int ev_init (int flags) |
435 | int ev_init (int flags) |
304 | { |
436 | { |
|
|
437 | if (!ev_method) |
|
|
438 | { |
305 | #if HAVE_MONOTONIC |
439 | #if HAVE_MONOTONIC |
306 | { |
440 | { |
307 | struct timespec ts; |
441 | struct timespec ts; |
308 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
309 | have_monotonic = 1; |
443 | have_monotonic = 1; |
310 | } |
444 | } |
311 | #endif |
445 | #endif |
312 | |
446 | |
313 | ev_now = ev_time (); |
447 | ev_now = ev_time (); |
314 | now = get_clock (); |
448 | now = get_clock (); |
315 | diff = ev_now - now; |
449 | diff = ev_now - now; |
316 | |
450 | |
317 | if (pipe (sigpipe)) |
451 | if (pipe (sigpipe)) |
318 | return 0; |
452 | return 0; |
319 | |
453 | |
320 | ev_method = EVMETHOD_NONE; |
454 | ev_method = EVMETHOD_NONE; |
321 | #if HAVE_EPOLL |
455 | #if HAVE_EPOLL |
322 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
323 | #endif |
457 | #endif |
324 | #if HAVE_SELECT |
458 | #if HAVE_SELECT |
325 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
326 | #endif |
460 | #endif |
327 | |
461 | |
328 | if (ev_method) |
462 | if (ev_method) |
329 | { |
463 | { |
330 | evw_init (&sigev, sigcb, 0); |
464 | ev_watcher_init (&sigev, sigcb); |
331 | siginit (); |
465 | siginit (); |
|
|
466 | |
|
|
467 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
468 | ev_signal_start (&childev); |
|
|
469 | } |
332 | } |
470 | } |
333 | |
471 | |
334 | return ev_method; |
472 | return ev_method; |
335 | } |
473 | } |
336 | |
474 | |
337 | /*****************************************************************************/ |
475 | /*****************************************************************************/ |
338 | |
476 | |
|
|
477 | void |
339 | void ev_prefork (void) |
478 | ev_prefork (void) |
340 | { |
479 | { |
341 | /* nop */ |
480 | /* nop */ |
342 | } |
481 | } |
343 | |
482 | |
|
|
483 | void |
344 | void ev_postfork_parent (void) |
484 | ev_postfork_parent (void) |
345 | { |
485 | { |
346 | /* nop */ |
486 | /* nop */ |
347 | } |
487 | } |
348 | |
488 | |
|
|
489 | void |
349 | void ev_postfork_child (void) |
490 | ev_postfork_child (void) |
350 | { |
491 | { |
351 | #if HAVE_EPOLL |
492 | #if HAVE_EPOLL |
352 | if (ev_method == EVMETHOD_EPOLL) |
493 | if (ev_method == EVMETHOD_EPOLL) |
353 | epoll_postfork_child (); |
494 | epoll_postfork_child (); |
354 | #endif |
495 | #endif |
355 | |
496 | |
356 | evio_stop (&sigev); |
497 | ev_io_stop (&sigev); |
357 | close (sigpipe [0]); |
498 | close (sigpipe [0]); |
358 | close (sigpipe [1]); |
499 | close (sigpipe [1]); |
359 | pipe (sigpipe); |
500 | pipe (sigpipe); |
360 | siginit (); |
501 | siginit (); |
361 | } |
502 | } |
362 | |
503 | |
363 | /*****************************************************************************/ |
504 | /*****************************************************************************/ |
364 | |
505 | |
365 | static void |
506 | static void |
366 | fd_reify (void) |
|
|
367 | { |
|
|
368 | int i; |
|
|
369 | |
|
|
370 | for (i = 0; i < fdchangecnt; ++i) |
|
|
371 | { |
|
|
372 | int fd = fdchanges [i]; |
|
|
373 | ANFD *anfd = anfds + fd; |
|
|
374 | struct ev_io *w; |
|
|
375 | |
|
|
376 | int wev = 0; |
|
|
377 | |
|
|
378 | for (w = anfd->head; w; w = w->next) |
|
|
379 | wev |= w->events; |
|
|
380 | |
|
|
381 | if (anfd->wev != wev) |
|
|
382 | { |
|
|
383 | method_modify (fd, anfd->wev, wev); |
|
|
384 | anfd->wev = wev; |
|
|
385 | } |
|
|
386 | } |
|
|
387 | |
|
|
388 | fdchangecnt = 0; |
|
|
389 | } |
|
|
390 | |
|
|
391 | static void |
|
|
392 | call_pending () |
507 | call_pending (void) |
393 | { |
508 | { |
394 | int i; |
509 | while (pendingcnt) |
395 | |
|
|
396 | for (i = 0; i < pendingcnt; ++i) |
|
|
397 | { |
510 | { |
398 | ANPENDING *p = pendings + i; |
511 | ANPENDING *p = pendings + --pendingcnt; |
399 | |
512 | |
400 | if (p->w) |
513 | if (p->w) |
401 | { |
514 | { |
402 | p->w->pending = 0; |
515 | p->w->pending = 0; |
403 | p->w->cb (p->w, p->events); |
516 | p->w->cb (p->w, p->events); |
404 | } |
517 | } |
405 | } |
518 | } |
406 | |
|
|
407 | pendingcnt = 0; |
|
|
408 | } |
519 | } |
409 | |
520 | |
410 | static void |
521 | static void |
411 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
522 | timers_reify (void) |
412 | { |
523 | { |
413 | while (timercnt && timers [0]->at <= now) |
524 | while (timercnt && timers [0]->at <= now) |
414 | { |
525 | { |
415 | struct ev_timer *w = timers [0]; |
526 | struct ev_timer *w = timers [0]; |
|
|
527 | |
|
|
528 | event ((W)w, EV_TIMEOUT); |
416 | |
529 | |
417 | /* first reschedule or stop timer */ |
530 | /* first reschedule or stop timer */ |
418 | if (w->repeat) |
531 | if (w->repeat) |
419 | { |
532 | { |
420 | if (w->is_abs) |
|
|
421 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
422 | else |
|
|
423 | w->at = now + w->repeat; |
533 | w->at = now + w->repeat; |
424 | |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
425 | assert (w->at > now); |
|
|
426 | |
|
|
427 | downheap (timers, timercnt, 0); |
535 | downheap ((WT *)timers, timercnt, 0); |
428 | } |
536 | } |
429 | else |
537 | else |
430 | { |
|
|
431 | evtimer_stop (w); /* nonrepeating: stop timer */ |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
432 | --timercnt; /* maybe pass by reference instead? */ |
539 | } |
|
|
540 | } |
|
|
541 | |
|
|
542 | static void |
|
|
543 | periodics_reify (void) |
|
|
544 | { |
|
|
545 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
546 | { |
|
|
547 | struct ev_periodic *w = periodics [0]; |
|
|
548 | |
|
|
549 | /* first reschedule or stop timer */ |
|
|
550 | if (w->interval) |
433 | } |
551 | { |
|
|
552 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
553 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
554 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
555 | } |
|
|
556 | else |
|
|
557 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
434 | |
558 | |
435 | event ((W)w, EV_TIMEOUT); |
559 | event ((W)w, EV_TIMEOUT); |
436 | } |
560 | } |
437 | } |
561 | } |
438 | |
562 | |
439 | static void |
563 | static void |
440 | time_update () |
564 | periodics_reschedule (ev_tstamp diff) |
441 | { |
565 | { |
442 | int i; |
566 | int i; |
|
|
567 | |
|
|
568 | /* adjust periodics after time jump */ |
|
|
569 | for (i = 0; i < periodiccnt; ++i) |
|
|
570 | { |
|
|
571 | struct ev_periodic *w = periodics [i]; |
|
|
572 | |
|
|
573 | if (w->interval) |
|
|
574 | { |
|
|
575 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
576 | |
|
|
577 | if (fabs (diff) >= 1e-4) |
|
|
578 | { |
|
|
579 | ev_periodic_stop (w); |
|
|
580 | ev_periodic_start (w); |
|
|
581 | |
|
|
582 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
583 | } |
|
|
584 | } |
|
|
585 | } |
|
|
586 | } |
|
|
587 | |
|
|
588 | static void |
|
|
589 | time_update (void) |
|
|
590 | { |
|
|
591 | int i; |
|
|
592 | |
443 | ev_now = ev_time (); |
593 | ev_now = ev_time (); |
444 | |
594 | |
445 | if (have_monotonic) |
595 | if (have_monotonic) |
446 | { |
596 | { |
447 | ev_tstamp odiff = diff; |
597 | ev_tstamp odiff = diff; |
448 | |
598 | |
449 | /* detecting time jumps is much more difficult */ |
|
|
450 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
599 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
451 | { |
600 | { |
452 | now = get_clock (); |
601 | now = get_clock (); |
453 | diff = ev_now - now; |
602 | diff = ev_now - now; |
454 | |
603 | |
455 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
604 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
456 | return; /* all is well */ |
605 | return; /* all is well */ |
457 | |
606 | |
458 | ev_now = ev_time (); |
607 | ev_now = ev_time (); |
459 | } |
608 | } |
460 | |
609 | |
461 | /* time jump detected, reschedule atimers */ |
610 | periodics_reschedule (diff - odiff); |
462 | for (i = 0; i < atimercnt; ++i) |
611 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
463 | { |
|
|
464 | struct ev_timer *w = atimers [i]; |
|
|
465 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
466 | } |
|
|
467 | } |
612 | } |
468 | else |
613 | else |
469 | { |
614 | { |
470 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
615 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
471 | /* time jump detected, adjust rtimers */ |
616 | { |
|
|
617 | periodics_reschedule (ev_now - now); |
|
|
618 | |
|
|
619 | /* adjust timers. this is easy, as the offset is the same for all */ |
472 | for (i = 0; i < rtimercnt; ++i) |
620 | for (i = 0; i < timercnt; ++i) |
473 | rtimers [i]->at += ev_now - now; |
621 | timers [i]->at += diff; |
|
|
622 | } |
474 | |
623 | |
475 | now = ev_now; |
624 | now = ev_now; |
476 | } |
625 | } |
477 | } |
626 | } |
478 | |
627 | |
479 | int ev_loop_done; |
628 | int ev_loop_done; |
480 | |
629 | |
481 | void ev_loop (int flags) |
630 | void ev_loop (int flags) |
482 | { |
631 | { |
483 | double block; |
632 | double block; |
484 | ev_loop_done = flags & EVLOOP_ONESHOT; |
633 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
485 | |
|
|
486 | if (checkcnt) |
|
|
487 | { |
|
|
488 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
489 | call_pending (); |
|
|
490 | } |
|
|
491 | |
634 | |
492 | do |
635 | do |
493 | { |
636 | { |
|
|
637 | /* queue check watchers (and execute them) */ |
|
|
638 | if (preparecnt) |
|
|
639 | { |
|
|
640 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
641 | call_pending (); |
|
|
642 | } |
|
|
643 | |
494 | /* update fd-related kernel structures */ |
644 | /* update fd-related kernel structures */ |
495 | fd_reify (); |
645 | fd_reify (); |
496 | |
646 | |
497 | /* calculate blocking time */ |
647 | /* calculate blocking time */ |
|
|
648 | |
|
|
649 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
650 | always have timers, we just calculate it always */ |
|
|
651 | ev_now = ev_time (); |
|
|
652 | |
498 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
653 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
499 | block = 0.; |
654 | block = 0.; |
500 | else |
655 | else |
501 | { |
656 | { |
502 | block = MAX_BLOCKTIME; |
657 | block = MAX_BLOCKTIME; |
503 | |
658 | |
504 | if (rtimercnt) |
659 | if (timercnt) |
505 | { |
660 | { |
506 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
661 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
507 | if (block > to) block = to; |
662 | if (block > to) block = to; |
508 | } |
663 | } |
509 | |
664 | |
510 | if (atimercnt) |
665 | if (periodiccnt) |
511 | { |
666 | { |
512 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
667 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
513 | if (block > to) block = to; |
668 | if (block > to) block = to; |
514 | } |
669 | } |
515 | |
670 | |
516 | if (block < 0.) block = 0.; |
671 | if (block < 0.) block = 0.; |
517 | } |
672 | } |
… | |
… | |
520 | |
675 | |
521 | /* update ev_now, do magic */ |
676 | /* update ev_now, do magic */ |
522 | time_update (); |
677 | time_update (); |
523 | |
678 | |
524 | /* queue pending timers and reschedule them */ |
679 | /* queue pending timers and reschedule them */ |
525 | /* absolute timers first */ |
680 | timers_reify (); /* relative timers called last */ |
526 | timers_reify (atimers, atimercnt, ev_now); |
681 | periodics_reify (); /* absolute timers called first */ |
527 | /* relative timers second */ |
|
|
528 | timers_reify (rtimers, rtimercnt, now); |
|
|
529 | |
682 | |
530 | /* queue idle watchers unless io or timers are pending */ |
683 | /* queue idle watchers unless io or timers are pending */ |
531 | if (!pendingcnt) |
684 | if (!pendingcnt) |
532 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
685 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
533 | |
686 | |
534 | /* queue check and possibly idle watchers */ |
687 | /* queue check watchers, to be executed first */ |
|
|
688 | if (checkcnt) |
535 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
689 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
536 | |
690 | |
537 | call_pending (); |
691 | call_pending (); |
538 | } |
692 | } |
539 | while (!ev_loop_done); |
693 | while (!ev_loop_done); |
|
|
694 | |
|
|
695 | if (ev_loop_done != 2) |
|
|
696 | ev_loop_done = 0; |
540 | } |
697 | } |
541 | |
698 | |
542 | /*****************************************************************************/ |
699 | /*****************************************************************************/ |
543 | |
700 | |
544 | static void |
701 | static void |
… | |
… | |
562 | head = &(*head)->next; |
719 | head = &(*head)->next; |
563 | } |
720 | } |
564 | } |
721 | } |
565 | |
722 | |
566 | static void |
723 | static void |
|
|
724 | ev_clear (W w) |
|
|
725 | { |
|
|
726 | if (w->pending) |
|
|
727 | { |
|
|
728 | pendings [w->pending - 1].w = 0; |
|
|
729 | w->pending = 0; |
|
|
730 | } |
|
|
731 | } |
|
|
732 | |
|
|
733 | static void |
567 | ev_start (W w, int active) |
734 | ev_start (W w, int active) |
568 | { |
735 | { |
569 | w->pending = 0; |
|
|
570 | w->active = active; |
736 | w->active = active; |
571 | } |
737 | } |
572 | |
738 | |
573 | static void |
739 | static void |
574 | ev_stop (W w) |
740 | ev_stop (W w) |
575 | { |
741 | { |
576 | if (w->pending) |
|
|
577 | pendings [w->pending - 1].w = 0; |
|
|
578 | |
|
|
579 | w->active = 0; |
742 | w->active = 0; |
580 | } |
743 | } |
581 | |
744 | |
582 | /*****************************************************************************/ |
745 | /*****************************************************************************/ |
583 | |
746 | |
584 | void |
747 | void |
585 | evio_start (struct ev_io *w) |
748 | ev_io_start (struct ev_io *w) |
586 | { |
749 | { |
587 | if (ev_is_active (w)) |
750 | if (ev_is_active (w)) |
588 | return; |
751 | return; |
589 | |
752 | |
590 | int fd = w->fd; |
753 | int fd = w->fd; |
591 | |
754 | |
592 | ev_start ((W)w, 1); |
755 | ev_start ((W)w, 1); |
593 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
756 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
594 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
757 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
595 | |
758 | |
596 | ++fdchangecnt; |
759 | fd_change (fd); |
597 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
598 | fdchanges [fdchangecnt - 1] = fd; |
|
|
599 | } |
760 | } |
600 | |
761 | |
601 | void |
762 | void |
602 | evio_stop (struct ev_io *w) |
763 | ev_io_stop (struct ev_io *w) |
603 | { |
764 | { |
|
|
765 | ev_clear ((W)w); |
604 | if (!ev_is_active (w)) |
766 | if (!ev_is_active (w)) |
605 | return; |
767 | return; |
606 | |
768 | |
607 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
769 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
608 | ev_stop ((W)w); |
770 | ev_stop ((W)w); |
609 | |
771 | |
610 | ++fdchangecnt; |
772 | fd_change (w->fd); |
611 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
612 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
613 | } |
773 | } |
614 | |
774 | |
615 | void |
775 | void |
616 | evtimer_start (struct ev_timer *w) |
776 | ev_timer_start (struct ev_timer *w) |
617 | { |
777 | { |
|
|
778 | if (ev_is_active (w)) |
|
|
779 | return; |
|
|
780 | |
|
|
781 | w->at += now; |
|
|
782 | |
|
|
783 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
784 | |
|
|
785 | ev_start ((W)w, ++timercnt); |
|
|
786 | array_needsize (timers, timermax, timercnt, ); |
|
|
787 | timers [timercnt - 1] = w; |
|
|
788 | upheap ((WT *)timers, timercnt - 1); |
|
|
789 | } |
|
|
790 | |
|
|
791 | void |
|
|
792 | ev_timer_stop (struct ev_timer *w) |
|
|
793 | { |
|
|
794 | ev_clear ((W)w); |
618 | if (ev_is_active (w)) |
795 | if (!ev_is_active (w)) |
619 | return; |
796 | return; |
620 | |
797 | |
621 | if (w->is_abs) |
798 | if (w->active < timercnt--) |
|
|
799 | { |
|
|
800 | timers [w->active - 1] = timers [timercnt]; |
|
|
801 | downheap ((WT *)timers, timercnt, w->active - 1); |
622 | { |
802 | } |
623 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
803 | |
|
|
804 | w->at = w->repeat; |
|
|
805 | |
|
|
806 | ev_stop ((W)w); |
|
|
807 | } |
|
|
808 | |
|
|
809 | void |
|
|
810 | ev_timer_again (struct ev_timer *w) |
|
|
811 | { |
|
|
812 | if (ev_is_active (w)) |
|
|
813 | { |
624 | if (w->repeat) |
814 | if (w->repeat) |
|
|
815 | { |
|
|
816 | w->at = now + w->repeat; |
|
|
817 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
818 | } |
|
|
819 | else |
|
|
820 | ev_timer_stop (w); |
|
|
821 | } |
|
|
822 | else if (w->repeat) |
|
|
823 | ev_timer_start (w); |
|
|
824 | } |
|
|
825 | |
|
|
826 | void |
|
|
827 | ev_periodic_start (struct ev_periodic *w) |
|
|
828 | { |
|
|
829 | if (ev_is_active (w)) |
|
|
830 | return; |
|
|
831 | |
|
|
832 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
833 | |
|
|
834 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
835 | if (w->interval) |
625 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
836 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
626 | |
837 | |
627 | ev_start ((W)w, ++atimercnt); |
838 | ev_start ((W)w, ++periodiccnt); |
628 | array_needsize (atimers, atimermax, atimercnt, ); |
839 | array_needsize (periodics, periodicmax, periodiccnt, ); |
629 | atimers [atimercnt - 1] = w; |
840 | periodics [periodiccnt - 1] = w; |
630 | upheap (atimers, atimercnt - 1); |
841 | upheap ((WT *)periodics, periodiccnt - 1); |
631 | } |
|
|
632 | else |
|
|
633 | { |
|
|
634 | w->at += now; |
|
|
635 | |
|
|
636 | ev_start ((W)w, ++rtimercnt); |
|
|
637 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
638 | rtimers [rtimercnt - 1] = w; |
|
|
639 | upheap (rtimers, rtimercnt - 1); |
|
|
640 | } |
|
|
641 | |
|
|
642 | } |
842 | } |
643 | |
843 | |
644 | void |
844 | void |
645 | evtimer_stop (struct ev_timer *w) |
845 | ev_periodic_stop (struct ev_periodic *w) |
646 | { |
846 | { |
|
|
847 | ev_clear ((W)w); |
647 | if (!ev_is_active (w)) |
848 | if (!ev_is_active (w)) |
648 | return; |
849 | return; |
649 | |
850 | |
650 | if (w->is_abs) |
|
|
651 | { |
|
|
652 | if (w->active < atimercnt--) |
851 | if (w->active < periodiccnt--) |
653 | { |
|
|
654 | atimers [w->active - 1] = atimers [atimercnt]; |
|
|
655 | downheap (atimers, atimercnt, w->active - 1); |
|
|
656 | } |
|
|
657 | } |
852 | { |
658 | else |
853 | periodics [w->active - 1] = periodics [periodiccnt]; |
659 | { |
854 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
660 | if (w->active < rtimercnt--) |
|
|
661 | { |
|
|
662 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
|
|
663 | downheap (rtimers, rtimercnt, w->active - 1); |
|
|
664 | } |
|
|
665 | } |
855 | } |
666 | |
856 | |
667 | ev_stop ((W)w); |
857 | ev_stop ((W)w); |
668 | } |
858 | } |
669 | |
859 | |
670 | void |
860 | void |
671 | evsignal_start (struct ev_signal *w) |
861 | ev_signal_start (struct ev_signal *w) |
672 | { |
862 | { |
673 | if (ev_is_active (w)) |
863 | if (ev_is_active (w)) |
674 | return; |
864 | return; |
675 | |
865 | |
676 | ev_start ((W)w, 1); |
866 | ev_start ((W)w, 1); |
… | |
… | |
686 | sigaction (w->signum, &sa, 0); |
876 | sigaction (w->signum, &sa, 0); |
687 | } |
877 | } |
688 | } |
878 | } |
689 | |
879 | |
690 | void |
880 | void |
691 | evsignal_stop (struct ev_signal *w) |
881 | ev_signal_stop (struct ev_signal *w) |
692 | { |
882 | { |
|
|
883 | ev_clear ((W)w); |
693 | if (!ev_is_active (w)) |
884 | if (!ev_is_active (w)) |
694 | return; |
885 | return; |
695 | |
886 | |
696 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
887 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
697 | ev_stop ((W)w); |
888 | ev_stop ((W)w); |
698 | |
889 | |
699 | if (!signals [w->signum - 1].head) |
890 | if (!signals [w->signum - 1].head) |
700 | signal (w->signum, SIG_DFL); |
891 | signal (w->signum, SIG_DFL); |
701 | } |
892 | } |
702 | |
893 | |
|
|
894 | void |
703 | void evidle_start (struct ev_idle *w) |
895 | ev_idle_start (struct ev_idle *w) |
704 | { |
896 | { |
705 | if (ev_is_active (w)) |
897 | if (ev_is_active (w)) |
706 | return; |
898 | return; |
707 | |
899 | |
708 | ev_start ((W)w, ++idlecnt); |
900 | ev_start ((W)w, ++idlecnt); |
709 | array_needsize (idles, idlemax, idlecnt, ); |
901 | array_needsize (idles, idlemax, idlecnt, ); |
710 | idles [idlecnt - 1] = w; |
902 | idles [idlecnt - 1] = w; |
711 | } |
903 | } |
712 | |
904 | |
|
|
905 | void |
713 | void evidle_stop (struct ev_idle *w) |
906 | ev_idle_stop (struct ev_idle *w) |
714 | { |
907 | { |
|
|
908 | ev_clear ((W)w); |
|
|
909 | if (ev_is_active (w)) |
|
|
910 | return; |
|
|
911 | |
715 | idles [w->active - 1] = idles [--idlecnt]; |
912 | idles [w->active - 1] = idles [--idlecnt]; |
716 | ev_stop ((W)w); |
913 | ev_stop ((W)w); |
717 | } |
914 | } |
718 | |
915 | |
|
|
916 | void |
|
|
917 | ev_prepare_start (struct ev_prepare *w) |
|
|
918 | { |
|
|
919 | if (ev_is_active (w)) |
|
|
920 | return; |
|
|
921 | |
|
|
922 | ev_start ((W)w, ++preparecnt); |
|
|
923 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
924 | prepares [preparecnt - 1] = w; |
|
|
925 | } |
|
|
926 | |
|
|
927 | void |
|
|
928 | ev_prepare_stop (struct ev_prepare *w) |
|
|
929 | { |
|
|
930 | ev_clear ((W)w); |
|
|
931 | if (ev_is_active (w)) |
|
|
932 | return; |
|
|
933 | |
|
|
934 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
935 | ev_stop ((W)w); |
|
|
936 | } |
|
|
937 | |
|
|
938 | void |
719 | void evcheck_start (struct ev_check *w) |
939 | ev_check_start (struct ev_check *w) |
720 | { |
940 | { |
721 | if (ev_is_active (w)) |
941 | if (ev_is_active (w)) |
722 | return; |
942 | return; |
723 | |
943 | |
724 | ev_start ((W)w, ++checkcnt); |
944 | ev_start ((W)w, ++checkcnt); |
725 | array_needsize (checks, checkmax, checkcnt, ); |
945 | array_needsize (checks, checkmax, checkcnt, ); |
726 | checks [checkcnt - 1] = w; |
946 | checks [checkcnt - 1] = w; |
727 | } |
947 | } |
728 | |
948 | |
|
|
949 | void |
729 | void evcheck_stop (struct ev_check *w) |
950 | ev_check_stop (struct ev_check *w) |
730 | { |
951 | { |
|
|
952 | ev_clear ((W)w); |
|
|
953 | if (ev_is_active (w)) |
|
|
954 | return; |
|
|
955 | |
731 | checks [w->active - 1] = checks [--checkcnt]; |
956 | checks [w->active - 1] = checks [--checkcnt]; |
732 | ev_stop ((W)w); |
957 | ev_stop ((W)w); |
733 | } |
958 | } |
734 | |
959 | |
|
|
960 | void |
|
|
961 | ev_child_start (struct ev_child *w) |
|
|
962 | { |
|
|
963 | if (ev_is_active (w)) |
|
|
964 | return; |
|
|
965 | |
|
|
966 | ev_start ((W)w, 1); |
|
|
967 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
968 | } |
|
|
969 | |
|
|
970 | void |
|
|
971 | ev_child_stop (struct ev_child *w) |
|
|
972 | { |
|
|
973 | ev_clear ((W)w); |
|
|
974 | if (ev_is_active (w)) |
|
|
975 | return; |
|
|
976 | |
|
|
977 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
978 | ev_stop ((W)w); |
|
|
979 | } |
|
|
980 | |
|
|
981 | /*****************************************************************************/ |
|
|
982 | |
|
|
983 | struct ev_once |
|
|
984 | { |
|
|
985 | struct ev_io io; |
|
|
986 | struct ev_timer to; |
|
|
987 | void (*cb)(int revents, void *arg); |
|
|
988 | void *arg; |
|
|
989 | }; |
|
|
990 | |
|
|
991 | static void |
|
|
992 | once_cb (struct ev_once *once, int revents) |
|
|
993 | { |
|
|
994 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
995 | void *arg = once->arg; |
|
|
996 | |
|
|
997 | ev_io_stop (&once->io); |
|
|
998 | ev_timer_stop (&once->to); |
|
|
999 | free (once); |
|
|
1000 | |
|
|
1001 | cb (revents, arg); |
|
|
1002 | } |
|
|
1003 | |
|
|
1004 | static void |
|
|
1005 | once_cb_io (struct ev_io *w, int revents) |
|
|
1006 | { |
|
|
1007 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
1008 | } |
|
|
1009 | |
|
|
1010 | static void |
|
|
1011 | once_cb_to (struct ev_timer *w, int revents) |
|
|
1012 | { |
|
|
1013 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
1014 | } |
|
|
1015 | |
|
|
1016 | void |
|
|
1017 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
1018 | { |
|
|
1019 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
1020 | |
|
|
1021 | if (!once) |
|
|
1022 | cb (EV_ERROR, arg); |
|
|
1023 | else |
|
|
1024 | { |
|
|
1025 | once->cb = cb; |
|
|
1026 | once->arg = arg; |
|
|
1027 | |
|
|
1028 | ev_watcher_init (&once->io, once_cb_io); |
|
|
1029 | |
|
|
1030 | if (fd >= 0) |
|
|
1031 | { |
|
|
1032 | ev_io_set (&once->io, fd, events); |
|
|
1033 | ev_io_start (&once->io); |
|
|
1034 | } |
|
|
1035 | |
|
|
1036 | ev_watcher_init (&once->to, once_cb_to); |
|
|
1037 | |
|
|
1038 | if (timeout >= 0.) |
|
|
1039 | { |
|
|
1040 | ev_timer_set (&once->to, timeout, 0.); |
|
|
1041 | ev_timer_start (&once->to); |
|
|
1042 | } |
|
|
1043 | } |
|
|
1044 | } |
|
|
1045 | |
735 | /*****************************************************************************/ |
1046 | /*****************************************************************************/ |
736 | |
1047 | |
737 | #if 0 |
1048 | #if 0 |
738 | |
1049 | |
|
|
1050 | struct ev_io wio; |
|
|
1051 | |
739 | static void |
1052 | static void |
740 | sin_cb (struct ev_io *w, int revents) |
1053 | sin_cb (struct ev_io *w, int revents) |
741 | { |
1054 | { |
742 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
1055 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
743 | } |
1056 | } |
744 | |
1057 | |
745 | static void |
1058 | static void |
746 | ocb (struct ev_timer *w, int revents) |
1059 | ocb (struct ev_timer *w, int revents) |
747 | { |
1060 | { |
748 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1061 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
749 | evtimer_stop (w); |
1062 | ev_timer_stop (w); |
750 | evtimer_start (w); |
1063 | ev_timer_start (w); |
751 | } |
1064 | } |
752 | |
1065 | |
753 | static void |
1066 | static void |
754 | scb (struct ev_signal *w, int revents) |
1067 | scb (struct ev_signal *w, int revents) |
755 | { |
1068 | { |
756 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1069 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1070 | ev_io_stop (&wio); |
|
|
1071 | ev_io_start (&wio); |
757 | } |
1072 | } |
758 | |
1073 | |
759 | static void |
1074 | static void |
760 | gcb (struct ev_signal *w, int revents) |
1075 | gcb (struct ev_signal *w, int revents) |
761 | { |
1076 | { |
762 | fprintf (stderr, "generic %x\n", revents); |
1077 | fprintf (stderr, "generic %x\n", revents); |
|
|
1078 | |
763 | } |
1079 | } |
764 | |
1080 | |
765 | int main (void) |
1081 | int main (void) |
766 | { |
1082 | { |
767 | struct ev_io sin; |
|
|
768 | |
|
|
769 | ev_init (0); |
1083 | ev_init (0); |
770 | |
1084 | |
771 | evw_init (&sin, sin_cb, 55); |
|
|
772 | evio_set (&sin, 0, EV_READ); |
1085 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
773 | evio_start (&sin); |
1086 | ev_io_start (&wio); |
774 | |
1087 | |
775 | struct ev_timer t[10000]; |
1088 | struct ev_timer t[10000]; |
776 | |
1089 | |
777 | #if 0 |
1090 | #if 0 |
778 | int i; |
1091 | int i; |
779 | for (i = 0; i < 10000; ++i) |
1092 | for (i = 0; i < 10000; ++i) |
780 | { |
1093 | { |
781 | struct ev_timer *w = t + i; |
1094 | struct ev_timer *w = t + i; |
782 | evw_init (w, ocb, i); |
1095 | ev_watcher_init (w, ocb, i); |
783 | evtimer_set_abs (w, drand48 (), 0.99775533); |
1096 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
784 | evtimer_start (w); |
1097 | ev_timer_start (w); |
785 | if (drand48 () < 0.5) |
1098 | if (drand48 () < 0.5) |
786 | evtimer_stop (w); |
1099 | ev_timer_stop (w); |
787 | } |
1100 | } |
788 | #endif |
1101 | #endif |
789 | |
1102 | |
790 | struct ev_timer t1; |
1103 | struct ev_timer t1; |
791 | evw_init (&t1, ocb, 0); |
1104 | ev_timer_init (&t1, ocb, 5, 10); |
792 | evtimer_set_abs (&t1, 5, 10); |
|
|
793 | evtimer_start (&t1); |
1105 | ev_timer_start (&t1); |
794 | |
1106 | |
795 | struct ev_signal sig; |
1107 | struct ev_signal sig; |
796 | evw_init (&sig, scb, 65535); |
|
|
797 | evsignal_set (&sig, SIGQUIT); |
1108 | ev_signal_init (&sig, scb, SIGQUIT); |
798 | evsignal_start (&sig); |
1109 | ev_signal_start (&sig); |
799 | |
1110 | |
800 | struct ev_check cw; |
1111 | struct ev_check cw; |
801 | evw_init (&cw, gcb, 0); |
1112 | ev_check_init (&cw, gcb); |
802 | evcheck_start (&cw); |
1113 | ev_check_start (&cw); |
803 | |
1114 | |
804 | struct ev_idle iw; |
1115 | struct ev_idle iw; |
805 | evw_init (&iw, gcb, 0); |
1116 | ev_idle_init (&iw, gcb); |
806 | evidle_start (&iw); |
1117 | ev_idle_start (&iw); |
807 | |
1118 | |
808 | ev_loop (0); |
1119 | ev_loop (0); |
809 | |
1120 | |
810 | return 0; |
1121 | return 0; |
811 | } |
1122 | } |