golang sync.Cond 中的 copyChecker

简介

        介绍条件变量的例子比较多了,不再细说。文章的主要目的是了解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)
			}
		}
	}
}

实话实说,我至今没想到大神那么写到底有啥好处?

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值