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