beginend.cc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /* Copyright (C) 2008-2022 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@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. #include <pthread.h>
  21. using namespace GTM;
  22. #if !defined(HAVE_ARCH_GTM_THREAD) || !defined(HAVE_ARCH_GTM_THREAD_DISP)
  23. extern __thread gtm_thread_tls _gtm_thr_tls;
  24. #endif
  25. // Put this at the start of a cacheline so that serial_lock's writers and
  26. // htm_fastpath fields are on the same cacheline, so that HW transactions
  27. // only have to pay one cacheline capacity to monitor both.
  28. gtm_rwlock GTM::gtm_thread::serial_lock
  29. __attribute__((aligned(HW_CACHELINE_SIZE)));
  30. gtm_thread *GTM::gtm_thread::list_of_threads = 0;
  31. unsigned GTM::gtm_thread::number_of_threads = 0;
  32. /* ??? Move elsewhere when we figure out library initialization. */
  33. uint64_t GTM::gtm_spin_count_var = 1000;
  34. #ifdef HAVE_64BIT_SYNC_BUILTINS
  35. static atomic<_ITM_transactionId_t> global_tid;
  36. #else
  37. static _ITM_transactionId_t global_tid;
  38. static pthread_mutex_t global_tid_lock = PTHREAD_MUTEX_INITIALIZER;
  39. #endif
  40. // Provides a on-thread-exit callback used to release per-thread data.
  41. static pthread_key_t thr_release_key;
  42. static pthread_once_t thr_release_once = PTHREAD_ONCE_INIT;
  43. /* Allocate a transaction structure. */
  44. void *
  45. GTM::gtm_thread::operator new (size_t s)
  46. {
  47. void *tx;
  48. assert(s == sizeof(gtm_thread));
  49. tx = xmalloc (sizeof (gtm_thread), true);
  50. memset (tx, 0, sizeof (gtm_thread));
  51. return tx;
  52. }
  53. /* Free the given transaction. Raises an error if the transaction is still
  54. in use. */
  55. void
  56. GTM::gtm_thread::operator delete(void *tx)
  57. {
  58. free(tx);
  59. }
  60. static void
  61. thread_exit_handler(void *)
  62. {
  63. gtm_thread *thr = gtm_thr();
  64. if (thr)
  65. delete thr;
  66. set_gtm_thr(0);
  67. }
  68. static void
  69. thread_exit_init()
  70. {
  71. if (pthread_key_create(&thr_release_key, thread_exit_handler))
  72. GTM_fatal("Creating thread release TLS key failed.");
  73. }
  74. GTM::gtm_thread::~gtm_thread()
  75. {
  76. if (nesting > 0)
  77. GTM_fatal("Thread exit while a transaction is still active.");
  78. // Deregister this transaction.
  79. serial_lock.write_lock ();
  80. gtm_thread **prev = &list_of_threads;
  81. for (; *prev; prev = &(*prev)->next_thread)
  82. {
  83. if (*prev == this)
  84. {
  85. *prev = (*prev)->next_thread;
  86. break;
  87. }
  88. }
  89. number_of_threads--;
  90. number_of_threads_changed(number_of_threads + 1, number_of_threads);
  91. serial_lock.write_unlock ();
  92. }
  93. GTM::gtm_thread::gtm_thread ()
  94. {
  95. // This object's memory has been set to zero by operator new, so no need
  96. // to initialize any of the other primitive-type members that do not have
  97. // constructors.
  98. shared_state.store(-1, memory_order_relaxed);
  99. // Register this transaction with the list of all threads' transactions.
  100. serial_lock.write_lock ();
  101. next_thread = list_of_threads;
  102. list_of_threads = this;
  103. number_of_threads++;
  104. number_of_threads_changed(number_of_threads - 1, number_of_threads);
  105. serial_lock.write_unlock ();
  106. init_cpp_exceptions ();
  107. if (pthread_once(&thr_release_once, thread_exit_init))
  108. GTM_fatal("Initializing thread release TLS key failed.");
  109. // Any non-null value is sufficient to trigger destruction of this
  110. // transaction when the current thread terminates.
  111. if (pthread_setspecific(thr_release_key, this))
  112. GTM_fatal("Setting thread release TLS key failed.");
  113. }
  114. static inline uint32_t
  115. choose_code_path(uint32_t prop, abi_dispatch *disp)
  116. {
  117. if ((prop & pr_uninstrumentedCode) && disp->can_run_uninstrumented_code())
  118. return a_runUninstrumentedCode;
  119. else
  120. return a_runInstrumentedCode;
  121. }
  122. #ifdef TARGET_BEGIN_TRANSACTION_ATTRIBUTE
  123. /* This macro can be used to define target specific attributes for this
  124. function. For example, S/390 requires floating point to be disabled in
  125. begin_transaction. */
  126. TARGET_BEGIN_TRANSACTION_ATTRIBUTE
  127. #endif
  128. uint32_t
  129. GTM::gtm_thread::begin_transaction (uint32_t prop, const gtm_jmpbuf *jb)
  130. {
  131. static const _ITM_transactionId_t tid_block_size = 1 << 16;
  132. gtm_thread *tx;
  133. abi_dispatch *disp;
  134. uint32_t ret;
  135. // ??? pr_undoLogCode is not properly defined in the ABI. Are barriers
  136. // omitted because they are not necessary (e.g., a transaction on thread-
  137. // local data) or because the compiler thinks that some kind of global
  138. // synchronization might perform better?
  139. if (unlikely(prop & pr_undoLogCode))
  140. GTM_fatal("pr_undoLogCode not supported");
  141. #ifdef USE_HTM_FASTPATH
  142. // HTM fastpath. Only chosen in the absence of transaction_cancel to allow
  143. // using an uninstrumented code path.
  144. // The fastpath is enabled only by dispatch_htm's method group, which uses
  145. // serial-mode methods as fallback. Serial-mode transactions cannot execute
  146. // concurrently with HW transactions because the latter monitor the serial
  147. // lock's writer flag and thus abort if another thread is or becomes a
  148. // serial transaction. Therefore, if the fastpath is enabled, then a
  149. // transaction is not executing as a HW transaction iff the serial lock is
  150. // write-locked. Also, HW transactions monitor the fastpath control
  151. // variable, so that they will only execute if dispatch_htm is still the
  152. // current method group. This allows us to use htm_fastpath and the serial
  153. // lock's writers flag to reliable determine whether the current thread runs
  154. // a HW transaction, and thus we do not need to maintain this information in
  155. // per-thread state.
  156. // If an uninstrumented code path is not available, we can still run
  157. // instrumented code from a HW transaction because the HTM fastpath kicks
  158. // in early in both begin and commit, and the transaction is not canceled.
  159. // HW transactions might get requests to switch to serial-irrevocable mode,
  160. // but these can be ignored because the HTM provides all necessary
  161. // correctness guarantees. Transactions cannot detect whether they are
  162. // indeed in serial mode, and HW transactions should never need serial mode
  163. // for any internal changes (e.g., they never abort visibly to the STM code
  164. // and thus do not trigger the standard retry handling).
  165. #ifndef HTM_CUSTOM_FASTPATH
  166. if (likely(serial_lock.get_htm_fastpath() && (prop & pr_hasNoAbort)))
  167. {
  168. // Note that the snapshot of htm_fastpath that we take here could be
  169. // outdated, and a different method group than dispatch_htm may have
  170. // been chosen in the meantime. Therefore, take care not not touch
  171. // anything besides the serial lock, which is independent of method
  172. // groups.
  173. for (uint32_t t = serial_lock.get_htm_fastpath(); t; t--)
  174. {
  175. uint32_t ret = htm_begin();
  176. if (htm_begin_success(ret))
  177. {
  178. // We are executing a transaction now.
  179. // Monitor the writer flag in the serial-mode lock, and abort
  180. // if there is an active or waiting serial-mode transaction.
  181. // Also checks that htm_fastpath is still nonzero and thus
  182. // HW transactions are allowed to run.
  183. // Note that this can also happen due to an enclosing
  184. // serial-mode transaction; we handle this case below.
  185. if (unlikely(serial_lock.htm_fastpath_disabled()))
  186. htm_abort();
  187. else
  188. // We do not need to set a_saveLiveVariables because of HTM.
  189. return (prop & pr_uninstrumentedCode) ?
  190. a_runUninstrumentedCode : a_runInstrumentedCode;
  191. }
  192. // The transaction has aborted. Don't retry if it's unlikely that
  193. // retrying the transaction will be successful.
  194. if (!htm_abort_should_retry(ret))
  195. break;
  196. // Check whether the HTM fastpath has been disabled.
  197. if (!serial_lock.get_htm_fastpath())
  198. break;
  199. // Wait until any concurrent serial-mode transactions have finished.
  200. // This is an empty critical section, but won't be elided.
  201. if (serial_lock.htm_fastpath_disabled())
  202. {
  203. tx = gtm_thr();
  204. if (unlikely(tx == NULL))
  205. {
  206. // See below.
  207. tx = new gtm_thread();
  208. set_gtm_thr(tx);
  209. }
  210. // Check whether there is an enclosing serial-mode transaction;
  211. // if so, we just continue as a nested transaction and don't
  212. // try to use the HTM fastpath. This case can happen when an
  213. // outermost relaxed transaction calls unsafe code that starts
  214. // a transaction.
  215. if (tx->nesting > 0)
  216. break;
  217. // Another thread is running a serial-mode transaction. Wait.
  218. serial_lock.read_lock(tx);
  219. serial_lock.read_unlock(tx);
  220. // TODO We should probably reset the retry count t here, unless
  221. // we have retried so often that we should go serial to avoid
  222. // starvation.
  223. }
  224. }
  225. }
  226. #else
  227. // If we have a custom HTM fastpath in ITM_beginTransaction, we implement
  228. // just the retry policy here. We communicate with the custom fastpath
  229. // through additional property bits and return codes, and either transfer
  230. // control back to the custom fastpath or run the fallback mechanism. The
  231. // fastpath synchronization algorithm itself is the same.
  232. // pr_HTMRetryableAbort states that a HW transaction started by the custom
  233. // HTM fastpath aborted, and that we thus have to decide whether to retry
  234. // the fastpath (returning a_tryHTMFastPath) or just proceed with the
  235. // fallback method.
  236. if (likely(serial_lock.get_htm_fastpath() && (prop & pr_HTMRetryableAbort)))
  237. {
  238. tx = gtm_thr();
  239. if (unlikely(tx == NULL))
  240. {
  241. // See below.
  242. tx = new gtm_thread();
  243. set_gtm_thr(tx);
  244. }
  245. // If this is the first abort, reset the retry count. We abuse
  246. // restart_total for the retry count, which is fine because our only
  247. // other fallback will use serial transactions, which don't use
  248. // restart_total but will reset it when committing.
  249. if (!(prop & pr_HTMRetriedAfterAbort))
  250. tx->restart_total = gtm_thread::serial_lock.get_htm_fastpath();
  251. if (--tx->restart_total > 0)
  252. {
  253. // Wait until any concurrent serial-mode transactions have finished.
  254. // Essentially the same code as above.
  255. if (!serial_lock.get_htm_fastpath())
  256. goto stop_custom_htm_fastpath;
  257. if (serial_lock.htm_fastpath_disabled())
  258. {
  259. if (tx->nesting > 0)
  260. goto stop_custom_htm_fastpath;
  261. serial_lock.read_lock(tx);
  262. serial_lock.read_unlock(tx);
  263. }
  264. // Let ITM_beginTransaction retry the custom HTM fastpath.
  265. return a_tryHTMFastPath;
  266. }
  267. }
  268. stop_custom_htm_fastpath:
  269. #endif
  270. #endif
  271. tx = gtm_thr();
  272. if (unlikely(tx == NULL))
  273. {
  274. // Create the thread object. The constructor will also set up automatic
  275. // deletion on thread termination.
  276. tx = new gtm_thread();
  277. set_gtm_thr(tx);
  278. }
  279. if (tx->nesting > 0)
  280. {
  281. // This is a nested transaction.
  282. // Check prop compatibility:
  283. // The ABI requires pr_hasNoFloatUpdate, pr_hasNoVectorUpdate,
  284. // pr_hasNoIrrevocable, pr_aWBarriersOmitted, pr_RaRBarriersOmitted, and
  285. // pr_hasNoSimpleReads to hold for the full dynamic scope of a
  286. // transaction. We could check that these are set for the nested
  287. // transaction if they are also set for the parent transaction, but the
  288. // ABI does not require these flags to be set if they could be set,
  289. // so the check could be too strict.
  290. // ??? For pr_readOnly, lexical or dynamic scope is unspecified.
  291. if (prop & pr_hasNoAbort)
  292. {
  293. // We can use flat nesting, so elide this transaction.
  294. if (!(prop & pr_instrumentedCode))
  295. {
  296. if (!(tx->state & STATE_SERIAL) ||
  297. !(tx->state & STATE_IRREVOCABLE))
  298. tx->serialirr_mode();
  299. }
  300. // Increment nesting level after checking that we have a method that
  301. // allows us to continue.
  302. tx->nesting++;
  303. return choose_code_path(prop, abi_disp());
  304. }
  305. // The transaction might abort, so use closed nesting if possible.
  306. // pr_hasNoAbort has lexical scope, so the compiler should really have
  307. // generated an instrumented code path.
  308. assert(prop & pr_instrumentedCode);
  309. // Create a checkpoint of the current transaction.
  310. gtm_transaction_cp *cp = tx->parent_txns.push();
  311. cp->save(tx);
  312. new (&tx->alloc_actions) aa_tree<uintptr_t, gtm_alloc_action>();
  313. // Check whether the current method actually supports closed nesting.
  314. // If we can switch to another one, do so.
  315. // If not, we assume that actual aborts are infrequent, and rather
  316. // restart in _ITM_abortTransaction when we really have to.
  317. disp = abi_disp();
  318. if (!disp->closed_nesting())
  319. {
  320. // ??? Should we elide the transaction if there is no alternative
  321. // method that supports closed nesting? If we do, we need to set
  322. // some flag to prevent _ITM_abortTransaction from aborting the
  323. // wrong transaction (i.e., some parent transaction).
  324. abi_dispatch *cn_disp = disp->closed_nesting_alternative();
  325. if (cn_disp)
  326. {
  327. disp = cn_disp;
  328. set_abi_disp(disp);
  329. }
  330. }
  331. }
  332. else
  333. {
  334. // Outermost transaction
  335. disp = tx->decide_begin_dispatch (prop);
  336. set_abi_disp (disp);
  337. }
  338. // Initialization that is common for outermost and nested transactions.
  339. tx->prop = prop;
  340. tx->nesting++;
  341. tx->jb = *jb;
  342. // As long as we have not exhausted a previously allocated block of TIDs,
  343. // we can avoid an atomic operation on a shared cacheline.
  344. if (tx->local_tid & (tid_block_size - 1))
  345. tx->id = tx->local_tid++;
  346. else
  347. {
  348. #ifdef HAVE_64BIT_SYNC_BUILTINS
  349. // We don't really care which block of TIDs we get but only that we
  350. // acquire one atomically; therefore, relaxed memory order is
  351. // sufficient.
  352. tx->id = global_tid.fetch_add(tid_block_size, memory_order_relaxed);
  353. tx->local_tid = tx->id + 1;
  354. #else
  355. pthread_mutex_lock (&global_tid_lock);
  356. global_tid += tid_block_size;
  357. tx->id = global_tid;
  358. tx->local_tid = tx->id + 1;
  359. pthread_mutex_unlock (&global_tid_lock);
  360. #endif
  361. }
  362. // Log the number of uncaught exceptions if we might have to roll back this
  363. // state.
  364. if (tx->cxa_uncaught_count_ptr != 0)
  365. tx->cxa_uncaught_count = *tx->cxa_uncaught_count_ptr;
  366. // Run dispatch-specific restart code. Retry until we succeed.
  367. GTM::gtm_restart_reason rr;
  368. while ((rr = disp->begin_or_restart()) != NO_RESTART)
  369. {
  370. tx->decide_retry_strategy(rr);
  371. disp = abi_disp();
  372. }
  373. // Determine the code path to run. Only irrevocable transactions cannot be
  374. // restarted, so all other transactions need to save live variables.
  375. ret = choose_code_path(prop, disp);
  376. if (!(tx->state & STATE_IRREVOCABLE))
  377. ret |= a_saveLiveVariables;
  378. return ret;
  379. }
  380. void
  381. GTM::gtm_transaction_cp::save(gtm_thread* tx)
  382. {
  383. // Save everything that we might have to restore on restarts or aborts.
  384. jb = tx->jb;
  385. undolog_size = tx->undolog.size();
  386. /* FIXME! Assignment of an aatree like alloc_actions is unsafe; if either
  387. *this or *tx is destroyed, the other ends up pointing to a freed node. */
  388. #pragma GCC diagnostic warning "-Wdeprecated-copy"
  389. alloc_actions = tx->alloc_actions;
  390. user_actions_size = tx->user_actions.size();
  391. id = tx->id;
  392. prop = tx->prop;
  393. cxa_catch_count = tx->cxa_catch_count;
  394. cxa_uncaught_count = tx->cxa_uncaught_count;
  395. disp = abi_disp();
  396. nesting = tx->nesting;
  397. }
  398. void
  399. GTM::gtm_transaction_cp::commit(gtm_thread* tx)
  400. {
  401. // Restore state that is not persistent across commits. Exception handling,
  402. // information, nesting level, and any logs do not need to be restored on
  403. // commits of nested transactions. Allocation actions must be committed
  404. // before committing the snapshot.
  405. tx->jb = jb;
  406. tx->alloc_actions = alloc_actions;
  407. tx->id = id;
  408. tx->prop = prop;
  409. }
  410. void
  411. GTM::gtm_thread::rollback (gtm_transaction_cp *cp, bool aborting)
  412. {
  413. // The undo log is special in that it used for both thread-local and shared
  414. // data. Because of the latter, we have to roll it back before any
  415. // dispatch-specific rollback (which handles synchronization with other
  416. // transactions).
  417. undolog.rollback (this, cp ? cp->undolog_size : 0);
  418. // Perform dispatch-specific rollback.
  419. abi_disp()->rollback (cp);
  420. // Roll back all actions that are supposed to happen around the transaction.
  421. rollback_user_actions (cp ? cp->user_actions_size : 0);
  422. commit_allocations (true, (cp ? &cp->alloc_actions : 0));
  423. revert_cpp_exceptions (cp);
  424. if (cp)
  425. {
  426. // We do not yet handle restarts of nested transactions. To do that, we
  427. // would have to restore some state (jb, id, prop, nesting) not to the
  428. // checkpoint but to the transaction that was started from this
  429. // checkpoint (e.g., nesting = cp->nesting + 1);
  430. assert(aborting);
  431. // Roll back the rest of the state to the checkpoint.
  432. jb = cp->jb;
  433. id = cp->id;
  434. prop = cp->prop;
  435. if (cp->disp != abi_disp())
  436. set_abi_disp(cp->disp);
  437. alloc_actions = cp->alloc_actions;
  438. nesting = cp->nesting;
  439. }
  440. else
  441. {
  442. // Roll back to the outermost transaction.
  443. // Restore the jump buffer and transaction properties, which we will
  444. // need for the longjmp used to restart or abort the transaction.
  445. if (parent_txns.size() > 0)
  446. {
  447. jb = parent_txns[0].jb;
  448. id = parent_txns[0].id;
  449. prop = parent_txns[0].prop;
  450. }
  451. // Reset the transaction. Do not reset this->state, which is handled by
  452. // the callers. Note that if we are not aborting, we reset the
  453. // transaction to the point after having executed begin_transaction
  454. // (we will return from it), so the nesting level must be one, not zero.
  455. nesting = (aborting ? 0 : 1);
  456. parent_txns.clear();
  457. }
  458. if (this->eh_in_flight)
  459. {
  460. _Unwind_DeleteException ((_Unwind_Exception *) this->eh_in_flight);
  461. this->eh_in_flight = NULL;
  462. }
  463. }
  464. void ITM_REGPARM
  465. _ITM_abortTransaction (_ITM_abortReason reason)
  466. {
  467. gtm_thread *tx = gtm_thr();
  468. assert (reason == userAbort || reason == (userAbort | outerAbort));
  469. assert ((tx->prop & pr_hasNoAbort) == 0);
  470. if (tx->state & gtm_thread::STATE_IRREVOCABLE)
  471. abort ();
  472. // Roll back to innermost transaction.
  473. if (tx->parent_txns.size() > 0 && !(reason & outerAbort))
  474. {
  475. // If the current method does not support closed nesting but we are
  476. // nested and must only roll back the innermost transaction, then
  477. // restart with a method that supports closed nesting.
  478. abi_dispatch *disp = abi_disp();
  479. if (!disp->closed_nesting())
  480. tx->restart(RESTART_CLOSED_NESTING);
  481. // The innermost transaction is a closed nested transaction.
  482. gtm_transaction_cp *cp = tx->parent_txns.pop();
  483. uint32_t longjmp_prop = tx->prop;
  484. gtm_jmpbuf longjmp_jb = tx->jb;
  485. tx->rollback (cp, true);
  486. // Jump to nested transaction (use the saved jump buffer).
  487. GTM_longjmp (a_abortTransaction | a_restoreLiveVariables,
  488. &longjmp_jb, longjmp_prop);
  489. }
  490. else
  491. {
  492. // There is no nested transaction or an abort of the outermost
  493. // transaction was requested, so roll back to the outermost transaction.
  494. tx->rollback (0, true);
  495. // Aborting an outermost transaction finishes execution of the whole
  496. // transaction. Therefore, reset transaction state.
  497. if (tx->state & gtm_thread::STATE_SERIAL)
  498. gtm_thread::serial_lock.write_unlock ();
  499. else
  500. gtm_thread::serial_lock.read_unlock (tx);
  501. tx->state = 0;
  502. GTM_longjmp (a_abortTransaction | a_restoreLiveVariables,
  503. &tx->jb, tx->prop);
  504. }
  505. }
  506. bool
  507. GTM::gtm_thread::trycommit ()
  508. {
  509. nesting--;
  510. // Skip any real commit for elided transactions.
  511. if (nesting > 0 && (parent_txns.size() == 0 ||
  512. nesting > parent_txns[parent_txns.size() - 1].nesting))
  513. return true;
  514. if (nesting > 0)
  515. {
  516. // Commit of a closed-nested transaction. Remove one checkpoint and add
  517. // any effects of this transaction to the parent transaction.
  518. gtm_transaction_cp *cp = parent_txns.pop();
  519. commit_allocations(false, &cp->alloc_actions);
  520. cp->commit(this);
  521. return true;
  522. }
  523. // Commit of an outermost transaction.
  524. gtm_word priv_time = 0;
  525. if (abi_disp()->trycommit (priv_time))
  526. {
  527. // The transaction is now finished but we will still access some shared
  528. // data if we have to ensure privatization safety.
  529. bool do_read_unlock = false;
  530. if (state & gtm_thread::STATE_SERIAL)
  531. {
  532. gtm_thread::serial_lock.write_unlock ();
  533. // There are no other active transactions, so there's no need to
  534. // enforce privatization safety.
  535. priv_time = 0;
  536. }
  537. else
  538. {
  539. // If we have to ensure privatization safety, we must not yet
  540. // release the read lock and become inactive because (1) we still
  541. // have to go through the list of all transactions, which can be
  542. // modified by serial mode threads, and (2) we interpret each
  543. // transactions' shared_state in the context of what we believe to
  544. // be the current method group (and serial mode transactions can
  545. // change the method group). Therefore, if we have to ensure
  546. // privatization safety, delay becoming inactive but set a maximum
  547. // snapshot time (we have committed and thus have an empty snapshot,
  548. // so it will always be most recent). Use release MO so that this
  549. // synchronizes with other threads observing our snapshot time.
  550. if (priv_time)
  551. {
  552. do_read_unlock = true;
  553. shared_state.store((~(typeof gtm_thread::shared_state)0) - 1,
  554. memory_order_release);
  555. }
  556. else
  557. gtm_thread::serial_lock.read_unlock (this);
  558. }
  559. state = 0;
  560. // We can commit the undo log after dispatch-specific commit and after
  561. // making the transaction inactive because we only have to reset
  562. // gtm_thread state.
  563. undolog.commit ();
  564. // Reset further transaction state.
  565. cxa_catch_count = 0;
  566. restart_total = 0;
  567. // Ensure privatization safety, if necessary.
  568. if (priv_time)
  569. {
  570. // There must be a seq_cst fence between the following loads of the
  571. // other transactions' shared_state and the dispatch-specific stores
  572. // that signal updates by this transaction (e.g., lock
  573. // acquisitions). This ensures that if we read prior to other
  574. // reader transactions setting their shared_state to 0, then those
  575. // readers will observe our updates. We can reuse the seq_cst fence
  576. // in serial_lock.read_unlock() if we performed that; if not, we
  577. // issue the fence.
  578. if (do_read_unlock)
  579. atomic_thread_fence (memory_order_seq_cst);
  580. // TODO Don't just spin but also block using cond vars / futexes
  581. // here. Should probably be integrated with the serial lock code.
  582. for (gtm_thread *it = gtm_thread::list_of_threads; it != 0;
  583. it = it->next_thread)
  584. {
  585. if (it == this) continue;
  586. // We need to load other threads' shared_state using acquire
  587. // semantics (matching the release semantics of the respective
  588. // updates). This is necessary to ensure that the other
  589. // threads' memory accesses happen before our actions that
  590. // assume privatization safety.
  591. // TODO Are there any platform-specific optimizations (e.g.,
  592. // merging barriers)?
  593. while (it->shared_state.load(memory_order_acquire) < priv_time)
  594. cpu_relax();
  595. }
  596. }
  597. // After ensuring privatization safety, we are now truly inactive and
  598. // thus can release the read lock. We will also execute potentially
  599. // privatizing actions (e.g., calling free()). User actions are first.
  600. if (do_read_unlock)
  601. gtm_thread::serial_lock.read_unlock (this);
  602. commit_user_actions ();
  603. commit_allocations (false, 0);
  604. return true;
  605. }
  606. return false;
  607. }
  608. void ITM_NORETURN
  609. GTM::gtm_thread::restart (gtm_restart_reason r, bool finish_serial_upgrade)
  610. {
  611. // Roll back to outermost transaction. Do not reset transaction state because
  612. // we will continue executing this transaction.
  613. rollback ();
  614. // If we have to restart while an upgrade of the serial lock is happening,
  615. // we need to finish this here, after rollback (to ensure privatization
  616. // safety despite undo writes) and before deciding about the retry strategy
  617. // (which could switch to/from serial mode).
  618. if (finish_serial_upgrade)
  619. gtm_thread::serial_lock.write_upgrade_finish(this);
  620. decide_retry_strategy (r);
  621. // Run dispatch-specific restart code. Retry until we succeed.
  622. abi_dispatch* disp = abi_disp();
  623. GTM::gtm_restart_reason rr;
  624. while ((rr = disp->begin_or_restart()) != NO_RESTART)
  625. {
  626. decide_retry_strategy(rr);
  627. disp = abi_disp();
  628. }
  629. GTM_longjmp (choose_code_path(prop, disp) | a_restoreLiveVariables,
  630. &jb, prop);
  631. }
  632. void ITM_REGPARM
  633. _ITM_commitTransaction(void)
  634. {
  635. #if defined(USE_HTM_FASTPATH)
  636. // HTM fastpath. If we are not executing a HW transaction, then we will be
  637. // a serial-mode transaction. If we are, then there will be no other
  638. // concurrent serial-mode transaction.
  639. // See gtm_thread::begin_transaction.
  640. if (likely(!gtm_thread::serial_lock.htm_fastpath_disabled()))
  641. {
  642. htm_commit();
  643. return;
  644. }
  645. #endif
  646. gtm_thread *tx = gtm_thr();
  647. if (!tx->trycommit ())
  648. tx->restart (RESTART_VALIDATE_COMMIT);
  649. }
  650. void ITM_REGPARM
  651. _ITM_commitTransactionEH(void *exc_ptr)
  652. {
  653. #if defined(USE_HTM_FASTPATH)
  654. // See _ITM_commitTransaction.
  655. if (likely(!gtm_thread::serial_lock.htm_fastpath_disabled()))
  656. {
  657. htm_commit();
  658. return;
  659. }
  660. #endif
  661. gtm_thread *tx = gtm_thr();
  662. if (!tx->trycommit ())
  663. {
  664. tx->eh_in_flight = exc_ptr;
  665. tx->restart (RESTART_VALIDATE_COMMIT);
  666. }
  667. }