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