Daily bump.
[official-gcc.git] / libitm / beginend.cc
blob86f7b39173e2a1312aa582caed463e3bdddc807d
1 /* Copyright (C) 2008-2015 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 gtm_stmlock GTM::gtm_stmlock_array[LOCK_ARRAY_SIZE];
40 atomic<gtm_version> GTM::gtm_clock;
42 /* ??? Move elsewhere when we figure out library initialization. */
43 uint64_t GTM::gtm_spin_count_var = 1000;
45 #ifdef HAVE_64BIT_SYNC_BUILTINS
46 static atomic<_ITM_transactionId_t> global_tid;
47 #else
48 static _ITM_transactionId_t global_tid;
49 static pthread_mutex_t global_tid_lock = PTHREAD_MUTEX_INITIALIZER;
50 #endif
53 // Provides a on-thread-exit callback used to release per-thread data.
54 static pthread_key_t thr_release_key;
55 static pthread_once_t thr_release_once = PTHREAD_ONCE_INIT;
57 // See gtm_thread::begin_transaction.
58 uint32_t GTM::htm_fastpath = 0;
60 /* Allocate a transaction structure. */
61 void *
62 GTM::gtm_thread::operator new (size_t s)
64 void *tx;
66 assert(s == sizeof(gtm_thread));
68 tx = xmalloc (sizeof (gtm_thread), true);
69 memset (tx, 0, sizeof (gtm_thread));
71 return tx;
74 /* Free the given transaction. Raises an error if the transaction is still
75 in use. */
76 void
77 GTM::gtm_thread::operator delete(void *tx)
79 free(tx);
82 static void
83 thread_exit_handler(void *)
85 gtm_thread *thr = gtm_thr();
86 if (thr)
87 delete thr;
88 set_gtm_thr(0);
91 static void
92 thread_exit_init()
94 if (pthread_key_create(&thr_release_key, thread_exit_handler))
95 GTM_fatal("Creating thread release TLS key failed.");
99 GTM::gtm_thread::~gtm_thread()
101 if (nesting > 0)
102 GTM_fatal("Thread exit while a transaction is still active.");
104 // Deregister this transaction.
105 serial_lock.write_lock ();
106 gtm_thread **prev = &list_of_threads;
107 for (; *prev; prev = &(*prev)->next_thread)
109 if (*prev == this)
111 *prev = (*prev)->next_thread;
112 break;
115 number_of_threads--;
116 number_of_threads_changed(number_of_threads + 1, number_of_threads);
117 serial_lock.write_unlock ();
120 GTM::gtm_thread::gtm_thread ()
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.
125 shared_state.store(-1, memory_order_relaxed);
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 ();
135 init_cpp_exceptions ();
137 if (pthread_once(&thr_release_once, thread_exit_init))
138 GTM_fatal("Initializing thread release TLS key failed.");
139 // Any non-null value is sufficient to trigger destruction of this
140 // transaction when the current thread terminates.
141 if (pthread_setspecific(thr_release_key, this))
142 GTM_fatal("Setting thread release TLS key failed.");
145 static inline uint32_t
146 choose_code_path(uint32_t prop, abi_dispatch *disp)
148 if ((prop & pr_uninstrumentedCode) && disp->can_run_uninstrumented_code())
149 return a_runUninstrumentedCode;
150 else
151 return a_runInstrumentedCode;
154 uint32_t
155 GTM::gtm_thread::begin_transaction (uint32_t prop, const gtm_jmpbuf *jb)
157 static const _ITM_transactionId_t tid_block_size = 1 << 16;
159 gtm_thread *tx;
160 abi_dispatch *disp;
161 uint32_t ret;
163 // ??? pr_undoLogCode is not properly defined in the ABI. Are barriers
164 // omitted because they are not necessary (e.g., a transaction on thread-
165 // local data) or because the compiler thinks that some kind of global
166 // synchronization might perform better?
167 if (unlikely(prop & pr_undoLogCode))
168 GTM_fatal("pr_undoLogCode not supported");
170 #ifdef USE_HTM_FASTPATH
171 // HTM fastpath. Only chosen in the absence of transaction_cancel to allow
172 // using an uninstrumented code path.
173 // The fastpath is enabled only by dispatch_htm's method group, which uses
174 // serial-mode methods as fallback. Serial-mode transactions cannot execute
175 // concurrently with HW transactions because the latter monitor the serial
176 // lock's writer flag and thus abort if another thread is or becomes a
177 // serial transaction. Therefore, if the fastpath is enabled, then a
178 // transaction is not executing as a HW transaction iff the serial lock is
179 // write-locked. This allows us to use htm_fastpath and the serial lock's
180 // writer flag to reliable determine whether the current thread runs a HW
181 // transaction, and thus we do not need to maintain this information in
182 // per-thread state.
183 // If an uninstrumented code path is not available, we can still run
184 // instrumented code from a HW transaction because the HTM fastpath kicks
185 // in early in both begin and commit, and the transaction is not canceled.
186 // HW transactions might get requests to switch to serial-irrevocable mode,
187 // but these can be ignored because the HTM provides all necessary
188 // correctness guarantees. Transactions cannot detect whether they are
189 // indeed in serial mode, and HW transactions should never need serial mode
190 // for any internal changes (e.g., they never abort visibly to the STM code
191 // and thus do not trigger the standard retry handling).
192 #ifndef HTM_CUSTOM_FASTPATH
193 if (likely(htm_fastpath && (prop & pr_hasNoAbort)))
195 for (uint32_t t = htm_fastpath; t; t--)
197 uint32_t ret = htm_begin();
198 if (htm_begin_success(ret))
200 // We are executing a transaction now.
201 // Monitor the writer flag in the serial-mode lock, and abort
202 // if there is an active or waiting serial-mode transaction.
203 // Note that this can also happen due to an enclosing
204 // serial-mode transaction; we handle this case below.
205 if (unlikely(serial_lock.is_write_locked()))
206 htm_abort();
207 else
208 // We do not need to set a_saveLiveVariables because of HTM.
209 return (prop & pr_uninstrumentedCode) ?
210 a_runUninstrumentedCode : a_runInstrumentedCode;
212 // The transaction has aborted. Don't retry if it's unlikely that
213 // retrying the transaction will be successful.
214 if (!htm_abort_should_retry(ret))
215 break;
216 // Wait until any concurrent serial-mode transactions have finished.
217 // This is an empty critical section, but won't be elided.
218 if (serial_lock.is_write_locked())
220 tx = gtm_thr();
221 if (unlikely(tx == NULL))
223 // See below.
224 tx = new gtm_thread();
225 set_gtm_thr(tx);
227 // Check whether there is an enclosing serial-mode transaction;
228 // if so, we just continue as a nested transaction and don't
229 // try to use the HTM fastpath. This case can happen when an
230 // outermost relaxed transaction calls unsafe code that starts
231 // a transaction.
232 if (tx->nesting > 0)
233 break;
234 // Another thread is running a serial-mode transaction. Wait.
235 serial_lock.read_lock(tx);
236 serial_lock.read_unlock(tx);
237 // TODO We should probably reset the retry count t here, unless
238 // we have retried so often that we should go serial to avoid
239 // starvation.
243 #else
244 // If we have a custom HTM fastpath in ITM_beginTransaction, we implement
245 // just the retry policy here. We communicate with the custom fastpath
246 // through additional property bits and return codes, and either transfer
247 // control back to the custom fastpath or run the fallback mechanism. The
248 // fastpath synchronization algorithm itself is the same.
249 // pr_HTMRetryableAbort states that a HW transaction started by the custom
250 // HTM fastpath aborted, and that we thus have to decide whether to retry
251 // the fastpath (returning a_tryHTMFastPath) or just proceed with the
252 // fallback method.
253 if (likely(htm_fastpath && (prop & pr_HTMRetryableAbort)))
255 tx = gtm_thr();
256 if (unlikely(tx == NULL))
258 // See below.
259 tx = new gtm_thread();
260 set_gtm_thr(tx);
262 // If this is the first abort, reset the retry count. We abuse
263 // restart_total for the retry count, which is fine because our only
264 // other fallback will use serial transactions, which don't use
265 // restart_total but will reset it when committing.
266 if (!(prop & pr_HTMRetriedAfterAbort))
267 tx->restart_total = htm_fastpath;
269 if (--tx->restart_total > 0)
271 // Wait until any concurrent serial-mode transactions have finished.
272 // Essentially the same code as above.
273 if (serial_lock.is_write_locked())
275 if (tx->nesting > 0)
276 goto stop_custom_htm_fastpath;
277 serial_lock.read_lock(tx);
278 serial_lock.read_unlock(tx);
280 // Let ITM_beginTransaction retry the custom HTM fastpath.
281 return a_tryHTMFastPath;
284 stop_custom_htm_fastpath:
285 #endif
286 #endif
288 tx = gtm_thr();
289 if (unlikely(tx == NULL))
291 // Create the thread object. The constructor will also set up automatic
292 // deletion on thread termination.
293 tx = new gtm_thread();
294 set_gtm_thr(tx);
297 if (tx->nesting > 0)
299 // This is a nested transaction.
300 // Check prop compatibility:
301 // The ABI requires pr_hasNoFloatUpdate, pr_hasNoVectorUpdate,
302 // pr_hasNoIrrevocable, pr_aWBarriersOmitted, pr_RaRBarriersOmitted, and
303 // pr_hasNoSimpleReads to hold for the full dynamic scope of a
304 // transaction. We could check that these are set for the nested
305 // transaction if they are also set for the parent transaction, but the
306 // ABI does not require these flags to be set if they could be set,
307 // so the check could be too strict.
308 // ??? For pr_readOnly, lexical or dynamic scope is unspecified.
310 if (prop & pr_hasNoAbort)
312 // We can use flat nesting, so elide this transaction.
313 if (!(prop & pr_instrumentedCode))
315 if (!(tx->state & STATE_SERIAL) ||
316 !(tx->state & STATE_IRREVOCABLE))
317 tx->serialirr_mode();
319 // Increment nesting level after checking that we have a method that
320 // allows us to continue.
321 tx->nesting++;
322 return choose_code_path(prop, abi_disp());
325 // The transaction might abort, so use closed nesting if possible.
326 // pr_hasNoAbort has lexical scope, so the compiler should really have
327 // generated an instrumented code path.
328 assert(prop & pr_instrumentedCode);
330 // Create a checkpoint of the current transaction.
331 gtm_transaction_cp *cp = tx->parent_txns.push();
332 cp->save(tx);
333 new (&tx->alloc_actions) aa_tree<uintptr_t, gtm_alloc_action>();
335 // Check whether the current method actually supports closed nesting.
336 // If we can switch to another one, do so.
337 // If not, we assume that actual aborts are infrequent, and rather
338 // restart in _ITM_abortTransaction when we really have to.
339 disp = abi_disp();
340 if (!disp->closed_nesting())
342 // ??? Should we elide the transaction if there is no alternative
343 // method that supports closed nesting? If we do, we need to set
344 // some flag to prevent _ITM_abortTransaction from aborting the
345 // wrong transaction (i.e., some parent transaction).
346 abi_dispatch *cn_disp = disp->closed_nesting_alternative();
347 if (cn_disp)
349 disp = cn_disp;
350 set_abi_disp(disp);
354 else
356 // Outermost transaction
357 disp = tx->decide_begin_dispatch (prop);
358 set_abi_disp (disp);
361 // Initialization that is common for outermost and nested transactions.
362 tx->prop = prop;
363 tx->nesting++;
365 tx->jb = *jb;
367 // As long as we have not exhausted a previously allocated block of TIDs,
368 // we can avoid an atomic operation on a shared cacheline.
369 if (tx->local_tid & (tid_block_size - 1))
370 tx->id = tx->local_tid++;
371 else
373 #ifdef HAVE_64BIT_SYNC_BUILTINS
374 // We don't really care which block of TIDs we get but only that we
375 // acquire one atomically; therefore, relaxed memory order is
376 // sufficient.
377 tx->id = global_tid.fetch_add(tid_block_size, memory_order_relaxed);
378 tx->local_tid = tx->id + 1;
379 #else
380 pthread_mutex_lock (&global_tid_lock);
381 global_tid += tid_block_size;
382 tx->id = global_tid;
383 tx->local_tid = tx->id + 1;
384 pthread_mutex_unlock (&global_tid_lock);
385 #endif
388 // Log the number of uncaught exceptions if we might have to roll back this
389 // state.
390 if (tx->cxa_uncaught_count_ptr != 0)
391 tx->cxa_uncaught_count = *tx->cxa_uncaught_count_ptr;
393 // Run dispatch-specific restart code. Retry until we succeed.
394 GTM::gtm_restart_reason rr;
395 while ((rr = disp->begin_or_restart()) != NO_RESTART)
397 tx->decide_retry_strategy(rr);
398 disp = abi_disp();
401 // Determine the code path to run. Only irrevocable transactions cannot be
402 // restarted, so all other transactions need to save live variables.
403 ret = choose_code_path(prop, disp);
404 if (!(tx->state & STATE_IRREVOCABLE))
405 ret |= a_saveLiveVariables;
406 return ret;
410 void
411 GTM::gtm_transaction_cp::save(gtm_thread* tx)
413 // Save everything that we might have to restore on restarts or aborts.
414 jb = tx->jb;
415 undolog_size = tx->undolog.size();
416 memcpy(&alloc_actions, &tx->alloc_actions, sizeof(alloc_actions));
417 user_actions_size = tx->user_actions.size();
418 id = tx->id;
419 prop = tx->prop;
420 cxa_catch_count = tx->cxa_catch_count;
421 cxa_uncaught_count = tx->cxa_uncaught_count;
422 disp = abi_disp();
423 nesting = tx->nesting;
426 void
427 GTM::gtm_transaction_cp::commit(gtm_thread* tx)
429 // Restore state that is not persistent across commits. Exception handling,
430 // information, nesting level, and any logs do not need to be restored on
431 // commits of nested transactions. Allocation actions must be committed
432 // before committing the snapshot.
433 tx->jb = jb;
434 memcpy(&tx->alloc_actions, &alloc_actions, sizeof(alloc_actions));
435 tx->id = id;
436 tx->prop = prop;
440 void
441 GTM::gtm_thread::rollback (gtm_transaction_cp *cp, bool aborting)
443 // The undo log is special in that it used for both thread-local and shared
444 // data. Because of the latter, we have to roll it back before any
445 // dispatch-specific rollback (which handles synchronization with other
446 // transactions).
447 undolog.rollback (this, cp ? cp->undolog_size : 0);
449 // Perform dispatch-specific rollback.
450 abi_disp()->rollback (cp);
452 // Roll back all actions that are supposed to happen around the transaction.
453 rollback_user_actions (cp ? cp->user_actions_size : 0);
454 commit_allocations (true, (cp ? &cp->alloc_actions : 0));
455 revert_cpp_exceptions (cp);
457 if (cp)
459 // We do not yet handle restarts of nested transactions. To do that, we
460 // would have to restore some state (jb, id, prop, nesting) not to the
461 // checkpoint but to the transaction that was started from this
462 // checkpoint (e.g., nesting = cp->nesting + 1);
463 assert(aborting);
464 // Roll back the rest of the state to the checkpoint.
465 jb = cp->jb;
466 id = cp->id;
467 prop = cp->prop;
468 if (cp->disp != abi_disp())
469 set_abi_disp(cp->disp);
470 memcpy(&alloc_actions, &cp->alloc_actions, sizeof(alloc_actions));
471 nesting = cp->nesting;
473 else
475 // Roll back to the outermost transaction.
476 // Restore the jump buffer and transaction properties, which we will
477 // need for the longjmp used to restart or abort the transaction.
478 if (parent_txns.size() > 0)
480 jb = parent_txns[0].jb;
481 id = parent_txns[0].id;
482 prop = parent_txns[0].prop;
484 // Reset the transaction. Do not reset this->state, which is handled by
485 // the callers. Note that if we are not aborting, we reset the
486 // transaction to the point after having executed begin_transaction
487 // (we will return from it), so the nesting level must be one, not zero.
488 nesting = (aborting ? 0 : 1);
489 parent_txns.clear();
492 if (this->eh_in_flight)
494 _Unwind_DeleteException ((_Unwind_Exception *) this->eh_in_flight);
495 this->eh_in_flight = NULL;
499 void ITM_REGPARM
500 _ITM_abortTransaction (_ITM_abortReason reason)
502 gtm_thread *tx = gtm_thr();
504 assert (reason == userAbort || reason == (userAbort | outerAbort));
505 assert ((tx->prop & pr_hasNoAbort) == 0);
507 if (tx->state & gtm_thread::STATE_IRREVOCABLE)
508 abort ();
510 // Roll back to innermost transaction.
511 if (tx->parent_txns.size() > 0 && !(reason & outerAbort))
513 // If the current method does not support closed nesting but we are
514 // nested and must only roll back the innermost transaction, then
515 // restart with a method that supports closed nesting.
516 abi_dispatch *disp = abi_disp();
517 if (!disp->closed_nesting())
518 tx->restart(RESTART_CLOSED_NESTING);
520 // The innermost transaction is a closed nested transaction.
521 gtm_transaction_cp *cp = tx->parent_txns.pop();
522 uint32_t longjmp_prop = tx->prop;
523 gtm_jmpbuf longjmp_jb = tx->jb;
525 tx->rollback (cp, true);
527 // Jump to nested transaction (use the saved jump buffer).
528 GTM_longjmp (a_abortTransaction | a_restoreLiveVariables,
529 &longjmp_jb, longjmp_prop);
531 else
533 // There is no nested transaction or an abort of the outermost
534 // transaction was requested, so roll back to the outermost transaction.
535 tx->rollback (0, true);
537 // Aborting an outermost transaction finishes execution of the whole
538 // transaction. Therefore, reset transaction state.
539 if (tx->state & gtm_thread::STATE_SERIAL)
540 gtm_thread::serial_lock.write_unlock ();
541 else
542 gtm_thread::serial_lock.read_unlock (tx);
543 tx->state = 0;
545 GTM_longjmp (a_abortTransaction | a_restoreLiveVariables,
546 &tx->jb, tx->prop);
550 bool
551 GTM::gtm_thread::trycommit ()
553 nesting--;
555 // Skip any real commit for elided transactions.
556 if (nesting > 0 && (parent_txns.size() == 0 ||
557 nesting > parent_txns[parent_txns.size() - 1].nesting))
558 return true;
560 if (nesting > 0)
562 // Commit of a closed-nested transaction. Remove one checkpoint and add
563 // any effects of this transaction to the parent transaction.
564 gtm_transaction_cp *cp = parent_txns.pop();
565 commit_allocations(false, &cp->alloc_actions);
566 cp->commit(this);
567 return true;
570 // Commit of an outermost transaction.
571 gtm_word priv_time = 0;
572 if (abi_disp()->trycommit (priv_time))
574 // The transaction is now inactive. Everything that we still have to do
575 // will not synchronize with other transactions anymore.
576 if (state & gtm_thread::STATE_SERIAL)
578 gtm_thread::serial_lock.write_unlock ();
579 // There are no other active transactions, so there's no need to
580 // enforce privatization safety.
581 priv_time = 0;
583 else
584 gtm_thread::serial_lock.read_unlock (this);
585 state = 0;
587 // We can commit the undo log after dispatch-specific commit and after
588 // making the transaction inactive because we only have to reset
589 // gtm_thread state.
590 undolog.commit ();
591 // Reset further transaction state.
592 cxa_catch_count = 0;
593 restart_total = 0;
595 // Ensure privatization safety, if necessary.
596 if (priv_time)
598 // There must be a seq_cst fence between the following loads of the
599 // other transactions' shared_state and the dispatch-specific stores
600 // that signal updates by this transaction (e.g., lock
601 // acquisitions). This ensures that if we read prior to other
602 // reader transactions setting their shared_state to 0, then those
603 // readers will observe our updates. We can reuse the seq_cst fence
604 // in serial_lock.read_unlock() however, so we don't need another
605 // one here.
606 // TODO Don't just spin but also block using cond vars / futexes
607 // here. Should probably be integrated with the serial lock code.
608 for (gtm_thread *it = gtm_thread::list_of_threads; it != 0;
609 it = it->next_thread)
611 if (it == this) continue;
612 // We need to load other threads' shared_state using acquire
613 // semantics (matching the release semantics of the respective
614 // updates). This is necessary to ensure that the other
615 // threads' memory accesses happen before our actions that
616 // assume privatization safety.
617 // TODO Are there any platform-specific optimizations (e.g.,
618 // merging barriers)?
619 while (it->shared_state.load(memory_order_acquire) < priv_time)
620 cpu_relax();
624 // After ensuring privatization safety, we execute potentially
625 // privatizing actions (e.g., calling free()). User actions are first.
626 commit_user_actions ();
627 commit_allocations (false, 0);
629 return true;
631 return false;
634 void ITM_NORETURN
635 GTM::gtm_thread::restart (gtm_restart_reason r, bool finish_serial_upgrade)
637 // Roll back to outermost transaction. Do not reset transaction state because
638 // we will continue executing this transaction.
639 rollback ();
641 // If we have to restart while an upgrade of the serial lock is happening,
642 // we need to finish this here, after rollback (to ensure privatization
643 // safety despite undo writes) and before deciding about the retry strategy
644 // (which could switch to/from serial mode).
645 if (finish_serial_upgrade)
646 gtm_thread::serial_lock.write_upgrade_finish(this);
648 decide_retry_strategy (r);
650 // Run dispatch-specific restart code. Retry until we succeed.
651 abi_dispatch* disp = abi_disp();
652 GTM::gtm_restart_reason rr;
653 while ((rr = disp->begin_or_restart()) != NO_RESTART)
655 decide_retry_strategy(rr);
656 disp = abi_disp();
659 GTM_longjmp (choose_code_path(prop, disp) | a_restoreLiveVariables,
660 &jb, prop);
663 void ITM_REGPARM
664 _ITM_commitTransaction(void)
666 #if defined(USE_HTM_FASTPATH)
667 // HTM fastpath. If we are not executing a HW transaction, then we will be
668 // a serial-mode transaction. If we are, then there will be no other
669 // concurrent serial-mode transaction.
670 // See gtm_thread::begin_transaction.
671 if (likely(htm_fastpath && !gtm_thread::serial_lock.is_write_locked()))
673 htm_commit();
674 return;
676 #endif
677 gtm_thread *tx = gtm_thr();
678 if (!tx->trycommit ())
679 tx->restart (RESTART_VALIDATE_COMMIT);
682 void ITM_REGPARM
683 _ITM_commitTransactionEH(void *exc_ptr)
685 #if defined(USE_HTM_FASTPATH)
686 // See _ITM_commitTransaction.
687 if (likely(htm_fastpath && !gtm_thread::serial_lock.is_write_locked()))
689 htm_commit();
690 return;
692 #endif
693 gtm_thread *tx = gtm_thr();
694 if (!tx->trycommit ())
696 tx->eh_in_flight = exc_ptr;
697 tx->restart (RESTART_VALIDATE_COMMIT);