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