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