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