libnx v4.9.0
Loading...
Searching...
No Matches
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.
13typedef 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 */
25void 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 */
32
33/**
34 * @brief Decrements Semaphore and waits if 0.
35 * @param s Semaphore object.
36 */
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 */
Condition variable synchronization primitive.
u32 CondVar
Condition variable.
Definition condvar.h:13
Mutex synchronization primitive.
_LOCK_T Mutex
Mutex datatype, defined in newlib.
Definition mutex.h:12
bool semaphoreTryWait(Semaphore *s)
Attempts to get lock without waiting.
void semaphoreSignal(Semaphore *s)
Increments the Semaphore to allow other threads to continue.
void semaphoreWait(Semaphore *s)
Decrements Semaphore and waits if 0.
void semaphoreInit(Semaphore *s, u64 initial_count)
Initializes a semaphore and its internal counter.
Semaphore structure.
Definition semaphore.h:14
CondVar condvar
Condition variable object.
Definition semaphore.h:15
Mutex mutex
Mutex object.
Definition semaphore.h:16
u64 count
Internal counter.
Definition semaphore.h:17
uint64_t u64
64-bit unsigned integer.
Definition types.h:22