foreach.hpp (2748B)
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_FOREACH_HPP 11 #define RL_FOREACH_HPP 12 #ifdef _MSC_VER 13 # pragma once 14 #endif 15 16 #include "base.hpp" 17 18 namespace rl 19 { 20 21 22 template<typename T, thread_id_t i, thread_id_t index> 23 struct foreach_thread_impl 24 { 25 template<typename F> 26 RL_INLINE static void exec( 27 T* v1, 28 F func) 29 { 30 (*func)(v1[i]); 31 foreach_thread_impl<T, i + 1, index - 1>::exec(v1, func); 32 } 33 34 RL_INLINE static void exec( 35 T* v1, T* v2, 36 void (*func)(T& e1, T& e2)) 37 { 38 (*func)(v1[i], v2[i]); 39 foreach_thread_impl<T, i + 1, index - 1>::exec(v1, v2, func); 40 } 41 42 RL_INLINE static void exec( 43 T* v1, T* v2, T* v3, 44 void (*func)(T& e1, T& e2, T& e3)) 45 { 46 (*func)(v1[i], v2[i], v3[i]); 47 foreach_thread_impl<T, i + 1, index - 1>::exec(v1, v2, v3, func); 48 } 49 }; 50 51 template<typename T, thread_id_t i> 52 struct foreach_thread_impl<T, i, 0> 53 { 54 template<typename F> 55 RL_INLINE static void exec( 56 T*, 57 F) 58 { 59 } 60 61 RL_INLINE static void exec( 62 T*, T*, 63 void (*)(T&, T&)) 64 { 65 } 66 67 RL_INLINE static void exec( 68 T*, T*, T*, 69 void (*)(T&, T&, T&)) 70 { 71 } 72 }; 73 74 template<thread_id_t count, typename T, typename F> 75 RL_INLINE void foreach( 76 T* v1, 77 F func) 78 { 79 foreach_thread_impl<T, 0, count>::exec(v1, func); 80 } 81 82 template<thread_id_t count, typename T> 83 RL_INLINE void foreach( 84 T* v1, T* v2, 85 void (*func)(T& e1, T& e2)) 86 { 87 foreach_thread_impl<T, 0, count>::exec(v1, v2, func); 88 } 89 90 template<thread_id_t count, typename T> 91 RL_INLINE void foreach( 92 T* v1, T* v2, T* v3, 93 void (*func)(T& e1, T& e2, T& e3)) 94 { 95 foreach_thread_impl<T, 0, count>::exec(v1, v2, v3, func); 96 } 97 98 RL_INLINE void assign_zero(timestamp_t& elem) 99 { 100 elem = 0; 101 } 102 103 RL_INLINE void assign_zero_u(unsigned& elem) 104 { 105 elem = 0; 106 } 107 108 template<timestamp_t value> 109 RL_INLINE void assign(timestamp_t& elem) 110 { 111 elem = value; 112 } 113 114 RL_INLINE void assign(timestamp_t& elem1, timestamp_t& elem2) 115 { 116 elem1 = elem2; 117 } 118 119 RL_INLINE void assign_max(timestamp_t& elem1, timestamp_t& elem2) 120 { 121 if (elem2 > elem1) 122 elem1 = elem2; 123 } 124 125 RL_INLINE void plus_one(timestamp_t& elem) 126 { 127 elem += 1; 128 } 129 130 } 131 132 133 #endif