pthread_mutex_timedlock Subroutine

Purpose

Locks a mutex (ADVANCED REALTIME).

Syntax

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

int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex,
       const struct timespec *restrict abs_timeout); 

Description

The pthread_mutex_timedlock() function locks the mutex object referenced by mutex. If the mutex is already locked, the calling thread blocks until the mutex becomes available, as in the pthread_mutex_lock() function. If the mutex cannot be locked without waiting for another thread to unlock the mutex, 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 mutex can be locked immediately. The validity of the abs_timeout parameter does not need to be checked if the mutex can be locked immediately.

As a consequence of the priority inheritance rules (for mutexes initialized with the PRIO_INHERIT protocol), if a timed mutex wait is terminated because its timeout expires, the priority of the owner of the mutex adjusts as necessary to reflect the fact that this thread is no longer among the threads waiting for the mutex.

Application Usage

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

Return Values

If successful, the pthread_mutex_timedlock() function returns 0; otherwise, an error number is returned to indicate the error.

Error Codes

The pthread_mutex_timedlock() function fails if:

Item Description
[EDEADLK] The current thread already owns the mutex.
[EINVAL] The mutex was created with the protocol attribute having the value PTHREAD_PRIO_PROTECT, and the calling thread's priority is higher than the mutex's current priority ceiling.
[EINVAL] The process or thread would have blocked, and the abs_timeout parameter specified a nanoseconds field value less than 0 or greater than or equal to 1000 million.
[EINVAL] abs_timeout is a NULL pointer.
[EINVAL] The value specified by mutex does not refer to an initialized mutex object.
[ETIMEDOUT] The mutex could not be locked before the specified timeout expired.

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