14#include <DeterministicConcurrency>
22namespace DeterministicConcurrency{
36 template <
typename... Tuples>
39 static_cast<Tuples&&
>(tuples)...} {}
55 for(
size_t numThreads = 0; numThreads !=
sizeof...(threadIndexes);){
56 std::this_thread::sleep_for(std::chrono::milliseconds(1));
78 template<
typename BasicLockable>
80 while (lockable->try_lock()){
82 std::this_thread::sleep_for(std::chrono::milliseconds(1));
100 size_t threadIndex = -1;
101 for (;threadIndex == -1;){
102 std::this_thread::sleep_for(std::chrono::milliseconds(1));
105 threadIndex = threadIndexes;
122 template<
typename... Args>
152 template<
typename... Args>
154 static_assert(
sizeof...(threadIndexes) <= N,
"Too many args");
155 (_threads[threadIndexes].join(), ...);
167 for (
auto& _thread : _threads)
181 template<
typename... Args>
183 static_assert(
sizeof...(threadIndexes) <= N,
"Too many args");
184 (_threads[threadIndexes].tick(), ...);
197 template<
typename... Args>
198 void wait(Args&&... threadIndexes){
199 static_assert(
sizeof...(threadIndexes) <= N,
"Too many args");
200 (_threads[threadIndexes].wait_for_tock(), ...);
210 return _contexts[threadIndex].thread_status_v;
215 template <
typename... Tuples>
217 : _threads{std::make_from_tuple<DeterministicThread>(tuples)...} {}
219 template <
typename... Tuples, std::size_t... Is>
220 UserControlledScheduler(std::index_sequence<Is...>, Tuples&&... tuples)
221 : UserControlledScheduler{
222 emplace_t{}, std::tuple_cat(std::tuple{&std::get<Is>(_contexts)},
223 static_cast<Tuples&&>(tuples))...} {}
226 template <std::size_t... Is>
234 std::array<thread_context, N> _contexts;
235 std::array<DeterministicThread, N> _threads;
254 template<
typename... Tuples>
thread_status_t
Enum describing the possible states of a thread.
Definition DeterministicThread.h:25
auto make_UserControlledScheduler(Tuples &&... tuples)
Helper function to create an UserControlledScheduler.
Definition UserControlledScheduler.h:255
A scheduler which allow to manage the flow of its managed threads.
Definition UserControlledScheduler.h:29
void joinOn(Args &&... threadIndexes)
Perform a join on the threads with threadIndexes.
Definition UserControlledScheduler.h:153
void proceed(Args &&... threadIndexes)
Allow threadIndexes to continue while not stopping the scheduler thread.
Definition UserControlledScheduler.h:182
void switchContextAll()
Switch context allowing all the threads to proceed while stopping the scheduler from executing until ...
Definition UserControlledScheduler.h:138
void wait(Args &&... threadIndexes)
Wait until the threads with threadIndexes go into WAITING status.
Definition UserControlledScheduler.h:198
void joinAll()
Perform a join on all threads.
Definition UserControlledScheduler.h:166
size_t waitUntilOneThreadStatus(Args &&... threadIndexes)
Wait until at least one of the threadIndexes threads have thread_status_v equal to S and return the i...
Definition UserControlledScheduler.h:99
thread_status_t getThreadStatus(size_t threadIndex)
Get the Thread Status of the thread with threadIndex.
Definition UserControlledScheduler.h:209
static void waitUntilLocked(BasicLockable *lockable)
Wait until lockable is owned.
Definition UserControlledScheduler.h:79
void switchContextTo(Args &&... threadIndexes)
Switch context allowing the threads with threadIndexes to proceed while stopping the scheduler from e...
Definition UserControlledScheduler.h:123
void waitUntilAllThreadStatus(Args &&... threadIndexes)
Wait until all of the threadIndexes threads have thread_status_v equal to S.
Definition UserControlledScheduler.h:54