libnx  v4.7.0
rwlock.h
Go to the documentation of this file.
1 /**
2  * @file rwlock.h
3  * @brief Read/write lock synchronization primitive.
4  * @author plutoo, SciresM
5  * @copyright libnx Authors
6  */
7 #pragma once
8 #include "../kernel/mutex.h"
9 #include "../kernel/condvar.h"
10 
11 /// Read/write lock structure.
12 typedef struct {
13  Mutex mutex;
14  CondVar condvar_reader_wait;
15  CondVar condvar_writer_wait;
16  u32 read_lock_count;
17  u32 read_waiter_count;
18  u32 write_lock_count;
19  u32 write_waiter_count;
20  u32 write_owner_tag;
21 } RwLock;
22 
23 /**
24  * @brief Initializes the read/write lock.
25  * @param r Read/write lock object.
26  */
27 void rwlockInit(RwLock* r);
28 
29 /**
30  * @brief Locks the read/write lock for reading.
31  * @param r Read/write lock object.
32  */
34 
35 /**
36  * @brief Attempts to lock the read/write lock for reading without waiting.
37  * @param r Read/write lock object.
38  * @return 1 if the mutex has been acquired successfully, and 0 on contention.
39  */
41 
42 /**
43  * @brief Unlocks the read/write lock for reading.
44  * @param r Read/write lock object.
45  */
47 
48 /**
49  * @brief Locks the read/write lock for writing.
50  * @param r Read/write lock object.
51  */
53 
54 /**
55  * @brief Attempts to lock the read/write lock for writing without waiting.
56  * @param r Read/write lock object.
57  * @return 1 if the mutex has been acquired successfully, and 0 on contention.
58  */
60 
61 /**
62  * @brief Unlocks the read/write lock for writing.
63  * @param r Read/write lock object.
64  */
66 
67 /**
68  * @brief Checks if the write lock is held by the current thread.
69  * @param r Read/write lock object.
70  * @return 1 if the current hold holds the write lock, and 0 if it does not.
71  */
73 
74 /**
75  * @brief Checks if the read/write lock is owned by the current thread.
76  * @param r Read/write lock object.
77  * @return 1 if the current hold holds the write lock or if it holds read locks acquired
78  * while it held the write lock, and 0 if it does not.
79  */
u32 CondVar
Condition variable.
Definition: condvar.h:13
_LOCK_T Mutex
Mutex datatype, defined in newlib.
Definition: mutex.h:12
void rwlockWriteUnlock(RwLock *r)
Unlocks the read/write lock for writing.
bool rwlockTryWriteLock(RwLock *r)
Attempts to lock the read/write lock for writing without waiting.
bool rwlockTryReadLock(RwLock *r)
Attempts to lock the read/write lock for reading without waiting.
bool rwlockIsOwnedByCurrentThread(RwLock *r)
Checks if the read/write lock is owned by the current thread.
void rwlockWriteLock(RwLock *r)
Locks the read/write lock for writing.
void rwlockReadUnlock(RwLock *r)
Unlocks the read/write lock for reading.
bool rwlockIsWriteLockHeldByCurrentThread(RwLock *r)
Checks if the write lock is held by the current thread.
void rwlockInit(RwLock *r)
Initializes the read/write lock.
void rwlockReadLock(RwLock *r)
Locks the read/write lock for reading.
Read/write lock structure.
Definition: rwlock.h:12
uint32_t u32
32-bit unsigned integer.
Definition: types.h:21