diff options
author | Matt Joiner <anacrolix@gmail.com> | 2019-11-04 16:52:07 +1100 |
---|---|---|
committer | Matt Joiner <anacrolix@gmail.com> | 2019-11-04 16:52:07 +1100 |
commit | 1b68eff1b3d892480a1976dc51714e30aa60450b (patch) | |
tree | aa2777afc7a07ec6a0421c274c900855b1f7e829 | |
parent | Reduce transaction locking on Tx.wait (diff) | |
download | stm-1b68eff1b3d892480a1976dc51714e30aa60450b.tar.gz stm-1b68eff1b3d892480a1976dc51714e30aa60450b.tar.xz |
Use atomic.Value for Var state
-rw-r--r-- | var.go | 21 |
1 files changed, 7 insertions, 14 deletions
@@ -3,27 +3,22 @@ package stm import ( "sync" "sync/atomic" - "unsafe" ) // Holds an STM variable. type Var struct { - state *varSnapshot + state atomic.Value watchers sync.Map mu sync.Mutex } -func (v *Var) addr() *unsafe.Pointer { - return (*unsafe.Pointer)(unsafe.Pointer(&v.state)) -} - -func (v *Var) loadState() *varSnapshot { - return (*varSnapshot)(atomic.LoadPointer(v.addr())) +func (v *Var) loadState() varSnapshot { + return v.state.Load().(varSnapshot) } func (v *Var) changeValue(new interface{}) { version := v.loadState().version - atomic.StorePointer(v.addr(), unsafe.Pointer(&varSnapshot{version: version + 1, val: new})) + v.state.Store(varSnapshot{version: version + 1, val: new}) } type varSnapshot struct { @@ -33,9 +28,7 @@ type varSnapshot struct { // Returns a new STM variable. func NewVar(val interface{}) *Var { - return &Var{ - state: &varSnapshot{ - val: val, - }, - } + v := &Var{} + v.state.Store(varSnapshot{version: 0, val: val}) + return v } |