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