libnx  v4.2.1
condvar.h
Go to the documentation of this file.
1 /**
2  * @file condvar.h
3  * @brief Condition variable synchronization primitive.
4  * @author plutoo
5  * @copyright libnx Authors
6  */
7 #pragma once
8 #include "../types.h"
9 #include "../kernel/svc.h"
10 #include "../kernel/mutex.h"
11 
12 /// Condition variable.
13 typedef u32 CondVar;
14 
15 /**
16  * @brief Initializes a condition variable.
17  * @param[in] c Condition variable object.
18  */
19 static inline void condvarInit(CondVar* c)
20 {
21  *c = 0;
22 }
23 
24 /**
25  * @brief Waits on a condition variable with a timeout.
26  * @param[in] c Condition variable object.
27  * @param[in] m Mutex object to use inside the condition variable.
28  * @param[in] timeout Timeout in nanoseconds.
29  * @return Result code (0xEA01 on timeout).
30  * @remark On function return, the underlying mutex is acquired.
31  */
32 Result condvarWaitTimeout(CondVar* c, Mutex* m, u64 timeout);
33 
34 /**
35  * @brief Waits on a condition variable.
36  * @param[in] c Condition variable object.
37  * @param[in] m Mutex object to use inside the condition variable.
38  * @return Result code.
39  * @remark On function return, the underlying mutex is acquired.
40  */
41 static inline Result condvarWait(CondVar* c, Mutex* m)
42 {
43  return condvarWaitTimeout(c, m, UINT64_MAX);
44 }
45 
46 /**
47  * @brief Wakes up up to the specified number of threads waiting on a condition variable.
48  * @param[in] c Condition variable object.
49  * @param[in] num Maximum number of threads to wake up (or -1 to wake them all up).
50  * @return Result code.
51  */
52 static inline Result condvarWake(CondVar* c, int num)
53 {
55  return 0;
56 }
57 
58 /**
59  * @brief Wakes up a single thread waiting on a condition variable.
60  * @param[in] c Condition variable object.
61  * @return Result code.
62  */
63 static inline Result condvarWakeOne(CondVar* c)
64 {
65  return condvarWake(c, 1);
66 }
67 
68 /**
69  * @brief Wakes up all thread waiting on a condition variable.
70  * @param[in] c Condition variable object.
71  * @return Result code.
72  */
73 static inline Result condvarWakeAll(CondVar* c)
74 {
75  return condvarWake(c, -1);
76 }
condvarWakeAll
static Result condvarWakeAll(CondVar *c)
Wakes up all thread waiting on a condition variable.
Definition: condvar.h:73
condvarWake
static Result condvarWake(CondVar *c, int num)
Wakes up up to the specified number of threads waiting on a condition variable.
Definition: condvar.h:52
svcSignalProcessWideKey
void svcSignalProcessWideKey(u32 *key, s32 num)
Performs a condition variable wake-up operation in userspace.
u32
uint32_t u32
32-bit unsigned integer.
Definition: types.h:21
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
Result
u32 Result
Function error code result type.
Definition: types.h:44
condvarWakeOne
static Result condvarWakeOne(CondVar *c)
Wakes up a single thread waiting on a condition variable.
Definition: condvar.h:63
condvarWait
static Result condvarWait(CondVar *c, Mutex *m)
Waits on a condition variable.
Definition: condvar.h:41
condvarInit
static void condvarInit(CondVar *c)
Initializes a condition variable.
Definition: condvar.h:19
condvarWaitTimeout
Result condvarWaitTimeout(CondVar *c, Mutex *m, u64 timeout)
Waits on a condition variable with a timeout.
CondVar
u32 CondVar
Condition variable.
Definition: condvar.h:13