8 #ifndef INCL_evo_thread_h 9 #define INCL_evo_thread_h 16 #if defined(EVO_CPP11) 23 #if defined(_MSC_VER) && !defined(EVO_CPP11) 25 #pragma warning(disable:4355) 143 assert( counter_ == 0 );
154 {
return write_mutex_.trylock(); }
167 {
return write_mutex_.trylock(timeout_ms); }
174 { write_mutex_.lock(); }
180 { write_mutex_.unlock(); }
191 if (read_mutex_.trylock()) {
192 if (counter_ > 0 || write_mutex_.trylock()) {
195 assert(counter_ > 0);
197 read_mutex_.unlock();
210 assert(counter_ > 0);
211 read_mutex_.unlock();
219 assert(counter_ > 0);
221 write_mutex_.unlock();
222 read_mutex_.unlock();
248 typedef CONDITION_VARIABLE
Handle;
250 static const ulong INF = INFINITE;
253 InitializeConditionVariable(&handle);
260 { InitializeConditionVariable(&handle); }
269 return (SleepConditionVariableCS(&handle, &mutex->
handle, timeout_ms) != 0);
273 { WakeConditionVariable(&handle); }
276 { WakeAllConditionVariable(&handle); }
282 static const ulong INF = ULONG_MAX;
321 return (pthread_cond_wait(&handle, &mutex->
handle) == 0);
327 #if defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME) && !defined(EVO_USE_GETTIMEOFDAY) 328 #if defined(CLOCK_MONOTONIC) && !defined(__APPLE__) 329 ::clock_gettime(CLOCK_MONOTONIC, &ts);
331 ::clock_gettime(CLOCK_REALTIME, &ts);
336 ::gettimeofday(&tv, NULL);
341 return (pthread_cond_timedwait(&handle, &mutex->
handle, &ts) == 0);
350 { pthread_cond_signal(&handle); }
357 { pthread_cond_broadcast(&handle); }
362 #if defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME) && !defined(EVO_USE_GETTIMEOFDAY) && !defined(__APPLE__) 364 #if defined(CLOCK_MONOTONIC) 365 id = CLOCK_MONOTONIC;
369 pthread_condattr_t attr;
370 pthread_condattr_init(&attr);
371 pthread_condattr_setclock(&attr,
id);
372 pthread_cond_init(&handle, &attr);
374 pthread_cond_init(&handle, NULL);
419 {
return mutex->
trylock(timeout_ms); }
439 const bool result = wait(timeout_ms,
false);
478 template<
class T>
struct BasicSmartPtr {
481 BasicSmartPtr(T* ptr=NULL) : ptr(ptr)
491 BasicSmartPtr(
const BasicSmartPtr&);
492 BasicSmartPtr& operator=(
const BasicSmartPtr&);
530 #if defined(EVO_CPP11) 531 typedef std::function<void (void*)>
Func;
533 typedef void (*Func)(
void*);
544 Init(Func func,
void* arg=NULL) : func(func), arg(arg)
547 Init(
const Init& src) : func(src.func), arg(src.arg)
561 Thread(Func func,
void* arg=NULL) : thread_init(func, arg), thread_active_(false)
567 Thread(
const Init& init) : thread_init(init), thread_active_(false)
581 {
return thread_impl_.handle; }
589 {
return thread_active_; }
597 if (!thread_active_ && thread_init.
func != NULL) {
598 impl::BasicSmartPtr<Init> init(
new Init(thread_init));
599 if (thread_impl_.start(Thread::thread_handler, init.ptr) ==
ENone) {
602 thread_active_ =
true;
614 if (thread_active_) {
615 thread_impl_.detach();
616 thread_active_ =
false;
627 if (thread_active_ && thread_impl_.join()) {
628 thread_active_ =
false;
653 if (init_ptr != NULL) {
656 if (init.
func != NULL)
732 thread_cancel_flag_.store(1);
748 {
return thread_cancel(locked).thread_join(); }
756 virtual void thread_run() = 0;
766 return (thread_cancel_flag_.load() != 0);
782 static void thread_base_handler(
void* ptr)
825 template<
class T=Thread>
831 { this->thread_start(); }
841 {
return this->thread_handle(); }
847 {
return this->thread_active(); }
855 {
return this->thread_start(); }
866 { this->thread_cancel(locked);
return *
this; }
875 {
return this->thread_cancel(locked).thread_join(); }
883 {
return this->thread_join(); }
932 { this->thread_start(); }
936 { this->thread_join(); }
942 {
return this->thread_handle(); }
948 {
return this->thread_active(); }
956 {
return this->thread_start(); }
962 {
return this->thread_join(); }
969 {
return this->thread_detach(); }
983 template<
class S,
class T>
984 struct ThreadGroupNode {
985 typedef ThreadGroupNode<S,T> Node;
986 static const bool THREAD_CLASS =
true;
993 ThreadGroupNode(
Thread::Init&, S& state, Node* prev_node=NULL) : thread(state) {
1000 { thread.thread_cancel(); }
1005 struct ThreadGroupNode<S,Thread> {
1006 typedef ThreadGroupNode<S,Thread> Node;
1007 static const bool THREAD_CLASS =
false;
1014 ThreadGroupNode(
Thread::Init& init, S& state, Node* prev_node=NULL) : thread(init) {
1103 template<
class T,
class S=
typename T::SharedState,
class M=MutexInert>
1113 first_ = last_ = NULL;
1115 cancel_flag_ =
false;
1123 first_ = last_ = NULL;
1125 cancel_flag_ =
false;
1170 if (count > 0 && !cancel_flag_ && (Node::THREAD_CLASS || thread_init.func != NULL)) {
1172 if (first_ == NULL) {
1173 first_ = last_ =
new Node(thread_init, shared_state);
1174 last_->thread.thread_start();
1180 last_->next =
new Node(thread_init, shared_state, last_);
1181 last_ = last_->next;
1182 last_->thread.thread_start();
1198 if (Node::THREAD_CLASS && active_ && !cancel_flag_) {
1199 cancel_flag_ =
true;
1200 Node* node = first_;
1201 while (node != NULL) {
1215 Node* node = first_;
1216 while (node != NULL) {
1217 Node* next = node->next;
1219 node->thread.thread_join();
1224 first_ = last_ = NULL;
1225 cancel_flag_ =
false;
1235 typedef impl::ThreadGroupNode<S,T>
Node;
1247 This& operator=(
const This&);
1252 #if EVO_ATOMIC_SAFE_STATICS == 1 || defined(DOXYGEN) 1267 static AtomicSmartPtr static_ptr;
1281 struct AtomicSmartPtr :
public Atomic<T*> {
1286 T* ptr = this->load();
1297 #if defined(_MSC_VER) && !defined(EVO_CPP11) 1298 #pragma warning(pop) SleepLock()
Constructor.
Definition: thread.h:80
~ThreadScope()
Destructor cancels and joins thread.
Definition: thread.h:834
bool thread_active() const
Get whether thread is active (running).
Definition: thread.h:588
ThreadScope< T > This
Current ThreadScope.
Definition: thread.h:827
Evo smart locks for synchronization.
Atomic signalling flag.
Definition: atomic.h:172
S SharedState
Thread shared state
Definition: thread.h:1107
MutexRW()
Constructor.
Definition: thread.h:138
static void yield()
Yield control to another thread or process.
Definition: thread.h:635
Func func
Thread function to use
Definition: thread.h:538
virtual ~Thread()
Destructor.
Definition: thread.h:574
bool start()
Start thread.
Definition: thread.h:854
M MutexT
Mutex type
Definition: thread.h:1108
SmartLock< Mutex > Lock
Lock object type – see SmartLock.
Definition: thread.h:105
Smart sleep-locking for synchronization.
Definition: lock.h:146
static ulong id()
Definition: systhread.h:139
Node * first_
Definition: thread.h:1238
Definition: systhread.h:165
void lock_notify()
Lock associated mutex, call notify(), then unlock.
Definition: thread.h:447
void unlock()
Write-Unlock mutex.
Definition: thread.h:179
This & cancel()
Set cancel flags to signal all threads to stop.
Definition: thread.h:1196
#define EVO_THREAD_RUN_DEFINE(NAME, PARAM_NAME)
Used to define a thread run function used with SysThread (used internally).
Definition: systhread.h:40
Manages a single thread of execution.
Definition: thread.h:529
SysThread::Handle handle() const
Get platform-specific thread handle.
Definition: thread.h:840
virtual ~ThreadClass()
Destructor.
Definition: thread.h:717
bool wait_inf(bool locked=true)
Wait for notification.
Definition: thread.h:394
bool trylock(ulong timeout_ms)
Try to Write-Lock mutex with a timeout.
Definition: thread.h:166
~Condition()
Destructor.
Definition: thread.h:382
Mutex * mutex
Pointer to associated mutex, never NULL – do not modify.
Definition: thread.h:463
bool active_
Definition: thread.h:1240
bool start()
Start thread.
Definition: thread.h:955
Read/Write Mutex for thread synchronization.
Definition: thread.h:132
ThreadScope(Thread::Func func, void *arg=NULL)
Constructor starts thread.
Definition: thread.h:931
bool active() const
Get whether thread group is active (threads running).
Definition: thread.h:1142
Handle handle
Condition object handle – do not modify.
Definition: thread.h:462
bool wait(ulong timeout_ms=Condition::INF, bool locked=true)
Wait for notification or timeout.
Definition: thread.h:317
Handle handle
Mutex handle – do not modify.
Definition: systhread.h:293
~MutexRW()
Destructor.
Definition: thread.h:142
Condition object for thread synchronization.
Definition: thread.h:243
Init(const Init &src)
Definition: thread.h:547
ThreadClass()
Constructor.
Definition: thread.h:710
bool cancel_check()
Check if thread has been cancelled.
Definition: thread.h:764
Evo system threads implementation.
Mutex for thread synchronization.
Definition: thread.h:104
Thread()
Constructor.
Definition: thread.h:554
SmartSleepLock< AtomicFlag > Lock
Sleep-Lock object type – see SmartSleepLock.
Definition: thread.h:77
#define EVO_THREAD_RUN_RETURN
Used to return from a thread run function used with SysThread (used internally).
Definition: systhread.h:45
ThreadGroup()
Default constructor.
Definition: thread.h:1111
#define EVO_ATOMIC_RELAXED
Relaxed memory ordering, used between start/end memory barriers.
Definition: atomic.h:21
SysThread::Handle handle() const
Get platform-specific thread handle.
Definition: thread.h:941
Condition condmutex
Condition and mutex object for thread, used to wake up thread.
Definition: thread.h:707
No error.
Definition: sys.h:1115
Evo inert synchronization implementation.
bool detach()
Detach thread to run in background.
Definition: thread.h:968
Creates a single thread of execution in given scope.
Definition: thread.h:826
SmartLock< AtomicFlag > Lock
Lock object type – see SmartLock.
Definition: thread.h:55
bool join()
Join thread by waiting for thread to stop.
Definition: thread.h:882
ThreadGroup< T, S, M > This
This type.
Definition: thread.h:1105
void notify()
Notify and wake a waiting thread.
Definition: thread.h:349
SmartLock< MutexRW > Lock
Write Lock object type, general Mutex interface (Mutex::Lock will also work) – see SmartLock...
Definition: thread.h:133
bool join()
Join all threads by waiting for them to stop.
Definition: thread.h:1212
bool active() const
Get whether thread is active (running).
Definition: thread.h:947
bool cancel_join(bool locked=false)
Cancels and joins thread.
Definition: thread.h:874
ulong get_tid()
Get current thread ID from system.
Definition: thread.h:41
bool thread_join()
Join thread by waiting for thread to stop.
Definition: thread.h:626
void lock()
Lock associated mutex.
Definition: thread.h:425
Thread::Init ThreadInit
Thread init type
Definition: thread.h:1106
void lock()
Write-Lock mutex.
Definition: thread.h:173
bool join()
Join thread by waiting for thread to stop.
Definition: thread.h:961
Smart locking for synchronization.
Definition: lock.h:28
static void yield()
Definition: systhread.h:129
SmartLock< MutexRW > LockWrite
Write Lock object type – see SmartLock.
Definition: thread.h:134
virtual ~ThreadGroup()
Destructor.
Definition: thread.h:1131
SmartSleepLock< AtomicFlag > SleepLock
Sleep-Lock object type – see SmartSleepLock.
Definition: thread.h:56
bool trylock()
Try to lock associated mutex, fail if already locked (non-blocking).
Definition: thread.h:406
void lock_read()
Read-Lock mutex.
Definition: thread.h:206
Init(Func func, void *arg=NULL)
Definition: thread.h:544
void lock_notify_all()
Lock associated mutex, call notify_all(), then unlock.
Definition: thread.h:456
bool trylock()
Try to Write-Lock mutex without blocking.
Definition: thread.h:153
ThreadClass & thread_cancel(bool locked=false)
Set cancel flag to signal thread to stop, and wake thread via condition object.
Definition: thread.h:730
Inert lock that doesn't do anything.
Definition: thread_inert.h:27
void * arg
Argument for thread function.
Definition: thread.h:539
static void set_timespec_tv(struct timespec &tm, struct timeval &tv)
Definition: sys.h:1533
SmartSleepLock & lock(ulong sleep_ms=1)
Lock object, if not already locked by this.
Definition: lock.h:169
MutexT mutex_
Definition: thread.h:1242
static void add_timespec_ms(struct timespec &tm, ulong ms)
Definition: sys.h:1546
Initialization structure for starting a function-based thread.
Definition: thread.h:537
static T * instance()
Get singleton instance.
Definition: thread.h:1265
bool trylock(ulong timeout_ms)
Try to lock associated mutex with a timeout.
Definition: thread.h:418
ulong size() const
Get thread group size.
Definition: thread.h:1157
Thread(const Init &init)
Constructor for function-based thread.
Definition: thread.h:567
pthread_cond_t Handle
Definition: thread.h:280
Node * last_
Definition: thread.h:1239
void lock()
Spin-lock flag.
Definition: atomic.h:234
void unlock()
Unlock associated mutex.
Definition: thread.h:431
std::function< void(void *)> Func
Thread function type – with C++11 supports lambda/functor, otherwise just function pointer ...
Definition: thread.h:531
bool active() const
Get whether thread is active (running).
Definition: thread.h:846
Evo C++ Library namespace.
Definition: alg.h:11
Manages a group of threads with shared state.
Definition: thread.h:1104
Thread(Func func, void *arg=NULL)
Constructor for function-based thread.
Definition: thread.h:561
#define EVO_ATOMIC_RELEASE
Release (end) memory ordering barrier started with "consume" or "acquire" barrier.
Definition: atomic.h:30
Spin-lock for thread synchronization.
Definition: thread.h:54
virtual bool thread_detach()
Detach thread to run in background.
Definition: thread.h:613
bool trylock_read()
Try to Read-Lock mutex without blocking.
Definition: thread.h:189
bool lock_wait(ulong timeout_ms=Condition::INF)
Lock associated mutex, call wait(), then unlock.
Definition: thread.h:438
pthread_t Handle
Definition: systhread.h:97
SpinLock()
Constructor.
Definition: thread.h:59
bool owned
Whether associated mutex is owned by this – do not modify.
Definition: thread.h:464
ulong size_
Definition: thread.h:1237
void unlock_read()
Read-Unlock mutex.
Definition: thread.h:217
bool thread_start()
Start thread.
Definition: thread.h:596
Base class for managing a single class-based thread of execution.
Definition: thread.h:706
Manages a singleton instance for given type.
Definition: thread.h:1261
ThreadScope< Thread > This
Definition: thread.h:925
void unlock()
Unlock mutex.
Definition: systhread.h:288
~ThreadScope()
Destructor joins thread.
Definition: thread.h:935
ThreadScope()
Constructor starts thread.
Definition: thread.h:830
Condition()
Default constructor.
Definition: thread.h:287
SmartLockRead< MutexRW > LockRead
Read Lock object type – see SmartLockRead.
Definition: thread.h:135
bool thread_active_
Definition: thread.h:646
Init thread_init
Thread function pointer
Definition: thread.h:551
Smart read-locking for synchronization.
Definition: lock.h:87
SysThread thread_impl_
Definition: thread.h:645
ThreadInit thread_init
Thread init values for function-based threads, not used for class-based threads
Definition: thread.h:1232
bool thread_cancel_join(bool locked=false)
Cancels and joins thread.
Definition: thread.h:747
SysThread::Handle thread_handle() const
Get platform-specific thread handle.
Definition: thread.h:580
Init()
Definition: thread.h:541
#define EVO_ATOMIC_CONSUME
Start "consume" memory ordering barrier, usually followed by a matching "release" barrier...
Definition: atomic.h:24
bool trylock()
Try to lock mutex without blocking.
Definition: systhread.h:223
SharedState shared_state
Shared state used by threads.
Definition: thread.h:1231
bool cancelled() const
Get cancel flag.
Definition: thread.h:1151
Condition(Mutex &mutex)
Constructor.
Definition: thread.h:300
static ulong id()
Get current thread ID from system.
Definition: thread.h:641
Creates a single thread of execution in given scope.
Definition: thread.h:924
This & cancel(bool locked=false)
Set cancel flag to signal thread to stop, and wake thread via condition object.
Definition: thread.h:865
SmartLock< Condition > Lock
Lock object type – see SmartLock.
Definition: thread.h:244
bool start(uint count=1)
Create new threads, add to group and start them.
Definition: thread.h:1168
Mutex()
Constructor.
Definition: thread.h:108
static const ulong INF
Infinite wait timeout value.
Definition: thread.h:282
Definition: systhread.h:54
impl::ThreadGroupNode< S, T > Node
Definition: thread.h:1235
void notify_all()
Notify and wake all waiting threads.
Definition: thread.h:356
bool cancel_flag_
Definition: thread.h:1241
Evo basic types and traits.
ThreadGroup(Thread::Func func)
Constructor for function-based thread.
Definition: thread.h:1121