rmw.hpp (1888B)
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_RMW_HPP 11 #define RL_RMW_HPP 12 #ifdef _MSC_VER 13 # pragma once 14 #endif 15 16 #include "base.hpp" 17 18 19 namespace rl 20 { 21 22 23 enum rmw_type_e 24 { 25 rmw_type_swap, 26 rmw_type_add, 27 rmw_type_sub, 28 rmw_type_and, 29 rmw_type_or, 30 rmw_type_xor, 31 }; 32 33 34 35 36 inline char const* format(rmw_type_e t) 37 { 38 switch (t) 39 { 40 case rmw_type_swap: return "exchange"; 41 case rmw_type_add: return "fetch_add"; 42 case rmw_type_sub: return "fetch_sub"; 43 case rmw_type_and: return "fetch_and"; 44 case rmw_type_or: return "fetch_or"; 45 case rmw_type_xor: return "fetch_xor"; 46 } 47 RL_VERIFY(!"invalid rmw type"); 48 throw std::logic_error("invalid rmw type"); 49 } 50 51 52 53 54 template<rmw_type_e type> struct rmw_type_t {}; 55 56 57 58 59 template<typename T, typename Y> 60 T perform_rmw(rmw_type_t<rmw_type_swap>, T v, Y op) 61 { 62 (void)v; 63 return op; 64 } 65 66 template<typename T, typename Y> 67 T perform_rmw(rmw_type_t<rmw_type_add>, T v, Y op) 68 { 69 return v + op; 70 } 71 72 template<typename T, typename Y> 73 T perform_rmw(rmw_type_t<rmw_type_sub>, T v, Y op) 74 { 75 return v - op; 76 } 77 78 template<typename T, typename Y> 79 T perform_rmw(rmw_type_t<rmw_type_and>, T v, Y op) 80 { 81 return v & op; 82 } 83 84 template<typename T, typename Y> 85 T perform_rmw(rmw_type_t<rmw_type_or>, T v, Y op) 86 { 87 return v | op; 88 } 89 90 template<typename T, typename Y> 91 T perform_rmw(rmw_type_t<rmw_type_xor>, T v, Y op) 92 { 93 return v ^ op; 94 } 95 96 97 98 } 99 100 101 #endif