简介
介绍条件变量的例子比较多了,不再细说。文章的主要目的是了解sync.Cond的复制检查机制。本文不讨论为啥Cond不能复制,只介绍Go是怎么做的检查。
golang 使用了两种方式进行了复制检查一个是使用 go vet 工具查找。从下面的源代码中可以找到方法,如下面代码中的noCopy,至于go vet -copylocks 的查找机制。
//文件sync.cond.go
.....
type Cond struct {
noCopy noCopy
// L is held while observing or changing the condition
L Locker
notify notifyList
checker copyChecker
}
....
// noCopy may be added to structs which must not be copied
// after the first use.
//
// See https://golang.org/issues/8005#issuecomment-190753527
// for details.
//
// Note that it must not be embedded, due to the Lock and Unlock methods.
type noCopy struct{}
// Lock is a no-op used by -copylocks checker from `go vet`.
func (*noCopy) Lock() {}
func (*noCopy) Unlock() {}
而今天要记录的是Cond中checker的使用。
copyChecker
所有的Cond对外提供的接口都会有一个检查c.checker.check(),只要检查出错,就panic。
checker 是一个 uintptr类型数据,用于保存checker自身的地址,当发现自身的值,和自身的地址不一致了,则认为整个结构被复制了。(因为如果copy Cond对象,会把其中的属性checker的值一块儿拷贝,而checker的地址就变了)
if checker == 0 {
checker = Pointer(&checker)
}else{
if checker != Pointer(&checker) {
panic()
}
}
// copyChecker holds back pointer to itself to detect object copying.
type copyChecker uintptr
func (c *copyChecker) check() {
if uintptr(*c) != uintptr(unsafe.Pointer(c)) &&
!atomic.CompareAndSwapUintptr((*uintptr)(c), 0, uintptr(unsafe.Pointer(c))) &&
uintptr(*c) != uintptr(unsafe.Pointer(c)) {
panic("sync.Cond is copied")
}
}
大神的代码写的太复杂,我简单做了一些精简:
func (c *copyChecker) check() {
if uintptr(*c) != uintptr(unsafe.Pointer(c)) {
//*c 在被创建的时候是0,第一调用逻辑检查会进入这个逻辑
if atomic.CompareAndSwapUintptr((*uintptr)(c), 0, uintptr(unsafe.Pointer(c))) {
//CompareAndSwapUintptr,成功说明没没拷贝
return
} else {
//CompareAndSwapUintptr,失败依然要检查是否相等,因为atomic.CompareAndSwapUintptr如果并发了,
//第二个调用就false
if uintptr(*c) != uintptr(unsafe.Pointer(c)) {
panic(1)
}
}
}
}
实话实说,我至今没想到大神那么写到底有啥好处?