beginend.cc 23 KB

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