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