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