PTHREAD...IMEDLOCK(3POSIX)

πŸ’… PTHREAD_MUTEX_TIMEDLOCK πŸ’…

okay bestie, welcome to the POSIX Programmer's Manual (that's the manual name, section 3POSIX) β€” we're talking pthread_mutex_timedlock() today and honestly? it's giving "I'll wait for you but only until the clock says so" ✨


🌸 PROLOG 🌸

so before we even start: this page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (pls go read the corresponding Linux manual page for the deets on Linux behavior), or like… the interface might not even be implemented on Linux at all. πŸ™ˆ just so you know.


✨ NAME ✨

pthread_mutex_timedlock β€” lock a mutex πŸ”’


πŸŽ€ SYNOPSIS πŸŽ€

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

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

look at those restrict qualifiers. main character energy honestly πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the vibe: pthread_mutex_timedlock() shall lock the mutex object referenced by mutex. if the mutex is already locked, the calling thread shall block until the mutex becomes available β€” exactly like pthread_mutex_lock() does. but here's the twist: if the mutex can't be locked without waiting for another thread to unlock it, that wait shall be terminated when the specified timeout expires. ⏰

the timeout shall expire when the absolute time specified by abstime passes β€” measured by the clock on which timeouts are based (that is, 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. no cap, if you're already late, it's already late.

the timeout shall be based on the CLOCK_REALTIME clock. the resolution of the timeout shall be the resolution of the clock on which it's based. the timespec data type is defined in the <time.h> header, iykyk.

🫢 under no circumstance shall the function fail with a timeout if the mutex can be locked immediately. and the validity of the abstime parameter need not be checked if the mutex can be locked immediately. so don't stress about it.

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 shall be adjusted as necessary to reflect the fact that this thread is no longer among the threads waiting for the mutex. ✨


now for the robust mutex lore, because it's a whole thing:

if mutex is a robust mutex and the process containing the owning thread terminated while holding the mutex lock, a call to pthread_mutex_timedlock() shall return the error value [EOWNERDEAD]. and if mutex is a robust mutex and the owning thread terminated while holding the mutex lock, a call to pthread_mutex_timedlock() may return the error value [EOWNERDEAD] even if the process in which the owning thread resides has not terminated. 😭

in these cases, the mutex is locked by the thread but the state it protects is marked as inconsistent. the application should ensure that the state is made consistent for reuse, and when that's complete, call pthread_mutex_consistent(). if the application is unable to recover the state, it should unlock the mutex without a prior call to pthread_mutex_consistent() β€” after which the mutex is marked permanently unusable. πŸ’€ yeah. forever. gone.

and if mutex does not refer to an initialized mutex object? the behavior is undefined. do not do that.


🌈 RETURN VALUE 🌈

if successful, pthread_mutex_timedlock() shall return zero πŸŽ‰ β€” otherwise, an error number shall be returned to indicate the error. so like, check your returns bestie.


πŸ”₯ ERRORS πŸ”₯

the ones that SHALL fail 🚨

  • EAGAIN β€” the mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded. you hit the ceiling, babe.
  • EDEADLK β€” the mutex type is PTHREAD_MUTEX_ERRORCHECK and the current thread already owns the mutex. you're literally fighting yourself.
  • 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' current priority ceiling.
  • EINVAL β€” the process or thread would have blocked, and the abstime parameter specified a nanoseconds field value less than zero or greater than or equal to 1000 million. (yes, two separate EINVAL reasons. iconic.)
  • ENOTRECOVERABLE β€” the state protected by the mutex is not recoverable. rip.
  • EOWNERDEAD β€” the mutex is a robust mutex and the process containing the previous owning thread terminated while holding the mutex lock. the mutex lock shall be acquired by the calling thread and it is up to the new owner to make the state consistent. your problem now, bestie 🫢
  • ETIMEDOUT β€” the mutex could not be locked before the specified timeout expired. clock ran out, no notes.

the ones that MAY fail πŸ€”

  • EDEADLK β€” a deadlock condition was detected.
  • EOWNERDEAD β€” the mutex is a robust mutex and the previous owning thread terminated while holding the mutex lock. the mutex lock shall be acquired by the calling thread and it is up to the new owner to make the state consistent.
βœ‹ and one more thing: this function shall not return an error code of [EINTR]. periodt.

the following sections are informative. (translation: the above was the real tea, this next bit is bonus commentary πŸ’–)


πŸ“ EXAMPLES πŸ“

None. β€” literally nothing. an empty plate. we move.


🧚 APPLICATION USAGE 🧚

applications that have assumed that non-zero return values are errors will need updating for use with robust mutexes β€” because a valid return for a thread acquiring a mutex which is protecting a currently inconsistent state is [EOWNERDEAD]. yeah. a "good" outcome that looks like a bad one. the way that flips the whole convention 😭

applications that do not check the error returns, due to ruling out the possibility of such errors arising, should not use robust mutexes. if an application is supposed to work with normal and robust mutexes, it should check all return values for error conditions and, if necessary, take appropriate action. check everything, always. πŸ’…


πŸ’œ RATIONALE πŸ’œ

refer to pthread_mutex_lock(). that's it. that's the whole section. go read that one, babe.


⭐ FUTURE DIRECTIONS ⭐

None. β€” the future is bright and empty, and honestly? we love that for us.


🌸 SEE ALSO 🌸

  • pthread_mutex_destroy()
  • pthread_mutex_lock()
  • time()

and from the Base Definitions volume of POSIX.1‐2017: Section 4.12, Memory Synchronization, <pthread.h>, <time.h> πŸ‘‘


portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html . πŸ“š

any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page, bestie!! this one came from IEEE/The Open Group, dated 2017, under the PTHREAD...IMEDLOCK(3POSIX) header. lock responsibly, check your return values, and never forget that [EOWNERDEAD] might just be a win in disguise πŸ’–βœ¨πŸ¦‹