Evo C++ Library v0.5.1
event_thread.h
Go to the documentation of this file.
1 // Evo C++ Library
2 /* Copyright 2019 Justin Crowell
3 Distributed under the BSD 2-Clause License -- see included file LICENSE.txt for details.
4 */
6 
7 #pragma once
8 #ifndef INCL_evo_event_thread_h
9 #define INCL_evo_event_thread_h
10 
11 #include "event.h"
12 #include "thread.h"
13 
14 namespace evo {
17 
19 
25  ulong waitms;
26 };
27 
62 class EventThreadPool : public ThreadGroup<Thread,EventThreadState> {
63 public:
65 
69  EventThreadPool(ulong wait_timeout_ms=1) : ThreadGroup<Thread,EventThreadState>(thread_run) {
70  shared_state.waitms = wait_timeout_ms;
71  }
72 
83  This& add(Event* event, ulongl spinwait_ns=1) {
84  shared_state.queue.add(event, spinwait_ns);
85  shared_state.queue.notify_multiwait(shared_state.condmutex);
86  return *this;
87  }
88 
95  This& shutdown() {
96  shared_state.shutdown.store(1, EVO_ATOMIC_RELEASE);
97  shared_state.condmutex.lock_notify_all();
98  return *this;
99  }
100 
101 private:
103 
104  Base& cancel() EVO_ONCPP11(= delete);
105  bool cancelled() const EVO_ONCPP11(= delete);
106 
107  static void thread_run(void* arg) {
108  EventThreadState& state = *(EventThreadState*)arg;
109  const ulong waitms = state.waitms;
110  while (state.shutdown.load(EVO_ATOMIC_ACQUIRE) == 0)
111  state.queue.process_multiwait(state.condmutex, state.shutdown, waitms);
112  while (state.queue.process_multi(state.condmutex));
113  }
114 };
115 
117 
118 }
119 #endif
ulong waitms
Thread wait timeout in milliseconds.
Definition: event_thread.h:25
This & shutdown()
Stop processing events and shutdown threads.
Definition: event_thread.h:95
This & add(Event *event, ulongl spinwait_ns=1)
Add an event to queue to be processed.
Definition: event_thread.h:83
AtomicInt shutdown
Thread shutdown flag.
Definition: event_thread.h:24
Manages a single thread of execution.
Definition: thread.h:529
#define EVO_ONCPP11(EXPR)
Compile EXPR only if C++11 support is detected, otherwise this is a no-op.
Definition: sys.h:259
Condition object for thread synchronization.
Definition: thread.h:243
Evo threads implementation.
EventThreadPool This
This type.
Definition: event_thread.h:64
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
EventQueue queue
EventQueue used by pool.
Definition: event_thread.h:22
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
Internal state used by EventThreadPool.
Definition: event_thread.h:21
Evo C++ Library namespace.
Definition: alg.h:11
Manages a group of threads with shared state.
Definition: thread.h:1104
EventThreadPool(ulong wait_timeout_ms=1)
Constructor.
Definition: event_thread.h:69
#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
Evo async event handling.
Event processing thread pool.
Definition: event_thread.h:62
Condition condmutex
Condition for multithreading.
Definition: event_thread.h:23
T load(MemOrder mem_order=std::memory_order_seq_cst) const
Load and return current value.
Lock-free event processing queue.
Definition: event.h:146