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