TheRiver | blog

You have reached the world's edge, none but devils play past here

0%

golang atomic.value

Package atomic provides low-level atomic memory primitives useful for implementing synchronization algorithms.

These functions require great care to be used correctly. Except for special, low-level applications, synchronization is better done with channels or the facilities of the sync package. Share memory by communicating; don’t communicate by sharing memory.

atomic.value

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

// A Value provides an atomic load and store of a consistently typed value.
// The zero value for a Value returns nil from Load.
// Once Store has been called, a Value must not be copied.
//
// A Value must not be copied after first use.
type Value struct {
v interface{}
}

// Load returns the value set by the most recent Store.
// It returns nil if there has been no call to Store for this Value.
func (v *Value) Load() (x interface{}) {
vp := (*ifaceWords)(unsafe.Pointer(v))
typ := LoadPointer(&vp.typ)
if typ == nil || uintptr(typ) == ^uintptr(0) {
// First store not yet completed.
return nil
}
data := LoadPointer(&vp.data)
xp := (*ifaceWords)(unsafe.Pointer(&x))
xp.typ = typ
xp.data = data
return
}

// ifaceWords is interface{} internal representation.
type ifaceWords struct {
typ unsafe.Pointer
data unsafe.Pointer
}

// Store sets the value of the Value to x.
// All calls to Store for a given Value must use values of the same concrete type.
// Store of an inconsistent type panics, as does Store(nil).
func (v *Value) Store(x interface{}) {
if x == nil {
panic("sync/atomic: store of nil value into Value")
}
vp := (*ifaceWords)(unsafe.Pointer(v)) //old value,类型一致,只改data
xp := (*ifaceWords)(unsafe.Pointer(&x)) //new value
for {
//原子读
typ := LoadPointer(&vp.typ)
//case1 nil
if typ == nil {
// Attempt to start first store.
// Disable preemption so that other goroutines can use
// active spin wait to wait for completion; and so that
// GC does not see the fake type accidentally.
runtime_procPin()
//set 中间值
if !CompareAndSwapPointer(&vp.typ, nil, unsafe.Pointer(^uintptr(0))) {
runtime_procUnpin()
continue
}
// Complete first store.
//第一次存储,既要更新data,还要更新type,这里的type作为实际的类型
StorePointer(&vp.data, xp.data)
StorePointer(&vp.typ, xp.typ)
runtime_procUnpin()
return
}
//case2 ^uintptr(0)
//第一阶段未完成,typ作为是否完成的标记
//第一阶段未完成,为什么会走下来?是异步的还是并发情况?
if uintptr(typ) == ^uintptr(0) {
// First store in progress. Wait.
// Since we disable preemption around the first store,
// we can wait with active spinning.
continue
}
// First store completed. Check type and overwrite data.
//case3 类型不符
if typ != xp.typ {
panic("sync/atomic: store of inconsistently typed value into Value")
}
StorePointer(&vp.data, xp.data)
return
}
}

总结

  • ifaceWords是interface的抽象,函数参数就是interface
  • ^uintptr(0)作为一个中间值,用来保证原子性,读和写都会判断
  • Load()只是将value转换成ifaceWords类型并返回
  • runtime_procPin涉及调度,作为todo以后看到调度的部分了再补充
  • atomic是原子的,针对小的操作,保证不可中断。mutex是操作系统实现,通过原子性的判断锁标记来隔离其他进程/线程访问大的临界区,临界区内可中断。

reference

[1]https://blog.betacat.io/post/golang-atomic-value-exploration/

[2]https://mp.weixin.qq.com/s/re_9CmGm3xEbY7xCr5CYbQ

[3]https://www.quora.com/What-is-the-difference-between-Mutex-and-Atomic-Operation

----------- ending -----------