Torvald Riegel | 11f30bb | 2012-01-08 14:13:49 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2008, 2009, 2011, 2012 Free Software Foundation, Inc. |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 2 | Contributed by Richard Henderson <rth@redhat.com>. |
| 3 | |
| 4 | This file is part of the GNU Transactional Memory Library (libitm). |
| 5 | |
| 6 | Libitm is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | Libitm is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 13 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | more details. |
| 15 | |
| 16 | Under Section 7 of GPL version 3, you are granted additional |
| 17 | permissions described in the GCC Runtime Library Exception, version |
| 18 | 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License and |
| 21 | a copy of the GCC Runtime Library Exception along with this program; |
| 22 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | <http://www.gnu.org/licenses/>. */ |
| 24 | |
| 25 | #include "libitm_i.h" |
| 26 | |
| 27 | // Avoid a dependency on libstdc++ for the pure virtuals in abi_dispatch. |
| 28 | extern "C" void HIDDEN |
| 29 | __cxa_pure_virtual () |
| 30 | { |
| 31 | abort (); |
| 32 | } |
| 33 | |
| 34 | using namespace GTM; |
| 35 | |
| 36 | namespace { |
| 37 | |
| 38 | // This group consists of the serial, serialirr, and serialirr_onwrite |
| 39 | // methods, which all need no global state (except what is already provided |
| 40 | // by the serial mode implementation). |
| 41 | struct serial_mg : public method_group |
| 42 | { |
| 43 | virtual void init() { } |
| 44 | virtual void fini() { } |
| 45 | }; |
| 46 | |
| 47 | static serial_mg o_serial_mg; |
| 48 | |
| 49 | |
| 50 | class serialirr_dispatch : public abi_dispatch |
| 51 | { |
| 52 | public: |
Torvald Riegel | b679c81 | 2012-10-24 19:52:02 +0000 | [diff] [blame] | 53 | serialirr_dispatch() : abi_dispatch(false, true, true, false, |
| 54 | gtm_thread::STATE_SERIAL | gtm_thread::STATE_IRREVOCABLE, &o_serial_mg) |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 55 | { } |
| 56 | |
| 57 | protected: |
| 58 | serialirr_dispatch(bool ro, bool wt, bool uninstrumented, |
Torvald Riegel | b679c81 | 2012-10-24 19:52:02 +0000 | [diff] [blame] | 59 | bool closed_nesting, uint32_t requires_serial, method_group* mg) : |
| 60 | abi_dispatch(ro, wt, uninstrumented, closed_nesting, requires_serial, mg) |
| 61 | { } |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 62 | |
| 63 | // Transactional loads and stores simply access memory directly. |
| 64 | // These methods are static to avoid indirect calls, and will be used by the |
| 65 | // virtual ABI dispatch methods or by static direct-access methods created |
| 66 | // below. |
| 67 | template <typename V> static V load(const V* addr, ls_modifier mod) |
| 68 | { |
| 69 | return *addr; |
| 70 | } |
| 71 | template <typename V> static void store(V* addr, const V value, |
| 72 | ls_modifier mod) |
| 73 | { |
| 74 | *addr = value; |
| 75 | } |
| 76 | |
| 77 | public: |
| 78 | static void memtransfer_static(void *dst, const void* src, size_t size, |
| 79 | bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod) |
| 80 | { |
| 81 | if (!may_overlap) |
| 82 | ::memcpy(dst, src, size); |
| 83 | else |
| 84 | ::memmove(dst, src, size); |
| 85 | } |
| 86 | |
| 87 | static void memset_static(void *dst, int c, size_t size, ls_modifier mod) |
| 88 | { |
| 89 | ::memset(dst, c, size); |
| 90 | } |
| 91 | |
| 92 | CREATE_DISPATCH_METHODS(virtual, ) |
| 93 | CREATE_DISPATCH_METHODS_MEM() |
| 94 | |
| 95 | virtual gtm_restart_reason begin_or_restart() { return NO_RESTART; } |
| 96 | virtual bool trycommit(gtm_word& priv_time) { return true; } |
| 97 | virtual void rollback(gtm_transaction_cp *cp) { abort(); } |
| 98 | |
| 99 | virtual abi_dispatch* closed_nesting_alternative() |
| 100 | { |
| 101 | // For nested transactions with an instrumented code path, we can do |
| 102 | // undo logging. |
| 103 | return GTM::dispatch_serial(); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | class serial_dispatch : public abi_dispatch |
| 108 | { |
| 109 | protected: |
| 110 | static void log(const void *addr, size_t len) |
| 111 | { |
Torvald Riegel | 11f30bb | 2012-01-08 14:13:49 +0000 | [diff] [blame] | 112 | gtm_thread *tx = gtm_thr(); |
| 113 | tx->undolog.log(addr, len); |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | template <typename V> static V load(const V* addr, ls_modifier mod) |
| 117 | { |
| 118 | return *addr; |
| 119 | } |
| 120 | template <typename V> static void store(V* addr, const V value, |
| 121 | ls_modifier mod) |
| 122 | { |
| 123 | if (mod != WaW) |
| 124 | log(addr, sizeof(V)); |
| 125 | *addr = value; |
| 126 | } |
| 127 | |
| 128 | public: |
| 129 | static void memtransfer_static(void *dst, const void* src, size_t size, |
| 130 | bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod) |
| 131 | { |
| 132 | if (dst_mod != WaW && dst_mod != NONTXNAL) |
| 133 | log(dst, size); |
| 134 | if (!may_overlap) |
| 135 | ::memcpy(dst, src, size); |
| 136 | else |
| 137 | ::memmove(dst, src, size); |
| 138 | } |
| 139 | |
| 140 | static void memset_static(void *dst, int c, size_t size, ls_modifier mod) |
| 141 | { |
| 142 | if (mod != WaW) |
| 143 | log(dst, size); |
| 144 | ::memset(dst, c, size); |
| 145 | } |
| 146 | |
| 147 | virtual gtm_restart_reason begin_or_restart() { return NO_RESTART; } |
| 148 | virtual bool trycommit(gtm_word& priv_time) { return true; } |
| 149 | // Local undo will handle this. |
| 150 | // trydropreference() need not be changed either. |
| 151 | virtual void rollback(gtm_transaction_cp *cp) { } |
| 152 | |
| 153 | CREATE_DISPATCH_METHODS(virtual, ) |
| 154 | CREATE_DISPATCH_METHODS_MEM() |
| 155 | |
Torvald Riegel | b679c81 | 2012-10-24 19:52:02 +0000 | [diff] [blame] | 156 | serial_dispatch() : abi_dispatch(false, true, false, true, |
| 157 | gtm_thread::STATE_SERIAL, &o_serial_mg) |
| 158 | { } |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | |
| 162 | // Like serialirr_dispatch but does not requests serial-irrevocable mode until |
| 163 | // the first write in the transaction. Can be useful for read-mostly workloads |
| 164 | // and testing, but is likely too simple to be of general purpose. |
| 165 | class serialirr_onwrite_dispatch : public serialirr_dispatch |
| 166 | { |
| 167 | public: |
| 168 | serialirr_onwrite_dispatch() : |
Torvald Riegel | b679c81 | 2012-10-24 19:52:02 +0000 | [diff] [blame] | 169 | serialirr_dispatch(false, true, false, false, 0, &o_serial_mg) { } |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 170 | |
| 171 | protected: |
| 172 | static void pre_write() |
| 173 | { |
| 174 | gtm_thread *tx = gtm_thr(); |
| 175 | if (!(tx->state & (gtm_thread::STATE_SERIAL |
| 176 | | gtm_thread::STATE_IRREVOCABLE))) |
| 177 | tx->serialirr_mode(); |
| 178 | } |
| 179 | |
| 180 | // Transactional loads access memory directly. |
| 181 | // Transactional stores switch to serial mode first. |
| 182 | template <typename V> static void store(V* addr, const V value, |
| 183 | ls_modifier mod) |
| 184 | { |
| 185 | pre_write(); |
| 186 | serialirr_dispatch::store(addr, value, mod); |
| 187 | } |
| 188 | |
| 189 | public: |
| 190 | static void memtransfer_static(void *dst, const void* src, size_t size, |
| 191 | bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod) |
| 192 | { |
| 193 | pre_write(); |
| 194 | serialirr_dispatch::memtransfer_static(dst, src, size, may_overlap, |
| 195 | dst_mod, src_mod); |
| 196 | } |
| 197 | |
| 198 | static void memset_static(void *dst, int c, size_t size, ls_modifier mod) |
| 199 | { |
| 200 | pre_write(); |
| 201 | serialirr_dispatch::memset_static(dst, c, size, mod); |
| 202 | } |
| 203 | |
| 204 | CREATE_DISPATCH_METHODS(virtual, ) |
| 205 | CREATE_DISPATCH_METHODS_MEM() |
| 206 | |
| 207 | virtual void rollback(gtm_transaction_cp *cp) |
| 208 | { |
| 209 | gtm_thread *tx = gtm_thr(); |
| 210 | if (tx->state & gtm_thread::STATE_IRREVOCABLE) |
| 211 | abort(); |
| 212 | } |
| 213 | }; |
| 214 | |
Torvald Riegel | 64fbcc7 | 2012-11-09 17:04:40 +0000 | [diff] [blame^] | 215 | // This group is pure HTM with serial mode as a fallback. There is no |
| 216 | // difference to serial_mg except that we need to enable or disable the HTM |
| 217 | // fastpath. See gtm_thread::begin_transaction. |
| 218 | struct htm_mg : public method_group |
| 219 | { |
| 220 | virtual void init() |
| 221 | { |
| 222 | // Enable the HTM fastpath if the HW is available. The fastpath is |
| 223 | // initially disabled. |
| 224 | #ifdef USE_HTM_FASTPATH |
| 225 | htm_fastpath = htm_init(); |
| 226 | #endif |
| 227 | } |
| 228 | virtual void fini() |
| 229 | { |
| 230 | // Disable the HTM fastpath. |
| 231 | htm_fastpath = 0; |
| 232 | } |
| 233 | }; |
| 234 | |
| 235 | static htm_mg o_htm_mg; |
| 236 | |
| 237 | // We just need the subclass to associate it with the HTM method group that |
| 238 | // sets up the HTM fast path. This will use serial_dispatch as fallback for |
| 239 | // transactions that might get canceled; it has a different method group, but |
| 240 | // this is harmless for serial dispatchs because they never abort. |
| 241 | class htm_dispatch : public serialirr_dispatch |
| 242 | { |
| 243 | public: |
| 244 | htm_dispatch() : serialirr_dispatch(false, true, false, false, |
| 245 | gtm_thread::STATE_SERIAL | gtm_thread::STATE_IRREVOCABLE, &o_htm_mg) |
| 246 | { } |
| 247 | }; |
| 248 | |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 249 | } // anon namespace |
| 250 | |
| 251 | static const serialirr_dispatch o_serialirr_dispatch; |
| 252 | static const serial_dispatch o_serial_dispatch; |
| 253 | static const serialirr_onwrite_dispatch o_serialirr_onwrite_dispatch; |
Torvald Riegel | 64fbcc7 | 2012-11-09 17:04:40 +0000 | [diff] [blame^] | 254 | static const htm_dispatch o_htm_dispatch; |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 255 | |
| 256 | abi_dispatch * |
| 257 | GTM::dispatch_serialirr () |
| 258 | { |
| 259 | return const_cast<serialirr_dispatch *>(&o_serialirr_dispatch); |
| 260 | } |
| 261 | |
| 262 | abi_dispatch * |
| 263 | GTM::dispatch_serial () |
| 264 | { |
| 265 | return const_cast<serial_dispatch *>(&o_serial_dispatch); |
| 266 | } |
| 267 | |
| 268 | abi_dispatch * |
| 269 | GTM::dispatch_serialirr_onwrite () |
| 270 | { |
| 271 | return |
| 272 | const_cast<serialirr_onwrite_dispatch *>(&o_serialirr_onwrite_dispatch); |
| 273 | } |
| 274 | |
Torvald Riegel | 64fbcc7 | 2012-11-09 17:04:40 +0000 | [diff] [blame^] | 275 | abi_dispatch * |
| 276 | GTM::dispatch_htm () |
| 277 | { |
| 278 | return const_cast<htm_dispatch *>(&o_htm_dispatch); |
| 279 | } |
| 280 | |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 281 | // Put the transaction into serial-irrevocable mode. |
| 282 | |
| 283 | void |
| 284 | GTM::gtm_thread::serialirr_mode () |
| 285 | { |
| 286 | struct abi_dispatch *disp = abi_disp (); |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 287 | |
Torvald Riegel | 64fbcc7 | 2012-11-09 17:04:40 +0000 | [diff] [blame^] | 288 | #if defined(USE_HTM_FASTPATH) |
| 289 | // HTM fastpath. If we are executing a HW transaction, don't go serial but |
| 290 | // continue. See gtm_thread::begin_transaction. |
| 291 | if (likely(htm_fastpath && !gtm_thread::serial_lock.is_write_locked())) |
| 292 | return; |
| 293 | #endif |
| 294 | |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 295 | if (this->state & STATE_SERIAL) |
| 296 | { |
| 297 | if (this->state & STATE_IRREVOCABLE) |
| 298 | return; |
| 299 | |
| 300 | // Try to commit the dispatch-specific part of the transaction, as we |
| 301 | // would do for an outermost commit. |
| 302 | // We're already serial, so we don't need to ensure privatization safety |
| 303 | // for other transactions here. |
| 304 | gtm_word priv_time = 0; |
| 305 | bool ok = disp->trycommit (priv_time); |
| 306 | // Given that we're already serial, the trycommit better work. |
| 307 | assert (ok); |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 308 | } |
| 309 | else if (serial_lock.write_upgrade (this)) |
| 310 | { |
| 311 | this->state |= STATE_SERIAL; |
| 312 | // Try to commit the dispatch-specific part of the transaction, as we |
| 313 | // would do for an outermost commit. |
| 314 | // We have successfully upgraded to serial mode, so we don't need to |
| 315 | // ensure privatization safety for other transactions here. |
Torvald Riegel | 610e390 | 2011-12-24 01:42:35 +0000 | [diff] [blame] | 316 | // However, we are still a reader (wrt. privatization safety) until we |
| 317 | // have either committed or restarted, so finish the upgrade after that. |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 318 | gtm_word priv_time = 0; |
Torvald Riegel | 610e390 | 2011-12-24 01:42:35 +0000 | [diff] [blame] | 319 | if (!disp->trycommit (priv_time)) |
| 320 | restart (RESTART_SERIAL_IRR, true); |
| 321 | gtm_thread::serial_lock.write_upgrade_finish(this); |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 322 | } |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 323 | else |
Torvald Riegel | 610e390 | 2011-12-24 01:42:35 +0000 | [diff] [blame] | 324 | restart (RESTART_SERIAL_IRR, false); |
| 325 | |
| 326 | this->state |= (STATE_SERIAL | STATE_IRREVOCABLE); |
| 327 | set_abi_disp (dispatch_serialirr ()); |
Aldy Hernandez | 0a35513 | 2011-11-08 11:13:41 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void ITM_REGPARM |
| 331 | _ITM_changeTransactionMode (_ITM_transactionState state) |
| 332 | { |
| 333 | assert (state == modeSerialIrrevocable); |
| 334 | gtm_thr()->serialirr_mode (); |
| 335 | } |