]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: check for preemption due to garbage collection
authorRuss Cox <rsc@golang.org>
Sat, 9 Jan 2010 17:47:45 +0000 (09:47 -0800)
committerRuss Cox <rsc@golang.org>
Sat, 9 Jan 2010 17:47:45 +0000 (09:47 -0800)
in various already expensive routines.

helps keep cpu utilization up when GOMAXPROCS > 1,
but not a full solution.

http://groups.google.com/group/golang-nuts/t/7a9535c4136d3e2

R=r
CC=golang-dev
https://golang.org/cl/184043

src/pkg/runtime/chan.c
src/pkg/runtime/hashmap.c
src/pkg/runtime/malloc.cgo
src/pkg/runtime/mgc0.c
src/pkg/runtime/proc.c
src/pkg/runtime/runtime.h

index b2a0b4facfc2976a54e43159b67a6db26d062f15..ec33d3f1b287c9f66b1de429557cf401a278a75a 100644 (file)
@@ -174,6 +174,9 @@ chansend(Hchan *c, byte *ep, bool *pres)
        SudoG *sg;
        G* gp;
 
+       if(gcwaiting)
+               gosched();
+
        if(debug) {
                prints("chansend: chan=");
                runtime·printpointer(c);
@@ -277,6 +280,9 @@ chanrecv(Hchan* c, byte *ep, bool* pres)
        SudoG *sg;
        G *gp;
 
+       if(gcwaiting)
+               gosched();
+
        if(debug) {
                prints("chanrecv: chan=");
                runtime·printpointer(c);
@@ -631,6 +637,9 @@ runtime·selectgo(Select *sel)
        G *gp;
        byte *as;
 
+       if(gcwaiting)
+               gosched();
+
        if(debug) {
                prints("selectgo: sel=");
                runtime·printpointer(sel);
@@ -908,6 +917,9 @@ runtime·closechan(Hchan *c)
        SudoG *sg;
        G* gp;
 
+       if(gcwaiting)
+               gosched();
+
        lock(c);
        incerr(c);
        c->closed |= Wclosed;
index 870274ae91c0eb396dfc2f9890d474118cc6b0bb..5bcd8bf4160f64c5bdedc99c452b2f491fc7e99f 100644 (file)
@@ -744,6 +744,9 @@ mapaccess(Hmap *h, byte *ak, byte *av, bool *pres)
 {
        byte *res;
 
+       if(gcwaiting)
+               gosched();
+
        res = nil;
        if(hash_lookup(h, ak, (void**)&res)) {
                *pres = true;
@@ -812,6 +815,9 @@ mapassign(Hmap *h, byte *ak, byte *av)
        byte *res;
        int32 hit;
 
+       if(gcwaiting)
+               gosched();
+
        res = nil;
        if(av == nil) {
                hash_remove(h, ak, (void**)&res);
@@ -908,6 +914,9 @@ mapiterinit(Hmap *h)
 void
 runtime·mapiternext(struct hash_iter *it)
 {
+       if(gcwaiting)
+               gosched();
+
        it->data = hash_next(it);
        if(debug) {
                prints("runtime·mapiternext: iter=");
index e34393a85ba1aa869d68fe4a07e765a8482a2cfd..948257973e5706f70c43cf53e4d4f81cc7b16944 100644 (file)
@@ -27,10 +27,11 @@ mallocgc(uintptr size, uint32 refflag, int32 dogc)
        void *v;
        uint32 *ref;
 
+       if(gcwaiting && g != m->g0)
+               gosched();
        if(m->mallocing)
                throw("malloc/free - deadlock");
        m->mallocing = 1;
-
        if(size == 0)
                size = 1;
 
index f0eafe3fd6cb8279d13ca351de375b8baec92e5f..2a050d378817ecb5aaa7932059956aea56123bf5 100644 (file)
@@ -244,7 +244,7 @@ gc(int32 force)
                sweep();
                mstats.next_gc = mstats.inuse_pages+mstats.inuse_pages*gcpercent/100;
        }
-       starttheworld();
        m->gcing = 0;
        semrelease(&gcsema);
+       starttheworld();
 }
index 6ac4090ebe26f20032ce0a0a80f5740673526467..6324b4be4cc4cc32e9bdc9285c6a9efca744bcc5 100644 (file)
@@ -14,6 +14,8 @@ G     g0;     // idle goroutine for m0
 
 static int32   debug   = 0;
 
+int32  gcwaiting;
+
 // Go scheduler
 //
 // The go scheduler's job is to match ready-to-run goroutines (`g's)
@@ -362,6 +364,7 @@ void
 stoptheworld(void)
 {
        lock(&sched);
+       gcwaiting = 1;
        sched.mcpumax = 1;
        while(sched.mcpu > 1) {
                noteclear(&sched.stopped);
@@ -379,6 +382,7 @@ void
 starttheworld(void)
 {
        lock(&sched);
+       gcwaiting = 0;
        sched.mcpumax = sched.gomaxprocs;
        matchmg();
        unlock(&sched);
index 2d956ea980f211626fcd27f8d9edca9291b8fdf0..ff0eab6b766aa9515a1d01ed82f3ec3638d08e97 100644 (file)
@@ -316,6 +316,7 @@ extern      int32   gomaxprocs;
 extern int32   panicking;
 extern int32   maxround;
 extern int32   fd;     // usually 1; set to 2 when panicking
+extern int32   gcwaiting;              // gc is waiting to run
 int8*  goos;
 
 /*