Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "hal/Notifier.h" |
| 6 | |
| 7 | #include <atomic> |
| 8 | #include <chrono> |
| 9 | #include <cstdio> |
| 10 | #include <cstring> |
| 11 | #include <string> |
| 12 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 13 | #include <wpi/SmallVector.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | #include <wpi/condition_variable.h> |
| 15 | #include <wpi/mutex.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | |
| 17 | #include "HALInitializer.h" |
| 18 | #include "NotifierInternal.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 19 | #include "hal/Errors.h" |
| 20 | #include "hal/HALBase.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 21 | #include "hal/cpp/fpga_clock.h" |
| 22 | #include "hal/handles/UnlimitedHandleResource.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 23 | #include "hal/simulation/NotifierData.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | struct Notifier { |
| 27 | std::string name; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 28 | uint64_t waitTime = UINT64_MAX; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 29 | bool active = true; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 30 | bool waitTimeValid = false; // True if waitTime is set and in the future |
| 31 | bool waitingForAlarm = false; // True if in HAL_WaitForNotifierAlarm() |
| 32 | uint64_t waitCount = 0; // Counts calls to HAL_WaitForNotifierAlarm() |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 33 | wpi::mutex mutex; |
| 34 | wpi::condition_variable cond; |
| 35 | }; |
| 36 | } // namespace |
| 37 | |
| 38 | using namespace hal; |
| 39 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 40 | static wpi::mutex notifiersWaiterMutex; |
| 41 | static wpi::condition_variable notifiersWaiterCond; |
| 42 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | class NotifierHandleContainer |
| 44 | : public UnlimitedHandleResource<HAL_NotifierHandle, Notifier, |
| 45 | HAL_HandleEnum::Notifier> { |
| 46 | public: |
| 47 | ~NotifierHandleContainer() { |
| 48 | ForEach([](HAL_NotifierHandle handle, Notifier* notifier) { |
| 49 | { |
| 50 | std::scoped_lock lock(notifier->mutex); |
| 51 | notifier->active = false; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 52 | notifier->waitTimeValid = false; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | } |
| 54 | notifier->cond.notify_all(); // wake up any waiting threads |
| 55 | }); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 56 | notifiersWaiterCond.notify_all(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 57 | } |
| 58 | }; |
| 59 | |
| 60 | static NotifierHandleContainer* notifierHandles; |
| 61 | static std::atomic<bool> notifiersPaused{false}; |
| 62 | |
| 63 | namespace hal { |
| 64 | namespace init { |
| 65 | void InitializeNotifier() { |
| 66 | static NotifierHandleContainer nH; |
| 67 | notifierHandles = &nH; |
| 68 | } |
| 69 | } // namespace init |
| 70 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 71 | void PauseNotifiers() { |
| 72 | notifiersPaused = true; |
| 73 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 74 | |
| 75 | void ResumeNotifiers() { |
| 76 | notifiersPaused = false; |
| 77 | WakeupNotifiers(); |
| 78 | } |
| 79 | |
| 80 | void WakeupNotifiers() { |
| 81 | notifierHandles->ForEach([](HAL_NotifierHandle handle, Notifier* notifier) { |
| 82 | notifier->cond.notify_all(); |
| 83 | }); |
| 84 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 85 | |
| 86 | void WaitNotifiers() { |
| 87 | std::unique_lock ulock(notifiersWaiterMutex); |
| 88 | wpi::SmallVector<HAL_NotifierHandle, 8> waiters; |
| 89 | |
| 90 | // Wait for all Notifiers to hit HAL_WaitForNotifierAlarm() |
| 91 | notifierHandles->ForEach([&](HAL_NotifierHandle handle, Notifier* notifier) { |
| 92 | std::scoped_lock lock(notifier->mutex); |
| 93 | if (notifier->active && !notifier->waitingForAlarm) { |
| 94 | waiters.emplace_back(handle); |
| 95 | } |
| 96 | }); |
| 97 | for (;;) { |
| 98 | int count = 0; |
| 99 | int end = waiters.size(); |
| 100 | while (count < end) { |
| 101 | auto& it = waiters[count]; |
| 102 | if (auto notifier = notifierHandles->Get(it)) { |
| 103 | std::scoped_lock lock(notifier->mutex); |
| 104 | if (notifier->active && !notifier->waitingForAlarm) { |
| 105 | ++count; |
| 106 | continue; |
| 107 | } |
| 108 | } |
| 109 | // No longer need to wait for it, put at end so it can be erased |
| 110 | std::swap(it, waiters[--end]); |
| 111 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 112 | if (count == 0) { |
| 113 | break; |
| 114 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 115 | waiters.resize(count); |
| 116 | notifiersWaiterCond.wait_for(ulock, std::chrono::duration<double>(1)); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void WakeupWaitNotifiers() { |
| 121 | std::unique_lock ulock(notifiersWaiterMutex); |
| 122 | int32_t status = 0; |
| 123 | uint64_t curTime = HAL_GetFPGATime(&status); |
| 124 | wpi::SmallVector<std::pair<HAL_NotifierHandle, uint64_t>, 8> waiters; |
| 125 | |
| 126 | // Wake up Notifiers that have expired timeouts |
| 127 | notifierHandles->ForEach([&](HAL_NotifierHandle handle, Notifier* notifier) { |
| 128 | std::scoped_lock lock(notifier->mutex); |
| 129 | |
| 130 | // Only wait for the Notifier if it has a valid timeout that's expired |
| 131 | if (notifier->active && notifier->waitTimeValid && |
| 132 | curTime >= notifier->waitTime) { |
| 133 | waiters.emplace_back(handle, notifier->waitCount); |
| 134 | notifier->cond.notify_all(); |
| 135 | } |
| 136 | }); |
| 137 | for (;;) { |
| 138 | int count = 0; |
| 139 | int end = waiters.size(); |
| 140 | while (count < end) { |
| 141 | auto& it = waiters[count]; |
| 142 | if (auto notifier = notifierHandles->Get(it.first)) { |
| 143 | std::scoped_lock lock(notifier->mutex); |
| 144 | |
| 145 | // waitCount is used here instead of waitingForAlarm because we want to |
| 146 | // wait until HAL_WaitForNotifierAlarm() is exited, then reentered |
| 147 | if (notifier->active && notifier->waitCount == it.second) { |
| 148 | ++count; |
| 149 | continue; |
| 150 | } |
| 151 | } |
| 152 | // No longer need to wait for it, put at end so it can be erased |
| 153 | it.swap(waiters[--end]); |
| 154 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 155 | if (count == 0) { |
| 156 | break; |
| 157 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 158 | waiters.resize(count); |
| 159 | notifiersWaiterCond.wait_for(ulock, std::chrono::duration<double>(1)); |
| 160 | } |
| 161 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 162 | } // namespace hal |
| 163 | |
| 164 | extern "C" { |
| 165 | |
| 166 | HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) { |
| 167 | hal::init::CheckInit(); |
| 168 | std::shared_ptr<Notifier> notifier = std::make_shared<Notifier>(); |
| 169 | HAL_NotifierHandle handle = notifierHandles->Allocate(notifier); |
| 170 | if (handle == HAL_kInvalidHandle) { |
| 171 | *status = HAL_HANDLE_ERROR; |
| 172 | return HAL_kInvalidHandle; |
| 173 | } |
| 174 | return handle; |
| 175 | } |
| 176 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 177 | HAL_Bool HAL_SetNotifierThreadPriority(HAL_Bool realTime, int32_t priority, |
| 178 | int32_t* status) { |
| 179 | return true; |
| 180 | } |
| 181 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 182 | void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle, const char* name, |
| 183 | int32_t* status) { |
| 184 | auto notifier = notifierHandles->Get(notifierHandle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 185 | if (!notifier) { |
| 186 | return; |
| 187 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 188 | std::scoped_lock lock(notifier->mutex); |
| 189 | notifier->name = name; |
| 190 | } |
| 191 | |
| 192 | void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { |
| 193 | auto notifier = notifierHandles->Get(notifierHandle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 194 | if (!notifier) { |
| 195 | return; |
| 196 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 197 | |
| 198 | { |
| 199 | std::scoped_lock lock(notifier->mutex); |
| 200 | notifier->active = false; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 201 | notifier->waitTimeValid = false; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 202 | } |
| 203 | notifier->cond.notify_all(); |
| 204 | } |
| 205 | |
| 206 | void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { |
| 207 | auto notifier = notifierHandles->Free(notifierHandle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 208 | if (!notifier) { |
| 209 | return; |
| 210 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 211 | |
| 212 | // Just in case HAL_StopNotifier() wasn't called... |
| 213 | { |
| 214 | std::scoped_lock lock(notifier->mutex); |
| 215 | notifier->active = false; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 216 | notifier->waitTimeValid = false; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 217 | } |
| 218 | notifier->cond.notify_all(); |
| 219 | } |
| 220 | |
| 221 | void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle, |
| 222 | uint64_t triggerTime, int32_t* status) { |
| 223 | auto notifier = notifierHandles->Get(notifierHandle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 224 | if (!notifier) { |
| 225 | return; |
| 226 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 227 | |
| 228 | { |
| 229 | std::scoped_lock lock(notifier->mutex); |
| 230 | notifier->waitTime = triggerTime; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 231 | notifier->waitTimeValid = (triggerTime != UINT64_MAX); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // We wake up any waiters to change how long they're sleeping for |
| 235 | notifier->cond.notify_all(); |
| 236 | } |
| 237 | |
| 238 | void HAL_CancelNotifierAlarm(HAL_NotifierHandle notifierHandle, |
| 239 | int32_t* status) { |
| 240 | auto notifier = notifierHandles->Get(notifierHandle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 241 | if (!notifier) { |
| 242 | return; |
| 243 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 244 | |
| 245 | { |
| 246 | std::scoped_lock lock(notifier->mutex); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 247 | notifier->waitTimeValid = false; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | uint64_t HAL_WaitForNotifierAlarm(HAL_NotifierHandle notifierHandle, |
| 252 | int32_t* status) { |
| 253 | auto notifier = notifierHandles->Get(notifierHandle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 254 | if (!notifier) { |
| 255 | return 0; |
| 256 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 257 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 258 | std::unique_lock ulock(notifiersWaiterMutex); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 259 | std::unique_lock lock(notifier->mutex); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 260 | notifier->waitingForAlarm = true; |
| 261 | ++notifier->waitCount; |
| 262 | ulock.unlock(); |
| 263 | notifiersWaiterCond.notify_all(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 264 | while (notifier->active) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 265 | uint64_t curTime = HAL_GetFPGATime(status); |
| 266 | if (notifier->waitTimeValid && curTime >= notifier->waitTime) { |
| 267 | notifier->waitTimeValid = false; |
| 268 | notifier->waitingForAlarm = false; |
| 269 | return curTime; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 270 | } |
| 271 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 272 | double waitDuration; |
| 273 | if (!notifier->waitTimeValid || notifiersPaused) { |
| 274 | // If not running, wait 1000 seconds |
| 275 | waitDuration = 1000.0; |
| 276 | } else { |
| 277 | waitDuration = (notifier->waitTime - curTime) * 1e-6; |
| 278 | } |
| 279 | |
| 280 | notifier->cond.wait_for(lock, std::chrono::duration<double>(waitDuration)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 281 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 282 | notifier->waitingForAlarm = false; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | uint64_t HALSIM_GetNextNotifierTimeout(void) { |
| 287 | uint64_t timeout = UINT64_MAX; |
| 288 | notifierHandles->ForEach([&](HAL_NotifierHandle, Notifier* notifier) { |
| 289 | std::scoped_lock lock(notifier->mutex); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 290 | if (notifier->active && notifier->waitTimeValid && |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 291 | timeout > notifier->waitTime) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 292 | timeout = notifier->waitTime; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 293 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 294 | }); |
| 295 | return timeout; |
| 296 | } |
| 297 | |
| 298 | int32_t HALSIM_GetNumNotifiers(void) { |
| 299 | int32_t count = 0; |
| 300 | notifierHandles->ForEach([&](HAL_NotifierHandle, Notifier* notifier) { |
| 301 | std::scoped_lock lock(notifier->mutex); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 302 | if (notifier->active) { |
| 303 | ++count; |
| 304 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 305 | }); |
| 306 | return count; |
| 307 | } |
| 308 | |
| 309 | int32_t HALSIM_GetNotifierInfo(struct HALSIM_NotifierInfo* arr, int32_t size) { |
| 310 | int32_t num = 0; |
| 311 | notifierHandles->ForEach([&](HAL_NotifierHandle handle, Notifier* notifier) { |
| 312 | std::scoped_lock lock(notifier->mutex); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 313 | if (!notifier->active) { |
| 314 | return; |
| 315 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 316 | if (num < size) { |
| 317 | arr[num].handle = handle; |
| 318 | if (notifier->name.empty()) { |
| 319 | std::snprintf(arr[num].name, sizeof(arr[num].name), "Notifier%d", |
| 320 | static_cast<int>(getHandleIndex(handle))); |
| 321 | } else { |
| 322 | std::strncpy(arr[num].name, notifier->name.c_str(), |
Austin Schuh | 75263e3 | 2022-02-22 18:05:32 -0800 | [diff] [blame] | 323 | sizeof(arr[num].name) - 1); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 324 | arr[num].name[sizeof(arr[num].name) - 1] = '\0'; |
| 325 | } |
| 326 | arr[num].timeout = notifier->waitTime; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 327 | arr[num].waitTimeValid = notifier->waitTimeValid; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 328 | } |
| 329 | ++num; |
| 330 | }); |
| 331 | return num; |
| 332 | } |
| 333 | |
| 334 | } // extern "C" |