PTHREAD_SPIN_LOCK(3P)

PTHREAD_SPIN_LOCK(3P) πŸ’…βœ¨

okay so bestie, this one's from the POSIX Programmer's Manual (yes, that POSIX) β€” it's about spin locks, and we're gonna get into it 🫢


PROLOG 🌸

This manual page is part of the POSIX Programmer's Manual, periodt. But heads up, bestie: the Linux implementation of this interface may differ (so like, go consult the corresponding Linux manual page for the tea on Linux behavior), or honestly the interface may just not be implemented on Linux at all πŸ’€. It's giving "your mileage may vary."


NAME πŸ‘‘

pthread_spin_lock, pthread_spin_trylock β€” lock a spin lock object πŸ”’


SYNOPSIS ✨

#include <pthread.h>

int pthread_spin_lock(pthread_spinlock_t *lock);
int pthread_spin_trylock(pthread_spinlock_t *lock);

DESCRIPTION πŸ¦‹

So pthread_spin_lock() shall lock the spin lock referenced by lock, no cap. If nobody else is holding it, the calling thread shall acquire the lock. But if somebody is holding it? The thread shall spin β€” and when we say spin, we mean it literally shall not return from the pthread_spin_lock() call until the lock becomes available. It's just gonna sit there. Spinning. Main character energy but make it a busy-wait πŸ’€

The results are undefined if the calling thread holds the lock at the time the call is made. So don't do that, bestie πŸ™ˆ

Meanwhile pthread_spin_trylock() is the chill one β€” it shall lock the spin lock referenced by lock if it's not held by any thread. Otherwise? The function shall fail. No spinning, no waiting, it just says "not today" and bounces.

And one more thing: the results are undefined if any of these functions is called with an uninitialized spin lock. Initialize your locks, fr fr 😭


RETURN VALUE πŸ’–

Slay behavior: on successful completion these functions shall return zero πŸŽ€. Otherwise, an error number shall be returned to indicate the error.


ERRORS πŸ”₯

The pthread_spin_lock() function may fail if:

  • EDEADLK β€” a deadlock condition was detected. 😡

The pthread_spin_trylock() function shall fail if:

  • EBUSY β€” a thread currently holds the lock. πŸ™…β™€οΈ

And important: these functions shall not return an error code of [EINTR]. Not a thing here, bestie.

The following sections are informative. (iykyk)

EXAMPLES 🌈

None. That's it. That's the section. πŸ’€


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. So like, know that going in, bestie.


RATIONALE πŸ‘‘

If an implementation detects that the value specified by the lock argument to pthread_spin_lock() or pthread_spin_trylock() does not refer to an initialized spin lock object, it is recommended that the function should fail and report an [EINVAL] error.

And if an implementation detects that the value specified by the lock argument to pthread_spin_lock() refers to a spin lock object for which the calling thread already holds the lock, it is recommended that the function should fail and report an [EDEADLK] error.


FUTURE DIRECTIONS ⭐

None. ✨


SEE ALSO πŸŽ€

  • pthread_spin_destroy()
  • pthread_spin_unlock()

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

  • Section 3.291, Priority Inversion
  • Section 4.12, Memory Synchronization
  • <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!! brought to you by IEEE/The Open Group, dated 2017, under the title PTHREAD_SPIN_LOCK(3P) β€” spin responsibly, initialize your locks, and stay un-deadlocked πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from