2 Copyright (C) 2010-2019 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program 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 General Public License for more details.
14 You should have received a copy of the GNU 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 int shared_across_processes _GL_UNUSED
)
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 /* Use GCC built-ins (available in GCC >= 4.7) that operate on the first byte
73 <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/_005f_005fatomic-Builtins.html> */
76 pthread_spin_init (pthread_spinlock_t
*lock
,
77 int shared_across_processes _GL_UNUSED
)
79 __atomic_clear (lock
, __ATOMIC_SEQ_CST
);
84 pthread_spin_lock (pthread_spinlock_t
*lock
)
86 while (__atomic_test_and_set (lock
, __ATOMIC_SEQ_CST
))
92 pthread_spin_trylock (pthread_spinlock_t
*lock
)
94 if (__atomic_test_and_set (lock
, __ATOMIC_SEQ_CST
))
100 pthread_spin_unlock (pthread_spinlock_t
*lock
)
102 __atomic_clear (lock
, __ATOMIC_SEQ_CST
);
107 pthread_spin_destroy (pthread_spinlock_t
*lock
)
113 /* Emulate a spin lock through a mutex. */
116 pthread_spin_init (pthread_spinlock_t
*lock
,
117 int shared_across_processes _GL_UNUSED
)
119 return pthread_mutex_init (lock
, NULL
);
123 pthread_spin_lock (pthread_spinlock_t
*lock
)
125 return pthread_mutex_lock (lock
);
129 pthread_spin_trylock (pthread_spinlock_t
*lock
)
131 return pthread_mutex_trylock (lock
);
135 pthread_spin_unlock (pthread_spinlock_t
*lock
)
137 return pthread_mutex_unlock (lock
);
141 pthread_spin_destroy (pthread_spinlock_t
*lock
)
143 return pthread_mutex_destroy (lock
);
149 /* Provide a dummy implementation for single-threaded applications. */
152 pthread_spin_init (pthread_spinlock_t
*lock _GL_UNUSED
,
153 int shared_across_processes _GL_UNUSED
)
159 pthread_spin_lock (pthread_spinlock_t
*lock _GL_UNUSED
)
165 pthread_spin_trylock (pthread_spinlock_t
*lock _GL_UNUSED
)
171 pthread_spin_unlock (pthread_spinlock_t
*lock _GL_UNUSED
)
177 pthread_spin_destroy (pthread_spinlock_t
*lock _GL_UNUSED
)