#pragma once #include <stdint.h> #include "../time/time.hpp" #include "../collections/Vector.hpp" #include <atomic> #ifndef SPRAWL_EVENT_MAX_MULTIPLE_WAIT # define SPRAWL_EVENT_MAX_MULTIPLE_WAIT 256 #endif #if defined(_WIN32) # include <Windows.h> typedef HANDLE EventType; #elif defined(__APPLE__) struct EventType { intptr_t ident; int queue; }; #else typedef int EventType; #endif namespace sprawl { namespace threading { class Event; } } class sprawl::threading::Event { public: typedef collections::Vector<Event const*> EventGroup; Event(); ~Event(); void Notify() const; void Wait() const; bool WaitFor(int64_t nanoseconds) const; bool WaitUntil(int64_t nanosecondTimestamp) const { return WaitFor(nanosecondTimestamp - sprawl::time::Now(sprawl::time::Resolution::Nanoseconds)); } static Event const* WaitAny(EventGroup const& values); static Event const* WaitAnyFor(EventGroup const& values, int64_t nanoseconds); static Event const* WaitAnyUntil(EventGroup const& values, int64_t nanosecondTimestamp) { return WaitAnyFor(values, nanosecondTimestamp - sprawl::time::Now(sprawl::time::Resolution::Nanoseconds)); } static void WaitAll(EventGroup const& values); static bool WaitAllFor(EventGroup const& values, int64_t nanoseconds); static bool WaitAllUntil(EventGroup const& values, int64_t nanosecondTimestamp) { return WaitAllFor(values, nanosecondTimestamp - sprawl::time::Now(sprawl::time::Resolution::Nanoseconds)); } static void NotifyAll(EventGroup const& values) { for(auto& value : values) { value->Notify(); } } static void NotifyAll(Event& event) { event.Notify(); } template<typename... Params> static void NotifyAll(Event& event, Params&... params) { event.Notify(); NotifyAll(params...); } // Template wrappers static Event const* WaitAny(EventGroup& values, Event const& ev) { values.PushBack(&ev); return WaitAny(values); } template<typename... Params> static Event const* WaitAny(EventGroup& values, Event const& ev, Params const&... params) { values.PushBack(&ev); return WaitAny(values, params...); } template<typename... Params> static Event const* WaitAny(Event const& ev, Params const&... params) { EventGroup values(sprawl::collections::Capacity(sizeof...(Params))); values.PushBack(&ev); return WaitAny(values, params...); } static Event const* WaitAnyFor(EventGroup& values, Event const& ev, int64_t nanoseconds) { values.PushBack(&ev); return WaitAnyFor(values, nanoseconds); } template<typename... Params> static Event const* WaitAnyFor(EventGroup& values, Event const& ev, Params const&... params) { values.PushBack(&ev); return WaitAnyFor(values, params...); } template<typename... Params> static Event const* WaitAnyFor(Event const& ev, Params const&... params) { EventGroup values(sprawl::collections::Capacity(sizeof...(Params))); values.PushBack(&ev); return WaitAnyFor(values, params...); } static Event const* WaitAnyUntil(EventGroup& values, Event const& ev, int64_t nanosecondTimestamp) { values.PushBack(&ev); return WaitAnyUntil(values, nanosecondTimestamp); } template<typename... Params> static Event const* WaitAnyUntil(EventGroup& values, Event const& ev, Params const&... params) { values.PushBack(&ev); return WaitAnyUntil(values, params...); } template<typename... Params> static Event const* WaitAnyUntil(Event const& ev, Params const&... params) { EventGroup values(sprawl::collections::Capacity(sizeof...(Params))); values.PushBack(&ev); return WaitAnyUntil(values, params...); } static void WaitAll(EventGroup& values, Event const& ev) { values.PushBack(&ev); WaitAll(values); } template<typename... Params> static void WaitAll(EventGroup& values, Event const& ev, Params const&... params) { values.PushBack(&ev); WaitAll(values, params...); } template<typename... Params> static void WaitAll(Event const& ev, Params const&... params) { EventGroup values(sprawl::collections::Capacity(sizeof...(Params))); values.PushBack(&ev); WaitAll(values, params...); } static bool WaitAllFor(EventGroup& values, Event const& ev, int64_t nanoseconds) { values.PushBack(&ev); return WaitAllFor(values, nanoseconds); } template<typename... Params> static bool WaitAllFor(EventGroup& values, Event const& ev, Params const&... params) { values.PushBack(&ev); return WaitAllFor(values, params...); } template<typename... Params> static bool WaitAllFor(Event const& ev, Params const&... params) { EventGroup values(sprawl::collections::Capacity(sizeof...(Params))); values.PushBack(&ev); return WaitAllFor(values, params...); } static bool WaitAllUntil(EventGroup& values, Event const& ev, int64_t nanosecondTimestamp) { values.PushBack(&ev); return WaitAllUntil(values, nanosecondTimestamp); } template<typename... Params> static bool WaitAllUntil(EventGroup& values, Event const& ev, Params const&... params) { values.PushBack(&ev); return WaitAllUntil(values, params...); } template<typename... Params> static bool WaitAllUntil(Event const& ev, Params const&... params) { EventGroup values(sprawl::collections::Capacity(sizeof...(Params))); values.PushBack(&ev); return WaitAllUntil(values, params...); } EventType m_event; };
# | 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 |