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

Comparing libev/ev.c (file contents):
Revision 1.16 by root, Wed Oct 31 13:57:34 2007 UTC vs.
Revision 1.26 by root, Wed Oct 31 21:50:15 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>
7 36
8#include <stdio.h> 37#include <stdio.h>
9 38
10#include <assert.h> 39#include <assert.h>
11#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
12#include <sys/time.h> 43#include <sys/time.h>
13#include <time.h> 44#include <time.h>
14
15#define HAVE_EPOLL 1
16 45
17#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
18# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
19# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
20# endif 49# endif
32# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
33#endif 62#endif
34 63
35#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) */
36#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
37 67
38#include "ev.h" 68#include "ev.h"
39 69
40typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
41typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
83} 113}
84 114
85#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
86 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
87 { \ 117 { \
88 int newcnt = cur ? cur << 1 : 16; \ 118 int newcnt = cur; \
119 do \
120 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \
122 } \
123 while ((cnt) > newcnt); \
124 \
89 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
90 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
91 cur = newcnt; \ 127 cur = newcnt; \
92 } 128 }
93 129
159 195
160 for (i = 0; i < eventcnt; ++i) 196 for (i = 0; i < eventcnt; ++i)
161 event (events [i], type); 197 event (events [i], type);
162} 198}
163 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
164/*****************************************************************************/ 216/*****************************************************************************/
165 217
166static struct ev_timer **timers; 218static struct ev_timer **timers;
167static int timermax, timercnt; 219static int timermax, timercnt;
168 220
284/*****************************************************************************/ 336/*****************************************************************************/
285 337
286static struct ev_idle **idles; 338static struct ev_idle **idles;
287static int idlemax, idlecnt; 339static int idlemax, idlecnt;
288 340
341static struct ev_prepare **prepares;
342static int preparemax, preparecnt;
343
289static struct ev_check **checks; 344static struct ev_check **checks;
290static int checkmax, checkcnt; 345static int checkmax, checkcnt;
291 346
292/*****************************************************************************/ 347/*****************************************************************************/
293 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
294#if HAVE_EPOLL 373#if HAVE_EPOLL
295# include "ev_epoll.c" 374# include "ev_epoll.c"
296#endif 375#endif
297#if HAVE_SELECT 376#if HAVE_SELECT
298# include "ev_select.c" 377# include "ev_select.c"
299#endif 378#endif
300 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
301int ev_init (int flags) 392int ev_init (int flags)
302{ 393{
394 if (!ev_method)
395 {
303#if HAVE_MONOTONIC 396#if HAVE_MONOTONIC
304 { 397 {
305 struct timespec ts; 398 struct timespec ts;
306 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 399 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
307 have_monotonic = 1; 400 have_monotonic = 1;
308 } 401 }
309#endif 402#endif
310 403
311 ev_now = ev_time (); 404 ev_now = ev_time ();
312 now = get_clock (); 405 now = get_clock ();
313 diff = ev_now - now; 406 diff = ev_now - now;
314 407
315 if (pipe (sigpipe)) 408 if (pipe (sigpipe))
316 return 0; 409 return 0;
317 410
318 ev_method = EVMETHOD_NONE; 411 ev_method = EVMETHOD_NONE;
319#if HAVE_EPOLL 412#if HAVE_EPOLL
320 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 413 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
321#endif 414#endif
322#if HAVE_SELECT 415#if HAVE_SELECT
323 if (ev_method == EVMETHOD_NONE) select_init (flags); 416 if (ev_method == EVMETHOD_NONE) select_init (flags);
324#endif 417#endif
325 418
326 if (ev_method) 419 if (ev_method)
327 { 420 {
328 evw_init (&sigev, sigcb); 421 evw_init (&sigev, sigcb);
329 siginit (); 422 siginit ();
423
424 evsignal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev);
426 }
330 } 427 }
331 428
332 return ev_method; 429 return ev_method;
333} 430}
334 431
335/*****************************************************************************/ 432/*****************************************************************************/
336 433
434void
337void ev_prefork (void) 435ev_prefork (void)
338{ 436{
339 /* nop */ 437 /* nop */
340} 438}
341 439
440void
342void ev_postfork_parent (void) 441ev_postfork_parent (void)
343{ 442{
344 /* nop */ 443 /* nop */
345} 444}
346 445
446void
347void ev_postfork_child (void) 447ev_postfork_child (void)
348{ 448{
349#if HAVE_EPOLL 449#if HAVE_EPOLL
350 if (ev_method == EVMETHOD_EPOLL) 450 if (ev_method == EVMETHOD_EPOLL)
351 epoll_postfork_child (); 451 epoll_postfork_child ();
352#endif 452#endif
385 485
386 fdchangecnt = 0; 486 fdchangecnt = 0;
387} 487}
388 488
389static void 489static void
390call_pending () 490call_pending (void)
391{ 491{
392 int i; 492 while (pendingcnt)
393
394 for (i = 0; i < pendingcnt; ++i)
395 { 493 {
396 ANPENDING *p = pendings + i; 494 ANPENDING *p = pendings + --pendingcnt;
397 495
398 if (p->w) 496 if (p->w)
399 { 497 {
400 p->w->pending = 0; 498 p->w->pending = 0;
401 p->w->cb (p->w, p->events); 499 p->w->cb (p->w, p->events);
402 } 500 }
403 } 501 }
404
405 pendingcnt = 0;
406} 502}
407 503
408static void 504static void
409timers_reify () 505timers_reify (void)
410{ 506{
411 while (timercnt && timers [0]->at <= now) 507 while (timercnt && timers [0]->at <= now)
412 { 508 {
413 struct ev_timer *w = timers [0]; 509 struct ev_timer *w = timers [0];
414 510
425 evtimer_stop (w); /* nonrepeating: stop timer */ 521 evtimer_stop (w); /* nonrepeating: stop timer */
426 } 522 }
427} 523}
428 524
429static void 525static void
430periodics_reify () 526periodics_reify (void)
431{ 527{
432 while (periodiccnt && periodics [0]->at <= ev_now) 528 while (periodiccnt && periodics [0]->at <= ev_now)
433 { 529 {
434 struct ev_periodic *w = periodics [0]; 530 struct ev_periodic *w = periodics [0];
435 531
471 } 567 }
472 } 568 }
473} 569}
474 570
475static void 571static void
476time_update () 572time_update (void)
477{ 573{
478 int i; 574 int i;
479 575
480 ev_now = ev_time (); 576 ev_now = ev_time ();
481 577
515int ev_loop_done; 611int ev_loop_done;
516 612
517void ev_loop (int flags) 613void ev_loop (int flags)
518{ 614{
519 double block; 615 double block;
520 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 616 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
521
522 if (checkcnt)
523 {
524 queue_events ((W *)checks, checkcnt, EV_CHECK);
525 call_pending ();
526 }
527 617
528 do 618 do
529 { 619 {
620 /* queue check watchers (and execute them) */
621 if (preparecnt)
622 {
623 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
624 call_pending ();
625 }
626
530 /* update fd-related kernel structures */ 627 /* update fd-related kernel structures */
531 fd_reify (); 628 fd_reify ();
532 629
533 /* calculate blocking time */ 630 /* calculate blocking time */
534 631
535 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 632 /* we only need this for !monotonic clockor timers, but as we basically
633 always have timers, we just calculate it always */
536 ev_now = ev_time (); 634 ev_now = ev_time ();
537 635
538 if (flags & EVLOOP_NONBLOCK || idlecnt) 636 if (flags & EVLOOP_NONBLOCK || idlecnt)
539 block = 0.; 637 block = 0.;
540 else 638 else
560 658
561 /* update ev_now, do magic */ 659 /* update ev_now, do magic */
562 time_update (); 660 time_update ();
563 661
564 /* queue pending timers and reschedule them */ 662 /* queue pending timers and reschedule them */
663 timers_reify (); /* relative timers called last */
565 periodics_reify (); /* absolute timers first */ 664 periodics_reify (); /* absolute timers called first */
566 timers_reify (); /* relative timers second */
567 665
568 /* queue idle watchers unless io or timers are pending */ 666 /* queue idle watchers unless io or timers are pending */
569 if (!pendingcnt) 667 if (!pendingcnt)
570 queue_events ((W *)idles, idlecnt, EV_IDLE); 668 queue_events ((W *)idles, idlecnt, EV_IDLE);
571 669
572 /* queue check and possibly idle watchers */ 670 /* queue check watchers, to be executed first */
671 if (checkcnt)
573 queue_events ((W *)checks, checkcnt, EV_CHECK); 672 queue_events ((W *)checks, checkcnt, EV_CHECK);
574 673
575 call_pending (); 674 call_pending ();
576 } 675 }
577 while (!ev_loop_done); 676 while (!ev_loop_done);
578 677
641 wlist_add ((WL *)&anfds[fd].head, (WL)w); 740 wlist_add ((WL *)&anfds[fd].head, (WL)w);
642 741
643 ++fdchangecnt; 742 ++fdchangecnt;
644 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 743 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
645 fdchanges [fdchangecnt - 1] = fd; 744 fdchanges [fdchangecnt - 1] = fd;
745
746 if (w->fd == 9)
747 printf ("start %p:%x\n", w, w->events);//D
646} 748}
647 749
648void 750void
649evio_stop (struct ev_io *w) 751evio_stop (struct ev_io *w)
650{ 752{
753 if (w->fd == 9)
754 printf ("stop %p:%x\n", w, w->events);//D
651 ev_clear ((W)w); 755 ev_clear ((W)w);
652 if (!ev_is_active (w)) 756 if (!ev_is_active (w))
653 return; 757 return;
654 758
655 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 759 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
797 901
798 idles [w->active - 1] = idles [--idlecnt]; 902 idles [w->active - 1] = idles [--idlecnt];
799 ev_stop ((W)w); 903 ev_stop ((W)w);
800} 904}
801 905
906void evprepare_start (struct ev_prepare *w)
907{
908 if (ev_is_active (w))
909 return;
910
911 ev_start ((W)w, ++preparecnt);
912 array_needsize (prepares, preparemax, preparecnt, );
913 prepares [preparecnt - 1] = w;
914}
915
916void evprepare_stop (struct ev_prepare *w)
917{
918 ev_clear ((W)w);
919 if (ev_is_active (w))
920 return;
921
922 prepares [w->active - 1] = prepares [--preparecnt];
923 ev_stop ((W)w);
924}
925
802void evcheck_start (struct ev_check *w) 926void evcheck_start (struct ev_check *w)
803{ 927{
804 if (ev_is_active (w)) 928 if (ev_is_active (w))
805 return; 929 return;
806 930
814 ev_clear ((W)w); 938 ev_clear ((W)w);
815 if (ev_is_active (w)) 939 if (ev_is_active (w))
816 return; 940 return;
817 941
818 checks [w->active - 1] = checks [--checkcnt]; 942 checks [w->active - 1] = checks [--checkcnt];
943 ev_stop ((W)w);
944}
945
946void evchild_start (struct ev_child *w)
947{
948 if (ev_is_active (w))
949 return;
950
951 ev_start ((W)w, 1);
952 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
953}
954
955void evchild_stop (struct ev_child *w)
956{
957 ev_clear ((W)w);
958 if (ev_is_active (w))
959 return;
960
961 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
819 ev_stop ((W)w); 962 ev_stop ((W)w);
820} 963}
821 964
822/*****************************************************************************/ 965/*****************************************************************************/
823 966

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines