1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // atomic.hfa -- simpler macros to access atomic instructions |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Thu May 25 15:22:46 2023 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Fri Jun 9 13:36:47 2023 |
---|
13 | // Update Count : 46 |
---|
14 | // |
---|
15 | |
---|
16 | #define LOAD( val ) (LOADM( val, __ATOMIC_SEQ_CST)) |
---|
17 | #define LOADM( val, memorder ) (__atomic_load_n( &(val), memorder)) |
---|
18 | |
---|
19 | #define STORE( val, assn ) (STOREM( val, assn, __ATOMIC_SEQ_CST)) |
---|
20 | #define STOREM( val, assn, memorder ) (__atomic_store_n( &(val), assn, memorder)) |
---|
21 | |
---|
22 | #define TAS( lock ) (TASM( lock, __ATOMIC_ACQUIRE)) |
---|
23 | #define TASM( lock, memorder ) (__atomic_test_and_set( &(lock), memorder)) |
---|
24 | |
---|
25 | #define TASCLR( lock ) (TASCLRM( lock, __ATOMIC_RELEASE)) |
---|
26 | #define TASCLRM( lock, memorder ) (__atomic_clear( &(lock), memorder)) |
---|
27 | |
---|
28 | #define FAS( assn, replace ) (FASM(assn, replace, __ATOMIC_SEQ_CST)) |
---|
29 | #define FASM( assn, replace, memorder ) (__atomic_exchange_n( &(assn), (replace), memorder)) |
---|
30 | |
---|
31 | #define FAI( assn, Inc ) (__atomic_fetch_add( &(assn), (Inc), __ATOMIC_SEQ_CST)) |
---|
32 | #define FAIM( assn, Inc, memorder ) (__atomic_fetch_add( &(assn), (Inc), memorder)) |
---|
33 | |
---|
34 | // Use __sync because __atomic with 128-bit CAA can result in calls to pthread_mutex_lock. |
---|
35 | |
---|
36 | // #define CAS( assn, comp, replace ) (CASM( assn, comp, replace, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) |
---|
37 | // #define CASM( assn, comp, replace, memorder... ) ({ \ |
---|
38 | // typeof(comp) __temp = (comp); \ |
---|
39 | // __atomic_compare_exchange_n( &(assn), &(__temp), (replace), false, memorder ); \ |
---|
40 | // }) |
---|
41 | #define CAS( assn, comp, replace ) (__sync_bool_compare_and_swap( &assn, comp, replace)) |
---|
42 | #define CASM( assn, comp, replace, memorder... ) _Static_assert( false, "memory order unsupported for CAS macro" ); |
---|
43 | |
---|
44 | // #define CASV( assn, comp, replace ) (__atomic_compare_exchange_n( &(assn), &(comp), (replace), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST )) |
---|
45 | // #define CASVM( assn, comp, replace, memorder... ) (__atomic_compare_exchange_n( &(assn), &(comp), (replace), false, memorder, memorder )) |
---|
46 | #define CASV( assn, comp, replace ) ({ \ |
---|
47 | typeof(comp) temp = comp; \ |
---|
48 | typeof(comp) old = __sync_val_compare_and_swap( &(assn), (comp), (replace) ); \ |
---|
49 | old == temp ? true : (comp = old, false); \ |
---|
50 | }) |
---|
51 | #define CASVM( assn, comp, replace, memorder... ) _Static_assert( false, "memory order unsupported for CASV macro" ); |
---|