1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_MUTEX
|
---|
30 | #define LIBCPP_MUTEX
|
---|
31 |
|
---|
32 | #include <internal/common.hpp>
|
---|
33 | #include <thread>
|
---|
34 |
|
---|
35 | namespace std
|
---|
36 | {
|
---|
37 | extern "C" {
|
---|
38 | #include <fibril.h>
|
---|
39 | #include <fibril_synch.h>
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * 20.4.1.2.1, class mutex:
|
---|
44 | */
|
---|
45 |
|
---|
46 | class mutex
|
---|
47 | {
|
---|
48 | public:
|
---|
49 | constexpr mutex() noexcept;
|
---|
50 | ~mutex();
|
---|
51 |
|
---|
52 | mutex(const mutex&) = delete;
|
---|
53 | mutex& operator=(const mutex&) = delete;
|
---|
54 |
|
---|
55 | void lock();
|
---|
56 | bool try_lock();
|
---|
57 | void unlock();
|
---|
58 |
|
---|
59 | using native_handle_type = fibril_mutex_t*;
|
---|
60 | native_handle_type native_handle();
|
---|
61 |
|
---|
62 | private:
|
---|
63 | native_handle_type mtx_;
|
---|
64 | };
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * 30.4.1.2.2, class recursive_mutex:
|
---|
68 | */
|
---|
69 |
|
---|
70 | class recursive_mutex
|
---|
71 | {
|
---|
72 | public:
|
---|
73 | constexpr recursive_mutex() noexcept;
|
---|
74 | ~recursive_mutex();
|
---|
75 |
|
---|
76 | recursive_mutex(const recursive_mutex&) = delete;
|
---|
77 | recursive_mutex& operator=(const recursive_mutex&) = delete;
|
---|
78 |
|
---|
79 | void lock();
|
---|
80 | bool try_lock();
|
---|
81 | void unlock();
|
---|
82 |
|
---|
83 | using native_handle_type = fibril_mutex_t*;
|
---|
84 | native_handle_type native_handle();
|
---|
85 |
|
---|
86 | private:
|
---|
87 | native_handle_type mtx_;
|
---|
88 | size_t lock_level_;
|
---|
89 | thread::id owner_;
|
---|
90 | };
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * 30.4.1.3.1, class timed_mutex:
|
---|
94 | */
|
---|
95 |
|
---|
96 | // TODO: implement
|
---|
97 | class timed_mutex;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * 30.4.1.3.2, class recursive_timed_mutex:
|
---|
101 | */
|
---|
102 |
|
---|
103 | // TODO: implement
|
---|
104 | class recursive_timed_mutex;
|
---|
105 |
|
---|
106 | struct defer_lock_t
|
---|
107 | { /* DUMMY BODY */ };
|
---|
108 |
|
---|
109 | struct try_to_lock_t
|
---|
110 | { /* DUMMY BODY */ };
|
---|
111 |
|
---|
112 | struct adopt_lock_t
|
---|
113 | { /* DUMMY BODY */ };
|
---|
114 |
|
---|
115 | constexpr defer_lock_t defer_lock
|
---|
116 | { /* DUMMY BODY */ };
|
---|
117 |
|
---|
118 | constexpr try_to_lock_t try_to_lock
|
---|
119 | { /* DUMMY BODY */ };
|
---|
120 |
|
---|
121 | constexpr adopt_lock_t adopt_lock
|
---|
122 | { /* DUMMY BODY */ };
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * 30.4.2.1, class template lock_guard:
|
---|
126 | */
|
---|
127 |
|
---|
128 | template<class Mutex>
|
---|
129 | class lock_guard
|
---|
130 | {
|
---|
131 | public:
|
---|
132 | using mutex_type = Mutex;
|
---|
133 |
|
---|
134 | explicit lock_guard(mutex_type& mtx)
|
---|
135 | : mtx_{mtx}
|
---|
136 | {
|
---|
137 | mtx.lock();
|
---|
138 | }
|
---|
139 |
|
---|
140 | lock_guard(mutex_type& mtx, adopt_lock_t)
|
---|
141 | : mtx_{mtx}
|
---|
142 | { /* DUMMY BODY */ }
|
---|
143 |
|
---|
144 | ~lock_guard()
|
---|
145 | {
|
---|
146 | mtx_.unlock();
|
---|
147 | }
|
---|
148 |
|
---|
149 | lock_guard(const lock_guard&) = delete;
|
---|
150 | lock_guard& operator=(const lock_guard&) = delete;
|
---|
151 |
|
---|
152 | private:
|
---|
153 | mutex_type& mtx_;
|
---|
154 | };
|
---|
155 |
|
---|
156 | template<class Mutex>
|
---|
157 | class unique_lock;
|
---|
158 |
|
---|
159 | template<class Mutex>
|
---|
160 | void swap(unique_lock<Mutex>& lhs, unique_lock<Mutex>& rhs) noexcept;
|
---|
161 |
|
---|
162 | template<class L1, class L2, class... L3>
|
---|
163 | int try_lock(L1& l1, L2& l2, L3&... ls);
|
---|
164 |
|
---|
165 | template<class L1, class L2, class... L3>
|
---|
166 | void lock(L1& l1, L2& l2, L3&... ls);
|
---|
167 |
|
---|
168 | struct once_flag
|
---|
169 | {
|
---|
170 | constexpr once_flag() noexcept;
|
---|
171 |
|
---|
172 | once_flag(const once_flag&) = delete;
|
---|
173 | once_flag& operator=(const once_flag&) = delete;
|
---|
174 | };
|
---|
175 |
|
---|
176 | template<class Callable, class... Args>
|
---|
177 | void call_once(once_flag& flag, Callable&& func, Args&&... args);
|
---|
178 | }
|
---|
179 |
|
---|
180 | #endif
|
---|