cli_volatile.hpp (3151B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | /* Relacy Race Detector * Copyright (c) 2008-2010, Dmitry S. Vyukov * All rights reserved. * This software is provided AS-IS with no warranty, either express or implied. * This software is distributed under a license and may not be copied, * modified or distributed except as expressly authorized under the * terms of the license contained in the file LICENSE.TXT in this distribution. */ #ifndef RL_CLI_VOLATILE_HPP #define RL_CLI_VOLATILE_HPP #ifdef _MSC_VER # pragma once #endif #include "base.hpp" #include "atomic.hpp" //!!! fix Java volatiles! // they must be modeled as seq_cst stores/loads namespace rl { template<typename T> class nvolatile; template<typename T> class nvolatile_proxy { public: typedef typename atomic_add_type<T>::type add_type; template<typename Y> friend class nvolatile; operator T () const { return load(); } T operator = (T value) { store(value); return value; } T operator = (nvolatile_proxy const& r) { T const value = r.load(); store(value); return *this; } T operator ++ (int) { T tmp = load(); store(tmp + 1); return tmp; } T operator -- (int) { T tmp = load(); store(tmp - 1); return tmp; } T operator ++ () { T tmp = load(); store(tmp + 1); return tmp + 1; } T operator -- () { T tmp = load(); store(tmp - 1); return tmp - 1; } T operator += (add_type value) { T tmp = load(); store(tmp + value); return tmp + value; } T operator -= (add_type value) { T tmp = load(); store(tmp - value); return tmp - value; } private: nvolatile<T>& var_; debug_info info_; nvolatile_proxy(nvolatile<T>& var, debug_info_param info) : var_(var) , info_(info) { } T load() const { return var_.load(mo_acquire, info_); } void store(T value) { var_.store(value, mo_release, info_); } }; template<typename T> class nvolatile : public generic_atomic<T, true> { public: typedef nvolatile_proxy<T> proxy_t; friend class nvolatile_proxy<T>; nvolatile() { } explicit nvolatile(T value) { //??? whether here must be mo_relaxed or mo_release? this->store(value, mo_release, $); } nvolatile(nvolatile const& r) { T const value = r.load(mo_acquire, $); //??? whether here must be mo_relaxed or mo_release? this->store(value, mo_release, $); } nvolatile(proxy_t const& r) { T const value = r.var_.load(mo_acquire, r.info_); //??? whether here must be mo_relaxed or mo_release? this->store(value, mo_release, r.info_); } proxy_t operator () (debug_info_param info) { return proxy_t(*this, info); } }; } #endif |