1
0
mirror of https://github.com/golang/go synced 2024-11-17 12:14:47 -07:00

sync: document why copyChecker checks the condition twice

Fixes #40924

Change-Id: I249a278be1ec3c67088819af4456e6c393431724

Change-Id: I249a278be1ec3c67088819af4456e6c393431724
GitHub-Last-Rev: 772c7ae7e1
GitHub-Pull-Request: golang/go#61978
Reviewed-on: https://go-review.googlesource.com/c/go/+/518961
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Austin Clements <austin@google.com>
Auto-Submit: Austin Clements <austin@google.com>
This commit is contained in:
qiulaidongfeng 2023-08-15 02:14:00 +00:00 committed by Gopher Robot
parent be0b8e84b0
commit 18b2c45b0a

View File

@ -96,6 +96,10 @@ func (c *Cond) Broadcast() {
type copyChecker uintptr type copyChecker uintptr
func (c *copyChecker) check() { 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)) && if uintptr(*c) != uintptr(unsafe.Pointer(c)) &&
!atomic.CompareAndSwapUintptr((*uintptr)(c), 0, uintptr(unsafe.Pointer(c))) && !atomic.CompareAndSwapUintptr((*uintptr)(c), 0, uintptr(unsafe.Pointer(c))) &&
uintptr(*c) != uintptr(unsafe.Pointer(c)) { uintptr(*c) != uintptr(unsafe.Pointer(c)) {