1 /* Copyright (C) 2008, 2009, 2011, 2012 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
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/>. */
31 #if !defined(HAVE_ARCH_GTM_THREAD) || !defined(HAVE_ARCH_GTM_THREAD_DISP)
32 extern __thread gtm_thread_tls _gtm_thr_tls
;
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
;
48 static _ITM_transactionId_t global_tid
;
49 static pthread_mutex_t global_tid_lock
= PTHREAD_MUTEX_INITIALIZER
;
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
;
58 /* Allocate a transaction structure. */
60 GTM::gtm_thread::operator new (size_t s
)
64 assert(s
== sizeof(gtm_thread
));
66 tx
= xmalloc (sizeof (gtm_thread
), true);
67 memset (tx
, 0, sizeof (gtm_thread
));
72 /* Free the given transaction. Raises an error if the transaction is still
75 GTM::gtm_thread::operator delete(void *tx
)
81 thread_exit_handler(void *)
83 gtm_thread
*thr
= gtm_thr();
92 if (pthread_key_create(&thr_release_key
, thread_exit_handler
))
93 GTM_fatal("Creating thread release TLS key failed.");
97 GTM::gtm_thread::~gtm_thread()
100 GTM_fatal("Thread exit while a transaction is still active.");
102 // Deregister this transaction.
103 serial_lock
.write_lock ();
104 gtm_thread
**prev
= &list_of_threads
;
105 for (; *prev
; prev
= &(*prev
)->next_thread
)
109 *prev
= (*prev
)->next_thread
;
114 number_of_threads_changed(number_of_threads
+ 1, number_of_threads
);
115 serial_lock
.write_unlock ();
118 GTM::gtm_thread::gtm_thread ()
120 // This object's memory has been set to zero by operator new, so no need
121 // to initialize any of the other primitive-type members that do not have
123 shared_state
.store(-1, memory_order_relaxed
);
125 // Register this transaction with the list of all threads' transactions.
126 serial_lock
.write_lock ();
127 next_thread
= list_of_threads
;
128 list_of_threads
= this;
130 number_of_threads_changed(number_of_threads
- 1, number_of_threads
);
131 serial_lock
.write_unlock ();
133 if (pthread_once(&thr_release_once
, thread_exit_init
))
134 GTM_fatal("Initializing thread release TLS key failed.");
135 // Any non-null value is sufficient to trigger destruction of this
136 // transaction when the current thread terminates.
137 if (pthread_setspecific(thr_release_key
, this))
138 GTM_fatal("Setting thread release TLS key failed.");
141 static inline uint32_t
142 choose_code_path(uint32_t prop
, abi_dispatch
*disp
)
144 if ((prop
& pr_uninstrumentedCode
) && disp
->can_run_uninstrumented_code())
145 return a_runUninstrumentedCode
;
147 return a_runInstrumentedCode
;
151 GTM::gtm_thread::begin_transaction (uint32_t prop
, const gtm_jmpbuf
*jb
)
153 static const _ITM_transactionId_t tid_block_size
= 1 << 16;
159 // ??? pr_undoLogCode is not properly defined in the ABI. Are barriers
160 // omitted because they are not necessary (e.g., a transaction on thread-
161 // local data) or because the compiler thinks that some kind of global
162 // synchronization might perform better?
163 if (unlikely(prop
& pr_undoLogCode
))
164 GTM_fatal("pr_undoLogCode not supported");
167 if (unlikely(tx
== NULL
))
169 // Create the thread object. The constructor will also set up automatic
170 // deletion on thread termination.
171 tx
= new gtm_thread();
177 // This is a nested transaction.
178 // Check prop compatibility:
179 // The ABI requires pr_hasNoFloatUpdate, pr_hasNoVectorUpdate,
180 // pr_hasNoIrrevocable, pr_aWBarriersOmitted, pr_RaRBarriersOmitted, and
181 // pr_hasNoSimpleReads to hold for the full dynamic scope of a
182 // transaction. We could check that these are set for the nested
183 // transaction if they are also set for the parent transaction, but the
184 // ABI does not require these flags to be set if they could be set,
185 // so the check could be too strict.
186 // ??? For pr_readOnly, lexical or dynamic scope is unspecified.
188 if (prop
& pr_hasNoAbort
)
190 // We can use flat nesting, so elide this transaction.
191 if (!(prop
& pr_instrumentedCode
))
193 if (!(tx
->state
& STATE_SERIAL
) ||
194 !(tx
->state
& STATE_IRREVOCABLE
))
195 tx
->serialirr_mode();
197 // Increment nesting level after checking that we have a method that
198 // allows us to continue.
200 return choose_code_path(prop
, abi_disp());
203 // The transaction might abort, so use closed nesting if possible.
204 // pr_hasNoAbort has lexical scope, so the compiler should really have
205 // generated an instrumented code path.
206 assert(prop
& pr_instrumentedCode
);
208 // Create a checkpoint of the current transaction.
209 gtm_transaction_cp
*cp
= tx
->parent_txns
.push();
211 new (&tx
->alloc_actions
) aa_tree
<uintptr_t, gtm_alloc_action
>();
213 // Check whether the current method actually supports closed nesting.
214 // If we can switch to another one, do so.
215 // If not, we assume that actual aborts are infrequent, and rather
216 // restart in _ITM_abortTransaction when we really have to.
218 if (!disp
->closed_nesting())
220 // ??? Should we elide the transaction if there is no alternative
221 // method that supports closed nesting? If we do, we need to set
222 // some flag to prevent _ITM_abortTransaction from aborting the
223 // wrong transaction (i.e., some parent transaction).
224 abi_dispatch
*cn_disp
= disp
->closed_nesting_alternative();
234 // Outermost transaction
235 disp
= tx
->decide_begin_dispatch (prop
);
239 // Initialization that is common for outermost and nested transactions.
245 // As long as we have not exhausted a previously allocated block of TIDs,
246 // we can avoid an atomic operation on a shared cacheline.
247 if (tx
->local_tid
& (tid_block_size
- 1))
248 tx
->id
= tx
->local_tid
++;
251 #ifdef HAVE_64BIT_SYNC_BUILTINS
252 // We don't really care which block of TIDs we get but only that we
253 // acquire one atomically; therefore, relaxed memory order is
255 tx
->id
= global_tid
.fetch_add(tid_block_size
, memory_order_relaxed
);
256 tx
->local_tid
= tx
->id
+ 1;
258 pthread_mutex_lock (&global_tid_lock
);
259 global_tid
+= tid_block_size
;
261 tx
->local_tid
= tx
->id
+ 1;
262 pthread_mutex_unlock (&global_tid_lock
);
266 // Run dispatch-specific restart code. Retry until we succeed.
267 GTM::gtm_restart_reason rr
;
268 while ((rr
= disp
->begin_or_restart()) != NO_RESTART
)
270 tx
->decide_retry_strategy(rr
);
274 // Determine the code path to run. Only irrevocable transactions cannot be
275 // restarted, so all other transactions need to save live variables.
276 ret
= choose_code_path(prop
, disp
);
277 if (!(tx
->state
& STATE_IRREVOCABLE
))
278 ret
|= a_saveLiveVariables
;
284 GTM::gtm_transaction_cp::save(gtm_thread
* tx
)
286 // Save everything that we might have to restore on restarts or aborts.
288 undolog_size
= tx
->undolog
.size();
289 memcpy(&alloc_actions
, &tx
->alloc_actions
, sizeof(alloc_actions
));
290 user_actions_size
= tx
->user_actions
.size();
293 cxa_catch_count
= tx
->cxa_catch_count
;
294 cxa_unthrown
= tx
->cxa_unthrown
;
296 nesting
= tx
->nesting
;
300 GTM::gtm_transaction_cp::commit(gtm_thread
* tx
)
302 // Restore state that is not persistent across commits. Exception handling,
303 // information, nesting level, and any logs do not need to be restored on
304 // commits of nested transactions. Allocation actions must be committed
305 // before committing the snapshot.
307 memcpy(&tx
->alloc_actions
, &alloc_actions
, sizeof(alloc_actions
));
314 GTM::gtm_thread::rollback (gtm_transaction_cp
*cp
, bool aborting
)
316 // The undo log is special in that it used for both thread-local and shared
317 // data. Because of the latter, we have to roll it back before any
318 // dispatch-specific rollback (which handles synchronization with other
320 undolog
.rollback (this, cp
? cp
->undolog_size
: 0);
322 // Perform dispatch-specific rollback.
323 abi_disp()->rollback (cp
);
325 // Roll back all actions that are supposed to happen around the transaction.
326 rollback_user_actions (cp
? cp
->user_actions_size
: 0);
327 commit_allocations (true, (cp
? &cp
->alloc_actions
: 0));
328 revert_cpp_exceptions (cp
);
332 // We do not yet handle restarts of nested transactions. To do that, we
333 // would have to restore some state (jb, id, prop, nesting) not to the
334 // checkpoint but to the transaction that was started from this
335 // checkpoint (e.g., nesting = cp->nesting + 1);
337 // Roll back the rest of the state to the checkpoint.
341 if (cp
->disp
!= abi_disp())
342 set_abi_disp(cp
->disp
);
343 memcpy(&alloc_actions
, &cp
->alloc_actions
, sizeof(alloc_actions
));
344 nesting
= cp
->nesting
;
348 // Roll back to the outermost transaction.
349 // Restore the jump buffer and transaction properties, which we will
350 // need for the longjmp used to restart or abort the transaction.
351 if (parent_txns
.size() > 0)
353 jb
= parent_txns
[0].jb
;
354 id
= parent_txns
[0].id
;
355 prop
= parent_txns
[0].prop
;
357 // Reset the transaction. Do not reset this->state, which is handled by
358 // the callers. Note that if we are not aborting, we reset the
359 // transaction to the point after having executed begin_transaction
360 // (we will return from it), so the nesting level must be one, not zero.
361 nesting
= (aborting
? 0 : 1);
365 if (this->eh_in_flight
)
367 _Unwind_DeleteException ((_Unwind_Exception
*) this->eh_in_flight
);
368 this->eh_in_flight
= NULL
;
373 _ITM_abortTransaction (_ITM_abortReason reason
)
375 gtm_thread
*tx
= gtm_thr();
377 assert (reason
== userAbort
|| reason
== (userAbort
| outerAbort
));
378 assert ((tx
->prop
& pr_hasNoAbort
) == 0);
380 if (tx
->state
& gtm_thread::STATE_IRREVOCABLE
)
383 // Roll back to innermost transaction.
384 if (tx
->parent_txns
.size() > 0 && !(reason
& outerAbort
))
386 // If the current method does not support closed nesting but we are
387 // nested and must only roll back the innermost transaction, then
388 // restart with a method that supports closed nesting.
389 abi_dispatch
*disp
= abi_disp();
390 if (!disp
->closed_nesting())
391 tx
->restart(RESTART_CLOSED_NESTING
);
393 // The innermost transaction is a closed nested transaction.
394 gtm_transaction_cp
*cp
= tx
->parent_txns
.pop();
395 uint32_t longjmp_prop
= tx
->prop
;
396 gtm_jmpbuf longjmp_jb
= tx
->jb
;
398 tx
->rollback (cp
, true);
400 // Jump to nested transaction (use the saved jump buffer).
401 GTM_longjmp (a_abortTransaction
| a_restoreLiveVariables
,
402 &longjmp_jb
, longjmp_prop
);
406 // There is no nested transaction or an abort of the outermost
407 // transaction was requested, so roll back to the outermost transaction.
408 tx
->rollback (0, true);
410 // Aborting an outermost transaction finishes execution of the whole
411 // transaction. Therefore, reset transaction state.
412 if (tx
->state
& gtm_thread::STATE_SERIAL
)
413 gtm_thread::serial_lock
.write_unlock ();
415 gtm_thread::serial_lock
.read_unlock (tx
);
418 GTM_longjmp (a_abortTransaction
| a_restoreLiveVariables
,
424 GTM::gtm_thread::trycommit ()
428 // Skip any real commit for elided transactions.
429 if (nesting
> 0 && (parent_txns
.size() == 0 ||
430 nesting
> parent_txns
[parent_txns
.size() - 1].nesting
))
435 // Commit of a closed-nested transaction. Remove one checkpoint and add
436 // any effects of this transaction to the parent transaction.
437 gtm_transaction_cp
*cp
= parent_txns
.pop();
438 commit_allocations(false, &cp
->alloc_actions
);
443 // Commit of an outermost transaction.
444 gtm_word priv_time
= 0;
445 if (abi_disp()->trycommit (priv_time
))
447 // The transaction is now inactive. Everything that we still have to do
448 // will not synchronize with other transactions anymore.
449 if (state
& gtm_thread::STATE_SERIAL
)
451 gtm_thread::serial_lock
.write_unlock ();
452 // There are no other active transactions, so there's no need to
453 // enforce privatization safety.
457 gtm_thread::serial_lock
.read_unlock (this);
460 // We can commit the undo log after dispatch-specific commit and after
461 // making the transaction inactive because we only have to reset
464 // Reset further transaction state.
469 // Ensure privatization safety, if necessary.
472 // There must be a seq_cst fence between the following loads of the
473 // other transactions' shared_state and the dispatch-specific stores
474 // that signal updates by this transaction (e.g., lock
475 // acquisitions). This ensures that if we read prior to other
476 // reader transactions setting their shared_state to 0, then those
477 // readers will observe our updates. We can reuse the seq_cst fence
478 // in serial_lock.read_unlock() however, so we don't need another
480 // TODO Don't just spin but also block using cond vars / futexes
481 // here. Should probably be integrated with the serial lock code.
482 for (gtm_thread
*it
= gtm_thread::list_of_threads
; it
!= 0;
483 it
= it
->next_thread
)
485 if (it
== this) continue;
486 // We need to load other threads' shared_state using acquire
487 // semantics (matching the release semantics of the respective
488 // updates). This is necessary to ensure that the other
489 // threads' memory accesses happen before our actions that
490 // assume privatization safety.
491 // TODO Are there any platform-specific optimizations (e.g.,
492 // merging barriers)?
493 while (it
->shared_state
.load(memory_order_acquire
) < priv_time
)
498 // After ensuring privatization safety, we execute potentially
499 // privatizing actions (e.g., calling free()). User actions are first.
500 commit_user_actions ();
501 commit_allocations (false, 0);
509 GTM::gtm_thread::restart (gtm_restart_reason r
, bool finish_serial_upgrade
)
511 // Roll back to outermost transaction. Do not reset transaction state because
512 // we will continue executing this transaction.
515 // If we have to restart while an upgrade of the serial lock is happening,
516 // we need to finish this here, after rollback (to ensure privatization
517 // safety despite undo writes) and before deciding about the retry strategy
518 // (which could switch to/from serial mode).
519 if (finish_serial_upgrade
)
520 gtm_thread::serial_lock
.write_upgrade_finish(this);
522 decide_retry_strategy (r
);
524 // Run dispatch-specific restart code. Retry until we succeed.
525 abi_dispatch
* disp
= abi_disp();
526 GTM::gtm_restart_reason rr
;
527 while ((rr
= disp
->begin_or_restart()) != NO_RESTART
)
529 decide_retry_strategy(rr
);
533 GTM_longjmp (choose_code_path(prop
, disp
) | a_restoreLiveVariables
,
538 _ITM_commitTransaction(void)
540 gtm_thread
*tx
= gtm_thr();
541 if (!tx
->trycommit ())
542 tx
->restart (RESTART_VALIDATE_COMMIT
);
546 _ITM_commitTransactionEH(void *exc_ptr
)
548 gtm_thread
*tx
= gtm_thr();
549 if (!tx
->trycommit ())
551 tx
->eh_in_flight
= exc_ptr
;
552 tx
->restart (RESTART_VALIDATE_COMMIT
);