blob: bd2cf206fb44210f823015034c15b0db7ce630fe (
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package stm
import (
"sync"
)
var (
txPool = sync.Pool{New: func() interface{} {
tx := &Tx{
reads: make(map[*Var]uint64),
writes: make(map[*Var]interface{}),
}
tx.cond.L = &globalLock
return tx
}}
)
// Atomically executes the atomic function fn.
func Atomically(fn func(*Tx)) interface{} {
// run the transaction
tx := txPool.Get().(*Tx)
retry:
tx.reset()
var ret interface{}
if func() (retry bool) {
defer func() {
r := recover()
if r == nil {
return
}
if _ret, ok := r.(_return); ok {
ret = _ret.value
} else if r == Retry {
// wait for one of the variables we read to change before retrying
tx.wait()
retry = true
} else {
panic(r)
}
}()
fn(tx)
return false
}() {
goto retry
}
// verify the read log
globalLock.Lock()
if !tx.verify() {
globalLock.Unlock()
goto retry
}
// commit the write log and broadcast that variables have changed
tx.commit()
globalLock.Unlock()
tx.recycle()
return ret
}
// AtomicGet is a helper function that atomically reads a value.
func AtomicGet(v *Var) interface{} {
return v.loadState().val
}
// AtomicSet is a helper function that atomically writes a value.
func AtomicSet(v *Var, val interface{}) {
Atomically(func(tx *Tx) {
tx.Set(v, val)
})
}
// Compose is a helper function that composes multiple transactions into a
// single transaction.
func Compose(fns ...func(*Tx)) func(*Tx) {
return func(tx *Tx) {
for _, f := range fns {
f(tx)
}
}
}
// Select runs the supplied functions in order. Execution stops when a
// function succeeds without calling Retry. If no functions succeed, the
// entire selection will be retried.
func Select(fns ...func(*Tx)) func(*Tx) {
return func(tx *Tx) {
switch len(fns) {
case 0:
// empty Select blocks forever
tx.Retry()
case 1:
fns[0](tx)
default:
if catchRetry(fns[0], tx) {
Select(fns[1:]...)(tx)
}
}
}
}
|