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