libnx v4.9.0
Loading...
Searching...
No Matches
mutex.h
Go to the documentation of this file.
1/**
2 * @file mutex.h
3 * @brief Mutex synchronization primitive.
4 * @author plutoo
5 * @copyright libnx Authors
6 */
7#pragma once
8#include <sys/lock.h>
9#include "../types.h"
10
11/// Mutex datatype, defined in newlib.
12typedef _LOCK_T Mutex;
13/// Recursive mutex datatype, defined in newlib.
14typedef _LOCK_RECURSIVE_T RMutex;
15
16/**
17 * @brief Initializes a mutex.
18 * @param m Mutex object.
19 * @note A mutex can also be statically initialized by assigning 0 to it.
20 */
21static inline void mutexInit(Mutex* m)
22{
23 *m = INVALID_HANDLE;
24}
25
26/**
27 * @brief Locks a mutex.
28 * @param m Mutex object.
29 */
31
32/**
33 * @brief Attempts to lock a mutex without waiting.
34 * @param m Mutex object.
35 * @return 1 if the mutex has been acquired successfully, and 0 on contention.
36 */
38
39/**
40 * @brief Unlocks a mutex.
41 * @param m Mutex object.
42 */
44
45/**
46 * @brief Gets whether the current thread owns the mutex.
47 * @param m Mutex object.
48 * @return 1 if the mutex is locked by the current thread, and 0 otherwise.
49 */
51
52/**
53 * @brief Initializes a recursive mutex.
54 * @param m Recursive mutex object.
55 * @note A recursive mutex can also be statically initialized by assigning {0,0,0} to it.
56 */
57static inline void rmutexInit(RMutex* m)
58{
59 m->lock = 0;
60 m->thread_tag = 0;
61 m->counter = 0;
62}
63
64/**
65 * @brief Locks a recursive mutex.
66 * @param m Recursive mutex object.
67 */
69
70/**
71 * @brief Attempts to lock a recursive mutex without waiting.
72 * @param m Recursive mutex object.
73 * @return 1 if the mutex has been acquired successfully, and 0 on contention.
74 */
76
77/**
78 * @brief Unlocks a recursive mutex.
79 * @param m Recursive mutex object.
80 */
_LOCK_T Mutex
Mutex datatype, defined in newlib.
Definition mutex.h:12
_LOCK_RECURSIVE_T RMutex
Recursive mutex datatype, defined in newlib.
Definition mutex.h:14
void mutexUnlock(Mutex *m)
Unlocks a mutex.
static void mutexInit(Mutex *m)
Initializes a mutex.
Definition mutex.h:21
bool rmutexTryLock(RMutex *m)
Attempts to lock a recursive mutex without waiting.
bool mutexTryLock(Mutex *m)
Attempts to lock a mutex without waiting.
void mutexLock(Mutex *m)
Locks a mutex.
void rmutexUnlock(RMutex *m)
Unlocks a recursive mutex.
bool mutexIsLockedByCurrentThread(const Mutex *m)
Gets whether the current thread owns the mutex.
void rmutexLock(RMutex *m)
Locks a recursive mutex.
static void rmutexInit(RMutex *m)
Initializes a recursive mutex.
Definition mutex.h:57
#define INVALID_HANDLE
Invalid handle.
Definition types.h:96