1 .\" $NetBSD: pthread_spin_lock.3,v 1.7 2008/05/26 00:31:08 ad Exp $
3 .\" Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
14 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
17 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 .\" POSSIBILITY OF SUCH DAMAGE.
26 .Dt PTHREAD_SPIN_LOCK 3
29 .Nm pthread_spin_lock ,
30 .Nm pthread_spin_trylock
31 .Nd acquire a spin lock
37 .Fn pthread_spin_lock "pthread_spinlock_t *lock"
39 .Fn pthread_spin_trylock "pthread_spinlock_t *lock"
43 function acquires a spin lock on
47 is not presently held.
49 immediately acquired, the calling thread repeatedly retries until it can
53 .Fn pthread_spin_trylock
54 function performs the same action, but does not block if the lock
55 cannot be immediately obtained (i.e., the lock is held).
60 .Fn pthread_spin_trylock
61 functions will return zero.
62 Otherwise an error number will be returned to indicate the error.
65 .Fn pthread_spin_trylock
66 function shall fail if:
69 The lock could not be acquired because a writer holds the lock or
78 The current thread already owns
86 .Fn pthread_spin_trylock
87 functions may fail if:
90 The value specified by
95 .Xr pthread_spin_destroy 3 ,
96 .Xr pthread_spin_init 3 ,
97 .Xr pthread_spin_unlock 3
101 .Fn pthread_spin_trylock
105 Applications using spinlocks are vulnerable to the effects of priority
107 Applications using real-time threads
108 .Pq Dv SCHED_FIFO , SCHED_RR
109 should not use these interfaces.
110 Outside carefully controlled environments, priority inversion with spinlocks
111 can lead to system deadlock.
112 Mutexes are preferable in nearly every possible use case.