1 | /* |
1 | /* |
|
|
2 | * libev event processing core, watcher management |
|
|
3 | * |
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
8 | * modification, are permitted provided that the following conditions are |
… | |
… | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (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 |
27 | * 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. |
28 | */ |
30 | */ |
|
|
31 | #if EV_USE_CONFIG_H |
|
|
32 | # include "config.h" |
|
|
33 | #endif |
29 | |
34 | |
30 | #include <math.h> |
35 | #include <math.h> |
31 | #include <stdlib.h> |
36 | #include <stdlib.h> |
32 | #include <unistd.h> |
37 | #include <unistd.h> |
33 | #include <fcntl.h> |
38 | #include <fcntl.h> |
… | |
… | |
36 | |
41 | |
37 | #include <stdio.h> |
42 | #include <stdio.h> |
38 | |
43 | |
39 | #include <assert.h> |
44 | #include <assert.h> |
40 | #include <errno.h> |
45 | #include <errno.h> |
|
|
46 | #include <sys/types.h> |
|
|
47 | #include <sys/wait.h> |
41 | #include <sys/time.h> |
48 | #include <sys/time.h> |
42 | #include <time.h> |
49 | #include <time.h> |
43 | |
50 | |
44 | #ifndef HAVE_MONOTONIC |
51 | #ifndef EV_USE_MONOTONIC |
|
|
52 | # define EV_USE_MONOTONIC 1 |
|
|
53 | #endif |
|
|
54 | |
45 | # ifdef CLOCK_MONOTONIC |
55 | #ifndef CLOCK_MONOTONIC |
|
|
56 | # undef EV_USE_MONOTONIC |
46 | # define HAVE_MONOTONIC 1 |
57 | # define EV_USE_MONOTONIC 0 |
47 | # endif |
58 | #endif |
48 | #endif |
|
|
49 | |
59 | |
50 | #ifndef HAVE_SELECT |
60 | #ifndef EV_USE_SELECT |
51 | # define HAVE_SELECT 1 |
61 | # define EV_USE_SELECT 1 |
52 | #endif |
62 | #endif |
53 | |
63 | |
54 | #ifndef HAVE_EPOLL |
64 | #ifndef EV_USE_EPOLL |
55 | # define HAVE_EPOLL 0 |
65 | # define EV_USE_EPOLL 0 |
56 | #endif |
66 | #endif |
57 | |
67 | |
|
|
68 | #ifndef CLOCK_REALTIME |
|
|
69 | # define EV_USE_REALTIME 0 |
|
|
70 | #endif |
58 | #ifndef HAVE_REALTIME |
71 | #ifndef EV_USE_REALTIME |
59 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
72 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
60 | #endif |
73 | #endif |
61 | |
74 | |
62 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
75 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
63 | #define MAX_BLOCKTIME 60. |
76 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
|
|
77 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
78 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
64 | |
79 | |
65 | #include "ev.h" |
80 | #include "ev.h" |
66 | |
81 | |
67 | typedef struct ev_watcher *W; |
82 | typedef struct ev_watcher *W; |
68 | typedef struct ev_watcher_list *WL; |
83 | typedef struct ev_watcher_list *WL; |
… | |
… | |
81 | /*****************************************************************************/ |
96 | /*****************************************************************************/ |
82 | |
97 | |
83 | ev_tstamp |
98 | ev_tstamp |
84 | ev_time (void) |
99 | ev_time (void) |
85 | { |
100 | { |
86 | #if HAVE_REALTIME |
101 | #if EV_USE_REALTIME |
87 | struct timespec ts; |
102 | struct timespec ts; |
88 | clock_gettime (CLOCK_REALTIME, &ts); |
103 | clock_gettime (CLOCK_REALTIME, &ts); |
89 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
104 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
90 | #else |
105 | #else |
91 | struct timeval tv; |
106 | struct timeval tv; |
… | |
… | |
95 | } |
110 | } |
96 | |
111 | |
97 | static ev_tstamp |
112 | static ev_tstamp |
98 | get_clock (void) |
113 | get_clock (void) |
99 | { |
114 | { |
100 | #if HAVE_MONOTONIC |
115 | #if EV_USE_MONOTONIC |
101 | if (have_monotonic) |
116 | if (have_monotonic) |
102 | { |
117 | { |
103 | struct timespec ts; |
118 | struct timespec ts; |
104 | clock_gettime (CLOCK_MONOTONIC, &ts); |
119 | clock_gettime (CLOCK_MONOTONIC, &ts); |
105 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
120 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
… | |
… | |
107 | #endif |
122 | #endif |
108 | |
123 | |
109 | return ev_time (); |
124 | return ev_time (); |
110 | } |
125 | } |
111 | |
126 | |
|
|
127 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
|
|
128 | |
112 | #define array_needsize(base,cur,cnt,init) \ |
129 | #define array_needsize(base,cur,cnt,init) \ |
113 | if ((cnt) > cur) \ |
130 | if ((cnt) > cur) \ |
114 | { \ |
131 | { \ |
115 | int newcnt = cur ? cur << 1 : 16; \ |
132 | int newcnt = cur; \ |
|
|
133 | do \ |
|
|
134 | { \ |
|
|
135 | newcnt = array_roundsize (base, newcnt << 1); \ |
|
|
136 | } \ |
|
|
137 | while ((cnt) > newcnt); \ |
|
|
138 | \ |
116 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
139 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
117 | init (base + cur, newcnt - cur); \ |
140 | init (base + cur, newcnt - cur); \ |
118 | cur = newcnt; \ |
141 | cur = newcnt; \ |
119 | } |
142 | } |
120 | |
143 | |
121 | /*****************************************************************************/ |
144 | /*****************************************************************************/ |
122 | |
145 | |
123 | typedef struct |
146 | typedef struct |
124 | { |
147 | { |
125 | struct ev_io *head; |
148 | struct ev_io *head; |
126 | unsigned char wev, rev; /* want, received event set */ |
149 | unsigned char events; |
|
|
150 | unsigned char reify; |
127 | } ANFD; |
151 | } ANFD; |
128 | |
152 | |
129 | static ANFD *anfds; |
153 | static ANFD *anfds; |
130 | static int anfdmax; |
154 | static int anfdmax; |
131 | |
155 | |
132 | static int *fdchanges; |
|
|
133 | static int fdchangemax, fdchangecnt; |
|
|
134 | |
|
|
135 | static void |
156 | static void |
136 | anfds_init (ANFD *base, int count) |
157 | anfds_init (ANFD *base, int count) |
137 | { |
158 | { |
138 | while (count--) |
159 | while (count--) |
139 | { |
160 | { |
140 | base->head = 0; |
161 | base->head = 0; |
141 | base->wev = base->rev = EV_NONE; |
162 | base->events = EV_NONE; |
|
|
163 | base->reify = 0; |
|
|
164 | |
142 | ++base; |
165 | ++base; |
143 | } |
166 | } |
144 | } |
167 | } |
145 | |
168 | |
146 | typedef struct |
169 | typedef struct |
… | |
… | |
153 | static int pendingmax, pendingcnt; |
176 | static int pendingmax, pendingcnt; |
154 | |
177 | |
155 | static void |
178 | static void |
156 | event (W w, int events) |
179 | event (W w, int events) |
157 | { |
180 | { |
158 | if (w->active) |
181 | if (w->pending) |
|
|
182 | { |
|
|
183 | pendings [w->pending - 1].events |= events; |
|
|
184 | return; |
159 | { |
185 | } |
|
|
186 | |
160 | w->pending = ++pendingcnt; |
187 | w->pending = ++pendingcnt; |
161 | array_needsize (pendings, pendingmax, pendingcnt, ); |
188 | array_needsize (pendings, pendingmax, pendingcnt, ); |
162 | pendings [pendingcnt - 1].w = w; |
189 | pendings [pendingcnt - 1].w = w; |
163 | pendings [pendingcnt - 1].events = events; |
190 | pendings [pendingcnt - 1].events = events; |
164 | } |
191 | } |
|
|
192 | |
|
|
193 | static void |
|
|
194 | queue_events (W *events, int eventcnt, int type) |
|
|
195 | { |
|
|
196 | int i; |
|
|
197 | |
|
|
198 | for (i = 0; i < eventcnt; ++i) |
|
|
199 | event (events [i], type); |
165 | } |
200 | } |
166 | |
201 | |
167 | static void |
202 | static void |
168 | fd_event (int fd, int events) |
203 | fd_event (int fd, int events) |
169 | { |
204 | { |
… | |
… | |
177 | if (ev) |
212 | if (ev) |
178 | event ((W)w, ev); |
213 | event ((W)w, ev); |
179 | } |
214 | } |
180 | } |
215 | } |
181 | |
216 | |
|
|
217 | /*****************************************************************************/ |
|
|
218 | |
|
|
219 | static int *fdchanges; |
|
|
220 | static int fdchangemax, fdchangecnt; |
|
|
221 | |
182 | static void |
222 | static void |
183 | queue_events (W *events, int eventcnt, int type) |
223 | fd_reify (void) |
184 | { |
224 | { |
185 | int i; |
225 | int i; |
186 | |
226 | |
187 | for (i = 0; i < eventcnt; ++i) |
227 | for (i = 0; i < fdchangecnt; ++i) |
188 | event (events [i], type); |
228 | { |
|
|
229 | int fd = fdchanges [i]; |
|
|
230 | ANFD *anfd = anfds + fd; |
|
|
231 | struct ev_io *w; |
|
|
232 | |
|
|
233 | int events = 0; |
|
|
234 | |
|
|
235 | for (w = anfd->head; w; w = w->next) |
|
|
236 | events |= w->events; |
|
|
237 | |
|
|
238 | anfd->reify = 0; |
|
|
239 | |
|
|
240 | if (anfd->events != events) |
|
|
241 | { |
|
|
242 | method_modify (fd, anfd->events, events); |
|
|
243 | anfd->events = events; |
|
|
244 | } |
|
|
245 | } |
|
|
246 | |
|
|
247 | fdchangecnt = 0; |
|
|
248 | } |
|
|
249 | |
|
|
250 | static void |
|
|
251 | fd_change (int fd) |
|
|
252 | { |
|
|
253 | if (anfds [fd].reify || fdchangecnt < 0) |
|
|
254 | return; |
|
|
255 | |
|
|
256 | anfds [fd].reify = 1; |
|
|
257 | |
|
|
258 | ++fdchangecnt; |
|
|
259 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
260 | fdchanges [fdchangecnt - 1] = fd; |
189 | } |
261 | } |
190 | |
262 | |
191 | /* called on EBADF to verify fds */ |
263 | /* called on EBADF to verify fds */ |
192 | static void |
264 | static void |
193 | fd_recheck () |
265 | fd_recheck (void) |
194 | { |
266 | { |
195 | int fd; |
267 | int fd; |
196 | |
268 | |
197 | for (fd = 0; fd < anfdmax; ++fd) |
269 | for (fd = 0; fd < anfdmax; ++fd) |
198 | if (anfds [fd].wev) |
270 | if (anfds [fd].events) |
199 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
271 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
200 | while (anfds [fd].head) |
272 | while (anfds [fd].head) |
|
|
273 | { |
201 | evio_stop (anfds [fd].head); |
274 | ev_io_stop (anfds [fd].head); |
|
|
275 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
|
|
276 | } |
202 | } |
277 | } |
203 | |
278 | |
204 | /*****************************************************************************/ |
279 | /*****************************************************************************/ |
205 | |
280 | |
206 | static struct ev_timer **timers; |
281 | static struct ev_timer **timers; |
… | |
… | |
253 | /*****************************************************************************/ |
328 | /*****************************************************************************/ |
254 | |
329 | |
255 | typedef struct |
330 | typedef struct |
256 | { |
331 | { |
257 | struct ev_signal *head; |
332 | struct ev_signal *head; |
258 | sig_atomic_t gotsig; |
333 | sig_atomic_t volatile gotsig; |
259 | } ANSIG; |
334 | } ANSIG; |
260 | |
335 | |
261 | static ANSIG *signals; |
336 | static ANSIG *signals; |
262 | static int signalmax; |
337 | static int signalmax; |
263 | |
338 | |
264 | static int sigpipe [2]; |
339 | static int sigpipe [2]; |
265 | static sig_atomic_t gotsig; |
340 | static sig_atomic_t volatile gotsig; |
266 | static struct ev_io sigev; |
341 | static struct ev_io sigev; |
267 | |
342 | |
268 | static void |
343 | static void |
269 | signals_init (ANSIG *base, int count) |
344 | signals_init (ANSIG *base, int count) |
270 | { |
345 | { |
271 | while (count--) |
346 | while (count--) |
272 | { |
347 | { |
273 | base->head = 0; |
348 | base->head = 0; |
274 | base->gotsig = 0; |
349 | base->gotsig = 0; |
|
|
350 | |
275 | ++base; |
351 | ++base; |
276 | } |
352 | } |
277 | } |
353 | } |
278 | |
354 | |
279 | static void |
355 | static void |
… | |
… | |
282 | signals [signum - 1].gotsig = 1; |
358 | signals [signum - 1].gotsig = 1; |
283 | |
359 | |
284 | if (!gotsig) |
360 | if (!gotsig) |
285 | { |
361 | { |
286 | gotsig = 1; |
362 | gotsig = 1; |
287 | write (sigpipe [1], &gotsig, 1); |
363 | write (sigpipe [1], &signum, 1); |
288 | } |
364 | } |
289 | } |
365 | } |
290 | |
366 | |
291 | static void |
367 | static void |
292 | sigcb (struct ev_io *iow, int revents) |
368 | sigcb (struct ev_io *iow, int revents) |
293 | { |
369 | { |
294 | struct ev_signal *w; |
370 | struct ev_signal *w; |
295 | int sig; |
371 | int sig; |
296 | |
372 | |
|
|
373 | read (sigpipe [0], &revents, 1); |
297 | gotsig = 0; |
374 | gotsig = 0; |
298 | read (sigpipe [0], &revents, 1); |
|
|
299 | |
375 | |
300 | for (sig = signalmax; sig--; ) |
376 | for (sig = signalmax; sig--; ) |
301 | if (signals [sig].gotsig) |
377 | if (signals [sig].gotsig) |
302 | { |
378 | { |
303 | signals [sig].gotsig = 0; |
379 | signals [sig].gotsig = 0; |
… | |
… | |
315 | |
391 | |
316 | /* rather than sort out wether we really need nb, set it */ |
392 | /* rather than sort out wether we really need nb, set it */ |
317 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
393 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
318 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
394 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
319 | |
395 | |
320 | evio_set (&sigev, sigpipe [0], EV_READ); |
396 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
321 | evio_start (&sigev); |
397 | ev_io_start (&sigev); |
322 | } |
398 | } |
323 | |
399 | |
324 | /*****************************************************************************/ |
400 | /*****************************************************************************/ |
325 | |
401 | |
326 | static struct ev_idle **idles; |
402 | static struct ev_idle **idles; |
327 | static int idlemax, idlecnt; |
403 | static int idlemax, idlecnt; |
328 | |
404 | |
|
|
405 | static struct ev_prepare **prepares; |
|
|
406 | static int preparemax, preparecnt; |
|
|
407 | |
329 | static struct ev_check **checks; |
408 | static struct ev_check **checks; |
330 | static int checkmax, checkcnt; |
409 | static int checkmax, checkcnt; |
331 | |
410 | |
332 | /*****************************************************************************/ |
411 | /*****************************************************************************/ |
333 | |
412 | |
|
|
413 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
414 | static struct ev_signal childev; |
|
|
415 | |
|
|
416 | #ifndef WCONTINUED |
|
|
417 | # define WCONTINUED 0 |
|
|
418 | #endif |
|
|
419 | |
|
|
420 | static void |
|
|
421 | childcb (struct ev_signal *sw, int revents) |
|
|
422 | { |
|
|
423 | struct ev_child *w; |
|
|
424 | int pid, status; |
|
|
425 | |
|
|
426 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
427 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
428 | if (w->pid == pid || w->pid == -1) |
|
|
429 | { |
|
|
430 | w->status = status; |
|
|
431 | event ((W)w, EV_CHILD); |
|
|
432 | } |
|
|
433 | } |
|
|
434 | |
|
|
435 | /*****************************************************************************/ |
|
|
436 | |
334 | #if HAVE_EPOLL |
437 | #if EV_USE_EPOLL |
335 | # include "ev_epoll.c" |
438 | # include "ev_epoll.c" |
336 | #endif |
439 | #endif |
337 | #if HAVE_SELECT |
440 | #if EV_USE_SELECT |
338 | # include "ev_select.c" |
441 | # include "ev_select.c" |
339 | #endif |
442 | #endif |
340 | |
443 | |
|
|
444 | int |
|
|
445 | ev_version_major (void) |
|
|
446 | { |
|
|
447 | return EV_VERSION_MAJOR; |
|
|
448 | } |
|
|
449 | |
|
|
450 | int |
|
|
451 | ev_version_minor (void) |
|
|
452 | { |
|
|
453 | return EV_VERSION_MINOR; |
|
|
454 | } |
|
|
455 | |
341 | int ev_init (int flags) |
456 | int ev_init (int flags) |
342 | { |
457 | { |
343 | #if HAVE_MONOTONIC |
|
|
344 | { |
|
|
345 | struct timespec ts; |
|
|
346 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
347 | have_monotonic = 1; |
|
|
348 | } |
|
|
349 | #endif |
|
|
350 | |
|
|
351 | ev_now = ev_time (); |
|
|
352 | now = get_clock (); |
|
|
353 | diff = ev_now - now; |
|
|
354 | |
|
|
355 | if (pipe (sigpipe)) |
|
|
356 | return 0; |
|
|
357 | |
|
|
358 | ev_method = EVMETHOD_NONE; |
|
|
359 | #if HAVE_EPOLL |
|
|
360 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
361 | #endif |
|
|
362 | #if HAVE_SELECT |
|
|
363 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
364 | #endif |
|
|
365 | |
|
|
366 | if (ev_method) |
458 | if (!ev_method) |
|
|
459 | { |
|
|
460 | #if EV_USE_MONOTONIC |
367 | { |
461 | { |
|
|
462 | struct timespec ts; |
|
|
463 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
|
|
464 | have_monotonic = 1; |
|
|
465 | } |
|
|
466 | #endif |
|
|
467 | |
|
|
468 | ev_now = ev_time (); |
|
|
469 | now = get_clock (); |
|
|
470 | diff = ev_now - now; |
|
|
471 | |
|
|
472 | if (pipe (sigpipe)) |
|
|
473 | return 0; |
|
|
474 | |
|
|
475 | ev_method = EVMETHOD_NONE; |
|
|
476 | #if EV_USE_EPOLL |
|
|
477 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
|
|
478 | #endif |
|
|
479 | #if EV_USE_SELECT |
|
|
480 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
|
|
481 | #endif |
|
|
482 | |
|
|
483 | if (ev_method) |
|
|
484 | { |
368 | evw_init (&sigev, sigcb); |
485 | ev_watcher_init (&sigev, sigcb); |
369 | siginit (); |
486 | siginit (); |
|
|
487 | |
|
|
488 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
489 | ev_signal_start (&childev); |
|
|
490 | } |
370 | } |
491 | } |
371 | |
492 | |
372 | return ev_method; |
493 | return ev_method; |
373 | } |
494 | } |
374 | |
495 | |
375 | /*****************************************************************************/ |
496 | /*****************************************************************************/ |
376 | |
497 | |
377 | void ev_prefork (void) |
498 | void |
|
|
499 | ev_fork_prepare (void) |
378 | { |
500 | { |
379 | /* nop */ |
501 | /* nop */ |
380 | } |
502 | } |
381 | |
503 | |
|
|
504 | void |
382 | void ev_postfork_parent (void) |
505 | ev_fork_parent (void) |
383 | { |
506 | { |
384 | /* nop */ |
507 | /* nop */ |
385 | } |
508 | } |
386 | |
509 | |
|
|
510 | void |
387 | void ev_postfork_child (void) |
511 | ev_fork_child (void) |
388 | { |
512 | { |
389 | #if HAVE_EPOLL |
513 | #if EV_USE_EPOLL |
390 | if (ev_method == EVMETHOD_EPOLL) |
514 | if (ev_method == EVMETHOD_EPOLL) |
391 | epoll_postfork_child (); |
515 | epoll_postfork_child (); |
392 | #endif |
516 | #endif |
393 | |
517 | |
394 | evio_stop (&sigev); |
518 | ev_io_stop (&sigev); |
395 | close (sigpipe [0]); |
519 | close (sigpipe [0]); |
396 | close (sigpipe [1]); |
520 | close (sigpipe [1]); |
397 | pipe (sigpipe); |
521 | pipe (sigpipe); |
398 | siginit (); |
522 | siginit (); |
399 | } |
523 | } |
400 | |
524 | |
401 | /*****************************************************************************/ |
525 | /*****************************************************************************/ |
402 | |
526 | |
403 | static void |
527 | static void |
404 | fd_reify (void) |
|
|
405 | { |
|
|
406 | int i; |
|
|
407 | |
|
|
408 | for (i = 0; i < fdchangecnt; ++i) |
|
|
409 | { |
|
|
410 | int fd = fdchanges [i]; |
|
|
411 | ANFD *anfd = anfds + fd; |
|
|
412 | struct ev_io *w; |
|
|
413 | |
|
|
414 | int wev = 0; |
|
|
415 | |
|
|
416 | for (w = anfd->head; w; w = w->next) |
|
|
417 | wev |= w->events; |
|
|
418 | |
|
|
419 | if (anfd->wev != wev) |
|
|
420 | { |
|
|
421 | method_modify (fd, anfd->wev, wev); |
|
|
422 | anfd->wev = wev; |
|
|
423 | } |
|
|
424 | } |
|
|
425 | |
|
|
426 | fdchangecnt = 0; |
|
|
427 | } |
|
|
428 | |
|
|
429 | static void |
|
|
430 | call_pending () |
528 | call_pending (void) |
431 | { |
529 | { |
432 | while (pendingcnt) |
530 | while (pendingcnt) |
433 | { |
531 | { |
434 | ANPENDING *p = pendings + --pendingcnt; |
532 | ANPENDING *p = pendings + --pendingcnt; |
435 | |
533 | |
… | |
… | |
440 | } |
538 | } |
441 | } |
539 | } |
442 | } |
540 | } |
443 | |
541 | |
444 | static void |
542 | static void |
445 | timers_reify () |
543 | timers_reify (void) |
446 | { |
544 | { |
447 | while (timercnt && timers [0]->at <= now) |
545 | while (timercnt && timers [0]->at <= now) |
448 | { |
546 | { |
449 | struct ev_timer *w = timers [0]; |
547 | struct ev_timer *w = timers [0]; |
450 | |
|
|
451 | event ((W)w, EV_TIMEOUT); |
|
|
452 | |
548 | |
453 | /* first reschedule or stop timer */ |
549 | /* first reschedule or stop timer */ |
454 | if (w->repeat) |
550 | if (w->repeat) |
455 | { |
551 | { |
|
|
552 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
456 | w->at = now + w->repeat; |
553 | w->at = now + w->repeat; |
457 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
458 | downheap ((WT *)timers, timercnt, 0); |
554 | downheap ((WT *)timers, timercnt, 0); |
459 | } |
555 | } |
460 | else |
556 | else |
461 | evtimer_stop (w); /* nonrepeating: stop timer */ |
557 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
462 | } |
|
|
463 | } |
|
|
464 | |
558 | |
|
|
559 | event ((W)w, EV_TIMEOUT); |
|
|
560 | } |
|
|
561 | } |
|
|
562 | |
465 | static void |
563 | static void |
466 | periodics_reify () |
564 | periodics_reify (void) |
467 | { |
565 | { |
468 | while (periodiccnt && periodics [0]->at <= ev_now) |
566 | while (periodiccnt && periodics [0]->at <= ev_now) |
469 | { |
567 | { |
470 | struct ev_periodic *w = periodics [0]; |
568 | struct ev_periodic *w = periodics [0]; |
471 | |
569 | |
472 | /* first reschedule or stop timer */ |
570 | /* first reschedule or stop timer */ |
473 | if (w->interval) |
571 | if (w->interval) |
474 | { |
572 | { |
475 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
573 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
476 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
574 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
477 | downheap ((WT *)periodics, periodiccnt, 0); |
575 | downheap ((WT *)periodics, periodiccnt, 0); |
478 | } |
576 | } |
479 | else |
577 | else |
480 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
578 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
481 | |
579 | |
482 | event ((W)w, EV_TIMEOUT); |
580 | event ((W)w, EV_PERIODIC); |
483 | } |
581 | } |
484 | } |
582 | } |
485 | |
583 | |
486 | static void |
584 | static void |
487 | periodics_reschedule (ev_tstamp diff) |
585 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
497 | { |
595 | { |
498 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
596 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
499 | |
597 | |
500 | if (fabs (diff) >= 1e-4) |
598 | if (fabs (diff) >= 1e-4) |
501 | { |
599 | { |
502 | evperiodic_stop (w); |
600 | ev_periodic_stop (w); |
503 | evperiodic_start (w); |
601 | ev_periodic_start (w); |
504 | |
602 | |
505 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
603 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
506 | } |
604 | } |
507 | } |
605 | } |
508 | } |
606 | } |
509 | } |
607 | } |
510 | |
608 | |
511 | static void |
609 | static void |
512 | time_update () |
610 | time_update (void) |
513 | { |
611 | { |
514 | int i; |
612 | int i; |
515 | |
613 | |
516 | ev_now = ev_time (); |
614 | ev_now = ev_time (); |
517 | |
615 | |
… | |
… | |
551 | int ev_loop_done; |
649 | int ev_loop_done; |
552 | |
650 | |
553 | void ev_loop (int flags) |
651 | void ev_loop (int flags) |
554 | { |
652 | { |
555 | double block; |
653 | double block; |
556 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
654 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
557 | |
|
|
558 | if (checkcnt) |
|
|
559 | { |
|
|
560 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
561 | call_pending (); |
|
|
562 | } |
|
|
563 | |
655 | |
564 | do |
656 | do |
565 | { |
657 | { |
|
|
658 | /* queue check watchers (and execute them) */ |
|
|
659 | if (preparecnt) |
|
|
660 | { |
|
|
661 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
662 | call_pending (); |
|
|
663 | } |
|
|
664 | |
566 | /* update fd-related kernel structures */ |
665 | /* update fd-related kernel structures */ |
567 | fd_reify (); |
666 | fd_reify (); |
568 | |
667 | |
569 | /* calculate blocking time */ |
668 | /* calculate blocking time */ |
570 | |
669 | |
571 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
670 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
671 | always have timers, we just calculate it always */ |
572 | ev_now = ev_time (); |
672 | ev_now = ev_time (); |
573 | |
673 | |
574 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
674 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
575 | block = 0.; |
675 | block = 0.; |
576 | else |
676 | else |
… | |
… | |
596 | |
696 | |
597 | /* update ev_now, do magic */ |
697 | /* update ev_now, do magic */ |
598 | time_update (); |
698 | time_update (); |
599 | |
699 | |
600 | /* queue pending timers and reschedule them */ |
700 | /* queue pending timers and reschedule them */ |
|
|
701 | timers_reify (); /* relative timers called last */ |
601 | periodics_reify (); /* absolute timers first */ |
702 | periodics_reify (); /* absolute timers called first */ |
602 | timers_reify (); /* relative timers second */ |
|
|
603 | |
703 | |
604 | /* queue idle watchers unless io or timers are pending */ |
704 | /* queue idle watchers unless io or timers are pending */ |
605 | if (!pendingcnt) |
705 | if (!pendingcnt) |
606 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
706 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
607 | |
707 | |
608 | /* queue check and possibly idle watchers */ |
708 | /* queue check watchers, to be executed first */ |
|
|
709 | if (checkcnt) |
609 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
710 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
610 | |
711 | |
611 | call_pending (); |
712 | call_pending (); |
612 | } |
713 | } |
613 | while (!ev_loop_done); |
714 | while (!ev_loop_done); |
614 | |
715 | |
… | |
… | |
639 | head = &(*head)->next; |
740 | head = &(*head)->next; |
640 | } |
741 | } |
641 | } |
742 | } |
642 | |
743 | |
643 | static void |
744 | static void |
644 | ev_clear (W w) |
745 | ev_clear_pending (W w) |
645 | { |
746 | { |
646 | if (w->pending) |
747 | if (w->pending) |
647 | { |
748 | { |
648 | pendings [w->pending - 1].w = 0; |
749 | pendings [w->pending - 1].w = 0; |
649 | w->pending = 0; |
750 | w->pending = 0; |
… | |
… | |
663 | } |
764 | } |
664 | |
765 | |
665 | /*****************************************************************************/ |
766 | /*****************************************************************************/ |
666 | |
767 | |
667 | void |
768 | void |
668 | evio_start (struct ev_io *w) |
769 | ev_io_start (struct ev_io *w) |
669 | { |
770 | { |
670 | if (ev_is_active (w)) |
|
|
671 | return; |
|
|
672 | |
|
|
673 | int fd = w->fd; |
771 | int fd = w->fd; |
|
|
772 | |
|
|
773 | if (ev_is_active (w)) |
|
|
774 | return; |
|
|
775 | |
|
|
776 | assert (("ev_io_start called with negative fd", fd >= 0)); |
674 | |
777 | |
675 | ev_start ((W)w, 1); |
778 | ev_start ((W)w, 1); |
676 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
779 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
677 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
780 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
678 | |
781 | |
679 | ++fdchangecnt; |
782 | fd_change (fd); |
680 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
681 | fdchanges [fdchangecnt - 1] = fd; |
|
|
682 | } |
783 | } |
683 | |
784 | |
684 | void |
785 | void |
685 | evio_stop (struct ev_io *w) |
786 | ev_io_stop (struct ev_io *w) |
686 | { |
787 | { |
687 | ev_clear ((W)w); |
788 | ev_clear_pending ((W)w); |
688 | if (!ev_is_active (w)) |
789 | if (!ev_is_active (w)) |
689 | return; |
790 | return; |
690 | |
791 | |
691 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
792 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
692 | ev_stop ((W)w); |
793 | ev_stop ((W)w); |
693 | |
794 | |
694 | ++fdchangecnt; |
795 | fd_change (w->fd); |
695 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
696 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
697 | } |
796 | } |
698 | |
797 | |
699 | void |
798 | void |
700 | evtimer_start (struct ev_timer *w) |
799 | ev_timer_start (struct ev_timer *w) |
701 | { |
800 | { |
702 | if (ev_is_active (w)) |
801 | if (ev_is_active (w)) |
703 | return; |
802 | return; |
704 | |
803 | |
705 | w->at += now; |
804 | w->at += now; |
706 | |
805 | |
707 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
806 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
708 | |
807 | |
709 | ev_start ((W)w, ++timercnt); |
808 | ev_start ((W)w, ++timercnt); |
710 | array_needsize (timers, timermax, timercnt, ); |
809 | array_needsize (timers, timermax, timercnt, ); |
711 | timers [timercnt - 1] = w; |
810 | timers [timercnt - 1] = w; |
712 | upheap ((WT *)timers, timercnt - 1); |
811 | upheap ((WT *)timers, timercnt - 1); |
713 | } |
812 | } |
714 | |
813 | |
715 | void |
814 | void |
716 | evtimer_stop (struct ev_timer *w) |
815 | ev_timer_stop (struct ev_timer *w) |
717 | { |
816 | { |
718 | ev_clear ((W)w); |
817 | ev_clear_pending ((W)w); |
719 | if (!ev_is_active (w)) |
818 | if (!ev_is_active (w)) |
720 | return; |
819 | return; |
721 | |
820 | |
722 | if (w->active < timercnt--) |
821 | if (w->active < timercnt--) |
723 | { |
822 | { |
… | |
… | |
729 | |
828 | |
730 | ev_stop ((W)w); |
829 | ev_stop ((W)w); |
731 | } |
830 | } |
732 | |
831 | |
733 | void |
832 | void |
734 | evtimer_again (struct ev_timer *w) |
833 | ev_timer_again (struct ev_timer *w) |
735 | { |
834 | { |
736 | if (ev_is_active (w)) |
835 | if (ev_is_active (w)) |
737 | { |
836 | { |
738 | if (w->repeat) |
837 | if (w->repeat) |
739 | { |
838 | { |
740 | w->at = now + w->repeat; |
839 | w->at = now + w->repeat; |
741 | downheap ((WT *)timers, timercnt, w->active - 1); |
840 | downheap ((WT *)timers, timercnt, w->active - 1); |
742 | } |
841 | } |
743 | else |
842 | else |
744 | evtimer_stop (w); |
843 | ev_timer_stop (w); |
745 | } |
844 | } |
746 | else if (w->repeat) |
845 | else if (w->repeat) |
747 | evtimer_start (w); |
846 | ev_timer_start (w); |
748 | } |
847 | } |
749 | |
848 | |
750 | void |
849 | void |
751 | evperiodic_start (struct ev_periodic *w) |
850 | ev_periodic_start (struct ev_periodic *w) |
752 | { |
851 | { |
753 | if (ev_is_active (w)) |
852 | if (ev_is_active (w)) |
754 | return; |
853 | return; |
755 | |
854 | |
756 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
855 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
757 | |
856 | |
758 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
857 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
759 | if (w->interval) |
858 | if (w->interval) |
760 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
859 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
761 | |
860 | |
… | |
… | |
764 | periodics [periodiccnt - 1] = w; |
863 | periodics [periodiccnt - 1] = w; |
765 | upheap ((WT *)periodics, periodiccnt - 1); |
864 | upheap ((WT *)periodics, periodiccnt - 1); |
766 | } |
865 | } |
767 | |
866 | |
768 | void |
867 | void |
769 | evperiodic_stop (struct ev_periodic *w) |
868 | ev_periodic_stop (struct ev_periodic *w) |
770 | { |
869 | { |
771 | ev_clear ((W)w); |
870 | ev_clear_pending ((W)w); |
772 | if (!ev_is_active (w)) |
871 | if (!ev_is_active (w)) |
773 | return; |
872 | return; |
774 | |
873 | |
775 | if (w->active < periodiccnt--) |
874 | if (w->active < periodiccnt--) |
776 | { |
875 | { |
… | |
… | |
780 | |
879 | |
781 | ev_stop ((W)w); |
880 | ev_stop ((W)w); |
782 | } |
881 | } |
783 | |
882 | |
784 | void |
883 | void |
785 | evsignal_start (struct ev_signal *w) |
884 | ev_signal_start (struct ev_signal *w) |
786 | { |
885 | { |
787 | if (ev_is_active (w)) |
886 | if (ev_is_active (w)) |
788 | return; |
887 | return; |
|
|
888 | |
|
|
889 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
789 | |
890 | |
790 | ev_start ((W)w, 1); |
891 | ev_start ((W)w, 1); |
791 | array_needsize (signals, signalmax, w->signum, signals_init); |
892 | array_needsize (signals, signalmax, w->signum, signals_init); |
792 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
893 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
793 | |
894 | |
… | |
… | |
800 | sigaction (w->signum, &sa, 0); |
901 | sigaction (w->signum, &sa, 0); |
801 | } |
902 | } |
802 | } |
903 | } |
803 | |
904 | |
804 | void |
905 | void |
805 | evsignal_stop (struct ev_signal *w) |
906 | ev_signal_stop (struct ev_signal *w) |
806 | { |
907 | { |
807 | ev_clear ((W)w); |
908 | ev_clear_pending ((W)w); |
808 | if (!ev_is_active (w)) |
909 | if (!ev_is_active (w)) |
809 | return; |
910 | return; |
810 | |
911 | |
811 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
912 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
812 | ev_stop ((W)w); |
913 | ev_stop ((W)w); |
813 | |
914 | |
814 | if (!signals [w->signum - 1].head) |
915 | if (!signals [w->signum - 1].head) |
815 | signal (w->signum, SIG_DFL); |
916 | signal (w->signum, SIG_DFL); |
816 | } |
917 | } |
817 | |
918 | |
|
|
919 | void |
818 | void evidle_start (struct ev_idle *w) |
920 | ev_idle_start (struct ev_idle *w) |
819 | { |
921 | { |
820 | if (ev_is_active (w)) |
922 | if (ev_is_active (w)) |
821 | return; |
923 | return; |
822 | |
924 | |
823 | ev_start ((W)w, ++idlecnt); |
925 | ev_start ((W)w, ++idlecnt); |
824 | array_needsize (idles, idlemax, idlecnt, ); |
926 | array_needsize (idles, idlemax, idlecnt, ); |
825 | idles [idlecnt - 1] = w; |
927 | idles [idlecnt - 1] = w; |
826 | } |
928 | } |
827 | |
929 | |
|
|
930 | void |
828 | void evidle_stop (struct ev_idle *w) |
931 | ev_idle_stop (struct ev_idle *w) |
829 | { |
932 | { |
830 | ev_clear ((W)w); |
933 | ev_clear_pending ((W)w); |
831 | if (ev_is_active (w)) |
934 | if (ev_is_active (w)) |
832 | return; |
935 | return; |
833 | |
936 | |
834 | idles [w->active - 1] = idles [--idlecnt]; |
937 | idles [w->active - 1] = idles [--idlecnt]; |
835 | ev_stop ((W)w); |
938 | ev_stop ((W)w); |
836 | } |
939 | } |
837 | |
940 | |
|
|
941 | void |
|
|
942 | ev_prepare_start (struct ev_prepare *w) |
|
|
943 | { |
|
|
944 | if (ev_is_active (w)) |
|
|
945 | return; |
|
|
946 | |
|
|
947 | ev_start ((W)w, ++preparecnt); |
|
|
948 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
949 | prepares [preparecnt - 1] = w; |
|
|
950 | } |
|
|
951 | |
|
|
952 | void |
|
|
953 | ev_prepare_stop (struct ev_prepare *w) |
|
|
954 | { |
|
|
955 | ev_clear_pending ((W)w); |
|
|
956 | if (ev_is_active (w)) |
|
|
957 | return; |
|
|
958 | |
|
|
959 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
960 | ev_stop ((W)w); |
|
|
961 | } |
|
|
962 | |
|
|
963 | void |
838 | void evcheck_start (struct ev_check *w) |
964 | ev_check_start (struct ev_check *w) |
839 | { |
965 | { |
840 | if (ev_is_active (w)) |
966 | if (ev_is_active (w)) |
841 | return; |
967 | return; |
842 | |
968 | |
843 | ev_start ((W)w, ++checkcnt); |
969 | ev_start ((W)w, ++checkcnt); |
844 | array_needsize (checks, checkmax, checkcnt, ); |
970 | array_needsize (checks, checkmax, checkcnt, ); |
845 | checks [checkcnt - 1] = w; |
971 | checks [checkcnt - 1] = w; |
846 | } |
972 | } |
847 | |
973 | |
|
|
974 | void |
848 | void evcheck_stop (struct ev_check *w) |
975 | ev_check_stop (struct ev_check *w) |
849 | { |
976 | { |
850 | ev_clear ((W)w); |
977 | ev_clear_pending ((W)w); |
851 | if (ev_is_active (w)) |
978 | if (ev_is_active (w)) |
852 | return; |
979 | return; |
853 | |
980 | |
854 | checks [w->active - 1] = checks [--checkcnt]; |
981 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
982 | ev_stop ((W)w); |
|
|
983 | } |
|
|
984 | |
|
|
985 | void |
|
|
986 | ev_child_start (struct ev_child *w) |
|
|
987 | { |
|
|
988 | if (ev_is_active (w)) |
|
|
989 | return; |
|
|
990 | |
|
|
991 | ev_start ((W)w, 1); |
|
|
992 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
993 | } |
|
|
994 | |
|
|
995 | void |
|
|
996 | ev_child_stop (struct ev_child *w) |
|
|
997 | { |
|
|
998 | ev_clear_pending ((W)w); |
|
|
999 | if (ev_is_active (w)) |
|
|
1000 | return; |
|
|
1001 | |
|
|
1002 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
855 | ev_stop ((W)w); |
1003 | ev_stop ((W)w); |
856 | } |
1004 | } |
857 | |
1005 | |
858 | /*****************************************************************************/ |
1006 | /*****************************************************************************/ |
859 | |
1007 | |
… | |
… | |
869 | once_cb (struct ev_once *once, int revents) |
1017 | once_cb (struct ev_once *once, int revents) |
870 | { |
1018 | { |
871 | void (*cb)(int revents, void *arg) = once->cb; |
1019 | void (*cb)(int revents, void *arg) = once->cb; |
872 | void *arg = once->arg; |
1020 | void *arg = once->arg; |
873 | |
1021 | |
874 | evio_stop (&once->io); |
1022 | ev_io_stop (&once->io); |
875 | evtimer_stop (&once->to); |
1023 | ev_timer_stop (&once->to); |
876 | free (once); |
1024 | free (once); |
877 | |
1025 | |
878 | cb (revents, arg); |
1026 | cb (revents, arg); |
879 | } |
1027 | } |
880 | |
1028 | |
… | |
… | |
894 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1042 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
895 | { |
1043 | { |
896 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1044 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
897 | |
1045 | |
898 | if (!once) |
1046 | if (!once) |
899 | cb (EV_ERROR, arg); |
1047 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
900 | else |
1048 | else |
901 | { |
1049 | { |
902 | once->cb = cb; |
1050 | once->cb = cb; |
903 | once->arg = arg; |
1051 | once->arg = arg; |
904 | |
1052 | |
905 | evw_init (&once->io, once_cb_io); |
1053 | ev_watcher_init (&once->io, once_cb_io); |
906 | |
|
|
907 | if (fd >= 0) |
1054 | if (fd >= 0) |
908 | { |
1055 | { |
909 | evio_set (&once->io, fd, events); |
1056 | ev_io_set (&once->io, fd, events); |
910 | evio_start (&once->io); |
1057 | ev_io_start (&once->io); |
911 | } |
1058 | } |
912 | |
1059 | |
913 | evw_init (&once->to, once_cb_to); |
1060 | ev_watcher_init (&once->to, once_cb_to); |
914 | |
|
|
915 | if (timeout >= 0.) |
1061 | if (timeout >= 0.) |
916 | { |
1062 | { |
917 | evtimer_set (&once->to, timeout, 0.); |
1063 | ev_timer_set (&once->to, timeout, 0.); |
918 | evtimer_start (&once->to); |
1064 | ev_timer_start (&once->to); |
919 | } |
1065 | } |
920 | } |
1066 | } |
921 | } |
1067 | } |
922 | |
1068 | |
923 | /*****************************************************************************/ |
1069 | /*****************************************************************************/ |
… | |
… | |
934 | |
1080 | |
935 | static void |
1081 | static void |
936 | ocb (struct ev_timer *w, int revents) |
1082 | ocb (struct ev_timer *w, int revents) |
937 | { |
1083 | { |
938 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1084 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
939 | evtimer_stop (w); |
1085 | ev_timer_stop (w); |
940 | evtimer_start (w); |
1086 | ev_timer_start (w); |
941 | } |
1087 | } |
942 | |
1088 | |
943 | static void |
1089 | static void |
944 | scb (struct ev_signal *w, int revents) |
1090 | scb (struct ev_signal *w, int revents) |
945 | { |
1091 | { |
946 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1092 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
947 | evio_stop (&wio); |
1093 | ev_io_stop (&wio); |
948 | evio_start (&wio); |
1094 | ev_io_start (&wio); |
949 | } |
1095 | } |
950 | |
1096 | |
951 | static void |
1097 | static void |
952 | gcb (struct ev_signal *w, int revents) |
1098 | gcb (struct ev_signal *w, int revents) |
953 | { |
1099 | { |
… | |
… | |
957 | |
1103 | |
958 | int main (void) |
1104 | int main (void) |
959 | { |
1105 | { |
960 | ev_init (0); |
1106 | ev_init (0); |
961 | |
1107 | |
962 | evio_init (&wio, sin_cb, 0, EV_READ); |
1108 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
963 | evio_start (&wio); |
1109 | ev_io_start (&wio); |
964 | |
1110 | |
965 | struct ev_timer t[10000]; |
1111 | struct ev_timer t[10000]; |
966 | |
1112 | |
967 | #if 0 |
1113 | #if 0 |
968 | int i; |
1114 | int i; |
969 | for (i = 0; i < 10000; ++i) |
1115 | for (i = 0; i < 10000; ++i) |
970 | { |
1116 | { |
971 | struct ev_timer *w = t + i; |
1117 | struct ev_timer *w = t + i; |
972 | evw_init (w, ocb, i); |
1118 | ev_watcher_init (w, ocb, i); |
973 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1119 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
974 | evtimer_start (w); |
1120 | ev_timer_start (w); |
975 | if (drand48 () < 0.5) |
1121 | if (drand48 () < 0.5) |
976 | evtimer_stop (w); |
1122 | ev_timer_stop (w); |
977 | } |
1123 | } |
978 | #endif |
1124 | #endif |
979 | |
1125 | |
980 | struct ev_timer t1; |
1126 | struct ev_timer t1; |
981 | evtimer_init (&t1, ocb, 5, 10); |
1127 | ev_timer_init (&t1, ocb, 5, 10); |
982 | evtimer_start (&t1); |
1128 | ev_timer_start (&t1); |
983 | |
1129 | |
984 | struct ev_signal sig; |
1130 | struct ev_signal sig; |
985 | evsignal_init (&sig, scb, SIGQUIT); |
1131 | ev_signal_init (&sig, scb, SIGQUIT); |
986 | evsignal_start (&sig); |
1132 | ev_signal_start (&sig); |
987 | |
1133 | |
988 | struct ev_check cw; |
1134 | struct ev_check cw; |
989 | evcheck_init (&cw, gcb); |
1135 | ev_check_init (&cw, gcb); |
990 | evcheck_start (&cw); |
1136 | ev_check_start (&cw); |
991 | |
1137 | |
992 | struct ev_idle iw; |
1138 | struct ev_idle iw; |
993 | evidle_init (&iw, gcb); |
1139 | ev_idle_init (&iw, gcb); |
994 | evidle_start (&iw); |
1140 | ev_idle_start (&iw); |
995 | |
1141 | |
996 | ev_loop (0); |
1142 | ev_loop (0); |
997 | |
1143 | |
998 | return 0; |
1144 | return 0; |
999 | } |
1145 | } |