8 #ifndef INCL_evo_event_h 9 #define INCL_evo_event_h 13 #if defined(EVO_CPP11) 52 #if defined(EVO_CPP11) 61 typedef std::function<bool()>
Lambda;
80 lambda_ = src.lambda_;
145 template<
class T=Event>
151 static const Size DEFAULT_SIZE = 256;
157 ringbuf_size_ = adjust_size(size);
158 ringbuf_ =
new T*[ringbuf_size_];
159 ringbuf_size_mask_ = ringbuf_size_ - 1;
160 memset(ringbuf_, 0,
sizeof(T*) * ringbuf_size_);
169 if (read_pos_.load() <= cursor_pos_.load()) {
185 void add(T* event, ulongl spinwait_ns=1) {
193 ringbuf_[seq & ringbuf_size_mask_] = event;
197 const uint64 prev_seq = seq - 1;
212 if (condmutex.trylock()) {
230 event = ringbuf_[seq & ringbuf_size_mask_];
235 return (seq > start);
249 uint64 seq, count = 0;
250 typename U::Lock lock(mutex);
253 event = ringbuf_[seq & ringbuf_size_mask_];
280 typename U::Lock lock(condmutex);
284 event = ringbuf_[seq & ringbuf_size_mask_];
294 condmutex.wait(waitms,
true);
307 Size ringbuf_size_mask_;
315 static Size adjust_size(Size size) {
316 const Size MIN_SIZE = 16;
318 if (size <= MIN_SIZE)
320 else if (size >= MAX_SIZE)
T & max(T &a, T &b)
Returns highest of given values.
Definition: alg.h:47
EventLambda & operator=(const EventLambda &src)
Assignment operator.
Definition: event.h:79
#define EVO_ATOMIC_ACQ_REL
Combined "acquire" & "release" level memory barrier.
Definition: atomic.h:33
virtual bool operator()()=0
Event function.
void notify_multiwait(U &condmutex)
Notify an item has been added with multiple consumer threads.
Definition: event.h:211
Implement Event using a lambda function (C++11).
Definition: event.h:59
#define EVO_ATOMIC_FENCE(MEM_ORDER)
Sets a memory fence/barrier.
Definition: atomic.h:52
T next_pow2(T v)
Get next power of 2 equal to or greater than given number.
Definition: type.h:1932
bool operator()()
Event function.
Definition: event.h:85
#define EVO_ATOMIC_RELAXED
Relaxed memory ordering, used between start/end memory barriers.
Definition: atomic.h:21
void process_multiwait(U &condmutex, AtomicInt &stopflag, ulong waitms=1)
Process queued events until stopflag is set, allowing multiple consumer threads, and waiting with con...
Definition: event.h:277
virtual ~Event()
Destructor.
Definition: event.h:37
bool process_multi(U &mutex)
Process queued events and return, allowing multiple consumer threads.
Definition: event.h:247
#define EVO_ATOMIC_ACQUIRE
Start "acquire" memory ordering barrier, usually followed by a matching "release" barrier...
Definition: atomic.h:27
uint Size
Queue size type.
Definition: event.h:149
Event()
Constructor.
Definition: event.h:33
T EventT
Event type used
Definition: event.h:148
Evo C++ Library namespace.
Definition: alg.h:11
#define EVO_ATOMIC_RELEASE
Release (end) memory ordering barrier started with "consume" or "acquire" barrier.
Definition: atomic.h:30
Event base type used with EventQueue.
Definition: event.h:31
bool sleepns(ulongl nsec)
Sleep for number of nanoseconds.
Definition: sys.h:706
void add(T *event, ulongl spinwait_ns=1)
Add an event to queue.
Definition: event.h:185
bool process()
Process queued events and return.
Definition: event.h:225
~EventQueue()
Destructor.
Definition: event.h:168
EventLambda(const EventLambda &src)
Copy constructor.
Definition: event.h:72
EventLambda(const Lambda &lambda)
Constructor.
Definition: event.h:66
EventQueue(Size size=DEFAULT_SIZE)
Constructor.
Definition: event.h:156
T load(MemOrder mem_order=std::memory_order_seq_cst) const
Load and return current value.
std::function< bool()> Lambda
Lambda function type for Event.
Definition: event.h:61
Lock-free event processing queue.
Definition: event.h:146