Determinic Concurrency


Loading...
Searching...
No Matches
DeterministicThread.h
Go to the documentation of this file.
1
13#pragma once
14#include <DeterministicConcurrency>
15#include <thread>
16#include <mutex>
17#include <condition_variable>
18#include <tuple>
19
20namespace DeterministicConcurrency{
25 enum class thread_status_t{
26 RUNNING,
27 WAITING,
28 NOT_STARTED,
29 FINISHED,
30 WAITING_EXTERNAL
31 };
32
33 class DeterministicThread;
34
66 public:
67 thread_context() noexcept : control_mutex(), tick_tock(), thread_status_v(thread_status_t::NOT_STARTED) {}
68
84 tock();
85 wait_for_tick();
86 }
87
111 template<typename BasicLockable, typename... Args>
112 void lock(BasicLockable* lockable, Args&&... args){
113
114 {
115 std::lock_guard<std::mutex> lock(control_mutex);
116 thread_status_v = DeterministicConcurrency::thread_status_t::WAITING_EXTERNAL;
117 }
118
119 lockable->lock(std::forward<Args>(args)...);
120
121 {
122 std::lock_guard<std::mutex> lock(control_mutex);
123 thread_status_v = DeterministicConcurrency::thread_status_t::RUNNING;
124 }
125
126 }
127
151 template<typename BasicLockable, typename... Args>
152 void lock_shared(BasicLockable* lockable, Args&&... args){
153
154 {
155 std::lock_guard<std::mutex> lock(control_mutex);
156 thread_status_v = DeterministicConcurrency::thread_status_t::WAITING_EXTERNAL;
157 }
158
159 lockable->lock_shared(std::forward<Args>(args)...);
160
161 {
162 std::lock_guard<std::mutex> lock(control_mutex);
163 thread_status_v = DeterministicConcurrency::thread_status_t::RUNNING;
164 }
165
166 }
167
168 private:
169
172 friend class DeterministicThread;
173
177 template<size_t N>
179
183 void start(){
184 std::unique_lock<std::mutex> lock(control_mutex);
185 while (thread_status_v == thread_status_t::NOT_STARTED)
186 tick_tock.wait(lock);
187 }
188
192 void finish(){
193 {
194 std::unique_lock<std::mutex> lock(control_mutex);
195 thread_status_v = thread_status_t::FINISHED;
196 }
197 tick_tock.notify_one();
198 }
199
203 void tock() {
204 {
205 std::unique_lock<std::mutex> lock(control_mutex);
206 thread_status_v = thread_status_t::WAITING;
207 }
208 tick_tock.notify_one();
209 }
210
214 void wait_for_tick(){
215 std::unique_lock<std::mutex> lock(control_mutex);
216 while (thread_status_v == thread_status_t::WAITING)
217 tick_tock.wait(lock);
218 }
219
220 std::condition_variable tick_tock;
221 volatile thread_status_t thread_status_v;
222 std::mutex control_mutex;
223 };
224
229 class DeterministicThread {
230 public:
232 template <typename Func, typename... Args>
233 explicit DeterministicThread(thread_context* t ,Func&& func, Args&&... args)
234 : _thread([function = std::forward<Func>(func), t, tuple = std::make_tuple(std::forward<Args>(args)...)]() mutable {
235 t->start();
236 std::apply(function, std::tuple_cat(std::make_tuple(t), tuple));
237 t->finish();
238 })
239 , _this_thread(t) {}
240
244 void join() {
245 _thread.join();
246 }
247
251 void tick() {
252 {
253 std::unique_lock<std::mutex> lock(_this_thread->control_mutex);
254 if (_this_thread->thread_status_v == thread_status_t::FINISHED)return;
255 _this_thread->thread_status_v = thread_status_t::RUNNING;
256 }
257 _this_thread->tick_tock.notify_one();
258 }
259
263 void wait_for_tock(){
264 std::unique_lock<std::mutex> lock(_this_thread->control_mutex);
265 while (_this_thread->thread_status_v == thread_status_t::RUNNING)
266 _this_thread->tick_tock.wait(lock);
267 }
268
269 private:
270 thread_context* _this_thread;
271 std::thread _thread;
272 };
273}
thread_status_t
Enum describing the possible states of a thread.
Definition DeterministicThread.h:25
A scheduler which allow to manage the flow of its managed threads.
Definition UserControlledScheduler.h:29
Provide the thread with basic functionalities.
Definition DeterministicThread.h:65
void switchContext()
Notify the scheduler that this thread is ready to give it back the control and wait until the schedul...
Definition DeterministicThread.h:83
void lock_shared(BasicLockable *lockable, Args &&... args)
Lock lockable in shared mode and update the current thread_status_v of the current deterministic thre...
Definition DeterministicThread.h:152
void lock(BasicLockable *lockable, Args &&... args)
Lock lockable and update the current thread_status_v of the current deterministic thread.
Definition DeterministicThread.h:112