-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathmutex.h
More file actions
90 lines (75 loc) · 2.28 KB
/
mutex.h
File metadata and controls
90 lines (75 loc) · 2.28 KB
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
#ifndef _GHLIBCPP_MUTEX
#define _GHLIBCPP_MUTEX
#include <chrono>
namespace std {
struct adopt_lock_t {
explicit adopt_lock_t() = default;
};
struct defer_lock_t {
explicit defer_lock_t() = default;
};
struct try_to_lock_t {
explicit try_to_lock_t() = default;
};
constexpr adopt_lock_t adopt_lock{};
constexpr defer_lock_t defer_lock{};
constexpr try_to_lock_t try_to_lock{};
class mutex {
public:
constexpr mutex() noexcept;
~mutex() = default;
mutex(const mutex &) = delete;
mutex &operator=(const mutex &) = delete;
void lock();
bool try_lock();
void unlock();
};
// unique_lock uses a simplified chrono library
template <class Mutex> class unique_lock {
public:
typedef Mutex mutex_type;
unique_lock() noexcept;
explicit unique_lock(mutex_type &m);
unique_lock(mutex_type &__m, adopt_lock_t);
unique_lock(mutex_type &__m, defer_lock_t);
unique_lock(mutex_type &__m, try_to_lock_t);
unique_lock(mutex_type &m, const chrono::duration &rel_time);
~unique_lock();
unique_lock(unique_lock const &) = delete;
unique_lock &operator=(unique_lock const &) = delete;
unique_lock(unique_lock &&u) noexcept;
unique_lock &operator=(unique_lock &&u);
void lock();
bool try_lock();
bool try_lock_for(const chrono::duration &rel_time);
bool try_lock_until(const chrono::time_point &abs_time);
void unlock();
void swap(unique_lock &u) noexcept;
mutex_type *release() noexcept;
bool owns_lock() const noexcept;
explicit operator bool() const noexcept;
mutex_type *mutex() const noexcept;
};
template <class Mutex>
void swap(unique_lock<Mutex> &x, unique_lock<Mutex> &y) noexcept;
template <class _Lock0, class _Lock1, class... _LockN>
void lock(_Lock0 &_Lk0, _Lock1 &_Lk1, _LockN &..._LkN) {}
template <typename Mutex> class lock_guard {
public:
typedef Mutex mutex_type;
explicit lock_guard(mutex_type &__m) : _m(__m) { _m.lock(); }
lock_guard(const lock_guard &) = delete;
lock_guard &operator=(const lock_guard &) = delete;
~lock_guard() { _m.unlock(); }
private:
mutex_type &_m;
};
template <class... MutexTypes> class scoped_lock {
public:
explicit scoped_lock(MutexTypes &...m);
scoped_lock(const scoped_lock &) = delete;
scoped_lock &operator=(const scoped_lock &) = delete;
~scoped_lock();
};
} // namespace std
#endif // _GHLIBCPP_MUTEX