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