[IE][VPU][XLink]: XLink semaphore wrappers impl (#3079)
[platform/upstream/dldt.git] / inference-engine / thirdparty / movidius / XLink / shared / include / XLinkSemaphore.h
1 // Copyright (C) 2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 ///
6 /// @file
7 ///
8 /// @brief     Application configuration Leon header
9 ///
10
11 #ifndef _XLINKSEMAPHORE_H
12 #define _XLINKSEMAPHORE_H
13
14 # if (defined(_WIN32) || defined(_WIN64))
15 #  include "win_pthread.h"
16 #  include "win_semaphore.h"
17 #  include "win_synchapi.h"
18 # else
19 #  include <pthread.h>
20 #  ifdef __APPLE__
21 #   include "pthread_semaphore.h"
22 #  else
23 #   include <semaphore.h>
24 # endif
25 # endif
26
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #endif
31
32 //
33 // This structure describes the semaphore used in XLink and
34 // extends the standard semaphore with a reference count.
35 // The counter is thread-safe and changes only in cases if
36 // all tools of thread synchronization are really unlocked.
37 // refs == -1 in case if semaphore was destroyed;
38 // refs == 0 in case if semaphore was initialized but has no waiters;
39 // refs == N in case if there are N waiters which called sem_wait().
40 //
41
42 typedef struct {
43     sem_t psem;
44     int refs;
45 } XLink_sem_t;
46
47 //
48 // XLink wrappers for POSIX semaphore functions (refer sem_overview for details)
49 // In description of standard sem_destroy the following can be noted:
50 // "Destroying a semaphore that other processes or threads are currently
51 // blocked on (in sem_wait(3)) produces undefined behavior."
52 // XLink wrappers use thread-safe reference count and destroy the semaphore only in case
53 // if there are no waiters
54 //
55
56 int XLink_sem_init(XLink_sem_t* sem, int pshared, unsigned int value);
57 int XLink_sem_destroy(XLink_sem_t* sem);
58 int XLink_sem_post(XLink_sem_t* sem);
59 int XLink_sem_wait(XLink_sem_t* sem);
60 int XLink_sem_timedwait(XLink_sem_t* sem, const struct timespec* abstime);
61
62 //
63 // Helper functions for XLink semaphore wrappers.
64 // Use them only in case if you know what you are doing.
65 //
66
67 int XLink_sem_set_refs(XLink_sem_t* sem, int refs);
68 int XLink_sem_get_refs(XLink_sem_t* sem, int *sval);
69
70 #ifdef __cplusplus
71 }
72 #endif
73
74 #endif  // _XLINKSEMAPHORE_H