PR tree-optimization/68911
[official-gcc.git] / libitm / beginend.cc
blobc801dab005bcaad37877bfca47c91a93f202d37b
1 /* Copyright (C) 2008-2016 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU Transactional Memory Library (libitm).
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.
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.
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.
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/>. */
25 #include "libitm_i.h"
26 #include <pthread.h>
29 using namespace GTM;
31 #if !defined(HAVE_ARCH_GTM_THREAD) || !defined(HAVE_ARCH_GTM_THREAD_DISP)
32 extern __thread gtm_thread_tls _gtm_thr_tls;
33 #endif
35 gtm_rwlock GTM::gtm_thread::serial_lock;
36 gtm_thread *GTM::gtm_thread::list_of_threads = 0;
37 unsigned GTM::gtm_thread::number_of_threads = 0;
39 /* ??? Move elsewhere when we figure out library initialization. */
40 uint64_t GTM::gtm_spin_count_var = 1000;
42 #ifdef HAVE_64BIT_SYNC_BUILTINS
43 static atomic<_ITM_transactionId_t> global_tid;
44 #else
45 static _ITM_transactionId_t global_tid;
46 static pthread_mutex_t global_tid_lock = PTHREAD_MUTEX_INITIALIZER;
47 #endif
50 // Provides a on-thread-exit callback used to release per-thread data.
51 static pthread_key_t thr_release_key;
52 static pthread_once_t thr_release_once = PTHREAD_ONCE_INIT;
54 // See gtm_thread::begin_transaction.
55 uint32_t GTM::htm_fastpath = 0;
57 /* Allocate a transaction structure. */
58 void *
59 GTM::gtm_thread::operator new (size_t s)
61 void *tx;
63 assert(s == sizeof(gtm_thread));
65 tx = xmalloc (sizeof (gtm_thread), true);
66 memset (tx, 0, sizeof (gtm_thread));
68 return tx;
71 /* Free the given transaction. Raises an error if the transaction is still
72 in use. */
73 void
74 GTM::gtm_thread::operator delete(void *tx)
76 free(tx);
79 static void
80 thread_exit_handler(void *)
82 gtm_thread *thr = gtm_thr();
83 if (thr)
84 delete thr;
85 set_gtm_thr(0);
88 static void
89 thread_exit_init()
91 if (pthread_key_create(&thr_release_key, thread_exit_handler))
92 GTM_fatal("Creating thread release TLS key failed.");
96 GTM::gtm_thread::~gtm_thread()
98 if (nesting > 0)
99 GTM_fatal("Thread exit while a transaction is still active.");
101 // Deregister this transaction.
102 serial_lock.write_lock ();
103 gtm_thread **prev = &list_of_threads;
104 for (; *prev; prev = &(*prev)->next_thread)
106 if (*prev == this)
108 *prev = (*prev)->next_thread;
109 break;
112 number_of_threads--;
113 number_of_threads_changed(number_of_threads + 1, number_of_threads);
114 serial_lock.write_unlock ();
117 GTM::gtm_thread::gtm_thread ()
119 // This object's memory has been set to zero by operator new, so no need
120 // to initialize any of the other primitive-type members that do not have
121 // constructors.
122 shared_state.store(-1, memory_order_relaxed);
124 // Register this transaction with the list of all threads' transactions.
125 serial_lock.write_lock ();
126 next_thread = list_of_threads;
127 list_of_threads = this;
128 number_of_threads++;
129 number_of_threads_changed(number_of_threads - 1, number_of_threads);
130 serial_lock.write_unlock ();
132 init_cpp_exceptions ();
134 if (pthread_once(&thr_release_once, thread_exit_init))
135 GTM_fatal("Initializing thread release TLS key failed.");
136 // Any non-null value is sufficient to trigger destruction of this
137 // transaction when the current thread terminates.
138 if (pthread_setspecific(thr_release_key, this))
139 GTM_fatal("Setting thread release TLS key failed.");
142 static inline uint32_t
143 choose_code_path(uint32_t prop, abi_dispatch *disp)
145 if ((prop & pr_uninstrumentedCode) && disp->can_run_uninstrumented_code())
146 return a_runUninstrumentedCode;
147 else
148 return a_runInstrumentedCode;
151 uint32_t
152 GTM::gtm_thread::begin_transaction (uint32_t prop, const gtm_jmpbuf *jb)
154 static const _ITM_transactionId_t tid_block_size = 1 << 16;
156 gtm_thread *tx;
157 abi_dispatch *disp;
158 uint32_t ret;
160 // ??? pr_undoLogCode is not properly defined in the ABI. Are barriers
161 // omitted because they are not necessary (e.g., a transaction on thread-
162 // local data) or because the compiler thinks that some kind of global
163 // synchronization might perform better?
164 if (unlikely(prop & pr_undoLogCode))
165 GTM_fatal("pr_undoLogCode not supported");
167 #ifdef USE_HTM_FASTPATH
168 // HTM fastpath. Only chosen in the absence of transaction_cancel to allow
169 // using an uninstrumented code path.
170 // The fastpath is enabled only by dispatch_htm's method group, which uses
171 // serial-mode methods as fallback. Serial-mode transactions cannot execute
172 // concurrently with HW transactions because the latter monitor the serial
173 // lock's writer flag and thus abort if another thread is or becomes a
174 // serial transaction. Therefore, if the fastpath is enabled, then a
175 // transaction is not executing as a HW transaction iff the serial lock is
176 // write-locked. This allows us to use htm_fastpath and the serial lock's
177 // writer flag to reliable determine whether the current thread runs a HW
178 // transaction, and thus we do not need to maintain this information in
179 // per-thread state.
180 // If an uninstrumented code path is not available, we can still run
181 // instrumented code from a HW transaction because the HTM fastpath kicks
182 // in early in both begin and commit, and the transaction is not canceled.
183 // HW transactions might get requests to switch to serial-irrevocable mode,
184 // but these can be ignored because the HTM provides all necessary
185 // correctness guarantees. Transactions cannot detect whether they are
186 // indeed in serial mode, and HW transactions should never need serial mode
187 // for any internal changes (e.g., they never abort visibly to the STM code
188 // and thus do not trigger the standard retry handling).
189 #ifndef HTM_CUSTOM_FASTPATH
190 if (likely(htm_fastpath && (prop & pr_hasNoAbort)))
192 for (uint32_t t = htm_fastpath; t; t--)
194 uint32_t ret = htm_begin();
195 if (htm_begin_success(ret))
197 // We are executing a transaction now.
198 // Monitor the writer flag in the serial-mode lock, and abort
199 // if there is an active or waiting serial-mode transaction.
200 // Note that this can also happen due to an enclosing
201 // serial-mode transaction; we handle this case below.
202 if (unlikely(serial_lock.is_write_locked()))
203 htm_abort();
204 else
205 // We do not need to set a_saveLiveVariables because of HTM.
206 return (prop & pr_uninstrumentedCode) ?
207 a_runUninstrumentedCode : a_runInstrumentedCode;
209 // The transaction has aborted. Don't retry if it's unlikely that
210 // retrying the transaction will be successful.
211 if (!htm_abort_should_retry(ret))
212 break;
213 // Wait until any concurrent serial-mode transactions have finished.
214 // This is an empty critical section, but won't be elided.
215 if (serial_lock.is_write_locked())
217 tx = gtm_thr();
218 if (unlikely(tx == NULL))
220 // See below.
221 tx = new gtm_thread();
222 set_gtm_thr(tx);
224 // Check whether there is an enclosing serial-mode transaction;
225 // if so, we just continue as a nested transaction and don't
226 // try to use the HTM fastpath. This case can happen when an
227 // outermost relaxed transaction calls unsafe code that starts
228 // a transaction.
229 if (tx->nesting > 0)
230 break;
231 // Another thread is running a serial-mode transaction. Wait.
232 serial_lock.read_lock(tx);
233 serial_lock.read_unlock(tx);
234 // TODO We should probably reset the retry count t here, unless
235 // we have retried so often that we should go serial to avoid
236 // starvation.
240 #else
241 // If we have a custom HTM fastpath in ITM_beginTransaction, we implement
242 // just the retry policy here. We communicate with the custom fastpath
243 // through additional property bits and return codes, and either transfer
244 // control back to the custom fastpath or run the fallback mechanism. The
245 // fastpath synchronization algorithm itself is the same.
246 // pr_HTMRetryableAbort states that a HW transaction started by the custom
247 // HTM fastpath aborted, and that we thus have to decide whether to retry
248 // the fastpath (returning a_tryHTMFastPath) or just proceed with the
249 // fallback method.
250 if (likely(htm_fastpath && (prop & pr_HTMRetryableAbort)))
252 tx = gtm_thr();
253 if (unlikely(tx == NULL))
255 // See below.
256 tx = new gtm_thread();
257 set_gtm_thr(tx);
259 // If this is the first abort, reset the retry count. We abuse
260 // restart_total for the retry count, which is fine because our only
261 // other fallback will use serial transactions, which don't use
262 // restart_total but will reset it when committing.
263 if (!(prop & pr_HTMRetriedAfterAbort))
264 tx->restart_total = htm_fastpath;
266 if (--tx->restart_total > 0)
268 // Wait until any concurrent serial-mode transactions have finished.
269 // Essentially the same code as above.
270 if (serial_lock.is_write_locked())
272 if (tx->nesting > 0)
273 goto stop_custom_htm_fastpath;
274 serial_lock.read_lock(tx);
275 serial_lock.read_unlock(tx);
277 // Let ITM_beginTransaction retry the custom HTM fastpath.
278 return a_tryHTMFastPath;
281 stop_custom_htm_fastpath:
282 #endif
283 #endif
285 tx = gtm_thr();
286 if (unlikely(tx == NULL))
288 // Create the thread object. The constructor will also set up automatic
289 // deletion on thread termination.
290 tx = new gtm_thread();
291 set_gtm_thr(tx);
294 if (tx->nesting > 0)
296 // This is a nested transaction.
297 // Check prop compatibility:
298 // The ABI requires pr_hasNoFloatUpdate, pr_hasNoVectorUpdate,
299 // pr_hasNoIrrevocable, pr_aWBarriersOmitted, pr_RaRBarriersOmitted, and
300 // pr_hasNoSimpleReads to hold for the full dynamic scope of a
301 // transaction. We could check that these are set for the nested
302 // transaction if they are also set for the parent transaction, but the
303 // ABI does not require these flags to be set if they could be set,
304 // so the check could be too strict.
305 // ??? For pr_readOnly, lexical or dynamic scope is unspecified.
307 if (prop & pr_hasNoAbort)
309 // We can use flat nesting, so elide this transaction.
310 if (!(prop & pr_instrumentedCode))
312 if (!(tx->state & STATE_SERIAL) ||
313 !(tx->state & STATE_IRREVOCABLE))
314 tx->serialirr_mode();
316 // Increment nesting level after checking that we have a method that
317 // allows us to continue.
318 tx->nesting++;
319 return choose_code_path(prop, abi_disp());
322 // The transaction might abort, so use closed nesting if possible.
323 // pr_hasNoAbort has lexical scope, so the compiler should really have
324 // generated an instrumented code path.
325 assert(prop & pr_instrumentedCode);
327 // Create a checkpoint of the current transaction.
328 gtm_transaction_cp *cp = tx->parent_txns.push();
329 cp->save(tx);
330 new (&tx->alloc_actions) aa_tree<uintptr_t, gtm_alloc_action>();
332 // Check whether the current method actually supports closed nesting.
333 // If we can switch to another one, do so.
334 // If not, we assume that actual aborts are infrequent, and rather
335 // restart in _ITM_abortTransaction when we really have to.
336 disp = abi_disp();
337 if (!disp->closed_nesting())
339 // ??? Should we elide the transaction if there is no alternative
340 // method that supports closed nesting? If we do, we need to set
341 // some flag to prevent _ITM_abortTransaction from aborting the
342 // wrong transaction (i.e., some parent transaction).
343 abi_dispatch *cn_disp = disp->closed_nesting_alternative();
344 if (cn_disp)
346 disp = cn_disp;
347 set_abi_disp(disp);
351 else
353 // Outermost transaction
354 disp = tx->decide_begin_dispatch (prop);
355 set_abi_disp (disp);
358 // Initialization that is common for outermost and nested transactions.
359 tx->prop = prop;
360 tx->nesting++;
362 tx->jb = *jb;
364 // As long as we have not exhausted a previously allocated block of TIDs,
365 // we can avoid an atomic operation on a shared cacheline.
366 if (tx->local_tid & (tid_block_size - 1))
367 tx->id = tx->local_tid++;
368 else
370 #ifdef HAVE_64BIT_SYNC_BUILTINS
371 // We don't really care which block of TIDs we get but only that we
372 // acquire one atomically; therefore, relaxed memory order is
373 // sufficient.
374 tx->id = global_tid.fetch_add(tid_block_size, memory_order_relaxed);
375 tx->local_tid = tx->id + 1;
376 #else
377 pthread_mutex_lock (&global_tid_lock);
378 global_tid += tid_block_size;
379 tx->id = global_tid;
380 tx->local_tid = tx->id + 1;
381 pthread_mutex_unlock (&global_tid_lock);
382 #endif
385 // Log the number of uncaught exceptions if we might have to roll back this
386 // state.
387 if (tx->cxa_uncaught_count_ptr != 0)
388 tx->cxa_uncaught_count = *tx->cxa_uncaught_count_ptr;
390 // Run dispatch-specific restart code. Retry until we succeed.
391 GTM::gtm_restart_reason rr;
392 while ((rr = disp->begin_or_restart()) != NO_RESTART)
394 tx->decide_retry_strategy(rr);
395 disp = abi_disp();
398 // Determine the code path to run. Only irrevocable transactions cannot be
399 // restarted, so all other transactions need to save live variables.
400 ret = choose_code_path(prop, disp);
401 if (!(tx->state & STATE_IRREVOCABLE))
402 ret |= a_saveLiveVariables;
403 return ret;
407 void
408 GTM::gtm_transaction_cp::save(gtm_thread* tx)
410 // Save everything that we might have to restore on restarts or aborts.
411 jb = tx->jb;
412 undolog_size = tx->undolog.size();
413 memcpy(&alloc_actions, &tx->alloc_actions, sizeof(alloc_actions));
414 user_actions_size = tx->user_actions.size();
415 id = tx->id;
416 prop = tx->prop;
417 cxa_catch_count = tx->cxa_catch_count;
418 cxa_uncaught_count = tx->cxa_uncaught_count;
419 disp = abi_disp();
420 nesting = tx->nesting;
423 void
424 GTM::gtm_transaction_cp::commit(gtm_thread* tx)
426 // Restore state that is not persistent across commits. Exception handling,
427 // information, nesting level, and any logs do not need to be restored on
428 // commits of nested transactions. Allocation actions must be committed
429 // before committing the snapshot.
430 tx->jb = jb;
431 memcpy(&tx->alloc_actions, &alloc_actions, sizeof(alloc_actions));
432 tx->id = id;
433 tx->prop = prop;
437 void
438 GTM::gtm_thread::rollback (gtm_transaction_cp *cp, bool aborting)
440 // The undo log is special in that it used for both thread-local and shared
441 // data. Because of the latter, we have to roll it back before any
442 // dispatch-specific rollback (which handles synchronization with other
443 // transactions).
444 undolog.rollback (this, cp ? cp->undolog_size : 0);
446 // Perform dispatch-specific rollback.
447 abi_disp()->rollback (cp);
449 // Roll back all actions that are supposed to happen around the transaction.
450 rollback_user_actions (cp ? cp->user_actions_size : 0);
451 commit_allocations (true, (cp ? &cp->alloc_actions : 0));
452 revert_cpp_exceptions (cp);
454 if (cp)
456 // We do not yet handle restarts of nested transactions. To do that, we
457 // would have to restore some state (jb, id, prop, nesting) not to the
458 // checkpoint but to the transaction that was started from this
459 // checkpoint (e.g., nesting = cp->nesting + 1);
460 assert(aborting);
461 // Roll back the rest of the state to the checkpoint.
462 jb = cp->jb;
463 id = cp->id;
464 prop = cp->prop;
465 if (cp->disp != abi_disp())
466 set_abi_disp(cp->disp);
467 memcpy(&alloc_actions, &cp->alloc_actions, sizeof(alloc_actions));
468 nesting = cp->nesting;
470 else
472 // Roll back to the outermost transaction.
473 // Restore the jump buffer and transaction properties, which we will
474 // need for the longjmp used to restart or abort the transaction.
475 if (parent_txns.size() > 0)
477 jb = parent_txns[0].jb;
478 id = parent_txns[0].id;
479 prop = parent_txns[0].prop;
481 // Reset the transaction. Do not reset this->state, which is handled by
482 // the callers. Note that if we are not aborting, we reset the
483 // transaction to the point after having executed begin_transaction
484 // (we will return from it), so the nesting level must be one, not zero.
485 nesting = (aborting ? 0 : 1);
486 parent_txns.clear();
489 if (this->eh_in_flight)
491 _Unwind_DeleteException ((_Unwind_Exception *) this->eh_in_flight);
492 this->eh_in_flight = NULL;
496 void ITM_REGPARM
497 _ITM_abortTransaction (_ITM_abortReason reason)
499 gtm_thread *tx = gtm_thr();
501 assert (reason == userAbort || reason == (userAbort | outerAbort));
502 assert ((tx->prop & pr_hasNoAbort) == 0);
504 if (tx->state & gtm_thread::STATE_IRREVOCABLE)
505 abort ();
507 // Roll back to innermost transaction.
508 if (tx->parent_txns.size() > 0 && !(reason & outerAbort))
510 // If the current method does not support closed nesting but we are
511 // nested and must only roll back the innermost transaction, then
512 // restart with a method that supports closed nesting.
513 abi_dispatch *disp = abi_disp();
514 if (!disp->closed_nesting())
515 tx->restart(RESTART_CLOSED_NESTING);
517 // The innermost transaction is a closed nested transaction.
518 gtm_transaction_cp *cp = tx->parent_txns.pop();
519 uint32_t longjmp_prop = tx->prop;
520 gtm_jmpbuf longjmp_jb = tx->jb;
522 tx->rollback (cp, true);
524 // Jump to nested transaction (use the saved jump buffer).
525 GTM_longjmp (a_abortTransaction | a_restoreLiveVariables,
526 &longjmp_jb, longjmp_prop);
528 else
530 // There is no nested transaction or an abort of the outermost
531 // transaction was requested, so roll back to the outermost transaction.
532 tx->rollback (0, true);
534 // Aborting an outermost transaction finishes execution of the whole
535 // transaction. Therefore, reset transaction state.
536 if (tx->state & gtm_thread::STATE_SERIAL)
537 gtm_thread::serial_lock.write_unlock ();
538 else
539 gtm_thread::serial_lock.read_unlock (tx);
540 tx->state = 0;
542 GTM_longjmp (a_abortTransaction | a_restoreLiveVariables,
543 &tx->jb, tx->prop);
547 bool
548 GTM::gtm_thread::trycommit ()
550 nesting--;
552 // Skip any real commit for elided transactions.
553 if (nesting > 0 && (parent_txns.size() == 0 ||
554 nesting > parent_txns[parent_txns.size() - 1].nesting))
555 return true;
557 if (nesting > 0)
559 // Commit of a closed-nested transaction. Remove one checkpoint and add
560 // any effects of this transaction to the parent transaction.
561 gtm_transaction_cp *cp = parent_txns.pop();
562 commit_allocations(false, &cp->alloc_actions);
563 cp->commit(this);
564 return true;
567 // Commit of an outermost transaction.
568 gtm_word priv_time = 0;
569 if (abi_disp()->trycommit (priv_time))
571 // The transaction is now inactive. Everything that we still have to do
572 // will not synchronize with other transactions anymore.
573 if (state & gtm_thread::STATE_SERIAL)
575 gtm_thread::serial_lock.write_unlock ();
576 // There are no other active transactions, so there's no need to
577 // enforce privatization safety.
578 priv_time = 0;
580 else
581 gtm_thread::serial_lock.read_unlock (this);
582 state = 0;
584 // We can commit the undo log after dispatch-specific commit and after
585 // making the transaction inactive because we only have to reset
586 // gtm_thread state.
587 undolog.commit ();
588 // Reset further transaction state.
589 cxa_catch_count = 0;
590 restart_total = 0;
592 // Ensure privatization safety, if necessary.
593 if (priv_time)
595 // There must be a seq_cst fence between the following loads of the
596 // other transactions' shared_state and the dispatch-specific stores
597 // that signal updates by this transaction (e.g., lock
598 // acquisitions). This ensures that if we read prior to other
599 // reader transactions setting their shared_state to 0, then those
600 // readers will observe our updates. We can reuse the seq_cst fence
601 // in serial_lock.read_unlock() however, so we don't need another
602 // one here.
603 // TODO Don't just spin but also block using cond vars / futexes
604 // here. Should probably be integrated with the serial lock code.
605 for (gtm_thread *it = gtm_thread::list_of_threads; it != 0;
606 it = it->next_thread)
608 if (it == this) continue;
609 // We need to load other threads' shared_state using acquire
610 // semantics (matching the release semantics of the respective
611 // updates). This is necessary to ensure that the other
612 // threads' memory accesses happen before our actions that
613 // assume privatization safety.
614 // TODO Are there any platform-specific optimizations (e.g.,
615 // merging barriers)?
616 while (it->shared_state.load(memory_order_acquire) < priv_time)
617 cpu_relax();
621 // After ensuring privatization safety, we execute potentially
622 // privatizing actions (e.g., calling free()). User actions are first.
623 commit_user_actions ();
624 commit_allocations (false, 0);
626 return true;
628 return false;
631 void ITM_NORETURN
632 GTM::gtm_thread::restart (gtm_restart_reason r, bool finish_serial_upgrade)
634 // Roll back to outermost transaction. Do not reset transaction state because
635 // we will continue executing this transaction.
636 rollback ();
638 // If we have to restart while an upgrade of the serial lock is happening,
639 // we need to finish this here, after rollback (to ensure privatization
640 // safety despite undo writes) and before deciding about the retry strategy
641 // (which could switch to/from serial mode).
642 if (finish_serial_upgrade)
643 gtm_thread::serial_lock.write_upgrade_finish(this);
645 decide_retry_strategy (r);
647 // Run dispatch-specific restart code. Retry until we succeed.
648 abi_dispatch* disp = abi_disp();
649 GTM::gtm_restart_reason rr;
650 while ((rr = disp->begin_or_restart()) != NO_RESTART)
652 decide_retry_strategy(rr);
653 disp = abi_disp();
656 GTM_longjmp (choose_code_path(prop, disp) | a_restoreLiveVariables,
657 &jb, prop);
660 void ITM_REGPARM
661 _ITM_commitTransaction(void)
663 #if defined(USE_HTM_FASTPATH)
664 // HTM fastpath. If we are not executing a HW transaction, then we will be
665 // a serial-mode transaction. If we are, then there will be no other
666 // concurrent serial-mode transaction.
667 // See gtm_thread::begin_transaction.
668 if (likely(htm_fastpath && !gtm_thread::serial_lock.is_write_locked()))
670 htm_commit();
671 return;
673 #endif
674 gtm_thread *tx = gtm_thr();
675 if (!tx->trycommit ())
676 tx->restart (RESTART_VALIDATE_COMMIT);
679 void ITM_REGPARM
680 _ITM_commitTransactionEH(void *exc_ptr)
682 #if defined(USE_HTM_FASTPATH)
683 // See _ITM_commitTransaction.
684 if (likely(htm_fastpath && !gtm_thread::serial_lock.is_write_locked()))
686 htm_commit();
687 return;
689 #endif
690 gtm_thread *tx = gtm_thr();
691 if (!tx->trycommit ())
693 tx->eh_in_flight = exc_ptr;
694 tx->restart (RESTART_VALIDATE_COMMIT);