我想知道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的写入都是通过上述函数完成的。

在运行时中,有几个变量没有正确配对,但似乎它们有其理由,并确保它们具有对其的独占访问权。

这是https://github.com/golang/go/issues/21931提出的相同问题,这里https://go-review.googlesource.com/c/go/+/65210 GC开发人员就更改相同内容进行了一些讨论。

关于go - 为什么在runtime.GC中未调用atomic.Load来加载gcphase?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52576971/

10-09 13:23