pthread_rwlock.h File Reference

Implementation of a fair, POSIX conforming reader/writer lock. More...

Detailed Description

Implementation of a fair, POSIX conforming reader/writer lock.

Note
Do not include this header file directly, but pthread.h.

Definition in file pthread_rwlock.h.

#include "priority_queue.h"
#include "thread.h"
#include <errno.h>
#include <stdbool.h>
+ Include dependency graph for pthread_rwlock.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pthread_rwlock_t
 A fair reader writer lock. More...
 
struct  __pthread_rwlock_waiter_node_t
 Internal structure that stores one waiting thread. More...
 
int pthread_rwlock_init (pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr)
 Initialize a reader/writer lock. More...
 
int pthread_rwlock_destroy (pthread_rwlock_t *rwlock)
 Destroy a reader/writer lock. More...
 
int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock)
 Lock a reader/writer lock for reading. More...
 
int pthread_rwlock_tryrdlock (pthread_rwlock_t *rwlock)
 Try to lock a reader/writer lock for reader. More...
 
int pthread_rwlock_timedrdlock (pthread_rwlock_t *rwlock, const struct timespec *abstime)
 Try to acquire a read lock in a given timeframe. More...
 
int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock)
 Lock a reader/writer lock for writing. More...
 
int pthread_rwlock_trywrlock (pthread_rwlock_t *rwlock)
 Try to lock a reader/writer lock for writing. More...
 
int pthread_rwlock_timedwrlock (pthread_rwlock_t *rwlock, const struct timespec *abstime)
 Try to acquire a write lock in a given timeframe. More...
 
int pthread_rwlock_unlock (pthread_rwlock_t *rwlock)
 Unlock the reader/writer lock. More...
 
bool __pthread_rwlock_blocked_readingly (const pthread_rwlock_t *rwlock)
 Internal function to determine of the lock can be acquired for reading. More...
 
bool __pthread_rwlock_blocked_writingly (const pthread_rwlock_t *rwlock)
 Internal function to determine of the lock can be acquired for writing. More...
 

Function Documentation

◆ __pthread_rwlock_blocked_readingly()

bool __pthread_rwlock_blocked_readingly ( const pthread_rwlock_t rwlock)

Internal function to determine of the lock can be acquired for reading.

Parameters
[in]rwlockLock to query.
Returns
false if locking for reading is possible without blocking.

◆ __pthread_rwlock_blocked_writingly()

bool __pthread_rwlock_blocked_writingly ( const pthread_rwlock_t rwlock)

Internal function to determine of the lock can be acquired for writing.

Parameters
[in]rwlockLock to query.
Returns
false if locking for writing is possible without blocking.

◆ pthread_rwlock_destroy()

int pthread_rwlock_destroy ( pthread_rwlock_t rwlock)

Destroy a reader/writer lock.

This is a no-op. Destroying a reader/writer lock while a thread holds it, or there are threads waiting for it causes undefined behavior. Datum must be reinitialized before using it again.

Parameters
[in]rwlockLock to destroy.
Returns
0 on success. EINVAL if rwlock == NULL. EBUSY if the lock was in use.

◆ pthread_rwlock_init()

int pthread_rwlock_init ( pthread_rwlock_t rwlock,
const pthread_rwlockattr_t attr 
)

Initialize a reader/writer lock.

A zeroed out datum is initialized.

Parameters
[in,out]rwlockLock to initialize.
[in]attrUnused.
Returns
0 on success. EINVAL if rwlock == NULL.

◆ pthread_rwlock_rdlock()

int pthread_rwlock_rdlock ( pthread_rwlock_t rwlock)

Lock a reader/writer lock for reading.

This function may block.

Parameters
[in]rwlockLock to acquire for reading.
Returns
0 if the lock could be acquired. EINVAL if rwlock == NULL.

◆ pthread_rwlock_timedrdlock()

int pthread_rwlock_timedrdlock ( pthread_rwlock_t rwlock,
const struct timespec *  abstime 
)

Try to acquire a read lock in a given timeframe.

Parameters
[in]rwlockLock to acquire for reading.
[in]abstimeMaximum timestamp when to wakeup, absolute.
Returns
0 if the lock could be acquired. EDEADLK if the lock could not be acquired in the given timeframe. EINVAL if rwlock == NULL.

◆ pthread_rwlock_timedwrlock()

int pthread_rwlock_timedwrlock ( pthread_rwlock_t rwlock,
const struct timespec *  abstime 
)

Try to acquire a write lock in a given timeframe.

Parameters
[in]rwlockLock to acquire for writing.
[in]abstimeMaximum timestamp when to wakeup, absolute.
Returns
0 if the lock could be acquired. EDEADLK if the lock could not be acquired in the given timeframe. EINVAL if rwlock == NULL.

◆ pthread_rwlock_tryrdlock()

int pthread_rwlock_tryrdlock ( pthread_rwlock_t rwlock)

Try to lock a reader/writer lock for reader.

This function won't block.

Parameters
[in]rwlockLock to acquire for reading.
Returns
0 if the lock could be acquired. EBUSY if acquiring the lock cannot be done without blocking. EINVAL if rwlock == NULL.

◆ pthread_rwlock_trywrlock()

int pthread_rwlock_trywrlock ( pthread_rwlock_t rwlock)

Try to lock a reader/writer lock for writing.

This function won't block.

Parameters
[in]rwlockLock to acquire for writing.
Returns
0 if the lock could be acquired. EBUSY if acquiring the lock cannot be done without blocking. EINVAL if rwlock == NULL.

◆ pthread_rwlock_unlock()

int pthread_rwlock_unlock ( pthread_rwlock_t rwlock)

Unlock the reader/writer lock.

Must only be used if the lock is currently held. You may release the lock out of another thread, but the lock, operate, unlock logic must not be broken.

Parameters
[in]rwlockLock to release
Returns
0 on success. EPERM if the lock was not held for any operation. EINVAL if rwlock == NULL.

◆ pthread_rwlock_wrlock()

int pthread_rwlock_wrlock ( pthread_rwlock_t rwlock)

Lock a reader/writer lock for writing.

This function may block.

Parameters
[in]rwlockLock to acquire for writing.
Returns
0 if the lock could be acquired. EINVAL if rwlock == NULL.