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