ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.10 by root, Wed Oct 31 07:36:03 2007 UTC vs.
Revision 1.44 by root, Fri Nov 2 20:59:14 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines