1 // Copyright (C) 2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
8 /// @brief Application configuration Leon header
11 #ifndef _XLINKSEMAPHORE_H
12 #define _XLINKSEMAPHORE_H
14 # if (defined(_WIN32) || defined(_WIN64))
15 # include "win_pthread.h"
16 # include "win_semaphore.h"
17 # include "win_synchapi.h"
21 # include "pthread_semaphore.h"
23 # include <semaphore.h>
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().
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
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);
63 // Helper functions for XLink semaphore wrappers.
64 // Use them only in case if you know what you are doing.
67 int XLink_sem_set_refs(XLink_sem_t* sem, int refs);
68 int XLink_sem_get_refs(XLink_sem_t* sem, int *sval);
74 #endif // _XLINKSEMAPHORE_H