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

Comparing libev/ev.c (file contents):
Revision 1.437 by root, Tue May 29 21:03:22 2012 UTC vs.
Revision 1.447 by root, Tue Jun 19 12:29:43 2012 UTC

559 559
560#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */ 560#define ECB_C (__STDC__+0) /* this assumes that __STDC__ is either empty or a number */
561#define ECB_C99 (__STDC_VERSION__ >= 199901L) 561#define ECB_C99 (__STDC_VERSION__ >= 199901L)
562#define ECB_C11 (__STDC_VERSION__ >= 201112L) 562#define ECB_C11 (__STDC_VERSION__ >= 201112L)
563#define ECB_CPP (__cplusplus+0) 563#define ECB_CPP (__cplusplus+0)
564#define ECB_CPP98 (__cplusplus >= 199711L)
565#define ECB_CPP11 (__cplusplus >= 201103L) 564#define ECB_CPP11 (__cplusplus >= 201103L)
566 565
567/*****************************************************************************/ 566/*****************************************************************************/
568 567
569/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ 568/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */
570/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ 569/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */
571 570
572#if ECB_NO_THREADS 571#if ECB_NO_THREADS
573# define ECB_NO_SMP 1 572 #define ECB_NO_SMP 1
574#endif 573#endif
575 574
576#if ECB_NO_SMP 575#if ECB_NO_SMP
577 #define ECB_MEMORY_FENCE do { } while (0) 576 #define ECB_MEMORY_FENCE do { } while (0)
578#endif 577#endif
614 #endif 613 #endif
615#endif 614#endif
616 615
617#ifndef ECB_MEMORY_FENCE 616#ifndef ECB_MEMORY_FENCE
618 #if ECB_GCC_VERSION(4,7) 617 #if ECB_GCC_VERSION(4,7)
619 /* see comment below about the C11 memory model. in short - avoid */ 618 /* see comment below (stdatomic.h) about the C11 memory model. */
620 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST) 619 #define ECB_MEMORY_FENCE __atomic_thread_fence (__ATOMIC_SEQ_CST)
621 #elif defined __clang && __has_feature (cxx_atomic) 620 #elif defined __clang && __has_feature (cxx_atomic)
622 /* see above */ 621 /* see comment below (stdatomic.h) about the C11 memory model. */
623 #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST) 622 #define ECB_MEMORY_FENCE __c11_atomic_thread_fence (__ATOMIC_SEQ_CST)
624 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__ 623 #elif ECB_GCC_VERSION(4,4) || defined __INTEL_COMPILER || defined __clang__
625 #define ECB_MEMORY_FENCE __sync_synchronize () 624 #define ECB_MEMORY_FENCE __sync_synchronize ()
626 /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */
627 /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */
628 #elif _MSC_VER >= 1400 /* VC++ 2005 */ 625 #elif _MSC_VER >= 1400 /* VC++ 2005 */
629 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) 626 #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier)
630 #define ECB_MEMORY_FENCE _ReadWriteBarrier () 627 #define ECB_MEMORY_FENCE _ReadWriteBarrier ()
631 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ 628 #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */
632 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () 629 #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier ()
646#ifndef ECB_MEMORY_FENCE 643#ifndef ECB_MEMORY_FENCE
647 #if ECB_C11 && !defined __STDC_NO_ATOMICS__ 644 #if ECB_C11 && !defined __STDC_NO_ATOMICS__
648 /* we assume that these memory fences work on all variables/all memory accesses, */ 645 /* we assume that these memory fences work on all variables/all memory accesses, */
649 /* not just C11 atomics and atomic accesses */ 646 /* not just C11 atomics and atomic accesses */
650 #include <stdatomic.h> 647 #include <stdatomic.h>
651 /* unfortunately, the C11 memory model seems to be very limited, and unable to express */ 648 /* Unfortunately, neither gcc 4.7 nor clang 3.1 generate any instructions for */
652 /* simple barrier semantics. That means we need to take out thor's hammer. */ 649 /* any fence other than seq_cst, which isn't very efficient for us. */
650 /* Why that is, we don't know - either the C11 memory model is quite useless */
651 /* for most usages, or gcc and clang have a bug */
652 /* I *currently* lean towards the latter, and inefficiently implement */
653 /* all three of ecb's fences as a seq_cst fence */
653 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst) 654 #define ECB_MEMORY_FENCE atomic_thread_fence (memory_order_seq_cst)
654 #endif
655 #endif 655 #endif
656#endif 656#endif
657 657
658#ifndef ECB_MEMORY_FENCE 658#ifndef ECB_MEMORY_FENCE
659 #if !ECB_AVOID_PTHREADS 659 #if !ECB_AVOID_PTHREADS
1191} 1191}
1192 1192
1193static void * 1193static void *
1194ev_realloc_emul (void *ptr, long size) EV_THROW 1194ev_realloc_emul (void *ptr, long size) EV_THROW
1195{ 1195{
1196#if __GLIBC__
1197 return realloc (ptr, size);
1198#else
1199 /* some systems, notably openbsd and darwin, fail to properly 1196 /* some systems, notably openbsd and darwin, fail to properly
1200 * implement realloc (x, 0) (as required by both ansi c-89 and 1197 * implement realloc (x, 0) (as required by both ansi c-89 and
1201 * the single unix specification, so work around them here. 1198 * the single unix specification, so work around them here.
1199 * recently, also (at least) fedora and debian started breaking it,
1200 * despite documenting it otherwise.
1202 */ 1201 */
1203 1202
1204 if (size) 1203 if (size)
1205 return realloc (ptr, size); 1204 return realloc (ptr, size);
1206 1205
1207 free (ptr); 1206 free (ptr);
1208 return 0; 1207 return 0;
1209#endif
1210} 1208}
1211 1209
1212static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul; 1210static void *(*alloc)(void *ptr, long size) EV_THROW = ev_realloc_emul;
1213 1211
1214void ecb_cold 1212void ecb_cold
2045void noinline 2043void noinline
2046ev_feed_signal_event (EV_P_ int signum) EV_THROW 2044ev_feed_signal_event (EV_P_ int signum) EV_THROW
2047{ 2045{
2048 WL w; 2046 WL w;
2049 2047
2050 if (expect_false (signum <= 0 || signum > EV_NSIG)) 2048 if (expect_false (signum <= 0 || signum >= EV_NSIG))
2051 return; 2049 return;
2052 2050
2053 --signum; 2051 --signum;
2054 2052
2055#if EV_MULTIPLICITY 2053#if EV_MULTIPLICITY
2059 if (expect_false (signals [signum].loop != EV_A)) 2057 if (expect_false (signals [signum].loop != EV_A))
2060 return; 2058 return;
2061#endif 2059#endif
2062 2060
2063 signals [signum].pending = 0; 2061 signals [signum].pending = 0;
2064 MEMORY_FENCE_RELEASE; 2062 ECB_MEMORY_FENCE_RELEASE;
2065 2063
2066 for (w = signals [signum].head; w; w = w->next) 2064 for (w = signals [signum].head; w; w = w->next)
2067 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 2065 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
2068} 2066}
2069 2067
2550 EV_WIN32_CLOSE_FD (evpipe [1]); 2548 EV_WIN32_CLOSE_FD (evpipe [1]);
2551 } 2549 }
2552 2550
2553#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE 2551#if EV_SIGNAL_ENABLE || EV_ASYNC_ENABLE
2554 evpipe_init (EV_A); 2552 evpipe_init (EV_A);
2555 /* now iterate over everything, in case we missed something */ 2553 /* iterate over everything, in case we missed something before */
2556 pipecb (EV_A_ &pipe_w, EV_READ); 2554 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
2557#endif 2555#endif
2558 } 2556 }
2559 2557
2560 postfork = 0; 2558 postfork = 0;
2561} 2559}
2736} 2734}
2737 2735
2738void 2736void
2739ev_loop_fork (EV_P) EV_THROW 2737ev_loop_fork (EV_P) EV_THROW
2740{ 2738{
2741 postfork = 1; /* must be in line with ev_default_fork */ 2739 postfork = 1;
2742} 2740}
2743 2741
2744/*****************************************************************************/ 2742/*****************************************************************************/
2745 2743
2746void 2744void
2762} 2760}
2763 2761
2764void noinline 2762void noinline
2765ev_invoke_pending (EV_P) 2763ev_invoke_pending (EV_P)
2766{ 2764{
2767 for (pendingpri = NUMPRI; pendingpri--; ) /* pendingpri is modified during the loop */ 2765 pendingpri = NUMPRI;
2766
2767 while (pendingpri) /* pendingpri possibly gets modified in the inner loop */
2768 {
2769 --pendingpri;
2770
2768 while (pendingcnt [pendingpri]) 2771 while (pendingcnt [pendingpri])
2769 { 2772 {
2770 ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri]; 2773 ANPENDING *p = pendings [pendingpri] + --pendingcnt [pendingpri];
2771 2774
2772 p->w->pending = 0; 2775 p->w->pending = 0;
2773 EV_CB_INVOKE (p->w, p->events); 2776 EV_CB_INVOKE (p->w, p->events);
2774 EV_FREQUENT_CHECK; 2777 EV_FREQUENT_CHECK;
2775 } 2778 }
2779 }
2776} 2780}
2777 2781
2778#if EV_IDLE_ENABLE 2782#if EV_IDLE_ENABLE
2779/* make idle watchers pending. this handles the "call-idle */ 2783/* make idle watchers pending. this handles the "call-idle */
2780/* only when higher priorities are idle" logic */ 2784/* only when higher priorities are idle" logic */
3138 backend_poll (EV_A_ waittime); 3142 backend_poll (EV_A_ waittime);
3139 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 3143 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
3140 3144
3141 pipe_write_wanted = 0; /* just an optimisation, no fence needed */ 3145 pipe_write_wanted = 0; /* just an optimisation, no fence needed */
3142 3146
3147 ECB_MEMORY_FENCE_ACQUIRE;
3143 if (pipe_write_skipped) 3148 if (pipe_write_skipped)
3144 { 3149 {
3145 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); 3150 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
3146 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 3151 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);
3147 } 3152 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines