mirror of
https://github.com/switchbrew/libnx.git
synced 2025-06-21 20:42:44 +02:00
Document kernel/condvar.h
This commit is contained in:
parent
78ff885390
commit
521bc1c5c9
@ -1,18 +1,69 @@
|
|||||||
// Copyright 2018 plutoo
|
/**
|
||||||
|
* @file condvar.h
|
||||||
|
* @brief Condition variable synchronization primitive.
|
||||||
|
* @author plutoo
|
||||||
|
* @copyright libnx Authors
|
||||||
|
*/
|
||||||
#include "../types.h"
|
#include "../types.h"
|
||||||
#include "../kernel/mutex.h"
|
#include "../kernel/mutex.h"
|
||||||
|
|
||||||
|
/// Condition variable structure.
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u32 tag;
|
u32 tag;
|
||||||
Mutex* mutex;
|
Mutex* mutex;
|
||||||
} CondVar;
|
} CondVar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initializes a condition variable.
|
||||||
|
* @param[in] c Condition variable object.
|
||||||
|
* @param[in] m Mutex object to use inside the condition variable.
|
||||||
|
*/
|
||||||
void condvarInit(CondVar* c, Mutex* m);
|
void condvarInit(CondVar* c, Mutex* m);
|
||||||
|
|
||||||
// When the wait-functions return, the mutex is acquired.
|
/**
|
||||||
void condvarWaitTimeout(CondVar* c, u64 timeout);
|
* @brief Waits on a condition variable with a timeout.
|
||||||
void condvarWait(CondVar* c);
|
* @param[in] c Condition variable object.
|
||||||
|
* @param[in] timeout Timeout in nanoseconds.
|
||||||
|
* @return Result code (0xEA01 on timeout).
|
||||||
|
* @remark On function return, the underlying mutex is acquired.
|
||||||
|
*/
|
||||||
|
Result condvarWaitTimeout(CondVar* c, u64 timeout);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Waits on a condition variable.
|
||||||
|
* @param[in] c Condition variable object.
|
||||||
|
* @return Result code.
|
||||||
|
* @remark On function return, the underlying mutex is acquired.
|
||||||
|
*/
|
||||||
|
static inline Result condvarWait(CondVar* c)
|
||||||
|
{
|
||||||
|
return condvarWaitTimeout(c, -1ull);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Wakes up up to the specified number of threads waiting on a condition variable.
|
||||||
|
* @param[in] c Condition variable object.
|
||||||
|
* @param[in] num Maximum number of threads to wake up (or -1 to wake them all up).
|
||||||
|
* @return Result code.
|
||||||
|
*/
|
||||||
Result condvarWake(CondVar* c, int num);
|
Result condvarWake(CondVar* c, int num);
|
||||||
Result condvarWakeOne(CondVar* c);
|
|
||||||
Result condvarWakeAll(CondVar* c);
|
/**
|
||||||
|
* @brief Wakes up a single thread waiting on a condition variable.
|
||||||
|
* @param[in] c Condition variable object.
|
||||||
|
* @return Result code.
|
||||||
|
*/
|
||||||
|
static inline Result condvarWakeOne(CondVar* c)
|
||||||
|
{
|
||||||
|
return condvarWake(c, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Wakes up all thread waiting on a condition variable.
|
||||||
|
* @param[in] c Condition variable object.
|
||||||
|
* @return Result code.
|
||||||
|
*/
|
||||||
|
static inline Result condvarWakeAll(CondVar* c)
|
||||||
|
{
|
||||||
|
return condvarWake(c, -1);
|
||||||
|
}
|
||||||
|
@ -10,7 +10,7 @@ void condvarInit(CondVar* c, Mutex* m) {
|
|||||||
c->mutex = m;
|
c->mutex = m;
|
||||||
}
|
}
|
||||||
|
|
||||||
void condvarWaitTimeout(CondVar* c, u64 timeout) {
|
Result condvarWaitTimeout(CondVar* c, u64 timeout) {
|
||||||
Result rc;
|
Result rc;
|
||||||
|
|
||||||
rc = svcWaitProcessWideKeyAtomic(&c->tag, (u32*) c->mutex, getThreadVars()->handle, timeout);
|
rc = svcWaitProcessWideKeyAtomic(&c->tag, (u32*) c->mutex, getThreadVars()->handle, timeout);
|
||||||
@ -18,20 +18,10 @@ void condvarWaitTimeout(CondVar* c, u64 timeout) {
|
|||||||
// On timeout, we need to acquire it manually.
|
// On timeout, we need to acquire it manually.
|
||||||
if (rc == 0xEA01)
|
if (rc == 0xEA01)
|
||||||
mutexLock(c->mutex);
|
mutexLock(c->mutex);
|
||||||
}
|
|
||||||
|
|
||||||
void condvarWait(CondVar* c) {
|
return rc;
|
||||||
return condvarWaitTimeout(c, -1ull);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Result condvarWake(CondVar* c, int num) {
|
Result condvarWake(CondVar* c, int num) {
|
||||||
return svcSignalProcessWideKey((u32*) &c->tag, num);
|
return svcSignalProcessWideKey((u32*) &c->tag, num);
|
||||||
}
|
}
|
||||||
|
|
||||||
Result condvarWakeOne(CondVar* c) {
|
|
||||||
return condvarWake(c, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
Result condvarWakeAll(CondVar* c) {
|
|
||||||
return condvarWake(c, -1);
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user