Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Cisco and/or its affiliates. |
| 3 | * Copyright (c) 2018 Arm Limited. and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef included_clib_atomics_h |
| 18 | #define included_clib_atomics_h |
| 19 | |
| 20 | /* Legacy __sync builtins */ |
| 21 | |
| 22 | /* Full Barrier */ |
| 23 | #define clib_atomic_fetch_add(a, b) __sync_fetch_and_add(a, b) |
| 24 | #define clib_atomic_fetch_sub(a, b) __sync_fetch_and_sub(a, b) |
| 25 | #define clib_atomic_fetch_and(a, b) __sync_fetch_and_and(a, b) |
| 26 | #define clib_atomic_fetch_xor(a, b) __sync_fetch_and_xor(a, b) |
| 27 | #define clib_atomic_fetch_or(a, b) __sync_fetch_and_or(a, b) |
| 28 | #define clib_atomic_fetch_nand(a, b) __sync_fetch_nand(a, b) |
| 29 | |
| 30 | #define clib_atomic_add_fetch(a, b) __sync_add_and_fetch(a, b) |
| 31 | #define clib_atomic_sub_fetch(a, b) __sync_sub_and_fetch(a, b) |
| 32 | #define clib_atomic_and_fetch(a, b) __sync_and_and_fetch(a, b) |
| 33 | #define clib_atomic_xor_fetch(a, b) __sync_xor_and_fetch(a, b) |
| 34 | #define clib_atomic_or_fetch(a, b) __sync_or_and_fetch(a, b) |
| 35 | #define clib_atomic_nand_fetch(a, b) __sync_nand_and_fetch(a, b) |
| 36 | |
| 37 | #define clib_atomic_cmp_and_swap(addr,old,new) __sync_val_compare_and_swap(addr, old, new) |
| 38 | #define clib_atomic_bool_cmp_and_swap(addr,old,new) __sync_bool_compare_and_swap(addr, old, new) |
| 39 | |
jaszha03 | fb1ccc7 | 2019-06-26 17:06:59 -0500 | [diff] [blame] | 40 | #define clib_atomic_cmp_and_swap_acq_relax_n(addr,exp,new,weak) __atomic_compare_exchange_n ((addr), (exp), (new), (weak), __ATOMIC_ACQUIRE, __ATOMIC_RELAXED) |
Florin Coras | 213b1bb | 2020-12-07 14:33:58 -0800 | [diff] [blame] | 41 | #define clib_atomic_cmp_and_swap_acq_relax(addr, exp, new, weak) \ |
| 42 | __atomic_compare_exchange ((addr), (exp), (new), (weak), __ATOMIC_ACQUIRE, \ |
| 43 | __ATOMIC_RELAXED) |
jaszha03 | fb1ccc7 | 2019-06-26 17:06:59 -0500 | [diff] [blame] | 44 | |
Sirshak Das | 3bb6540 | 2019-05-28 08:44:46 -0500 | [diff] [blame] | 45 | #define clib_atomic_test_and_set(a) __atomic_exchange_n(a, 1, __ATOMIC_ACQUIRE) |
Sirshak Das | 21aed5c | 2019-05-28 08:46:27 -0500 | [diff] [blame] | 46 | #define clib_atomic_release(a) __atomic_store_n(a, 0, __ATOMIC_RELEASE) |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 47 | |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 48 | #define clib_atomic_fence_rel() __atomic_thread_fence(__ATOMIC_RELEASE); |
| 49 | |
jaszha03 | fb1ccc7 | 2019-06-26 17:06:59 -0500 | [diff] [blame] | 50 | #define clib_atomic_load_relax_n(a) __atomic_load_n((a), __ATOMIC_RELAXED) |
Sirshak Das | 19515ac | 2018-11-07 18:46:42 -0600 | [diff] [blame] | 51 | #define clib_atomic_load_acq_n(a) __atomic_load_n((a), __ATOMIC_ACQUIRE) |
Sirshak Das | 5b718d5 | 2018-10-12 09:38:27 -0500 | [diff] [blame] | 52 | #define clib_atomic_store_rel_n(a, b) __atomic_store_n ((a), (b), __ATOMIC_RELEASE) |
Mohsin Kazmi | d8b7dec | 2020-12-10 09:49:21 +0000 | [diff] [blame] | 53 | #define clib_atomic_store_seq_cst(a, b) \ |
| 54 | __atomic_store_n ((a), (b), __ATOMIC_SEQ_CST) |
| 55 | #define clib_atomic_load_seq_cst(a) __atomic_load_n ((a), __ATOMIC_SEQ_CST) |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 56 | |
Sirshak Das | 5b718d5 | 2018-10-12 09:38:27 -0500 | [diff] [blame] | 57 | #define clib_atomic_swap_acq_n(a, b) __atomic_exchange_n ((a), (b), __ATOMIC_ACQUIRE) |
Florin Coras | 881d13e | 2019-03-21 21:17:43 -0700 | [diff] [blame] | 58 | #define clib_atomic_swap_rel_n(a, b) __atomic_exchange_n ((a), (b), __ATOMIC_RELEASE) |
Sirshak Das | 5b718d5 | 2018-10-12 09:38:27 -0500 | [diff] [blame] | 59 | |
Sirshak Das | 19515ac | 2018-11-07 18:46:42 -0600 | [diff] [blame] | 60 | #define clib_atomic_fetch_add_rel(a, b) __atomic_fetch_add((a), (b), __ATOMIC_RELEASE) |
| 61 | #define clib_atomic_fetch_sub_rel(a, b) __atomic_fetch_sub((a), (b), __ATOMIC_RELEASE) |
| 62 | |
Florin Coras | d01d2ce | 2020-11-17 18:42:38 -0800 | [diff] [blame] | 63 | #define clib_atomic_fetch_add_relax(a, b) __atomic_fetch_add((a), (b), __ATOMIC_RELAXED) |
| 64 | #define clib_atomic_fetch_sub_relax(a, b) __atomic_fetch_sub((a), (b), __ATOMIC_RELAXED) |
| 65 | |
Sirshak Das | 2f6d7bb | 2018-10-03 22:53:51 +0000 | [diff] [blame] | 66 | #endif /* included_clib_atomics_h */ |