pthread_rwlock_timedrdlock Subroutine

Purpose

Locks a read-write lock for reading.

Syntax

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

int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict rwlock,
       const struct timespec *restrict abs_timeout);

Description

The pthread_rwlock_timedrdlock() function applies a read lock to the read-write lock referenced by rwlock as in the pthread_rwlock_rdlock() function. However, if the lock cannot 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 (that is, when the value of that clock equals or exceeds abs_timeout)—or when the absolute time specified by abs_timeout has already been passed at the time of the call.

If the Timers option is supported, the timeout is based on the CLOCK_REALTIME clock; if the Timers option is not supported, the timeout is based on the system clock as returned by the time() function.

The resolution of the timeout matches the resolution of the clock on which it is based. The timespec data type is defined in the <time.h> header.

The function never fails with a timeout if the lock can be acquired immediately. The validity of the abs_timeout parameter does not need to be checked if the lock can be immediately acquired.

If a signal that causes a signal handler to be executed is delivered to a thread that is blocked on a read-write lock through a call to pthread_rwlock_timedrdlock(), the thread resumes waiting for the lock (as if it were not interrupted) after the signal handler returns.

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

Application Usage

The pthread_rwlock_timedrdlock() function is part of the Threads and Timeouts options and do not need to be provided on all implementations.

Return Values

The pthread_rwlock_timedrdlock() function returns 0 if the lock for reading on the read-write lock object referenced by rwlock is acquired. Otherwise, an error number is returned to indicate the error.

Error Codes

The pthread_rwlock_timedrdlock() function fails if:

Item Description
[ETIMEDOUT] The lock could not be acquired before the specified timeout expired.

The pthread_rwlock_timedrdlock() function might fail if:

Item Description
[EAGAIN] The read lock could not be acquired because the maximum number of read locks for lock would be exceeded.
[EDEADLK] The calling thread already holds a write lock on rwlock.
[EINVAL] The value specified by rwlock does not refer to an initialized read-write lock object, or the abs_timeout nanosecond value is less than 0 or greater than or equal to 1000 million.

This function does not return an error code of [EINTR].