… | |
… | |
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 _MSC_VER >= 1400 && 0 /* TODO: only true when using volatiles */ |
|
|
517 | #define ECB_MEMORY_FENCE do { } while (0) |
|
|
518 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
519 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
520 | #elif defined(_WIN32) |
|
|
521 | #include <WinNT.h> |
|
|
522 | #define ECB_MEMORY_FENCE MemoryBarrier () |
|
|
523 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
524 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
525 | #endif |
|
|
526 | #endif |
|
|
527 | |
|
|
528 | #ifndef ECB_MEMORY_FENCE |
|
|
529 | #include <pthread.h> |
|
|
530 | |
|
|
531 | static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; |
|
|
532 | #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) |
|
|
533 | #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE |
|
|
534 | #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE |
|
|
535 | #endif |
|
|
536 | |
497 | #if ECB_GCC_VERSION(3,1) |
537 | #if ECB_GCC_VERSION(3,1) |
498 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
538 | #define ecb_attribute(attrlist) __attribute__(attrlist) |
499 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
539 | #define ecb_is_constant(expr) __builtin_constant_p (expr) |
500 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
540 | #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) |
501 | #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
541 | #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
… | |
… | |
964 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
1004 | memset ((void *)(base), 0, sizeof (*(base)) * (count)) |
965 | |
1005 | |
966 | #define array_needsize(type,base,cur,cnt,init) \ |
1006 | #define array_needsize(type,base,cur,cnt,init) \ |
967 | if (expect_false ((cnt) > (cur))) \ |
1007 | if (expect_false ((cnt) > (cur))) \ |
968 | { \ |
1008 | { \ |
969 | int ocur_ = (cur); \ |
1009 | int ecb_unused ocur_ = (cur); \ |
970 | (base) = (type *)array_realloc \ |
1010 | (base) = (type *)array_realloc \ |
971 | (sizeof (type), (base), &(cur), (cnt)); \ |
1011 | (sizeof (type), (base), &(cur), (cnt)); \ |
972 | init ((base) + (ocur_), (cur) - ocur_); \ |
1012 | init ((base) + (ocur_), (cur) - ocur_); \ |
973 | } |
1013 | } |
974 | |
1014 | |
… | |
… | |
1420 | } |
1460 | } |
1421 | |
1461 | |
1422 | inline_speed void |
1462 | inline_speed void |
1423 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1463 | evpipe_write (EV_P_ EV_ATOMIC_T *flag) |
1424 | { |
1464 | { |
1425 | if (!*flag) |
1465 | if (expect_true (*flag)) |
1426 | { |
1466 | return; |
|
|
1467 | |
1427 | *flag = 1; |
1468 | *flag = 1; |
1428 | |
1469 | |
|
|
1470 | ECB_MEMORY_FENCE_RELEASE; /* make sure flag is visible before the wakeup */ |
|
|
1471 | |
1429 | pipe_write_skipped = 1; |
1472 | pipe_write_skipped = 1; |
1430 | |
1473 | |
|
|
1474 | ECB_MEMORY_FENCE; /* make sure pipe_write_skipped is visible before we check pipe_write_wanted */ |
|
|
1475 | |
1431 | if (pipe_write_wanted) |
1476 | if (pipe_write_wanted) |
|
|
1477 | { |
|
|
1478 | int old_errno; |
|
|
1479 | |
|
|
1480 | pipe_write_skipped = 0; /* just an optimsiation, no fence needed */ |
|
|
1481 | |
|
|
1482 | old_errno = errno; /* save errno because write will clobber it */ |
|
|
1483 | |
|
|
1484 | #if EV_USE_EVENTFD |
|
|
1485 | if (evfd >= 0) |
1432 | { |
1486 | { |
1433 | int old_errno; |
|
|
1434 | |
|
|
1435 | pipe_write_skipped = 0; |
|
|
1436 | |
|
|
1437 | old_errno = errno; /* save errno because write will clobber it */ |
|
|
1438 | |
|
|
1439 | #if EV_USE_EVENTFD |
|
|
1440 | if (evfd >= 0) |
|
|
1441 | { |
|
|
1442 | uint64_t counter = 1; |
1487 | uint64_t counter = 1; |
1443 | write (evfd, &counter, sizeof (uint64_t)); |
1488 | write (evfd, &counter, sizeof (uint64_t)); |
1444 | } |
|
|
1445 | else |
|
|
1446 | #endif |
|
|
1447 | { |
|
|
1448 | /* win32 people keep sending patches that change this write() to send() */ |
|
|
1449 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
|
|
1450 | /* so when you think this write should be a send instead, please find out */ |
|
|
1451 | /* where your send() is from - it's definitely not the microsoft send, and */ |
|
|
1452 | /* tell me. thank you. */ |
|
|
1453 | write (evpipe [1], &(evpipe [1]), 1); |
|
|
1454 | } |
|
|
1455 | |
|
|
1456 | errno = old_errno; |
|
|
1457 | } |
1489 | } |
|
|
1490 | else |
|
|
1491 | #endif |
|
|
1492 | { |
|
|
1493 | /* win32 people keep sending patches that change this write() to send() */ |
|
|
1494 | /* and then run away. but send() is wrong, it wants a socket handle on win32 */ |
|
|
1495 | /* so when you think this write should be a send instead, please find out */ |
|
|
1496 | /* where your send() is from - it's definitely not the microsoft send, and */ |
|
|
1497 | /* tell me. thank you. */ |
|
|
1498 | write (evpipe [1], &(evpipe [1]), 1); |
|
|
1499 | } |
|
|
1500 | |
|
|
1501 | errno = old_errno; |
1458 | } |
1502 | } |
1459 | } |
1503 | } |
1460 | |
1504 | |
1461 | /* called whenever the libev signal pipe */ |
1505 | /* called whenever the libev signal pipe */ |
1462 | /* got some events (signal, async) */ |
1506 | /* got some events (signal, async) */ |
… | |
… | |
1520 | |
1564 | |
1521 | if (!EV_A) |
1565 | if (!EV_A) |
1522 | return; |
1566 | return; |
1523 | #endif |
1567 | #endif |
1524 | |
1568 | |
1525 | evpipe_init (EV_A); |
1569 | if (!ev_active (&pipe_w)) |
|
|
1570 | return; |
1526 | |
1571 | |
1527 | signals [signum - 1].pending = 1; |
1572 | signals [signum - 1].pending = 1; |
1528 | evpipe_write (EV_A_ &sig_pending); |
1573 | evpipe_write (EV_A_ &sig_pending); |
1529 | } |
1574 | } |
1530 | |
1575 | |
… | |
… | |
2572 | time_update (EV_A_ 1e100); |
2617 | time_update (EV_A_ 1e100); |
2573 | |
2618 | |
2574 | /* from now on, we want a pipe-wake-up */ |
2619 | /* from now on, we want a pipe-wake-up */ |
2575 | pipe_write_wanted = 1; |
2620 | pipe_write_wanted = 1; |
2576 | |
2621 | |
|
|
2622 | ECB_MEMORY_FENCE; /* amke sure pipe_write_wanted is visible before we check for potential skips */ |
|
|
2623 | |
2577 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2624 | if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt || pipe_write_skipped))) |
2578 | { |
2625 | { |
2579 | waittime = MAX_BLOCKTIME; |
2626 | waittime = MAX_BLOCKTIME; |
2580 | |
2627 | |
2581 | if (timercnt) |
2628 | if (timercnt) |
… | |
… | |
2622 | #endif |
2669 | #endif |
2623 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2670 | assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */ |
2624 | backend_poll (EV_A_ waittime); |
2671 | backend_poll (EV_A_ waittime); |
2625 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2672 | assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */ |
2626 | |
2673 | |
2627 | pipe_write_wanted = 0; |
2674 | pipe_write_wanted = 0; /* just an optimsiation, no fence needed */ |
2628 | |
2675 | |
2629 | if (pipe_write_skipped) |
2676 | if (pipe_write_skipped) |
2630 | { |
2677 | { |
2631 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
2678 | assert (("libev: pipe_w not active, but pipe not written", ev_is_active (&pipe_w))); |
2632 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |
2679 | ev_feed_event (EV_A_ &pipe_w, EV_CUSTOM); |