Skip to content

Commit

Permalink
remove/deprecate unsafe XLinkConnection::isClosed/checkClosed
Browse files Browse the repository at this point in the history
- partial fix luxonis#520
  • Loading branch information
diablodale committed Mar 14, 2023
1 parent ddca0b2 commit 603dc9e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
5 changes: 3 additions & 2 deletions include/depthai/xlink/XLinkConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ class XLinkConnection {

/**
* Is the connection already closed (or disconnected)
*
* @warning This function is thread-unsafe and may return incorrect values.
*/
bool isClosed() const;
[[deprecated("thread-unsafe and may return incorrect values")]] bool isClosed() const;

private:
friend struct XLinkReadError;
Expand All @@ -116,7 +118,6 @@ class XLinkConnection {
static std::string convertErrorCodeToString(XLinkError_t errorCode);

void initDevice(const DeviceInfo& deviceToInit, XLinkDeviceState_t expectedState = X_LINK_BOOTED);
void checkClosed() const;

bool bootDevice = true;
bool bootWithPath = true;
Expand Down
11 changes: 5 additions & 6 deletions src/xlink/XLinkConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,16 @@ XLinkConnection::XLinkConnection(const DeviceInfo& deviceDesc, XLinkDeviceState_
initDevice(deviceDesc, expectedState);
}

// BUGBUG this function is thread-unsafe. The `closed` value is only known and valid
// 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::unique_lock<std::mutex> lock(closedMtx);
std::lock_guard<std::mutex> lock(closedMtx);
return closed;
}

void XLinkConnection::checkClosed() const {
if(isClosed()) throw std::invalid_argument("XLinkConnection already closed or disconnected");
}

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

using namespace std::chrono;
Expand Down

0 comments on commit 603dc9e

Please sign in to comment.