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

Comparing libev/ev.c (file contents):
Revision 1.252 by root, Thu May 22 03:43:32 2008 UTC vs.
Revision 1.262 by root, Wed Oct 1 04:25:25 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
164#endif 165#endif
165 166
166/* this block tries to deduce configuration from header-defined symbols and defaults */ 167/* this block tries to deduce configuration from header-defined symbols and defaults */
167 168
168#ifndef EV_USE_MONOTONIC 169#ifndef EV_USE_MONOTONIC
170# if defined (_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
171# define EV_USE_MONOTONIC 1
172# else
169# define EV_USE_MONOTONIC 0 173# define EV_USE_MONOTONIC 0
174# endif
170#endif 175#endif
171 176
172#ifndef EV_USE_REALTIME 177#ifndef EV_USE_REALTIME
173# define EV_USE_REALTIME 0 178# define EV_USE_REALTIME 0
174#endif 179#endif
175 180
176#ifndef EV_USE_NANOSLEEP 181#ifndef EV_USE_NANOSLEEP
182# if _POSIX_C_SOURCE >= 199309L
183# define EV_USE_NANOSLEEP 1
184# else
177# define EV_USE_NANOSLEEP 0 185# define EV_USE_NANOSLEEP 0
186# endif
178#endif 187#endif
179 188
180#ifndef EV_USE_SELECT 189#ifndef EV_USE_SELECT
181# define EV_USE_SELECT 1 190# define EV_USE_SELECT 1
182#endif 191#endif
556 struct timeval tv; 565 struct timeval tv;
557 566
558 tv.tv_sec = (time_t)delay; 567 tv.tv_sec = (time_t)delay;
559 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6); 568 tv.tv_usec = (long)((delay - (ev_tstamp)(tv.tv_sec)) * 1e6);
560 569
570 /* here we rely on sys/time.h + sys/types.h + unistd.h providing select */
571 /* somehting nto guaranteed by newer posix versions, but guaranteed */
572 /* by older ones */
561 select (0, 0, 0, 0, &tv); 573 select (0, 0, 0, 0, &tv);
562#endif 574#endif
563 } 575 }
564} 576}
565 577
699 events |= (unsigned char)w->events; 711 events |= (unsigned char)w->events;
700 712
701#if EV_SELECT_IS_WINSOCKET 713#if EV_SELECT_IS_WINSOCKET
702 if (events) 714 if (events)
703 { 715 {
704 unsigned long argp; 716 unsigned long arg;
705 #ifdef EV_FD_TO_WIN32_HANDLE 717 #ifdef EV_FD_TO_WIN32_HANDLE
706 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd); 718 anfd->handle = EV_FD_TO_WIN32_HANDLE (fd);
707 #else 719 #else
708 anfd->handle = _get_osfhandle (fd); 720 anfd->handle = _get_osfhandle (fd);
709 #endif 721 #endif
710 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0)); 722 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &arg) == 0));
711 } 723 }
712#endif 724#endif
713 725
714 { 726 {
715 unsigned char o_events = anfd->events; 727 unsigned char o_events = anfd->events;
768{ 780{
769 int fd; 781 int fd;
770 782
771 for (fd = 0; fd < anfdmax; ++fd) 783 for (fd = 0; fd < anfdmax; ++fd)
772 if (anfds [fd].events) 784 if (anfds [fd].events)
773 if (!fd_valid (fd) == -1 && errno == EBADF) 785 if (!fd_valid (fd) && errno == EBADF)
774 fd_kill (EV_A_ fd); 786 fd_kill (EV_A_ fd);
775} 787}
776 788
777/* called on ENOMEM in select/poll to kill some fds and retry */ 789/* called on ENOMEM in select/poll to kill some fds and retry */
778static void noinline 790static void noinline
975 987
976void inline_speed 988void inline_speed
977fd_intern (int fd) 989fd_intern (int fd)
978{ 990{
979#ifdef _WIN32 991#ifdef _WIN32
980 int arg = 1; 992 unsigned long arg = 1;
981 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 993 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
982#else 994#else
983 fcntl (fd, F_SETFD, FD_CLOEXEC); 995 fcntl (fd, F_SETFD, FD_CLOEXEC);
984 fcntl (fd, F_SETFL, O_NONBLOCK); 996 fcntl (fd, F_SETFL, O_NONBLOCK);
985#endif 997#endif
1499{ 1511{
1500 postfork = 1; /* must be in line with ev_default_fork */ 1512 postfork = 1; /* must be in line with ev_default_fork */
1501} 1513}
1502 1514
1503#if EV_VERIFY 1515#if EV_VERIFY
1504void noinline 1516static void noinline
1505verify_watcher (EV_P_ W w) 1517verify_watcher (EV_P_ W w)
1506{ 1518{
1507 assert (("watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI)); 1519 assert (("watcher has invalid priority", ABSPRI (w) >= 0 && ABSPRI (w) < NUMPRI));
1508 1520
1509 if (w->pending) 1521 if (w->pending)
1897ev_unref (EV_P) 1909ev_unref (EV_P)
1898{ 1910{
1899 --activecnt; 1911 --activecnt;
1900} 1912}
1901 1913
1914void
1915ev_now_update (EV_P)
1916{
1917 time_update (EV_A_ 1e100);
1918}
1919
1902static int loop_done; 1920static int loop_done;
1903 1921
1904void 1922void
1905ev_loop (EV_P_ int flags) 1923ev_loop (EV_P_ int flags)
1906{ 1924{
2559 } 2577 }
2560 2578
2561 } 2579 }
2562} 2580}
2563 2581
2582#endif
2583
2584#ifdef _WIN32
2585# define EV_LSTAT(p,b) _stati64 (p, b)
2586#else
2587# define EV_LSTAT(p,b) lstat (p, b)
2564#endif 2588#endif
2565 2589
2566void 2590void
2567ev_stat_stat (EV_P_ ev_stat *w) 2591ev_stat_stat (EV_P_ ev_stat *w)
2568{ 2592{
2809 ev_loop (EV_A_ EVLOOP_NONBLOCK); 2833 ev_loop (EV_A_ EVLOOP_NONBLOCK);
2810 } 2834 }
2811 } 2835 }
2812} 2836}
2813 2837
2838static void
2839embed_fork_cb (EV_P_ ev_fork *fork_w, int revents)
2840{
2841 ev_embed *w = (ev_embed *)(((char *)fork_w) - offsetof (ev_embed, fork));
2842
2843 {
2844 struct ev_loop *loop = w->other;
2845
2846 ev_loop_fork (EV_A);
2847 }
2848}
2849
2814#if 0 2850#if 0
2815static void 2851static void
2816embed_idle_cb (EV_P_ ev_idle *idle, int revents) 2852embed_idle_cb (EV_P_ ev_idle *idle, int revents)
2817{ 2853{
2818 ev_idle_stop (EV_A_ idle); 2854 ev_idle_stop (EV_A_ idle);
2838 2874
2839 ev_prepare_init (&w->prepare, embed_prepare_cb); 2875 ev_prepare_init (&w->prepare, embed_prepare_cb);
2840 ev_set_priority (&w->prepare, EV_MINPRI); 2876 ev_set_priority (&w->prepare, EV_MINPRI);
2841 ev_prepare_start (EV_A_ &w->prepare); 2877 ev_prepare_start (EV_A_ &w->prepare);
2842 2878
2879 ev_fork_init (&w->fork, embed_fork_cb);
2880 ev_fork_start (EV_A_ &w->fork);
2881
2843 /*ev_idle_init (&w->idle, e,bed_idle_cb);*/ 2882 /*ev_idle_init (&w->idle, e,bed_idle_cb);*/
2844 2883
2845 ev_start (EV_A_ (W)w, 1); 2884 ev_start (EV_A_ (W)w, 1);
2846 2885
2847 EV_FREQUENT_CHECK; 2886 EV_FREQUENT_CHECK;
2854 if (expect_false (!ev_is_active (w))) 2893 if (expect_false (!ev_is_active (w)))
2855 return; 2894 return;
2856 2895
2857 EV_FREQUENT_CHECK; 2896 EV_FREQUENT_CHECK;
2858 2897
2859 ev_io_stop (EV_A_ &w->io); 2898 ev_io_stop (EV_A_ &w->io);
2860 ev_prepare_stop (EV_A_ &w->prepare); 2899 ev_prepare_stop (EV_A_ &w->prepare);
2861 2900 ev_fork_stop (EV_A_ &w->fork);
2862 ev_stop (EV_A_ (W)w);
2863 2901
2864 EV_FREQUENT_CHECK; 2902 EV_FREQUENT_CHECK;
2865} 2903}
2866#endif 2904#endif
2867 2905
2964once_cb (EV_P_ struct ev_once *once, int revents) 3002once_cb (EV_P_ struct ev_once *once, int revents)
2965{ 3003{
2966 void (*cb)(int revents, void *arg) = once->cb; 3004 void (*cb)(int revents, void *arg) = once->cb;
2967 void *arg = once->arg; 3005 void *arg = once->arg;
2968 3006
2969 ev_io_stop (EV_A_ &once->io); 3007 ev_io_stop (EV_A_ &once->io);
2970 ev_timer_stop (EV_A_ &once->to); 3008 ev_timer_stop (EV_A_ &once->to);
2971 ev_free (once); 3009 ev_free (once);
2972 3010
2973 cb (revents, arg); 3011 cb (revents, arg);
2974} 3012}
2975 3013
2976static void 3014static void
2977once_cb_io (EV_P_ ev_io *w, int revents) 3015once_cb_io (EV_P_ ev_io *w, int revents)
2978{ 3016{
2979 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 3017 struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io));
3018
3019 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->to));
2980} 3020}
2981 3021
2982static void 3022static void
2983once_cb_to (EV_P_ ev_timer *w, int revents) 3023once_cb_to (EV_P_ ev_timer *w, int revents)
2984{ 3024{
2985 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 3025 struct ev_once *once = (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to));
3026
3027 once_cb (EV_A_ once, revents | ev_clear_pending (EV_A_ &once->io));
2986} 3028}
2987 3029
2988void 3030void
2989ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 3031ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
2990{ 3032{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines