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