Skip to content

Commit ab39c60

Browse files
committed
sync: revise for go1.12
Update #3
1 parent 6ad9fe8 commit ab39c60

File tree

3 files changed

+13
-37
lines changed

3 files changed

+13
-37
lines changed

content/11-pkg/sync/map.md

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,17 @@ func (m *Map) Store(key, value interface{}) {
9393
//
9494
// 如果 entry 被删除了,则 tryStore 返回 false 且不修改 entry
9595
func (e *entry) tryStore(i *interface{}) bool {
96-
97-
// 读取 entry
98-
p := atomic.LoadPointer(&e.p)
99-
100-
// 如果 entry 已经删除,则无法存储,返回
101-
if p == expunged {
102-
return false
103-
}
104-
10596
for {
106-
// 交换 p 和 i 的值,原子操作,如果成功则立即返回
107-
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
108-
return true
109-
}
110-
111-
// 如果没有成功,则再读一次 entry
112-
p = atomic.LoadPointer(&e.p)
97+
// 读取 entry
98+
p := atomic.LoadPointer(&e.p)
11399
// 如果 entry 已经删除,则无法存储,返回
114100
if p == expunged {
115101
return false
116102
}
117-
118-
// 再次尝试,说明只要 key 不删除,那么更新操作一定会直接更新 read map,不涉及 dirty map
103+
// 交换 p 和 i 的值,如果成功则立即返回
104+
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
105+
return true
106+
}
119107
}
120108
}
121109
```

gosrc/sync/map.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -172,29 +172,17 @@ func (m *Map) Store(key, value interface{}) {
172172
//
173173
// 如果 entry 被删除了,则 tryStore 返回 false 且不修改 entry
174174
func (e *entry) tryStore(i *interface{}) bool {
175-
176-
// 读取 entry
177-
p := atomic.LoadPointer(&e.p)
178-
179-
// 如果 entry 已经删除,则无法存储,返回
180-
if p == expunged {
181-
return false
182-
}
183-
184175
for {
185-
// 交换 p 和 i 的值,原子操作,如果成功则立即返回
186-
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
187-
return true
188-
}
189-
190-
// 如果没有成功,则再读一次 entry
191-
p = atomic.LoadPointer(&e.p)
176+
// 读取 entry
177+
p := atomic.LoadPointer(&e.p)
192178
// 如果 entry 已经删除,则无法存储,返回
193179
if p == expunged {
194180
return false
195181
}
196-
197-
// 再次尝试,说明只要 key 不删除,那么更新操作一定会直接更新 read map,不涉及 dirty map
182+
// 交换 p 和 i 的值,如果成功则立即返回
183+
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
184+
return true
185+
}
198186
}
199187
}
200188

gosrc/sync/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func init() {
5050
}
5151

5252
// Active spinning runtime support.
53-
// runtime_canSpin returns true is spinning makes sense at the moment.
53+
// runtime_canSpin reports whether is spinning makes sense at the moment.
5454
func runtime_canSpin(i int) bool
5555

5656
// runtime_doSpin does active spinning.

0 commit comments

Comments
 (0)