SDL3pp
A slim C++ wrapper for SDL3
|
A mutex that allows read-only threads to run in parallel. More...
Public Member Functions | |
void | LockForReading () |
Lock the read/write lock for read only operations. | |
void | LockForWriting () |
Lock the read/write lock for write operations. | |
void | TryLockForReading () |
Try to lock a read/write lock for reading without blocking. | |
void | TryLockForWriting () |
Try to lock a read/write lock for writing without blocking. | |
void | Unlock () |
Unlock the read/write lock. | |
constexpr | Resource (T resource={}) |
Constructs from the underlying resource. | |
constexpr | Resource (const ResourceHandle< Resource< T > > auto &resource) |
Constructs from pointer like. | |
constexpr | Resource (std::nullptr_t) |
Equivalent to default ctor. | |
constexpr | Resource (std::nullopt_t) |
Equivalent to default ctor. | |
![]() | |
constexpr | Resource (SDL_RWLock * resource={}) |
Constructs from the underlying resource. | |
constexpr | Resource (const ResourceHandle< Resource< SDL_RWLock * > > auto &resource) |
Constructs from pointer like. | |
constexpr | Resource (std::nullptr_t) |
Equivalent to default ctor. | |
constexpr | Resource (std::nullopt_t) |
Equivalent to default ctor. | |
constexpr | operator bool () const |
True if contains a valid resource. | |
constexpr | operator value_type () const |
Converts back to underlying type. | |
constexpr bool | operator== (const Resource &other) const=default |
Comparison. | |
constexpr bool | operator== (std::nullopt_t) const |
Comparison. | |
constexpr bool | operator== (std::nullptr_t) const |
Comparison. | |
constexpr SDL_RWLock * | get () const |
Return contained resource;. | |
constexpr const SDL_RWLock * | operator-> () const |
Access to fields. | |
constexpr SDL_RWLock * | operator-> () |
Access to fields. | |
Static Public Member Functions | |
static void | reset (SDL_RWLock *resource) |
Destroy a read/write lock created with RWLock.Create(). | |
Additional Inherited Members | |
![]() | |
using | value_type = SDL_RWLock * |
The raw resource type. | |
A rwlock is roughly the same concept as Mutex, but allows threads that request read-only access to all hold the lock at the same time. If a thread requests write access, it will block until all read-only threads have released the lock, and no one else can hold the thread (for reading or writing) at the same time as the writing thread.
This can be more efficient in cases where several threads need to access data frequently, but changes to that data are rare.
There are other rules that apply to rwlocks that don't apply to mutexes, about how threads are scheduled and when they can be recursively locked. These are documented in the other rwlock functions.
|
inline |
This will block until the rwlock is available, which is to say it is not locked for writing by any other thread. Of all threads waiting to lock the rwlock, all may do so at the same time as long as they are requesting read-only access; if a thread wants to lock for writing, only one may do so at a time, and no other threads, read-only or not, may hold the lock at the same time.
It is legal for the owning thread to lock an already-locked rwlock for reading. It must unlock it the same number of times before it is actually made available for other threads in the system (this is known as a "recursive rwlock").
Note that locking for writing is not recursive (this is only available to read-only locks).
It is illegal to request a read-only lock from a thread that already holds the write lock. Doing so results in undefined behavior. Unlock the write lock before requesting a read-only lock. (But, of course, if you have the write lock, you don't need further locks to read in any case.)
This function does not fail; if rwlock is nullptr, it will return immediately having locked nothing. If the rwlock is valid, this function will always block until it can lock the mutex, and return with it locked.
|
inline |
This will block until the rwlock is available, which is to say it is not locked for reading or writing by any other thread. Only one thread may hold the lock when it requests write access; all other threads, whether they also want to write or only want read-only access, must wait until the writer thread has released the lock.
It is illegal for the owning thread to lock an already-locked rwlock for writing (read-only may be locked recursively, writing can not). Doing so results in undefined behavior.
It is illegal to request a write lock from a thread that already holds a read-only lock. Doing so results in undefined behavior. Unlock the read-only lock before requesting a write lock.
This function does not fail; if rwlock is nullptr, it will return immediately having locked nothing. If the rwlock is valid, this function will always block until it can lock the mutex, and return with it locked.
|
inlinestatic |
This function must be called on any read/write lock that is no longer needed. Failure to destroy a rwlock will result in a system memory or resource leak. While it is safe to destroy a rwlock that is unlocked, it is not safe to attempt to destroy a locked rwlock, and may result in undefined behavior depending on the platform.
resource | the rwlock to destroy. |
|
inline |
This works just like RWLockRef.LockForReading(), but if the rwlock is not available, then this function returns false immediately.
This technique is useful if you need access to a resource but don't want to wait for it, and will return to it to try again later.
Trying to lock for read-only access can succeed if other threads are holding read-only locks, as this won't prevent access.
This function returns true if passed a nullptr rwlock.
Error | on failure. |
|
inline |
This works just like RWLockRef.LockForWriting(), but if the rwlock is not available, then this function returns false immediately.
This technique is useful if you need exclusive access to a resource but don't want to wait for it, and will return to it to try again later.
It is illegal for the owning thread to lock an already-locked rwlock for writing (read-only may be locked recursively, writing can not). Doing so results in undefined behavior.
It is illegal to request a write lock from a thread that already holds a read-only lock. Doing so results in undefined behavior. Unlock the read-only lock before requesting a write lock.
This function returns true if passed a nullptr rwlock.
Error | on failure. |
|
inline |
Use this function to unlock the rwlock, whether it was locked for read-only or write operations.
It is legal for the owning thread to lock an already-locked read-only lock. It must unlock it the same number of times before it is actually made available for other threads in the system (this is known as a "recursive rwlock").
It is illegal to unlock a rwlock that has not been locked by the current thread, and doing so results in undefined behavior.