atoll: Assume that the compiler supports 'long long'.
[gnulib.git] / lib / pthread-spin.c
blob0b3410937ac9950daa53dac9659bd17875007faf
1 /* POSIX spin locks.
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)
7 any later version.
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. */
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 int shared_across_processes _GL_UNUSED)
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 /* Use GCC built-ins (available in GCC >= 4.7) that operate on the first byte
71 of the lock.
72 Documentation:
73 <https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/_005f_005fatomic-Builtins.html> */
75 int
76 pthread_spin_init (pthread_spinlock_t *lock,
77 int shared_across_processes _GL_UNUSED)
79 __atomic_clear (lock, __ATOMIC_SEQ_CST);
80 return 0;
83 int
84 pthread_spin_lock (pthread_spinlock_t *lock)
86 while (__atomic_test_and_set (lock, __ATOMIC_SEQ_CST))
88 return 0;
91 int
92 pthread_spin_trylock (pthread_spinlock_t *lock)
94 if (__atomic_test_and_set (lock, __ATOMIC_SEQ_CST))
95 return EBUSY;
96 return 0;
99 int
100 pthread_spin_unlock (pthread_spinlock_t *lock)
102 __atomic_clear (lock, __ATOMIC_SEQ_CST);
103 return 0;
107 pthread_spin_destroy (pthread_spinlock_t *lock)
109 return 0;
112 # else
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);
146 # endif
148 #else
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)
155 return 0;
159 pthread_spin_lock (pthread_spinlock_t *lock _GL_UNUSED)
161 return 0;
165 pthread_spin_trylock (pthread_spinlock_t *lock _GL_UNUSED)
167 return 0;
171 pthread_spin_unlock (pthread_spinlock_t *lock _GL_UNUSED)
173 return 0;
177 pthread_spin_destroy (pthread_spinlock_t *lock _GL_UNUSED)
179 return 0;
182 #endif