Skip to content

Commit

Permalink
[IE][VPU][XLink]: XLink semaphore wrappers impl (#3079)
Browse files Browse the repository at this point in the history
XLink wrappers for POSIX semaphore functions (refer sem_overview for details). In the description of standard sem_destroy the following is noted:
"Destroying a semaphore that other processes or threads are currently blocked on (in sem_wait(3)) produces undefined behavior."
XLink wrappers use thread-safe reference count and destroy the semaphore only in case if there are no waiters.

* XLink semaphore wrapper impl
* Extend XLink win_synchapi
  • Loading branch information
Maxim-Doronin authored Nov 15, 2020
1 parent fec3bc0 commit 2a7f2f5
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
extern "C" {
#endif

#define PTHREAD_COND_INITIALIZER {0}

typedef struct _pthread_condattr_t pthread_condattr_t;

typedef struct
Expand All @@ -23,9 +25,13 @@ pthread_cond_t;
int pthread_cond_init(pthread_cond_t* __cond, const pthread_condattr_t* __cond_attr);
int pthread_cond_destroy(pthread_cond_t* __cond);

int pthread_cond_wait(pthread_cond_t *__cond,
pthread_mutex_t *__mutex);
int pthread_cond_timedwait(pthread_cond_t* __cond,
pthread_mutex_t* __mutex,
const struct timespec* __abstime);

int pthread_cond_signal(pthread_cond_t* __cond);
int pthread_cond_broadcast(pthread_cond_t* __cond);

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ int pthread_cond_destroy(pthread_cond_t* __cond)
return 0;
}

int pthread_cond_wait(pthread_cond_t *__cond,
pthread_mutex_t *__mutex)
{
if (__cond == NULL || __mutex == NULL)
return ERROR_INVALID_HANDLE;
return pthread_cond_timedwait(__cond, __mutex, NULL);
}

int pthread_cond_timedwait(pthread_cond_t* __cond,
pthread_mutex_t* __mutex,
const struct timespec* __abstime)
const struct timespec* __abstime)
{
if (__cond == NULL) {
return ERROR_INVALID_HANDLE;
Expand All @@ -42,7 +50,7 @@ int pthread_cond_timedwait(pthread_cond_t* __cond,
return rc == ERROR_TIMEOUT ? ETIMEDOUT : rc;
}

int pthread_cond_broadcast(pthread_cond_t *__cond)
int pthread_cond_signal(pthread_cond_t *__cond)
{
if (__cond == NULL) {
return ERROR_INVALID_HANDLE;
Expand All @@ -51,3 +59,13 @@ int pthread_cond_broadcast(pthread_cond_t *__cond)
WakeConditionVariable(&__cond->_cv);
return 0;
}

int pthread_cond_broadcast(pthread_cond_t *__cond)
{
if (__cond == NULL) {
return ERROR_INVALID_HANDLE;
}

WakeAllConditionVariable(&__cond->_cv);
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef struct xLinkDesc_t {
xLinkState_t peerState;
xLinkDeviceHandle_t deviceHandle;
linkId_t id;
sem_t dispatcherClosedSem;
XLink_sem_t dispatcherClosedSem;

//Deprecated fields. Begin.
int hostClosedFD;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (C) 2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

///
/// @file
///
/// @brief Application configuration Leon header
///

#ifndef _XLINKSEMAPHORE_H
#define _XLINKSEMAPHORE_H

# if (defined(_WIN32) || defined(_WIN64))
# include "win_pthread.h"
# include "win_semaphore.h"
# include "win_synchapi.h"
# else
# include <pthread.h>
# ifdef __APPLE__
# include "pthread_semaphore.h"
# else
# include <semaphore.h>
# endif
# endif

#ifdef __cplusplus
extern "C"
{
#endif

//
// This structure describes the semaphore used in XLink and
// extends the standard semaphore with a reference count.
// The counter is thread-safe and changes only in cases if
// all tools of thread synchronization are really unlocked.
// refs == -1 in case if semaphore was destroyed;
// refs == 0 in case if semaphore was initialized but has no waiters;
// refs == N in case if there are N waiters which called sem_wait().
//

typedef struct {
sem_t psem;
int refs;
} XLink_sem_t;

//
// XLink wrappers for POSIX semaphore functions (refer sem_overview for details)
// In description of standard sem_destroy the following can be noted:
// "Destroying a semaphore that other processes or threads are currently
// blocked on (in sem_wait(3)) produces undefined behavior."
// XLink wrappers use thread-safe reference count and destroy the semaphore only in case
// if there are no waiters
//

int XLink_sem_init(XLink_sem_t* sem, int pshared, unsigned int value);
int XLink_sem_destroy(XLink_sem_t* sem);
int XLink_sem_post(XLink_sem_t* sem);
int XLink_sem_wait(XLink_sem_t* sem);
int XLink_sem_timedwait(XLink_sem_t* sem, const struct timespec* abstime);

//
// Helper functions for XLink semaphore wrappers.
// Use them only in case if you know what you are doing.
//

int XLink_sem_set_refs(XLink_sem_t* sem, int refs);
int XLink_sem_get_refs(XLink_sem_t* sem, int *sval);

#ifdef __cplusplus
}
#endif

#endif // _XLINKSEMAPHORE_H
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@
#define _XLINKSTREAM_H

#include "XLinkPublicDefines.h"

# if (defined(_WIN32) || defined(_WIN64))
# include "win_semaphore.h"
# else
# ifdef __APPLE__
# include "pthread_semaphore.h"
# else
# include <semaphore.h>
# endif
# endif
#include "XLinkSemaphore.h"

/**
* @brief Streams opened to device
Expand All @@ -40,7 +31,7 @@ typedef struct{

uint32_t closeStreamInitiated;

sem_t sem;
XLink_sem_t sem;
}streamDesc_t;

XLinkError_t XLinkStreamInitialize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ XLinkError_t XLinkResetRemote(linkId_t id)
XLINK_RET_ERR_IF(DispatcherWaitEventComplete(&link->deviceHandle),
X_LINK_TIMEOUT);

if(sem_wait(&link->dispatcherClosedSem)) {
if(XLink_sem_wait(&link->dispatcherClosedSem)) {
mvLog(MVLOG_ERROR,"can't wait dispatcherClosedSem\n");
return X_LINK_ERROR;
}
Expand Down Expand Up @@ -390,7 +390,7 @@ static xLinkDesc_t* getNextAvailableLink() {

xLinkDesc_t* link = &availableXLinks[i];

if (sem_init(&link->dispatcherClosedSem, 0 ,0)) {
if (XLink_sem_init(&link->dispatcherClosedSem, 0 ,0)) {
mvLog(MVLOG_ERROR, "Cannot initialize semaphore\n");
return NULL;
}
Expand Down
Loading

0 comments on commit 2a7f2f5

Please sign in to comment.