sem_clockwait()

Updated: April 19, 2023

Wait on a semaphore with a timeout measured against a specific clock

Synopsis:

#include <semaphore.h>
#include <time.h>

int sem_clockwait(
        sem_t *sem,
        clockid_t clk,
        const struct timespec *abstime );

Arguments:

sem
The semaphore that you want to wait on.
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 structure 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 sem_clockwait() function decrements the semaphore referenced by sem as in the sem_wait() function. If the semaphore cannot be decremented (i.e., if the count is 0), the function blocks until another thread increments the semaphore's count, or until the specified timeout expires.

The timeout expires when the absolute time specified by abstime passes, as measured by the clock specified by clk (i.e., when the value of that clock equals or exceeds abstime), or if the absolute time specified by abstime has already been passed at the time of the call.

Returns:

0
The calling thread successfully decremented the semaphore designated by sem.
-1
The call was unsuccessful (errno is set). The state of the semaphore is unchanged.

Errors:

EINTR
A signal interrupted this function.
EINVAL
One or more of the following is true:
  • One or more of cond, mutex, clk, and abstime is invalid.
  • Concurrent waits or timed waits on cond used different mutexes.
  • The mutex has died; see SyncMutexEvent().
  • 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.
  • Invalid semaphore sem, or the thread would have blocked, and the abs_timeout parameter specified a nanoseconds field value less than zero or greater than or equal to 1000 million.
ETIMEDOUT
The semaphore couldn't be decremented before the specified timeout expired.

Classification:

POSIX 1003.1

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