This source file includes following definitions.
- pthread_spin_init
- pthread_spin_lock
- pthread_spin_trylock
- pthread_spin_unlock
- pthread_spin_destroy
- pthread_spin_init
- pthread_spin_lock
- pthread_spin_trylock
- pthread_spin_unlock
- pthread_spin_init
- pthread_spin_lock
- pthread_spin_trylock
- pthread_spin_unlock
- pthread_spin_destroy
- pthread_spin_init
- pthread_spin_lock
- pthread_spin_trylock
- pthread_spin_unlock
- pthread_spin_destroy
- pthread_spin_init
- pthread_spin_lock
- pthread_spin_trylock
- pthread_spin_unlock
- pthread_spin_destroy
- pthread_spin_init
- pthread_spin_lock
- pthread_spin_trylock
- pthread_spin_unlock
- pthread_spin_destroy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21
22 #include <pthread.h>
23
24 #include <stdbool.h>
25
26 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
27 # include "windows-spin.h"
28 #endif
29
30 #if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
31
32
33 int
34 pthread_spin_init (pthread_spinlock_t *lock,
35 _GL_UNUSED int shared_across_processes)
36 {
37 glwthread_spin_init (lock);
38 return 0;
39 }
40
41 int
42 pthread_spin_lock (pthread_spinlock_t *lock)
43 {
44 return glwthread_spin_lock (lock);
45 }
46
47 int
48 pthread_spin_trylock (pthread_spinlock_t *lock)
49 {
50 return glwthread_spin_trylock (lock);
51 }
52
53 int
54 pthread_spin_unlock (pthread_spinlock_t *lock)
55 {
56 return glwthread_spin_unlock (lock);
57 }
58
59 int
60 pthread_spin_destroy (pthread_spinlock_t *lock)
61 {
62 return glwthread_spin_destroy (lock);
63 }
64
65 #elif HAVE_PTHREAD_H
66
67
68
69
70
71 # if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) \
72 || __clang_major > 3 || (__clang_major__ == 3 && __clang_minor__ >= 1)) \
73 && !defined __ibmxl__
74
75
76
77
78
79 # if 1
80
81
82 int
83 pthread_spin_init (pthread_spinlock_t *lock,
84 _GL_UNUSED int shared_across_processes)
85 {
86 __atomic_store_n ((unsigned int *) lock, 0, __ATOMIC_SEQ_CST);
87 return 0;
88 }
89
90 int
91 pthread_spin_lock (pthread_spinlock_t *lock)
92 {
93
94 unsigned int zero;
95 while (!(zero = 0,
96 __atomic_compare_exchange_n ((unsigned int *) lock, &zero, 1, false,
97 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)))
98 ;
99 return 0;
100 }
101
102 int
103 pthread_spin_trylock (pthread_spinlock_t *lock)
104 {
105 unsigned int zero;
106 if (!(zero = 0,
107 __atomic_compare_exchange_n ((unsigned int *) lock, &zero, 1, false,
108 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)))
109 return EBUSY;
110 return 0;
111 }
112
113 int
114 pthread_spin_unlock (pthread_spinlock_t *lock)
115 {
116
117 unsigned int one = 1;
118 if (!__atomic_compare_exchange_n ((unsigned int *) lock, &one, 0, false,
119 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
120 abort ();
121 return 0;
122 }
123
124 # else
125
126
127
128 int
129 pthread_spin_init (pthread_spinlock_t *lock,
130 _GL_UNUSED int shared_across_processes)
131 {
132 __atomic_clear (lock, __ATOMIC_SEQ_CST);
133 return 0;
134 }
135
136 int
137 pthread_spin_lock (pthread_spinlock_t *lock)
138 {
139 while (__atomic_test_and_set (lock, __ATOMIC_SEQ_CST))
140 ;
141 return 0;
142 }
143
144 int
145 pthread_spin_trylock (pthread_spinlock_t *lock)
146 {
147 if (__atomic_test_and_set (lock, __ATOMIC_SEQ_CST))
148 return EBUSY;
149 return 0;
150 }
151
152 int
153 pthread_spin_unlock (pthread_spinlock_t *lock)
154 {
155 __atomic_clear (lock, __ATOMIC_SEQ_CST);
156 return 0;
157 }
158
159 # endif
160
161 int
162 pthread_spin_destroy (pthread_spinlock_t *lock)
163 {
164 return 0;
165 }
166
167 # elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) \
168 || __clang_major__ >= 3) \
169 && !defined __ibmxl__
170
171
172
173
174 int
175 pthread_spin_init (pthread_spinlock_t *lock,
176 _GL_UNUSED int shared_across_processes)
177 {
178 * (volatile unsigned int *) lock = 0;
179 __sync_synchronize ();
180 return 0;
181 }
182
183 int
184 pthread_spin_lock (pthread_spinlock_t *lock)
185 {
186
187 while (__sync_val_compare_and_swap ((unsigned int *) lock, 0, 1) != 0)
188 ;
189 return 0;
190 }
191
192 int
193 pthread_spin_trylock (pthread_spinlock_t *lock)
194 {
195 if (__sync_val_compare_and_swap ((unsigned int *) lock, 0, 1) != 0)
196 return EBUSY;
197 return 0;
198 }
199
200 int
201 pthread_spin_unlock (pthread_spinlock_t *lock)
202 {
203
204 if (__sync_val_compare_and_swap ((unsigned int *) lock, 1, 0) != 1)
205 abort ();
206 return 0;
207 }
208
209 int
210 pthread_spin_destroy (pthread_spinlock_t *lock)
211 {
212 return 0;
213 }
214
215 # else
216
217
218 int
219 pthread_spin_init (pthread_spinlock_t *lock,
220 _GL_UNUSED int shared_across_processes)
221 {
222 return pthread_mutex_init (lock, NULL);
223 }
224
225 int
226 pthread_spin_lock (pthread_spinlock_t *lock)
227 {
228 return pthread_mutex_lock (lock);
229 }
230
231 int
232 pthread_spin_trylock (pthread_spinlock_t *lock)
233 {
234 return pthread_mutex_trylock (lock);
235 }
236
237 int
238 pthread_spin_unlock (pthread_spinlock_t *lock)
239 {
240 return pthread_mutex_unlock (lock);
241 }
242
243 int
244 pthread_spin_destroy (pthread_spinlock_t *lock)
245 {
246 return pthread_mutex_destroy (lock);
247 }
248
249 # endif
250
251 #else
252
253
254 int
255 pthread_spin_init (_GL_UNUSED pthread_spinlock_t *lock,
256 _GL_UNUSED int shared_across_processes)
257 {
258 return 0;
259 }
260
261 int
262 pthread_spin_lock (_GL_UNUSED pthread_spinlock_t *lock)
263 {
264 return 0;
265 }
266
267 int
268 pthread_spin_trylock (_GL_UNUSED pthread_spinlock_t *lock)
269 {
270 return 0;
271 }
272
273 int
274 pthread_spin_unlock (_GL_UNUSED pthread_spinlock_t *lock)
275 {
276 return 0;
277 }
278
279 int
280 pthread_spin_destroy (_GL_UNUSED pthread_spinlock_t *lock)
281 {
282 return 0;
283 }
284
285 #endif