libnx  v4.2.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  */
33 void rwlockReadLock(RwLock* r);
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  */
40 bool rwlockTryReadLock(RwLock* r);
41 
42 /**
43  * @brief Unlocks the read/write lock for reading.
44  * @param r Read/write lock object.
45  */
46 void rwlockReadUnlock(RwLock* r);
47 
48 /**
49  * @brief Locks the read/write lock for writing.
50  * @param r Read/write lock object.
51  */
52 void rwlockWriteLock(RwLock* r);
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  */
59 bool rwlockTryWriteLock(RwLock* r);
60 
61 /**
62  * @brief Unlocks the read/write lock for writing.
63  * @param r Read/write lock object.
64  */
65 void rwlockWriteUnlock(RwLock* r);
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  */
rwlockTryReadLock
bool rwlockTryReadLock(RwLock *r)
Attempts to lock the read/write lock for reading without waiting.
RwLock
Read/write lock structure.
Definition: rwlock.h:12
rwlockWriteUnlock
void rwlockWriteUnlock(RwLock *r)
Unlocks the read/write lock for writing.
u32
uint32_t u32
32-bit unsigned integer.
Definition: types.h:21
rwlockReadUnlock
void rwlockReadUnlock(RwLock *r)
Unlocks the read/write lock for reading.
rwlockIsWriteLockHeldByCurrentThread
bool rwlockIsWriteLockHeldByCurrentThread(RwLock *r)
Checks if the write lock is held by the current thread.
rwlockTryWriteLock
bool rwlockTryWriteLock(RwLock *r)
Attempts to lock the read/write lock for writing without waiting.
Mutex
_LOCK_T Mutex
Mutex datatype, defined in newlib.
Definition: mutex.h:12
rwlockInit
void rwlockInit(RwLock *r)
Initializes the read/write lock.
rwlockIsOwnedByCurrentThread
bool rwlockIsOwnedByCurrentThread(RwLock *r)
Checks if the read/write lock is owned by the current thread.
rwlockReadLock
void rwlockReadLock(RwLock *r)
Locks the read/write lock for reading.
rwlockWriteLock
void rwlockWriteLock(RwLock *r)
Locks the read/write lock for writing.
CondVar
u32 CondVar
Condition variable.
Definition: condvar.h:13