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

Comparing libev/ev.c (file contents):
Revision 1.1 by root, Tue Oct 30 20:59:31 2007 UTC vs.
Revision 1.21 by root, Wed Oct 31 18:37:38 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
66} 110}
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; \ 115 int newcnt = cur ? cur << 1 : 16; \
72 do \
73 { \
74 newcnt += (newcnt >> 1) + 16; \
75 } \
76 while ((cnt) > newcnt); \
77 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\
78 base = realloc (base, sizeof (*base) * (newcnt)); \ 116 base = realloc (base, sizeof (*base) * (newcnt)); \
79 init (base + cur, newcnt - cur); \ 117 init (base + cur, newcnt - cur); \
80 cur = newcnt; \ 118 cur = newcnt; \
81 } 119 }
82 120
121/*****************************************************************************/
122
83typedef struct 123typedef struct
84{ 124{
85 struct ev_io *head; 125 struct ev_io *head;
86 unsigned char wev, rev; /* want, received event set */ 126 unsigned char wev, rev; /* want, received event set */
87} ANFD; 127} ANFD;
103 } 143 }
104} 144}
105 145
106typedef struct 146typedef struct
107{ 147{
108 struct ev_watcher *w; 148 W w;
109 int events; 149 int events;
110} ANPENDING; 150} ANPENDING;
111 151
112static ANPENDING *pendings; 152static ANPENDING *pendings;
113static int pendingmax, pendingcnt; 153static int pendingmax, pendingcnt;
114 154
115static void 155static void
116event (struct ev_watcher *w, int events) 156event (W w, int events)
117{ 157{
158 if (w->active)
159 {
118 w->pending = ++pendingcnt; 160 w->pending = ++pendingcnt;
119 array_needsize (pendings, pendingmax, pendingcnt, ); 161 array_needsize (pendings, pendingmax, pendingcnt, );
120 pendings [pendingcnt - 1].w = w; 162 pendings [pendingcnt - 1].w = w;
121 pendings [pendingcnt - 1].events = events; 163 pendings [pendingcnt - 1].events = events;
164 }
122} 165}
123 166
124static void 167static void
125fd_event (int fd, int events) 168fd_event (int fd, int events)
126{ 169{
130 for (w = anfd->head; w; w = w->next) 173 for (w = anfd->head; w; w = w->next)
131 { 174 {
132 int ev = w->events & events; 175 int ev = w->events & events;
133 176
134 if (ev) 177 if (ev)
135 event ((struct ev_watcher *)w, ev); 178 event ((W)w, ev);
136 } 179 }
137} 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/*****************************************************************************/
138 205
139static struct ev_timer **timers; 206static struct ev_timer **timers;
140static int timermax, timercnt; 207static int timermax, timercnt;
141 208
209static struct ev_periodic **periodics;
210static int periodicmax, periodiccnt;
211
142static void 212static void
143upheap (int k) 213upheap (WT *timers, int k)
144{ 214{
145 struct ev_timer *w = timers [k]; 215 WT w = timers [k];
146 216
147 while (k && timers [k >> 1]->at > w->at) 217 while (k && timers [k >> 1]->at > w->at)
148 { 218 {
149 timers [k] = timers [k >> 1]; 219 timers [k] = timers [k >> 1];
150 timers [k]->active = k + 1; 220 timers [k]->active = k + 1;
155 timers [k]->active = k + 1; 225 timers [k]->active = k + 1;
156 226
157} 227}
158 228
159static void 229static void
160downheap (int k) 230downheap (WT *timers, int N, int k)
161{ 231{
162 struct ev_timer *w = timers [k]; 232 WT w = timers [k];
163 233
164 while (k <= (timercnt >> 1)) 234 while (k < (N >> 1))
165 { 235 {
166 int j = k << 1; 236 int j = k << 1;
167 237
168 if (j + 1 < timercnt && timers [j]->at > timers [j + 1]->at) 238 if (j + 1 < N && timers [j]->at > timers [j + 1]->at)
169 ++j; 239 ++j;
170 240
171 if (w->at <= timers [j]->at) 241 if (w->at <= timers [j]->at)
172 break; 242 break;
173 243
174 timers [k] = timers [j]; 244 timers [k] = timers [j];
175 timers [k]->active = k; 245 timers [k]->active = k + 1;
176 k = j; 246 k = j;
177 } 247 }
178 248
179 timers [k] = w; 249 timers [k] = w;
180 timers [k]->active = k + 1; 250 timers [k]->active = k + 1;
181} 251}
182 252
183static 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;
184static int signalmax, signalcnt; 262static int signalmax;
185 263
264static int sigpipe [2];
265static sig_atomic_t gotsig;
266static struct ev_io sigev;
267
186static void 268static void
187signals_init (struct ev_signal **base, int count) 269signals_init (ANSIG *base, int count)
188{ 270{
189 while (count--) 271 while (count--)
190 *base++ = 0; 272 {
273 base->head = 0;
274 base->gotsig = 0;
275 ++base;
276 }
191} 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/*****************************************************************************/
192 336
193#if HAVE_EPOLL 337#if HAVE_EPOLL
194# include "ev_epoll.c" 338# include "ev_epoll.c"
195#endif 339#endif
196#if HAVE_SELECT 340#if HAVE_SELECT
206 have_monotonic = 1; 350 have_monotonic = 1;
207 } 351 }
208#endif 352#endif
209 353
210 ev_now = ev_time (); 354 ev_now = ev_time ();
355 now = get_clock ();
356 diff = ev_now - now;
211 357
358 if (pipe (sigpipe))
359 return 0;
360
361 ev_method = EVMETHOD_NONE;
212#if HAVE_EPOLL 362#if HAVE_EPOLL
213 if (epoll_init (flags)) 363 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
214 return ev_method;
215#endif 364#endif
216#if HAVE_SELECT 365#if HAVE_SELECT
217 if (select_init (flags)) 366 if (ev_method == EVMETHOD_NONE) select_init (flags);
218 return ev_method;
219#endif 367#endif
220 368
221 ev_method = EVMETHOD_NONE; 369 if (ev_method)
370 {
371 evw_init (&sigev, sigcb);
372 siginit ();
373 }
374
222 return ev_method; 375 return ev_method;
223} 376}
224 377
378/*****************************************************************************/
379
225void ev_prefork (void) 380void ev_prefork (void)
226{ 381{
382 /* nop */
227} 383}
228 384
229void ev_postfork_parent (void) 385void ev_postfork_parent (void)
230{ 386{
387 /* nop */
231} 388}
232 389
233void ev_postfork_child (void) 390void ev_postfork_child (void)
234{ 391{
235#if HAVE_EPOLL 392#if HAVE_EPOLL
393 if (ev_method == EVMETHOD_EPOLL)
236 epoll_postfork_child (); 394 epoll_postfork_child ();
237#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;
238} 430}
239 431
240static void 432static void
241call_pending () 433call_pending ()
242{ 434{
243 int i; 435 while (pendingcnt)
244
245 for (i = 0; i < pendingcnt; ++i)
246 { 436 {
247 ANPENDING *p = pendings + i; 437 ANPENDING *p = pendings + --pendingcnt;
248 438
249 if (p->w) 439 if (p->w)
250 { 440 {
251 p->w->pending = 0; 441 p->w->pending = 0;
252 p->w->cb (p->w, p->events); 442 p->w->cb (p->w, p->events);
253 } 443 }
254 } 444 }
255
256 pendingcnt = 0;
257} 445}
258 446
259static void 447static void
260timer_reify (void) 448timers_reify ()
261{ 449{
262 while (timercnt && timers [0]->at <= ev_now) 450 while (timercnt && timers [0]->at <= now)
263 { 451 {
264 struct ev_timer *w = timers [0]; 452 struct ev_timer *w = timers [0];
265 453
454 event ((W)w, EV_TIMEOUT);
455
266 /* first reschedule timer */ 456 /* first reschedule or stop timer */
267 if (w->repeat) 457 if (w->repeat)
268 { 458 {
269 fprintf (stderr, "a %f now %f repeat %f, %f\n", w->at, ev_now, w->repeat, w->repeat *1e30);//D
270 if (w->is_abs)
271 w->at += floor ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
272 else
273 w->at = ev_now + w->repeat; 459 w->at = now + w->repeat;
274 460 assert (("timer timeout in the past, negative repeat?", w->at > now));
275 fprintf (stderr, "b %f\n", w->at);//D 461 downheap ((WT *)timers, timercnt, 0);
276
277 downheap (0);
278 } 462 }
279 else 463 else
280 evtimer_stop (w); /* nonrepeating: stop timer */ 464 evtimer_stop (w); /* nonrepeating: stop timer */
465 }
466}
281 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
282 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;
283 } 551 }
284} 552}
285 553
286int ev_loop_done; 554int ev_loop_done;
287 555
288int ev_loop (int flags) 556void ev_loop (int flags)
289{ 557{
290 double block; 558 double block;
291 ev_loop_done = flags & EVLOOP_ONESHOT; 559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
292 560
293 do 561 do
294 { 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
295 /* update fd-related kernel structures */ 570 /* update fd-related kernel structures */
296 method_reify (); fdchangecnt = 0; 571 fd_reify ();
297 572
298 /* 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 */
299 ev_now = ev_time (); 577 ev_now = ev_time ();
300 578
301 if (flags & EVLOOP_NONBLOCK) 579 if (flags & EVLOOP_NONBLOCK || idlecnt)
302 block = 0.; 580 block = 0.;
303 else if (!timercnt)
304 block = MAX_BLOCKTIME;
305 else 581 else
306 { 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 {
307 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
308 if (block < 0.) block = 0.; 597 if (block < 0.) block = 0.;
309 else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME;
310 } 598 }
311 599
312 fprintf (stderr, "block %f\n", block);//D
313 method_poll (block); 600 method_poll (block);
314 601
602 /* update ev_now, do magic */
603 time_update ();
604
315 /* put pending timers into pendign queue and reschedule them */ 605 /* queue pending timers and reschedule them */
316 timer_reify (); 606 timers_reify (); /* relative timers called last */
607 periodics_reify (); /* absolute timers called first */
317 608
318 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
319 call_pending (); 617 call_pending ();
320 } 618 }
321 while (!ev_loop_done); 619 while (!ev_loop_done);
322}
323 620
621 if (ev_loop_done != 2)
622 ev_loop_done = 0;
623}
624
625/*****************************************************************************/
626
324static void 627static void
325wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 628wlist_add (WL *head, WL elem)
326{ 629{
327 elem->next = *head; 630 elem->next = *head;
328 *head = elem; 631 *head = elem;
329} 632}
330 633
331static void 634static void
332wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 635wlist_del (WL *head, WL elem)
333{ 636{
334 while (*head) 637 while (*head)
335 { 638 {
336 if (*head == elem) 639 if (*head == elem)
337 { 640 {
342 head = &(*head)->next; 645 head = &(*head)->next;
343 } 646 }
344} 647}
345 648
346static void 649static void
347ev_start (struct ev_watcher *w, int active) 650ev_clear (W w)
348{ 651{
652 if (w->pending)
653 {
654 pendings [w->pending - 1].w = 0;
349 w->pending = 0; 655 w->pending = 0;
656 }
657}
658
659static void
660ev_start (W w, int active)
661{
350 w->active = active; 662 w->active = active;
351} 663}
352 664
353static void 665static void
354ev_stop (struct ev_watcher *w) 666ev_stop (W w)
355{ 667{
356 if (w->pending)
357 pendings [w->pending - 1].w = 0;
358
359 w->active = 0; 668 w->active = 0;
360 /* nop */
361} 669}
670
671/*****************************************************************************/
362 672
363void 673void
364evio_start (struct ev_io *w) 674evio_start (struct ev_io *w)
365{ 675{
366 if (ev_is_active (w)) 676 if (ev_is_active (w))
367 return; 677 return;
368 678
369 int fd = w->fd; 679 int fd = w->fd;
370 680
371 ev_start ((struct ev_watcher *)w, 1); 681 ev_start ((W)w, 1);
372 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 682 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
373 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 683 wlist_add ((WL *)&anfds[fd].head, (WL)w);
374 684
375 ++fdchangecnt; 685 ++fdchangecnt;
376 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 686 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
377 fdchanges [fdchangecnt - 1] = fd; 687 fdchanges [fdchangecnt - 1] = fd;
378} 688}
379 689
380void 690void
381evio_stop (struct ev_io *w) 691evio_stop (struct ev_io *w)
382{ 692{
693 ev_clear ((W)w);
383 if (!ev_is_active (w)) 694 if (!ev_is_active (w))
384 return; 695 return;
385 696
386 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);
387 ev_stop ((struct ev_watcher *)w); 698 ev_stop ((W)w);
388 699
389 ++fdchangecnt; 700 ++fdchangecnt;
390 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 701 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
391 fdchanges [fdchangecnt - 1] = w->fd; 702 fdchanges [fdchangecnt - 1] = w->fd;
392} 703}
395evtimer_start (struct ev_timer *w) 706evtimer_start (struct ev_timer *w)
396{ 707{
397 if (ev_is_active (w)) 708 if (ev_is_active (w))
398 return; 709 return;
399 710
400 fprintf (stderr, "t1 %f a %d\n", w->at, w->is_abs);//D
401 if (w->is_abs)
402 {
403 if (w->repeat)
404 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
405 }
406 else
407 w->at += ev_now; 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
416void 721void
417evtimer_stop (struct ev_timer *w) 722evtimer_stop (struct ev_timer *w)
418{ 723{
724 ev_clear ((W)w);
419 if (!ev_is_active (w)) 725 if (!ev_is_active (w))
420 return; 726 return;
421 727
728 if (w->active < timercnt--)
729 {
422 timers [w->active - 1] = timers [--timercnt]; 730 timers [w->active - 1] = timers [timercnt];
423 downheap (w->active - 1); 731 downheap ((WT *)timers, timercnt, w->active - 1);
424 ev_stop ((struct ev_watcher *)w); 732 }
733
734 w->at = w->repeat;
735
736 ev_stop ((W)w);
737}
738
739void
740evtimer_again (struct ev_timer *w)
741{
742 if (ev_is_active (w))
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}
755
756void
757evperiodic_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
774void
775evperiodic_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);
425} 788}
426 789
427void 790void
428evsignal_start (struct ev_signal *w) 791evsignal_start (struct ev_signal *w)
429{ 792{
430 if (ev_is_active (w)) 793 if (ev_is_active (w))
431 return; 794 return;
432 795
433 ev_start ((struct ev_watcher *)w, 1); 796 ev_start ((W)w, 1);
434 array_needsize (signals, signalmax, w->signum, signals_init); 797 array_needsize (signals, signalmax, w->signum, signals_init);
435 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 }
436} 808}
437 809
438void 810void
439evsignal_stop (struct ev_signal *w) 811evsignal_stop (struct ev_signal *w)
440{ 812{
813 ev_clear ((W)w);
441 if (!ev_is_active (w)) 814 if (!ev_is_active (w))
442 return; 815 return;
443 816
444 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);
445 ev_stop ((struct ev_watcher *)w); 818 ev_stop ((W)w);
446}
447 819
820 if (!signals [w->signum - 1].head)
821 signal (w->signum, SIG_DFL);
822}
823
824void 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
834void 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
844void 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
854void 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
864void 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
874void 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
448/*****************************************************************************/ 884/*****************************************************************************/
885
886struct 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
894static void
895once_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
907static void
908once_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
913static void
914once_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
919void
920ev_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
449#if 1 951#if 0
952
953struct ev_io wio;
450 954
451static void 955static void
452sin_cb (struct ev_io *w, int revents) 956sin_cb (struct ev_io *w, int revents)
453{ 957{
454 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 958 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
455} 959}
456 960
457static void 961static void
458ocb (struct ev_timer *w, int revents) 962ocb (struct ev_timer *w, int revents)
459{ 963{
460 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
969static void
970scb (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
977static void
978gcb (struct ev_signal *w, int revents)
979{
980 fprintf (stderr, "generic %x\n", revents);
981
461} 982}
462 983
463int main (void) 984int main (void)
464{ 985{
465 struct ev_io sin;
466
467 ev_init (0); 986 ev_init (0);
468 987
469 evw_init (&sin, sin_cb, 55);
470 evio_set (&sin, 0, EV_READ); 988 evio_init (&wio, sin_cb, 0, EV_READ);
471 evio_start (&sin); 989 evio_start (&wio);
990
991 struct ev_timer t[10000];
992
993#if 0
994 int i;
995 for (i = 0; i < 10000; ++i)
996 {
997 struct ev_timer *w = t + i;
998 evw_init (w, ocb, i);
999 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
1000 evtimer_start (w);
1001 if (drand48 () < 0.5)
1002 evtimer_stop (w);
1003 }
1004#endif
472 1005
473 struct ev_timer t1; 1006 struct ev_timer t1;
474 evw_init (&t1, ocb, 1); 1007 evtimer_init (&t1, ocb, 5, 10);
475 evtimer_set_rel (&t1, 1, 0);
476 evtimer_start (&t1); 1008 evtimer_start (&t1);
477 1009
478 struct ev_timer t2; 1010 struct ev_signal sig;
479 evw_init (&t2, ocb, 2); 1011 evsignal_init (&sig, scb, SIGQUIT);
480 evtimer_set_abs (&t2, ev_time () + 2, 0); 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);
481 evtimer_start (&t2); 1020 evidle_start (&iw);
482 1021
483 ev_loop (0); 1022 ev_loop (0);
484 1023
485 return 0; 1024 return 0;
486} 1025}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines