Evo C++ Library v0.5.1
Public Types | Public Member Functions | Static Public Attributes | List of all members
EventQueue< T > Class Template Reference

#include <evo/event.h>

Detailed Description

template<class T = Event>
class evo::EventQueue< T >

Lock-free event processing queue.

Template Parameters
TEvent type to use – must be Event or inherit from it
  • Concrete types have slightly better performace (inlining, no vtable looups)
Example
#include <evo/event.h>
using namespace evo;
struct MyEvent : Event {
bool operator()() {
// ...
return true;
}
};
int main() {
EventQueue<> queue;
// Add an event to queue
queue.add(new MyEvent);
// Process queue (single consumer)
queue.process();
return 0;
}

Public Types

typedef T EventT
 Event type used More...
 
typedef uint Size
 Queue size type. More...
 

Public Member Functions

 EventQueue (Size size=DEFAULT_SIZE)
 Constructor. More...
 
 ~EventQueue ()
 Destructor. More...
 
void add (T *event, ulongl spinwait_ns=1)
 Add an event to queue. More...
 
template<class U >
void notify_multiwait (U &condmutex)
 Notify an item has been added with multiple consumer threads. More...
 
bool process ()
 Process queued events and return. More...
 
template<class U >
bool process_multi (U &mutex)
 Process queued events and return, allowing multiple consumer threads. More...
 
template<class U >
void process_multiwait (U &condmutex, AtomicInt &stopflag, ulong waitms=1)
 Process queued events until stopflag is set, allowing multiple consumer threads, and waiting with condmutex while idle. More...
 

Static Public Attributes

static const Size DEFAULT_SIZE = 256
 Default queue size. More...
 

Member Typedef Documentation

◆ EventT

typedef T EventT

Event type used

◆ Size

typedef uint Size

Queue size type.

Constructor & Destructor Documentation

◆ EventQueue()

EventQueue ( Size  size = DEFAULT_SIZE)
inline

Constructor.

Parameters
sizeQueue size, rounded up to nearest power of 2

◆ ~EventQueue()

~EventQueue ( )
inline

Destructor.

  • Queue should be empty, otherwise incomplete events can leak memory or have undefined behavior

Member Function Documentation

◆ add()

void add ( T *  event,
ulongl  spinwait_ns = 1 
)
inline

Add an event to queue.

  • This takes ownership of the event pointer, and will free it once the event is completed (via C++ delete operator)
    • The event is only freed if it returns true, otherwise it's assumed that ownership was transferred elsewhere
  • This blocks while queue is full (spin-wait with 1 nanosecond sleep) – a full queue should be avoided
  • If this is a multi-consumer queue using process_multiwait(), call notify_multiwait() after this
  • Caution: When events are processed on the same thread, do not call from the same queue that invoked (called) the event, this will deadlock if the queue is full
Parameters
eventEvent pointer to add and take ownership of
spinwait_nsSpin-wait sleep time in nanoseconds (usually default is preferred) – used to sleep each loop while spin waiting

◆ notify_multiwait()

void notify_multiwait ( U &  condmutex)
inline

Notify an item has been added with multiple consumer threads.

Template Parameters
UConditio/Mutex type, inferred from argument
Parameters
condmutexCondition/mutex to use

◆ process()

bool process ( )
inline

Process queued events and return.

  • This pops and invokes (calls) all queued events
  • Popped events that return true are freed, otherwise they are detached (owned elsewhere)
  • Caution: Only 1 thread may call this at a time, otherwise results are undefined
Returns
Whether any events were processed

◆ process_multi()

bool process_multi ( U &  mutex)
inline

Process queued events and return, allowing multiple consumer threads.

  • This locks the mutex while extracting the next event, and unlocks it while invoking (calling) the event (giving the queue to the next consumer)
  • Caution: Do not mix with process() on the same instance
Template Parameters
UMutex or Condition type, inferred from argument
Parameters
mutexMutex or Condition object to lock while extracting next event from queue
Returns
Whether any events were processed

◆ process_multiwait()

void process_multiwait ( U &  condmutex,
AtomicInt stopflag,
ulong  waitms = 1 
)
inline

Process queued events until stopflag is set, allowing multiple consumer threads, and waiting with condmutex while idle.

  • This locks the mutex while extracting the next event, and unlocks it while invoking (calling) the event (giving the queue to the next consumer)
  • While queue is empty this waits on condmutex using a timeout of waitms
  • Call notify_multiwait() after adding an event to try to wake up a consumer
  • Caution: Do not mix with process() or process_multi() on the same instance
Template Parameters
UCondition/Mutex type, inferred from argument
Parameters
condmutexCondition/mutex object to lock while extracting next event from queue
stopflagFlag to stop processing when set to non-zero
waitmsMax wait time in milliseconds, 0 for none (spin-wait, not recommended)

Member Data Documentation

◆ DEFAULT_SIZE

const Size DEFAULT_SIZE = 256
static

Default queue size.


The documentation for this class was generated from the following file: