-
Notifications
You must be signed in to change notification settings - Fork 5
/
mutex.hpp
193 lines (164 loc) · 4.85 KB
/
mutex.hpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (c) 2017 UChicago Argonne, LLC
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __THALLIUM_MUTEX_HPP
#define __THALLIUM_MUTEX_HPP
#include <abt.h>
#include <thallium/abt_errors.hpp>
#include <thallium/exception.hpp>
namespace thallium {
/**
* Exception class thrown by the mutex class.
*/
class mutex_exception : public exception {
public:
template <typename... Args>
mutex_exception(Args&&... args)
: exception(std::forward<Args>(args)...) {}
};
#define TL_MUTEX_EXCEPTION(__fun, __ret) \
mutex_exception(#__fun, " returned ", abt_error_get_name(__ret), " (", \
abt_error_get_description(__ret), ") in ", __FILE__, ":", \
__LINE__);
#define TL_MUTEX_ASSERT(__call) \
{ \
int __ret = __call; \
if(__ret != ABT_SUCCESS) { \
throw TL_MUTEX_EXCEPTION(__call, __ret); \
} \
}
/**
* @brief The mutex class is an equivalent of std::mutex
* but build around Argobot's mutex.
*/
class mutex {
ABT_mutex m_mutex;
public:
/**
* @brief Type of the underlying native handle.
*/
typedef ABT_mutex native_handle_type;
/**
* @brief Constructor.
*
* @param recursive whether the mutex is recursive or not.
*/
explicit mutex(bool recursive = false) {
ABT_mutex_attr attr;
TL_MUTEX_ASSERT(ABT_mutex_attr_create(&attr));
if(recursive) {
TL_MUTEX_ASSERT(ABT_mutex_attr_set_recursive(attr, ABT_TRUE));
}
TL_MUTEX_ASSERT(ABT_mutex_create_with_attr(attr, &m_mutex));
TL_MUTEX_ASSERT(ABT_mutex_attr_free(&attr));
}
/**
* @brief Copy constructor is deleted.
*/
mutex(const mutex& other) = delete;
/**
* @brief Copy assignment operator is deleted.
*/
mutex& operator=(const mutex& other) = delete;
/**
* @brief Move assignment operator is deleted.
*/
mutex& operator=(mutex&& other) {
if(m_mutex != ABT_MUTEX_NULL) {
TL_MUTEX_ASSERT(ABT_mutex_free(&m_mutex));
}
m_mutex = other.m_mutex;
other.m_mutex = ABT_MUTEX_NULL;
return *this;
}
/**
* @brief Move constructor.
*/
mutex(mutex&& other) noexcept {
m_mutex = other.m_mutex;
other.m_mutex = ABT_MUTEX_NULL;
}
/**
* @brief Destructor.
*/
virtual ~mutex() noexcept {
if(m_mutex != ABT_MUTEX_NULL)
ABT_mutex_free(&m_mutex);
}
/**
* @brief Lock the mutex.
*/
void lock() { TL_MUTEX_ASSERT(ABT_mutex_lock(m_mutex)); }
/**
* @brief Lock the mutex in low priority.
*/
void lock_low() { TL_MUTEX_ASSERT(ABT_mutex_lock_low(m_mutex)); }
/**
* @brief Lock the mutex without context switch.
*/
void spin_lock() { TL_MUTEX_ASSERT(ABT_mutex_spinlock(m_mutex)); }
/**
* @brief Try locking the mutex without blocking.
*
* @return true if it managed to lock the mutex.
*/
bool try_lock() {
int ret = ABT_mutex_trylock(m_mutex);
if(ABT_SUCCESS == ret) {
return true;
} else if(ABT_ERR_MUTEX_LOCKED == ret) {
return false;
} else {
TL_MUTEX_EXCEPTION(ABT_mutex_trylock(m_mutex), ret);
}
return false;
}
/**
* @brief Unlock the mutex.
*/
void unlock() { TL_MUTEX_ASSERT(ABT_mutex_unlock(m_mutex)); }
/**
* @brief Hand over the mutex within the ES.
*/
void unlock_se() { TL_MUTEX_ASSERT(ABT_mutex_unlock_se(m_mutex)); }
/**
* @brief Get the underlying native handle.
*
* @return the underlying native handle.
*/
ABT_mutex native_handle() const noexcept { return m_mutex; }
};
/**
* @brief Child class of mutex to represent a recursive mutex.
*/
class recursive_mutex : public mutex {
public:
/**
* @brief Constructor.
*/
recursive_mutex()
: mutex(true) {}
/**
* @brief Copy constructor is deleted.
*/
recursive_mutex(const recursive_mutex& other) = delete;
/**
* @brief Move constructor.
*/
recursive_mutex(recursive_mutex&& other)
: mutex(std::move(other)) {}
/**
* @brief Copy assignment operator is deleted.
*/
recursive_mutex& operator=(const recursive_mutex& other) = delete;
/**
* @brief Move assignment operator.
*/
recursive_mutex& operator=(recursive_mutex&& other) = default;
};
} // namespace thallium
#undef TL_MUTEX_EXCEPTION
#undef TL_MUTEX_ASSERT
#endif /* end of include guard */