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

Comparing libev/ev.c (file contents):
Revision 1.3 by root, Tue Oct 30 21:45:00 2007 UTC vs.
Revision 1.20 by root, Wed Oct 31 18:28:00 2007 UTC

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
22struct ev_watcher { 67typedef struct ev_watcher *W;
23 EV_WATCHER (ev_watcher);
24};
25
26struct ev_watcher_list { 68typedef struct ev_watcher_list *WL;
27 EV_WATCHER_LIST (ev_watcher_list); 69typedef struct ev_watcher_time *WT;
28};
29 70
71static ev_tstamp now, diff; /* monotonic clock */
30ev_tstamp ev_now; 72ev_tstamp ev_now;
31int ev_method; 73int ev_method;
32 74
33static int have_monotonic; /* runtime */ 75static int have_monotonic; /* runtime */
34 76
35static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 77static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
36static void (*method_reify)(void); 78static void (*method_modify)(int fd, int oev, int nev);
37static void (*method_poll)(ev_tstamp timeout); 79static void (*method_poll)(ev_tstamp timeout);
80
81/*****************************************************************************/
38 82
39ev_tstamp 83ev_tstamp
40ev_time (void) 84ev_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
78typedef struct 123typedef 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
101typedef struct 146typedef struct
102{ 147{
103 struct ev_watcher *w; 148 W w;
104 int events; 149 int events;
105} ANPENDING; 150} ANPENDING;
106 151
107static ANPENDING *pendings; 152static ANPENDING *pendings;
108static int pendingmax, pendingcnt; 153static int pendingmax, pendingcnt;
109 154
110static void 155static void
111event (struct ev_watcher *w, int events) 156event (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
119static void 167static void
120fd_event (int fd, int events) 168fd_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
182static void
183queue_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 */
192static void
193fd_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
134static struct ev_timer **timers; 206static struct ev_timer **timers;
135static int timermax, timercnt; 207static int timermax, timercnt;
136 208
209static struct ev_periodic **periodics;
210static int periodicmax, periodiccnt;
211
137static void 212static void
138upheap (int k) 213upheap (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
154static void 229static void
155downheap (int k) 230downheap (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
178static struct ev_signal **signals; 253/*****************************************************************************/
254
255typedef struct
256{
257 struct ev_signal *head;
258 sig_atomic_t gotsig;
259} ANSIG;
260
261static ANSIG *signals;
179static int signalmax, signalcnt; 262static int signalmax;
180 263
264static int sigpipe [2];
265static sig_atomic_t gotsig;
266static struct ev_io sigev;
267
181static void 268static void
182signals_init (struct ev_signal **base, int count) 269signals_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
279static void
280sighandler (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
291static void
292sigcb (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
310static void
311siginit (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
326static struct ev_idle **idles;
327static int idlemax, idlecnt;
328
329static struct ev_prepare **prepares;
330static int preparemax, preparecnt;
331
332static struct ev_check **checks;
333static 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
220void ev_prefork (void) 380void ev_prefork (void)
221{ 381{
382 /* nop */
222} 383}
223 384
224void ev_postfork_parent (void) 385void ev_postfork_parent (void)
225{ 386{
387 /* nop */
226} 388}
227 389
228void ev_postfork_child (void) 390void 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
406static void
407fd_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
235static void 432static void
236call_pending () 433call_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
254static void 447static void
255timer_reify (void) 448timers_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
454 event ((W)w, EV_TIMEOUT);
455
261 /* first reschedule timer */ 456 /* first reschedule or stop timer */
262 if (w->repeat) 457 if (w->repeat)
263 { 458 {
264 if (w->is_abs)
265 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
266 else
267 w->at = ev_now + w->repeat; 459 w->at = now + w->repeat;
268 460 assert (("timer timeout in the past, negative repeat?", w->at > now));
269 downheap (0); 461 downheap ((WT *)timers, timercnt, 0);
270 } 462 }
271 else 463 else
272 evtimer_stop (w); /* nonrepeating: stop timer */ 464 evtimer_stop (w); /* nonrepeating: stop timer */
465 }
466}
273 467
468static void
469periodics_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)
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 */
484
274 event ((struct ev_watcher *)w, EV_TIMEOUT); 485 event ((W)w, EV_TIMEOUT);
486 }
487}
488
489static void
490periodics_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
514static void
515time_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;
275 } 551 }
276} 552}
277 553
278int ev_loop_done; 554int ev_loop_done;
279 555
280int ev_loop (int flags) 556void ev_loop (int flags)
281{ 557{
282 double block; 558 double block;
283 ev_loop_done = flags & EVLOOP_ONESHOT; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
284 560
285 do 561 do
286 { 562 {
563 /* queue check watchers (and execute them) */
564 if (checkcnt)
565 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending ();
568 }
569
287 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
288 method_reify (); fdchangecnt = 0; 571 fd_reify ();
289 572
290 /* calculate blocking time */ 573 /* calculate blocking time */
574
575 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
291 ev_now = ev_time (); 576 ev_now = ev_time ();
292 577
293 if (flags & EVLOOP_NONBLOCK) 578 if (flags & EVLOOP_NONBLOCK || idlecnt)
294 block = 0.; 579 block = 0.;
295 else if (!timercnt)
296 block = MAX_BLOCKTIME;
297 else 580 else
298 { 581 {
582 block = MAX_BLOCKTIME;
583
584 if (timercnt)
585 {
586 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
587 if (block > to) block = to;
588 }
589
590 if (periodiccnt)
591 {
299 block = timers [0]->at - ev_now + method_fudge; 592 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
593 if (block > to) block = to;
594 }
595
300 if (block < 0.) block = 0.; 596 if (block < 0.) block = 0.;
301 else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME;
302 } 597 }
303 598
304 method_poll (block); 599 method_poll (block);
305 600
601 /* update ev_now, do magic */
602 time_update ();
603
306 /* put pending timers into pendign queue and reschedule them */ 604 /* queue pending timers and reschedule them */
307 timer_reify (); 605 timers_reify (); /* relative timers called last */
606 periodics_reify (); /* absolute timers called first */
308 607
309 ev_now = ev_time (); 608 /* queue idle watchers unless io or timers are pending */
609 if (!pendingcnt)
610 queue_events ((W *)idles, idlecnt, EV_IDLE);
611
612 /* queue check watchers, to be executed first */
613 if (checkcnt)
614 queue_events ((W *)checks, checkcnt, EV_CHECK);
615
310 call_pending (); 616 call_pending ();
311 } 617 }
312 while (!ev_loop_done); 618 while (!ev_loop_done);
313}
314 619
620 if (ev_loop_done != 2)
621 ev_loop_done = 0;
622}
623
624/*****************************************************************************/
625
315static void 626static void
316wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 627wlist_add (WL *head, WL elem)
317{ 628{
318 elem->next = *head; 629 elem->next = *head;
319 *head = elem; 630 *head = elem;
320} 631}
321 632
322static void 633static void
323wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 634wlist_del (WL *head, WL elem)
324{ 635{
325 while (*head) 636 while (*head)
326 { 637 {
327 if (*head == elem) 638 if (*head == elem)
328 { 639 {
333 head = &(*head)->next; 644 head = &(*head)->next;
334 } 645 }
335} 646}
336 647
337static void 648static void
338ev_start (struct ev_watcher *w, int active) 649ev_clear (W w)
339{ 650{
651 if (w->pending)
652 {
653 pendings [w->pending - 1].w = 0;
340 w->pending = 0; 654 w->pending = 0;
655 }
656}
657
658static void
659ev_start (W w, int active)
660{
341 w->active = active; 661 w->active = active;
342} 662}
343 663
344static void 664static void
345ev_stop (struct ev_watcher *w) 665ev_stop (W w)
346{ 666{
347 if (w->pending)
348 pendings [w->pending - 1].w = 0;
349
350 w->active = 0; 667 w->active = 0;
351 /* nop */
352} 668}
669
670/*****************************************************************************/
353 671
354void 672void
355evio_start (struct ev_io *w) 673evio_start (struct ev_io *w)
356{ 674{
357 if (ev_is_active (w)) 675 if (ev_is_active (w))
358 return; 676 return;
359 677
360 int fd = w->fd; 678 int fd = w->fd;
361 679
362 ev_start ((struct ev_watcher *)w, 1); 680 ev_start ((W)w, 1);
363 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 681 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
364 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 682 wlist_add ((WL *)&anfds[fd].head, (WL)w);
365 683
366 ++fdchangecnt; 684 ++fdchangecnt;
367 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 685 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
368 fdchanges [fdchangecnt - 1] = fd; 686 fdchanges [fdchangecnt - 1] = fd;
369} 687}
370 688
371void 689void
372evio_stop (struct ev_io *w) 690evio_stop (struct ev_io *w)
373{ 691{
692 ev_clear ((W)w);
374 if (!ev_is_active (w)) 693 if (!ev_is_active (w))
375 return; 694 return;
376 695
377 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
378 ev_stop ((struct ev_watcher *)w); 697 ev_stop ((W)w);
379 698
380 ++fdchangecnt; 699 ++fdchangecnt;
381 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 700 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
382 fdchanges [fdchangecnt - 1] = w->fd; 701 fdchanges [fdchangecnt - 1] = w->fd;
383} 702}
386evtimer_start (struct ev_timer *w) 705evtimer_start (struct ev_timer *w)
387{ 706{
388 if (ev_is_active (w)) 707 if (ev_is_active (w))
389 return; 708 return;
390 709
391 if (w->is_abs)
392 {
393 /* this formula differs from the one in timer_reify becuse we do not round up */
394 if (w->repeat)
395 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
396 }
397 else
398 w->at += ev_now; 710 w->at += now;
399 711
400 ev_start ((struct ev_watcher *)w, ++timercnt); 712 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
713
714 ev_start ((W)w, ++timercnt);
401 array_needsize (timers, timermax, timercnt, ); 715 array_needsize (timers, timermax, timercnt, );
402 timers [timercnt - 1] = w; 716 timers [timercnt - 1] = w;
403 upheap (timercnt - 1); 717 upheap ((WT *)timers, timercnt - 1);
404} 718}
405 719
406void 720void
407evtimer_stop (struct ev_timer *w) 721evtimer_stop (struct ev_timer *w)
408{ 722{
723 ev_clear ((W)w);
409 if (!ev_is_active (w)) 724 if (!ev_is_active (w))
410 return; 725 return;
411 726
412 if (w->active < timercnt--) 727 if (w->active < timercnt--)
413 { 728 {
414 timers [w->active - 1] = timers [timercnt]; 729 timers [w->active - 1] = timers [timercnt];
415 downheap (w->active - 1); 730 downheap ((WT *)timers, timercnt, w->active - 1);
731 }
732
733 w->at = w->repeat;
734
735 ev_stop ((W)w);
736}
737
738void
739evtimer_again (struct ev_timer *w)
740{
741 if (ev_is_active (w))
416 } 742 {
743 if (w->repeat)
744 {
745 w->at = now + w->repeat;
746 downheap ((WT *)timers, timercnt, w->active - 1);
747 }
748 else
749 evtimer_stop (w);
750 }
751 else if (w->repeat)
752 evtimer_start (w);
753}
417 754
418 ev_stop ((struct ev_watcher *)w); 755void
756evperiodic_start (struct ev_periodic *w)
757{
758 if (ev_is_active (w))
759 return;
760
761 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
762
763 /* this formula differs from the one in periodic_reify because we do not always round up */
764 if (w->interval)
765 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
766
767 ev_start ((W)w, ++periodiccnt);
768 array_needsize (periodics, periodicmax, periodiccnt, );
769 periodics [periodiccnt - 1] = w;
770 upheap ((WT *)periodics, periodiccnt - 1);
771}
772
773void
774evperiodic_stop (struct ev_periodic *w)
775{
776 ev_clear ((W)w);
777 if (!ev_is_active (w))
778 return;
779
780 if (w->active < periodiccnt--)
781 {
782 periodics [w->active - 1] = periodics [periodiccnt];
783 downheap ((WT *)periodics, periodiccnt, w->active - 1);
784 }
785
786 ev_stop ((W)w);
419} 787}
420 788
421void 789void
422evsignal_start (struct ev_signal *w) 790evsignal_start (struct ev_signal *w)
423{ 791{
424 if (ev_is_active (w)) 792 if (ev_is_active (w))
425 return; 793 return;
426 794
427 ev_start ((struct ev_watcher *)w, 1); 795 ev_start ((W)w, 1);
428 array_needsize (signals, signalmax, w->signum, signals_init); 796 array_needsize (signals, signalmax, w->signum, signals_init);
429 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 797 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
798
799 if (!w->next)
800 {
801 struct sigaction sa;
802 sa.sa_handler = sighandler;
803 sigfillset (&sa.sa_mask);
804 sa.sa_flags = 0;
805 sigaction (w->signum, &sa, 0);
806 }
430} 807}
431 808
432void 809void
433evsignal_stop (struct ev_signal *w) 810evsignal_stop (struct ev_signal *w)
434{ 811{
812 ev_clear ((W)w);
435 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
436 return; 814 return;
437 815
438 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 816 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
439 ev_stop ((struct ev_watcher *)w); 817 ev_stop ((W)w);
440}
441 818
819 if (!signals [w->signum - 1].head)
820 signal (w->signum, SIG_DFL);
821}
822
823void evidle_start (struct ev_idle *w)
824{
825 if (ev_is_active (w))
826 return;
827
828 ev_start ((W)w, ++idlecnt);
829 array_needsize (idles, idlemax, idlecnt, );
830 idles [idlecnt - 1] = w;
831}
832
833void evidle_stop (struct ev_idle *w)
834{
835 ev_clear ((W)w);
836 if (ev_is_active (w))
837 return;
838
839 idles [w->active - 1] = idles [--idlecnt];
840 ev_stop ((W)w);
841}
842
843void evprepare_start (struct ev_prepare *w)
844{
845 if (ev_is_active (w))
846 return;
847
848 ev_start ((W)w, ++preparecnt);
849 array_needsize (prepares, preparemax, preparecnt, );
850 prepares [preparecnt - 1] = w;
851}
852
853void evprepare_stop (struct ev_prepare *w)
854{
855 ev_clear ((W)w);
856 if (ev_is_active (w))
857 return;
858
859 prepares [w->active - 1] = prepares [--preparecnt];
860 ev_stop ((W)w);
861}
862
863void evcheck_start (struct ev_check *w)
864{
865 if (ev_is_active (w))
866 return;
867
868 ev_start ((W)w, ++checkcnt);
869 array_needsize (checks, checkmax, checkcnt, );
870 checks [checkcnt - 1] = w;
871}
872
873void evcheck_stop (struct ev_check *w)
874{
875 ev_clear ((W)w);
876 if (ev_is_active (w))
877 return;
878
879 checks [w->active - 1] = checks [--checkcnt];
880 ev_stop ((W)w);
881}
882
442/*****************************************************************************/ 883/*****************************************************************************/
884
885struct ev_once
886{
887 struct ev_io io;
888 struct ev_timer to;
889 void (*cb)(int revents, void *arg);
890 void *arg;
891};
892
893static void
894once_cb (struct ev_once *once, int revents)
895{
896 void (*cb)(int revents, void *arg) = once->cb;
897 void *arg = once->arg;
898
899 evio_stop (&once->io);
900 evtimer_stop (&once->to);
901 free (once);
902
903 cb (revents, arg);
904}
905
906static void
907once_cb_io (struct ev_io *w, int revents)
908{
909 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
910}
911
912static void
913once_cb_to (struct ev_timer *w, int revents)
914{
915 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
916}
917
918void
919ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
920{
921 struct ev_once *once = malloc (sizeof (struct ev_once));
922
923 if (!once)
924 cb (EV_ERROR, arg);
925 else
926 {
927 once->cb = cb;
928 once->arg = arg;
929
930 evw_init (&once->io, once_cb_io);
931
932 if (fd >= 0)
933 {
934 evio_set (&once->io, fd, events);
935 evio_start (&once->io);
936 }
937
938 evw_init (&once->to, once_cb_to);
939
940 if (timeout >= 0.)
941 {
942 evtimer_set (&once->to, timeout, 0.);
943 evtimer_start (&once->to);
944 }
945 }
946}
947
948/*****************************************************************************/
949
443#if 1 950#if 0
951
952struct ev_io wio;
444 953
445static void 954static void
446sin_cb (struct ev_io *w, int revents) 955sin_cb (struct ev_io *w, int revents)
447{ 956{
448 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 957 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
449} 958}
450 959
451static void 960static void
452ocb (struct ev_timer *w, int revents) 961ocb (struct ev_timer *w, int revents)
453{ 962{
454 fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 963 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
964 evtimer_stop (w);
965 evtimer_start (w);
966}
967
968static void
969scb (struct ev_signal *w, int revents)
970{
971 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
972 evio_stop (&wio);
973 evio_start (&wio);
974}
975
976static void
977gcb (struct ev_signal *w, int revents)
978{
979 fprintf (stderr, "generic %x\n", revents);
980
455} 981}
456 982
457int main (void) 983int main (void)
458{ 984{
459 struct ev_io sin;
460
461 ev_init (0); 985 ev_init (0);
462 986
463 evw_init (&sin, sin_cb, 55);
464 evio_set (&sin, 0, EV_READ); 987 evio_init (&wio, sin_cb, 0, EV_READ);
465 evio_start (&sin); 988 evio_start (&wio);
466 989
467 struct ev_timer t[1000]; 990 struct ev_timer t[10000];
468 991
992#if 0
469 int i; 993 int i;
470 for (i = 0; i < 1000; ++i) 994 for (i = 0; i < 10000; ++i)
471 { 995 {
472 struct ev_timer *w = t + i; 996 struct ev_timer *w = t + i;
473 evw_init (w, ocb, i); 997 evw_init (w, ocb, i);
474 evtimer_set_rel (w, drand48 (), 0); 998 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
475 evtimer_start (w); 999 evtimer_start (w);
476 if (drand48 () < 0.5) 1000 if (drand48 () < 0.5)
477 evtimer_stop (w); 1001 evtimer_stop (w);
478 } 1002 }
1003#endif
1004
1005 struct ev_timer t1;
1006 evtimer_init (&t1, ocb, 5, 10);
1007 evtimer_start (&t1);
1008
1009 struct ev_signal sig;
1010 evsignal_init (&sig, scb, SIGQUIT);
1011 evsignal_start (&sig);
1012
1013 struct ev_check cw;
1014 evcheck_init (&cw, gcb);
1015 evcheck_start (&cw);
1016
1017 struct ev_idle iw;
1018 evidle_init (&iw, gcb);
1019 evidle_start (&iw);
479 1020
480 ev_loop (0); 1021 ev_loop (0);
481 1022
482 return 0; 1023 return 0;
483} 1024}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines