Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 1 | #include "aos/events/shm-event-loop.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 2 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 3 | #include <sys/timerfd.h> |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 4 | #include <algorithm> |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 5 | #include <atomic> |
| 6 | #include <chrono> |
| 7 | #include <stdexcept> |
| 8 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 9 | #include "aos/events/epoll.h" |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 10 | #include "aos/init.h" |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 11 | #include "aos/logging/logging.h" |
| 12 | #include "aos/queue.h" |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 13 | #include "aos/util/phased_loop.h" |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 14 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 15 | namespace aos { |
| 16 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 17 | ShmEventLoop::ShmEventLoop() {} |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 18 | |
| 19 | namespace { |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 20 | |
| 21 | namespace chrono = ::std::chrono; |
| 22 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 23 | class ShmFetcher : public RawFetcher { |
| 24 | public: |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 25 | explicit ShmFetcher(RawQueue *queue) : queue_(queue) { |
| 26 | // Move index_ to point to the end of the queue as it is at construction |
| 27 | // time. Also grab the oldest message but don't expose it to the user yet. |
| 28 | static constexpr Options<RawQueue> kOptions = |
| 29 | RawQueue::kFromEnd | RawQueue::kNonBlock; |
James Kuszmaul | af04d73 | 2019-10-06 21:51:55 -0700 | [diff] [blame] | 30 | msg_ = queue_->ReadMessageIndex(kOptions, &index_); |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 31 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 32 | ~ShmFetcher() { |
| 33 | if (msg_) { |
| 34 | queue_->FreeMessage(msg_); |
| 35 | } |
| 36 | } |
| 37 | |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 38 | bool FetchNext() override { |
James Kuszmaul | af04d73 | 2019-10-06 21:51:55 -0700 | [diff] [blame] | 39 | const void *msg = queue_->ReadMessageIndex(RawQueue::kNonBlock, &index_); |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 40 | // Only update the internal pointer if we got a new message. |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 41 | if (msg != nullptr) { |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 42 | queue_->FreeMessage(msg_); |
| 43 | msg_ = msg; |
| 44 | set_most_recent(msg_); |
| 45 | } |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 46 | return msg != nullptr; |
James Kuszmaul | c79768b | 2019-02-18 15:08:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool Fetch() override { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 50 | static constexpr Options<RawQueue> kOptions = |
| 51 | RawQueue::kFromEnd | RawQueue::kNonBlock; |
James Kuszmaul | af04d73 | 2019-10-06 21:51:55 -0700 | [diff] [blame] | 52 | const void *msg = queue_->ReadMessageIndex(kOptions, &index_); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 53 | // Only update the internal pointer if we got a new message. |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 54 | if (msg != nullptr && msg != msg_) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 55 | queue_->FreeMessage(msg_); |
| 56 | msg_ = msg; |
| 57 | set_most_recent(msg_); |
| 58 | return true; |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 59 | } else { |
| 60 | // The message has to get freed if we didn't use it (and |
| 61 | // RawQueue::FreeMessage is ok to call on nullptr). |
| 62 | queue_->FreeMessage(msg); |
| 63 | |
| 64 | // We have a message from construction time. Give it to the user now. |
| 65 | if (msg_ != nullptr && most_recent() != msg_) { |
| 66 | set_most_recent(msg_); |
| 67 | return true; |
| 68 | } else { |
| 69 | return false; |
| 70 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 71 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | private: |
| 75 | int index_ = 0; |
| 76 | RawQueue *queue_; |
James Kuszmaul | af04d73 | 2019-10-06 21:51:55 -0700 | [diff] [blame] | 77 | const void *msg_ = nullptr; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | class ShmSender : public RawSender { |
| 81 | public: |
| 82 | explicit ShmSender(RawQueue *queue) : queue_(queue) {} |
| 83 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 84 | ::aos::Message *GetMessage() override { |
| 85 | return reinterpret_cast<::aos::Message *>(queue_->GetMessage()); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 88 | void Free(::aos::Message *msg) override { queue_->FreeMessage(msg); } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 89 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 90 | bool Send(::aos::Message *msg) override { |
Austin Schuh | bbce72d | 2019-05-26 15:11:46 -0700 | [diff] [blame] | 91 | assert(queue_ != nullptr); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 92 | { |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 93 | // TODO(austin): This lets multiple senders reorder messages since time |
| 94 | // isn't acquired with a lock held. |
James Kuszmaul | cd1db35 | 2019-05-26 16:42:29 -0700 | [diff] [blame] | 95 | if (msg->sent_time == monotonic_clock::min_time) { |
| 96 | msg->sent_time = monotonic_clock::now(); |
Austin Schuh | 7267c53 | 2019-05-19 19:55:53 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 99 | return queue_->WriteMessage(msg, RawQueue::kOverride); |
| 100 | } |
| 101 | |
Austin Schuh | d681bbd | 2019-02-02 12:03:32 -0800 | [diff] [blame] | 102 | const char *name() const override { return queue_->name(); } |
| 103 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 104 | private: |
| 105 | RawQueue *queue_; |
| 106 | }; |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 107 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 108 | } // namespace |
| 109 | |
| 110 | namespace internal { |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 111 | |
| 112 | // Class to manage the state for a Watcher. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 113 | class WatcherThreadState { |
| 114 | public: |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 115 | WatcherThreadState( |
| 116 | ShmEventLoop::ThreadState *thread_state, RawQueue *queue, |
| 117 | ::std::function<void(const ::aos::Message *message)> watcher) |
| 118 | : thread_state_(thread_state), |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 119 | queue_(queue), |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 120 | index_(0), |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 121 | watcher_(::std::move(watcher)) {} |
| 122 | |
| 123 | ~WatcherThreadState() { |
| 124 | // Only kill the thread if it is running. |
| 125 | if (running_) { |
| 126 | // TODO(austin): CHECK that we aren't RT here. |
| 127 | |
| 128 | // Try joining. If we fail, we weren't asleep on the condition in the |
| 129 | // queue. So hit it again and again until that's true. |
| 130 | struct timespec end_time; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 131 | AOS_PCHECK(clock_gettime(CLOCK_REALTIME, &end_time) == 0); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 132 | while (true) { |
| 133 | void *retval = nullptr; |
| 134 | end_time.tv_nsec += 100000000; |
| 135 | if (end_time.tv_nsec > 1000000000L) { |
| 136 | end_time.tv_nsec -= 1000000000L; |
| 137 | ++end_time.tv_sec; |
| 138 | } |
| 139 | int ret = pthread_timedjoin_np(pthread_, &retval, &end_time); |
| 140 | if (ret == ETIMEDOUT) continue; |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 141 | AOS_PCHECK(ret == 0); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 142 | break; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Starts the thread and waits until it is running. |
| 148 | void Start() { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 149 | AOS_PCHECK(pthread_create(&pthread_, nullptr, &StaticRun, this) == 0); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 150 | IPCRecursiveMutexLocker locker(&thread_started_mutex_); |
| 151 | if (locker.owner_died()) ::aos::Die("Owner died"); |
| 152 | while (!running_) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 153 | AOS_CHECK(!thread_started_condition_.Wait()); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | void GrabQueueIndex() { |
| 158 | // Right after we are signaled to start, point index to the current index |
| 159 | // so we don't read any messages received before now. Otherwise we will |
| 160 | // get a significantly delayed read. |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 161 | static constexpr Options<RawQueue> kOptions = |
| 162 | RawQueue::kFromEnd | RawQueue::kNonBlock; |
| 163 | const void *msg = queue_->ReadMessageIndex(kOptions, &index_); |
| 164 | if (msg) { |
| 165 | queue_->FreeMessage(msg); |
| 166 | } |
| 167 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 168 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 169 | private: |
| 170 | // Runs Run given a WatcherThreadState as the argument. This is an adapter |
| 171 | // between pthreads and Run. |
| 172 | static void *StaticRun(void *arg) { |
| 173 | WatcherThreadState *watcher_thread_state = |
| 174 | reinterpret_cast<WatcherThreadState *>(arg); |
| 175 | watcher_thread_state->Run(); |
| 176 | return nullptr; |
| 177 | } |
| 178 | |
| 179 | // Runs the watcher callback on new messages. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 180 | void Run() { |
Austin Schuh | 0fc3b3e | 2019-06-29 13:56:21 -0700 | [diff] [blame] | 181 | ::aos::SetCurrentThreadName(thread_state_->name() + ".watcher"); |
| 182 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 183 | // Signal the main thread that we are now ready. |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 184 | thread_state_->MaybeSetCurrentThreadRealtimePriority(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 185 | { |
| 186 | IPCRecursiveMutexLocker locker(&thread_started_mutex_); |
| 187 | if (locker.owner_died()) ::aos::Die("Owner died"); |
| 188 | running_ = true; |
| 189 | thread_started_condition_.Broadcast(); |
| 190 | } |
| 191 | |
| 192 | // Wait for the global start before handling events. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 193 | thread_state_->WaitForStart(); |
| 194 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 195 | // Bail immediately if we are supposed to stop. |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 196 | if (!thread_state_->is_running()) { |
| 197 | ::aos::UnsetCurrentThreadRealtimePriority(); |
| 198 | return; |
| 199 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 200 | |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 201 | const void *msg = nullptr; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 202 | while (true) { |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 203 | msg = queue_->ReadMessageIndex(RawQueue::kBlock, &index_, |
| 204 | chrono::seconds(1)); |
| 205 | // We hit a timeout. Confirm that we should be running and retry. Note, |
| 206 | // is_running is threadsafe (it's an atomic underneath). Worst case, we |
| 207 | // check again in a second. |
| 208 | if (msg == nullptr) { |
| 209 | if (!thread_state_->is_running()) break; |
| 210 | continue; |
| 211 | } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 212 | |
| 213 | { |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 214 | // Grab the lock so that only one callback can be called at a time. |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 215 | MutexLocker locker(&thread_state_->mutex_); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 216 | if (!thread_state_->is_running()) break; |
| 217 | |
| 218 | watcher_(reinterpret_cast<const Message *>(msg)); |
| 219 | // watcher_ may have exited the event loop. |
| 220 | if (!thread_state_->is_running()) break; |
| 221 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 222 | // Drop the reference. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 223 | queue_->FreeMessage(msg); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 226 | // And drop the last reference. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 227 | queue_->FreeMessage(msg); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 228 | // Now that everything is cleaned up, drop RT priority before destroying the |
| 229 | // thread. |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 230 | ::aos::UnsetCurrentThreadRealtimePriority(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 231 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 232 | pthread_t pthread_; |
| 233 | ShmEventLoop::ThreadState *thread_state_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 234 | RawQueue *queue_; |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 235 | int32_t index_; |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 236 | bool running_ = false; |
Austin Schuh | 3578a2e | 2019-05-25 18:17:59 -0700 | [diff] [blame] | 237 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 238 | ::std::function<void(const Message *message)> watcher_; |
| 239 | |
| 240 | // Mutex and condition variable used to wait until the thread is started |
| 241 | // before going RT. |
| 242 | ::aos::Mutex thread_started_mutex_; |
| 243 | ::aos::Condition thread_started_condition_{&thread_started_mutex_}; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 244 | }; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 245 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 246 | // Adapter class to adapt a timerfd to a TimerHandler. |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 247 | // The part of the API which is accessed by the TimerHandler interface needs to |
| 248 | // be threadsafe. This means Setup and Disable. |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 249 | class TimerHandlerState : public TimerHandler { |
| 250 | public: |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 251 | TimerHandlerState(ShmEventLoop *shm_event_loop, ::std::function<void()> fn) |
| 252 | : shm_event_loop_(shm_event_loop), fn_(::std::move(fn)) { |
| 253 | shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() { |
| 254 | MutexLocker locker(&shm_event_loop_->thread_state_.mutex_); |
| 255 | timerfd_.Read(); |
| 256 | fn_(); |
| 257 | }); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 260 | ~TimerHandlerState() { shm_event_loop_->epoll_.DeleteFd(timerfd_.fd()); } |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 261 | |
| 262 | void Setup(monotonic_clock::time_point base, |
| 263 | monotonic_clock::duration repeat_offset) override { |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 264 | // SetTime is threadsafe already. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 265 | timerfd_.SetTime(base, repeat_offset); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 268 | void Disable() override { |
| 269 | // Disable is also threadsafe already. |
| 270 | timerfd_.Disable(); |
| 271 | } |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 272 | |
| 273 | private: |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 274 | ShmEventLoop *shm_event_loop_; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 275 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 276 | TimerFd timerfd_; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 277 | |
| 278 | // Function to be run on the thread |
| 279 | ::std::function<void()> fn_; |
| 280 | }; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 281 | |
| 282 | // Adapter class to the timerfd and PhasedLoop. |
| 283 | // The part of the API which is accessed by the PhasedLoopHandler interface |
| 284 | // needs to be threadsafe. This means set_interval_and_offset |
| 285 | class PhasedLoopHandler : public ::aos::PhasedLoopHandler { |
| 286 | public: |
| 287 | PhasedLoopHandler(ShmEventLoop *shm_event_loop, ::std::function<void(int)> fn, |
| 288 | const monotonic_clock::duration interval, |
| 289 | const monotonic_clock::duration offset) |
| 290 | : shm_event_loop_(shm_event_loop), |
| 291 | phased_loop_(interval, shm_event_loop_->monotonic_now(), offset), |
| 292 | fn_(::std::move(fn)) { |
| 293 | shm_event_loop_->epoll_.OnReadable(timerfd_.fd(), [this]() { |
| 294 | MutexLocker locker(&shm_event_loop_->thread_state_.mutex_); |
| 295 | { |
| 296 | MutexLocker locker(&mutex_); |
| 297 | timerfd_.Read(); |
| 298 | } |
| 299 | // Call the function. To avoid needing a recursive mutex, drop the lock |
| 300 | // before running the function. |
| 301 | fn_(cycles_elapsed_); |
| 302 | { |
| 303 | MutexLocker locker(&mutex_); |
| 304 | Reschedule(); |
| 305 | } |
| 306 | }); |
| 307 | } |
| 308 | |
| 309 | ~PhasedLoopHandler() { shm_event_loop_->epoll_.DeleteFd(timerfd_.fd()); } |
| 310 | |
| 311 | void set_interval_and_offset( |
| 312 | const monotonic_clock::duration interval, |
| 313 | const monotonic_clock::duration offset) override { |
| 314 | MutexLocker locker(&mutex_); |
| 315 | phased_loop_.set_interval_and_offset(interval, offset); |
| 316 | } |
| 317 | |
| 318 | void Startup() { |
| 319 | MutexLocker locker(&mutex_); |
| 320 | phased_loop_.Reset(shm_event_loop_->monotonic_now()); |
| 321 | Reschedule(); |
| 322 | } |
| 323 | |
| 324 | private: |
| 325 | // Reschedules the timer. Must be called with the mutex held. |
| 326 | void Reschedule() { |
| 327 | cycles_elapsed_ = phased_loop_.Iterate(shm_event_loop_->monotonic_now()); |
| 328 | timerfd_.SetTime(phased_loop_.sleep_time(), ::aos::monotonic_clock::zero()); |
| 329 | } |
| 330 | |
| 331 | ShmEventLoop *shm_event_loop_; |
| 332 | |
| 333 | // Mutex to protect access to the timerfd_ (not strictly necessary), and the |
| 334 | // phased_loop (necessary). |
| 335 | ::aos::Mutex mutex_; |
| 336 | |
| 337 | TimerFd timerfd_; |
| 338 | time::PhasedLoop phased_loop_; |
| 339 | |
| 340 | int cycles_elapsed_ = 1; |
| 341 | |
| 342 | // Function to be run |
| 343 | const ::std::function<void(int)> fn_; |
| 344 | }; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 345 | } // namespace internal |
| 346 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 347 | ::std::unique_ptr<RawFetcher> ShmEventLoop::MakeRawFetcher( |
| 348 | const ::std::string &path, const QueueTypeInfo &type) { |
| 349 | return ::std::unique_ptr<RawFetcher>(new ShmFetcher( |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 350 | RawQueue::Fetch(path.c_str(), type.size, type.hash, type.queue_length))); |
| 351 | } |
| 352 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 353 | ::std::unique_ptr<RawSender> ShmEventLoop::MakeRawSender( |
| 354 | const ::std::string &path, const QueueTypeInfo &type) { |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 355 | Take(path); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 356 | return ::std::unique_ptr<RawSender>(new ShmSender( |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 357 | RawQueue::Fetch(path.c_str(), type.size, type.hash, type.queue_length))); |
| 358 | } |
| 359 | |
| 360 | void ShmEventLoop::MakeRawWatcher( |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 361 | const ::std::string &path, const QueueTypeInfo &type, |
| 362 | ::std::function<void(const Message *message)> watcher) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 363 | Take(path); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 364 | ::std::unique_ptr<internal::WatcherThreadState> state( |
| 365 | new internal::WatcherThreadState( |
| 366 | &thread_state_, RawQueue::Fetch(path.c_str(), type.size, type.hash, |
| 367 | type.queue_length), |
| 368 | std::move(watcher))); |
| 369 | watchers_.push_back(::std::move(state)); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 372 | TimerHandler *ShmEventLoop::AddTimer(::std::function<void()> callback) { |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 373 | ::std::unique_ptr<internal::TimerHandlerState> timer( |
| 374 | new internal::TimerHandlerState(this, ::std::move(callback))); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 375 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 376 | timers_.push_back(::std::move(timer)); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 377 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 378 | return timers_.back().get(); |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 381 | PhasedLoopHandler *ShmEventLoop::AddPhasedLoop( |
| 382 | ::std::function<void(int)> callback, |
| 383 | const monotonic_clock::duration interval, |
| 384 | const monotonic_clock::duration offset) { |
| 385 | ::std::unique_ptr<internal::PhasedLoopHandler> phased_loop( |
| 386 | new internal::PhasedLoopHandler(this, ::std::move(callback), interval, |
| 387 | offset)); |
| 388 | |
| 389 | phased_loops_.push_back(::std::move(phased_loop)); |
| 390 | |
| 391 | return phased_loops_.back().get(); |
| 392 | } |
| 393 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 394 | void ShmEventLoop::OnRun(::std::function<void()> on_run) { |
| 395 | on_run_.push_back(::std::move(on_run)); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Austin Schuh | 0fc3b3e | 2019-06-29 13:56:21 -0700 | [diff] [blame] | 398 | void ShmEventLoop::set_name(const char *name) { thread_state_.name_ = name; } |
| 399 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 400 | void ShmEventLoop::Run() { |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 401 | // Start all the watcher threads. |
| 402 | for (::std::unique_ptr<internal::WatcherThreadState> &watcher : watchers_) { |
| 403 | watcher->Start(); |
| 404 | } |
| 405 | |
Austin Schuh | 0fc3b3e | 2019-06-29 13:56:21 -0700 | [diff] [blame] | 406 | ::aos::SetCurrentThreadName(thread_state_.name()); |
| 407 | |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 408 | // Now, all the threads are up. Lock everything into memory and go RT. |
| 409 | if (thread_state_.priority_ != -1) { |
| 410 | ::aos::InitRT(); |
| 411 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 412 | thread_state_.MaybeSetCurrentThreadRealtimePriority(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 413 | set_is_running(true); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 414 | |
| 415 | // Now that we are realtime (but before the OnRun handlers run), snap the |
| 416 | // queue index. |
| 417 | for (::std::unique_ptr<internal::WatcherThreadState> &watcher : watchers_) { |
| 418 | watcher->GrabQueueIndex(); |
| 419 | } |
| 420 | |
| 421 | // Now that we are RT, run all the OnRun handlers. |
| 422 | for (const auto &run : on_run_) { |
| 423 | run(); |
| 424 | } |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame] | 425 | |
| 426 | // Start up all the phased loops. |
| 427 | for (::std::unique_ptr<internal::PhasedLoopHandler> &phased_loop : |
| 428 | phased_loops_) { |
| 429 | phased_loop->Startup(); |
| 430 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 431 | // TODO(austin): We don't need a separate watcher thread if there are only |
| 432 | // watchers and fetchers. Could lazily create the epoll loop and pick a |
| 433 | // victim watcher to run in this thread. |
| 434 | // Trigger all the threads to start now. |
| 435 | thread_state_.Start(); |
| 436 | |
| 437 | // And start our main event loop which runs all the timers and handles Quit. |
| 438 | epoll_.Run(); |
| 439 | |
| 440 | // Once epoll exits, there is no useful nonrt work left to do. |
| 441 | set_is_running(false); |
| 442 | |
| 443 | // Signal all the watcher threads to exit. After this point, no more |
| 444 | // callbacks will be handled. |
| 445 | thread_state_.Exit(); |
| 446 | |
| 447 | // Nothing time or synchronization critical needs to happen after this point. |
| 448 | // Drop RT priority. |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 449 | ::aos::UnsetCurrentThreadRealtimePriority(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 450 | |
| 451 | // The watcher threads get cleaned up in the destructor. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 454 | void ShmEventLoop::ThreadState::Start() { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 455 | MutexLocker locker(&mutex_); |
| 456 | loop_running_ = true; |
| 457 | if (loop_finished_) ::aos::Die("Cannot restart an ShmEventLoop()"); |
| 458 | loop_running_cond_.Broadcast(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void ShmEventLoop::ThreadState::WaitForStart() { |
| 462 | MutexLocker locker(&mutex_); |
| 463 | while (!(loop_running_ || loop_finished_)) { |
| 464 | Condition::WaitResult wait_result = |
| 465 | loop_running_cond_.WaitTimed(chrono::milliseconds(1000)); |
| 466 | if (wait_result == Condition::WaitResult::kOwnerDied) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 467 | ::aos::Die("ShmEventLoop mutex lock problem.\n"); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 472 | void ShmEventLoop::ThreadState::MaybeSetCurrentThreadRealtimePriority() { |
| 473 | if (priority_ != -1) { |
| 474 | ::aos::SetCurrentThreadRealtimePriority(priority_); |
| 475 | } |
| 476 | } |
| 477 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 478 | void ShmEventLoop::Exit() { epoll_.Quit(); } |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 479 | |
| 480 | void ShmEventLoop::ThreadState::Exit() { |
| 481 | IPCRecursiveMutexLocker locker(&mutex_); |
| 482 | if (locker.owner_died()) ::aos::Die("Owner died"); |
| 483 | loop_running_ = false; |
| 484 | loop_finished_ = true; |
| 485 | loop_running_cond_.Broadcast(); |
| 486 | } |
| 487 | |
| 488 | ShmEventLoop::~ShmEventLoop() { |
| 489 | if (is_running()) { |
| 490 | ::aos::Die("ShmEventLoop destroyed while running\n"); |
| 491 | } |
| 492 | } |
| 493 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 494 | void ShmEventLoop::Take(const ::std::string &path) { |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 495 | if (is_running()) { |
| 496 | ::aos::Die("Cannot add new objects while running.\n"); |
| 497 | } |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 498 | |
| 499 | const auto prior = ::std::find(taken_.begin(), taken_.end(), path); |
| 500 | if (prior != taken_.end()) { |
| 501 | ::aos::Die("%s is already being used.", path.c_str()); |
| 502 | } else { |
| 503 | taken_.emplace_back(path); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
| 507 | } // namespace aos |