SEM_TIMEDWAIT(3)
π NAME π
okay so this is the SEM_TIMEDWAIT(3) page from the Library Functions Manual, and itβs here to explain how to lock a semaphore with a timeout. iconic behavior, honestly.
sem_timedwait,sem_clockwait_npβ lock a semaphore π
π LIBRARY π
Standard C Library (libc, -lc) β yes bestie, the classic.
β¨ SYNOPSIS β¨
#include <semaphore.h>
#include <time.h>
int
sem_timedwait(sem_t * restrict sem,
const struct timespec * restrict abs_timeout);
int
sem_clockwait_np(sem_t * restrict sem, clockid_t clock_id, int flags,
const struct timespec * rqtp, struct timespec * rmtp);
πΈ DESCRIPTION πΈ
So sem_timedwait() locks the semaphore referenced by sem, just like sem_wait(3) does. but with a timeout twist π If the semaphore canβt be locked without waiting for another process or thread to unlock it by doing a sem_post(3), the wait gets terminated 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 if the absolute time specified by abs_timeout has already been passed at the time of the call. also: the timeout is based on the CLOCK_REALTIME clock, noted and slayed β°β¨
The validity of abs_timeout is not checked if the semaphore can be locked immediately. like, if itβs already free, it does not even do a vibe check on that timeout π
sem_clockwait_np() is the more flexible variant of sem_timedwait(). the clock_id parameter specifies the reference clock. If the flags parameter contains TIMER_ABSTIME, then the requested timeout (rqtp) is an absolute timeout; otherwise, the timeout is relative. If this function fails with EINTR and the timeout is relative, a non-NULL rmtp will be updated to contain the amount of time remaining in the interval β thatβs the requested time minus the time actually slept. An absolute timeout has no effect on rmtp. and yes bestie, a single structure can be used for both rqtp and rmtp π«Ά
π₯ RETURN VALUES π₯
These functions return zero if the calling process successfully performed the semaphore lock operation on the semaphore designated by sem. If the call was unsuccessful, the state of the semaphore is unchanged, and the function returns a value of -1 and sets the global variable errno to indicate the error. classic -1 + errno moment π
π ERRORS π
These functions will fail if:
[EINVAL]β Thesemargument does not refer to a valid semaphore, or the process or thread would have blocked, and theabs_timeoutparameter specified a nanoseconds field value less than zero or greater than or equal to 1000 million. π©[ETIMEDOUT]β The semaphore could not be locked before the specified timeout expired. β³[EINTR]β A signal interrupted this function. π
π¦ SEE ALSO π¦
π STANDARDS π
The sem_timedwait() function conforms to IEEE Std 1003.1-2004 (βPOSIX.1β). The sem_clockwait_np() function is not specified by any standard; it exists only on FreeBSD at the time of this writing. very exclusive, very βyou wouldnβt get itβ energy π
β HISTORY β
The sem_timedwait() function first appeared in FreeBSD 5.0. The sem_clockwait_np() function first appeared in FreeBSD 11.1. glow-up timeline fr fr β¨
mwah π this page came from Debian, dated May 24, 2017, and itβs the SEM_TIMEDWAIT(3) manual page. bye bestie, go lock those semaphores responsibly π«Άπ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2008, David Xu <davidxu@FreeBSD.org>
- Copyright (C) 2001-2004 by the Institute of
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms