1 /* -----------------------------------------------------------------------------
2 * (c) The GHC Team 1998-2005
9 * See the PPoPP 2005 paper "Composable memory transactions". In summary,
10 * each transcation has a TRec (transaction record) holding entries for each of the
11 * TVars (transactional variables) that it has accessed. Each entry records
12 * (a) the TVar, (b) the expected value seen in the TVar, (c) the new value that
13 * the transaction wants to write to the TVar, (d) during commit, the identity of
14 * the TRec that wrote the expected value.
16 * Separate TRecs are used for each level in a nest of transactions. This allows
17 * a nested transaction to be aborted without condemning its enclosing transactions.
18 * This is needed in the implementation of catchRetry. Note that the "expected value"
19 * in a nested transaction's TRec is the value expected to be *held in memory* if
20 * the transaction commits -- not the "new value" stored in one of the enclosing
21 * transactions. This means that validation can be done without searching through
27 * Three different concurrency control schemes can be built according to the settings
30 * STM_UNIPROC assumes that the caller serialises invocations on the STM interface.
31 * In the Haskell RTS this means it is suitable only for non-THREADED_RTS builds.
33 * STM_CG_LOCK uses coarse-grained locking -- a single 'stm lock' is acquired during
34 * an invocation on the STM interface. Note that this does not mean that
35 * transactions are simply serialized -- the lock is only held *within* the
36 * implementation of stmCommitTransaction, stmWait etc.
38 * STM_FG_LOCKS uses fine-grained locking -- locking is done on a per-TVar basis
39 * and, when committing a transaction, no locks are acquired for TVars that have
40 * been read but not updated.
42 * Concurrency control is implemented in the functions:
46 * lock_tvar / cond_lock_tvar
49 * The choice between STM_UNIPROC / STM_CG_LOCK / STM_FG_LOCKS affects the
50 * implementation of these functions.
52 * lock_stm & unlock_stm are straightforward : they acquire a simple spin-lock
53 * using STM_CG_LOCK, and otherwise they are no-ops.
55 * lock_tvar / cond_lock_tvar and unlock_tvar are more complex because they
56 * have other effects (present in STM_UNIPROC and STM_CG_LOCK builds) as well
57 * as the actual business of maniupultaing a lock (present only in STM_FG_LOCKS
58 * builds). This is because locking a TVar is implemented by writing the lock
59 * holder's TRec into the TVar's current_value field:
61 * lock_tvar - lock a specified TVar (STM_FG_LOCKS only), returning the value
64 * cond_lock_tvar - lock a specified TVar (STM_FG_LOCKS only) if it
65 * contains a specified value. Return TRUE if this succeeds,
68 * unlock_tvar - release the lock on a specified TVar (STM_FG_LOCKS only),
69 * storing a specified value in place of the lock entry.
71 * Using these operations, the typcial pattern of a commit/validate/wait operation
72 * is to (a) lock the STM, (b) lock all the TVars being updated, (c) check that
73 * the TVars that were only read from still contain their expected values,
74 * (d) release the locks on the TVars, writing updates to them in the case of a
75 * commit, (e) unlock the STM.
77 * Queues of waiting threads hang off the first_watch_queue_entry
78 * field of each TVar. This may only be manipulated when holding that
79 * TVar's lock. In particular, when a thread is putting itself to
80 * sleep, it mustn't release the TVar's lock until it has added itself
81 * to the wait queue and marked its TSO as BlockedOnSTM -- this makes
82 * sure that other threads will know to wake it.
84 * ---------------------------------------------------------------------------*/
86 #include "PosixSource.h"
100 // ACQ_ASSERT is used for assertions which are only required for
101 // THREADED_RTS builds with fine-grained locking.
103 #if defined(STM_FG_LOCKS)
104 #define ACQ_ASSERT(_X) ASSERT(_X)
105 #define NACQ_ASSERT(_X) /*Nothing*/
107 #define ACQ_ASSERT(_X) /*Nothing*/
108 #define NACQ_ASSERT(_X) ASSERT(_X)
111 /*......................................................................*/
113 // If SHAKE is defined then validation will sometime spuriously fail. They helps test
114 // unusualy code paths if genuine contention is rare
116 #define TRACE(_x...) debugTrace(DEBUG_stm, "STM: " _x)
119 static const int do_shake = TRUE;
121 static const int do_shake = FALSE;
123 static int shake_ctr = 0;
124 static int shake_lim = 1;
126 static int shake(void) {
128 if (((shake_ctr++) % shake_lim) == 0) {
139 /*......................................................................*/
141 // Helper macros for iterating over entries within a transaction
144 #define FOR_EACH_ENTRY(_t,_x,CODE) do { \
145 StgTRecHeader *__t = (_t); \
146 StgTRecChunk *__c = __t -> current_chunk; \
147 StgWord __limit = __c -> next_entry_idx; \
148 TRACE("%p : FOR_EACH_ENTRY, current_chunk=%p limit=%ld", __t, __c, __limit); \
149 while (__c != END_STM_CHUNK_LIST) { \
151 for (__i = 0; __i < __limit; __i ++) { \
152 TRecEntry *_x = &(__c -> entries[__i]); \
153 do { CODE } while (0); \
155 __c = __c -> prev_chunk; \
156 __limit = TREC_CHUNK_NUM_ENTRIES; \
159 if (FALSE) goto exit_for_each; \
162 #define BREAK_FOR_EACH goto exit_for_each
164 /*......................................................................*/
166 // if REUSE_MEMORY is defined then attempt to re-use descriptors, log chunks,
167 // and wait queue entries without GC
171 /*......................................................................*/
173 #define IF_STM_UNIPROC(__X) do { } while (0)
174 #define IF_STM_CG_LOCK(__X) do { } while (0)
175 #define IF_STM_FG_LOCKS(__X) do { } while (0)
177 #if defined(STM_UNIPROC)
178 #undef IF_STM_UNIPROC
179 #define IF_STM_UNIPROC(__X) do { __X } while (0)
180 static const StgBool config_use_read_phase = FALSE;
182 static void lock_stm(StgTRecHeader *trec STG_UNUSED) {
183 TRACE("%p : lock_stm()", trec);
186 static void unlock_stm(StgTRecHeader *trec STG_UNUSED) {
187 TRACE("%p : unlock_stm()", trec);
190 static StgClosure *lock_tvar(StgTRecHeader *trec STG_UNUSED,
191 StgTVar *s STG_UNUSED) {
193 TRACE("%p : lock_tvar(%p)", trec, s);
194 result = s -> current_value;
198 static void unlock_tvar(StgTRecHeader *trec STG_UNUSED,
199 StgTVar *s STG_UNUSED,
201 StgBool force_update) {
202 TRACE("%p : unlock_tvar(%p)", trec, s);
204 s -> current_value = c;
208 static StgBool cond_lock_tvar(StgTRecHeader *trec STG_UNUSED,
209 StgTVar *s STG_UNUSED,
210 StgClosure *expected) {
212 TRACE("%p : cond_lock_tvar(%p, %p)", trec, s, expected);
213 result = s -> current_value;
214 TRACE("%p : %s", trec, (result == expected) ? "success" : "failure");
215 return (result == expected);
218 static StgBool lock_inv(StgAtomicInvariant *inv STG_UNUSED) {
219 // Nothing -- uniproc
223 static void unlock_inv(StgAtomicInvariant *inv STG_UNUSED) {
224 // Nothing -- uniproc
228 #if defined(STM_CG_LOCK) /*........................................*/
230 #undef IF_STM_CG_LOCK
231 #define IF_STM_CG_LOCK(__X) do { __X } while (0)
232 static const StgBool config_use_read_phase = FALSE;
233 static volatile StgTRecHeader *smp_locked = NULL;
235 static void lock_stm(StgTRecHeader *trec) {
236 while (cas(&smp_locked, NULL, trec) != NULL) { }
237 TRACE("%p : lock_stm()", trec);
240 static void unlock_stm(StgTRecHeader *trec STG_UNUSED) {
241 TRACE("%p : unlock_stm()", trec);
242 ASSERT (smp_locked == trec);
246 static StgClosure *lock_tvar(StgTRecHeader *trec STG_UNUSED,
247 StgTVar *s STG_UNUSED) {
249 TRACE("%p : lock_tvar(%p)", trec, s);
250 ASSERT (smp_locked == trec);
251 result = s -> current_value;
255 static void *unlock_tvar(StgTRecHeader *trec STG_UNUSED,
256 StgTVar *s STG_UNUSED,
258 StgBool force_update) {
259 TRACE("%p : unlock_tvar(%p, %p)", trec, s, c);
260 ASSERT (smp_locked == trec);
262 s -> current_value = c;
266 static StgBool cond_lock_tvar(StgTRecHeader *trec STG_UNUSED,
267 StgTVar *s STG_UNUSED,
268 StgClosure *expected) {
270 TRACE("%p : cond_lock_tvar(%p, %p)", trec, s, expected);
271 ASSERT (smp_locked == trec);
272 result = s -> current_value;
273 TRACE("%p : %d", result ? "success" : "failure");
274 return (result == expected);
277 static StgBool lock_inv(StgAtomicInvariant *inv STG_UNUSED) {
278 // Nothing -- protected by STM lock
282 static void unlock_inv(StgAtomicInvariant *inv STG_UNUSED) {
283 // Nothing -- protected by STM lock
287 #if defined(STM_FG_LOCKS) /*...................................*/
289 #undef IF_STM_FG_LOCKS
290 #define IF_STM_FG_LOCKS(__X) do { __X } while (0)
291 static const StgBool config_use_read_phase = TRUE;
293 static void lock_stm(StgTRecHeader *trec STG_UNUSED) {
294 TRACE("%p : lock_stm()", trec);
297 static void unlock_stm(StgTRecHeader *trec STG_UNUSED) {
298 TRACE("%p : unlock_stm()", trec);
301 static StgClosure *lock_tvar(StgTRecHeader *trec,
302 StgTVar *s STG_UNUSED) {
304 TRACE("%p : lock_tvar(%p)", trec, s);
307 result = s -> current_value;
308 } while (GET_INFO(UNTAG_CLOSURE(result)) == &stg_TREC_HEADER_info);
309 } while (cas((void *)&(s -> current_value),
310 (StgWord)result, (StgWord)trec) != (StgWord)result);
314 static void unlock_tvar(StgTRecHeader *trec STG_UNUSED,
317 StgBool force_update STG_UNUSED) {
318 TRACE("%p : unlock_tvar(%p, %p)", trec, s, c);
319 ASSERT(s -> current_value == (StgClosure *)trec);
320 s -> current_value = c;
323 static StgBool cond_lock_tvar(StgTRecHeader *trec,
325 StgClosure *expected) {
328 TRACE("%p : cond_lock_tvar(%p, %p)", trec, s, expected);
329 w = cas((void *)&(s -> current_value), (StgWord)expected, (StgWord)trec);
330 result = (StgClosure *)w;
331 TRACE("%p : %s", trec, result ? "success" : "failure");
332 return (result == expected);
335 static StgBool lock_inv(StgAtomicInvariant *inv) {
336 return (cas(&(inv -> lock), 0, 1) == 0);
339 static void unlock_inv(StgAtomicInvariant *inv) {
340 ASSERT(inv -> lock == 1);
345 /*......................................................................*/
347 static StgBool watcher_is_tso(StgTVarWatchQueue *q) {
348 StgClosure *c = q -> closure;
349 StgInfoTable *info = get_itbl(c);
350 return (info -> type) == TSO;
353 static StgBool watcher_is_invariant(StgTVarWatchQueue *q) {
354 StgClosure *c = q -> closure;
355 return (c->header.info == &stg_ATOMIC_INVARIANT_info);
358 /*......................................................................*/
360 // Helper functions for thread blocking and unblocking
362 static void park_tso(StgTSO *tso) {
363 ASSERT(tso -> why_blocked == NotBlocked);
364 tso -> why_blocked = BlockedOnSTM;
365 tso -> block_info.closure = (StgClosure *) END_TSO_QUEUE;
366 TRACE("park_tso on tso=%p", tso);
369 static void unpark_tso(Capability *cap, StgTSO *tso) {
370 // We will continue unparking threads while they remain on one of the wait
371 // queues: it's up to the thread itself to remove it from the wait queues
372 // if it decides to do so when it is scheduled.
374 // Unblocking a TSO from BlockedOnSTM is done under the TSO lock,
375 // to avoid multiple CPUs unblocking the same TSO, and also to
376 // synchronise with throwTo().
378 if (tso -> why_blocked == BlockedOnSTM) {
379 TRACE("unpark_tso on tso=%p", tso);
380 tryWakeupThread(cap,tso);
382 TRACE("spurious unpark_tso on tso=%p", tso);
387 static void unpark_waiters_on(Capability *cap, StgTVar *s) {
388 StgTVarWatchQueue *q;
389 StgTVarWatchQueue *trail;
390 TRACE("unpark_waiters_on tvar=%p", s);
391 // unblock TSOs in reverse order, to be a bit fairer (#2319)
392 for (q = s -> first_watch_queue_entry, trail = q;
393 q != END_STM_WATCH_QUEUE;
394 q = q -> next_queue_entry) {
399 q != END_STM_WATCH_QUEUE;
400 q = q -> prev_queue_entry) {
401 if (watcher_is_tso(q)) {
402 unpark_tso(cap, (StgTSO *)(q -> closure));
407 /*......................................................................*/
409 // Helper functions for downstream allocation and initialization
411 static StgInvariantCheckQueue *new_stg_invariant_check_queue(Capability *cap,
412 StgAtomicInvariant *invariant) {
413 StgInvariantCheckQueue *result;
414 result = (StgInvariantCheckQueue *)allocate(cap, sizeofW(StgInvariantCheckQueue));
415 SET_HDR (result, &stg_INVARIANT_CHECK_QUEUE_info, CCS_SYSTEM);
416 result -> invariant = invariant;
417 result -> my_execution = NO_TREC;
421 static StgTVarWatchQueue *new_stg_tvar_watch_queue(Capability *cap,
422 StgClosure *closure) {
423 StgTVarWatchQueue *result;
424 result = (StgTVarWatchQueue *)allocate(cap, sizeofW(StgTVarWatchQueue));
425 SET_HDR (result, &stg_TVAR_WATCH_QUEUE_info, CCS_SYSTEM);
426 result -> closure = closure;
430 static StgTRecChunk *new_stg_trec_chunk(Capability *cap) {
431 StgTRecChunk *result;
432 result = (StgTRecChunk *)allocate(cap, sizeofW(StgTRecChunk));
433 SET_HDR (result, &stg_TREC_CHUNK_info, CCS_SYSTEM);
434 result -> prev_chunk = END_STM_CHUNK_LIST;
435 result -> next_entry_idx = 0;
439 static StgTRecHeader *new_stg_trec_header(Capability *cap,
440 StgTRecHeader *enclosing_trec) {
441 StgTRecHeader *result;
442 result = (StgTRecHeader *) allocate(cap, sizeofW(StgTRecHeader));
443 SET_HDR (result, &stg_TREC_HEADER_info, CCS_SYSTEM);
445 result -> enclosing_trec = enclosing_trec;
446 result -> current_chunk = new_stg_trec_chunk(cap);
447 result -> invariants_to_check = END_INVARIANT_CHECK_QUEUE;
449 if (enclosing_trec == NO_TREC) {
450 result -> state = TREC_ACTIVE;
452 ASSERT(enclosing_trec -> state == TREC_ACTIVE ||
453 enclosing_trec -> state == TREC_CONDEMNED);
454 result -> state = enclosing_trec -> state;
460 /*......................................................................*/
462 // Allocation / deallocation functions that retain per-capability lists
463 // of closures that can be re-used
465 static StgInvariantCheckQueue *alloc_stg_invariant_check_queue(Capability *cap,
466 StgAtomicInvariant *invariant) {
467 StgInvariantCheckQueue *result = NULL;
468 if (cap -> free_invariant_check_queues == END_INVARIANT_CHECK_QUEUE) {
469 result = new_stg_invariant_check_queue(cap, invariant);
471 result = cap -> free_invariant_check_queues;
472 result -> invariant = invariant;
473 result -> my_execution = NO_TREC;
474 cap -> free_invariant_check_queues = result -> next_queue_entry;
479 static StgTVarWatchQueue *alloc_stg_tvar_watch_queue(Capability *cap,
480 StgClosure *closure) {
481 StgTVarWatchQueue *result = NULL;
482 if (cap -> free_tvar_watch_queues == END_STM_WATCH_QUEUE) {
483 result = new_stg_tvar_watch_queue(cap, closure);
485 result = cap -> free_tvar_watch_queues;
486 result -> closure = closure;
487 cap -> free_tvar_watch_queues = result -> next_queue_entry;
492 static void free_stg_tvar_watch_queue(Capability *cap,
493 StgTVarWatchQueue *wq) {
494 #if defined(REUSE_MEMORY)
495 wq -> next_queue_entry = cap -> free_tvar_watch_queues;
496 cap -> free_tvar_watch_queues = wq;
500 static StgTRecChunk *alloc_stg_trec_chunk(Capability *cap) {
501 StgTRecChunk *result = NULL;
502 if (cap -> free_trec_chunks == END_STM_CHUNK_LIST) {
503 result = new_stg_trec_chunk(cap);
505 result = cap -> free_trec_chunks;
506 cap -> free_trec_chunks = result -> prev_chunk;
507 result -> prev_chunk = END_STM_CHUNK_LIST;
508 result -> next_entry_idx = 0;
513 static void free_stg_trec_chunk(Capability *cap,
515 #if defined(REUSE_MEMORY)
516 c -> prev_chunk = cap -> free_trec_chunks;
517 cap -> free_trec_chunks = c;
521 static StgTRecHeader *alloc_stg_trec_header(Capability *cap,
522 StgTRecHeader *enclosing_trec) {
523 StgTRecHeader *result = NULL;
524 if (cap -> free_trec_headers == NO_TREC) {
525 result = new_stg_trec_header(cap, enclosing_trec);
527 result = cap -> free_trec_headers;
528 cap -> free_trec_headers = result -> enclosing_trec;
529 result -> enclosing_trec = enclosing_trec;
530 result -> current_chunk -> next_entry_idx = 0;
531 result -> invariants_to_check = END_INVARIANT_CHECK_QUEUE;
532 if (enclosing_trec == NO_TREC) {
533 result -> state = TREC_ACTIVE;
535 ASSERT(enclosing_trec -> state == TREC_ACTIVE ||
536 enclosing_trec -> state == TREC_CONDEMNED);
537 result -> state = enclosing_trec -> state;
543 static void free_stg_trec_header(Capability *cap,
544 StgTRecHeader *trec) {
545 #if defined(REUSE_MEMORY)
546 StgTRecChunk *chunk = trec -> current_chunk -> prev_chunk;
547 while (chunk != END_STM_CHUNK_LIST) {
548 StgTRecChunk *prev_chunk = chunk -> prev_chunk;
549 free_stg_trec_chunk(cap, chunk);
552 trec -> current_chunk -> prev_chunk = END_STM_CHUNK_LIST;
553 trec -> enclosing_trec = cap -> free_trec_headers;
554 cap -> free_trec_headers = trec;
558 /*......................................................................*/
560 // Helper functions for managing waiting lists
562 static void build_watch_queue_entries_for_trec(Capability *cap,
564 StgTRecHeader *trec) {
565 ASSERT(trec != NO_TREC);
566 ASSERT(trec -> enclosing_trec == NO_TREC);
567 ASSERT(trec -> state == TREC_ACTIVE);
569 TRACE("%p : build_watch_queue_entries_for_trec()", trec);
571 FOR_EACH_ENTRY(trec, e, {
573 StgTVarWatchQueue *q;
574 StgTVarWatchQueue *fq;
576 TRACE("%p : adding tso=%p to watch queue for tvar=%p", trec, tso, s);
577 ACQ_ASSERT(s -> current_value == (StgClosure *)trec);
578 NACQ_ASSERT(s -> current_value == e -> expected_value);
579 fq = s -> first_watch_queue_entry;
580 q = alloc_stg_tvar_watch_queue(cap, (StgClosure*) tso);
581 q -> next_queue_entry = fq;
582 q -> prev_queue_entry = END_STM_WATCH_QUEUE;
583 if (fq != END_STM_WATCH_QUEUE) {
584 fq -> prev_queue_entry = q;
586 s -> first_watch_queue_entry = q;
587 e -> new_value = (StgClosure *) q;
591 static void remove_watch_queue_entries_for_trec(Capability *cap,
592 StgTRecHeader *trec) {
593 ASSERT(trec != NO_TREC);
594 ASSERT(trec -> enclosing_trec == NO_TREC);
595 ASSERT(trec -> state == TREC_WAITING ||
596 trec -> state == TREC_CONDEMNED);
598 TRACE("%p : remove_watch_queue_entries_for_trec()", trec);
600 FOR_EACH_ENTRY(trec, e, {
602 StgTVarWatchQueue *pq;
603 StgTVarWatchQueue *nq;
604 StgTVarWatchQueue *q;
607 saw = lock_tvar(trec, s);
608 q = (StgTVarWatchQueue *) (e -> new_value);
609 TRACE("%p : removing tso=%p from watch queue for tvar=%p",
613 ACQ_ASSERT(s -> current_value == (StgClosure *)trec);
614 nq = q -> next_queue_entry;
615 pq = q -> prev_queue_entry;
616 if (nq != END_STM_WATCH_QUEUE) {
617 nq -> prev_queue_entry = pq;
619 if (pq != END_STM_WATCH_QUEUE) {
620 pq -> next_queue_entry = nq;
622 ASSERT (s -> first_watch_queue_entry == q);
623 s -> first_watch_queue_entry = nq;
625 free_stg_tvar_watch_queue(cap, q);
626 unlock_tvar(trec, s, saw, FALSE);
630 /*......................................................................*/
632 static TRecEntry *get_new_entry(Capability *cap,
638 c = t -> current_chunk;
639 i = c -> next_entry_idx;
640 ASSERT(c != END_STM_CHUNK_LIST);
642 if (i < TREC_CHUNK_NUM_ENTRIES) {
643 // Continue to use current chunk
644 result = &(c -> entries[i]);
645 c -> next_entry_idx ++;
647 // Current chunk is full: allocate a fresh one
649 nc = alloc_stg_trec_chunk(cap);
650 nc -> prev_chunk = c;
651 nc -> next_entry_idx = 1;
652 t -> current_chunk = nc;
653 result = &(nc -> entries[0]);
659 /*......................................................................*/
661 static void merge_update_into(Capability *cap,
664 StgClosure *expected_value,
665 StgClosure *new_value) {
668 // Look for an entry in this trec
670 FOR_EACH_ENTRY(t, e, {
675 if (e -> expected_value != expected_value) {
676 // Must abort if the two entries start from different values
677 TRACE("%p : update entries inconsistent at %p (%p vs %p)",
678 t, tvar, e -> expected_value, expected_value);
679 t -> state = TREC_CONDEMNED;
681 e -> new_value = new_value;
687 // No entry so far in this trec
689 ne = get_new_entry(cap, t);
691 ne -> expected_value = expected_value;
692 ne -> new_value = new_value;
696 /*......................................................................*/
698 static void merge_read_into(Capability *cap,
701 StgClosure *expected_value) {
704 // Look for an entry in this trec
706 FOR_EACH_ENTRY(t, e, {
711 if (e -> expected_value != expected_value) {
712 // Must abort if the two entries start from different values
713 TRACE("%p : read entries inconsistent at %p (%p vs %p)",
714 t, tvar, e -> expected_value, expected_value);
715 t -> state = TREC_CONDEMNED;
722 // No entry so far in this trec
724 ne = get_new_entry(cap, t);
726 ne -> expected_value = expected_value;
727 ne -> new_value = expected_value;
731 /*......................................................................*/
733 static StgBool entry_is_update(TRecEntry *e) {
735 result = (e -> expected_value != e -> new_value);
739 #if defined(STM_FG_LOCKS)
740 static StgBool entry_is_read_only(TRecEntry *e) {
742 result = (e -> expected_value == e -> new_value);
746 static StgBool tvar_is_locked(StgTVar *s, StgTRecHeader *h) {
749 c = s -> current_value;
750 result = (c == (StgClosure *) h);
755 // revert_ownership : release a lock on a TVar, storing back
756 // the value that it held when the lock was acquired. "revert_all"
757 // is set in stmWait and stmReWait when we acquired locks on all of
758 // the TVars involved. "revert_all" is not set in commit operations
759 // where we don't lock TVars that have been read from but not updated.
761 static void revert_ownership(StgTRecHeader *trec STG_UNUSED,
762 StgBool revert_all STG_UNUSED) {
763 #if defined(STM_FG_LOCKS)
764 FOR_EACH_ENTRY(trec, e, {
765 if (revert_all || entry_is_update(e)) {
768 if (tvar_is_locked(s, trec)) {
769 unlock_tvar(trec, s, e -> expected_value, TRUE);
776 /*......................................................................*/
778 // validate_and_acquire_ownership : this performs the twin functions
779 // of checking that the TVars referred to by entries in trec hold the
780 // expected values and:
782 // - locking the TVar (on updated TVars during commit, or all TVars
785 // - recording the identity of the TRec who wrote the value seen in the
786 // TVar (on non-updated TVars during commit). These values are
787 // stashed in the TRec entries and are then checked in check_read_only
788 // to ensure that an atomic snapshot of all of these locations has been
791 static StgBool validate_and_acquire_ownership (StgTRecHeader *trec,
793 int retain_ownership) {
797 TRACE("%p : shake, pretending trec is invalid when it may not be", trec);
801 ASSERT ((trec -> state == TREC_ACTIVE) ||
802 (trec -> state == TREC_WAITING) ||
803 (trec -> state == TREC_CONDEMNED));
804 result = !((trec -> state) == TREC_CONDEMNED);
806 FOR_EACH_ENTRY(trec, e, {
809 if (acquire_all || entry_is_update(e)) {
810 TRACE("%p : trying to acquire %p", trec, s);
811 if (!cond_lock_tvar(trec, s, e -> expected_value)) {
812 TRACE("%p : failed to acquire %p", trec, s);
817 ASSERT(config_use_read_phase);
819 TRACE("%p : will need to check %p", trec, s);
820 if (s -> current_value != e -> expected_value) {
821 TRACE("%p : doesn't match", trec);
825 e -> num_updates = s -> num_updates;
826 if (s -> current_value != e -> expected_value) {
827 TRACE("%p : doesn't match (race)", trec);
831 TRACE("%p : need to check version %ld", trec, e -> num_updates);
838 if ((!result) || (!retain_ownership)) {
839 revert_ownership(trec, acquire_all);
845 // check_read_only : check that we've seen an atomic snapshot of the
846 // non-updated TVars accessed by a trec. This checks that the last TRec to
847 // commit an update to the TVar is unchanged since the value was stashed in
848 // validate_and_acquire_ownership. If no udpate is seen to any TVar than
849 // all of them contained their expected values at the start of the call to
852 // The paper "Concurrent programming without locks" (under submission), or
853 // Keir Fraser's PhD dissertation "Practical lock-free programming" discuss
854 // this kind of algorithm.
856 static StgBool check_read_only(StgTRecHeader *trec STG_UNUSED) {
857 StgBool result = TRUE;
859 ASSERT (config_use_read_phase);
861 FOR_EACH_ENTRY(trec, e, {
864 if (entry_is_read_only(e)) {
865 TRACE("%p : check_read_only for TVar %p, saw %ld", trec, s, e -> num_updates);
866 if (s -> num_updates != e -> num_updates) {
867 // ||s -> current_value != e -> expected_value) {
868 TRACE("%p : mismatch", trec);
880 /************************************************************************/
882 void stmPreGCHook() {
886 TRACE("stmPreGCHook");
887 for (i = 0; i < n_capabilities; i ++) {
888 Capability *cap = &capabilities[i];
889 cap -> free_tvar_watch_queues = END_STM_WATCH_QUEUE;
890 cap -> free_trec_chunks = END_STM_CHUNK_LIST;
891 cap -> free_trec_headers = NO_TREC;
896 /************************************************************************/
898 // check_read_only relies on version numbers held in TVars' "num_updates"
899 // fields not wrapping around while a transaction is committed. The version
900 // number is incremented each time an update is committed to the TVar
901 // This is unlikely to wrap around when 32-bit integers are used for the counts,
902 // but to ensure correctness we maintain a shared count on the maximum
903 // number of commit operations that may occur and check that this has
904 // not increased by more than 2^32 during a commit.
906 #define TOKEN_BATCH_SIZE 1024
908 static volatile StgInt64 max_commits = 0;
910 #if defined(THREADED_RTS)
911 static volatile StgBool token_locked = FALSE;
913 static void getTokenBatch(Capability *cap) {
914 while (cas((void *)&token_locked, FALSE, TRUE) == TRUE) { /* nothing */ }
915 max_commits += TOKEN_BATCH_SIZE;
916 TRACE("%p : cap got token batch, max_commits=%" FMT_Int64, cap, max_commits);
917 cap -> transaction_tokens = TOKEN_BATCH_SIZE;
918 token_locked = FALSE;
921 static void getToken(Capability *cap) {
922 if (cap -> transaction_tokens == 0) {
925 cap -> transaction_tokens --;
928 static void getToken(Capability *cap STG_UNUSED) {
933 /*......................................................................*/
935 StgTRecHeader *stmStartTransaction(Capability *cap,
936 StgTRecHeader *outer) {
938 TRACE("%p : stmStartTransaction with %d tokens",
940 cap -> transaction_tokens);
944 t = alloc_stg_trec_header(cap, outer);
945 TRACE("%p : stmStartTransaction()=%p", outer, t);
949 /*......................................................................*/
951 void stmAbortTransaction(Capability *cap,
952 StgTRecHeader *trec) {
954 TRACE("%p : stmAbortTransaction", trec);
955 ASSERT (trec != NO_TREC);
956 ASSERT ((trec -> state == TREC_ACTIVE) ||
957 (trec -> state == TREC_WAITING) ||
958 (trec -> state == TREC_CONDEMNED));
962 et = trec -> enclosing_trec;
964 // We're a top-level transaction: remove any watch queue entries that
966 TRACE("%p : aborting top-level transaction", trec);
968 if (trec -> state == TREC_WAITING) {
969 ASSERT (trec -> enclosing_trec == NO_TREC);
970 TRACE("%p : stmAbortTransaction aborting waiting transaction", trec);
971 remove_watch_queue_entries_for_trec(cap, trec);
975 // We're a nested transaction: merge our read set into our parent's
976 TRACE("%p : retaining read-set into parent %p", trec, et);
978 FOR_EACH_ENTRY(trec, e, {
979 StgTVar *s = e -> tvar;
980 merge_read_into(cap, et, s, e -> expected_value);
984 trec -> state = TREC_ABORTED;
987 TRACE("%p : stmAbortTransaction done", trec);
990 /*......................................................................*/
992 void stmFreeAbortedTRec(Capability *cap,
993 StgTRecHeader *trec) {
994 TRACE("%p : stmFreeAbortedTRec", trec);
995 ASSERT (trec != NO_TREC);
996 ASSERT ((trec -> state == TREC_CONDEMNED) ||
997 (trec -> state == TREC_ABORTED));
999 free_stg_trec_header(cap, trec);
1001 TRACE("%p : stmFreeAbortedTRec done", trec);
1004 /*......................................................................*/
1006 void stmCondemnTransaction(Capability *cap,
1007 StgTRecHeader *trec) {
1008 TRACE("%p : stmCondemnTransaction", trec);
1009 ASSERT (trec != NO_TREC);
1010 ASSERT ((trec -> state == TREC_ACTIVE) ||
1011 (trec -> state == TREC_WAITING) ||
1012 (trec -> state == TREC_CONDEMNED));
1015 if (trec -> state == TREC_WAITING) {
1016 ASSERT (trec -> enclosing_trec == NO_TREC);
1017 TRACE("%p : stmCondemnTransaction condemning waiting transaction", trec);
1018 remove_watch_queue_entries_for_trec(cap, trec);
1020 trec -> state = TREC_CONDEMNED;
1023 TRACE("%p : stmCondemnTransaction done", trec);
1026 /*......................................................................*/
1028 StgBool stmValidateNestOfTransactions(StgTRecHeader *trec) {
1032 TRACE("%p : stmValidateNestOfTransactions", trec);
1033 ASSERT(trec != NO_TREC);
1034 ASSERT((trec -> state == TREC_ACTIVE) ||
1035 (trec -> state == TREC_WAITING) ||
1036 (trec -> state == TREC_CONDEMNED));
1042 while (t != NO_TREC) {
1043 result &= validate_and_acquire_ownership(t, TRUE, FALSE);
1044 t = t -> enclosing_trec;
1047 if (!result && trec -> state != TREC_WAITING) {
1048 trec -> state = TREC_CONDEMNED;
1053 TRACE("%p : stmValidateNestOfTransactions()=%d", trec, result);
1057 /*......................................................................*/
1059 static TRecEntry *get_entry_for(StgTRecHeader *trec, StgTVar *tvar, StgTRecHeader **in) {
1060 TRecEntry *result = NULL;
1062 TRACE("%p : get_entry_for TVar %p", trec, tvar);
1063 ASSERT(trec != NO_TREC);
1066 FOR_EACH_ENTRY(trec, e, {
1067 if (e -> tvar == tvar) {
1075 trec = trec -> enclosing_trec;
1076 } while (result == NULL && trec != NO_TREC);
1081 /*......................................................................*/
1084 * Add/remove links between an invariant TVars. The caller must have
1085 * locked the TVars involved and the invariant.
1088 static void disconnect_invariant(Capability *cap,
1089 StgAtomicInvariant *inv) {
1090 StgTRecHeader *last_execution = inv -> last_execution;
1092 TRACE("unhooking last execution inv=%p trec=%p", inv, last_execution);
1094 FOR_EACH_ENTRY(last_execution, e, {
1095 StgTVar *s = e -> tvar;
1096 StgTVarWatchQueue *q = s -> first_watch_queue_entry;
1097 StgBool found = FALSE;
1098 TRACE(" looking for trec on tvar=%p", s);
1099 for (q = s -> first_watch_queue_entry;
1100 q != END_STM_WATCH_QUEUE;
1101 q = q -> next_queue_entry) {
1102 if (q -> closure == (StgClosure*)inv) {
1103 StgTVarWatchQueue *pq;
1104 StgTVarWatchQueue *nq;
1105 nq = q -> next_queue_entry;
1106 pq = q -> prev_queue_entry;
1107 if (nq != END_STM_WATCH_QUEUE) {
1108 nq -> prev_queue_entry = pq;
1110 if (pq != END_STM_WATCH_QUEUE) {
1111 pq -> next_queue_entry = nq;
1113 ASSERT (s -> first_watch_queue_entry == q);
1114 s -> first_watch_queue_entry = nq;
1116 TRACE(" found it in watch queue entry %p", q);
1117 free_stg_tvar_watch_queue(cap, q);
1124 inv -> last_execution = NO_TREC;
1127 static void connect_invariant_to_trec(Capability *cap,
1128 StgAtomicInvariant *inv,
1129 StgTRecHeader *my_execution) {
1130 TRACE("connecting execution inv=%p trec=%p", inv, my_execution);
1132 ASSERT(inv -> last_execution == NO_TREC);
1134 FOR_EACH_ENTRY(my_execution, e, {
1135 StgTVar *s = e -> tvar;
1136 StgTVarWatchQueue *q = alloc_stg_tvar_watch_queue(cap, (StgClosure*)inv);
1137 StgTVarWatchQueue *fq = s -> first_watch_queue_entry;
1139 // We leave "last_execution" holding the values that will be
1140 // in the heap after the transaction we're in the process
1141 // of committing has finished.
1142 TRecEntry *entry = get_entry_for(my_execution -> enclosing_trec, s, NULL);
1143 if (entry != NULL) {
1144 e -> expected_value = entry -> new_value;
1145 e -> new_value = entry -> new_value;
1148 TRACE(" linking trec on tvar=%p value=%p q=%p", s, e -> expected_value, q);
1149 q -> next_queue_entry = fq;
1150 q -> prev_queue_entry = END_STM_WATCH_QUEUE;
1151 if (fq != END_STM_WATCH_QUEUE) {
1152 fq -> prev_queue_entry = q;
1154 s -> first_watch_queue_entry = q;
1157 inv -> last_execution = my_execution;
1161 * Add a new invariant to the trec's list of invariants to check on commit
1163 void stmAddInvariantToCheck(Capability *cap,
1164 StgTRecHeader *trec,
1166 StgAtomicInvariant *invariant;
1167 StgInvariantCheckQueue *q;
1168 TRACE("%p : stmAddInvariantToCheck closure=%p", trec, code);
1169 ASSERT(trec != NO_TREC);
1170 ASSERT(trec -> state == TREC_ACTIVE ||
1171 trec -> state == TREC_CONDEMNED);
1174 // 1. Allocate an StgAtomicInvariant, set last_execution to NO_TREC
1175 // to signal that this is a new invariant in the current atomic block
1177 invariant = (StgAtomicInvariant *) allocate(cap, sizeofW(StgAtomicInvariant));
1178 TRACE("%p : stmAddInvariantToCheck allocated invariant=%p", trec, invariant);
1179 SET_HDR (invariant, &stg_ATOMIC_INVARIANT_info, CCS_SYSTEM);
1180 invariant -> code = code;
1181 invariant -> last_execution = NO_TREC;
1182 invariant -> lock = 0;
1184 // 2. Allocate an StgInvariantCheckQueue entry, link it to the current trec
1186 q = alloc_stg_invariant_check_queue(cap, invariant);
1187 TRACE("%p : stmAddInvariantToCheck allocated q=%p", trec, q);
1188 q -> invariant = invariant;
1189 q -> my_execution = NO_TREC;
1190 q -> next_queue_entry = trec -> invariants_to_check;
1191 trec -> invariants_to_check = q;
1193 TRACE("%p : stmAddInvariantToCheck done", trec);
1197 * Fill in the trec's list of invariants that might be violated by the
1198 * current transaction.
1201 StgInvariantCheckQueue *stmGetInvariantsToCheck(Capability *cap, StgTRecHeader *trec) {
1203 TRACE("%p : stmGetInvariantsToCheck, head was %p",
1205 trec -> invariants_to_check);
1207 ASSERT(trec != NO_TREC);
1208 ASSERT ((trec -> state == TREC_ACTIVE) ||
1209 (trec -> state == TREC_WAITING) ||
1210 (trec -> state == TREC_CONDEMNED));
1211 ASSERT(trec -> enclosing_trec == NO_TREC);
1214 c = trec -> current_chunk;
1215 while (c != END_STM_CHUNK_LIST) {
1217 for (i = 0; i < c -> next_entry_idx; i ++) {
1218 TRecEntry *e = &(c -> entries[i]);
1219 if (entry_is_update(e)) {
1220 StgTVar *s = e -> tvar;
1221 StgClosure *old = lock_tvar(trec, s);
1223 // Pick up any invariants on the TVar being updated
1226 StgTVarWatchQueue *q;
1227 TRACE("%p : checking for invariants on %p", trec, s);
1228 for (q = s -> first_watch_queue_entry;
1229 q != END_STM_WATCH_QUEUE;
1230 q = q -> next_queue_entry) {
1231 if (watcher_is_invariant(q)) {
1232 StgBool found = FALSE;
1233 StgInvariantCheckQueue *q2;
1234 TRACE("%p : Touching invariant %p", trec, q -> closure);
1235 for (q2 = trec -> invariants_to_check;
1236 q2 != END_INVARIANT_CHECK_QUEUE;
1237 q2 = q2 -> next_queue_entry) {
1238 if (q2 -> invariant == (StgAtomicInvariant*)(q -> closure)) {
1239 TRACE("%p : Already found %p", trec, q -> closure);
1246 StgInvariantCheckQueue *q3;
1247 TRACE("%p : Not already found %p", trec, q -> closure);
1248 q3 = alloc_stg_invariant_check_queue(cap,
1249 (StgAtomicInvariant*) q -> closure);
1250 q3 -> next_queue_entry = trec -> invariants_to_check;
1251 trec -> invariants_to_check = q3;
1256 unlock_tvar(trec, s, old, FALSE);
1259 c = c -> prev_chunk;
1264 TRACE("%p : stmGetInvariantsToCheck, head now %p",
1266 trec -> invariants_to_check);
1268 return (trec -> invariants_to_check);
1271 /*......................................................................*/
1273 StgBool stmCommitTransaction(Capability *cap, StgTRecHeader *trec) {
1275 StgInt64 max_commits_at_start = max_commits;
1276 StgBool touched_invariants;
1277 StgBool use_read_phase;
1279 TRACE("%p : stmCommitTransaction()", trec);
1280 ASSERT (trec != NO_TREC);
1284 ASSERT (trec -> enclosing_trec == NO_TREC);
1285 ASSERT ((trec -> state == TREC_ACTIVE) ||
1286 (trec -> state == TREC_CONDEMNED));
1288 // touched_invariants is true if we've written to a TVar with invariants
1289 // attached to it, or if we're trying to add a new invariant to the system.
1291 touched_invariants = (trec -> invariants_to_check != END_INVARIANT_CHECK_QUEUE);
1293 // If we have touched invariants then (i) lock the invariant, and (ii) add
1294 // the invariant's read set to our own. Step (i) is needed to serialize
1295 // concurrent transactions that attempt to make conflicting updates
1296 // to the invariant's trec (suppose it read from t1 and t2, and that one
1297 // concurrent transcation writes only to t1, and a second writes only to
1298 // t2). Step (ii) is needed so that both transactions will lock t1 and t2
1299 // to gain access to their wait lists (and hence be able to unhook the
1300 // invariant from both tvars).
1302 if (touched_invariants) {
1303 StgInvariantCheckQueue *q = trec -> invariants_to_check;
1304 TRACE("%p : locking invariants", trec);
1305 while (q != END_INVARIANT_CHECK_QUEUE) {
1306 StgTRecHeader *inv_old_trec;
1307 StgAtomicInvariant *inv;
1308 TRACE("%p : locking invariant %p", trec, q -> invariant);
1309 inv = q -> invariant;
1310 if (!lock_inv(inv)) {
1311 TRACE("%p : failed to lock %p", trec, inv);
1312 trec -> state = TREC_CONDEMNED;
1316 inv_old_trec = inv -> last_execution;
1317 if (inv_old_trec != NO_TREC) {
1318 StgTRecChunk *c = inv_old_trec -> current_chunk;
1319 while (c != END_STM_CHUNK_LIST) {
1321 for (i = 0; i < c -> next_entry_idx; i ++) {
1322 TRecEntry *e = &(c -> entries[i]);
1323 TRACE("%p : ensuring we lock TVars for %p", trec, e -> tvar);
1324 merge_read_into (cap, trec, e -> tvar, e -> expected_value);
1326 c = c -> prev_chunk;
1329 q = q -> next_queue_entry;
1331 TRACE("%p : finished locking invariants", trec);
1334 // Use a read-phase (i.e. don't lock TVars we've read but not updated) if
1335 // (i) the configuration lets us use a read phase, and (ii) we've not
1336 // touched or introduced any invariants.
1338 // In principle we could extend the implementation to support a read-phase
1339 // and invariants, but it complicates the logic: the links between
1340 // invariants and TVars are managed by the TVar watch queues which are
1341 // protected by the TVar's locks.
1343 use_read_phase = ((config_use_read_phase) && (!touched_invariants));
1345 result = validate_and_acquire_ownership(trec, (!use_read_phase), TRUE);
1347 // We now know that all the updated locations hold their expected values.
1348 ASSERT (trec -> state == TREC_ACTIVE);
1350 if (use_read_phase) {
1351 StgInt64 max_commits_at_end;
1352 StgInt64 max_concurrent_commits;
1353 TRACE("%p : doing read check", trec);
1354 result = check_read_only(trec);
1355 TRACE("%p : read-check %s", trec, result ? "succeeded" : "failed");
1357 max_commits_at_end = max_commits;
1358 max_concurrent_commits = ((max_commits_at_end - max_commits_at_start) +
1359 (n_capabilities * TOKEN_BATCH_SIZE));
1360 if (((max_concurrent_commits >> 32) > 0) || shake()) {
1366 // We now know that all of the read-only locations held their exepcted values
1367 // at the end of the call to validate_and_acquire_ownership. This forms the
1368 // linearization point of the commit.
1370 // 1. If we have touched or introduced any invariants then unhook them
1371 // from the TVars they depended on last time they were executed
1372 // and hook them on the TVars that they now depend on.
1373 if (touched_invariants) {
1374 StgInvariantCheckQueue *q = trec -> invariants_to_check;
1375 while (q != END_INVARIANT_CHECK_QUEUE) {
1376 StgAtomicInvariant *inv = q -> invariant;
1377 if (inv -> last_execution != NO_TREC) {
1378 disconnect_invariant(cap, inv);
1381 TRACE("%p : hooking up new execution trec=%p", trec, q -> my_execution);
1382 connect_invariant_to_trec(cap, inv, q -> my_execution);
1384 TRACE("%p : unlocking invariant %p", trec, inv);
1387 q = q -> next_queue_entry;
1391 // 2. Make the updates required by the transaction
1392 FOR_EACH_ENTRY(trec, e, {
1395 if ((!use_read_phase) || (e -> new_value != e -> expected_value)) {
1396 // Either the entry is an update or we're not using a read phase:
1397 // write the value back to the TVar, unlocking it if necessary.
1399 ACQ_ASSERT(tvar_is_locked(s, trec));
1400 TRACE("%p : writing %p to %p, waking waiters", trec, e -> new_value, s);
1401 unpark_waiters_on(cap,s);
1403 s -> num_updates ++;
1405 unlock_tvar(trec, s, e -> new_value, TRUE);
1407 ACQ_ASSERT(!tvar_is_locked(s, trec));
1410 revert_ownership(trec, FALSE);
1416 free_stg_trec_header(cap, trec);
1418 TRACE("%p : stmCommitTransaction()=%d", trec, result);
1423 /*......................................................................*/
1425 StgBool stmCommitNestedTransaction(Capability *cap, StgTRecHeader *trec) {
1428 ASSERT (trec != NO_TREC && trec -> enclosing_trec != NO_TREC);
1429 TRACE("%p : stmCommitNestedTransaction() into %p", trec, trec -> enclosing_trec);
1430 ASSERT ((trec -> state == TREC_ACTIVE) || (trec -> state == TREC_CONDEMNED));
1434 et = trec -> enclosing_trec;
1435 result = validate_and_acquire_ownership(trec, (!config_use_read_phase), TRUE);
1437 // We now know that all the updated locations hold their expected values.
1439 if (config_use_read_phase) {
1440 TRACE("%p : doing read check", trec);
1441 result = check_read_only(trec);
1444 // We now know that all of the read-only locations held their exepcted values
1445 // at the end of the call to validate_and_acquire_ownership. This forms the
1446 // linearization point of the commit.
1448 TRACE("%p : read-check succeeded", trec);
1449 FOR_EACH_ENTRY(trec, e, {
1450 // Merge each entry into the enclosing transaction record, release all
1455 if (entry_is_update(e)) {
1456 unlock_tvar(trec, s, e -> expected_value, FALSE);
1458 merge_update_into(cap, et, s, e -> expected_value, e -> new_value);
1459 ACQ_ASSERT(s -> current_value != (StgClosure *)trec);
1462 revert_ownership(trec, FALSE);
1468 free_stg_trec_header(cap, trec);
1470 TRACE("%p : stmCommitNestedTransaction()=%d", trec, result);
1475 /*......................................................................*/
1477 StgBool stmWait(Capability *cap, StgTSO *tso, StgTRecHeader *trec) {
1479 TRACE("%p : stmWait(%p)", trec, tso);
1480 ASSERT (trec != NO_TREC);
1481 ASSERT (trec -> enclosing_trec == NO_TREC);
1482 ASSERT ((trec -> state == TREC_ACTIVE) ||
1483 (trec -> state == TREC_CONDEMNED));
1486 result = validate_and_acquire_ownership(trec, TRUE, TRUE);
1488 // The transaction is valid so far so we can actually start waiting.
1489 // (Otherwise the transaction was not valid and the thread will have to
1492 // Put ourselves to sleep. We retain locks on all the TVars involved
1493 // until we are sound asleep : (a) on the wait queues, (b) BlockedOnSTM
1494 // in the TSO, (c) TREC_WAITING in the Trec.
1495 build_watch_queue_entries_for_trec(cap, tso, trec);
1497 trec -> state = TREC_WAITING;
1499 // We haven't released ownership of the transaction yet. The TSO
1500 // has been put on the wait queue for the TVars it is waiting for,
1501 // but we haven't yet tidied up the TSO's stack and made it safe
1502 // to wake up the TSO. Therefore, we must wait until the TSO is
1503 // safe to wake up before we release ownership - when all is well,
1504 // the runtime will call stmWaitUnlock() below, with the same
1509 free_stg_trec_header(cap, trec);
1512 TRACE("%p : stmWait(%p)=%d", trec, tso, result);
1518 stmWaitUnlock(Capability *cap STG_UNUSED, StgTRecHeader *trec) {
1519 revert_ownership(trec, TRUE);
1523 /*......................................................................*/
1525 StgBool stmReWait(Capability *cap, StgTSO *tso) {
1527 StgTRecHeader *trec = tso->trec;
1529 TRACE("%p : stmReWait", trec);
1530 ASSERT (trec != NO_TREC);
1531 ASSERT (trec -> enclosing_trec == NO_TREC);
1532 ASSERT ((trec -> state == TREC_WAITING) ||
1533 (trec -> state == TREC_CONDEMNED));
1536 result = validate_and_acquire_ownership(trec, TRUE, TRUE);
1537 TRACE("%p : validation %s", trec, result ? "succeeded" : "failed");
1539 // The transaction remains valid -- do nothing because it is already on
1541 ASSERT (trec -> state == TREC_WAITING);
1543 revert_ownership(trec, TRUE);
1545 // The transcation has become invalid. We can now remove it from the wait
1547 if (trec -> state != TREC_CONDEMNED) {
1548 remove_watch_queue_entries_for_trec (cap, trec);
1550 free_stg_trec_header(cap, trec);
1554 TRACE("%p : stmReWait()=%d", trec, result);
1558 /*......................................................................*/
1560 static StgClosure *read_current_value(StgTRecHeader *trec STG_UNUSED, StgTVar *tvar) {
1562 result = tvar -> current_value;
1564 #if defined(STM_FG_LOCKS)
1565 while (GET_INFO(UNTAG_CLOSURE(result)) == &stg_TREC_HEADER_info) {
1566 TRACE("%p : read_current_value(%p) saw %p", trec, tvar, result);
1567 result = tvar -> current_value;
1571 TRACE("%p : read_current_value(%p)=%p", trec, tvar, result);
1575 /*......................................................................*/
1577 StgClosure *stmReadTVar(Capability *cap,
1578 StgTRecHeader *trec,
1580 StgTRecHeader *entry_in = NULL;
1581 StgClosure *result = NULL;
1582 TRecEntry *entry = NULL;
1583 TRACE("%p : stmReadTVar(%p)", trec, tvar);
1584 ASSERT (trec != NO_TREC);
1585 ASSERT (trec -> state == TREC_ACTIVE ||
1586 trec -> state == TREC_CONDEMNED);
1588 entry = get_entry_for(trec, tvar, &entry_in);
1590 if (entry != NULL) {
1591 if (entry_in == trec) {
1592 // Entry found in our trec
1593 result = entry -> new_value;
1595 // Entry found in another trec
1596 TRecEntry *new_entry = get_new_entry(cap, trec);
1597 new_entry -> tvar = tvar;
1598 new_entry -> expected_value = entry -> expected_value;
1599 new_entry -> new_value = entry -> new_value;
1600 result = new_entry -> new_value;
1604 StgClosure *current_value = read_current_value(trec, tvar);
1605 TRecEntry *new_entry = get_new_entry(cap, trec);
1606 new_entry -> tvar = tvar;
1607 new_entry -> expected_value = current_value;
1608 new_entry -> new_value = current_value;
1609 result = current_value;
1612 TRACE("%p : stmReadTVar(%p)=%p", trec, tvar, result);
1616 /*......................................................................*/
1618 void stmWriteTVar(Capability *cap,
1619 StgTRecHeader *trec,
1621 StgClosure *new_value) {
1623 StgTRecHeader *entry_in = NULL;
1624 TRecEntry *entry = NULL;
1625 TRACE("%p : stmWriteTVar(%p, %p)", trec, tvar, new_value);
1626 ASSERT (trec != NO_TREC);
1627 ASSERT (trec -> state == TREC_ACTIVE ||
1628 trec -> state == TREC_CONDEMNED);
1630 entry = get_entry_for(trec, tvar, &entry_in);
1632 if (entry != NULL) {
1633 if (entry_in == trec) {
1634 // Entry found in our trec
1635 entry -> new_value = new_value;
1637 // Entry found in another trec
1638 TRecEntry *new_entry = get_new_entry(cap, trec);
1639 new_entry -> tvar = tvar;
1640 new_entry -> expected_value = entry -> expected_value;
1641 new_entry -> new_value = new_value;
1645 StgClosure *current_value = read_current_value(trec, tvar);
1646 TRecEntry *new_entry = get_new_entry(cap, trec);
1647 new_entry -> tvar = tvar;
1648 new_entry -> expected_value = current_value;
1649 new_entry -> new_value = new_value;
1652 TRACE("%p : stmWriteTVar done", trec);
1655 /*......................................................................*/
1657 StgTVar *stmNewTVar(Capability *cap,
1658 StgClosure *new_value) {
1660 result = (StgTVar *)allocate(cap, sizeofW(StgTVar));
1661 SET_HDR (result, &stg_TVAR_info, CCS_SYSTEM);
1662 result -> current_value = new_value;
1663 result -> first_watch_queue_entry = END_STM_WATCH_QUEUE;
1664 #if defined(THREADED_RTS)
1665 result -> num_updates = 0;
1670 /*......................................................................*/