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 <cstdlib> // For std::atexit() |
| 9 | #include <memory> |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 10 | #include <thread> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 12 | #include <fmt/core.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 13 | #include <wpi/condition_variable.h> |
| 14 | #include <wpi/mutex.h> |
| 15 | |
| 16 | #include "HALInitializer.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 17 | #include "HALInternal.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 18 | #include "hal/ChipObject.h" |
| 19 | #include "hal/Errors.h" |
| 20 | #include "hal/HAL.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 21 | #include "hal/Threads.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 22 | #include "hal/handles/UnlimitedHandleResource.h" |
| 23 | |
| 24 | using namespace hal; |
| 25 | |
| 26 | static constexpr int32_t kTimerInterruptNumber = 28; |
| 27 | |
| 28 | static wpi::mutex notifierMutex; |
| 29 | static std::unique_ptr<tAlarm> notifierAlarm; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 30 | static std::thread notifierThread; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 31 | static HAL_Bool notifierThreadRealTime = false; |
| 32 | static int32_t notifierThreadPriority = 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 33 | static uint64_t closestTrigger{UINT64_MAX}; |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | struct Notifier { |
| 38 | uint64_t triggerTime = UINT64_MAX; |
| 39 | uint64_t triggeredTime = UINT64_MAX; |
| 40 | bool active = true; |
| 41 | wpi::mutex mutex; |
| 42 | wpi::condition_variable cond; |
| 43 | }; |
| 44 | |
| 45 | } // namespace |
| 46 | |
| 47 | static std::atomic_flag notifierAtexitRegistered{ATOMIC_FLAG_INIT}; |
| 48 | static std::atomic_int notifierRefCount{0}; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 49 | static std::atomic_bool notifierRunning{false}; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 50 | |
| 51 | using namespace hal; |
| 52 | |
| 53 | class NotifierHandleContainer |
| 54 | : public UnlimitedHandleResource<HAL_NotifierHandle, Notifier, |
| 55 | HAL_HandleEnum::Notifier> { |
| 56 | public: |
| 57 | ~NotifierHandleContainer() { |
| 58 | ForEach([](HAL_NotifierHandle handle, Notifier* notifier) { |
| 59 | { |
| 60 | std::scoped_lock lock(notifier->mutex); |
| 61 | notifier->triggerTime = UINT64_MAX; |
| 62 | notifier->triggeredTime = 0; |
| 63 | notifier->active = false; |
| 64 | } |
| 65 | notifier->cond.notify_all(); // wake up any waiting threads |
| 66 | }); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | static NotifierHandleContainer* notifierHandles; |
| 71 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 72 | static void alarmCallback() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 73 | std::scoped_lock lock(notifierMutex); |
| 74 | int32_t status = 0; |
| 75 | uint64_t currentTime = 0; |
| 76 | |
| 77 | // the hardware disables itself after each alarm |
| 78 | closestTrigger = UINT64_MAX; |
| 79 | |
| 80 | // process all notifiers |
| 81 | notifierHandles->ForEach([&](HAL_NotifierHandle handle, Notifier* notifier) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 82 | if (notifier->triggerTime == UINT64_MAX) { |
| 83 | return; |
| 84 | } |
| 85 | if (currentTime == 0) { |
| 86 | currentTime = HAL_GetFPGATime(&status); |
| 87 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 88 | std::unique_lock lock(notifier->mutex); |
| 89 | if (notifier->triggerTime < currentTime) { |
| 90 | notifier->triggerTime = UINT64_MAX; |
| 91 | notifier->triggeredTime = currentTime; |
| 92 | lock.unlock(); |
| 93 | notifier->cond.notify_all(); |
| 94 | } else if (notifier->triggerTime < closestTrigger) { |
| 95 | closestTrigger = notifier->triggerTime; |
| 96 | } |
| 97 | }); |
| 98 | |
| 99 | if (notifierAlarm && closestTrigger != UINT64_MAX) { |
| 100 | // Simply truncate the hardware trigger time to 32-bit. |
| 101 | notifierAlarm->writeTriggerTime(static_cast<uint32_t>(closestTrigger), |
| 102 | &status); |
| 103 | // Enable the alarm. The hardware disables itself after each alarm. |
| 104 | notifierAlarm->writeEnable(true, &status); |
| 105 | } |
| 106 | } |
| 107 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 108 | static void notifierThreadMain() { |
| 109 | tRioStatusCode status = 0; |
| 110 | tInterruptManager manager{1 << kTimerInterruptNumber, true, &status}; |
| 111 | while (notifierRunning) { |
| 112 | auto triggeredMask = manager.watch(10000, false, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 113 | if (!notifierRunning) { |
| 114 | break; |
| 115 | } |
| 116 | if (triggeredMask == 0) |
| 117 | continue; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 118 | alarmCallback(); |
| 119 | } |
| 120 | } |
| 121 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 122 | static void cleanupNotifierAtExit() { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 123 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 124 | if (notifierAlarm) |
| 125 | notifierAlarm->writeEnable(false, &status); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 126 | notifierAlarm = nullptr; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 127 | notifierRunning = false; |
| 128 | hal::ReleaseFPGAInterrupt(kTimerInterruptNumber); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 129 | if (notifierThread.joinable()) { |
| 130 | notifierThread.join(); |
| 131 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 134 | namespace hal::init { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 135 | void InitializeNotifier() { |
| 136 | static NotifierHandleContainer nH; |
| 137 | notifierHandles = &nH; |
| 138 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 139 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 140 | |
| 141 | extern "C" { |
| 142 | |
| 143 | HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) { |
| 144 | hal::init::CheckInit(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 145 | if (!notifierAtexitRegistered.test_and_set()) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 146 | std::atexit(cleanupNotifierAtExit); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 147 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 148 | |
| 149 | if (notifierRefCount.fetch_add(1) == 0) { |
| 150 | std::scoped_lock lock(notifierMutex); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 151 | notifierRunning = true; |
| 152 | notifierThread = std::thread(notifierThreadMain); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 153 | |
| 154 | auto native = notifierThread.native_handle(); |
| 155 | HAL_SetThreadPriority(&native, notifierThreadRealTime, |
| 156 | notifierThreadPriority, status); |
| 157 | if (*status == HAL_THREAD_PRIORITY_ERROR) { |
| 158 | *status = 0; |
| 159 | fmt::print("{}: HAL Notifier thread\n", |
| 160 | HAL_THREAD_PRIORITY_ERROR_MESSAGE); |
| 161 | } |
| 162 | if (*status == HAL_THREAD_PRIORITY_RANGE_ERROR) { |
| 163 | *status = 0; |
| 164 | fmt::print("{}: HAL Notifier thread\n", |
| 165 | HAL_THREAD_PRIORITY_RANGE_ERROR_MESSAGE); |
| 166 | } |
| 167 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 168 | notifierAlarm.reset(tAlarm::create(status)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | std::shared_ptr<Notifier> notifier = std::make_shared<Notifier>(); |
| 172 | HAL_NotifierHandle handle = notifierHandles->Allocate(notifier); |
| 173 | if (handle == HAL_kInvalidHandle) { |
| 174 | *status = HAL_HANDLE_ERROR; |
| 175 | return HAL_kInvalidHandle; |
| 176 | } |
| 177 | return handle; |
| 178 | } |
| 179 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 180 | HAL_Bool HAL_SetNotifierThreadPriority(HAL_Bool realTime, int32_t priority, |
| 181 | int32_t* status) { |
| 182 | std::scoped_lock lock(notifierMutex); |
| 183 | notifierThreadRealTime = realTime; |
| 184 | notifierThreadPriority = priority; |
| 185 | if (notifierThread.joinable()) { |
| 186 | auto native = notifierThread.native_handle(); |
| 187 | return HAL_SetThreadPriority(&native, realTime, priority, status); |
| 188 | } else { |
| 189 | return true; |
| 190 | } |
| 191 | } |
| 192 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 193 | void HAL_SetNotifierName(HAL_NotifierHandle notifierHandle, const char* name, |
| 194 | int32_t* status) {} |
| 195 | |
| 196 | void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { |
| 197 | auto notifier = notifierHandles->Get(notifierHandle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 198 | if (!notifier) |
| 199 | return; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 200 | |
| 201 | { |
| 202 | std::scoped_lock lock(notifier->mutex); |
| 203 | notifier->triggerTime = UINT64_MAX; |
| 204 | notifier->triggeredTime = 0; |
| 205 | notifier->active = false; |
| 206 | } |
| 207 | notifier->cond.notify_all(); // wake up any waiting threads |
| 208 | } |
| 209 | |
| 210 | void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { |
| 211 | auto notifier = notifierHandles->Free(notifierHandle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 212 | if (!notifier) |
| 213 | return; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 214 | |
| 215 | // Just in case HAL_StopNotifier() wasn't called... |
| 216 | { |
| 217 | std::scoped_lock lock(notifier->mutex); |
| 218 | notifier->triggerTime = UINT64_MAX; |
| 219 | notifier->triggeredTime = 0; |
| 220 | notifier->active = false; |
| 221 | } |
| 222 | notifier->cond.notify_all(); |
| 223 | |
| 224 | if (notifierRefCount.fetch_sub(1) == 1) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 225 | // if this was the last notifier, clean up alarm and thread |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 226 | // the notifier can call back into our callback, so don't hold the lock |
| 227 | // here (the atomic fetch_sub will prevent multiple parallel entries |
| 228 | // into this function) |
| 229 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 230 | if (notifierAlarm) |
| 231 | notifierAlarm->writeEnable(false, status); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 232 | notifierRunning = false; |
| 233 | hal::ReleaseFPGAInterrupt(kTimerInterruptNumber); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 234 | if (notifierThread.joinable()) { |
| 235 | notifierThread.join(); |
| 236 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 237 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 238 | std::scoped_lock lock(notifierMutex); |
| 239 | notifierAlarm = nullptr; |
| 240 | closestTrigger = UINT64_MAX; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle, |
| 245 | uint64_t triggerTime, int32_t* status) { |
| 246 | auto notifier = notifierHandles->Get(notifierHandle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 247 | if (!notifier) |
| 248 | return; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 249 | |
| 250 | { |
| 251 | std::scoped_lock lock(notifier->mutex); |
| 252 | notifier->triggerTime = triggerTime; |
| 253 | notifier->triggeredTime = UINT64_MAX; |
| 254 | } |
| 255 | |
| 256 | std::scoped_lock lock(notifierMutex); |
| 257 | // Update alarm time if closer than current. |
| 258 | if (triggerTime < closestTrigger) { |
| 259 | bool wasActive = (closestTrigger != UINT64_MAX); |
| 260 | closestTrigger = triggerTime; |
| 261 | // Simply truncate the hardware trigger time to 32-bit. |
| 262 | notifierAlarm->writeTriggerTime(static_cast<uint32_t>(closestTrigger), |
| 263 | status); |
| 264 | // Enable the alarm. |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 265 | if (!wasActive) |
| 266 | notifierAlarm->writeEnable(true, status); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | void HAL_CancelNotifierAlarm(HAL_NotifierHandle notifierHandle, |
| 271 | int32_t* status) { |
| 272 | auto notifier = notifierHandles->Get(notifierHandle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 273 | if (!notifier) |
| 274 | return; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 275 | |
| 276 | { |
| 277 | std::scoped_lock lock(notifier->mutex); |
| 278 | notifier->triggerTime = UINT64_MAX; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | uint64_t HAL_WaitForNotifierAlarm(HAL_NotifierHandle notifierHandle, |
| 283 | int32_t* status) { |
| 284 | auto notifier = notifierHandles->Get(notifierHandle); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 285 | if (!notifier) |
| 286 | return 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 287 | std::unique_lock lock(notifier->mutex); |
| 288 | notifier->cond.wait(lock, [&] { |
| 289 | return !notifier->active || notifier->triggeredTime != UINT64_MAX; |
| 290 | }); |
| 291 | return notifier->active ? notifier->triggeredTime : 0; |
| 292 | } |
| 293 | |
| 294 | } // extern "C" |