我想知道为什么 gcphase
不受 atomic.Load
保护:
n := atomic.Load(&work.cycles)
if gcphase == _GCmark {
// Wait until sweep termination, mark, and mark
// termination of cycle N complete.
gp.schedlink = work.sweepWaiters.head
work.sweepWaiters.head.set(gp)
goparkunlock(&work.sweepWaiters.lock, "wait for GC cycle", traceEvGoBlock, 1)
} else {
// We're in sweep N already.
unlock(&work.sweepWaiters.lock)
}
有人知道吗?
最佳答案
代码摘录:
func setGCPhase(x uint32) {
atomic.Store(&gcphase, x)
writeBarrier.needed = gcphase == _GCmark || gcphase == _GCmarktermination
writeBarrier.enabled = writeBarrier.needed || writeBarrier.cgo
}
虽然gcphase
是一个全局变量,但是所有对gcphase
的写入都是通过上面的函数完成的。
运行时有几个变量没有正确配对,但似乎他们有理由这样做,并且确信他们有独占访问权。
Here is the issue https://github.com/golang/go/issues/21931 filed about the same and here https://go-review.googlesource.com/c/go/+/65210 GC developers had some discussions on changing the same.
关于go - 为什么runtime.GC中没有调用atomic.Load加载gcphase?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52576971/