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

Comparing libev/ev.c (file contents):
Revision 1.2 by root, Tue Oct 30 21:42:12 2007 UTC vs.
Revision 1.19 by root, Wed Oct 31 17:55:55 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_check **checks;
330static int checkmax, checkcnt;
331
332/*****************************************************************************/
187 333
188#if HAVE_EPOLL 334#if HAVE_EPOLL
189# include "ev_epoll.c" 335# include "ev_epoll.c"
190#endif 336#endif
191#if HAVE_SELECT 337#if HAVE_SELECT
201 have_monotonic = 1; 347 have_monotonic = 1;
202 } 348 }
203#endif 349#endif
204 350
205 ev_now = ev_time (); 351 ev_now = ev_time ();
352 now = get_clock ();
353 diff = ev_now - now;
206 354
355 if (pipe (sigpipe))
356 return 0;
357
358 ev_method = EVMETHOD_NONE;
207#if HAVE_EPOLL 359#if HAVE_EPOLL
208 if (epoll_init (flags)) 360 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
209 return ev_method;
210#endif 361#endif
211#if HAVE_SELECT 362#if HAVE_SELECT
212 if (select_init (flags)) 363 if (ev_method == EVMETHOD_NONE) select_init (flags);
213 return ev_method;
214#endif 364#endif
215 365
216 ev_method = EVMETHOD_NONE; 366 if (ev_method)
367 {
368 evw_init (&sigev, sigcb);
369 siginit ();
370 }
371
217 return ev_method; 372 return ev_method;
218} 373}
219 374
375/*****************************************************************************/
376
220void ev_prefork (void) 377void ev_prefork (void)
221{ 378{
379 /* nop */
222} 380}
223 381
224void ev_postfork_parent (void) 382void ev_postfork_parent (void)
225{ 383{
384 /* nop */
226} 385}
227 386
228void ev_postfork_child (void) 387void ev_postfork_child (void)
229{ 388{
230#if HAVE_EPOLL 389#if HAVE_EPOLL
390 if (ev_method == EVMETHOD_EPOLL)
231 epoll_postfork_child (); 391 epoll_postfork_child ();
232#endif 392#endif
393
394 evio_stop (&sigev);
395 close (sigpipe [0]);
396 close (sigpipe [1]);
397 pipe (sigpipe);
398 siginit ();
399}
400
401/*****************************************************************************/
402
403static void
404fd_reify (void)
405{
406 int i;
407
408 for (i = 0; i < fdchangecnt; ++i)
409 {
410 int fd = fdchanges [i];
411 ANFD *anfd = anfds + fd;
412 struct ev_io *w;
413
414 int wev = 0;
415
416 for (w = anfd->head; w; w = w->next)
417 wev |= w->events;
418
419 if (anfd->wev != wev)
420 {
421 method_modify (fd, anfd->wev, wev);
422 anfd->wev = wev;
423 }
424 }
425
426 fdchangecnt = 0;
233} 427}
234 428
235static void 429static void
236call_pending () 430call_pending ()
237{ 431{
238 int i; 432 while (pendingcnt)
239
240 for (i = 0; i < pendingcnt; ++i)
241 { 433 {
242 ANPENDING *p = pendings + i; 434 ANPENDING *p = pendings + --pendingcnt;
243 435
244 if (p->w) 436 if (p->w)
245 { 437 {
246 p->w->pending = 0; 438 p->w->pending = 0;
247 p->w->cb (p->w, p->events); 439 p->w->cb (p->w, p->events);
248 } 440 }
249 } 441 }
250
251 pendingcnt = 0;
252} 442}
253 443
254static void 444static void
255timer_reify (void) 445timers_reify ()
256{ 446{
257 while (timercnt && timers [0]->at <= ev_now) 447 while (timercnt && timers [0]->at <= now)
258 { 448 {
259 struct ev_timer *w = timers [0]; 449 struct ev_timer *w = timers [0];
260 450
261 fprintf (stderr, "0 %f, %d c%d\n", w->at, w->active, timercnt);//D 451 event ((W)w, EV_TIMEOUT);
452
262 /* first reschedule timer */ 453 /* first reschedule or stop timer */
263 if (w->repeat) 454 if (w->repeat)
264 { 455 {
265 fprintf (stderr, "a %f now %f repeat %f, %f\n", w->at, ev_now, w->repeat, w->repeat *1e30);//D
266 if (w->is_abs)
267 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
268 else
269 w->at = ev_now + w->repeat; 456 w->at = now + w->repeat;
270 457 assert (("timer timeout in the past, negative repeat?", w->at > now));
271 fprintf (stderr, "b %f\n", w->at);//D 458 downheap ((WT *)timers, timercnt, 0);
272
273 downheap (0);
274 } 459 }
275 else 460 else
276 {
277 fprintf (stderr, "c %f, %d c%d\n", w->at, w->active, timercnt);//D
278 evtimer_stop (w); /* nonrepeating: stop timer */ 461 evtimer_stop (w); /* nonrepeating: stop timer */
462 }
463}
464
465static void
466periodics_reify ()
467{
468 while (periodiccnt && periodics [0]->at <= ev_now)
469 {
470 struct ev_periodic *w = periodics [0];
471
472 /* first reschedule or stop timer */
473 if (w->interval)
279 } 474 {
475 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
476 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
477 downheap ((WT *)periodics, periodiccnt, 0);
478 }
479 else
480 evperiodic_stop (w); /* nonrepeating: stop timer */
280 481
281 event ((struct ev_watcher *)w, EV_TIMEOUT); 482 event ((W)w, EV_TIMEOUT);
483 }
484}
485
486static void
487periodics_reschedule (ev_tstamp diff)
488{
489 int i;
490
491 /* adjust periodics after time jump */
492 for (i = 0; i < periodiccnt; ++i)
493 {
494 struct ev_periodic *w = periodics [i];
495
496 if (w->interval)
497 {
498 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
499
500 if (fabs (diff) >= 1e-4)
501 {
502 evperiodic_stop (w);
503 evperiodic_start (w);
504
505 i = 0; /* restart loop, inefficient, but time jumps should be rare */
506 }
507 }
508 }
509}
510
511static void
512time_update ()
513{
514 int i;
515
516 ev_now = ev_time ();
517
518 if (have_monotonic)
519 {
520 ev_tstamp odiff = diff;
521
522 for (i = 4; --i; ) /* loop a few times, before making important decisions */
523 {
524 now = get_clock ();
525 diff = ev_now - now;
526
527 if (fabs (odiff - diff) < MIN_TIMEJUMP)
528 return; /* all is well */
529
530 ev_now = ev_time ();
531 }
532
533 periodics_reschedule (diff - odiff);
534 /* no timer adjustment, as the monotonic clock doesn't jump */
535 }
536 else
537 {
538 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
539 {
540 periodics_reschedule (ev_now - now);
541
542 /* adjust timers. this is easy, as the offset is the same for all */
543 for (i = 0; i < timercnt; ++i)
544 timers [i]->at += diff;
545 }
546
547 now = ev_now;
282 } 548 }
283} 549}
284 550
285int ev_loop_done; 551int ev_loop_done;
286 552
287int ev_loop (int flags) 553void ev_loop (int flags)
288{ 554{
289 double block; 555 double block;
290 ev_loop_done = flags & EVLOOP_ONESHOT; 556 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
557
558 if (checkcnt)
559 {
560 queue_events ((W *)checks, checkcnt, EV_CHECK);
561 call_pending ();
562 }
291 563
292 do 564 do
293 { 565 {
294 /* update fd-related kernel structures */ 566 /* update fd-related kernel structures */
295 method_reify (); fdchangecnt = 0; 567 fd_reify ();
296 568
297 /* calculate blocking time */ 569 /* calculate blocking time */
570
571 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
298 ev_now = ev_time (); 572 ev_now = ev_time ();
299 573
300 if (flags & EVLOOP_NONBLOCK) 574 if (flags & EVLOOP_NONBLOCK || idlecnt)
301 block = 0.; 575 block = 0.;
302 else if (!timercnt)
303 block = MAX_BLOCKTIME;
304 else 576 else
305 { 577 {
578 block = MAX_BLOCKTIME;
579
580 if (timercnt)
581 {
582 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
583 if (block > to) block = to;
584 }
585
586 if (periodiccnt)
587 {
306 block = timers [0]->at - ev_now + method_fudge; 588 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
589 if (block > to) block = to;
590 }
591
307 if (block < 0.) block = 0.; 592 if (block < 0.) block = 0.;
308 else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME;
309 } 593 }
310 594
311 fprintf (stderr, "block %f\n", block);//D
312 method_poll (block); 595 method_poll (block);
313 596
597 /* update ev_now, do magic */
598 time_update ();
599
314 /* put pending timers into pendign queue and reschedule them */ 600 /* queue pending timers and reschedule them */
315 timer_reify (); 601 periodics_reify (); /* absolute timers first */
602 timers_reify (); /* relative timers second */
316 603
317 ev_now = ev_time (); 604 /* queue idle watchers unless io or timers are pending */
605 if (!pendingcnt)
606 queue_events ((W *)idles, idlecnt, EV_IDLE);
607
608 /* queue check and possibly idle watchers */
609 queue_events ((W *)checks, checkcnt, EV_CHECK);
610
318 call_pending (); 611 call_pending ();
319 } 612 }
320 while (!ev_loop_done); 613 while (!ev_loop_done);
321}
322 614
615 if (ev_loop_done != 2)
616 ev_loop_done = 0;
617}
618
619/*****************************************************************************/
620
323static void 621static void
324wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 622wlist_add (WL *head, WL elem)
325{ 623{
326 elem->next = *head; 624 elem->next = *head;
327 *head = elem; 625 *head = elem;
328} 626}
329 627
330static void 628static void
331wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 629wlist_del (WL *head, WL elem)
332{ 630{
333 while (*head) 631 while (*head)
334 { 632 {
335 if (*head == elem) 633 if (*head == elem)
336 { 634 {
341 head = &(*head)->next; 639 head = &(*head)->next;
342 } 640 }
343} 641}
344 642
345static void 643static void
346ev_start (struct ev_watcher *w, int active) 644ev_clear (W w)
347{ 645{
646 if (w->pending)
647 {
648 pendings [w->pending - 1].w = 0;
348 w->pending = 0; 649 w->pending = 0;
650 }
651}
652
653static void
654ev_start (W w, int active)
655{
349 w->active = active; 656 w->active = active;
350} 657}
351 658
352static void 659static void
353ev_stop (struct ev_watcher *w) 660ev_stop (W w)
354{ 661{
355 if (w->pending)
356 pendings [w->pending - 1].w = 0;
357
358 w->active = 0; 662 w->active = 0;
359 /* nop */
360} 663}
664
665/*****************************************************************************/
361 666
362void 667void
363evio_start (struct ev_io *w) 668evio_start (struct ev_io *w)
364{ 669{
365 if (ev_is_active (w)) 670 if (ev_is_active (w))
366 return; 671 return;
367 672
368 int fd = w->fd; 673 int fd = w->fd;
369 674
370 ev_start ((struct ev_watcher *)w, 1); 675 ev_start ((W)w, 1);
371 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 676 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
372 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 677 wlist_add ((WL *)&anfds[fd].head, (WL)w);
373 678
374 ++fdchangecnt; 679 ++fdchangecnt;
375 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 680 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
376 fdchanges [fdchangecnt - 1] = fd; 681 fdchanges [fdchangecnt - 1] = fd;
377} 682}
378 683
379void 684void
380evio_stop (struct ev_io *w) 685evio_stop (struct ev_io *w)
381{ 686{
687 ev_clear ((W)w);
382 if (!ev_is_active (w)) 688 if (!ev_is_active (w))
383 return; 689 return;
384 690
385 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 691 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
386 ev_stop ((struct ev_watcher *)w); 692 ev_stop ((W)w);
387 693
388 ++fdchangecnt; 694 ++fdchangecnt;
389 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 695 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
390 fdchanges [fdchangecnt - 1] = w->fd; 696 fdchanges [fdchangecnt - 1] = w->fd;
391} 697}
394evtimer_start (struct ev_timer *w) 700evtimer_start (struct ev_timer *w)
395{ 701{
396 if (ev_is_active (w)) 702 if (ev_is_active (w))
397 return; 703 return;
398 704
399 fprintf (stderr, "t1 %f a %d\n", w->at, w->is_abs);//D
400 if (w->is_abs)
401 {
402 /* this formula differs from the one in timer_reify becuse we do not round up */
403 if (w->repeat)
404 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
405 }
406 else
407 w->at += ev_now; 705 w->at += now;
408 fprintf (stderr, "t2 %f a %d\n", w->at, w->is_abs);//D
409 706
410 ev_start ((struct ev_watcher *)w, ++timercnt); 707 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
708
709 ev_start ((W)w, ++timercnt);
411 array_needsize (timers, timermax, timercnt, ); 710 array_needsize (timers, timermax, timercnt, );
412 timers [timercnt - 1] = w; 711 timers [timercnt - 1] = w;
413 upheap (timercnt - 1); 712 upheap ((WT *)timers, timercnt - 1);
414} 713}
415 714
416void 715void
417evtimer_stop (struct ev_timer *w) 716evtimer_stop (struct ev_timer *w)
418{ 717{
419 fprintf (stderr, "-topping %d, %d\n", w->active, timercnt);//D 718 ev_clear ((W)w);
420 if (!ev_is_active (w)) 719 if (!ev_is_active (w))
421 return; 720 return;
422 721
423 fprintf (stderr, "stopping %d, %d\n", w->active, timercnt);//D
424 if (w->active < timercnt) 722 if (w->active < timercnt--)
425 { 723 {
426 timers [w->active - 1] = timers [--timercnt]; 724 timers [w->active - 1] = timers [timercnt];
427 downheap (w->active - 1); 725 downheap ((WT *)timers, timercnt, w->active - 1);
726 }
727
728 w->at = w->repeat;
729
730 ev_stop ((W)w);
731}
732
733void
734evtimer_again (struct ev_timer *w)
735{
736 if (ev_is_active (w))
428 } 737 {
738 if (w->repeat)
739 {
740 w->at = now + w->repeat;
741 downheap ((WT *)timers, timercnt, w->active - 1);
742 }
743 else
744 evtimer_stop (w);
745 }
746 else if (w->repeat)
747 evtimer_start (w);
748}
429 749
430 ev_stop ((struct ev_watcher *)w); 750void
751evperiodic_start (struct ev_periodic *w)
752{
753 if (ev_is_active (w))
754 return;
755
756 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
757
758 /* this formula differs from the one in periodic_reify because we do not always round up */
759 if (w->interval)
760 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
761
762 ev_start ((W)w, ++periodiccnt);
763 array_needsize (periodics, periodicmax, periodiccnt, );
764 periodics [periodiccnt - 1] = w;
765 upheap ((WT *)periodics, periodiccnt - 1);
766}
767
768void
769evperiodic_stop (struct ev_periodic *w)
770{
771 ev_clear ((W)w);
772 if (!ev_is_active (w))
773 return;
774
775 if (w->active < periodiccnt--)
776 {
777 periodics [w->active - 1] = periodics [periodiccnt];
778 downheap ((WT *)periodics, periodiccnt, w->active - 1);
779 }
780
781 ev_stop ((W)w);
431} 782}
432 783
433void 784void
434evsignal_start (struct ev_signal *w) 785evsignal_start (struct ev_signal *w)
435{ 786{
436 if (ev_is_active (w)) 787 if (ev_is_active (w))
437 return; 788 return;
438 789
439 ev_start ((struct ev_watcher *)w, 1); 790 ev_start ((W)w, 1);
440 array_needsize (signals, signalmax, w->signum, signals_init); 791 array_needsize (signals, signalmax, w->signum, signals_init);
441 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 792 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
793
794 if (!w->next)
795 {
796 struct sigaction sa;
797 sa.sa_handler = sighandler;
798 sigfillset (&sa.sa_mask);
799 sa.sa_flags = 0;
800 sigaction (w->signum, &sa, 0);
801 }
442} 802}
443 803
444void 804void
445evsignal_stop (struct ev_signal *w) 805evsignal_stop (struct ev_signal *w)
446{ 806{
807 ev_clear ((W)w);
447 if (!ev_is_active (w)) 808 if (!ev_is_active (w))
448 return; 809 return;
449 810
450 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 811 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
451 ev_stop ((struct ev_watcher *)w); 812 ev_stop ((W)w);
452}
453 813
814 if (!signals [w->signum - 1].head)
815 signal (w->signum, SIG_DFL);
816}
817
818void evidle_start (struct ev_idle *w)
819{
820 if (ev_is_active (w))
821 return;
822
823 ev_start ((W)w, ++idlecnt);
824 array_needsize (idles, idlemax, idlecnt, );
825 idles [idlecnt - 1] = w;
826}
827
828void evidle_stop (struct ev_idle *w)
829{
830 ev_clear ((W)w);
831 if (ev_is_active (w))
832 return;
833
834 idles [w->active - 1] = idles [--idlecnt];
835 ev_stop ((W)w);
836}
837
838void evcheck_start (struct ev_check *w)
839{
840 if (ev_is_active (w))
841 return;
842
843 ev_start ((W)w, ++checkcnt);
844 array_needsize (checks, checkmax, checkcnt, );
845 checks [checkcnt - 1] = w;
846}
847
848void evcheck_stop (struct ev_check *w)
849{
850 ev_clear ((W)w);
851 if (ev_is_active (w))
852 return;
853
854 checks [w->active - 1] = checks [--checkcnt];
855 ev_stop ((W)w);
856}
857
454/*****************************************************************************/ 858/*****************************************************************************/
859
860struct ev_once
861{
862 struct ev_io io;
863 struct ev_timer to;
864 void (*cb)(int revents, void *arg);
865 void *arg;
866};
867
868static void
869once_cb (struct ev_once *once, int revents)
870{
871 void (*cb)(int revents, void *arg) = once->cb;
872 void *arg = once->arg;
873
874 evio_stop (&once->io);
875 evtimer_stop (&once->to);
876 free (once);
877
878 cb (revents, arg);
879}
880
881static void
882once_cb_io (struct ev_io *w, int revents)
883{
884 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
885}
886
887static void
888once_cb_to (struct ev_timer *w, int revents)
889{
890 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
891}
892
893void
894ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
895{
896 struct ev_once *once = malloc (sizeof (struct ev_once));
897
898 if (!once)
899 cb (EV_ERROR, arg);
900 else
901 {
902 once->cb = cb;
903 once->arg = arg;
904
905 evw_init (&once->io, once_cb_io);
906
907 if (fd >= 0)
908 {
909 evio_set (&once->io, fd, events);
910 evio_start (&once->io);
911 }
912
913 evw_init (&once->to, once_cb_to);
914
915 if (timeout >= 0.)
916 {
917 evtimer_set (&once->to, timeout, 0.);
918 evtimer_start (&once->to);
919 }
920 }
921}
922
923/*****************************************************************************/
924
455#if 1 925#if 0
926
927struct ev_io wio;
456 928
457static void 929static void
458sin_cb (struct ev_io *w, int revents) 930sin_cb (struct ev_io *w, int revents)
459{ 931{
460 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 932 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
461} 933}
462 934
463static void 935static void
464ocb (struct ev_timer *w, int revents) 936ocb (struct ev_timer *w, int revents)
465{ 937{
466 fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 938 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
939 evtimer_stop (w);
940 evtimer_start (w);
941}
942
943static void
944scb (struct ev_signal *w, int revents)
945{
946 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
947 evio_stop (&wio);
948 evio_start (&wio);
949}
950
951static void
952gcb (struct ev_signal *w, int revents)
953{
954 fprintf (stderr, "generic %x\n", revents);
955
467} 956}
468 957
469int main (void) 958int main (void)
470{ 959{
471 struct ev_io sin;
472
473 ev_init (0); 960 ev_init (0);
474 961
475 evw_init (&sin, sin_cb, 55);
476 evio_set (&sin, 0, EV_READ); 962 evio_init (&wio, sin_cb, 0, EV_READ);
477 evio_start (&sin); 963 evio_start (&wio);
478 964
479 struct ev_timer t[1000]; 965 struct ev_timer t[10000];
480 966
967#if 0
481 int i; 968 int i;
482 for (i = 0; i < 1000; ++i) 969 for (i = 0; i < 10000; ++i)
483 { 970 {
484 struct ev_timer *w = t + i; 971 struct ev_timer *w = t + i;
485 evw_init (w, ocb, i); 972 evw_init (w, ocb, i);
486 evtimer_set_rel (w, drand48 (), 0); 973 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
487 evtimer_start (w); 974 evtimer_start (w);
488 if (drand48 () < 0.5) 975 if (drand48 () < 0.5)
489 evtimer_stop (w); 976 evtimer_stop (w);
490 } 977 }
978#endif
979
980 struct ev_timer t1;
981 evtimer_init (&t1, ocb, 5, 10);
982 evtimer_start (&t1);
983
984 struct ev_signal sig;
985 evsignal_init (&sig, scb, SIGQUIT);
986 evsignal_start (&sig);
987
988 struct ev_check cw;
989 evcheck_init (&cw, gcb);
990 evcheck_start (&cw);
991
992 struct ev_idle iw;
993 evidle_init (&iw, gcb);
994 evidle_start (&iw);
491 995
492 ev_loop (0); 996 ev_loop (0);
493 997
494 return 0; 998 return 0;
495} 999}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines