-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_atomic.go
57 lines (45 loc) · 1.12 KB
/
mem_atomic.go
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
package adhoctx
import (
"fmt"
"log"
"sync/atomic"
"unsafe"
)
const DebugMemAtomic = false
type atomicCommitter struct {
current unsafe.Pointer
Counters struct {
Commits uint64
Rollbacks uint64
}
}
func (d *atomicCommitter) String() string {
return fmt.Sprintf(`list32[commits:%d,rollbacks:%d]`,
atomic.LoadUint64(&d.Counters.Commits),
atomic.LoadUint64(&d.Counters.Rollbacks),
)
}
func (d *atomicCommitter) unsafe_race_condition_commit(id byte, new unsafe.Pointer) {
if DebugMemAtomic {
log.Printf("ac set %c %p %p", id, d.current, new)
}
atomic.StorePointer(&d.current, new)
}
func (d *atomicCommitter) getCurrent(id byte) unsafe.Pointer {
if DebugMemAtomic {
log.Printf("ac get %c %p", id, d.current)
}
return atomic.LoadPointer(&d.current)
}
func (d *atomicCommitter) tryCommit(id byte, old, new unsafe.Pointer) bool {
if DebugMemAtomic {
log.Printf("ac try %c %p %p %p", id, d.current, old, new)
}
swapped := atomic.CompareAndSwapPointer(&d.current, old, new)
if swapped {
atomic.AddUint64(&d.Counters.Commits, 1)
return true
}
atomic.AddUint64(&d.Counters.Rollbacks, 1)
return false
}