From 772c7ae7e140ebd3896fc7786aae8b43ab3128c3 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Sat, 12 Aug 2023 18:06:08 +0800 Subject: [PATCH] sync: document why copyChecker checks the condition twice Fixes #40924 Change-Id: I249a278be1ec3c67088819af4456e6c393431724 --- src/sync/cond.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/sync/cond.go b/src/sync/cond.go index cc927adf58..7ef3188a1e 100644 --- a/src/sync/cond.go +++ b/src/sync/cond.go @@ -96,6 +96,10 @@ func (c *Cond) Broadcast() { type copyChecker uintptr func (c *copyChecker) check() { + // Check if c has been copied in three steps: + // 1. The first comparison is the fast-path. If c has been initialized and not copied, this will return immediately. Otherwise, c is either not initialized, or has been copied. + // 2. Ensure c is initialized. If the CAS succeeds, we're done. If it fails, c was either initialized concurrently and we simply lost the race, or c has been copied. + // 3. Do step 1 again. Now that c is definitely initialized, if this fails, c was copied. if uintptr(*c) != uintptr(unsafe.Pointer(c)) && !atomic.CompareAndSwapUintptr((*uintptr)(c), 0, uintptr(unsafe.Pointer(c))) && uintptr(*c) != uintptr(unsafe.Pointer(c)) {