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. */
24 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
25 # include "windows-spin.h"
28 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
29 /* Use Windows threads. */
32 pthread_spin_init (pthread_spinlock_t
*lock
,
33 _GL_UNUSED
int shared_across_processes
)
35 glwthread_spin_init (lock
);
40 pthread_spin_lock (pthread_spinlock_t
*lock
)
42 return glwthread_spin_lock (lock
);
46 pthread_spin_trylock (pthread_spinlock_t
*lock
)
48 return glwthread_spin_trylock (lock
);
52 pthread_spin_unlock (pthread_spinlock_t
*lock
)
54 return glwthread_spin_unlock (lock
);
58 pthread_spin_destroy (pthread_spinlock_t
*lock
)
60 return glwthread_spin_destroy (lock
);
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)) \
72 /* Use GCC built-ins (available in GCC >= 4.7 and clang >= 3.1) that operate on
73 the first byte of the lock.
75 <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/_005f_005fatomic-Builtins.html> */
78 /* An implementation that verifies the unlocks. */
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
);
89 pthread_spin_lock (pthread_spinlock_t
*lock
)
91 /* Wait until *lock becomes 0, then replace it with 1. */
94 __atomic_compare_exchange_n ((unsigned int *) lock
, &zero
, 1, false,
95 __ATOMIC_SEQ_CST
, __ATOMIC_SEQ_CST
)))
101 pthread_spin_trylock (pthread_spinlock_t
*lock
)
105 __atomic_compare_exchange_n ((unsigned int *) lock
, &zero
, 1, false,
106 __ATOMIC_SEQ_CST
, __ATOMIC_SEQ_CST
)))
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
))
123 /* An implementation that is a little bit more optimized, but does not verify
127 pthread_spin_init (pthread_spinlock_t
*lock
,
128 _GL_UNUSED
int shared_across_processes
)
130 __atomic_clear (lock
, __ATOMIC_SEQ_CST
);
135 pthread_spin_lock (pthread_spinlock_t
*lock
)
137 while (__atomic_test_and_set (lock
, __ATOMIC_SEQ_CST
))
143 pthread_spin_trylock (pthread_spinlock_t
*lock
)
145 if (__atomic_test_and_set (lock
, __ATOMIC_SEQ_CST
))
151 pthread_spin_unlock (pthread_spinlock_t
*lock
)
153 __atomic_clear (lock
, __ATOMIC_SEQ_CST
);
160 pthread_spin_destroy (pthread_spinlock_t
*lock
)
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
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 ();
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)
192 pthread_spin_trylock (pthread_spinlock_t
*lock
)
194 if (__sync_val_compare_and_swap ((unsigned int *) lock
, 0, 1) != 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)
209 pthread_spin_destroy (pthread_spinlock_t
*lock
)
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
);
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
)
261 pthread_spin_lock (_GL_UNUSED pthread_spinlock_t
*lock
)
267 pthread_spin_trylock (_GL_UNUSED pthread_spinlock_t
*lock
)
273 pthread_spin_unlock (_GL_UNUSED pthread_spinlock_t
*lock
)
279 pthread_spin_destroy (_GL_UNUSED pthread_spinlock_t
*lock
)