blob: 9f549d5ec6388bbdea102a10a85d5df9932ba98b [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "hal/Notifier.h"
9
10#include <chrono>
11
12#include <wpi/condition_variable.h>
13#include <wpi/mutex.h>
14#include <wpi/timestamp.h>
15
16#include "HALInitializer.h"
17#include "hal/HAL.h"
18#include "hal/cpp/fpga_clock.h"
19#include "hal/handles/UnlimitedHandleResource.h"
20
21namespace {
22struct Notifier {
23 uint64_t waitTime;
24 bool updatedAlarm = false;
25 bool active = true;
26 bool running = false;
27 wpi::mutex mutex;
28 wpi::condition_variable cond;
29};
30} // namespace
31
32using namespace hal;
33
34class NotifierHandleContainer
35 : public UnlimitedHandleResource<HAL_NotifierHandle, Notifier,
36 HAL_HandleEnum::Notifier> {
37 public:
38 ~NotifierHandleContainer() {
39 ForEach([](HAL_NotifierHandle handle, Notifier* notifier) {
40 {
41 std::lock_guard<wpi::mutex> lock(notifier->mutex);
42 notifier->active = false;
43 notifier->running = false;
44 }
45 notifier->cond.notify_all(); // wake up any waiting threads
46 });
47 }
48};
49
50static NotifierHandleContainer* notifierHandles;
51
52namespace hal {
53namespace init {
54void InitializeNotifier() {
55 static NotifierHandleContainer nH;
56 notifierHandles = &nH;
57}
58} // namespace init
59} // namespace hal
60
61extern "C" {
62
63HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) {
64 hal::init::CheckInit();
65 std::shared_ptr<Notifier> notifier = std::make_shared<Notifier>();
66 HAL_NotifierHandle handle = notifierHandles->Allocate(notifier);
67 if (handle == HAL_kInvalidHandle) {
68 *status = HAL_HANDLE_ERROR;
69 return HAL_kInvalidHandle;
70 }
71 return handle;
72}
73
74void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
75 auto notifier = notifierHandles->Get(notifierHandle);
76 if (!notifier) return;
77
78 {
79 std::lock_guard<wpi::mutex> lock(notifier->mutex);
80 notifier->active = false;
81 notifier->running = false;
82 }
83 notifier->cond.notify_all();
84}
85
86void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) {
87 auto notifier = notifierHandles->Free(notifierHandle);
88 if (!notifier) return;
89
90 // Just in case HAL_StopNotifier() wasn't called...
91 {
92 std::lock_guard<wpi::mutex> lock(notifier->mutex);
93 notifier->active = false;
94 notifier->running = false;
95 }
96 notifier->cond.notify_all();
97}
98
99void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle,
100 uint64_t triggerTime, int32_t* status) {
101 auto notifier = notifierHandles->Get(notifierHandle);
102 if (!notifier) return;
103
104 {
105 std::lock_guard<wpi::mutex> lock(notifier->mutex);
106 notifier->waitTime = triggerTime;
107 notifier->running = true;
108 notifier->updatedAlarm = true;
109 }
110
111 // We wake up any waiters to change how long they're sleeping for
112 notifier->cond.notify_all();
113}
114
115void HAL_CancelNotifierAlarm(HAL_NotifierHandle notifierHandle,
116 int32_t* status) {
117 auto notifier = notifierHandles->Get(notifierHandle);
118 if (!notifier) return;
119
120 {
121 std::lock_guard<wpi::mutex> lock(notifier->mutex);
122 notifier->running = false;
123 }
124}
125
126uint64_t HAL_WaitForNotifierAlarm(HAL_NotifierHandle notifierHandle,
127 int32_t* status) {
128 auto notifier = notifierHandles->Get(notifierHandle);
129 if (!notifier) return 0;
130
131 std::unique_lock<wpi::mutex> lock(notifier->mutex);
132 while (notifier->active) {
133 double waitTime;
134 if (!notifier->running) {
135 waitTime = (HAL_GetFPGATime(status) * 1e-6) + 1000.0;
136 // If not running, wait 1000 seconds
137 } else {
138 waitTime = notifier->waitTime * 1e-6;
139 }
140
141 // Don't wait twice
142 notifier->updatedAlarm = false;
143
144 auto timeoutTime =
145 hal::fpga_clock::epoch() + std::chrono::duration<double>(waitTime);
146 notifier->cond.wait_until(lock, timeoutTime);
147 if (notifier->updatedAlarm) {
148 notifier->updatedAlarm = false;
149 continue;
150 }
151 if (!notifier->running) continue;
152 if (!notifier->active) break;
153 notifier->running = false;
154 return HAL_GetFPGATime(status);
155 }
156 return 0;
157}
158
159} // extern "C"