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

Comparing libev/ev.c (file contents):
Revision 1.379 by root, Sun Jun 19 17:55:13 2011 UTC vs.
Revision 1.385 by root, Wed Jul 20 01:04:03 2011 UTC

492 #define ecb_inline static inline 492 #define ecb_inline static inline
493#else 493#else
494 #define ecb_inline static 494 #define ecb_inline static
495#endif 495#endif
496 496
497#ifndef ECB_MEMORY_FENCE
498 #if ECB_GCC_VERSION(2,5)
499 #if __x86
500 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory")
501 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
502 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE /* better be safe than sorry */
503 #elif __amd64
504 #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory")
505 #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory")
506 #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence")
507 #endif
508 #endif
509#endif
510
511#ifndef ECB_MEMORY_FENCE
512 #if ECB_GCC_VERSION(4,4)
513 #define ECB_MEMORY_FENCE __sync_synchronize ()
514 #define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); })
515 #define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); })
516 #elif defined(_WIN32) && defined(MemoryBarrier)
517 #define ECB_MEMORY_FENCE MemoryBarrier ()
518 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
519 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
520 #endif
521#endif
522
523#ifndef ECB_MEMORY_FENCE
524 #include <pthread.h>
525
526 static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER;
527 #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0)
528 #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE
529 #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE
530#endif
531
497#if ECB_GCC_VERSION(3,1) 532#if ECB_GCC_VERSION(3,1)
498 #define ecb_attribute(attrlist) __attribute__(attrlist) 533 #define ecb_attribute(attrlist) __attribute__(attrlist)
499 #define ecb_is_constant(expr) __builtin_constant_p (expr) 534 #define ecb_is_constant(expr) __builtin_constant_p (expr)
500 #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) 535 #define ecb_expect(expr,value) __builtin_expect ((expr),(value))
501 #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) 536 #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality)
964 memset ((void *)(base), 0, sizeof (*(base)) * (count)) 999 memset ((void *)(base), 0, sizeof (*(base)) * (count))
965 1000
966#define array_needsize(type,base,cur,cnt,init) \ 1001#define array_needsize(type,base,cur,cnt,init) \
967 if (expect_false ((cnt) > (cur))) \ 1002 if (expect_false ((cnt) > (cur))) \
968 { \ 1003 { \
969 int ocur_ = (cur); \ 1004 int ecb_unused ocur_ = (cur); \
970 (base) = (type *)array_realloc \ 1005 (base) = (type *)array_realloc \
971 (sizeof (type), (base), &(cur), (cnt)); \ 1006 (sizeof (type), (base), &(cur), (cnt)); \
972 init ((base) + (ocur_), (cur) - ocur_); \ 1007 init ((base) + (ocur_), (cur) - ocur_); \
973 } 1008 }
974 1009
1417 ev_io_start (EV_A_ &pipe_w); 1452 ev_io_start (EV_A_ &pipe_w);
1418 ev_unref (EV_A); /* watcher should not keep loop alive */ 1453 ev_unref (EV_A); /* watcher should not keep loop alive */
1419 } 1454 }
1420} 1455}
1421 1456
1422inline_size void 1457inline_speed void
1423evpipe_write (EV_P_ EV_ATOMIC_T *flag) 1458evpipe_write (EV_P_ EV_ATOMIC_T *flag)
1424{ 1459{
1425 if (!*flag) 1460 if (expect_true (*flag))
1426 { 1461 /*return*//*D*/;
1462
1427 *flag = 1; 1463 *flag = 1;
1428 1464
1465 ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */
1466
1429 pipe_write_skipped = 1; 1467 pipe_write_skipped = 1;
1430 1468
1469 ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */
1470
1431 if (pipe_write_wanted) 1471 if (pipe_write_wanted)
1472 {
1473 int old_errno;
1474
1475 pipe_write_skipped = 0; /* just an optimsiation, no fence needed */
1476
1477 old_errno = errno; /* save errno because write will clobber it */
1478
1479#if EV_USE_EVENTFD
1480 if (evfd >= 0)
1432 { 1481 {
1433 int old_errno = errno; /* save errno because write will clobber it */
1434 char dummy;
1435
1436 pipe_write_skipped = 0;
1437
1438#if EV_USE_EVENTFD
1439 if (evfd >= 0)
1440 {
1441 uint64_t counter = 1; 1482 uint64_t counter = 1;
1442 write (evfd, &counter, sizeof (uint64_t)); 1483 write (evfd, &counter, sizeof (uint64_t));
1443 }
1444 else
1445#endif
1446 {
1447 /* win32 people keep sending patches that change this write() to send() */
1448 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1449 /* so when you think this write should be a send instead, please find out */
1450 /* where your send() is from - it's definitely not the microsoft send, and */
1451 /* tell me. thank you. */
1452 write (evpipe [1], &dummy, 1);
1453 }
1454
1455 errno = old_errno;
1456 } 1484 }
1485 else
1486#endif
1487 {
1488 /* win32 people keep sending patches that change this write() to send() */
1489 /* and then run away. but send() is wrong, it wants a socket handle on win32 */
1490 /* so when you think this write should be a send instead, please find out */
1491 /* where your send() is from - it's definitely not the microsoft send, and */
1492 /* tell me. thank you. */
1493 write (evpipe [1], &(evpipe [1]), 1);
1494 }
1495
1496 errno = old_errno;
1457 } 1497 }
1458} 1498}
1459 1499
1460/* called whenever the libev signal pipe */ 1500/* called whenever the libev signal pipe */
1461/* got some events (signal, async) */ 1501/* got some events (signal, async) */
1519 1559
1520 if (!EV_A) 1560 if (!EV_A)
1521 return; 1561 return;
1522#endif 1562#endif
1523 1563
1524 evpipe_init (EV_A); 1564 if (!ev_active (&pipe_w))
1565 return;
1525 1566
1526 signals [signum - 1].pending = 1; 1567 signals [signum - 1].pending = 1;
1527 evpipe_write (EV_A_ &sig_pending); 1568 evpipe_write (EV_A_ &sig_pending);
1528} 1569}
1529 1570
2571 time_update (EV_A_ 1e100); 2612 time_update (EV_A_ 1e100);
2572 2613
2573 /* from now on, we want a pipe-wake-up */ 2614 /* from now on, we want a pipe-wake-up */
2574 pipe_write_wanted = 1; 2615 pipe_write_wanted = 1;
2575 2616
2617 ECB_MEMORY_FENCE; /* amke sure pipe_write_wanted is visible before we check for potential skips */
2618
2576 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) 2619 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped)))
2577 { 2620 {
2578 waittime = MAX_BLOCKTIME; 2621 waittime = MAX_BLOCKTIME;
2579 2622
2580 if (timercnt) 2623 if (timercnt)
2621#endif 2664#endif
2622 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ 2665 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2623 backend_poll (EV_A_ waittime); 2666 backend_poll (EV_A_ waittime);
2624 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ 2667 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2625 2668
2626 pipe_write_wanted = 0; 2669 pipe_write_wanted = 0; /* just an optimsiation, no fence needed */
2627 2670
2628 if (pipe_write_skipped) 2671 if (pipe_write_skipped)
2629 { 2672 {
2630 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); 2673 assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w)));
2631 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); 2674 ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines