From 2a64b89bdbc8fec645dd688bd0322f45c067f295 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 4 Sep 2020 15:44:51 +1000 Subject: Add custom VarValue and const for sleep backoff --- var.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'var.go') diff --git a/var.go b/var.go index facad56..cdf9667 100644 --- a/var.go +++ b/var.go @@ -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 + }) +} -- cgit v1.2.3