Commit 5789b285 authored by Russ Cox's avatar Russ Cox

runtime: start GC background sweep eagerly

Starting it lazily causes a memory allocation (for the goroutine) during GC.

First use of channels for runtime implementation.

Change-Id: I9cd24dcadbbf0ee5070ee6d0ed7ea415504f316c
Reviewed-on: https://go-review.googlesource.com/6960
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: 's avatarAustin Clements <austin@google.com>
parent 632217aa
...@@ -176,6 +176,16 @@ func gcinit() { ...@@ -176,6 +176,16 @@ func gcinit() {
memstats.next_gc = heapminimum memstats.next_gc = heapminimum
} }
// gcenable is called after the bulk of the runtime initialization,
// just before we're about to start letting user code run.
// It kicks off the background sweeper goroutine and enables GC.
func gcenable() {
c := make(chan int, 1)
go bgsweep(c)
<-c
memstats.enablegc = true // now that runtime is initialized, GC is okay
}
func setGCPercent(in int32) (out int32) { func setGCPercent(in int32) (out int32) {
lock(&mheap_.lock) lock(&mheap_.lock)
out = gcpercent out = gcpercent
...@@ -568,10 +578,7 @@ func gcSweep(mode int) { ...@@ -568,10 +578,7 @@ func gcSweep(mode int) {
// Background sweep. // Background sweep.
lock(&sweep.lock) lock(&sweep.lock)
if !sweep.started { if sweep.parked {
go bgsweep()
sweep.started = true
} else if sweep.parked {
sweep.parked = false sweep.parked = false
ready(sweep.g) ready(sweep.g)
} }
......
...@@ -42,8 +42,14 @@ func finishsweep_m() { ...@@ -42,8 +42,14 @@ func finishsweep_m() {
} }
} }
func bgsweep() { func bgsweep(c chan int) {
sweep.g = getg() sweep.g = getg()
lock(&sweep.lock)
sweep.parked = true
c <- 1
goparkunlock(&sweep.lock, "GC sweep wait", traceEvGoBlock)
for { for {
for gosweepone() != ^uintptr(0) { for gosweepone() != ^uintptr(0) {
sweep.nbgsweep++ sweep.nbgsweep++
......
...@@ -58,7 +58,7 @@ func main() { ...@@ -58,7 +58,7 @@ func main() {
} }
}() }()
memstats.enablegc = true // now that runtime is initialized, GC is okay gcenable()
if iscgo { if iscgo {
if _cgo_thread_start == nil { if _cgo_thread_start == nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment