-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathshared_state_mutex.h
115 lines (92 loc) · 3.71 KB
/
shared_state_mutex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef SHARED_STATE_MUTEX_H
#define SHARED_STATE_MUTEX_H
#ifdef SHARED_STATE_BOOST_MUTEX
#include <boost/thread/shared_mutex.hpp>
namespace shared_state_chrono = boost::chrono;
class shared_state_mutex_notimeout_noshared: public boost::mutex {
public:
// recursive read locks are allowed to dead-lock so it is valid to replace a shared_timed_mutex with a mutex
void lock_shared() { lock(); }
bool try_lock_shared() { return try_lock(); }
void unlock_shared() { unlock(); }
// Discard any timeout parameters
bool try_lock_for(...) { lock(); return true; }
bool try_lock_shared_for(...) { lock_shared(); return true; }
};
class shared_state_mutex_notimeout: public boost::shared_mutex {
public:
// Discard any timeout parameters
bool try_lock_for(...) { lock(); return true; }
bool try_lock_shared_for(...) { lock_shared(); return true; }
};
class shared_state_mutex_noshared: public boost::timed_mutex {
public:
void lock_shared() { lock(); }
bool try_lock_shared() { return try_lock(); }
void unlock_shared() { unlock(); }
template <class Rep, class Period>
bool try_lock_shared_for(const shared_state_chrono::duration<Rep, Period>& rel_time) { return try_lock_for(rel_time); }
};
typedef boost::shared_mutex shared_state_mutex_default;
#else
//#include <mutex>
//#include <shared_mutex> // Requires C++14
//namespace std {
// typedef shared_mutex shared_timed_mutex;
//}
#include "shared_timed_mutex_polyfill.h" // Requires C++11
namespace std {
using namespace std_polyfill;
}
namespace shared_state_chrono = std::chrono;
class shared_state_mutex_notimeout_noshared: public std::mutex {
public:
void lock_shared() { lock(); }
bool try_lock_shared() { return try_lock(); }
void unlock_shared() { unlock(); }
bool try_lock_for(...) { lock(); return true; }
bool try_lock_shared_for(...) { lock_shared(); return true; }
};
class shared_state_mutex_notimeout: public std::shared_timed_mutex {
public:
bool try_lock_for(...) { lock(); return true; }
bool try_lock_shared_for(...) { lock_shared(); return true; }
};
class shared_state_mutex_noshared: public std::timed_mutex {
public:
void lock_shared() { lock(); }
bool try_lock_shared() { return try_lock(); }
void unlock_shared() { unlock(); }
template <class Rep, class Period>
bool try_lock_shared_for(const shared_state_chrono::duration<Rep, Period>& rel_time) { return try_lock_for(rel_time); }
};
typedef std::shared_timed_mutex shared_state_mutex_default;
#endif
/**
* @brief The shared_state_nomutex class is a dummy class than can be used with
* shared_state_traits to disable mutex locking for a given type. This is meant
* to be used temporarily during testing.
*/
class shared_state_nomutex {
public:
void lock() {}
void unlock() {}
void lock_shared() { lock(); }
bool try_lock() { return true; }
bool try_lock_shared() { return true; }
void unlock_shared() { unlock(); }
bool try_lock_for(...) { lock(); return true; }
bool try_lock_shared_for(...) { lock_shared(); return true; }
};
#if defined SHARED_STATE_NO_TIMEOUT
#if defined SHARED_STATE_NO_SHARED_MUTEX
typedef shared_state_mutex_notimeout_noshared shared_state_mutex;
#else
typedef shared_state_mutex_notimeout shared_state_mutex;
#endif
#elif defined SHARED_STATE_NO_SHARED_MUTEX
typedef shared_state_mutex_noshared shared_state_mutex;
#else
typedef shared_state_mutex_default shared_state_mutex;
#endif
#endif // SHARED_STATE_MUTEX_H