#include "event.hpp" sprawl::threading::Event::Event() : m_event(CreateEvent(nullptr, false, false, nullptr)) { } sprawl::threading::Event::~Event() { CloseHandle(m_event); } void sprawl::threading::Event::Notify() const { SetEvent(m_event); } void sprawl::threading::Event::Wait() const { WaitForSingleObject(m_event, INFINITE); } bool sprawl::threading::Event::WaitFor(int64_t nanoseconds) const { if(nanoseconds < 0) { return false; } DWORD ret = WaitForSingleObject(m_event, time::Convert(nanoseconds, time::Resolution::Nanoseconds, time::Resolution::Milliseconds)); return ret == WAIT_OBJECT_0; } /*static*/ sprawl::threading::Event const* sprawl::threading::Event::WaitAny(EventGroup const& values) { HANDLE events[SPRAWL_EVENT_MAX_MULTIPLE_WAIT]; for(int i = 0; i < values.Size(); ++i) { events[i] = values[i]->m_event; } DWORD ret = WaitForMultipleObjects(values.Size(), events, false, INFINITE); if(ret < WAIT_OBJECT_0 || ret >= WAIT_OBJECT_0 + values.Size()) { return nullptr; } return values[ret - WAIT_OBJECT_0]; } /*static*/ sprawl::threading::Event const* sprawl::threading::Event::WaitAnyFor(EventGroup const& values, int64_t nanoseconds) { HANDLE events[SPRAWL_EVENT_MAX_MULTIPLE_WAIT]; for(int i = 0; i < values.Size(); ++i) { events[i] = values[i]->m_event; } DWORD ret = WaitForMultipleObjects(values.Size(), events, false, time::Convert(nanoseconds, time::Resolution::Nanoseconds, time::Resolution::Milliseconds)); if(ret < WAIT_OBJECT_0 || ret >= WAIT_OBJECT_0 + values.Size()) { return nullptr; } return values[ret - WAIT_OBJECT_0]; } /*static*/ void sprawl::threading::Event::WaitAll(EventGroup const& values) { HANDLE events[SPRAWL_EVENT_MAX_MULTIPLE_WAIT]; for(int i = 0; i < values.Size(); ++i) { events[i] = values[i]->m_event; } DWORD ret = WaitForMultipleObjects(values.Size(), events, true, INFINITE); } /*static*/ bool sprawl::threading::Event::WaitAllFor(EventGroup const& values, int64_t nanoseconds) { HANDLE events[SPRAWL_EVENT_MAX_MULTIPLE_WAIT]; for(int i = 0; i < values.Size(); ++i) { events[i] = values[i]->m_event; } DWORD ret = WaitForMultipleObjects(values.Size(), events, true, time::Convert(nanoseconds, time::Resolution::Nanoseconds, time::Resolution::Milliseconds)); if(ret < WAIT_OBJECT_0 || ret >= WAIT_OBJECT_0 + values.Size()) { return false; } return true; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 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 |
||
#2 | 16081 | ShadauxCat |
Addressing tasks from previous review. #review-16082 |
||
#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 |