cli_interlocked.hpp (1923B)
1 /* Relacy Race Detector 2 * Copyright (c) 2008-2013, Dmitry S. Vyukov 3 * All rights reserved. 4 * This software is provided AS-IS with no warranty, either express or implied. 5 * This software is distributed under a license and may not be copied, 6 * modified or distributed except as expressly authorized under the 7 * terms of the license contained in the file LICENSE in this distribution. 8 */ 9 10 #ifndef RL_CLI_INTERLOCKED_HPP 11 #define RL_CLI_INTERLOCKED_HPP 12 #ifdef _MSC_VER 13 # pragma once 14 #endif 15 16 #include "base.hpp" 17 #include "atomic.hpp" 18 19 20 namespace rl 21 { 22 23 struct Interlocked 24 { 25 template<typename T> 26 static T Add(generic_atomic<T, true>& v, T x, debug_info_param info) 27 { 28 T result = v.rmw(rmw_type_t<rmw_type_add>(), x, mo_seq_cst, info) + x; 29 return result; 30 } 31 32 template<typename T> 33 static T CompareExchange(generic_atomic<T, true>& v, T xchg, T cmp, debug_info_param info) 34 { 35 v.compare_exchange(bool_t<false>(), cmp, xchg, mo_seq_cst, mo_seq_cst, info); 36 return cmp; 37 } 38 39 template<typename T> 40 static T Increment(generic_atomic<T, true>& v, debug_info_param info) 41 { 42 return Add(v, (T)1, info); 43 } 44 45 template<typename T> 46 static T Decrement(generic_atomic<T, true>& v, debug_info_param info) 47 { 48 return Add(v, (T)-1, info); 49 } 50 51 template<typename T> 52 static T Exchange(generic_atomic<T, true>& v, T x, debug_info_param info) 53 { 54 T result = v.rmw(rmw_type_t<rmw_type_swap>(), x, mo_seq_cst, info); 55 return result; 56 } 57 58 template<typename T> 59 static T Read(generic_atomic<T, true> const& v, debug_info_param info) 60 { 61 return v.load(mo_acquire, info); 62 } 63 }; 64 65 } 66 67 #endif