1 .\" Copyright (c) 2017, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH PTHREAD_SPIN_INIT 3 2021-03-22 "Linux" "Linux Programmer's Manual"
27 pthread_spin_init, pthread_spin_destroy \- initialize or destroy a spin lock
30 .B #include <pthread.h>
32 .BI "int pthread_spin_init(pthread_spinlock_t *" lock ", int " pshared ");"
33 .BI "int pthread_spin_destroy(pthread_spinlock_t *" lock ");"
36 Compile and link with \fI\-pthread\fP.
39 Feature Test Macro Requirements for glibc (see
40 .BR feature_test_macros (7)):
43 .BR pthread_spin_init (),
44 .BR pthread_spin_destroy ():
46 _POSIX_C_SOURCE >= 200112L
50 Most programs should use mutexes
51 instead of spin locks.
52 Spin locks are primarily useful in conjunction with real-time
57 .BR pthread_spin_init ()
58 function allocates any resources required for the use of
59 the spin lock referred to by
61 and initializes the lock to be in the unlocked state.
64 argument must have one of the following values:
66 .B PTHREAD_PROCESS_PRIVATE
67 The spin lock is to be operated on only by threads in the same process
68 as the thread that calls
69 .BR pthread_spin_init ().
70 (Attempting to share the spin lock between processes
71 results in undefined behavior.)
73 .B PTHREAD_PROCESS_SHARED
74 The spin lock may be operated on by any thread in any process that
75 has access to the memory containing the lock
76 (i.e., the lock may be in a shared memory object that is
77 shared among multiple processes).
80 .BR pthread_spin_init ()
81 on a spin lock that has already been initialized results
82 in undefined behavior.
85 .BR pthread_spin_destroy ()
86 function destroys a previously initialized spin lock,
87 freeing any resources that were allocated for that lock.
88 Destroying a spin lock that has not been previously been initialized
89 or destroying a spin lock while another thread holds the lock
90 results in undefined behavior.
92 Once a spin lock has been destroyed,
93 performing any operation on the lock other than
94 once more initializing it with
95 .BR pthread_spin_init ()
96 results in undefined behavior.
98 The result of performing operations such as
99 .BR pthread_spin_lock (3),
100 .BR pthread_spin_unlock (3),
102 .BR pthread_spin_destroy ()
105 of the object referred to by
109 On success, there functions return zero.
110 On failure, they return an error number.
112 .BR pthread_spin_init ()
113 fails, the lock is not initialized.
115 .BR pthread_spin_init ()
116 may fail with the following errors:
117 .\" These errors don't occur on the glibc implementation
120 The system has insufficient resources to initialize
124 Insufficient memory to initialize the spin lock.
126 These functions first appeared in glibc in version 2.2.
130 Support for process-shared spin locks is a POSIX option.
131 The option is supported in the glibc implementation.
133 Spin locks should be employed in conjunction with
134 real-time scheduling policies
138 Use of spin locks with nondeterministic scheduling policies such as
140 probably indicates a design mistake.
141 The problem is that if a thread operating under such a policy
142 is scheduled off the CPU while it holds a spin lock,
143 then other threads will waste time spinning on the lock
144 until the lock holder is once more rescheduled and releases the lock.
146 If threads create a deadlock situation while employing spin locks,
147 those threads will spin forever consuming CPU time.
149 User-space spin locks are
151 applicable as a general locking solution.
152 They are, by definition,
153 prone to priority inversion and unbounded spin times.
154 A programmer using spin locks must be exceptionally careful not
155 only in the code, but also in terms of system configuration,
156 thread placement, and priority assignment.
157 .\" FIXME . When PTHREAD_MUTEX_ADAPTIVE_NP is one day document
158 .\" make reference to it here
162 .BR pthread_mutex_init (3),
163 .BR pthread_mutex_lock (3),
164 .BR pthread_spin_lock (3),
165 .BR pthread_spin_unlock (3),