8#ifndef Sawyer_Synchronization_H
9#define Sawyer_Synchronization_H
11#include <Sawyer/Sawyer.h>
12#include <Sawyer/Map.h>
13#include <Sawyer/Type.h>
15#if SAWYER_MULTI_THREADED
21 #include <boost/version.hpp>
22 #if __cplusplus < 201103L && BOOST_VERSION == 105400
26 #include <boost/thread.hpp>
27 #include <boost/thread/barrier.hpp>
28 #include <boost/thread/condition_variable.hpp>
29 #include <boost/thread/mutex.hpp>
30 #include <boost/thread/locks.hpp>
31 #include <boost/thread/once.hpp>
32 #include <boost/thread/recursive_mutex.hpp>
58 void unlock() { ++n; }
59 bool try_lock() {
return true; }
86 throw std::runtime_error(
"barrier would deadlock");
94template<
typename Mutex>
98 LockGuard2(Mutex &m1, Mutex &m2): m1_(m1), m2_(m2) {
99#if SAWYER_MULTI_THREADED
116template<
typename SyncTag>
121#if SAWYER_MULTI_THREADED
122 enum { SUPPORTED = 1 };
123 typedef boost::mutex
Mutex;
125 typedef boost::lock_guard<boost::mutex>
LockGuard;
126 typedef boost::unique_lock<boost::mutex>
UniqueLock;
128 typedef boost::condition_variable_any ConditionVariable;
129 typedef boost::barrier
Barrier;
131 enum { SUPPORTED = 0 };
146 enum { SUPPORTED = 0 };
158SAWYER_EXPORT SAWYER_THREAD_TRAITS::RecursiveMutex& bigMutex();
198 static SAWYER_THREAD_LOCAL
Repo *repo_;
205 repo_->
insert(
reinterpret_cast<IntPtr
>(
this), T());
212 repo_->
insert(
reinterpret_cast<IntPtr
>(
this), value);
218 repo_->
insert(
reinterpret_cast<IntPtr
>(
this), other.
get());
225 repo_->
insert(
reinterpret_cast<IntPtr
>(
this), value);
232 repo_->insert(
reinterpret_cast<IntPtr
>(
this), other.
get());
236 ~MultiInstanceTls() {
238 repo_->erase(
reinterpret_cast<IntPtr
>(
this));
260 const T& operator*()
const {
268 const T* operator->()
const {
283SAWYER_THREAD_LOCAL Container::Map<Type::UnsignedInteger<8*
sizeof(
void*)>::type, T>* MultiInstanceTls<T>::repo_;
Container associating values with keys.
Value & insertMaybeDefault(const Key &key)
Conditionally insert a new key with default value.
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
Thread local data per object instance.
MultiInstanceTls(const T &value)
Initialize value.
MultiInstanceTls & operator=(const T &value)
Assignment operator.
T & get()
Get interior object.
MultiInstanceTls()
Default-constructed value.
const T & get() const
Get interior object.
size_t fastRandomIndex(size_t n, size_t seed=0)
Thread-safe random number generator.
Tag indicating that an algorithm or API should assume multiple threads.
Tag indicating that an algorithm or API can assume only a single thread.
Traits for thread synchronization.
An unsigned integer of particular size.