aboutsummaryrefslogtreecommitdiff
path: root/var.go
diff options
context:
space:
mode:
authorMatt Joiner <anacrolix@gmail.com>2020-09-04 15:44:51 +1000
committerMatt Joiner <anacrolix@gmail.com>2020-09-10 09:22:35 +1000
commit2a64b89bdbc8fec645dd688bd0322f45c067f295 (patch)
tree6e1e7d5606d3a16a77ccdd47eb8654296862acf7 /var.go
parentAdd exponentially longer sleeping between transaction attempts (diff)
downloadstm-2a64b89bdbc8fec645dd688bd0322f45c067f295.tar.gz
stm-2a64b89bdbc8fec645dd688bd0322f45c067f295.tar.xz
Add custom VarValue and const for sleep backoff
Diffstat (limited to 'var.go')
-rw-r--r--var.go28
1 files changed, 20 insertions, 8 deletions
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
+ })
+}