Skip to content

Commit

Permalink
change closed back to atomic<bool>
Browse files Browse the repository at this point in the history
- revert more closed locking behavior #520
  • Loading branch information
diablodale committed Apr 24, 2023
1 parent c9e8062 commit 45cb91c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 15 deletions.
3 changes: 1 addition & 2 deletions include/depthai/device/DeviceBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,8 +833,7 @@ class DeviceBase {
std::chrono::steady_clock::time_point lastWatchdogPingTime;

// closed
mutable std::mutex closedMtx;
bool closed{false};
std::atomic<bool> closed{false};

// pimpl
class Impl;
Expand Down
3 changes: 1 addition & 2 deletions include/depthai/xlink/XLinkConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ class XLinkConnection {
DeviceInfo deviceInfo;

// closed
mutable std::mutex closedMtx;
bool closed{false};
std::atomic<bool> closed{false};

constexpr static std::chrono::milliseconds WAIT_FOR_BOOTUP_TIMEOUT{15000};
constexpr static std::chrono::milliseconds WAIT_FOR_CONNECT_TIMEOUT{5000};
Expand Down
10 changes: 4 additions & 6 deletions src/device/DeviceBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,10 @@ DeviceBase::DeviceBase(Config config, const DeviceInfo& devInfo) : deviceInfo(de
}

void DeviceBase::close() {
std::unique_lock<std::mutex> lock(closedMtx);
if(!closed) {
closeImpl();
closed = true;
}
// Only allow to close once
if(closed.exchange(true)) return;

closeImpl();
}

void DeviceBase::closeImpl() {
Expand Down Expand Up @@ -430,7 +429,6 @@ void DeviceBase::closeImpl() {
// is invalidated during the return by value and continues to degrade in
// validity to the caller
bool DeviceBase::isClosed() const {
std::unique_lock<std::mutex> lock(closedMtx);
return closed || !watchdogRunning;
}

Expand Down
6 changes: 1 addition & 5 deletions src/xlink/XLinkConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,11 @@ XLinkConnection::XLinkConnection(const DeviceInfo& deviceDesc, XLinkDeviceState_
// within the context of the lock_guard. The value is immediately invalid and outdated
// when it is returned by value to the caller
bool XLinkConnection::isClosed() const {
std::lock_guard<std::mutex> lock(closedMtx);
return closed;
}

void XLinkConnection::close() {
std::lock_guard<std::mutex> lock(closedMtx);
if(closed) return;
if(closed.exchange(true)) return;

constexpr auto RESET_TIMEOUT = 2s;
constexpr auto BOOTUP_SEARCH = 5s;
Expand Down Expand Up @@ -332,8 +330,6 @@ void XLinkConnection::close() {

spdlog::debug("XLinkResetRemote of linkId: ({})", previousLinkId);
}

closed = true;
}

XLinkConnection::~XLinkConnection() {
Expand Down

0 comments on commit 45cb91c

Please sign in to comment.