libnx  v4.2.2
semaphore.h
Go to the documentation of this file.
1 /**
2  * @file semaphore.h
3  * @brief Thread synchronization based on Mutex.
4  * @author SciresM & Kevoot
5  * @copyright libnx Authors
6  */
7 #pragma once
8 
9 #include "mutex.h"
10 #include "condvar.h"
11 
12 /// Semaphore structure.
13 typedef struct Semaphore
14 {
15  CondVar condvar; ///< Condition variable object.
16  Mutex mutex; ///< Mutex object.
17  u64 count; ///< Internal counter.
18 } Semaphore;
19 
20 /**
21  * @brief Initializes a semaphore and its internal counter.
22  * @param s Semaphore object.
23  * @param initial_count initial value for internal counter (typically the # of free resources).
24  */
25 void semaphoreInit(Semaphore *s, u64 initial_count);
26 
27 /**
28  * @brief Increments the Semaphore to allow other threads to continue.
29  * @param s Semaphore object.
30  */
31 void semaphoreSignal(Semaphore *s);
32 
33 /**
34  * @brief Decrements Semaphore and waits if 0.
35  * @param s Semaphore object.
36  */
37 void semaphoreWait(Semaphore *s);
38 
39 /**
40  * @brief Attempts to get lock without waiting.
41  * @param s Semaphore object.
42  * @return true if no wait and successful lock, false otherwise.
43  */
semaphoreTryWait
bool semaphoreTryWait(Semaphore *s)
Attempts to get lock without waiting.
semaphoreWait
void semaphoreWait(Semaphore *s)
Decrements Semaphore and waits if 0.
Semaphore::mutex
Mutex mutex
Mutex object.
Definition: semaphore.h:16
semaphoreSignal
void semaphoreSignal(Semaphore *s)
Increments the Semaphore to allow other threads to continue.
condvar.h
Condition variable synchronization primitive.
Semaphore::condvar
CondVar condvar
Condition variable object.
Definition: semaphore.h:15
u64
uint64_t u64
64-bit unsigned integer.
Definition: types.h:22
Mutex
_LOCK_T Mutex
Mutex datatype, defined in newlib.
Definition: mutex.h:12
semaphoreInit
void semaphoreInit(Semaphore *s, u64 initial_count)
Initializes a semaphore and its internal counter.
Semaphore::count
u64 count
Internal counter.
Definition: semaphore.h:17
mutex.h
Mutex synchronization primitive.
Semaphore
Semaphore structure.
Definition: semaphore.h:13
CondVar
u32 CondVar
Condition variable.
Definition: condvar.h:13