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