blob: 2d0cb21f5f3419d2d585b201b8c27289b1c3cd3d (
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
35
36
37
38
39
40
|
package stm
import (
"sync/atomic"
"unsafe"
)
// Holds an STM variable.
type Var struct {
state *varSnapshot
watchers map[*Tx]struct{}
}
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) changeValue(new interface{}) {
version := v.loadState().version
atomic.StorePointer(v.addr(), unsafe.Pointer(&varSnapshot{version: version + 1, val: new}))
}
type varSnapshot struct {
val interface{}
version uint64
}
// Returns a new STM variable.
func NewVar(val interface{}) *Var {
return &Var{
state: &varSnapshot{
val: val,
},
watchers: make(map[*Tx]struct{}),
}
}
|