Skip to content

Commit

Permalink
xlink init unlocks mutex on errors + allows retry
Browse files Browse the repository at this point in the history
  • Loading branch information
diablodale committed May 8, 2022
1 parent ff9f8e7 commit 197b2fc
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/shared/XLinkDevice.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,18 @@ static XLinkError_t parsePlatformError(xLinkPlatformErrorCode_t rc);

XLinkError_t XLinkInitialize(XLinkGlobalHandler_t* globalHandler)
{
XLINK_RET_IF(globalHandler == NULL);
XLINK_RET_ERR_IF(pthread_mutex_lock(&init_mutex), X_LINK_ERROR);
if(init_once){
// BUGBUG missing pthread_mutex_unlock()?
pthread_mutex_unlock(&init_mutex);
return X_LINK_SUCCESS;
}
// BUGBUG only allow init once? Even if there was an error on previous try?
init_once = 1;

#ifdef __DEVICE__
mvLogLevelSet(MVLOG_FATAL);
mvLogDefaultLevelSet(MVLOG_FATAL);
#endif

XLINK_RET_IF(globalHandler == NULL);
ASSERT_XLINK(XLINK_MAX_STREAMS <= MAX_POOLS_ALLOC);
glHandler = globalHandler;
if (sem_init(&pingSem,0,0)) {
Expand Down Expand Up @@ -120,7 +118,11 @@ XLinkError_t XLinkInitialize(XLinkGlobalHandler_t* globalHandler)
controlFunctionTbl.closeLink = &dispatcherCloseLink;
controlFunctionTbl.closeDeviceFd = &dispatcherCloseDeviceFd;

XLINK_RET_IF(DispatcherInitialize(&controlFunctionTbl));
if (DispatcherInitialize(&controlFunctionTbl)) {
mvLog(MVLOG_ERROR, "Condition failed: DispatcherInitialize(&controlFunctionTbl)");
pthread_mutex_unlock(&init_mutex);
return X_LINK_ERROR;
}

//initialize availableStreams
memset(availableXLinks, 0, sizeof(availableXLinks));
Expand All @@ -139,24 +141,34 @@ XLinkError_t XLinkInitialize(XLinkGlobalHandler_t* globalHandler)

#ifdef __DEVICE__
link = getNextAvailableLink();
if (link == NULL)
// BUGBUG missing pthread_mutex_unlock()?
if (link == NULL) {
pthread_mutex_unlock(&init_mutex);
return X_LINK_COMMUNICATION_NOT_OPEN;
}
link->peerState = XLINK_UP;
link->deviceHandle.xLinkFD = NULL;
link->deviceHandle.protocol = globalHandler->protocol;

xLinkDeviceHandle_t temp = {0};
temp.protocol = globalHandler->protocol;
XLINK_RET_IF_FAIL(DispatcherStart(&temp)); //myriad has one
{
int rc;
if (rc = DispatcherStart(&temp)) { //myriad has one
mvLog(MVLOG_ERROR, " DispatcherStart(&temp) method call failed with an error: %d", rc);
pthread_mutex_unlock(&init_mutex);
return rc;
}
}

while(((sem_wait(&pingSem) == -1) && errno == EINTR)
continue;

#endif

init_once = 1;
int status = pthread_mutex_unlock(&init_mutex);
if(status){
// rare and unstable scenario; xlink is technically initialized yet mutex unlock failed
return X_LINK_ERROR;
}

Expand Down

0 comments on commit 197b2fc

Please sign in to comment.