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