PTHREAD_R...MEDWRLOCK(3P)

πŸ’– PTHREAD_RWLOCK_TIMEDWRLOCK(3P) πŸ’–

okay bestie, gather round, because we're reading a page straight out of the POSIX Programmer's Manual and it's giving ✨systems programming✨. pull up a chair, grab a boba, and let's get into it πŸ§‹πŸ¦‹


🚨 PROLOG 🚨

so real talk first: this manual page is part of the POSIX Programmer's Manual. that's the OG spec energy. but heads up, queen β€”

the Linux implementation of this interface may differ (so go consult the corresponding Linux manual page for the deets on Linux behavior), or honestly? the interface may not be implemented on Linux at all. πŸ’€

no cap, always double-check before you ship.


✨ NAME ✨

pthread_rwlock_timedwrlock β€” lock a read-write lock for writing πŸŽ€

that's it. that's the whole vibe. she locks, she writes, she has a timer. iconic.


🌸 SYNOPSIS 🌸

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

int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict rwlock,
    const struct timespec *restrict abstime);

two restrict pointers, zero chill. πŸ’…


πŸ’œ DESCRIPTION πŸ’œ

okay so pthread_rwlock_timedwrlock() applies a write lock to the read-write lock referenced by rwlock, exactly like pthread_rwlock_wrlock() does. same girl, different accessory.

but here's the plot twist 🫢: if the lock can't be acquired without waiting for other threads to unlock it, this wait gets terminated when the specified timeout expires. the timeout expires when the absolute time specified by abstime passes β€” as measured by the clock that timeouts are based on, meaning when the value of that clock equals or exceeds abstime β€” or if that absolute time has already been passed at the moment of the call. so, y'know, fashionably late = instant timeout. 😭

the timeout is based on the CLOCK_REALTIME clock. ⏰ the resolution of the timeout is the resolution of the CLOCK_REALTIME clock. the timespec data type is defined in the <time.h> header.

and now the important part, bestie, do NOT skip this:

  • under no circumstances shall the function fail with a timeout if the lock can be acquired immediately πŸ”₯
  • the validity of the abstime parameter need not be checked if the lock can be immediately acquired

if a signal that causes a signal handler to be executed gets delivered to a thread blocked on a read-write lock via a call to pthread_rwlock_timedwrlock(), then upon return from the signal handler the thread shall resume waiting for the lock as if it was not interrupted. she does not get distracted. she does not spiral. main character focus. πŸ‘‘

πŸ’€ deadlock warning, fr fr: the calling thread may deadlock if at the time the call is made it holds the read-write lock. and the results are undefined if this function is called with an uninitialized read-write lock. so like... please initialize your locks. i am begging. πŸ™ˆ

🌈 RETURN VALUE 🌈

pthread_rwlock_timedwrlock() shall return zero if the lock for writing on the read-write lock object referenced by rwlock is acquired. πŸŽ‰

otherwise, an error number shall be returned to indicate the error. slay but make it diagnostic.


πŸŽ€ ERRORS πŸŽ€

pthread_rwlock_timedwrlock() shall fail if:

  • ETIMEDOUT β€” the lock could not be acquired before the specified timeout expired. ⏳ yeah she ran out of time, it happens to the best of us

pthread_rwlock_timedwrlock() may fail if:

  • EDEADLK β€” a deadlock condition was detected, or the calling thread already holds the rwlock. πŸŒ€
  • EINVAL β€” the abstime nanosecond value is less than zero or greater than or equal to 1000 million. 🚫

and one more, underlined twice in glitter pen:

this function shall not return an error code of [EINTR]. periodt. πŸ’…

the following sections are informative. πŸ“’ (yes, that's a real line, we're keeping it, it's the manpage's little disclaimer moment)


πŸ¦‹ EXAMPLES πŸ¦‹

None. 😌 nothing. zero. the void. she simply did not provide.


πŸ’… APPLICATION USAGE πŸ’…

applications using this function may be subject to priority inversion β€” as discussed in the Base Definitions volume of POSIX.1‐2017, Section 3.291, Priority Inversion. go read it if that's your thing, nerd (affectionate). 🫢


🧚 RATIONALE 🧚

if an implementation detects that the value specified by the rwlock argument to pthread_rwlock_timedwrlock() does not refer to an initialized read-write lock object, it is recommended that the function should fail and report an [EINVAL] error. πŸ‘€ consistency queen.


⭐ FUTURE DIRECTIONS ⭐

None. the future is a blank canvas and she's not filling it in. 🎨


πŸ”₯ SEE ALSO πŸ”₯

  • pthread_rwlock_destroy()
  • pthread_rwlock_rdlock()
  • pthread_rwlock_timedrdlock()
  • pthread_rwlock_trywrlock()
  • pthread_rwlock_unlock()

and from the Base Definitions volume of POSIX.1‐2017:

  • Section 3.291, Priority Inversion
  • Section 4.12, Memory Synchronization
  • <pthread.h>
  • <time.h>

the whole friend group πŸ«‚


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 . πŸ› (not me volunteering to fix them tho πŸ’€)


okay that's the whole page, bestie, you ate it up, you understood it, you're basically a POSIX girlie now πŸŒΈπŸ¦‹

this one came to you from IEEE/The Open Group, 2017 β€” mwah, love you, bye πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from