mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
Checks for system initialization in functions that can be called before creating handle based objects (#285)
This commit is contained in:
committed by
Peter Johnson
parent
c46c490376
commit
df4a2c07f0
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "DigitalInternal.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
@@ -27,7 +28,8 @@ std::unique_ptr<tDIO> digitalSystem;
|
||||
std::unique_ptr<tRelay> relaySystem;
|
||||
std::unique_ptr<tPWM> pwmSystem;
|
||||
|
||||
bool digitalSystemsInitialized = false;
|
||||
static std::atomic<bool> digitalSystemsInitialized{false};
|
||||
static priority_mutex initializeMutex;
|
||||
|
||||
DigitalHandleResource<HAL_DigitalHandle, DigitalPort,
|
||||
kNumDigitalChannels + kNumPWMHeaders>
|
||||
@@ -37,6 +39,11 @@ DigitalHandleResource<HAL_DigitalHandle, DigitalPort,
|
||||
* Initialize the digital system.
|
||||
*/
|
||||
void initializeDigital(int32_t* status) {
|
||||
// Initial check, as if it's true initialization has finished
|
||||
if (digitalSystemsInitialized) return;
|
||||
|
||||
std::lock_guard<priority_mutex> lock(initializeMutex);
|
||||
// Second check in case another thread was waiting
|
||||
if (digitalSystemsInitialized) return;
|
||||
|
||||
digitalSystem.reset(tDIO::create(status));
|
||||
|
||||
Reference in New Issue
Block a user