PTHREAD...DESTROY(3POSIX)

๐Ÿฆ‹ PROLOG ๐Ÿฆ‹

okay so bestie, real talk first ๐Ÿ’– this page is part of the POSIX Programmer's Manual โ€” that's the manual name, we're in PTHREAD...DESTROY(3POSIX) territory rn. But heads up: the Linux implementation of this interface may differ, so consult the corresponding Linux manual page for details on Linux behavior, or honestly the interface might not even be implemented on Linux at all. ๐Ÿ™ˆ iykyk.


โœจ NAME โœจ

pthread_spin_destroy, pthread_spin_init โ€” destroy or initialize a spin lock object. two little functions, one big job, slay ๐Ÿ’…


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <pthread.h>

int pthread_spin_destroy(pthread_spinlock_t *lock);
int pthread_spin_init(pthread_spinlock_t *lock, int pshared);

๐Ÿ’œ DESCRIPTION ๐Ÿ’œ

okay so pthread_spin_destroy() shall destroy the spin lock referenced by lock and release any resources used by the lock. and like, the effect of subsequent use of the lock is undefined until the lock is reinitialized by another call to pthread_spin_init(). periodt. also the results are undefined if pthread_spin_destroy() is called when a thread holds the lock, or if this function is called with an uninitialized thread spin lock. so don't do that. ๐Ÿ˜ญ

pthread_spin_init() shall allocate any resources required to use the spin lock referenced by lock and initialize the lock to an unlocked state. cute, fresh start, main character energy โœจ

If the Thread Process-Shared Synchronization option is supported and the value of pshared is PTHREAD_PROCESS_SHARED, the implementation shall permit the spin lock to be operated upon by any thread that has access to the memory where the spin lock is allocated โ€” even if it's allocated in memory that is shared by multiple processes. so like, cross-process besties. iconic ๐Ÿซถ

See Section 2.9.9, Synchronization Object Copies and Alternative Mappings for further requirements. (yes there's homework.)

the results are undefined if pthread_spin_init() is called specifying an already initialized spin lock. also undefined if a spin lock is used without first being initialized. so initialize first, queen. ๐Ÿ’…

If the pthread_spin_init() function fails, the lock is not initialized and the contents of lock are undefined.

Only the object referenced by lock may be used for performing synchronization. just that one. no side pieces.

and the result of referring to copies of that object in calls to pthread_spin_destroy(), pthread_spin_lock(), pthread_spin_trylock(), or pthread_spin_unlock() is undefined. ๐Ÿ’€ so no dupe-copies, got it.


๐Ÿ’– RETURN VALUE ๐Ÿ’–

upon successful completion, these functions shall return zero; otherwise, an error number shall be returned to indicate the error. zero = good vibes, nonzero = the vibe check failed. ๐ŸŒˆ


๐ŸŽ€ ERRORS ๐ŸŽ€

the pthread_spin_init() function shall fail if:

  • EAGAIN โ€” the system lacks the necessary resources to initialize another spin lock. (system said "no more, i'm tired") ๐Ÿ˜ญ
  • ENOMEM โ€” insufficient memory exists to initialize the lock. (out of memory, we've all been there)

these functions shall not return an error code of [EINTR]. so don't go looking for it. ๐Ÿ™…

the following sections are informative. (that's the manual being like "okay the rest is just lore, not rules") โœจ


๐ŸŒŸ EXAMPLES ๐ŸŒŸ

None. (nothing here bestie, move along ๐Ÿ’€)


๐Ÿฆ„ APPLICATION USAGE ๐Ÿฆ„

None. (also nothing. the void is voiding.)


๐Ÿ‘‘ RATIONALE ๐Ÿ‘‘

okay so if an implementation detects that the value specified by the lock argument to pthread_spin_destroy() does not refer to an initialized spin lock object, it is recommended that the function should fail and report an [EINVAL] error. like, it's a recommendation, not a mandate, but a good one ngl.

and if an implementation detects that the value specified by the lock argument to pthread_spin_destroy() or pthread_spin_init() refers to a locked spin lock object โ€” or detects that the value specified by the lock argument to pthread_spin_init() refers to an already initialized spin lock object โ€” it is recommended that the function should fail and report an [EBUSY] error. busy busy busy. ๐Ÿ”ฅ


๐Ÿ”ฎ FUTURE DIRECTIONS ๐Ÿ”ฎ

None. (the future is... also nothing. slay.)


๐ŸŒˆ SEE ALSO ๐ŸŒˆ

  • pthread_spin_lock()
  • pthread_spin_unlock()

and from the Base Definitions volume of POSIX.1โ€2017: <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 .


okay that's the whole page bestie, we ate and left no crumbs ๐Ÿ“ this one's brought to you by IEEE/The Open Group, 2017 โ€” yeah, 2017, vintage icon behavior โ€” and it lives in the POSIX Programmer's Manual under PTHREAD...DESTROY(3POSIX). spin responsibly, initialize before you use, and never ever refer to copies of the object. mwah ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from