[IE][VPU][XLink]: XLink semaphore wrappers impl (#3079)
[platform/upstream/dldt.git] / inference-engine / thirdparty / movidius / XLink / pc / Win / src / win_synchapi.c
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include "win_synchapi.h"
6
7 int pthread_cond_init(pthread_cond_t* __cond, const pthread_condattr_t* __cond_attr)
8 {
9     if (__cond == NULL) {
10         return ERROR_INVALID_HANDLE;
11     }
12
13     (void)__cond_attr;
14     InitializeConditionVariable(&__cond->_cv);
15     return 0;
16 }
17
18 int pthread_cond_destroy(pthread_cond_t* __cond)
19 {
20     (void)__cond;
21     return 0;
22 }
23
24 int pthread_cond_wait(pthread_cond_t *__cond,
25     pthread_mutex_t *__mutex)
26 {
27     if (__cond == NULL || __mutex == NULL)
28         return ERROR_INVALID_HANDLE;
29     return pthread_cond_timedwait(__cond, __mutex, NULL);
30 }
31
32 int pthread_cond_timedwait(pthread_cond_t* __cond,
33     pthread_mutex_t* __mutex,
34     const struct timespec* __abstime)
35 {
36     if (__cond == NULL) {
37         return ERROR_INVALID_HANDLE;
38     }
39
40     long long msec = INFINITE;
41     if (__abstime != NULL) {
42         msec = __abstime->tv_sec * 1000 + __abstime->tv_nsec / 1000000;
43     }
44
45     // SleepConditionVariableCS returns bool=true on success.
46     if (SleepConditionVariableCS(&__cond->_cv, __mutex, (DWORD)msec))
47         return 0;
48
49     const int rc = (int)GetLastError();
50     return rc == ERROR_TIMEOUT ? ETIMEDOUT : rc;
51 }
52
53 int pthread_cond_signal(pthread_cond_t *__cond)
54 {
55     if (__cond == NULL) {
56         return ERROR_INVALID_HANDLE;
57     }
58
59     WakeConditionVariable(&__cond->_cv);
60     return 0;
61 }
62
63 int pthread_cond_broadcast(pthread_cond_t *__cond)
64 {
65     if (__cond == NULL) {
66         return ERROR_INVALID_HANDLE;
67     }
68
69     WakeAllConditionVariable(&__cond->_cv);
70     return 0;
71 }