|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 TU Dresden |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Authors: |
| 6 | + * Christian Menard |
| 7 | + */ |
| 8 | + |
| 9 | +#ifndef REACTOR_CPP_GROUP_SCHEDULER_HH |
| 10 | +#define REACTOR_CPP_GROUP_SCHEDULER_HH |
| 11 | + |
| 12 | +#include <condition_variable> |
| 13 | +#include <functional> |
| 14 | +#include <future> |
| 15 | +#include <map> |
| 16 | +#include <mutex> |
| 17 | +#include <set> |
| 18 | +#include <thread> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#include "fwd.hh" |
| 22 | +#include "logical_time.hh" |
| 23 | +#include "semaphore.hh" |
| 24 | + |
| 25 | +namespace reactor { |
| 26 | + |
| 27 | +// forward declarations |
| 28 | +class GroupScheduler; |
| 29 | +class GroupWorker; |
| 30 | + |
| 31 | +class GroupWorker { // NOLINT |
| 32 | +public: |
| 33 | + GroupScheduler& scheduler_; |
| 34 | + const unsigned int identity_{0}; |
| 35 | + std::thread thread_{}; |
| 36 | + |
| 37 | + // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
| 38 | + static thread_local const GroupWorker* current_worker; |
| 39 | + |
| 40 | + void work() const; |
| 41 | + void execute_reaction(Reaction* reaction) const; |
| 42 | + |
| 43 | + GroupWorker(GroupScheduler& scheduler, unsigned int identity) |
| 44 | + : scheduler_{scheduler} |
| 45 | + , identity_{identity} {} |
| 46 | + GroupWorker(GroupWorker&& worker); // NOLINT(performance-noexcept-move-constructor) |
| 47 | + GroupWorker(const GroupWorker& worker) = delete; |
| 48 | + |
| 49 | + void start_thread() { thread_ = std::thread(&GroupWorker::work, this); } |
| 50 | + void join_thread() { thread_.join(); } |
| 51 | + |
| 52 | + static auto current_worker_id() -> unsigned { return current_worker->identity_; } |
| 53 | +}; |
| 54 | + |
| 55 | +class GroupReadyQueue { |
| 56 | +private: |
| 57 | + std::vector<Reaction*> queue_{}; |
| 58 | + std::atomic<std::ptrdiff_t> size_{0}; |
| 59 | + Semaphore sem_{0}; |
| 60 | + std::ptrdiff_t waiting_workers_{0}; |
| 61 | + const unsigned int num_workers_; |
| 62 | + |
| 63 | +public: |
| 64 | + explicit GroupReadyQueue(unsigned num_workers) |
| 65 | + : num_workers_(num_workers) {} |
| 66 | + |
| 67 | + /** |
| 68 | + * Retrieve a ready reaction from the queue. |
| 69 | + * |
| 70 | + * This method may be called concurrently. In case the queue is empty, the |
| 71 | + * method blocks and waits until a ready reaction becomes available. |
| 72 | + */ |
| 73 | + auto pop() -> Reaction*; |
| 74 | + |
| 75 | + /** |
| 76 | + * Fill the queue up with ready reactions. |
| 77 | + * |
| 78 | + * This method assumes that the internal queue is empty. It moves all |
| 79 | + * reactions from the provided `ready_reactions` vector to the internal |
| 80 | + * queue, leaving `ready_reactions` empty. |
| 81 | + * |
| 82 | + * Note that this method is not thread-safe. The caller needs to ensure that |
| 83 | + * no other thread will try to read from the queue during this operation. |
| 84 | + */ |
| 85 | + void fill_up(std::vector<Reaction*>& ready_reactions); |
| 86 | +}; |
| 87 | + |
| 88 | +using EventMap = std::map<BaseAction*, std::function<void(void)>>; |
| 89 | + |
| 90 | +class GroupScheduler { // NOLINT |
| 91 | +private: |
| 92 | + const bool using_workers_; |
| 93 | + LogicalTime logical_time_{}; |
| 94 | + |
| 95 | + Environment* environment_; |
| 96 | + std::vector<GroupWorker> workers_{}; |
| 97 | + |
| 98 | + std::mutex scheduling_mutex_; |
| 99 | + std::unique_lock<std::mutex> scheduling_lock_{scheduling_mutex_, std::defer_lock}; |
| 100 | + std::condition_variable cv_schedule_; |
| 101 | + |
| 102 | + std::mutex lock_event_queue_; |
| 103 | + std::map<Tag, EventMap> event_queue_; |
| 104 | + |
| 105 | + std::vector<std::vector<BasePort*>> set_ports_; |
| 106 | + std::vector<std::vector<Reaction*>> triggered_reactions_; |
| 107 | + |
| 108 | + std::vector<std::vector<Reaction*>> reaction_queue_; |
| 109 | + unsigned int reaction_queue_pos_{std::numeric_limits<unsigned>::max()}; |
| 110 | + |
| 111 | + GroupReadyQueue ready_queue_; |
| 112 | + std::atomic<std::ptrdiff_t> reactions_to_process_{0}; // NOLINT |
| 113 | + |
| 114 | + std::atomic<bool> stop_{false}; |
| 115 | + bool continue_execution_{true}; |
| 116 | + |
| 117 | + void schedule() noexcept; |
| 118 | + auto schedule_ready_reactions() -> bool; |
| 119 | + void next(); |
| 120 | + void terminate_all_workers(); |
| 121 | + void set_port_helper(BasePort* port); |
| 122 | + |
| 123 | +public: |
| 124 | + explicit GroupScheduler(Environment* env); |
| 125 | + ~GroupScheduler(); |
| 126 | + |
| 127 | + void schedule_sync(const Tag& tag, BaseAction* action, std::function<void(void)> pre_handler); |
| 128 | + void schedule_async(const Tag& tag, BaseAction* action, std::function<void(void)> pre_handler); |
| 129 | + |
| 130 | + void inline lock() noexcept { scheduling_lock_.lock(); } |
| 131 | + void inline unlock() noexcept { scheduling_lock_.unlock(); } |
| 132 | + |
| 133 | + void set_port(BasePort* port); |
| 134 | + |
| 135 | + [[nodiscard]] inline auto logical_time() const noexcept -> const auto& { return logical_time_; } |
| 136 | + |
| 137 | + void start(); |
| 138 | + void stop(); |
| 139 | + |
| 140 | + friend GroupWorker; |
| 141 | +}; |
| 142 | + |
| 143 | +} // namespace reactor |
| 144 | + |
| 145 | +#endif // REACTOR_CPP_GROUP_SCHEDULER_HH |
0 commit comments