go - 为什么runtime.GC中没有调用atomic.Load加载gcphase?

标签 go runtime

我想知道为什么 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/

相关文章:

database - RETURNING 必须包含至少一个值

dictionary - golang struct concurrent read and write without Lock 也运行ok?

objective-c - 当变量是僵尸还是不是?

java - 以前 Java 版本中 Java 运行时保留注释的兼容性

email - 如何使用 starttls 在 Go 中从交换服务器连接和发送电子邮件?

go - 基于字符串断言类型?

java - 为什么我的 O(NLogN) 查找字谜算法比我的 O(N) 算法运行得更快?

build - InstallShield 运行时?

java - 运行时编译时类型

go - 使用 GoLang 将参数传递给 wkhtmltopdf