From 80ec29f423aeb8f7de75c057b12b2e5cd8cb35ba Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 4 Nov 2019 15:14:33 +1100 Subject: Use atomic pointers for Var data --- tx.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'tx.go') diff --git a/tx.go b/tx.go index bae2006..f6d2fbf 100644 --- a/tx.go +++ b/tx.go @@ -14,9 +14,7 @@ type Tx struct { // Check that none of the logged values have changed since the transaction began. func (tx *Tx) verify() bool { for v, version := range tx.reads { - v.mu.Lock() - changed := v.version != version - v.mu.Unlock() + changed := v.loadState().version != version if changed { return false } @@ -27,10 +25,7 @@ func (tx *Tx) verify() bool { // Writes the values in the transaction log to their respective Vars. func (tx *Tx) commit() { for v, val := range tx.writes { - v.mu.Lock() - v.val = val - v.version++ - v.mu.Unlock() + v.changeValue(val) for tx := range v.watchers { tx.cond.Broadcast() delete(v.watchers, tx) @@ -59,13 +54,12 @@ func (tx *Tx) Get(v *Var) interface{} { if val, ok := tx.writes[v]; ok { return val } - v.mu.Lock() - defer v.mu.Unlock() + state := v.loadState() // If we haven't previously read v, record its version if _, ok := tx.reads[v]; !ok { - tx.reads[v] = v.version + tx.reads[v] = state.version } - return v.val + return state.val } // Set sets the value of a Var for the lifetime of the transaction. -- cgit v1.2.3