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