blob: 4859643a257d159c75a08fa81abd3551eff1d0fe (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package stm
import (
"sync"
"sync/atomic"
)
// Holds an STM variable.
type Var struct {
state 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})
}
type varSnapshot struct {
val interface{}
version uint64
}
// Returns a new STM variable.
func NewVar(val interface{}) *Var {
v := &Var{}
v.state.Store(varSnapshot{version: 0, val: val})
return v
}
|