Skip to content

Commit 19f0984

Browse files
committed
Add RAII locks for thread safety
- Removed the extra step checking for PolledBno devices, as the thread-safety additions ensure no streams will clash
1 parent da2daa6 commit 19f0984

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

Source/Onix1.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ std::vector<int> Onix1::getDeviceIndices(device_map_t deviceMap, int hubIndex)
120120

121121
int Onix1::get_opt_(int option, void* value, size_t* size) const
122122
{
123+
const ScopedLock lock(optionLock);
124+
123125
int rc = oni_get_opt(ctx_, option, value, size);
124126
if (rc != ONI_ESUCCESS)
125127
LOGE(oni_error_str(rc));
@@ -128,6 +130,8 @@ int Onix1::get_opt_(int option, void* value, size_t* size) const
128130

129131
int Onix1::readRegister(oni_dev_idx_t devIndex, oni_reg_addr_t registerAddress, oni_reg_val_t* value) const
130132
{
133+
const ScopedLock lock(registerLock);
134+
131135
int rc = oni_read_reg(ctx_, devIndex, registerAddress, value);
132136
if (rc != ONI_ESUCCESS)
133137
LOGE(oni_error_str(rc));
@@ -136,6 +140,8 @@ int Onix1::readRegister(oni_dev_idx_t devIndex, oni_reg_addr_t registerAddress,
136140

137141
int Onix1::writeRegister(oni_dev_idx_t devIndex, oni_reg_addr_t registerAddress, oni_reg_val_t value) const
138142
{
143+
const ScopedLock lock(registerLock);
144+
139145
int rc = oni_write_reg(ctx_, devIndex, registerAddress, value);
140146
if (rc != ONI_ESUCCESS)
141147
LOGE(oni_error_str(rc));
@@ -161,6 +167,8 @@ double Onix1::convertTimestampToSeconds(uint64_t timestamp) const
161167

162168
oni_frame_t* Onix1::readFrame() const
163169
{
170+
const ScopedLock lock(frameLock);
171+
164172
oni_frame_t* frame = nullptr;
165173
int rc = oni_read_frame(ctx_, &frame);
166174
if (rc < ONI_ESUCCESS)

Source/Onix1.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <system_error>
2828
#include <exception>
2929

30+
#include <DataThreadHeaders.h>
31+
3032
#include "../../plugin-GUI/Source/Utils/Utils.h"
3133

3234
namespace OnixSourcePlugin
@@ -90,6 +92,8 @@ namespace OnixSourcePlugin
9092
template <typename opt_t>
9193
int setOption(int option, const opt_t value)
9294
{
95+
const ScopedLock lock(optionLock);
96+
9397
int rc = oni_set_opt(ctx_, option, &value, opt_size_<opt_t>(value));
9498
if (rc != ONI_ESUCCESS) LOGE(oni_error_str(rc));
9599
return rc;
@@ -123,6 +127,10 @@ namespace OnixSourcePlugin
123127
/** The ONI ctx object */
124128
oni_ctx ctx_;
125129

130+
CriticalSection registerLock;
131+
CriticalSection frameLock;
132+
CriticalSection optionLock;
133+
126134
int major;
127135
int minor;
128136
int patch;

Source/OnixDevice.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222

2323
#pragma once
2424

25-
#include <DataThreadHeaders.h>
26-
2725
#include <ctime>
2826
#include <ratio>
2927
#include <chrono>

Source/OnixSource.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,12 +1052,6 @@ bool OnixSource::stopAcquisition()
10521052
if (!portA->getErrorFlag() && !portB->getErrorFlag())
10531053
waitForThreadToExit(2000);
10541054

1055-
for (const auto& polledBno055 : getDevices(OnixDeviceType::POLLEDBNO))
1056-
{
1057-
if (polledBno055 != nullptr && polledBno055->isEnabled())
1058-
polledBno055->stopAcquisition(); // NB: Polled BNO must be stopped before other devices to ensure there are no stream clashes
1059-
}
1060-
10611055
for (const auto& source : enabledSources)
10621056
{
10631057
source->stopAcquisition();

0 commit comments

Comments
 (0)