2013-09-09 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libitm / beginend.cc
blob93e702efc9eb53c155608e4f75c96f7fa79259cb
1 /* Copyright (C) 2008-2013 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 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.");
143 static inline uint32_t
144 choose_code_path(uint32_t prop, abi_dispatch *disp)
146 if ((prop & pr_uninstrumentedCode) && disp->can_run_uninstrumented_code())
147 return a_runUninstrumentedCode;
148 else
149 return a_runInstrumentedCode;
152 uint32_t
153 GTM::gtm_thread::begin_transaction (uint32_t prop, const gtm_jmpbuf *jb)
155 static const _ITM_transactionId_t tid_block_size = 1 << 16;
157 gtm_thread *tx;
158 abi_dispatch *disp;
159 uint32_t ret;
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");
168 #if defined(USE_HTM_FASTPATH) && !defined(HTM_CUSTOM_FASTPATH)
169 // 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).
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 if (unlikely(serial_lock.is_write_locked()))
201 htm_abort();
202 else
203 // We do not need to set a_saveLiveVariables because of HTM.
204 return (prop & pr_uninstrumentedCode) ?
205 a_runUninstrumentedCode : a_runInstrumentedCode;
207 // The transaction has aborted. Don't retry if it's unlikely that
208 // retrying the transaction will be successful.
209 if (!htm_abort_should_retry(ret))
210 break;
211 // Wait until any concurrent serial-mode transactions have finished.
212 // This is an empty critical section, but won't be elided.
213 if (serial_lock.is_write_locked())
215 tx = gtm_thr();
216 if (unlikely(tx == NULL))
218 // See below.
219 tx = new gtm_thread();
220 set_gtm_thr(tx);
222 serial_lock.read_lock(tx);
223 serial_lock.read_unlock(tx);
224 // TODO We should probably reset the retry count t here, unless
225 // we have retried so often that we should go serial to avoid
226 // starvation.
230 #endif
232 tx = gtm_thr();
233 if (unlikely(tx == NULL))
235 // Create the thread object. The constructor will also set up automatic
236 // deletion on thread termination.
237 tx = new gtm_thread();
238 set_gtm_thr(tx);
241 if (tx->nesting > 0)
243 // This is a nested transaction.
244 // Check prop compatibility:
245 // The ABI requires pr_hasNoFloatUpdate, pr_hasNoVectorUpdate,
246 // pr_hasNoIrrevocable, pr_aWBarriersOmitted, pr_RaRBarriersOmitted, and
247 // pr_hasNoSimpleReads to hold for the full dynamic scope of a
248 // transaction. We could check that these are set for the nested
249 // transaction if they are also set for the parent transaction, but the
250 // ABI does not require these flags to be set if they could be set,
251 // so the check could be too strict.
252 // ??? For pr_readOnly, lexical or dynamic scope is unspecified.
254 if (prop & pr_hasNoAbort)
256 // We can use flat nesting, so elide this transaction.
257 if (!(prop & pr_instrumentedCode))
259 if (!(tx->state & STATE_SERIAL) ||
260 !(tx->state & STATE_IRREVOCABLE))
261 tx->serialirr_mode();
263 // Increment nesting level after checking that we have a method that
264 // allows us to continue.
265 tx->nesting++;
266 return choose_code_path(prop, abi_disp());
269 // The transaction might abort, so use closed nesting if possible.
270 // pr_hasNoAbort has lexical scope, so the compiler should really have
271 // generated an instrumented code path.
272 assert(prop & pr_instrumentedCode);
274 // Create a checkpoint of the current transaction.
275 gtm_transaction_cp *cp = tx->parent_txns.push();
276 cp->save(tx);
277 new (&tx->alloc_actions) aa_tree<uintptr_t, gtm_alloc_action>();
279 // Check whether the current method actually supports closed nesting.
280 // If we can switch to another one, do so.
281 // If not, we assume that actual aborts are infrequent, and rather
282 // restart in _ITM_abortTransaction when we really have to.
283 disp = abi_disp();
284 if (!disp->closed_nesting())
286 // ??? Should we elide the transaction if there is no alternative
287 // method that supports closed nesting? If we do, we need to set
288 // some flag to prevent _ITM_abortTransaction from aborting the
289 // wrong transaction (i.e., some parent transaction).
290 abi_dispatch *cn_disp = disp->closed_nesting_alternative();
291 if (cn_disp)
293 disp = cn_disp;
294 set_abi_disp(disp);
298 else
300 // Outermost transaction
301 disp = tx->decide_begin_dispatch (prop);
302 set_abi_disp (disp);
305 // Initialization that is common for outermost and nested transactions.
306 tx->prop = prop;
307 tx->nesting++;
309 tx->jb = *jb;
311 // As long as we have not exhausted a previously allocated block of TIDs,
312 // we can avoid an atomic operation on a shared cacheline.
313 if (tx->local_tid & (tid_block_size - 1))
314 tx->id = tx->local_tid++;
315 else
317 #ifdef HAVE_64BIT_SYNC_BUILTINS
318 // We don't really care which block of TIDs we get but only that we
319 // acquire one atomically; therefore, relaxed memory order is
320 // sufficient.
321 tx->id = global_tid.fetch_add(tid_block_size, memory_order_relaxed);
322 tx->local_tid = tx->id + 1;
323 #else
324 pthread_mutex_lock (&global_tid_lock);
325 global_tid += tid_block_size;
326 tx->id = global_tid;
327 tx->local_tid = tx->id + 1;
328 pthread_mutex_unlock (&global_tid_lock);
329 #endif
332 // Run dispatch-specific restart code. Retry until we succeed.
333 GTM::gtm_restart_reason rr;
334 while ((rr = disp->begin_or_restart()) != NO_RESTART)
336 tx->decide_retry_strategy(rr);
337 disp = abi_disp();
340 // Determine the code path to run. Only irrevocable transactions cannot be
341 // restarted, so all other transactions need to save live variables.
342 ret = choose_code_path(prop, disp);
343 if (!(tx->state & STATE_IRREVOCABLE))
344 ret |= a_saveLiveVariables;
345 return ret;
349 void
350 GTM::gtm_transaction_cp::save(gtm_thread* tx)
352 // Save everything that we might have to restore on restarts or aborts.
353 jb = tx->jb;
354 undolog_size = tx->undolog.size();
355 memcpy(&alloc_actions, &tx->alloc_actions, sizeof(alloc_actions));
356 user_actions_size = tx->user_actions.size();
357 id = tx->id;
358 prop = tx->prop;
359 cxa_catch_count = tx->cxa_catch_count;
360 cxa_unthrown = tx->cxa_unthrown;
361 disp = abi_disp();
362 nesting = tx->nesting;
365 void
366 GTM::gtm_transaction_cp::commit(gtm_thread* tx)
368 // Restore state that is not persistent across commits. Exception handling,
369 // information, nesting level, and any logs do not need to be restored on
370 // commits of nested transactions. Allocation actions must be committed
371 // before committing the snapshot.
372 tx->jb = jb;
373 memcpy(&tx->alloc_actions, &alloc_actions, sizeof(alloc_actions));
374 tx->id = id;
375 tx->prop = prop;
379 void
380 GTM::gtm_thread::rollback (gtm_transaction_cp *cp, bool aborting)
382 // The undo log is special in that it used for both thread-local and shared
383 // data. Because of the latter, we have to roll it back before any
384 // dispatch-specific rollback (which handles synchronization with other
385 // transactions).
386 undolog.rollback (this, cp ? cp->undolog_size : 0);
388 // Perform dispatch-specific rollback.
389 abi_disp()->rollback (cp);
391 // Roll back all actions that are supposed to happen around the transaction.
392 rollback_user_actions (cp ? cp->user_actions_size : 0);
393 commit_allocations (true, (cp ? &cp->alloc_actions : 0));
394 revert_cpp_exceptions (cp);
396 if (cp)
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;
412 else
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)
419 jb = parent_txns[0].jb;
420 id = parent_txns[0].id;
421 prop = parent_txns[0].prop;
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();
431 if (this->eh_in_flight)
433 _Unwind_DeleteException ((_Unwind_Exception *) this->eh_in_flight);
434 this->eh_in_flight = NULL;
438 void ITM_REGPARM
439 _ITM_abortTransaction (_ITM_abortReason reason)
441 gtm_thread *tx = gtm_thr();
443 assert (reason == userAbort || reason == (userAbort | outerAbort));
444 assert ((tx->prop & pr_hasNoAbort) == 0);
446 if (tx->state & gtm_thread::STATE_IRREVOCABLE)
447 abort ();
449 // Roll back to innermost transaction.
450 if (tx->parent_txns.size() > 0 && !(reason & outerAbort))
452 // If the current method does not support closed nesting but we are
453 // nested and must only roll back the innermost transaction, then
454 // restart with a method that supports closed nesting.
455 abi_dispatch *disp = abi_disp();
456 if (!disp->closed_nesting())
457 tx->restart(RESTART_CLOSED_NESTING);
459 // The innermost transaction is a closed nested transaction.
460 gtm_transaction_cp *cp = tx->parent_txns.pop();
461 uint32_t longjmp_prop = tx->prop;
462 gtm_jmpbuf longjmp_jb = tx->jb;
464 tx->rollback (cp, true);
466 // Jump to nested transaction (use the saved jump buffer).
467 GTM_longjmp (a_abortTransaction | a_restoreLiveVariables,
468 &longjmp_jb, longjmp_prop);
470 else
472 // There is no nested transaction or an abort of the outermost
473 // transaction was requested, so roll back to the outermost transaction.
474 tx->rollback (0, true);
476 // Aborting an outermost transaction finishes execution of the whole
477 // transaction. Therefore, reset transaction state.
478 if (tx->state & gtm_thread::STATE_SERIAL)
479 gtm_thread::serial_lock.write_unlock ();
480 else
481 gtm_thread::serial_lock.read_unlock (tx);
482 tx->state = 0;
484 GTM_longjmp (a_abortTransaction | a_restoreLiveVariables,
485 &tx->jb, tx->prop);
489 bool
490 GTM::gtm_thread::trycommit ()
492 nesting--;
494 // Skip any real commit for elided transactions.
495 if (nesting > 0 && (parent_txns.size() == 0 ||
496 nesting > parent_txns[parent_txns.size() - 1].nesting))
497 return true;
499 if (nesting > 0)
501 // Commit of a closed-nested transaction. Remove one checkpoint and add
502 // any effects of this transaction to the parent transaction.
503 gtm_transaction_cp *cp = parent_txns.pop();
504 commit_allocations(false, &cp->alloc_actions);
505 cp->commit(this);
506 return true;
509 // Commit of an outermost transaction.
510 gtm_word priv_time = 0;
511 if (abi_disp()->trycommit (priv_time))
513 // The transaction is now inactive. Everything that we still have to do
514 // will not synchronize with other transactions anymore.
515 if (state & gtm_thread::STATE_SERIAL)
517 gtm_thread::serial_lock.write_unlock ();
518 // There are no other active transactions, so there's no need to
519 // enforce privatization safety.
520 priv_time = 0;
522 else
523 gtm_thread::serial_lock.read_unlock (this);
524 state = 0;
526 // We can commit the undo log after dispatch-specific commit and after
527 // making the transaction inactive because we only have to reset
528 // gtm_thread state.
529 undolog.commit ();
530 // Reset further transaction state.
531 cxa_catch_count = 0;
532 cxa_unthrown = NULL;
533 restart_total = 0;
535 // Ensure privatization safety, if necessary.
536 if (priv_time)
538 // There must be a seq_cst fence between the following loads of the
539 // other transactions' shared_state and the dispatch-specific stores
540 // that signal updates by this transaction (e.g., lock
541 // acquisitions). This ensures that if we read prior to other
542 // reader transactions setting their shared_state to 0, then those
543 // readers will observe our updates. We can reuse the seq_cst fence
544 // in serial_lock.read_unlock() however, so we don't need another
545 // one here.
546 // TODO Don't just spin but also block using cond vars / futexes
547 // here. Should probably be integrated with the serial lock code.
548 for (gtm_thread *it = gtm_thread::list_of_threads; it != 0;
549 it = it->next_thread)
551 if (it == this) continue;
552 // We need to load other threads' shared_state using acquire
553 // semantics (matching the release semantics of the respective
554 // updates). This is necessary to ensure that the other
555 // threads' memory accesses happen before our actions that
556 // assume privatization safety.
557 // TODO Are there any platform-specific optimizations (e.g.,
558 // merging barriers)?
559 while (it->shared_state.load(memory_order_acquire) < priv_time)
560 cpu_relax();
564 // After ensuring privatization safety, we execute potentially
565 // privatizing actions (e.g., calling free()). User actions are first.
566 commit_user_actions ();
567 commit_allocations (false, 0);
569 return true;
571 return false;
574 void ITM_NORETURN
575 GTM::gtm_thread::restart (gtm_restart_reason r, bool finish_serial_upgrade)
577 // Roll back to outermost transaction. Do not reset transaction state because
578 // we will continue executing this transaction.
579 rollback ();
581 // If we have to restart while an upgrade of the serial lock is happening,
582 // we need to finish this here, after rollback (to ensure privatization
583 // safety despite undo writes) and before deciding about the retry strategy
584 // (which could switch to/from serial mode).
585 if (finish_serial_upgrade)
586 gtm_thread::serial_lock.write_upgrade_finish(this);
588 decide_retry_strategy (r);
590 // Run dispatch-specific restart code. Retry until we succeed.
591 abi_dispatch* disp = abi_disp();
592 GTM::gtm_restart_reason rr;
593 while ((rr = disp->begin_or_restart()) != NO_RESTART)
595 decide_retry_strategy(rr);
596 disp = abi_disp();
599 GTM_longjmp (choose_code_path(prop, disp) | a_restoreLiveVariables,
600 &jb, prop);
603 void ITM_REGPARM
604 _ITM_commitTransaction(void)
606 #if defined(USE_HTM_FASTPATH)
607 // HTM fastpath. If we are not executing a HW transaction, then we will be
608 // a serial-mode transaction. If we are, then there will be no other
609 // concurrent serial-mode transaction.
610 // See gtm_thread::begin_transaction.
611 if (likely(htm_fastpath && !gtm_thread::serial_lock.is_write_locked()))
613 htm_commit();
614 return;
616 #endif
617 gtm_thread *tx = gtm_thr();
618 if (!tx->trycommit ())
619 tx->restart (RESTART_VALIDATE_COMMIT);
622 void ITM_REGPARM
623 _ITM_commitTransactionEH(void *exc_ptr)
625 #if defined(USE_HTM_FASTPATH)
626 // See _ITM_commitTransaction.
627 if (likely(htm_fastpath && !gtm_thread::serial_lock.is_write_locked()))
629 htm_commit();
630 return;
632 #endif
633 gtm_thread *tx = gtm_thr();
634 if (!tx->trycommit ())
636 tx->eh_in_flight = exc_ptr;
637 tx->restart (RESTART_VALIDATE_COMMIT);