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