pthread_rwlock_clockwrlock()

Updated: April 19, 2023

Lock a read-write lock for writing with a time limit measured against a specific clock

Synopsis:

#include <pthread.h>
#include <time.h>

int pthread_rwlock_clockwrlock(
                   pthread_rwlock_t *lock, 
                   clockid_t clk, 
                   const struct timespec *abs_timeout );

Arguments:

lock
The read-write lock that you want to lock.
clk
The clock against which the time limit is measured. The clock source is specified using the clk variable. The clk variable must be set to either CLOCK_REALTIME, CLOCK_ SOFTTIME or CLOCK_MONOTONIC.
abs_timeout
A pointer to a timespec that specifies the absolute time at which the timeout is to expire.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The pthread_rwlock_clockwrlock() function applies a write lock to the read-write lock referenced by rwlock as in pthread_rwlock_wrlock().

However, if the lock can't be acquired without waiting for other threads to unlock the lock, this wait terminates when the specified timeout expires. The timeout expires when the absolute time specified by abs_timeout passes, as measured by the clock on which timeouts are based (i.e., when the value of that clock equals or exceed abs_timeout), or if the absolute time specified by abs_timeout has already been passed at the time of the call.

The timeout is based on the clock.

If the read-write lock can be locked immediately, the validity of the abs_timeout parameter isn't checked, and the function won't fail with a timeout.

If a signal that causes a signal handler to be executed is delivered to a thread blocked on a read-write lock via a call to pthread_rwlock_timedwrlock(), upon return from the signal handler the thread resumes waiting for the lock as if it hadn't been interrupted.

The calling thread may deadlock if at the time the call is made it holds a write lock on rwlock. The results are undefined if this function is called with an uninitialized read-write lock.

Returns:

Zero if the lock for writing on the read-write lock object referenced by rwlock is acquired, or an error number to indicate the error.

Errors:

EAGAIN
Couldn't acquire read lock because the maximum number of read locks for lock would be exceeded.
EDEADLK
The calling thread already holds the rwlock.
EINVAL
One of the following occurred:
  • The clock is used in a context which does not make sense for a timeout; for example, when using CLOCK_THREAD_CPUTIME_ID.
  • The use of the clock as a timeout is not supported; for example, when using another thread's CPUTIME clock.
  • The clock does not exist.
  • The value specified by rwlock doesn't refer to an initialized read-write lock object, or the abs_timeout nanosecond value is less than zero or greater than or equal to 1,000 million.
ETIMEDOUT
The lock couldn't be acquired before the specified timeout expired.

Classification:

POSIX 1003.1

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes