PTHREAD...K_RDLOCK(3POSIX)

πŸ’– PTHREAD_RWLOCK_RDLOCK β€” the POSIX Programmer's Manual cut πŸ’–

okay bestie, quick vibe check before we dive in: this page lives in the POSIX Programmer's Manual (you'll see it tagged as PTHREAD...K_RDLOCK(3POSIX) up top, that's just how the cool kids file it). And a heads-up from the PROLOG, no cap:

⚠️ The Linux implementation of this interface may differ β€” go check the corresponding Linux manual page for the tea on Linux behavior. OR the interface may just... not be implemented on Linux at all. πŸ™ˆ So like, verify before you ship, fr fr.

✨ NAME ✨

pthread_rwlock_rdlock, pthread_rwlock_tryrdlock β€” lock a read-write lock object for reading πŸ”’πŸ“–


🌸 SYNOPSIS 🌸

#include <pthread.h>

int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

πŸ¦‹ DESCRIPTION πŸ¦‹

So pthread_rwlock_rdlock() shall apply a read lock to the read-write lock referenced by rwlock. The calling thread acquires that read lock if a writer does not hold the lock and there are no writers blocked on the lock. Both conditions, bestie. ✨

If the Thread Execution Scheduling option is supported, and the threads involved in the lock are executing with the scheduling policies SCHED_FIFO or SCHED_RR, then the calling thread shall not acquire the lock if a writer holds the lock, or if writers of higher or equal priority are blocked on the lock. Otherwise? The calling thread shall acquire the lock. πŸ’…

If the Thread Execution Scheduling option is supported, and the threads involved in the lock are executing with the SCHED_SPORADIC scheduling policy, same energy: the calling thread shall not acquire the lock if a writer holds the lock or if writers of higher or equal priority are blocked on the lock; otherwise, the calling thread shall acquire the lock. πŸ‘‘

If the Thread Execution Scheduling option is not supported, it's implementation-defined whether the calling thread acquires the lock when a writer does not hold the lock and there are writers blocked on the lock. But if a writer does hold the lock, the calling thread shall not acquire the read lock, periodt. And if the read lock is not acquired, the calling thread shall block until it can acquire the lock.

πŸ’€ Careful: the calling thread may deadlock if, at the time the call is made, it holds a write lock. Not the cute kind of deadlock, the "your program is just gone" kind.

A thread may hold multiple concurrent read locks on rwlock β€” like, successfully calling pthread_rwlock_rdlock() n times. If it does that, the application shall ensure the thread performs matching unlocks, i.e. it calls pthread_rwlock_unlock() n times. Matching. Unlocks. Don't leave them hanging. πŸŽ€

The maximum number of simultaneous read locks that an implementation guarantees can be applied to a read-write lock shall be implementation-defined, and pthread_rwlock_rdlock() may fail if that max would be exceeded. 😭

Now, pthread_rwlock_tryrdlock() β€” it shall apply a read lock just like pthread_rwlock_rdlock() does, except it shall fail if the equivalent pthread_rwlock_rdlock() call would have blocked the calling thread. And in no case shall pthread_rwlock_tryrdlock() ever block. It always either acquires the lock or fails and returns immediately. Iconic behavior honestly. 🫢

Results are undefined if any of these functions are called with an uninitialized read-write lock. Do not do that. πŸ™…

If a signal is delivered to a thread waiting for a read-write lock for reading, upon return from the signal handler the thread resumes waiting for the read-write lock for reading as if it was not interrupted. No reset, no restart, it just keeps waiting like nothing happened. 🌈


⭐ RETURN VALUE ⭐

If successful, pthread_rwlock_rdlock() shall return zero. Otherwise, an error number shall be returned to indicate the error. 🎯

pthread_rwlock_tryrdlock() shall return zero if the lock for reading on the read-write lock object referenced by rwlock is acquired. Otherwise, an error number shall be returned to indicate the error.


πŸ’… ERRORS πŸ’…

pthread_rwlock_tryrdlock() shall fail if:

  • EBUSY β€” the read-write lock could not be acquired for reading because a writer holds the lock, or a writer with the appropriate priority was blocked on it. πŸ™ˆ

pthread_rwlock_rdlock() and pthread_rwlock_tryrdlock() may fail if:

  • EAGAIN β€” the read lock could not be acquired because the maximum number of read locks for rwlock has been exceeded. 😭

pthread_rwlock_rdlock() may fail if:

  • EDEADLK β€” a deadlock condition was detected, or the current thread already owns the read-write lock for writing. πŸ’€
These functions shall not return an error code of [EINTR]. Nope. Not a thing here.

The following sections are informative. (aka: not the binding rules, just the helpful gossip πŸ‘€)


πŸŽ€ EXAMPLES πŸŽ€

None. Literally nothing. The void. πŸ«₯


🌸 APPLICATION USAGE 🌸

Applications using these functions may be subject to priority inversion, as discussed in the Base Definitions volume of POSIX.1‐2017, Section 3.291, Priority Inversion. iykyk. 😬


πŸ”₯ RATIONALE πŸ”₯

If an implementation detects that the value specified by the rwlock argument to pthread_rwlock_rdlock() or pthread_rwlock_tryrdlock() does not refer to an initialized read-write lock object, it is recommended that the function should fail and report an [EINVAL] error. Recommended, bestie. Not required. πŸ‘€


🧚 FUTURE DIRECTIONS 🧚

None. Same void energy. πŸ«₯


🌈 SEE ALSO 🌈

  • pthread_rwlock_destroy()
  • pthread_rwlock_timedrdlock()
  • pthread_rwlock_timedwrlock()
  • pthread_rwlock_trywrlock()
  • pthread_rwlock_unlock()

Also go read the Base Definitions volume of POSIX.1‐2017, Section 3.291, Priority Inversion, and Section 4.12, Memory Synchronization, plus the header <pthread.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's brought to you by IEEE/The Open Group, 2017 β€” PTHREAD...K_RDLOCK(3POSIX), signing off πŸŽ€πŸ¦‹πŸŒΈ