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 field of each
78 * TVar. This may only be manipulated when holding that TVar's lock. In
79 * particular, when a thread is putting itself to sleep, it mustn't release
80 * the TVar's lock until it has added itself to the wait queue and marked its
81 * TSO as BlockedOnSTM -- this makes sure that other threads will know to wake it.
83 * ---------------------------------------------------------------------------*/
85 #include "PosixSource.h"
101 // ACQ_ASSERT is used for assertions which are only required for
102 // THREADED_RTS builds with fine-grained locking.
104 #if defined(STM_FG_LOCKS)
105 #define ACQ_ASSERT(_X) ASSERT(_X)
106 #define NACQ_ASSERT(_X) /*Nothing*/
108 #define ACQ_ASSERT(_X) /*Nothing*/
109 #define NACQ_ASSERT(_X) ASSERT(_X)
112 /*......................................................................*/
114 // If SHAKE is defined then validation will sometime spuriously fail. They helps test
115 // unusualy code paths if genuine contention is rare
117 #define TRACE(_x...) debugTrace(DEBUG_stm, "STM: " _x)
120 static const int do_shake = TRUE;
122 static const int do_shake = FALSE;
124 static int shake_ctr = 0;
125 static int shake_lim = 1;
127 static int shake(void) {
129 if (((shake_ctr++) % shake_lim) == 0) {
140 /*......................................................................*/
142 // Helper macros for iterating over entries within a transaction
145 #define FOR_EACH_ENTRY(_t,_x,CODE) do { \
146 StgTRecHeader *__t = (_t); \
147 StgTRecChunk *__c = __t -> current_chunk; \
148 StgWord __limit = __c -> next_entry_idx; \
149 TRACE("%p : FOR_EACH_ENTRY, current_chunk=%p limit=%ld", __t, __c, __limit); \
150 while (__c != END_STM_CHUNK_LIST) { \
152 for (__i = 0; __i < __limit; __i ++) { \
153 TRecEntry *_x = &(__c -> entries[__i]); \
154 do { CODE } while (0); \
156 __c = __c -> prev_chunk; \
157 __limit = TREC_CHUNK_NUM_ENTRIES; \
160 if (FALSE) goto exit_for_each; \
163 #define BREAK_FOR_EACH goto exit_for_each
165 /*......................................................................*/
167 // if REUSE_MEMORY is defined then attempt to re-use descriptors, log chunks,
168 // and wait queue entries without GC
172 /*......................................................................*/
174 #define IF_STM_UNIPROC(__X) do { } while (0)
175 #define IF_STM_CG_LOCK(__X) do { } while (0)
176 #define IF_STM_FG_LOCKS(__X) do { } while (0)
178 #if defined(STM_UNIPROC)
179 #undef IF_STM_UNIPROC
180 #define IF_STM_UNIPROC(__X) do { __X } while (0)
181 static const StgBool config_use_read_phase = FALSE;
183 static void lock_stm(StgTRecHeader *trec STG_UNUSED) {
184 TRACE("%p : lock_stm()", trec);
187 static void unlock_stm(StgTRecHeader *trec STG_UNUSED) {
188 TRACE("%p : unlock_stm()", trec);
191 static StgClosure *lock_tvar(StgTRecHeader *trec STG_UNUSED,
192 StgTVar *s STG_UNUSED) {
194 TRACE("%p : lock_tvar(%p)", trec, s);
195 result = s -> current_value;
199 static void unlock_tvar(StgTRecHeader *trec STG_UNUSED,
200 StgTVar *s STG_UNUSED,
202 StgBool force_update) {
203 TRACE("%p : unlock_tvar(%p)", trec, s);
205 s -> current_value = c;
209 static StgBool cond_lock_tvar(StgTRecHeader *trec STG_UNUSED,
210 StgTVar *s STG_UNUSED,
211 StgClosure *expected) {
213 TRACE("%p : cond_lock_tvar(%p, %p)", trec, s, expected);
214 result = s -> current_value;
215 TRACE("%p : %s", trec, (result == expected) ? "success" : "failure");
216 return (result == expected);
219 static StgBool lock_inv(StgAtomicInvariant *inv STG_UNUSED) {
220 // Nothing -- uniproc
224 static void unlock_inv(StgAtomicInvariant *inv STG_UNUSED) {
225 // Nothing -- uniproc
229 #if defined(STM_CG_LOCK) /*........................................*/
231 #undef IF_STM_CG_LOCK
232 #define IF_STM_CG_LOCK(__X) do { __X } while (0)
233 static const StgBool config_use_read_phase = FALSE;
234 static volatile StgTRecHeader *smp_locked = NULL;
236 static void lock_stm(StgTRecHeader *trec) {
237 while (cas(&smp_locked, NULL, trec) != NULL) { }
238 TRACE("%p : lock_stm()", trec);
241 static void unlock_stm(StgTRecHeader *trec STG_UNUSED) {
242 TRACE("%p : unlock_stm()", trec);
243 ASSERT (smp_locked == trec);
247 static StgClosure *lock_tvar(StgTRecHeader *trec STG_UNUSED,
248 StgTVar *s STG_UNUSED) {
250 TRACE("%p : lock_tvar(%p)", trec, s);
251 ASSERT (smp_locked == trec);
252 result = s -> current_value;
256 static void *unlock_tvar(StgTRecHeader *trec STG_UNUSED,
257 StgTVar *s STG_UNUSED,
259 StgBool force_update) {
260 TRACE("%p : unlock_tvar(%p, %p)", trec, s, c);
261 ASSERT (smp_locked == trec);
263 s -> current_value = c;
267 static StgBool cond_lock_tvar(StgTRecHeader *trec STG_UNUSED,
268 StgTVar *s STG_UNUSED,
269 StgClosure *expected) {
271 TRACE("%p : cond_lock_tvar(%p, %p)", trec, s, expected);
272 ASSERT (smp_locked == trec);
273 result = s -> current_value;
274 TRACE("%p : %d", result ? "success" : "failure");
275 return (result == expected);
278 static StgBool lock_inv(StgAtomicInvariant *inv STG_UNUSED) {
279 // Nothing -- protected by STM lock
283 static void unlock_inv(StgAtomicInvariant *inv STG_UNUSED) {
284 // Nothing -- protected by STM lock
288 #if defined(STM_FG_LOCKS) /*...................................*/
290 #undef IF_STM_FG_LOCKS
291 #define IF_STM_FG_LOCKS(__X) do { __X } while (0)
292 static const StgBool config_use_read_phase = TRUE;
294 static void lock_stm(StgTRecHeader *trec STG_UNUSED) {
295 TRACE("%p : lock_stm()", trec);
298 static void unlock_stm(StgTRecHeader *trec STG_UNUSED) {
299 TRACE("%p : unlock_stm()", trec);
302 static StgClosure *lock_tvar(StgTRecHeader *trec,
303 StgTVar *s STG_UNUSED) {
305 TRACE("%p : lock_tvar(%p)", trec, s);
308 result = s -> current_value;
309 } while (GET_INFO(result) == &stg_TREC_HEADER_info);
310 } while (cas((void *)&(s -> current_value),
311 (StgWord)result, (StgWord)trec) != (StgWord)result);
315 static void unlock_tvar(StgTRecHeader *trec STG_UNUSED,
318 StgBool force_update STG_UNUSED) {
319 TRACE("%p : unlock_tvar(%p, %p)", trec, s, c);
320 ASSERT(s -> current_value == (StgClosure *)trec);
321 s -> current_value = c;
324 static StgBool cond_lock_tvar(StgTRecHeader *trec,
326 StgClosure *expected) {
329 TRACE("%p : cond_lock_tvar(%p, %p)", trec, s, expected);
330 w = cas((void *)&(s -> current_value), (StgWord)expected, (StgWord)trec);
331 result = (StgClosure *)w;
332 TRACE("%p : %s", trec, result ? "success" : "failure");
333 return (result == expected);
336 static StgBool lock_inv(StgAtomicInvariant *inv) {
337 return (cas(&(inv -> lock), 0, 1) == 0);
340 static void unlock_inv(StgAtomicInvariant *inv) {
341 ASSERT(inv -> lock == 1);
346 /*......................................................................*/
348 static StgBool watcher_is_tso(StgTVarWatchQueue *q) {
349 StgClosure *c = q -> closure;
350 StgInfoTable *info = get_itbl(c);
351 return (info -> type) == TSO;
354 static StgBool watcher_is_invariant(StgTVarWatchQueue *q) {
355 StgClosure *c = q -> closure;
356 StgInfoTable *info = get_itbl(c);
357 return (info -> type) == ATOMIC_INVARIANT;
360 /*......................................................................*/
362 // Helper functions for thread blocking and unblocking
364 static void park_tso(StgTSO *tso) {
365 ASSERT(tso -> why_blocked == NotBlocked);
366 tso -> why_blocked = BlockedOnSTM;
367 tso -> block_info.closure = (StgClosure *) END_TSO_QUEUE;
368 TRACE("park_tso on tso=%p", tso);
371 static void unpark_tso(Capability *cap, StgTSO *tso) {
372 // We will continue unparking threads while they remain on one of the wait
373 // queues: it's up to the thread itself to remove it from the wait queues
374 // if it decides to do so when it is scheduled.
376 // Unblocking a TSO from BlockedOnSTM is done under the TSO lock,
377 // to avoid multiple CPUs unblocking the same TSO, and also to
378 // synchronise with throwTo().
380 if (tso -> why_blocked == BlockedOnSTM) {
381 TRACE("unpark_tso on tso=%p", tso);
384 TRACE("spurious unpark_tso on tso=%p", tso);
389 static void unpark_waiters_on(Capability *cap, StgTVar *s) {
390 StgTVarWatchQueue *q;
391 StgTVarWatchQueue *trail;
392 TRACE("unpark_waiters_on tvar=%p", s);
393 // unblock TSOs in reverse order, to be a bit fairer (#2319)
394 for (q = s -> first_watch_queue_entry, trail = q;
395 q != END_STM_WATCH_QUEUE;
396 q = q -> next_queue_entry) {
401 q != END_STM_WATCH_QUEUE;
402 q = q -> prev_queue_entry) {
403 if (watcher_is_tso(q)) {
404 unpark_tso(cap, (StgTSO *)(q -> closure));
409 /*......................................................................*/
411 // Helper functions for downstream allocation and initialization
413 static StgInvariantCheckQueue *new_stg_invariant_check_queue(Capability *cap,
414 StgAtomicInvariant *invariant) {
415 StgInvariantCheckQueue *result;
416 result = (StgInvariantCheckQueue *)allocateLocal(cap, sizeofW(StgInvariantCheckQueue));
417 SET_HDR (result, &stg_INVARIANT_CHECK_QUEUE_info, CCS_SYSTEM);
418 result -> invariant = invariant;
419 result -> my_execution = NO_TREC;
423 static StgTVarWatchQueue *new_stg_tvar_watch_queue(Capability *cap,
424 StgClosure *closure) {
425 StgTVarWatchQueue *result;
426 result = (StgTVarWatchQueue *)allocateLocal(cap, sizeofW(StgTVarWatchQueue));
427 SET_HDR (result, &stg_TVAR_WATCH_QUEUE_info, CCS_SYSTEM);
428 result -> closure = closure;
432 static StgTRecChunk *new_stg_trec_chunk(Capability *cap) {
433 StgTRecChunk *result;
434 result = (StgTRecChunk *)allocateLocal(cap, sizeofW(StgTRecChunk));
435 SET_HDR (result, &stg_TREC_CHUNK_info, CCS_SYSTEM);
436 result -> prev_chunk = END_STM_CHUNK_LIST;
437 result -> next_entry_idx = 0;
441 static StgTRecHeader *new_stg_trec_header(Capability *cap,
442 StgTRecHeader *enclosing_trec) {
443 StgTRecHeader *result;
444 result = (StgTRecHeader *) allocateLocal(cap, sizeofW(StgTRecHeader));
445 SET_HDR (result, &stg_TREC_HEADER_info, CCS_SYSTEM);
447 result -> enclosing_trec = enclosing_trec;
448 result -> current_chunk = new_stg_trec_chunk(cap);
449 result -> invariants_to_check = END_INVARIANT_CHECK_QUEUE;
451 if (enclosing_trec == NO_TREC) {
452 result -> state = TREC_ACTIVE;
454 ASSERT(enclosing_trec -> state == TREC_ACTIVE ||
455 enclosing_trec -> state == TREC_CONDEMNED);
456 result -> state = enclosing_trec -> state;
462 /*......................................................................*/
464 // Allocation / deallocation functions that retain per-capability lists
465 // of closures that can be re-used
467 static StgInvariantCheckQueue *alloc_stg_invariant_check_queue(Capability *cap,
468 StgAtomicInvariant *invariant) {
469 StgInvariantCheckQueue *result = NULL;
470 if (cap -> free_invariant_check_queues == END_INVARIANT_CHECK_QUEUE) {
471 result = new_stg_invariant_check_queue(cap, invariant);
473 result = cap -> free_invariant_check_queues;
474 result -> invariant = invariant;
475 result -> my_execution = NO_TREC;
476 cap -> free_invariant_check_queues = result -> next_queue_entry;
481 static StgTVarWatchQueue *alloc_stg_tvar_watch_queue(Capability *cap,
482 StgClosure *closure) {
483 StgTVarWatchQueue *result = NULL;
484 if (cap -> free_tvar_watch_queues == END_STM_WATCH_QUEUE) {
485 result = new_stg_tvar_watch_queue(cap, closure);
487 result = cap -> free_tvar_watch_queues;
488 result -> closure = closure;
489 cap -> free_tvar_watch_queues = result -> next_queue_entry;
494 static void free_stg_tvar_watch_queue(Capability *cap,
495 StgTVarWatchQueue *wq) {
496 #if defined(REUSE_MEMORY)
497 wq -> next_queue_entry = cap -> free_tvar_watch_queues;
498 cap -> free_tvar_watch_queues = wq;
502 static StgTRecChunk *alloc_stg_trec_chunk(Capability *cap) {
503 StgTRecChunk *result = NULL;
504 if (cap -> free_trec_chunks == END_STM_CHUNK_LIST) {
505 result = new_stg_trec_chunk(cap);
507 result = cap -> free_trec_chunks;
508 cap -> free_trec_chunks = result -> prev_chunk;
509 result -> prev_chunk = END_STM_CHUNK_LIST;
510 result -> next_entry_idx = 0;
515 static void free_stg_trec_chunk(Capability *cap,
517 #if defined(REUSE_MEMORY)
518 c -> prev_chunk = cap -> free_trec_chunks;
519 cap -> free_trec_chunks = c;
523 static StgTRecHeader *alloc_stg_trec_header(Capability *cap,
524 StgTRecHeader *enclosing_trec) {
525 StgTRecHeader *result = NULL;
526 if (cap -> free_trec_headers == NO_TREC) {
527 result = new_stg_trec_header(cap, enclosing_trec);
529 result = cap -> free_trec_headers;
530 cap -> free_trec_headers = result -> enclosing_trec;
531 result -> enclosing_trec = enclosing_trec;
532 result -> current_chunk -> next_entry_idx = 0;
533 result -> invariants_to_check = END_INVARIANT_CHECK_QUEUE;
534 if (enclosing_trec == NO_TREC) {
535 result -> state = TREC_ACTIVE;
537 ASSERT(enclosing_trec -> state == TREC_ACTIVE ||
538 enclosing_trec -> state == TREC_CONDEMNED);
539 result -> state = enclosing_trec -> state;
545 static void free_stg_trec_header(Capability *cap,
546 StgTRecHeader *trec) {
547 #if defined(REUSE_MEMORY)
548 StgTRecChunk *chunk = trec -> current_chunk -> prev_chunk;
549 while (chunk != END_STM_CHUNK_LIST) {
550 StgTRecChunk *prev_chunk = chunk -> prev_chunk;
551 free_stg_trec_chunk(cap, chunk);
554 trec -> current_chunk -> prev_chunk = END_STM_CHUNK_LIST;
555 trec -> enclosing_trec = cap -> free_trec_headers;
556 cap -> free_trec_headers = trec;
560 /*......................................................................*/
562 // Helper functions for managing waiting lists
564 static void build_watch_queue_entries_for_trec(Capability *cap,
566 StgTRecHeader *trec) {
567 ASSERT(trec != NO_TREC);
568 ASSERT(trec -> enclosing_trec == NO_TREC);
569 ASSERT(trec -> state == TREC_ACTIVE);
571 TRACE("%p : build_watch_queue_entries_for_trec()", trec);
573 FOR_EACH_ENTRY(trec, e, {
575 StgTVarWatchQueue *q;
576 StgTVarWatchQueue *fq;
578 TRACE("%p : adding tso=%p to watch queue for tvar=%p", trec, tso, s);
579 ACQ_ASSERT(s -> current_value == (StgClosure *)trec);
580 NACQ_ASSERT(s -> current_value == e -> expected_value);
581 fq = s -> first_watch_queue_entry;
582 q = alloc_stg_tvar_watch_queue(cap, (StgClosure*) tso);
583 q -> next_queue_entry = fq;
584 q -> prev_queue_entry = END_STM_WATCH_QUEUE;
585 if (fq != END_STM_WATCH_QUEUE) {
586 fq -> prev_queue_entry = q;
588 s -> first_watch_queue_entry = q;
589 e -> new_value = (StgClosure *) q;
593 static void remove_watch_queue_entries_for_trec(Capability *cap,
594 StgTRecHeader *trec) {
595 ASSERT(trec != NO_TREC);
596 ASSERT(trec -> enclosing_trec == NO_TREC);
597 ASSERT(trec -> state == TREC_WAITING ||
598 trec -> state == TREC_CONDEMNED);
600 TRACE("%p : remove_watch_queue_entries_for_trec()", trec);
602 FOR_EACH_ENTRY(trec, e, {
604 StgTVarWatchQueue *pq;
605 StgTVarWatchQueue *nq;
606 StgTVarWatchQueue *q;
609 saw = lock_tvar(trec, s);
610 q = (StgTVarWatchQueue *) (e -> new_value);
611 TRACE("%p : removing tso=%p from watch queue for tvar=%p",
615 ACQ_ASSERT(s -> current_value == (StgClosure *)trec);
616 nq = q -> next_queue_entry;
617 pq = q -> prev_queue_entry;
618 if (nq != END_STM_WATCH_QUEUE) {
619 nq -> prev_queue_entry = pq;
621 if (pq != END_STM_WATCH_QUEUE) {
622 pq -> next_queue_entry = nq;
624 ASSERT (s -> first_watch_queue_entry == q);
625 s -> first_watch_queue_entry = nq;
627 free_stg_tvar_watch_queue(cap, q);
628 unlock_tvar(trec, s, saw, FALSE);
632 /*......................................................................*/
634 static TRecEntry *get_new_entry(Capability *cap,
640 c = t -> current_chunk;
641 i = c -> next_entry_idx;
642 ASSERT(c != END_STM_CHUNK_LIST);
644 if (i < TREC_CHUNK_NUM_ENTRIES) {
645 // Continue to use current chunk
646 result = &(c -> entries[i]);
647 c -> next_entry_idx ++;
649 // Current chunk is full: allocate a fresh one
651 nc = alloc_stg_trec_chunk(cap);
652 nc -> prev_chunk = c;
653 nc -> next_entry_idx = 1;
654 t -> current_chunk = nc;
655 result = &(nc -> entries[0]);
661 /*......................................................................*/
663 static void merge_update_into(Capability *cap,
666 StgClosure *expected_value,
667 StgClosure *new_value) {
670 // Look for an entry in this trec
672 FOR_EACH_ENTRY(t, e, {
677 if (e -> expected_value != expected_value) {
678 // Must abort if the two entries start from different values
679 TRACE("%p : update entries inconsistent at %p (%p vs %p)",
680 t, tvar, e -> expected_value, expected_value);
681 t -> state = TREC_CONDEMNED;
683 e -> new_value = new_value;
689 // No entry so far in this trec
691 ne = get_new_entry(cap, t);
693 ne -> expected_value = expected_value;
694 ne -> new_value = new_value;
698 /*......................................................................*/
700 static void merge_read_into(Capability *cap,
703 StgClosure *expected_value) {
706 // Look for an entry in this trec
708 FOR_EACH_ENTRY(t, e, {
713 if (e -> expected_value != expected_value) {
714 // Must abort if the two entries start from different values
715 TRACE("%p : read entries inconsistent at %p (%p vs %p)",
716 t, tvar, e -> expected_value, expected_value);
717 t -> state = TREC_CONDEMNED;
724 // No entry so far in this trec
726 ne = get_new_entry(cap, t);
728 ne -> expected_value = expected_value;
729 ne -> new_value = expected_value;
733 /*......................................................................*/
735 static StgBool entry_is_update(TRecEntry *e) {
737 result = (e -> expected_value != e -> new_value);
741 #if defined(STM_FG_LOCKS)
742 static StgBool entry_is_read_only(TRecEntry *e) {
744 result = (e -> expected_value == e -> new_value);
748 static StgBool tvar_is_locked(StgTVar *s, StgTRecHeader *h) {
751 c = s -> current_value;
752 result = (c == (StgClosure *) h);
757 // revert_ownership : release a lock on a TVar, storing back
758 // the value that it held when the lock was acquired. "revert_all"
759 // is set in stmWait and stmReWait when we acquired locks on all of
760 // the TVars involved. "revert_all" is not set in commit operations
761 // where we don't lock TVars that have been read from but not updated.
763 static void revert_ownership(StgTRecHeader *trec STG_UNUSED,
764 StgBool revert_all STG_UNUSED) {
765 #if defined(STM_FG_LOCKS)
766 FOR_EACH_ENTRY(trec, e, {
767 if (revert_all || entry_is_update(e)) {
770 if (tvar_is_locked(s, trec)) {
771 unlock_tvar(trec, s, e -> expected_value, TRUE);
778 /*......................................................................*/
780 // validate_and_acquire_ownership : this performs the twin functions
781 // of checking that the TVars referred to by entries in trec hold the
782 // expected values and:
784 // - locking the TVar (on updated TVars during commit, or all TVars
787 // - recording the identity of the TRec who wrote the value seen in the
788 // TVar (on non-updated TVars during commit). These values are
789 // stashed in the TRec entries and are then checked in check_read_only
790 // to ensure that an atomic snapshot of all of these locations has been
793 static StgBool validate_and_acquire_ownership (StgTRecHeader *trec,
795 int retain_ownership) {
799 TRACE("%p : shake, pretending trec is invalid when it may not be", trec);
803 ASSERT ((trec -> state == TREC_ACTIVE) ||
804 (trec -> state == TREC_WAITING) ||
805 (trec -> state == TREC_CONDEMNED));
806 result = !((trec -> state) == TREC_CONDEMNED);
808 FOR_EACH_ENTRY(trec, e, {
811 if (acquire_all || entry_is_update(e)) {
812 TRACE("%p : trying to acquire %p", trec, s);
813 if (!cond_lock_tvar(trec, s, e -> expected_value)) {
814 TRACE("%p : failed to acquire %p", trec, s);
819 ASSERT(config_use_read_phase);
821 TRACE("%p : will need to check %p", trec, s);
822 if (s -> current_value != e -> expected_value) {
823 TRACE("%p : doesn't match", trec);
827 e -> num_updates = s -> num_updates;
828 if (s -> current_value != e -> expected_value) {
829 TRACE("%p : doesn't match (race)", trec);
833 TRACE("%p : need to check version %ld", trec, e -> num_updates);
840 if ((!result) || (!retain_ownership)) {
841 revert_ownership(trec, acquire_all);
847 // check_read_only : check that we've seen an atomic snapshot of the
848 // non-updated TVars accessed by a trec. This checks that the last TRec to
849 // commit an update to the TVar is unchanged since the value was stashed in
850 // validate_and_acquire_ownership. If no udpate is seen to any TVar than
851 // all of them contained their expected values at the start of the call to
854 // The paper "Concurrent programming without locks" (under submission), or
855 // Keir Fraser's PhD dissertation "Practical lock-free programming" discuss
856 // this kind of algorithm.
858 static StgBool check_read_only(StgTRecHeader *trec STG_UNUSED) {
859 StgBool result = TRUE;
861 ASSERT (config_use_read_phase);
863 FOR_EACH_ENTRY(trec, e, {
866 if (entry_is_read_only(e)) {
867 TRACE("%p : check_read_only for TVar %p, saw %ld", trec, s, e -> num_updates);
868 if (s -> num_updates != e -> num_updates) {
869 // ||s -> current_value != e -> expected_value) {
870 TRACE("%p : mismatch", trec);
882 /************************************************************************/
884 void stmPreGCHook() {
888 TRACE("stmPreGCHook");
889 for (i = 0; i < n_capabilities; i ++) {
890 Capability *cap = &capabilities[i];
891 cap -> free_tvar_watch_queues = END_STM_WATCH_QUEUE;
892 cap -> free_trec_chunks = END_STM_CHUNK_LIST;
893 cap -> free_trec_headers = NO_TREC;
898 /************************************************************************/
900 // check_read_only relies on version numbers held in TVars' "num_updates"
901 // fields not wrapping around while a transaction is committed. The version
902 // number is incremented each time an update is committed to the TVar
903 // This is unlikely to wrap around when 32-bit integers are used for the counts,
904 // but to ensure correctness we maintain a shared count on the maximum
905 // number of commit operations that may occur and check that this has
906 // not increased by more than 2^32 during a commit.
908 #define TOKEN_BATCH_SIZE 1024
910 static volatile StgInt64 max_commits = 0;
912 #if defined(THREADED_RTS)
913 static volatile StgBool token_locked = FALSE;
915 static void getTokenBatch(Capability *cap) {
916 while (cas((void *)&token_locked, FALSE, TRUE) == TRUE) { /* nothing */ }
917 max_commits += TOKEN_BATCH_SIZE;
918 TRACE("%p : cap got token batch, max_commits=%" FMT_Int64, cap, max_commits);
919 cap -> transaction_tokens = TOKEN_BATCH_SIZE;
920 token_locked = FALSE;
923 static void getToken(Capability *cap) {
924 if (cap -> transaction_tokens == 0) {
927 cap -> transaction_tokens --;
930 static void getToken(Capability *cap STG_UNUSED) {
935 /*......................................................................*/
937 StgTRecHeader *stmStartTransaction(Capability *cap,
938 StgTRecHeader *outer) {
940 TRACE("%p : stmStartTransaction with %d tokens",
942 cap -> transaction_tokens);
946 t = alloc_stg_trec_header(cap, outer);
947 TRACE("%p : stmStartTransaction()=%p", outer, t);
951 /*......................................................................*/
953 void stmAbortTransaction(Capability *cap,
954 StgTRecHeader *trec) {
956 TRACE("%p : stmAbortTransaction", trec);
957 ASSERT (trec != NO_TREC);
958 ASSERT ((trec -> state == TREC_ACTIVE) ||
959 (trec -> state == TREC_WAITING) ||
960 (trec -> state == TREC_CONDEMNED));
964 et = trec -> enclosing_trec;
966 // We're a top-level transaction: remove any watch queue entries that
968 TRACE("%p : aborting top-level transaction", trec);
970 if (trec -> state == TREC_WAITING) {
971 ASSERT (trec -> enclosing_trec == NO_TREC);
972 TRACE("%p : stmAbortTransaction aborting waiting transaction", trec);
973 remove_watch_queue_entries_for_trec(cap, trec);
977 // We're a nested transaction: merge our read set into our parent's
978 TRACE("%p : retaining read-set into parent %p", trec, et);
980 FOR_EACH_ENTRY(trec, e, {
981 StgTVar *s = e -> tvar;
982 merge_read_into(cap, et, s, e -> expected_value);
986 trec -> state = TREC_ABORTED;
989 TRACE("%p : stmAbortTransaction done", trec);
992 /*......................................................................*/
994 void stmFreeAbortedTRec(Capability *cap,
995 StgTRecHeader *trec) {
996 TRACE("%p : stmFreeAbortedTRec", trec);
997 ASSERT (trec != NO_TREC);
998 ASSERT ((trec -> state == TREC_CONDEMNED) ||
999 (trec -> state == TREC_ABORTED));
1001 free_stg_trec_header(cap, trec);
1003 TRACE("%p : stmFreeAbortedTRec done", trec);
1006 /*......................................................................*/
1008 void stmCondemnTransaction(Capability *cap,
1009 StgTRecHeader *trec) {
1010 TRACE("%p : stmCondemnTransaction", trec);
1011 ASSERT (trec != NO_TREC);
1012 ASSERT ((trec -> state == TREC_ACTIVE) ||
1013 (trec -> state == TREC_WAITING) ||
1014 (trec -> state == TREC_CONDEMNED));
1017 if (trec -> state == TREC_WAITING) {
1018 ASSERT (trec -> enclosing_trec == NO_TREC);
1019 TRACE("%p : stmCondemnTransaction condemning waiting transaction", trec);
1020 remove_watch_queue_entries_for_trec(cap, trec);
1022 trec -> state = TREC_CONDEMNED;
1025 TRACE("%p : stmCondemnTransaction done", trec);
1028 /*......................................................................*/
1030 StgTRecHeader *stmGetEnclosingTRec(StgTRecHeader *trec) {
1031 StgTRecHeader *outer;
1032 TRACE("%p : stmGetEnclosingTRec", trec);
1033 outer = trec -> enclosing_trec;
1034 TRACE("%p : stmGetEnclosingTRec()=%p", trec, outer);
1038 /*......................................................................*/
1040 StgBool stmValidateNestOfTransactions(StgTRecHeader *trec) {
1044 TRACE("%p : stmValidateNestOfTransactions", trec);
1045 ASSERT(trec != NO_TREC);
1046 ASSERT((trec -> state == TREC_ACTIVE) ||
1047 (trec -> state == TREC_WAITING) ||
1048 (trec -> state == TREC_CONDEMNED));
1054 while (t != NO_TREC) {
1055 result &= validate_and_acquire_ownership(t, TRUE, FALSE);
1056 t = t -> enclosing_trec;
1059 if (!result && trec -> state != TREC_WAITING) {
1060 trec -> state = TREC_CONDEMNED;
1065 TRACE("%p : stmValidateNestOfTransactions()=%d", trec, result);
1069 /*......................................................................*/
1071 static TRecEntry *get_entry_for(StgTRecHeader *trec, StgTVar *tvar, StgTRecHeader **in) {
1072 TRecEntry *result = NULL;
1074 TRACE("%p : get_entry_for TVar %p", trec, tvar);
1075 ASSERT(trec != NO_TREC);
1078 FOR_EACH_ENTRY(trec, e, {
1079 if (e -> tvar == tvar) {
1087 trec = trec -> enclosing_trec;
1088 } while (result == NULL && trec != NO_TREC);
1093 /*......................................................................*/
1096 * Add/remove links between an invariant TVars. The caller must have
1097 * locked the TVars involved and the invariant.
1100 static void disconnect_invariant(Capability *cap,
1101 StgAtomicInvariant *inv) {
1102 StgTRecHeader *last_execution = inv -> last_execution;
1104 TRACE("unhooking last execution inv=%p trec=%p", inv, last_execution);
1106 FOR_EACH_ENTRY(last_execution, e, {
1107 StgTVar *s = e -> tvar;
1108 StgTVarWatchQueue *q = s -> first_watch_queue_entry;
1109 StgBool found = FALSE;
1110 TRACE(" looking for trec on tvar=%p", s);
1111 for (q = s -> first_watch_queue_entry;
1112 q != END_STM_WATCH_QUEUE;
1113 q = q -> next_queue_entry) {
1114 if (q -> closure == (StgClosure*)inv) {
1115 StgTVarWatchQueue *pq;
1116 StgTVarWatchQueue *nq;
1117 nq = q -> next_queue_entry;
1118 pq = q -> prev_queue_entry;
1119 if (nq != END_STM_WATCH_QUEUE) {
1120 nq -> prev_queue_entry = pq;
1122 if (pq != END_STM_WATCH_QUEUE) {
1123 pq -> next_queue_entry = nq;
1125 ASSERT (s -> first_watch_queue_entry == q);
1126 s -> first_watch_queue_entry = nq;
1128 TRACE(" found it in watch queue entry %p", q);
1129 free_stg_tvar_watch_queue(cap, q);
1136 inv -> last_execution = NO_TREC;
1139 static void connect_invariant_to_trec(Capability *cap,
1140 StgAtomicInvariant *inv,
1141 StgTRecHeader *my_execution) {
1142 TRACE("connecting execution inv=%p trec=%p", inv, my_execution);
1144 ASSERT(inv -> last_execution == NO_TREC);
1146 FOR_EACH_ENTRY(my_execution, e, {
1147 StgTVar *s = e -> tvar;
1148 StgTVarWatchQueue *q = alloc_stg_tvar_watch_queue(cap, (StgClosure*)inv);
1149 StgTVarWatchQueue *fq = s -> first_watch_queue_entry;
1151 // We leave "last_execution" holding the values that will be
1152 // in the heap after the transaction we're in the process
1153 // of committing has finished.
1154 TRecEntry *entry = get_entry_for(my_execution -> enclosing_trec, s, NULL);
1155 if (entry != NULL) {
1156 e -> expected_value = entry -> new_value;
1157 e -> new_value = entry -> new_value;
1160 TRACE(" linking trec on tvar=%p value=%p q=%p", s, e -> expected_value, q);
1161 q -> next_queue_entry = fq;
1162 q -> prev_queue_entry = END_STM_WATCH_QUEUE;
1163 if (fq != END_STM_WATCH_QUEUE) {
1164 fq -> prev_queue_entry = q;
1166 s -> first_watch_queue_entry = q;
1169 inv -> last_execution = my_execution;
1173 * Add a new invariant to the trec's list of invariants to check on commit
1175 void stmAddInvariantToCheck(Capability *cap,
1176 StgTRecHeader *trec,
1178 StgAtomicInvariant *invariant;
1179 StgInvariantCheckQueue *q;
1180 TRACE("%p : stmAddInvariantToCheck closure=%p", trec, code);
1181 ASSERT(trec != NO_TREC);
1182 ASSERT(trec -> state == TREC_ACTIVE ||
1183 trec -> state == TREC_CONDEMNED);
1186 // 1. Allocate an StgAtomicInvariant, set last_execution to NO_TREC
1187 // to signal that this is a new invariant in the current atomic block
1189 invariant = (StgAtomicInvariant *) allocateLocal(cap, sizeofW(StgAtomicInvariant));
1190 TRACE("%p : stmAddInvariantToCheck allocated invariant=%p", trec, invariant);
1191 SET_HDR (invariant, &stg_ATOMIC_INVARIANT_info, CCS_SYSTEM);
1192 invariant -> code = code;
1193 invariant -> last_execution = NO_TREC;
1195 // 2. Allocate an StgInvariantCheckQueue entry, link it to the current trec
1197 q = alloc_stg_invariant_check_queue(cap, invariant);
1198 TRACE("%p : stmAddInvariantToCheck allocated q=%p", trec, q);
1199 q -> invariant = invariant;
1200 q -> my_execution = NO_TREC;
1201 q -> next_queue_entry = trec -> invariants_to_check;
1202 trec -> invariants_to_check = q;
1204 TRACE("%p : stmAddInvariantToCheck done", trec);
1208 * Fill in the trec's list of invariants that might be violated by the
1209 * current transaction.
1212 StgInvariantCheckQueue *stmGetInvariantsToCheck(Capability *cap, StgTRecHeader *trec) {
1214 TRACE("%p : stmGetInvariantsToCheck, head was %p",
1216 trec -> invariants_to_check);
1218 ASSERT(trec != NO_TREC);
1219 ASSERT ((trec -> state == TREC_ACTIVE) ||
1220 (trec -> state == TREC_WAITING) ||
1221 (trec -> state == TREC_CONDEMNED));
1222 ASSERT(trec -> enclosing_trec == NO_TREC);
1225 c = trec -> current_chunk;
1226 while (c != END_STM_CHUNK_LIST) {
1228 for (i = 0; i < c -> next_entry_idx; i ++) {
1229 TRecEntry *e = &(c -> entries[i]);
1230 if (entry_is_update(e)) {
1231 StgTVar *s = e -> tvar;
1232 StgClosure *old = lock_tvar(trec, s);
1234 // Pick up any invariants on the TVar being updated
1237 StgTVarWatchQueue *q;
1238 TRACE("%p : checking for invariants on %p", trec, s);
1239 for (q = s -> first_watch_queue_entry;
1240 q != END_STM_WATCH_QUEUE;
1241 q = q -> next_queue_entry) {
1242 if (watcher_is_invariant(q)) {
1243 StgBool found = FALSE;
1244 StgInvariantCheckQueue *q2;
1245 TRACE("%p : Touching invariant %p", trec, q -> closure);
1246 for (q2 = trec -> invariants_to_check;
1247 q2 != END_INVARIANT_CHECK_QUEUE;
1248 q2 = q2 -> next_queue_entry) {
1249 if (q2 -> invariant == (StgAtomicInvariant*)(q -> closure)) {
1250 TRACE("%p : Already found %p", trec, q -> closure);
1257 StgInvariantCheckQueue *q3;
1258 TRACE("%p : Not already found %p", trec, q -> closure);
1259 q3 = alloc_stg_invariant_check_queue(cap,
1260 (StgAtomicInvariant*) q -> closure);
1261 q3 -> next_queue_entry = trec -> invariants_to_check;
1262 trec -> invariants_to_check = q3;
1267 unlock_tvar(trec, s, old, FALSE);
1270 c = c -> prev_chunk;
1275 TRACE("%p : stmGetInvariantsToCheck, head now %p",
1277 trec -> invariants_to_check);
1279 return (trec -> invariants_to_check);
1282 /*......................................................................*/
1284 StgBool stmCommitTransaction(Capability *cap, StgTRecHeader *trec) {
1286 StgInt64 max_commits_at_start = max_commits;
1287 StgBool touched_invariants;
1288 StgBool use_read_phase;
1290 TRACE("%p : stmCommitTransaction()", trec);
1291 ASSERT (trec != NO_TREC);
1295 ASSERT (trec -> enclosing_trec == NO_TREC);
1296 ASSERT ((trec -> state == TREC_ACTIVE) ||
1297 (trec -> state == TREC_CONDEMNED));
1299 // touched_invariants is true if we've written to a TVar with invariants
1300 // attached to it, or if we're trying to add a new invariant to the system.
1302 touched_invariants = (trec -> invariants_to_check != END_INVARIANT_CHECK_QUEUE);
1304 // If we have touched invariants then (i) lock the invariant, and (ii) add
1305 // the invariant's read set to our own. Step (i) is needed to serialize
1306 // concurrent transactions that attempt to make conflicting updates
1307 // to the invariant's trec (suppose it read from t1 and t2, and that one
1308 // concurrent transcation writes only to t1, and a second writes only to
1309 // t2). Step (ii) is needed so that both transactions will lock t1 and t2
1310 // to gain access to their wait lists (and hence be able to unhook the
1311 // invariant from both tvars).
1313 if (touched_invariants) {
1314 StgInvariantCheckQueue *q = trec -> invariants_to_check;
1315 TRACE("%p : locking invariants", trec);
1316 while (q != END_INVARIANT_CHECK_QUEUE) {
1317 StgTRecHeader *inv_old_trec;
1318 StgAtomicInvariant *inv;
1319 TRACE("%p : locking invariant %p", trec, q -> invariant);
1320 inv = q -> invariant;
1321 if (!lock_inv(inv)) {
1322 TRACE("%p : failed to lock %p", trec, inv);
1323 trec -> state = TREC_CONDEMNED;
1327 inv_old_trec = inv -> last_execution;
1328 if (inv_old_trec != NO_TREC) {
1329 StgTRecChunk *c = inv_old_trec -> current_chunk;
1330 while (c != END_STM_CHUNK_LIST) {
1332 for (i = 0; i < c -> next_entry_idx; i ++) {
1333 TRecEntry *e = &(c -> entries[i]);
1334 TRACE("%p : ensuring we lock TVars for %p", trec, e -> tvar);
1335 merge_read_into (cap, trec, e -> tvar, e -> expected_value);
1337 c = c -> prev_chunk;
1340 q = q -> next_queue_entry;
1342 TRACE("%p : finished locking invariants", trec);
1345 // Use a read-phase (i.e. don't lock TVars we've read but not updated) if
1346 // (i) the configuration lets us use a read phase, and (ii) we've not
1347 // touched or introduced any invariants.
1349 // In principle we could extend the implementation to support a read-phase
1350 // and invariants, but it complicates the logic: the links between
1351 // invariants and TVars are managed by the TVar watch queues which are
1352 // protected by the TVar's locks.
1354 use_read_phase = ((config_use_read_phase) && (!touched_invariants));
1356 result = validate_and_acquire_ownership(trec, (!use_read_phase), TRUE);
1358 // We now know that all the updated locations hold their expected values.
1359 ASSERT (trec -> state == TREC_ACTIVE);
1361 if (use_read_phase) {
1362 StgInt64 max_commits_at_end;
1363 StgInt64 max_concurrent_commits;
1364 TRACE("%p : doing read check", trec);
1365 result = check_read_only(trec);
1366 TRACE("%p : read-check %s", trec, result ? "succeeded" : "failed");
1368 max_commits_at_end = max_commits;
1369 max_concurrent_commits = ((max_commits_at_end - max_commits_at_start) +
1370 (n_capabilities * TOKEN_BATCH_SIZE));
1371 if (((max_concurrent_commits >> 32) > 0) || shake()) {
1377 // We now know that all of the read-only locations held their exepcted values
1378 // at the end of the call to validate_and_acquire_ownership. This forms the
1379 // linearization point of the commit.
1381 // 1. If we have touched or introduced any invariants then unhook them
1382 // from the TVars they depended on last time they were executed
1383 // and hook them on the TVars that they now depend on.
1384 if (touched_invariants) {
1385 StgInvariantCheckQueue *q = trec -> invariants_to_check;
1386 while (q != END_INVARIANT_CHECK_QUEUE) {
1387 StgAtomicInvariant *inv = q -> invariant;
1388 if (inv -> last_execution != NO_TREC) {
1389 disconnect_invariant(cap, inv);
1392 TRACE("%p : hooking up new execution trec=%p", trec, q -> my_execution);
1393 connect_invariant_to_trec(cap, inv, q -> my_execution);
1395 TRACE("%p : unlocking invariant %p", trec, inv);
1398 q = q -> next_queue_entry;
1402 // 2. Make the updates required by the transaction
1403 FOR_EACH_ENTRY(trec, e, {
1406 if ((!use_read_phase) || (e -> new_value != e -> expected_value)) {
1407 // Either the entry is an update or we're not using a read phase:
1408 // write the value back to the TVar, unlocking it if necessary.
1410 ACQ_ASSERT(tvar_is_locked(s, trec));
1411 TRACE("%p : writing %p to %p, waking waiters", trec, e -> new_value, s);
1412 unpark_waiters_on(cap,s);
1414 s -> num_updates ++;
1416 unlock_tvar(trec, s, e -> new_value, TRUE);
1418 ACQ_ASSERT(!tvar_is_locked(s, trec));
1421 revert_ownership(trec, FALSE);
1427 free_stg_trec_header(cap, trec);
1429 TRACE("%p : stmCommitTransaction()=%d", trec, result);
1434 /*......................................................................*/
1436 StgBool stmCommitNestedTransaction(Capability *cap, StgTRecHeader *trec) {
1439 ASSERT (trec != NO_TREC && trec -> enclosing_trec != NO_TREC);
1440 TRACE("%p : stmCommitNestedTransaction() into %p", trec, trec -> enclosing_trec);
1441 ASSERT ((trec -> state == TREC_ACTIVE) || (trec -> state == TREC_CONDEMNED));
1445 et = trec -> enclosing_trec;
1446 result = validate_and_acquire_ownership(trec, (!config_use_read_phase), TRUE);
1448 // We now know that all the updated locations hold their expected values.
1450 if (config_use_read_phase) {
1451 TRACE("%p : doing read check", trec);
1452 result = check_read_only(trec);
1455 // We now know that all of the read-only locations held their exepcted values
1456 // at the end of the call to validate_and_acquire_ownership. This forms the
1457 // linearization point of the commit.
1459 TRACE("%p : read-check succeeded", trec);
1460 FOR_EACH_ENTRY(trec, e, {
1461 // Merge each entry into the enclosing transaction record, release all
1466 if (entry_is_update(e)) {
1467 unlock_tvar(trec, s, e -> expected_value, FALSE);
1469 merge_update_into(cap, et, s, e -> expected_value, e -> new_value);
1470 ACQ_ASSERT(s -> current_value != (StgClosure *)trec);
1473 revert_ownership(trec, FALSE);
1479 free_stg_trec_header(cap, trec);
1481 TRACE("%p : stmCommitNestedTransaction()=%d", trec, result);
1486 /*......................................................................*/
1488 StgBool stmWait(Capability *cap, StgTSO *tso, StgTRecHeader *trec) {
1490 TRACE("%p : stmWait(%p)", trec, tso);
1491 ASSERT (trec != NO_TREC);
1492 ASSERT (trec -> enclosing_trec == NO_TREC);
1493 ASSERT ((trec -> state == TREC_ACTIVE) ||
1494 (trec -> state == TREC_CONDEMNED));
1497 result = validate_and_acquire_ownership(trec, TRUE, TRUE);
1499 // The transaction is valid so far so we can actually start waiting.
1500 // (Otherwise the transaction was not valid and the thread will have to
1503 // Put ourselves to sleep. We retain locks on all the TVars involved
1504 // until we are sound asleep : (a) on the wait queues, (b) BlockedOnSTM
1505 // in the TSO, (c) TREC_WAITING in the Trec.
1506 build_watch_queue_entries_for_trec(cap, tso, trec);
1508 trec -> state = TREC_WAITING;
1510 // We haven't released ownership of the transaction yet. The TSO
1511 // has been put on the wait queue for the TVars it is waiting for,
1512 // but we haven't yet tidied up the TSO's stack and made it safe
1513 // to wake up the TSO. Therefore, we must wait until the TSO is
1514 // safe to wake up before we release ownership - when all is well,
1515 // the runtime will call stmWaitUnlock() below, with the same
1520 free_stg_trec_header(cap, trec);
1523 TRACE("%p : stmWait(%p)=%d", trec, tso, result);
1529 stmWaitUnlock(Capability *cap STG_UNUSED, StgTRecHeader *trec) {
1530 revert_ownership(trec, TRUE);
1534 /*......................................................................*/
1536 StgBool stmReWait(Capability *cap, StgTSO *tso) {
1538 StgTRecHeader *trec = tso->trec;
1540 TRACE("%p : stmReWait", trec);
1541 ASSERT (trec != NO_TREC);
1542 ASSERT (trec -> enclosing_trec == NO_TREC);
1543 ASSERT ((trec -> state == TREC_WAITING) ||
1544 (trec -> state == TREC_CONDEMNED));
1547 result = validate_and_acquire_ownership(trec, TRUE, TRUE);
1548 TRACE("%p : validation %s", trec, result ? "succeeded" : "failed");
1550 // The transaction remains valid -- do nothing because it is already on
1552 ASSERT (trec -> state == TREC_WAITING);
1554 revert_ownership(trec, TRUE);
1556 // The transcation has become invalid. We can now remove it from the wait
1558 if (trec -> state != TREC_CONDEMNED) {
1559 remove_watch_queue_entries_for_trec (cap, trec);
1561 free_stg_trec_header(cap, trec);
1565 TRACE("%p : stmReWait()=%d", trec, result);
1569 /*......................................................................*/
1571 static StgClosure *read_current_value(StgTRecHeader *trec STG_UNUSED, StgTVar *tvar) {
1573 result = tvar -> current_value;
1575 #if defined(STM_FG_LOCKS)
1576 while (GET_INFO(result) == &stg_TREC_HEADER_info) {
1577 TRACE("%p : read_current_value(%p) saw %p", trec, tvar, result);
1578 result = tvar -> current_value;
1582 TRACE("%p : read_current_value(%p)=%p", trec, tvar, result);
1586 /*......................................................................*/
1588 StgClosure *stmReadTVar(Capability *cap,
1589 StgTRecHeader *trec,
1591 StgTRecHeader *entry_in = NULL;
1592 StgClosure *result = NULL;
1593 TRecEntry *entry = NULL;
1594 TRACE("%p : stmReadTVar(%p)", trec, tvar);
1595 ASSERT (trec != NO_TREC);
1596 ASSERT (trec -> state == TREC_ACTIVE ||
1597 trec -> state == TREC_CONDEMNED);
1599 entry = get_entry_for(trec, tvar, &entry_in);
1601 if (entry != NULL) {
1602 if (entry_in == trec) {
1603 // Entry found in our trec
1604 result = entry -> new_value;
1606 // Entry found in another trec
1607 TRecEntry *new_entry = get_new_entry(cap, trec);
1608 new_entry -> tvar = tvar;
1609 new_entry -> expected_value = entry -> expected_value;
1610 new_entry -> new_value = entry -> new_value;
1611 result = new_entry -> new_value;
1615 StgClosure *current_value = read_current_value(trec, tvar);
1616 TRecEntry *new_entry = get_new_entry(cap, trec);
1617 new_entry -> tvar = tvar;
1618 new_entry -> expected_value = current_value;
1619 new_entry -> new_value = current_value;
1620 result = current_value;
1623 TRACE("%p : stmReadTVar(%p)=%p", trec, tvar, result);
1627 /*......................................................................*/
1629 void stmWriteTVar(Capability *cap,
1630 StgTRecHeader *trec,
1632 StgClosure *new_value) {
1634 StgTRecHeader *entry_in = NULL;
1635 TRecEntry *entry = NULL;
1636 TRACE("%p : stmWriteTVar(%p, %p)", trec, tvar, new_value);
1637 ASSERT (trec != NO_TREC);
1638 ASSERT (trec -> state == TREC_ACTIVE ||
1639 trec -> state == TREC_CONDEMNED);
1641 entry = get_entry_for(trec, tvar, &entry_in);
1643 if (entry != NULL) {
1644 if (entry_in == trec) {
1645 // Entry found in our trec
1646 entry -> new_value = new_value;
1648 // Entry found in another trec
1649 TRecEntry *new_entry = get_new_entry(cap, trec);
1650 new_entry -> tvar = tvar;
1651 new_entry -> expected_value = entry -> expected_value;
1652 new_entry -> new_value = new_value;
1656 StgClosure *current_value = read_current_value(trec, tvar);
1657 TRecEntry *new_entry = get_new_entry(cap, trec);
1658 new_entry -> tvar = tvar;
1659 new_entry -> expected_value = current_value;
1660 new_entry -> new_value = new_value;
1663 TRACE("%p : stmWriteTVar done", trec);
1666 /*......................................................................*/
1668 StgTVar *stmNewTVar(Capability *cap,
1669 StgClosure *new_value) {
1671 result = (StgTVar *)allocateLocal(cap, sizeofW(StgTVar));
1672 SET_HDR (result, &stg_TVAR_info, CCS_SYSTEM);
1673 result -> current_value = new_value;
1674 result -> first_watch_queue_entry = END_STM_WATCH_QUEUE;
1675 #if defined(THREADED_RTS)
1676 result -> num_updates = 0;
1681 /*......................................................................*/