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

Comparing libev/ev.c (file contents):
Revision 1.253 by root, Sat May 31 03:13:27 2008 UTC vs.
Revision 1.265 by root, Thu Oct 23 04:56:49 2008 UTC

154#ifndef _WIN32 154#ifndef _WIN32
155# include <sys/time.h> 155# include <sys/time.h>
156# include <sys/wait.h> 156# include <sys/wait.h>
157# include <unistd.h> 157# include <unistd.h>
158#else 158#else
159# include <io.h>
159# define WIN32_LEAN_AND_MEAN 160# define WIN32_LEAN_AND_MEAN
160# include <windows.h> 161# include <windows.h>
161# ifndef EV_SELECT_IS_WINSOCKET 162# ifndef EV_SELECT_IS_WINSOCKET
162# define EV_SELECT_IS_WINSOCKET 1 163# define EV_SELECT_IS_WINSOCKET 1
163# endif 164# endif
285# include <sys/select.h> 286# include <sys/select.h>
286# endif 287# endif
287#endif 288#endif
288 289
289#if EV_USE_INOTIFY 290#if EV_USE_INOTIFY
291# include <sys/utsname.h>
290# include <sys/inotify.h> 292# include <sys/inotify.h>
293/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
294# ifndef IN_DONT_FOLLOW
295# undef EV_USE_INOTIFY
296# define EV_USE_INOTIFY 0
297# endif
291#endif 298#endif
292 299
293#if EV_SELECT_IS_WINSOCKET 300#if EV_SELECT_IS_WINSOCKET
294# include <winsock.h> 301# include <winsock.h>
295#endif 302#endif
442typedef struct 449typedef struct
443{ 450{
444 WL head; 451 WL head;
445 unsigned char events; 452 unsigned char events;
446 unsigned char reify; 453 unsigned char reify;
454 unsigned char emask; /* the epoll backend stores the actual kernel mask in here */
455 unsigned char unused; /* currently unused padding */
447#if EV_SELECT_IS_WINSOCKET 456#if EV_SELECT_IS_WINSOCKET
448 SOCKET handle; 457 SOCKET handle;
449#endif 458#endif
450} ANFD; 459} ANFD;
451 460
564 struct timeval tv; 573 struct timeval tv;
565 574
566 tv.tv_sec = (time_t)delay; 575 tv.tv_sec = (time_t)delay;
567 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 576 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
568 577
578 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
579 /* somehting nto guaranteed by newer posix versions, but guaranteed */
580 /* by older ones */
569 select (0, 0, 0, 0, &tv); 581 select (0, 0, 0, 0, &tv);
570#endif 582#endif
571 } 583 }
572} 584}
573 585
600array_realloc (int elem, void *base, int *cur, int cnt) 612array_realloc (int elem, void *base, int *cur, int cnt)
601{ 613{
602 *cur = array_nextsize (elem, *cur, cnt); 614 *cur = array_nextsize (elem, *cur, cnt);
603 return ev_realloc (base, elem * *cur); 615 return ev_realloc (base, elem * *cur);
604} 616}
617
618#define array_init_zero(base,count) \
619 memset ((void *)(base), 0, sizeof (*(base)) * (count))
605 620
606#define array_needsize(type,base,cur,cnt,init) \ 621#define array_needsize(type,base,cur,cnt,init) \
607 if (expect_false ((cnt) > (cur))) \ 622 if (expect_false ((cnt) > (cur))) \
608 { \ 623 { \
609 int ocur_ = (cur); \ 624 int ocur_ = (cur); \
653 ev_feed_event (EV_A_ events [i], type); 668 ev_feed_event (EV_A_ events [i], type);
654} 669}
655 670
656/*****************************************************************************/ 671/*****************************************************************************/
657 672
658void inline_size
659anfds_init (ANFD *base, int count)
660{
661 while (count--)
662 {
663 base->head = 0;
664 base->events = EV_NONE;
665 base->reify = 0;
666
667 ++base;
668 }
669}
670
671void inline_speed 673void inline_speed
672fd_event (EV_P_ int fd, int revents) 674fd_event (EV_P_ int fd, int revents)
673{ 675{
674 ANFD *anfd = anfds + fd; 676 ANFD *anfd = anfds + fd;
675 ev_io *w; 677 ev_io *w;
707 events |= (unsigned char)w->events; 709 events |= (unsigned char)w->events;
708 710
709#if EV_SELECT_IS_WINSOCKET 711#if EV_SELECT_IS_WINSOCKET
710 if (events) 712 if (events)
711 { 713 {
712 unsigned long argp; 714 unsigned long arg;
713 #ifdef EV_FD_TO_WIN32_HANDLE 715 #ifdef EV_FD_TO_WIN32_HANDLE
714 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 716 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
715 #else 717 #else
716 anfd->handle = _get_osfhandle (fd); 718 anfd->handle = _get_osfhandle (fd);
717 #endif 719 #endif
718 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); 720 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
719 } 721 }
720#endif 722#endif
721 723
722 { 724 {
723 unsigned char o_events = anfd->events; 725 unsigned char o_events = anfd->events;
776{ 778{
777 int fd; 779 int fd;
778 780
779 for (fd = 0; fd < anfdmax; ++fd) 781 for (fd = 0; fd < anfdmax; ++fd)
780 if (anfds [fd].events) 782 if (anfds [fd].events)
781 if (!fd_valid (fd) == -1 && errno == EBADF) 783 if (!fd_valid (fd) && errno == EBADF)
782 fd_kill (EV_A_ fd); 784 fd_kill (EV_A_ fd);
783} 785}
784 786
785/* called on ENOMEM in select/poll to kill some fds and retry */ 787/* called on ENOMEM in select/poll to kill some fds and retry */
786static void noinline 788static void noinline
965static ANSIG *signals; 967static ANSIG *signals;
966static int signalmax; 968static int signalmax;
967 969
968static EV_ATOMIC_T gotsig; 970static EV_ATOMIC_T gotsig;
969 971
970void inline_size
971signals_init (ANSIG *base, int count)
972{
973 while (count--)
974 {
975 base->head = 0;
976 base->gotsig = 0;
977
978 ++base;
979 }
980}
981
982/*****************************************************************************/ 972/*****************************************************************************/
983 973
984void inline_speed 974void inline_speed
985fd_intern (int fd) 975fd_intern (int fd)
986{ 976{
987#ifdef _WIN32 977#ifdef _WIN32
988 int arg = 1; 978 unsigned long arg = 1;
989 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 979 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
990#else 980#else
991 fcntl (fd, F_SETFD, FD_CLOEXEC); 981 fcntl (fd, F_SETFD, FD_CLOEXEC);
992 fcntl (fd, F_SETFL, O_NONBLOCK); 982 fcntl (fd, F_SETFL, O_NONBLOCK);
993#endif 983#endif
1507{ 1497{
1508 postfork = 1; /* must be in line with ev_default_fork */ 1498 postfork = 1; /* must be in line with ev_default_fork */
1509} 1499}
1510 1500
1511#if EV_VERIFY 1501#if EV_VERIFY
1512void noinline 1502static void noinline
1513verify_watcher (EV_P_ W w) 1503verify_watcher (EV_P_ W w)
1514{ 1504{
1515 assert (("watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); 1505 assert (("watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
1516 1506
1517 if (w->pending) 1507 if (w->pending)
1905ev_unref (EV_P) 1895ev_unref (EV_P)
1906{ 1896{
1907 --activecnt; 1897 --activecnt;
1908} 1898}
1909 1899
1900void
1901ev_now_update (EV_P)
1902{
1903 time_update (EV_A_ 1e100);
1904}
1905
1910static int loop_done; 1906static int loop_done;
1911 1907
1912void 1908void
1913ev_loop (EV_P_ int flags) 1909ev_loop (EV_P_ int flags)
1914{ 1910{
2123 2119
2124 if (expect_false (ev_is_active (w))) 2120 if (expect_false (ev_is_active (w)))
2125 return; 2121 return;
2126 2122
2127 assert (("ev_io_start called with negative fd", fd >= 0)); 2123 assert (("ev_io_start called with negative fd", fd >= 0));
2124 assert (("ev_io start called with illegal event mask", !(w->events & ~(EV_IOFDSET | EV_READ | EV_WRITE))));
2128 2125
2129 EV_FREQUENT_CHECK; 2126 EV_FREQUENT_CHECK;
2130 2127
2131 ev_start (EV_A_ (W)w, 1); 2128 ev_start (EV_A_ (W)w, 1);
2132 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init); 2129 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2133 wlist_add (&anfds[fd].head, (WL)w); 2130 wlist_add (&anfds[fd].head, (WL)w);
2134 2131
2135 fd_change (EV_A_ fd, w->events & EV_IOFDSET | 1); 2132 fd_change (EV_A_ fd, w->events & EV_IOFDSET | 1);
2136 w->events &= ~EV_IOFDSET; 2133 w->events &= ~EV_IOFDSET;
2137 2134
2329 sigset_t full, prev; 2326 sigset_t full, prev;
2330 sigfillset (&full); 2327 sigfillset (&full);
2331 sigprocmask (SIG_SETMASK, &full, &prev); 2328 sigprocmask (SIG_SETMASK, &full, &prev);
2332#endif 2329#endif
2333 2330
2334 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init); 2331 array_needsize (ANSIG, signals, signalmax, w->signum, array_init_zero);
2335 2332
2336#ifndef _WIN32 2333#ifndef _WIN32
2337 sigprocmask (SIG_SETMASK, &prev, 0); 2334 sigprocmask (SIG_SETMASK, &prev, 0);
2338#endif 2335#endif
2339 } 2336 }
2481 2478
2482static void noinline 2479static void noinline
2483infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev) 2480infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
2484{ 2481{
2485 if (slot < 0) 2482 if (slot < 0)
2486 /* overflow, need to check for all hahs slots */ 2483 /* overflow, need to check for all hash slots */
2487 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot) 2484 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
2488 infy_wd (EV_A_ slot, wd, ev); 2485 infy_wd (EV_A_ slot, wd, ev);
2489 else 2486 else
2490 { 2487 {
2491 WL w_; 2488 WL w_;
2525infy_init (EV_P) 2522infy_init (EV_P)
2526{ 2523{
2527 if (fs_fd != -2) 2524 if (fs_fd != -2)
2528 return; 2525 return;
2529 2526
2527 /* kernels < 2.6.25 are borked
2528 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
2529 */
2530 {
2531 struct utsname buf;
2532 int major, minor, micro;
2533
2534 fs_fd = -1;
2535
2536 if (uname (&buf))
2537 return;
2538
2539 if (sscanf (buf.release, "%d.%d.%d", &major, &minor, &micro) != 3)
2540 return;
2541
2542 if (major < 2
2543 || (major == 2 && minor < 6)
2544 || (major == 2 && minor == 6 && micro < 25))
2545 return;
2546 }
2547
2530 fs_fd = inotify_init (); 2548 fs_fd = inotify_init ();
2531 2549
2532 if (fs_fd >= 0) 2550 if (fs_fd >= 0)
2533 { 2551 {
2534 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ); 2552 ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
2563 if (fs_fd >= 0) 2581 if (fs_fd >= 0)
2564 infy_add (EV_A_ w); /* re-add, no matter what */ 2582 infy_add (EV_A_ w); /* re-add, no matter what */
2565 else 2583 else
2566 ev_timer_start (EV_A_ &w->timer); 2584 ev_timer_start (EV_A_ &w->timer);
2567 } 2585 }
2568
2569 } 2586 }
2570} 2587}
2571 2588
2589#endif
2590
2591#ifdef _WIN32
2592# define EV_LSTAT(p,b) _stati64 (p, b)
2593#else
2594# define EV_LSTAT(p,b) lstat (p, b)
2572#endif 2595#endif
2573 2596
2574void 2597void
2575ev_stat_stat (EV_P_ ev_stat *w) 2598ev_stat_stat (EV_P_ ev_stat *w)
2576{ 2599{
2603 || w->prev.st_atime != w->attr.st_atime 2626 || w->prev.st_atime != w->attr.st_atime
2604 || w->prev.st_mtime != w->attr.st_mtime 2627 || w->prev.st_mtime != w->attr.st_mtime
2605 || w->prev.st_ctime != w->attr.st_ctime 2628 || w->prev.st_ctime != w->attr.st_ctime
2606 ) { 2629 ) {
2607 #if EV_USE_INOTIFY 2630 #if EV_USE_INOTIFY
2631 if (fs_fd >= 0)
2632 {
2608 infy_del (EV_A_ w); 2633 infy_del (EV_A_ w);
2609 infy_add (EV_A_ w); 2634 infy_add (EV_A_ w);
2610 ev_stat_stat (EV_A_ w); /* avoid race... */ 2635 ev_stat_stat (EV_A_ w); /* avoid race... */
2636 }
2611 #endif 2637 #endif
2612 2638
2613 ev_feed_event (EV_A_ w, EV_STAT); 2639 ev_feed_event (EV_A_ w, EV_STAT);
2614 } 2640 }
2615} 2641}
2817 ev_loop (EV_A_ EVLOOP_NONBLOCK); 2843 ev_loop (EV_A_ EVLOOP_NONBLOCK);
2818 } 2844 }
2819 } 2845 }
2820} 2846}
2821 2847
2848static void
2849embed_fork_cb (EV_P_ ev_fork *fork_w, int revents)
2850{
2851 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
2852
2853 {
2854 struct ev_loop *loop = w->other;
2855
2856 ev_loop_fork (EV_A);
2857 }
2858}
2859
2822#if 0 2860#if 0
2823static void 2861static void
2824embed_idle_cb (EV_P_ ev_idle *idle, int revents) 2862embed_idle_cb (EV_P_ ev_idle *idle, int revents)
2825{ 2863{
2826 ev_idle_stop (EV_A_ idle); 2864 ev_idle_stop (EV_A_ idle);
2846 2884
2847 ev_prepare_init (&w->prepare, embed_prepare_cb); 2885 ev_prepare_init (&w->prepare, embed_prepare_cb);
2848 ev_set_priority (&w->prepare, EV_MINPRI); 2886 ev_set_priority (&w->prepare, EV_MINPRI);
2849 ev_prepare_start (EV_A_ &w->prepare); 2887 ev_prepare_start (EV_A_ &w->prepare);
2850 2888
2889 ev_fork_init (&w->fork, embed_fork_cb);
2890 ev_fork_start (EV_A_ &w->fork);
2891
2851 /*ev_idle_init (&w->idle, e,bed_idle_cb);*/ 2892 /*ev_idle_init (&w->idle, e,bed_idle_cb);*/
2852 2893
2853 ev_start (EV_A_ (W)w, 1); 2894 ev_start (EV_A_ (W)w, 1);
2854 2895
2855 EV_FREQUENT_CHECK; 2896 EV_FREQUENT_CHECK;
2862 if (expect_false (!ev_is_active (w))) 2903 if (expect_false (!ev_is_active (w)))
2863 return; 2904 return;
2864 2905
2865 EV_FREQUENT_CHECK; 2906 EV_FREQUENT_CHECK;
2866 2907
2867 ev_io_stop (EV_A_ &w->io); 2908 ev_io_stop (EV_A_ &w->io);
2868 ev_prepare_stop (EV_A_ &w->prepare); 2909 ev_prepare_stop (EV_A_ &w->prepare);
2869 2910 ev_fork_stop (EV_A_ &w->fork);
2870 ev_stop (EV_A_ (W)w);
2871 2911
2872 EV_FREQUENT_CHECK; 2912 EV_FREQUENT_CHECK;
2873} 2913}
2874#endif 2914#endif
2875 2915
2972once_cb (EV_P_ struct ev_once *once, int revents) 3012once_cb (EV_P_ struct ev_once *once, int revents)
2973{ 3013{
2974 void (*cb)(int revents, void *arg) = once->cb; 3014 void (*cb)(int revents, void *arg) = once->cb;
2975 void *arg = once->arg; 3015 void *arg = once->arg;
2976 3016
2977 ev_io_stop (EV_A_ &once->io); 3017 ev_io_stop (EV_A_ &once->io);
2978 ev_timer_stop (EV_A_ &once->to); 3018 ev_timer_stop (EV_A_ &once->to);
2979 ev_free (once); 3019 ev_free (once);
2980 3020
2981 cb (revents, arg); 3021 cb (revents, arg);
2982} 3022}
2983 3023
2984static void 3024static void
2985once_cb_io (EV_P_ ev_io *w, int revents) 3025once_cb_io (EV_P_ ev_io *w, int revents)
2986{ 3026{
2987 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 3027 struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io));
3028
3029 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->to));
2988} 3030}
2989 3031
2990static void 3032static void
2991once_cb_to (EV_P_ ev_timer *w, int revents) 3033once_cb_to (EV_P_ ev_timer *w, int revents)
2992{ 3034{
2993 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 3035 struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to));
3036
3037 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
2994} 3038}
2995 3039
2996void 3040void
2997ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 3041ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
2998{ 3042{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines