SEM_TIMEDWAIT(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

πŸŽ€ 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] β€” The sem argument does not refer to a valid semaphore, or the process or 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 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