SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
SDL::RWLockRef Struct Reference

A mutex that allows read-only threads to run in parallel. More...

Inheritance diagram for SDL::RWLockRef:
Inheritance graph
[legend]

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.
 
- Public Member Functions inherited from SDL::Resource< SDL_RWLock * >
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

- Public Types inherited from SDL::Resource< SDL_RWLock * >
using value_type = SDL_RWLock *
 The raw resource type.
 

Detailed Description

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.

Since
This struct is available since SDL 3.2.0.
Category:
Resource
See also
RWLock

Member Function Documentation

◆ LockForReading()

void SDL::RWLockRef::LockForReading ( )
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.

Since
This function is available since SDL 3.2.0.
See also
RWLockRef.LockForWriting
RWLockRef.TryLockForReading
RWLockRef.Unlock

◆ LockForWriting()

void SDL::RWLockRef::LockForWriting ( )
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.

Since
This function is available since SDL 3.2.0.
See also
RWLockRef.LockForReading
RWLockRef.TryLockForWriting
RWLockRef.Unlock

◆ reset()

static void SDL::RWLockRef::reset ( SDL_RWLock *  resource)
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.

Parameters
resourcethe rwlock to destroy.
Since
This function is available since SDL 3.2.0.
See also
RWLock.Create

◆ TryLockForReading()

void SDL::RWLockRef::TryLockForReading ( )
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.

Exceptions
Erroron failure.
Since
This function is available since SDL 3.2.0.
See also
RWLockRef.LockForReading
RWLockRef.TryLockForWriting
RWLockRef.Unlock

◆ TryLockForWriting()

void SDL::RWLockRef::TryLockForWriting ( )
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.

Exceptions
Erroron failure.
Since
This function is available since SDL 3.2.0.
See also
RWLockRef.LockForWriting
RWLockRef.TryLockForReading
RWLockRef.Unlock

◆ Unlock()

void SDL::RWLockRef::Unlock ( )
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.

Since
This function is available since SDL 3.2.0.
See also
RWLockRef.LockForReading
RWLockRef.LockForWriting
RWLockRef.TryLockForReading
RWLockRef.TryLockForWriting

The documentation for this struct was generated from the following file: