]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: start GC background sweep eagerly
authorRuss Cox <rsc@golang.org>
Thu, 5 Mar 2015 21:04:17 +0000 (16:04 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 5 Mar 2015 21:41:55 +0000 (21:41 +0000)
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: Austin Clements <austin@google.com>
src/runtime/mgc.go
src/runtime/mgcsweep.go
src/runtime/proc.go

index af1615376e1eecfd662dc43d62e97111c54e22cb..e8fd80c091f6a02be2e9bd57cc534abf24fa9c94 100644 (file)
@@ -176,6 +176,16 @@ func gcinit() {
        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) {
        lock(&mheap_.lock)
        out = gcpercent
@@ -568,10 +578,7 @@ func gcSweep(mode int) {
 
        // Background sweep.
        lock(&sweep.lock)
-       if !sweep.started {
-               go bgsweep()
-               sweep.started = true
-       } else if sweep.parked {
+       if sweep.parked {
                sweep.parked = false
                ready(sweep.g)
        }
index 8a1ced9f285c8141a001c6f63a5020329c7b2a7c..18b19f30b4ce9bb7487b802fdf5eee100a1ca7cc 100644 (file)
@@ -42,8 +42,14 @@ func finishsweep_m() {
        }
 }
 
-func bgsweep() {
+func bgsweep(c chan int) {
        sweep.g = getg()
+
+       lock(&sweep.lock)
+       sweep.parked = true
+       c <- 1
+       goparkunlock(&sweep.lock, "GC sweep wait", traceEvGoBlock)
+
        for {
                for gosweepone() != ^uintptr(0) {
                        sweep.nbgsweep++
index ae52826993428234c69c85c0186ddb36b55574e5..5763b3d066547af0f4ccd8240ee489fbc7c473bc 100644 (file)
@@ -58,7 +58,7 @@ func main() {
                }
        }()
 
-       memstats.enablegc = true // now that runtime is initialized, GC is okay
+       gcenable()
 
        if iscgo {
                if _cgo_thread_start == nil {