Skip to content

Commit

Permalink
sync: revise for go1.12
Browse files Browse the repository at this point in the history
Update #3
  • Loading branch information
changkun committed Jan 27, 2019
1 parent 6ad9fe8 commit ab39c60
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
24 changes: 6 additions & 18 deletions content/11-pkg/sync/map.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,17 @@ func (m *Map) Store(key, value interface{}) {
//
// 如果 entry 被删除了,则 tryStore 返回 false 且不修改 entry
func (e *entry) tryStore(i *interface{}) bool {

// 读取 entry
p := atomic.LoadPointer(&e.p)

// 如果 entry 已经删除,则无法存储,返回
if p == expunged {
return false
}

for {
// 交换 p 和 i 的值,原子操作,如果成功则立即返回
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}

// 如果没有成功,则再读一次 entry
p = atomic.LoadPointer(&e.p)
// 读取 entry
p := atomic.LoadPointer(&e.p)
// 如果 entry 已经删除,则无法存储,返回
if p == expunged {
return false
}

// 再次尝试,说明只要 key 不删除,那么更新操作一定会直接更新 read map,不涉及 dirty map
// 交换 p 和 i 的值,如果成功则立即返回
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}
}
}
```
Expand Down
24 changes: 6 additions & 18 deletions gosrc/sync/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,29 +172,17 @@ func (m *Map) Store(key, value interface{}) {
//
// 如果 entry 被删除了,则 tryStore 返回 false 且不修改 entry
func (e *entry) tryStore(i *interface{}) bool {

// 读取 entry
p := atomic.LoadPointer(&e.p)

// 如果 entry 已经删除,则无法存储,返回
if p == expunged {
return false
}

for {
// 交换 p 和 i 的值,原子操作,如果成功则立即返回
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}

// 如果没有成功,则再读一次 entry
p = atomic.LoadPointer(&e.p)
// 读取 entry
p := atomic.LoadPointer(&e.p)
// 如果 entry 已经删除,则无法存储,返回
if p == expunged {
return false
}

// 再次尝试,说明只要 key 不删除,那么更新操作一定会直接更新 read map,不涉及 dirty map
// 交换 p 和 i 的值,如果成功则立即返回
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion gosrc/sync/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func init() {
}

// Active spinning runtime support.
// runtime_canSpin returns true is spinning makes sense at the moment.
// runtime_canSpin reports whether is spinning makes sense at the moment.
func runtime_canSpin(i int) bool

// runtime_doSpin does active spinning.
Expand Down

0 comments on commit ab39c60

Please sign in to comment.