18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// An earlier version of this file appeared in the companion webpage for 48c2ecf20Sopenharmony_ci// "Frightening small children and disconcerting grown-ups: Concurrency 58c2ecf20Sopenharmony_ci// in the Linux kernel" by Alglave, Maranget, McKenney, Parri, and Stern, 68c2ecf20Sopenharmony_ci// which appeared in ASPLOS 2018. 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci// ONCE 98c2ecf20Sopenharmony_ciREAD_ONCE(X) __load{once}(X) 108c2ecf20Sopenharmony_ciWRITE_ONCE(X,V) { __store{once}(X,V); } 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci// Release Acquire and friends 138c2ecf20Sopenharmony_cismp_store_release(X,V) { __store{release}(*X,V); } 148c2ecf20Sopenharmony_cismp_load_acquire(X) __load{acquire}(*X) 158c2ecf20Sopenharmony_circu_assign_pointer(X,V) { __store{release}(X,V); } 168c2ecf20Sopenharmony_circu_dereference(X) __load{once}(X) 178c2ecf20Sopenharmony_cismp_store_mb(X,V) { __store{once}(X,V); __fence{mb}; } 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci// Fences 208c2ecf20Sopenharmony_cismp_mb() { __fence{mb}; } 218c2ecf20Sopenharmony_cismp_rmb() { __fence{rmb}; } 228c2ecf20Sopenharmony_cismp_wmb() { __fence{wmb}; } 238c2ecf20Sopenharmony_cismp_mb__before_atomic() { __fence{before-atomic}; } 248c2ecf20Sopenharmony_cismp_mb__after_atomic() { __fence{after-atomic}; } 258c2ecf20Sopenharmony_cismp_mb__after_spinlock() { __fence{after-spinlock}; } 268c2ecf20Sopenharmony_cismp_mb__after_unlock_lock() { __fence{after-unlock-lock}; } 278c2ecf20Sopenharmony_cibarrier() { __fence{barrier}; } 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci// Exchange 308c2ecf20Sopenharmony_cixchg(X,V) __xchg{mb}(X,V) 318c2ecf20Sopenharmony_cixchg_relaxed(X,V) __xchg{once}(X,V) 328c2ecf20Sopenharmony_cixchg_release(X,V) __xchg{release}(X,V) 338c2ecf20Sopenharmony_cixchg_acquire(X,V) __xchg{acquire}(X,V) 348c2ecf20Sopenharmony_cicmpxchg(X,V,W) __cmpxchg{mb}(X,V,W) 358c2ecf20Sopenharmony_cicmpxchg_relaxed(X,V,W) __cmpxchg{once}(X,V,W) 368c2ecf20Sopenharmony_cicmpxchg_acquire(X,V,W) __cmpxchg{acquire}(X,V,W) 378c2ecf20Sopenharmony_cicmpxchg_release(X,V,W) __cmpxchg{release}(X,V,W) 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci// Spinlocks 408c2ecf20Sopenharmony_cispin_lock(X) { __lock(X); } 418c2ecf20Sopenharmony_cispin_unlock(X) { __unlock(X); } 428c2ecf20Sopenharmony_cispin_trylock(X) __trylock(X) 438c2ecf20Sopenharmony_cispin_is_locked(X) __islocked(X) 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci// RCU 468c2ecf20Sopenharmony_circu_read_lock() { __fence{rcu-lock}; } 478c2ecf20Sopenharmony_circu_read_unlock() { __fence{rcu-unlock}; } 488c2ecf20Sopenharmony_cisynchronize_rcu() { __fence{sync-rcu}; } 498c2ecf20Sopenharmony_cisynchronize_rcu_expedited() { __fence{sync-rcu}; } 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci// SRCU 528c2ecf20Sopenharmony_cisrcu_read_lock(X) __srcu{srcu-lock}(X) 538c2ecf20Sopenharmony_cisrcu_read_unlock(X,Y) { __srcu{srcu-unlock}(X,Y); } 548c2ecf20Sopenharmony_cisynchronize_srcu(X) { __srcu{sync-srcu}(X); } 558c2ecf20Sopenharmony_cisynchronize_srcu_expedited(X) { __srcu{sync-srcu}(X); } 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci// Atomic 588c2ecf20Sopenharmony_ciatomic_read(X) READ_ONCE(*X) 598c2ecf20Sopenharmony_ciatomic_set(X,V) { WRITE_ONCE(*X,V); } 608c2ecf20Sopenharmony_ciatomic_read_acquire(X) smp_load_acquire(X) 618c2ecf20Sopenharmony_ciatomic_set_release(X,V) { smp_store_release(X,V); } 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ciatomic_add(V,X) { __atomic_op(X,+,V); } 648c2ecf20Sopenharmony_ciatomic_sub(V,X) { __atomic_op(X,-,V); } 658c2ecf20Sopenharmony_ciatomic_inc(X) { __atomic_op(X,+,1); } 668c2ecf20Sopenharmony_ciatomic_dec(X) { __atomic_op(X,-,1); } 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ciatomic_add_return(V,X) __atomic_op_return{mb}(X,+,V) 698c2ecf20Sopenharmony_ciatomic_add_return_relaxed(V,X) __atomic_op_return{once}(X,+,V) 708c2ecf20Sopenharmony_ciatomic_add_return_acquire(V,X) __atomic_op_return{acquire}(X,+,V) 718c2ecf20Sopenharmony_ciatomic_add_return_release(V,X) __atomic_op_return{release}(X,+,V) 728c2ecf20Sopenharmony_ciatomic_fetch_add(V,X) __atomic_fetch_op{mb}(X,+,V) 738c2ecf20Sopenharmony_ciatomic_fetch_add_relaxed(V,X) __atomic_fetch_op{once}(X,+,V) 748c2ecf20Sopenharmony_ciatomic_fetch_add_acquire(V,X) __atomic_fetch_op{acquire}(X,+,V) 758c2ecf20Sopenharmony_ciatomic_fetch_add_release(V,X) __atomic_fetch_op{release}(X,+,V) 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ciatomic_inc_return(X) __atomic_op_return{mb}(X,+,1) 788c2ecf20Sopenharmony_ciatomic_inc_return_relaxed(X) __atomic_op_return{once}(X,+,1) 798c2ecf20Sopenharmony_ciatomic_inc_return_acquire(X) __atomic_op_return{acquire}(X,+,1) 808c2ecf20Sopenharmony_ciatomic_inc_return_release(X) __atomic_op_return{release}(X,+,1) 818c2ecf20Sopenharmony_ciatomic_fetch_inc(X) __atomic_fetch_op{mb}(X,+,1) 828c2ecf20Sopenharmony_ciatomic_fetch_inc_relaxed(X) __atomic_fetch_op{once}(X,+,1) 838c2ecf20Sopenharmony_ciatomic_fetch_inc_acquire(X) __atomic_fetch_op{acquire}(X,+,1) 848c2ecf20Sopenharmony_ciatomic_fetch_inc_release(X) __atomic_fetch_op{release}(X,+,1) 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ciatomic_sub_return(V,X) __atomic_op_return{mb}(X,-,V) 878c2ecf20Sopenharmony_ciatomic_sub_return_relaxed(V,X) __atomic_op_return{once}(X,-,V) 888c2ecf20Sopenharmony_ciatomic_sub_return_acquire(V,X) __atomic_op_return{acquire}(X,-,V) 898c2ecf20Sopenharmony_ciatomic_sub_return_release(V,X) __atomic_op_return{release}(X,-,V) 908c2ecf20Sopenharmony_ciatomic_fetch_sub(V,X) __atomic_fetch_op{mb}(X,-,V) 918c2ecf20Sopenharmony_ciatomic_fetch_sub_relaxed(V,X) __atomic_fetch_op{once}(X,-,V) 928c2ecf20Sopenharmony_ciatomic_fetch_sub_acquire(V,X) __atomic_fetch_op{acquire}(X,-,V) 938c2ecf20Sopenharmony_ciatomic_fetch_sub_release(V,X) __atomic_fetch_op{release}(X,-,V) 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ciatomic_dec_return(X) __atomic_op_return{mb}(X,-,1) 968c2ecf20Sopenharmony_ciatomic_dec_return_relaxed(X) __atomic_op_return{once}(X,-,1) 978c2ecf20Sopenharmony_ciatomic_dec_return_acquire(X) __atomic_op_return{acquire}(X,-,1) 988c2ecf20Sopenharmony_ciatomic_dec_return_release(X) __atomic_op_return{release}(X,-,1) 998c2ecf20Sopenharmony_ciatomic_fetch_dec(X) __atomic_fetch_op{mb}(X,-,1) 1008c2ecf20Sopenharmony_ciatomic_fetch_dec_relaxed(X) __atomic_fetch_op{once}(X,-,1) 1018c2ecf20Sopenharmony_ciatomic_fetch_dec_acquire(X) __atomic_fetch_op{acquire}(X,-,1) 1028c2ecf20Sopenharmony_ciatomic_fetch_dec_release(X) __atomic_fetch_op{release}(X,-,1) 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ciatomic_xchg(X,V) __xchg{mb}(X,V) 1058c2ecf20Sopenharmony_ciatomic_xchg_relaxed(X,V) __xchg{once}(X,V) 1068c2ecf20Sopenharmony_ciatomic_xchg_release(X,V) __xchg{release}(X,V) 1078c2ecf20Sopenharmony_ciatomic_xchg_acquire(X,V) __xchg{acquire}(X,V) 1088c2ecf20Sopenharmony_ciatomic_cmpxchg(X,V,W) __cmpxchg{mb}(X,V,W) 1098c2ecf20Sopenharmony_ciatomic_cmpxchg_relaxed(X,V,W) __cmpxchg{once}(X,V,W) 1108c2ecf20Sopenharmony_ciatomic_cmpxchg_acquire(X,V,W) __cmpxchg{acquire}(X,V,W) 1118c2ecf20Sopenharmony_ciatomic_cmpxchg_release(X,V,W) __cmpxchg{release}(X,V,W) 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ciatomic_sub_and_test(V,X) __atomic_op_return{mb}(X,-,V) == 0 1148c2ecf20Sopenharmony_ciatomic_dec_and_test(X) __atomic_op_return{mb}(X,-,1) == 0 1158c2ecf20Sopenharmony_ciatomic_inc_and_test(X) __atomic_op_return{mb}(X,+,1) == 0 1168c2ecf20Sopenharmony_ciatomic_add_negative(V,X) __atomic_op_return{mb}(X,+,V) < 0 117