method-ml.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /* Copyright (C) 2012-2022 Free Software Foundation, Inc.
  2. Contributed by Torvald Riegel <triegel@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libitm_i.h"
  20. using namespace GTM;
  21. namespace {
  22. // This group consists of all TM methods that synchronize via multiple locks
  23. // (or ownership records).
  24. struct ml_mg : public method_group
  25. {
  26. static const gtm_word LOCK_BIT = (~(gtm_word)0 >> 1) + 1;
  27. static const gtm_word INCARNATION_BITS = 3;
  28. static const gtm_word INCARNATION_MASK = 7;
  29. // Maximum time is all bits except the lock bit, the overflow reserve bit,
  30. // and the incarnation bits).
  31. static const gtm_word TIME_MAX = (~(gtm_word)0 >> (2 + INCARNATION_BITS));
  32. // The overflow reserve bit is the MSB of the timestamp part of an orec,
  33. // so we can have TIME_MAX+1 pending timestamp increases before we overflow.
  34. static const gtm_word OVERFLOW_RESERVE = TIME_MAX + 1;
  35. static bool is_locked(gtm_word o) { return o & LOCK_BIT; }
  36. static gtm_word set_locked(gtm_thread *tx)
  37. {
  38. return ((uintptr_t)tx >> 1) | LOCK_BIT;
  39. }
  40. // Returns a time that includes the lock bit, which is required by both
  41. // validate() and is_more_recent_or_locked().
  42. static gtm_word get_time(gtm_word o) { return o >> INCARNATION_BITS; }
  43. static gtm_word set_time(gtm_word time) { return time << INCARNATION_BITS; }
  44. static bool is_more_recent_or_locked(gtm_word o, gtm_word than_time)
  45. {
  46. // LOCK_BIT is the MSB; thus, if O is locked, it is larger than TIME_MAX.
  47. return get_time(o) > than_time;
  48. }
  49. static bool has_incarnation_left(gtm_word o)
  50. {
  51. return (o & INCARNATION_MASK) < INCARNATION_MASK;
  52. }
  53. static gtm_word inc_incarnation(gtm_word o) { return o + 1; }
  54. // The shared time base.
  55. atomic<gtm_word> time __attribute__((aligned(HW_CACHELINE_SIZE)));
  56. // The array of ownership records.
  57. atomic<gtm_word>* orecs __attribute__((aligned(HW_CACHELINE_SIZE)));
  58. char tailpadding[HW_CACHELINE_SIZE - sizeof(atomic<gtm_word>*)];
  59. // Location-to-orec mapping. Stripes of 32B mapped to 2^16 orecs using
  60. // multiplicative hashing. See Section 5.2.2 of Torvald Riegel's PhD thesis
  61. // for the background on this choice of hash function and parameters:
  62. // http://nbn-resolving.de/urn:nbn:de:bsz:14-qucosa-115596
  63. // We pick the Mult32 hash because it works well with fewer orecs (i.e.,
  64. // less space overhead and just 32b multiplication).
  65. // We may want to check and potentially change these settings once we get
  66. // better or just more benchmarks.
  67. static const gtm_word L2O_ORECS_BITS = 16;
  68. static const gtm_word L2O_ORECS = 1 << L2O_ORECS_BITS;
  69. // An iterator over the orecs covering the region [addr,addr+len).
  70. struct orec_iterator
  71. {
  72. static const gtm_word L2O_SHIFT = 5;
  73. static const uint32_t L2O_MULT32 = 81007;
  74. uint32_t mult;
  75. size_t orec;
  76. size_t orec_end;
  77. orec_iterator (const void* addr, size_t len)
  78. {
  79. uint32_t a = (uintptr_t) addr >> L2O_SHIFT;
  80. uint32_t ae = ((uintptr_t) addr + len + (1 << L2O_SHIFT) - 1)
  81. >> L2O_SHIFT;
  82. mult = a * L2O_MULT32;
  83. orec = mult >> (32 - L2O_ORECS_BITS);
  84. // We can't really avoid this second multiplication unless we use a
  85. // branch instead or know more about the alignment of addr. (We often
  86. // know len at compile time because of instantiations of functions
  87. // such as _ITM_RU* for accesses of specific lengths.
  88. orec_end = (ae * L2O_MULT32) >> (32 - L2O_ORECS_BITS);
  89. }
  90. size_t get() { return orec; }
  91. void advance()
  92. {
  93. // We cannot simply increment orec because L2O_MULT32 is larger than
  94. // 1 << (32 - L2O_ORECS_BITS), and thus an increase of the stripe (i.e.,
  95. // addr >> L2O_SHIFT) could increase the resulting orec index by more
  96. // than one; with the current parameters, we would roughly acquire a
  97. // fourth more orecs than necessary for regions covering more than orec.
  98. // Keeping mult around as extra state shouldn't matter much.
  99. mult += L2O_MULT32;
  100. orec = mult >> (32 - L2O_ORECS_BITS);
  101. }
  102. bool reached_end() { return orec == orec_end; }
  103. };
  104. virtual void init()
  105. {
  106. // We assume that an atomic<gtm_word> is backed by just a gtm_word, so
  107. // starting with zeroed memory is fine.
  108. orecs = (atomic<gtm_word>*) xcalloc(
  109. sizeof(atomic<gtm_word>) * L2O_ORECS, true);
  110. // This store is only executed while holding the serial lock, so relaxed
  111. // memory order is sufficient here.
  112. time.store(0, memory_order_relaxed);
  113. }
  114. virtual void fini()
  115. {
  116. free(orecs);
  117. }
  118. // We only re-initialize when our time base overflows. Thus, only reset
  119. // the time base and the orecs but do not re-allocate the orec array.
  120. virtual void reinit()
  121. {
  122. // This store is only executed while holding the serial lock, so relaxed
  123. // memory order is sufficient here. Same holds for the memset.
  124. time.store(0, memory_order_relaxed);
  125. // The memset below isn't strictly kosher because it bypasses
  126. // the non-trivial assignment operator defined by std::atomic. Using
  127. // a local void* is enough to prevent GCC from warning for this.
  128. void *p = orecs;
  129. memset(p, 0, sizeof(atomic<gtm_word>) * L2O_ORECS);
  130. }
  131. };
  132. static ml_mg o_ml_mg;
  133. // The multiple lock, write-through TM method.
  134. // Maps each memory location to one of the orecs in the orec array, and then
  135. // acquires the associated orec eagerly before writing through.
  136. // Writes require undo-logging because we are dealing with several locks/orecs
  137. // and need to resolve deadlocks if necessary by aborting one of the
  138. // transactions.
  139. // Reads do time-based validation with snapshot time extensions. Incarnation
  140. // numbers are used to decrease contention on the time base (with those,
  141. // aborted transactions do not need to acquire a new version number for the
  142. // data that has been previously written in the transaction and needs to be
  143. // rolled back).
  144. // gtm_thread::shared_state is used to store a transaction's current
  145. // snapshot time (or commit time). The serial lock uses ~0 for inactive
  146. // transactions and 0 for active ones. Thus, we always have a meaningful
  147. // timestamp in shared_state that can be used to implement quiescence-based
  148. // privatization safety.
  149. class ml_wt_dispatch : public abi_dispatch
  150. {
  151. protected:
  152. static void pre_write(gtm_thread *tx, const void *addr, size_t len)
  153. {
  154. gtm_word snapshot = tx->shared_state.load(memory_order_relaxed);
  155. gtm_word locked_by_tx = ml_mg::set_locked(tx);
  156. // Lock all orecs that cover the region.
  157. ml_mg::orec_iterator oi(addr, len);
  158. do
  159. {
  160. // Load the orec. Relaxed memory order is sufficient here because
  161. // either we have acquired the orec or we will try to acquire it with
  162. // a CAS with stronger memory order.
  163. gtm_word o = o_ml_mg.orecs[oi.get()].load(memory_order_relaxed);
  164. // Check whether we have acquired the orec already.
  165. if (likely (locked_by_tx != o))
  166. {
  167. // If not, acquire. Make sure that our snapshot time is larger or
  168. // equal than the orec's version to avoid masking invalidations of
  169. // our snapshot with our own writes.
  170. if (unlikely (ml_mg::is_locked(o)))
  171. tx->restart(RESTART_LOCKED_WRITE);
  172. if (unlikely (ml_mg::get_time(o) > snapshot))
  173. {
  174. // We only need to extend the snapshot if we have indeed read
  175. // from this orec before. Given that we are an update
  176. // transaction, we will have to extend anyway during commit.
  177. // ??? Scan the read log instead, aborting if we have read
  178. // from data covered by this orec before?
  179. snapshot = extend(tx);
  180. }
  181. // We need acquire memory order here to synchronize with other
  182. // (ownership) releases of the orec. We do not need acq_rel order
  183. // because whenever another thread reads from this CAS'
  184. // modification, then it will abort anyway and does not rely on
  185. // any further happens-before relation to be established.
  186. if (unlikely (!o_ml_mg.orecs[oi.get()].compare_exchange_strong(
  187. o, locked_by_tx, memory_order_acquire)))
  188. tx->restart(RESTART_LOCKED_WRITE);
  189. // We use an explicit fence here to avoid having to use release
  190. // memory order for all subsequent data stores. This fence will
  191. // synchronize with loads of the data with acquire memory order.
  192. // See post_load() for why this is necessary.
  193. // Adding require memory order to the prior CAS is not sufficient,
  194. // at least according to the Batty et al. formalization of the
  195. // memory model.
  196. atomic_thread_fence(memory_order_release);
  197. // We log the previous value here to be able to use incarnation
  198. // numbers when we have to roll back.
  199. // ??? Reserve capacity early to avoid capacity checks here?
  200. gtm_rwlog_entry *e = tx->writelog.push();
  201. e->orec = o_ml_mg.orecs + oi.get();
  202. e->value = o;
  203. }
  204. oi.advance();
  205. }
  206. while (!oi.reached_end());
  207. // Do undo logging. We do not know which region prior writes logged
  208. // (even if orecs have been acquired), so just log everything.
  209. tx->undolog.log(addr, len);
  210. }
  211. static void pre_write(const void *addr, size_t len)
  212. {
  213. gtm_thread *tx = gtm_thr();
  214. pre_write(tx, addr, len);
  215. }
  216. // Returns true iff all the orecs in our read log still have the same time
  217. // or have been locked by the transaction itself.
  218. static bool validate(gtm_thread *tx)
  219. {
  220. gtm_word locked_by_tx = ml_mg::set_locked(tx);
  221. // ??? This might get called from pre_load() via extend(). In that case,
  222. // we don't really need to check the new entries that pre_load() is
  223. // adding. Stop earlier?
  224. for (gtm_rwlog_entry *i = tx->readlog.begin(), *ie = tx->readlog.end();
  225. i != ie; i++)
  226. {
  227. // Relaxed memory order is sufficient here because we do not need to
  228. // establish any new synchronizes-with relationships. We only need
  229. // to read a value that is as least as current as enforced by the
  230. // callers: extend() loads global time with acquire, and trycommit()
  231. // increments global time with acquire. Therefore, we will see the
  232. // most recent orec updates before the global time that we load.
  233. gtm_word o = i->orec->load(memory_order_relaxed);
  234. // We compare only the time stamp and the lock bit here. We know that
  235. // we have read only committed data before, so we can ignore
  236. // intermediate yet rolled-back updates presented by the incarnation
  237. // number bits.
  238. if (ml_mg::get_time(o) != ml_mg::get_time(i->value)
  239. && o != locked_by_tx)
  240. return false;
  241. }
  242. return true;
  243. }
  244. // Tries to extend the snapshot to a more recent time. Returns the new
  245. // snapshot time and updates TX->SHARED_STATE. If the snapshot cannot be
  246. // extended to the current global time, TX is restarted.
  247. static gtm_word extend(gtm_thread *tx)
  248. {
  249. // We read global time here, even if this isn't strictly necessary
  250. // because we could just return the maximum of the timestamps that
  251. // validate sees. However, the potential cache miss on global time is
  252. // probably a reasonable price to pay for avoiding unnecessary extensions
  253. // in the future.
  254. // We need acquire memory oder because we have to synchronize with the
  255. // increment of global time by update transactions, whose lock
  256. // acquisitions we have to observe (also see trycommit()).
  257. gtm_word snapshot = o_ml_mg.time.load(memory_order_acquire);
  258. if (!validate(tx))
  259. tx->restart(RESTART_VALIDATE_READ);
  260. // Update our public snapshot time. Probably useful to decrease waiting
  261. // due to quiescence-based privatization safety.
  262. // Use release memory order to establish synchronizes-with with the
  263. // privatizers; prior data loads should happen before the privatizers
  264. // potentially modify anything.
  265. tx->shared_state.store(snapshot, memory_order_release);
  266. return snapshot;
  267. }
  268. // First pass over orecs. Load and check all orecs that cover the region.
  269. // Write to read log, extend snapshot time if necessary.
  270. static gtm_rwlog_entry* pre_load(gtm_thread *tx, const void* addr,
  271. size_t len)
  272. {
  273. // Don't obtain an iterator yet because the log might get resized.
  274. size_t log_start = tx->readlog.size();
  275. gtm_word snapshot = tx->shared_state.load(memory_order_relaxed);
  276. gtm_word locked_by_tx = ml_mg::set_locked(tx);
  277. ml_mg::orec_iterator oi(addr, len);
  278. do
  279. {
  280. // We need acquire memory order here so that this load will
  281. // synchronize with the store that releases the orec in trycommit().
  282. // In turn, this makes sure that subsequent data loads will read from
  283. // a visible sequence of side effects that starts with the most recent
  284. // store to the data right before the release of the orec.
  285. gtm_word o = o_ml_mg.orecs[oi.get()].load(memory_order_acquire);
  286. if (likely (!ml_mg::is_more_recent_or_locked(o, snapshot)))
  287. {
  288. success:
  289. gtm_rwlog_entry *e = tx->readlog.push();
  290. e->orec = o_ml_mg.orecs + oi.get();
  291. e->value = o;
  292. }
  293. else if (!ml_mg::is_locked(o))
  294. {
  295. // We cannot read this part of the region because it has been
  296. // updated more recently than our snapshot time. If we can extend
  297. // our snapshot, then we can read.
  298. snapshot = extend(tx);
  299. goto success;
  300. }
  301. else
  302. {
  303. // If the orec is locked by us, just skip it because we can just
  304. // read from it. Otherwise, restart the transaction.
  305. if (o != locked_by_tx)
  306. tx->restart(RESTART_LOCKED_READ);
  307. }
  308. oi.advance();
  309. }
  310. while (!oi.reached_end());
  311. return &tx->readlog[log_start];
  312. }
  313. // Second pass over orecs, verifying that the we had a consistent read.
  314. // Restart the transaction if any of the orecs is locked by another
  315. // transaction.
  316. static void post_load(gtm_thread *tx, gtm_rwlog_entry* log)
  317. {
  318. for (gtm_rwlog_entry *end = tx->readlog.end(); log != end; log++)
  319. {
  320. // Check that the snapshot is consistent. We expect the previous data
  321. // load to have acquire memory order, or be atomic and followed by an
  322. // acquire fence.
  323. // As a result, the data load will synchronize with the release fence
  324. // issued by the transactions whose data updates the data load has read
  325. // from. This forces the orec load to read from a visible sequence of
  326. // side effects that starts with the other updating transaction's
  327. // store that acquired the orec and set it to locked.
  328. // We therefore either read a value with the locked bit set (and
  329. // restart) or read an orec value that was written after the data had
  330. // been written. Either will allow us to detect inconsistent reads
  331. // because it will have a higher/different value.
  332. // Also note that differently to validate(), we compare the raw value
  333. // of the orec here, including incarnation numbers. We must prevent
  334. // returning uncommitted data from loads (whereas when validating, we
  335. // already performed a consistent load).
  336. gtm_word o = log->orec->load(memory_order_relaxed);
  337. if (log->value != o)
  338. tx->restart(RESTART_VALIDATE_READ);
  339. }
  340. }
  341. template <typename V> static V load(const V* addr, ls_modifier mod)
  342. {
  343. // Read-for-write should be unlikely, but we need to handle it or will
  344. // break later WaW optimizations.
  345. if (unlikely(mod == RfW))
  346. {
  347. pre_write(addr, sizeof(V));
  348. return *addr;
  349. }
  350. if (unlikely(mod == RaW))
  351. return *addr;
  352. // ??? Optimize for RaR?
  353. gtm_thread *tx = gtm_thr();
  354. gtm_rwlog_entry* log = pre_load(tx, addr, sizeof(V));
  355. // Load the data.
  356. // This needs to have acquire memory order (see post_load()).
  357. // Alternatively, we can put an acquire fence after the data load but this
  358. // is probably less efficient.
  359. // FIXME We would need an atomic load with acquire memory order here but
  360. // we can't just forge an atomic load for nonatomic data because this
  361. // might not work on all implementations of atomics. However, we need
  362. // the acquire memory order and we can only establish this if we link
  363. // it to the matching release using a reads-from relation between atomic
  364. // loads. Also, the compiler is allowed to optimize nonatomic accesses
  365. // differently than atomic accesses (e.g., if the load would be moved to
  366. // after the fence, we potentially don't synchronize properly anymore).
  367. // Instead of the following, just use an ordinary load followed by an
  368. // acquire fence, and hope that this is good enough for now:
  369. // V v = atomic_load_explicit((atomic<V>*)addr, memory_order_acquire);
  370. V v = *addr;
  371. atomic_thread_fence(memory_order_acquire);
  372. // ??? Retry the whole load if it wasn't consistent?
  373. post_load(tx, log);
  374. return v;
  375. }
  376. template <typename V> static void store(V* addr, const V value,
  377. ls_modifier mod)
  378. {
  379. if (likely(mod != WaW))
  380. pre_write(addr, sizeof(V));
  381. // FIXME We would need an atomic store here but we can't just forge an
  382. // atomic load for nonatomic data because this might not work on all
  383. // implementations of atomics. However, we need this store to link the
  384. // release fence in pre_write() to the acquire operation in load, which
  385. // is only guaranteed if we have a reads-from relation between atomic
  386. // accesses. Also, the compiler is allowed to optimize nonatomic accesses
  387. // differently than atomic accesses (e.g., if the store would be moved
  388. // to before the release fence in pre_write(), things could go wrong).
  389. // atomic_store_explicit((atomic<V>*)addr, value, memory_order_relaxed);
  390. *addr = value;
  391. }
  392. public:
  393. static void memtransfer_static(void *dst, const void* src, size_t size,
  394. bool may_overlap, ls_modifier dst_mod, ls_modifier src_mod)
  395. {
  396. gtm_rwlog_entry* log = 0;
  397. gtm_thread *tx = 0;
  398. if (src_mod == RfW)
  399. {
  400. tx = gtm_thr();
  401. pre_write(tx, src, size);
  402. }
  403. else if (src_mod != RaW && src_mod != NONTXNAL)
  404. {
  405. tx = gtm_thr();
  406. log = pre_load(tx, src, size);
  407. }
  408. // ??? Optimize for RaR?
  409. if (dst_mod != NONTXNAL && dst_mod != WaW)
  410. {
  411. if (src_mod != RfW && (src_mod == RaW || src_mod == NONTXNAL))
  412. tx = gtm_thr();
  413. pre_write(tx, dst, size);
  414. }
  415. // FIXME We should use atomics here (see store()). Let's just hope that
  416. // memcpy/memmove are good enough.
  417. if (!may_overlap)
  418. ::memcpy(dst, src, size);
  419. else
  420. ::memmove(dst, src, size);
  421. // ??? Retry the whole memtransfer if it wasn't consistent?
  422. if (src_mod != RfW && src_mod != RaW && src_mod != NONTXNAL)
  423. {
  424. // See load() for why we need the acquire fence here.
  425. atomic_thread_fence(memory_order_acquire);
  426. post_load(tx, log);
  427. }
  428. }
  429. static void memset_static(void *dst, int c, size_t size, ls_modifier mod)
  430. {
  431. if (mod != WaW)
  432. pre_write(dst, size);
  433. // FIXME We should use atomics here (see store()). Let's just hope that
  434. // memset is good enough.
  435. ::memset(dst, c, size);
  436. }
  437. virtual gtm_restart_reason begin_or_restart()
  438. {
  439. // We don't need to do anything for nested transactions.
  440. gtm_thread *tx = gtm_thr();
  441. if (tx->parent_txns.size() > 0)
  442. return NO_RESTART;
  443. // Read the current time, which becomes our snapshot time.
  444. // Use acquire memory oder so that we see the lock acquisitions by update
  445. // transcations that incremented the global time (see trycommit()).
  446. gtm_word snapshot = o_ml_mg.time.load(memory_order_acquire);
  447. // Re-initialize method group on time overflow.
  448. if (snapshot >= o_ml_mg.TIME_MAX)
  449. return RESTART_INIT_METHOD_GROUP;
  450. // We don't need to enforce any ordering for the following store. There
  451. // are no earlier data loads in this transaction, so the store cannot
  452. // become visible before those (which could lead to the violation of
  453. // privatization safety). The store can become visible after later loads
  454. // but this does not matter because the previous value will have been
  455. // smaller or equal (the serial lock will set shared_state to zero when
  456. // marking the transaction as active, and restarts enforce immediate
  457. // visibility of a smaller or equal value with a barrier (see
  458. // rollback()).
  459. tx->shared_state.store(snapshot, memory_order_relaxed);
  460. return NO_RESTART;
  461. }
  462. virtual bool trycommit(gtm_word& priv_time)
  463. {
  464. gtm_thread* tx = gtm_thr();
  465. // If we haven't updated anything, we can commit.
  466. if (!tx->writelog.size())
  467. {
  468. tx->readlog.clear();
  469. // We still need to ensure privatization safety, unfortunately. While
  470. // we cannot have privatized anything by ourselves (because we are not
  471. // an update transaction), we can have observed the commits of
  472. // another update transaction that privatized something. Because any
  473. // commit happens before ensuring privatization, our snapshot and
  474. // commit can thus have happened before ensuring privatization safety
  475. // for this commit/snapshot time. Therefore, before we can return to
  476. // nontransactional code that might use the privatized data, we must
  477. // ensure privatization safety for our snapshot time.
  478. // This still seems to be better than not allowing use of the
  479. // snapshot time before privatization safety has been ensured because
  480. // we at least can run transactions such as this one, and in the
  481. // meantime the transaction producing this commit time might have
  482. // finished ensuring privatization safety for it.
  483. priv_time = tx->shared_state.load(memory_order_relaxed);
  484. return true;
  485. }
  486. // Get a commit time.
  487. // Overflow of o_ml_mg.time is prevented in begin_or_restart().
  488. // We need acq_rel here because (1) the acquire part is required for our
  489. // own subsequent call to validate(), and the release part is necessary to
  490. // make other threads' validate() work as explained there and in extend().
  491. gtm_word ct = o_ml_mg.time.fetch_add(1, memory_order_acq_rel) + 1;
  492. // Extend our snapshot time to at least our commit time.
  493. // Note that we do not need to validate if our snapshot time is right
  494. // before the commit time because we are never sharing the same commit
  495. // time with other transactions.
  496. // No need to reset shared_state, which will be modified by the serial
  497. // lock right after our commit anyway.
  498. gtm_word snapshot = tx->shared_state.load(memory_order_relaxed);
  499. if (snapshot < ct - 1 && !validate(tx))
  500. return false;
  501. // Release orecs.
  502. // See pre_load() / post_load() for why we need release memory order.
  503. // ??? Can we use a release fence and relaxed stores?
  504. gtm_word v = ml_mg::set_time(ct);
  505. for (gtm_rwlog_entry *i = tx->writelog.begin(), *ie = tx->writelog.end();
  506. i != ie; i++)
  507. i->orec->store(v, memory_order_release);
  508. // We're done, clear the logs.
  509. tx->writelog.clear();
  510. tx->readlog.clear();
  511. // Need to ensure privatization safety. Every other transaction must
  512. // have a snapshot time that is at least as high as our commit time
  513. // (i.e., our commit must be visible to them).
  514. priv_time = ct;
  515. return true;
  516. }
  517. virtual void rollback(gtm_transaction_cp *cp)
  518. {
  519. // We don't do anything for rollbacks of nested transactions.
  520. // ??? We could release locks here if we snapshot writelog size. readlog
  521. // is similar. This is just a performance optimization though. Nested
  522. // aborts should be rather infrequent, so the additional save/restore
  523. // overhead for the checkpoints could be higher.
  524. if (cp != 0)
  525. return;
  526. gtm_thread *tx = gtm_thr();
  527. gtm_word overflow_value = 0;
  528. // Release orecs.
  529. for (gtm_rwlog_entry *i = tx->writelog.begin(), *ie = tx->writelog.end();
  530. i != ie; i++)
  531. {
  532. // If possible, just increase the incarnation number.
  533. // See pre_load() / post_load() for why we need release memory order.
  534. // ??? Can we use a release fence and relaxed stores? (Same below.)
  535. if (ml_mg::has_incarnation_left(i->value))
  536. i->orec->store(ml_mg::inc_incarnation(i->value),
  537. memory_order_release);
  538. else
  539. {
  540. // We have an incarnation overflow. Acquire a new timestamp, and
  541. // use it from now on as value for each orec whose incarnation
  542. // number cannot be increased.
  543. // Overflow of o_ml_mg.time is prevented in begin_or_restart().
  544. // See pre_load() / post_load() for why we need release memory
  545. // order.
  546. if (!overflow_value)
  547. // Release memory order is sufficient but required here.
  548. // In contrast to the increment in trycommit(), we need release
  549. // for the same reason but do not need the acquire because we
  550. // do not validate subsequently.
  551. overflow_value = ml_mg::set_time(
  552. o_ml_mg.time.fetch_add(1, memory_order_release) + 1);
  553. i->orec->store(overflow_value, memory_order_release);
  554. }
  555. }
  556. // We need this release fence to ensure that privatizers see the
  557. // rolled-back original state (not any uncommitted values) when they read
  558. // the new snapshot time that we write in begin_or_restart().
  559. atomic_thread_fence(memory_order_release);
  560. // We're done, clear the logs.
  561. tx->writelog.clear();
  562. tx->readlog.clear();
  563. }
  564. virtual bool snapshot_most_recent()
  565. {
  566. // This is the same code as in extend() except that we do not restart
  567. // on failure but simply return the result, and that we don't validate
  568. // if our snapshot is already most recent.
  569. gtm_thread* tx = gtm_thr();
  570. gtm_word snapshot = o_ml_mg.time.load(memory_order_acquire);
  571. if (snapshot == tx->shared_state.load(memory_order_relaxed))
  572. return true;
  573. if (!validate(tx))
  574. return false;
  575. // Update our public snapshot time. Necessary so that we do not prevent
  576. // other transactions from ensuring privatization safety.
  577. tx->shared_state.store(snapshot, memory_order_release);
  578. return true;
  579. }
  580. virtual bool supports(unsigned number_of_threads)
  581. {
  582. // Each txn can commit and fail and rollback once before checking for
  583. // overflow, so this bounds the number of threads that we can support.
  584. // In practice, this won't be a problem but we check it anyway so that
  585. // we never break in the occasional weird situation.
  586. return (number_of_threads * 2 <= ml_mg::OVERFLOW_RESERVE);
  587. }
  588. CREATE_DISPATCH_METHODS(virtual, )
  589. CREATE_DISPATCH_METHODS_MEM()
  590. ml_wt_dispatch() : abi_dispatch(false, true, false, false, 0, &o_ml_mg)
  591. { }
  592. };
  593. } // anon namespace
  594. static const ml_wt_dispatch o_ml_wt_dispatch;
  595. abi_dispatch *
  596. GTM::dispatch_ml_wt ()
  597. {
  598. return const_cast<ml_wt_dispatch *>(&o_ml_wt_dispatch);
  599. }