From 347daceced3a516fec8080e0fe640f68c137db76 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 5 Dec 2019 16:15:30 -0500 Subject: use pipe to emulate eventfd on mac; add install script --- include/salticidae/event.h | 37 ++++++++----------------------------- 1 file changed, 8 insertions(+), 29 deletions(-) (limited to 'include') diff --git a/include/salticidae/event.h b/include/salticidae/event.h index fe94c0a..52cb058 100644 --- a/include/salticidae/event.h +++ b/include/salticidae/event.h @@ -577,7 +577,10 @@ class NotifyFd { throw SalticidaeError(SALTI_ERROR_FD); } bool reset() { - return read(fds[0], dummy, 8) == 8; + // clear the pipe buffer (not atomically) + while (read(fds[0], dummy, 8) > 0); + // may not work for MPMC, but salticidae currently doesn't use that + return true; } void notify() { write(fds[1], dummy, 8); @@ -597,28 +600,18 @@ template class MPSCQueueEventDriven: public MPSCQueue { private: std::atomic wait_sig; - //int fd; NotifyFd nfd; FdEvent ev; public: MPSCQueueEventDriven(): - wait_sig(true) { - //fd(eventfd(0, EFD_NONBLOCK)) { - //if (fd == -1) throw SalticidaeError(SALTI_ERROR_FD); - } - ~MPSCQueueEventDriven() { - //close(fd); - unreg_handler(); - } + wait_sig(true) {} + ~MPSCQueueEventDriven() { unreg_handler(); } template void reg_handler(const EventContext &ec, Func &&func) { ev = FdEvent(ec, nfd.read_fd(), [this, func=std::forward(func)](int, int) { - //fprintf(stderr, "%x\n", std::this_thread::get_id()); - //uint64_t t; - //read(fd, &t, 8); nfd.reset(); // the only undesirable case is there are some new items // enqueued before recovering wait_sig to true, so the consumer @@ -629,7 +622,6 @@ class MPSCQueueEventDriven: public MPSCQueue { wait_sig.exchange(true, std::memory_order_acq_rel); if (func(*this)) nfd.notify(); - //write(fd, &dummy, 8); }); ev.add(FdEvent::READ); } @@ -644,7 +636,6 @@ class MPSCQueueEventDriven: public MPSCQueue { if (wait_sig.exchange(false, std::memory_order_acq_rel)) { //SALTICIDAE_LOG_DEBUG("mpsc notify"); - //write(fd, &dummy, 8); nfd.notify(); } return true; @@ -658,33 +649,22 @@ template class MPMCQueueEventDriven: public MPMCQueue { private: std::atomic wait_sig; - //int fd; NotifyFd nfd; std::vector evs; public: MPMCQueueEventDriven(): - wait_sig(true) { - //fd(eventfd(0, EFD_NONBLOCK)) { - //if (fd == -1) throw SalticidaeError(SALTI_ERROR_FD); - } - ~MPMCQueueEventDriven() { - //close(fd); - unreg_handlers(); - } + wait_sig(true) {} + ~MPMCQueueEventDriven() { unreg_handlers(); } // this function is *NOT* thread-safe template void reg_handler(const EventContext &ec, Func &&func) { FdEvent ev(ec, nfd.read_fd(), [this, func=std::forward(func)](int, int) { - //fprintf(stderr, "%x\n", std::this_thread::get_id()); - uint64_t t; - //if (read(fd, &t, 8) != 8) return; if (!nfd.reset()) return; // only one consumer should be here a a time wait_sig.exchange(true, std::memory_order_acq_rel); if (func(*this)) - //write(fd, &dummy, 8); nfd.notify(); }); ev.add(FdEvent::READ); @@ -701,7 +681,6 @@ class MPMCQueueEventDriven: public MPMCQueue { if (wait_sig.exchange(false, std::memory_order_acq_rel)) { //SALTICIDAE_LOG_DEBUG("mpmc notify"); - //write(fd, &dummy, 8); nfd.notify(); } return true; -- cgit v1.2.3-70-g09d2