diff options
author | Matt Joiner <anacrolix@gmail.com> | 2020-09-04 15:44:51 +1000 |
---|---|---|
committer | Matt Joiner <anacrolix@gmail.com> | 2020-09-10 09:22:35 +1000 |
commit | 2a64b89bdbc8fec645dd688bd0322f45c067f295 (patch) | |
tree | 6e1e7d5606d3a16a77ccdd47eb8654296862acf7 /var.go | |
parent | Add exponentially longer sleeping between transaction attempts (diff) | |
download | stm-2a64b89bdbc8fec645dd688bd0322f45c067f295.tar.gz stm-2a64b89bdbc8fec645dd688bd0322f45c067f295.tar.xz |
Add custom VarValue and const for sleep backoff
Diffstat (limited to 'var.go')
-rw-r--r-- | var.go | 28 |
1 files changed, 20 insertions, 8 deletions
@@ -7,18 +7,13 @@ import ( // Holds an STM variable. type Var struct { - state atomic.Value + value atomic.Value watchers sync.Map mu sync.Mutex } -func (v *Var) loadState() varSnapshot { - return v.state.Load().(varSnapshot) -} - func (v *Var) changeValue(new interface{}) { - version := v.loadState().version - v.state.Store(varSnapshot{version: version + 1, val: new}) + v.value.Store(v.value.Load().(VarValue).Set(new)) v.wakeWatchers() } @@ -40,6 +35,23 @@ type varSnapshot struct { // Returns a new STM variable. func NewVar(val interface{}) *Var { v := &Var{} - v.state.Store(varSnapshot{version: 0, val: val}) + v.value.Store(versionedValue{ + value: val, + }) return v } + +func NewCustomVar(val interface{}, changed func(interface{}, interface{}) bool) *Var { + v := &Var{} + v.value.Store(customVarValue{ + value: val, + changed: changed, + }) + return v +} + +func NewBuiltinEqVar(val interface{}) *Var { + return NewCustomVar(val, func(a, b interface{}) bool { + return a != b + }) +} |