p
-----
The
.Fn pthread_spin_destroy
function is used to destroy a spin lock previously created with
.Fn pthread_spin_init .
It is undefined what happens if the function is called
when a thread holds the lock, or if the function is called
with an uninitialized spin lock.
p
-----
The
.Fn pthread_spin_lock
function acquires a spin lock on
.Fa lock ,
provided that
.Fa lock
is not presently held.
If the lock cannot be
immediately acquired, the calling thread repeatedly retries until it can
acquire the lock.
Undefined behavior may follow if the calling thread holds
the lock at the time the call is made.
p The .Fn pthread_spin_trylock function performs the same locking action, but does not block if the lock cannot be immediately obtained; if the lock is held, the call fails.
p
-----
The
.Fn pthread_spin_unlock
function is used to release the read/write lock previously obtained by
.Fn pthread_spin_lock
or
.Fn pthread_spin_trylock .
The results are undefined if the lock is not held by the calling thread.
----------------------------------------------------------------------------
.Sh RETURN VALUES
If successful, all described functions return zero.
Otherwise an error number will be returned to indicate the error.
.Sh ERRORS
The
.Fn pthread_spin_init
function shall fail if:
l -tag -width Er t Bq Er ENOMEM Insufficient memory exists to initialize the lock.
.El
p The .Fn pthread_spin_init function may fail if: l -tag -width Er t Bq Er EINVAL The .Fa lock parameter was .Dv NULL or the .Fa pshared parameter was neither .Dv PTHREAD_PROCESS_SHARED nor .Dv PTHREAD_PROCESS_PRIVATE . .El
p
-----
The
.Fn pthread_spin_destroy
function may fail if:
l -tag -width Er t Bq Er EBUSY The system has detected an attempt to destroy the object referenced by
.Fa lock
while it is locked.
t Bq Er EINVAL The value specified by
.Fa lock
is invalid.
.El
p
-----
The
.Fn pthread_spin_trylock
function shall fail if:
l -tag -width Er t Bq Er EBUSY The lock could not be acquired because a writer holds the lock or
was blocked on it.
.El
p The .Fn pthread_spin_lock function may fail if: l -tag -width Er t Bq Er EDEADLK The current thread already owns .Fa lock for writing. .El
p The .Fn pthread_spin_lock and .Fn pthread_spin_trylock functions may fail if: l -tag -width Er t Bq Er EINVAL The value specified by .Fa lock is invalid. .El
p
-----
The
.Fn pthread_spin_unlock
function may fail if:
l -tag -width Er t Bq Er EINVAL The value specified by
.Fa lock
is invalid.
.El
----------------------------------------------------------------------------
.Sh SEE ALSO
.Xr pthread 3 ,
.Xr pthread_barrier 3 ,
.Xr pthread_cond 3 ,
.Xr pthread_mutex 3 ,
.Xr pthread_rwlock 3
.Sh STANDARDS
These functions conform to
.St -p1003.1-2001 .
----------------------------------------------------------------------------
.Sh CAVEATS
Applications using spin locks are vulnerable to the effects of priority
inversion.
Applications using real-time threads
q Dv SCHED_FIFO ,
q Dv SCHED_RR should not use these interfaces. Outside carefully controlled environments, priority inversion with spin locks can lead to system deadlock. Mutexes are preferable in nearly every possible use case.