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