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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines