PAPI 7.1.0.0
Loading...
Searching...
No Matches
generic_pthread.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003 by Hewlett-Packard Company. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23/* The following is useful primarily for debugging and documentation. */
24/* We define various atomic operations by acquiring a global pthread */
25/* lock. The resulting implementation will perform poorly, but should */
26/* be correct unless it is used from signal handlers. */
27/* We assume that all pthread operations act like full memory barriers. */
28/* (We believe that is the intent of the specification.) */
29
30#include <pthread.h>
31
33 /* This is not necessarily compatible with the native */
34 /* implementation. But those can't be safely mixed anyway. */
35
36#ifdef __cplusplus
37 extern "C" {
38#endif
39
40/* We define only the full barrier variants, and count on the */
41/* generalization section below to fill in the rest. */
43
44#ifdef __cplusplus
45 } /* extern "C" */
46#endif
47
48AO_INLINE void
50{
51 pthread_mutex_lock(&AO_pt_lock);
52 pthread_mutex_unlock(&AO_pt_lock);
53}
54#define AO_HAVE_nop_full
55
57AO_load_full(const volatile AO_t *addr)
58{
60 pthread_mutex_lock(&AO_pt_lock);
61 result = *addr;
62 pthread_mutex_unlock(&AO_pt_lock);
63 return result;
64}
65#define AO_HAVE_load_full
66
67AO_INLINE void
68AO_store_full(volatile AO_t *addr, AO_t val)
69{
70 pthread_mutex_lock(&AO_pt_lock);
71 *addr = val;
72 pthread_mutex_unlock(&AO_pt_lock);
73}
74#define AO_HAVE_store_full
75
76AO_INLINE unsigned char
77AO_char_load_full(const volatile unsigned char *addr)
78{
79 unsigned char result;
80 pthread_mutex_lock(&AO_pt_lock);
81 result = *addr;
82 pthread_mutex_unlock(&AO_pt_lock);
83 return result;
84}
85#define AO_HAVE_char_load_full
86
87AO_INLINE void
88AO_char_store_full(volatile unsigned char *addr, unsigned char val)
89{
90 pthread_mutex_lock(&AO_pt_lock);
91 *addr = val;
92 pthread_mutex_unlock(&AO_pt_lock);
93}
94#define AO_HAVE_char_store_full
95
96AO_INLINE unsigned short
97AO_short_load_full(const volatile unsigned short *addr)
98{
99 unsigned short result;
100 pthread_mutex_lock(&AO_pt_lock);
101 result = *addr;
102 pthread_mutex_unlock(&AO_pt_lock);
103 return result;
104}
105#define AO_HAVE_short_load_full
106
107AO_INLINE void
108AO_short_store_full(volatile unsigned short *addr, unsigned short val)
109{
110 pthread_mutex_lock(&AO_pt_lock);
111 *addr = val;
112 pthread_mutex_unlock(&AO_pt_lock);
113}
114#define AO_HAVE_short_store_full
115
116AO_INLINE unsigned int
117AO_int_load_full(const volatile unsigned int *addr)
118{
119 unsigned int result;
120 pthread_mutex_lock(&AO_pt_lock);
121 result = *addr;
122 pthread_mutex_unlock(&AO_pt_lock);
123 return result;
124}
125#define AO_HAVE_int_load_full
126
127AO_INLINE void
128AO_int_store_full(volatile unsigned int *addr, unsigned int val)
129{
130 pthread_mutex_lock(&AO_pt_lock);
131 *addr = val;
132 pthread_mutex_unlock(&AO_pt_lock);
133}
134#define AO_HAVE_int_store_full
135
138{
140 pthread_mutex_lock(&AO_pt_lock);
141 result = (AO_TS_VAL_t)(*addr);
142 *addr = AO_TS_SET;
143 pthread_mutex_unlock(&AO_pt_lock);
144 assert(result == AO_TS_SET || result == AO_TS_CLEAR);
145 return result;
146}
147#define AO_HAVE_test_and_set_full
148
151{
152 AO_t old_val;
153
154 pthread_mutex_lock(&AO_pt_lock);
155 old_val = *p;
156 *p = old_val + incr;
157 pthread_mutex_unlock(&AO_pt_lock);
158 return old_val;
159}
160#define AO_HAVE_fetch_and_add_full
161
162AO_INLINE unsigned char
163AO_char_fetch_and_add_full(volatile unsigned char *p, unsigned char incr)
164{
165 unsigned char old_val;
166
167 pthread_mutex_lock(&AO_pt_lock);
168 old_val = *p;
169 *p = old_val + incr;
170 pthread_mutex_unlock(&AO_pt_lock);
171 return old_val;
172}
173#define AO_HAVE_char_fetch_and_add_full
174
175AO_INLINE unsigned short
176AO_short_fetch_and_add_full(volatile unsigned short *p, unsigned short incr)
177{
178 unsigned short old_val;
179
180 pthread_mutex_lock(&AO_pt_lock);
181 old_val = *p;
182 *p = old_val + incr;
183 pthread_mutex_unlock(&AO_pt_lock);
184 return old_val;
185}
186#define AO_HAVE_short_fetch_and_add_full
187
188AO_INLINE unsigned int
189AO_int_fetch_and_add_full(volatile unsigned int *p, unsigned int incr)
190{
191 unsigned int old_val;
192
193 pthread_mutex_lock(&AO_pt_lock);
194 old_val = *p;
195 *p = old_val + incr;
196 pthread_mutex_unlock(&AO_pt_lock);
197 return old_val;
198}
199#define AO_HAVE_int_fetch_and_add_full
200
201AO_INLINE void
202AO_and_full(volatile AO_t *p, AO_t value)
203{
204 pthread_mutex_lock(&AO_pt_lock);
205 *p &= value;
206 pthread_mutex_unlock(&AO_pt_lock);
207}
208#define AO_HAVE_and_full
209
210AO_INLINE void
211AO_or_full(volatile AO_t *p, AO_t value)
212{
213 pthread_mutex_lock(&AO_pt_lock);
214 *p |= value;
215 pthread_mutex_unlock(&AO_pt_lock);
216}
217#define AO_HAVE_or_full
218
219AO_INLINE void
220AO_xor_full(volatile AO_t *p, AO_t value)
221{
222 pthread_mutex_lock(&AO_pt_lock);
223 *p ^= value;
224 pthread_mutex_unlock(&AO_pt_lock);
225}
226#define AO_HAVE_xor_full
227
228AO_INLINE void
229AO_char_and_full(volatile unsigned char *p, unsigned char value)
230{
231 pthread_mutex_lock(&AO_pt_lock);
232 *p &= value;
233 pthread_mutex_unlock(&AO_pt_lock);
234}
235#define AO_HAVE_char_and_full
236
237AO_INLINE void
238AO_char_or_full(volatile unsigned char *p, unsigned char value)
239{
240 pthread_mutex_lock(&AO_pt_lock);
241 *p |= value;
242 pthread_mutex_unlock(&AO_pt_lock);
243}
244#define AO_HAVE_char_or_full
245
246AO_INLINE void
247AO_char_xor_full(volatile unsigned char *p, unsigned char value)
248{
249 pthread_mutex_lock(&AO_pt_lock);
250 *p ^= value;
251 pthread_mutex_unlock(&AO_pt_lock);
252}
253#define AO_HAVE_char_xor_full
254
255AO_INLINE void
256AO_short_and_full(volatile unsigned short *p, unsigned short value)
257{
258 pthread_mutex_lock(&AO_pt_lock);
259 *p &= value;
260 pthread_mutex_unlock(&AO_pt_lock);
261}
262#define AO_HAVE_short_and_full
263
264AO_INLINE void
265AO_short_or_full(volatile unsigned short *p, unsigned short value)
266{
267 pthread_mutex_lock(&AO_pt_lock);
268 *p |= value;
269 pthread_mutex_unlock(&AO_pt_lock);
270}
271#define AO_HAVE_short_or_full
272
273AO_INLINE void
274AO_short_xor_full(volatile unsigned short *p, unsigned short value)
275{
276 pthread_mutex_lock(&AO_pt_lock);
277 *p ^= value;
278 pthread_mutex_unlock(&AO_pt_lock);
279}
280#define AO_HAVE_short_xor_full
281
282AO_INLINE void
283AO_int_and_full(volatile unsigned *p, unsigned value)
284{
285 pthread_mutex_lock(&AO_pt_lock);
286 *p &= value;
287 pthread_mutex_unlock(&AO_pt_lock);
288}
289#define AO_HAVE_int_and_full
290
291AO_INLINE void
292AO_int_or_full(volatile unsigned *p, unsigned value)
293{
294 pthread_mutex_lock(&AO_pt_lock);
295 *p |= value;
296 pthread_mutex_unlock(&AO_pt_lock);
297}
298#define AO_HAVE_int_or_full
299
300AO_INLINE void
301AO_int_xor_full(volatile unsigned *p, unsigned value)
302{
303 pthread_mutex_lock(&AO_pt_lock);
304 *p ^= value;
305 pthread_mutex_unlock(&AO_pt_lock);
306}
307#define AO_HAVE_int_xor_full
308
311 AO_t new_val)
312{
313 AO_t fetched_val;
314
315 pthread_mutex_lock(&AO_pt_lock);
316 fetched_val = *addr;
317 if (fetched_val == old_val)
318 *addr = new_val;
319 pthread_mutex_unlock(&AO_pt_lock);
320 return fetched_val;
321}
322#define AO_HAVE_fetch_compare_and_swap_full
323
324AO_INLINE unsigned char
325AO_char_fetch_compare_and_swap_full(volatile unsigned char *addr,
326 unsigned char old_val,
327 unsigned char new_val)
328{
329 unsigned char fetched_val;
330
331 pthread_mutex_lock(&AO_pt_lock);
332 fetched_val = *addr;
333 if (fetched_val == old_val)
334 *addr = new_val;
335 pthread_mutex_unlock(&AO_pt_lock);
336 return fetched_val;
337}
338#define AO_HAVE_char_fetch_compare_and_swap_full
339
340AO_INLINE unsigned short
341AO_short_fetch_compare_and_swap_full(volatile unsigned short *addr,
342 unsigned short old_val,
343 unsigned short new_val)
344{
345 unsigned short fetched_val;
346
347 pthread_mutex_lock(&AO_pt_lock);
348 fetched_val = *addr;
349 if (fetched_val == old_val)
350 *addr = new_val;
351 pthread_mutex_unlock(&AO_pt_lock);
352 return fetched_val;
353}
354#define AO_HAVE_short_fetch_compare_and_swap_full
355
356AO_INLINE unsigned
357AO_int_fetch_compare_and_swap_full(volatile unsigned *addr, unsigned old_val,
358 unsigned new_val)
359{
360 unsigned fetched_val;
361
362 pthread_mutex_lock(&AO_pt_lock);
363 fetched_val = *addr;
364 if (fetched_val == old_val)
365 *addr = new_val;
366 pthread_mutex_unlock(&AO_pt_lock);
367 return fetched_val;
368}
369#define AO_HAVE_int_fetch_compare_and_swap_full
370
371/* Unlike real architectures, we define both double-width CAS variants. */
372
373typedef struct {
377#define AO_HAVE_double_t
378
379#define AO_DOUBLE_T_INITIALIZER { (AO_t)0, (AO_t)0 }
380
382AO_double_load_full(const volatile AO_double_t *addr)
383{
385
386 pthread_mutex_lock(&AO_pt_lock);
387 result.AO_val1 = addr->AO_val1;
388 result.AO_val2 = addr->AO_val2;
389 pthread_mutex_unlock(&AO_pt_lock);
390 return result;
391}
392#define AO_HAVE_double_load_full
393
394AO_INLINE void
396{
397 pthread_mutex_lock(&AO_pt_lock);
398 addr->AO_val1 = value.AO_val1;
399 addr->AO_val2 = value.AO_val2;
400 pthread_mutex_unlock(&AO_pt_lock);
401}
402#define AO_HAVE_double_store_full
403
404AO_INLINE int
406 AO_t old1, AO_t old2,
407 AO_t new1, AO_t new2)
408{
409 pthread_mutex_lock(&AO_pt_lock);
410 if (addr -> AO_val1 == old1 && addr -> AO_val2 == old2)
411 {
412 addr -> AO_val1 = new1;
413 addr -> AO_val2 = new2;
414 pthread_mutex_unlock(&AO_pt_lock);
415 return 1;
416 }
417 else
418 pthread_mutex_unlock(&AO_pt_lock);
419 return 0;
420}
421#define AO_HAVE_compare_double_and_swap_double_full
422
423AO_INLINE int
425 AO_t old1, AO_t new1, AO_t new2)
426{
427 pthread_mutex_lock(&AO_pt_lock);
428 if (addr -> AO_val1 == old1)
429 {
430 addr -> AO_val1 = new1;
431 addr -> AO_val2 = new2;
432 pthread_mutex_unlock(&AO_pt_lock);
433 return 1;
434 }
435 else
436 pthread_mutex_unlock(&AO_pt_lock);
437 return 0;
438}
439#define AO_HAVE_compare_and_swap_double_full
440
441/* We can't use hardware loads and stores, since they don't */
442/* interact correctly with atomic updates. */
volatile int result
#define AO_t
Definition: atomic_ops.h:156
#define AO_INLINE
Definition: atomic_ops.h:186
#define AO_API
Definition: atomic_ops.h:259
#define AO_store_full(addr, val)
Definition: emul_cas.h:82
#define AO_compare_double_and_swap_double_full(addr, old1, old2, newval1, newval2)
Definition: emul_cas.h:67
#define AO_fetch_compare_and_swap_full(addr, old, newval)
Definition: emul_cas.h:61
#define AO_TS_CLEAR
Definition: gcc/hppa.h:45
#define AO_TS_t
Definition: gcc/hppa.h:39
#define AO_TS_VAL_t
Definition: gcc/hppa.h:44
#define AO_TS_SET
Definition: gcc/hppa.h:46
AO_INLINE unsigned char AO_char_fetch_and_add_full(volatile unsigned char *p, unsigned char incr)
AO_INLINE void AO_double_store_full(volatile AO_double_t *addr, AO_double_t value)
AO_INLINE unsigned short AO_short_load_full(const volatile unsigned short *addr)
AO_INLINE void AO_int_store_full(volatile unsigned int *addr, unsigned int val)
AO_INLINE unsigned int AO_int_fetch_and_add_full(volatile unsigned int *p, unsigned int incr)
AO_INLINE unsigned int AO_int_load_full(const volatile unsigned int *addr)
AO_INLINE void AO_xor_full(volatile AO_t *p, AO_t value)
AO_INLINE void AO_short_and_full(volatile unsigned short *p, unsigned short value)
AO_INLINE unsigned short AO_short_fetch_compare_and_swap_full(volatile unsigned short *addr, unsigned short old_val, unsigned short new_val)
AO_INLINE void AO_or_full(volatile AO_t *p, AO_t value)
AO_INLINE void AO_char_and_full(volatile unsigned char *p, unsigned char value)
AO_INLINE void AO_short_store_full(volatile unsigned short *addr, unsigned short val)
AO_INLINE void AO_int_and_full(volatile unsigned *p, unsigned value)
AO_INLINE AO_t AO_fetch_and_add_full(volatile AO_t *p, AO_t incr)
AO_API pthread_mutex_t AO_pt_lock
AO_INLINE void AO_int_xor_full(volatile unsigned *p, unsigned value)
AO_INLINE void AO_char_or_full(volatile unsigned char *p, unsigned char value)
AO_INLINE unsigned short AO_short_fetch_and_add_full(volatile unsigned short *p, unsigned short incr)
AO_INLINE unsigned char AO_char_fetch_compare_and_swap_full(volatile unsigned char *addr, unsigned char old_val, unsigned char new_val)
AO_INLINE AO_TS_VAL_t AO_test_and_set_full(volatile AO_TS_t *addr)
AO_INLINE void AO_char_xor_full(volatile unsigned char *p, unsigned char value)
AO_INLINE void AO_char_store_full(volatile unsigned char *addr, unsigned char val)
AO_INLINE void AO_short_xor_full(volatile unsigned short *p, unsigned short value)
AO_INLINE AO_double_t AO_double_load_full(const volatile AO_double_t *addr)
AO_INLINE unsigned AO_int_fetch_compare_and_swap_full(volatile unsigned *addr, unsigned old_val, unsigned new_val)
AO_INLINE unsigned char AO_char_load_full(const volatile unsigned char *addr)
AO_INLINE void AO_short_or_full(volatile unsigned short *p, unsigned short value)
AO_INLINE int AO_compare_and_swap_double_full(volatile AO_double_t *addr, AO_t old1, AO_t new1, AO_t new2)
AO_INLINE void AO_int_or_full(volatile unsigned *p, unsigned value)
AO_INLINE void AO_and_full(volatile AO_t *p, AO_t value)
AO_INLINE AO_t AO_load_full(const volatile AO_t *addr)
AO_INLINE void AO_nop_full(void)
#define AO_val1
#define AO_val2