#include "event.hpp" #include <sys/eventfd.h> #include <unistd.h> #include <sys/select.h> sprawl::threading::Event::Event() : m_event(eventfd(0, 0)) { } sprawl::threading::Event::~Event() { close(m_event); } void sprawl::threading::Event::Notify() const { uint64_t const i = 1; write(m_event, &i, sizeof(uint64_t)); } void sprawl::threading::Event::Wait() const { uint64_t i; read(m_event, &i, sizeof(uint64_t)); } bool sprawl::threading::Event::WaitFor(int64_t nanoseconds) const { if(nanoseconds < 0) { return false; } fd_set inSet; FD_ZERO(&inSet); FD_SET(m_event, &inSet); int max = m_event; struct timeval tv; int64_t sec = sprawl::time::Convert(nanoseconds, sprawl::time::Resolution::Nanoseconds, sprawl::time::Resolution::Seconds); int64_t usec = sprawl::time::Convert(nanoseconds, sprawl::time::Resolution::Nanoseconds, sprawl::time::Resolution::Microseconds) - sprawl::time::Convert(sec, sprawl::time::Resolution::Seconds, sprawl::time::Resolution::Microseconds); tv.tv_sec = time_t(sec); tv.tv_usec = suseconds_t(usec); select(max + 1, &inSet, nullptr, nullptr, &tv); if(FD_ISSET(m_event, &inSet)) { uint64_t i; read(m_event, &i, sizeof(uint64_t)); return true; } return false; } /*static*/ sprawl::threading::Event const* sprawl::threading::Event::WaitAny(EventGroup const& values) { fd_set inSet; FD_ZERO(&inSet); int max = 0; for(auto& event : values) { FD_SET(event->m_event, &inSet); max = event->m_event > max ? event->m_event : max; } select(max + 1, &inSet, nullptr, nullptr, nullptr); Event const* ret = nullptr; for(auto& event : values) { if(FD_ISSET(event->m_event, &inSet)) { if(ret == nullptr) { ret = event; } //Clear the signaled state. event->Wait(); } } return ret; } /*static*/ sprawl::threading::Event const* sprawl::threading::Event::WaitAnyFor(EventGroup const& values, int64_t nanoseconds) { fd_set inSet; FD_ZERO(&inSet); int max = 0; for(auto& event : values) { FD_SET(event->m_event, &inSet); max = event->m_event > max ? event->m_event : max; } struct timeval tv; int64_t sec = sprawl::time::Convert(nanoseconds, sprawl::time::Resolution::Nanoseconds, sprawl::time::Resolution::Seconds); int64_t usec = sprawl::time::Convert(nanoseconds, sprawl::time::Resolution::Nanoseconds, sprawl::time::Resolution::Microseconds) - sprawl::time::Convert(sec, sprawl::time::Resolution::Seconds, sprawl::time::Resolution::Microseconds); tv.tv_sec = time_t(sec); tv.tv_usec = suseconds_t(usec); select(max + 1, &inSet, nullptr, nullptr, &tv); Event const* ret = nullptr; for(auto& event : values) { if(FD_ISSET(event->m_event, &inSet)) { if(ret == nullptr) { ret = event; } //Clear the signaled state. event->Wait(); } } return ret; } /*static*/ void sprawl::threading::Event::WaitAll(EventGroup const& values) { fd_set inSet; FD_ZERO(&inSet); int max = 0; for(auto& event : values) { FD_SET(event->m_event, &inSet); max = event->m_event > max ? event->m_event : max; } ssize_t total = 0; while(total < values.Size()) { total += select(max + 1, &inSet, nullptr, nullptr, nullptr); fd_set set = inSet; FD_ZERO(&inSet); for(auto& event : values) { if(FD_ISSET(event->m_event, &set)) { //Clear the signaled state. event->Wait(); } else { FD_SET(event->m_event, &inSet); } } } } /*static*/ bool sprawl::threading::Event::WaitAllFor(EventGroup const& values, int64_t nanoseconds) { int64_t timeout_time = sprawl::time::Now() + nanoseconds; fd_set inSet; FD_ZERO(&inSet); int max = 0; for(auto& event : values) { FD_SET(event->m_event, &inSet); max = event->m_event > max ? event->m_event : max; } ssize_t total = 0; while(total < values.Size()) { // On Linux, tv is updated after select and we don't have to do this // But if this code is used on any other platform with select(), that behavior will probably break struct timeval tv; int64_t sec = sprawl::time::Convert(nanoseconds, sprawl::time::Resolution::Nanoseconds, sprawl::time::Resolution::Seconds); int64_t usec = sprawl::time::Convert(nanoseconds, sprawl::time::Resolution::Nanoseconds, sprawl::time::Resolution::Microseconds) - sprawl::time::Convert(sec, sprawl::time::Resolution::Seconds, sprawl::time::Resolution::Microseconds); tv.tv_sec = time_t(sec); tv.tv_usec = suseconds_t(usec); int ret = select(max + 1, &inSet, nullptr, nullptr, &tv); if(ret == 0) { return false; } total += ret; fd_set set = inSet; FD_ZERO(&inSet); for(auto& event : values) { if(FD_ISSET(event->m_event, &set)) { //Clear the signaled state. event->Wait(); } else { FD_SET(event->m_event, &inSet); } } nanoseconds = timeout_time - sprawl::time::Now(); } return true; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 23398 | ququlala | "Forking branch Mainline of shadauxcat-libsprawl to ququlala-libsprawl." | ||
//guest/ShadauxCat/Sprawl/Mainline/threading/event_linux.cpp | |||||
#2 | 16153 | ShadauxCat |
- Changed compile-time-bound strings to be sprawl::StringLiteral instead of sprawl::String. (Was going to do raw char*, but StringLiteral allows the length of the string to be baked in at compile time and thus avoids costly strlen() operations.) - Split Event::WaitMultiple() into Event::WaitAny() and Event::WaitAll() - Added Event::NotifyAll() - Added variadic template functions to make WaitAny(), WaitAll(), and NotifyAll() easier to work with when the list of events is known at compile time (i.e., Event::WaitAny(event1, event2, event3); to wait for all thre events instead of having to construct the EventGroup manually - also ensures EventGroup construction eficiency by constructing it with the proper capacity for the number of events being waited on) #review-16154 |
||
#1 | 16052 | ShadauxCat |
- Changed default block size for concurrent queue to a more reasonable value - Changed some memory orders to memory_order_seq_cst when they don't actually need to be that to get around a bug in visual studio 2013 - debug builds assert when memory_order_acq_rel is used for a compare_exchange_strong (this is a standard library bug and is fixed in VS2015) - Added Event API - events are an alternative to condition variables that do not require a mutex and are guaranteed not to miss any signals, even if the signal comes while the thread is not listening for it. Unlike condition variables, however, they do not support broadcasting (and in fact, in general, are not safe to use with multiple threads listening for the same event simultaneously - though notifying on the same event is fine) - Rewrote ThreadManager around ConcurrentQueue and Event API so it is now lock-free. Also improved some behaviors of the staged thread manager operation so it now supports tasks that can be run on multiple stages via a bitmask. - Fixed an issue where the Coroutine copy constructor was calling the std::function constructor instead and another where initializing with a stack might try to call the wrong constructor and vice-versa - Fixed Coroutine never calling munmap() on its stack in linux and causing a memory leak - Added default arguments to time functions - Attempted to fix some issues with BinaryTree. Fixed some but not all. It's currently not suitable for use, sadly. - Logging Improvements: - - Added thread ID to logging - - Fixed some issues with category handlers - - Added backtraces - - Added the following additional log macros: - - - LOG_IF - - - LOG_EVERY_N - - - LOG_FIRST_N - - - LOG_IF_EVERY_N - - - LOG_IF_FIRST_N - - - LOG_ASSERT - - Added the ability to set extra info callbacks to get data such as script backtraces - - Removed the thread-related handlers and replaced them with RunHandler_Threaded and RunHandler_ThreadManager, which will enable any passed-in handler to be run in a threaded fashion - Removed StaticPoolAllocator and renamed DynamicPoolAllocator to PoolAllocator; adjusted unit tests accordingly - PoolAllocator now allocates its pool with mmap and VirtualAlloc, rather than with malloc - Fixed a bug with Vector copy assignment operator - Improved performance of StringBuilder considerably for cases where there are no modifier strings - Removed Copy-On-Write behavior of JSONToken as it was broken; copies are now performed with explicit DeepCopy() and ShallowCopy() functions - Fixed some parser bugs with JSONToken - Added iteration to JSONToken to iterate its children - Fixed crash when reading a negative number of bytes from a file - Changed StringBuilder to favor speed instead of memory by default - Added some performance unit tests for JSON token #review-16053 |