exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / pthread-spin.c
blob26caa72496b9be79131b9a58192a78e50ef118a1
1 /* POSIX spin locks.
2 Copyright (C) 2010-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Paul Eggert, 2010, and Bruno Haible <bruno@clisp.org>, 2019. */
19 #include <config.h>
21 /* Specification. */
22 #include <pthread.h>
24 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
25 # include "windows-spin.h"
26 #endif
28 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
29 /* Use Windows threads. */
31 int
32 pthread_spin_init (pthread_spinlock_t *lock,
33 _GL_UNUSED int shared_across_processes)
35 glwthread_spin_init (lock);
36 return 0;
39 int
40 pthread_spin_lock (pthread_spinlock_t *lock)
42 return glwthread_spin_lock (lock);
45 int
46 pthread_spin_trylock (pthread_spinlock_t *lock)
48 return glwthread_spin_trylock (lock);
51 int
52 pthread_spin_unlock (pthread_spinlock_t *lock)
54 return glwthread_spin_unlock (lock);
57 int
58 pthread_spin_destroy (pthread_spinlock_t *lock)
60 return glwthread_spin_destroy (lock);
63 #elif HAVE_PTHREAD_H
64 /* Provide workarounds for POSIX threads. */
66 /* We don't use the C11 <stdatomic.h> (available in GCC >= 4.9) because it would
67 require to link with -latomic. */
69 # if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) \
70 || __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 1)) \
71 && !defined __ibmxl__
72 /* Use GCC built-ins (available in GCC >= 4.7 and clang >= 3.1) that operate on
73 the first byte of the lock.
74 Documentation:
75 <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/_005f_005fatomic-Builtins.html> */
77 # if 1
78 /* An implementation that verifies the unlocks. */
80 int
81 pthread_spin_init (pthread_spinlock_t *lock,
82 _GL_UNUSED int shared_across_processes)
84 __atomic_store_n ((unsigned int *) lock, 0, __ATOMIC_SEQ_CST);
85 return 0;
88 int
89 pthread_spin_lock (pthread_spinlock_t *lock)
91 /* Wait until *lock becomes 0, then replace it with 1. */
92 unsigned int zero;
93 while (!(zero = 0,
94 __atomic_compare_exchange_n ((unsigned int *) lock, &zero, 1, false,
95 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)))
97 return 0;
101 pthread_spin_trylock (pthread_spinlock_t *lock)
103 unsigned int zero;
104 if (!(zero = 0,
105 __atomic_compare_exchange_n ((unsigned int *) lock, &zero, 1, false,
106 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)))
107 return EBUSY;
108 return 0;
112 pthread_spin_unlock (pthread_spinlock_t *lock)
114 /* If *lock is 1, then replace it with 0. */
115 unsigned int one = 1;
116 if (!__atomic_compare_exchange_n ((unsigned int *) lock, &one, 0, false,
117 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
118 abort ();
119 return 0;
122 # else
123 /* An implementation that is a little bit more optimized, but does not verify
124 the unlocks. */
127 pthread_spin_init (pthread_spinlock_t *lock,
128 _GL_UNUSED int shared_across_processes)
130 __atomic_clear (lock, __ATOMIC_SEQ_CST);
131 return 0;
135 pthread_spin_lock (pthread_spinlock_t *lock)
137 while (__atomic_test_and_set (lock, __ATOMIC_SEQ_CST))
139 return 0;
143 pthread_spin_trylock (pthread_spinlock_t *lock)
145 if (__atomic_test_and_set (lock, __ATOMIC_SEQ_CST))
146 return EBUSY;
147 return 0;
151 pthread_spin_unlock (pthread_spinlock_t *lock)
153 __atomic_clear (lock, __ATOMIC_SEQ_CST);
154 return 0;
157 # endif
160 pthread_spin_destroy (pthread_spinlock_t *lock)
162 return 0;
165 # elif (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) \
166 || __clang_major__ >= 3) \
167 && HAVE_ATOMIC_COMPARE_AND_SWAP_GCC41)
168 /* Use GCC built-ins (available on many platforms with GCC >= 4.1 or
169 clang >= 3.0).
170 Documentation:
171 <https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html> */
174 pthread_spin_init (pthread_spinlock_t *lock,
175 _GL_UNUSED int shared_across_processes)
177 * (volatile unsigned int *) lock = 0;
178 __sync_synchronize ();
179 return 0;
183 pthread_spin_lock (pthread_spinlock_t *lock)
185 /* Wait until *lock becomes 0, then replace it with 1. */
186 while (__sync_val_compare_and_swap ((unsigned int *) lock, 0, 1) != 0)
188 return 0;
192 pthread_spin_trylock (pthread_spinlock_t *lock)
194 if (__sync_val_compare_and_swap ((unsigned int *) lock, 0, 1) != 0)
195 return EBUSY;
196 return 0;
200 pthread_spin_unlock (pthread_spinlock_t *lock)
202 /* If *lock is 1, then replace it with 0. */
203 if (__sync_val_compare_and_swap ((unsigned int *) lock, 1, 0) != 1)
204 abort ();
205 return 0;
209 pthread_spin_destroy (pthread_spinlock_t *lock)
211 return 0;
214 # else
215 /* Emulate a spin lock through a mutex. */
218 pthread_spin_init (pthread_spinlock_t *lock,
219 _GL_UNUSED int shared_across_processes)
221 return pthread_mutex_init (lock, NULL);
225 pthread_spin_lock (pthread_spinlock_t *lock)
227 return pthread_mutex_lock (lock);
231 pthread_spin_trylock (pthread_spinlock_t *lock)
233 return pthread_mutex_trylock (lock);
237 pthread_spin_unlock (pthread_spinlock_t *lock)
239 return pthread_mutex_unlock (lock);
243 pthread_spin_destroy (pthread_spinlock_t *lock)
245 return pthread_mutex_destroy (lock);
248 # endif
250 #else
251 /* Provide a dummy implementation for single-threaded applications. */
254 pthread_spin_init (_GL_UNUSED pthread_spinlock_t *lock,
255 _GL_UNUSED int shared_across_processes)
257 return 0;
261 pthread_spin_lock (_GL_UNUSED pthread_spinlock_t *lock)
263 return 0;
267 pthread_spin_trylock (_GL_UNUSED pthread_spinlock_t *lock)
269 return 0;
273 pthread_spin_unlock (_GL_UNUSED pthread_spinlock_t *lock)
275 return 0;
279 pthread_spin_destroy (_GL_UNUSED pthread_spinlock_t *lock)
281 return 0;
284 #endif