Commit a0c68833 authored by Dmitriy Vyukov's avatar Dmitriy Vyukov

runtime: refactor proc.c

1. Rename 'g' and 'm' local vars to 'gp' and 'mp' (convention already used in some functions)
'g' and 'm' are global vars that mean current goroutine and current machine,
when they are shadowed by local vars, it's confusing, no ability to debug log both, etc.
2. White-space shuffling.
No semantic changes.
In preparation to bigger changes.

R=golang-dev, dave
CC=golang-dev
https://golang.org/cl/6355061
parent 8b7d39e7
...@@ -275,11 +275,11 @@ runtime·goexit(void) ...@@ -275,11 +275,11 @@ runtime·goexit(void)
} }
void void
runtime·goroutineheader(G *g) runtime·goroutineheader(G *gp)
{ {
int8 *status; int8 *status;
switch(g->status) { switch(gp->status) {
case Gidle: case Gidle:
status = "idle"; status = "idle";
break; break;
...@@ -293,8 +293,8 @@ runtime·goroutineheader(G *g) ...@@ -293,8 +293,8 @@ runtime·goroutineheader(G *g)
status = "syscall"; status = "syscall";
break; break;
case Gwaiting: case Gwaiting:
if(g->waitreason) if(gp->waitreason)
status = g->waitreason; status = gp->waitreason;
else else
status = "waiting"; status = "waiting";
break; break;
...@@ -305,20 +305,20 @@ runtime·goroutineheader(G *g) ...@@ -305,20 +305,20 @@ runtime·goroutineheader(G *g)
status = "???"; status = "???";
break; break;
} }
runtime·printf("goroutine %d [%s]:\n", g->goid, status); runtime·printf("goroutine %d [%s]:\n", gp->goid, status);
} }
void void
runtime·tracebackothers(G *me) runtime·tracebackothers(G *me)
{ {
G *g; G *gp;
for(g = runtime·allg; g != nil; g = g->alllink) { for(gp = runtime·allg; gp != nil; gp = gp->alllink) {
if(g == me || g->status == Gdead) if(gp == me || gp->status == Gdead)
continue; continue;
runtime·printf("\n"); runtime·printf("\n");
runtime·goroutineheader(g); runtime·goroutineheader(gp);
runtime·traceback(g->sched.pc, (byte*)g->sched.sp, 0, g); runtime·traceback(gp->sched.pc, (byte*)gp->sched.sp, 0, gp);
} }
} }
...@@ -335,24 +335,24 @@ runtime·idlegoroutine(void) ...@@ -335,24 +335,24 @@ runtime·idlegoroutine(void)
} }
static void static void
mcommoninit(M *m) mcommoninit(M *mp)
{ {
m->id = runtime·sched.mcount++; mp->id = runtime·sched.mcount++;
m->fastrand = 0x49f6428aUL + m->id + runtime·cputicks(); mp->fastrand = 0x49f6428aUL + mp->id + runtime·cputicks();
m->stackalloc = runtime·malloc(sizeof(*m->stackalloc)); mp->stackalloc = runtime·malloc(sizeof(*mp->stackalloc));
runtime·FixAlloc_Init(m->stackalloc, FixedStack, runtime·SysAlloc, nil, nil); runtime·FixAlloc_Init(mp->stackalloc, FixedStack, runtime·SysAlloc, nil, nil);
if(m->mcache == nil) if(mp->mcache == nil)
m->mcache = runtime·allocmcache(); mp->mcache = runtime·allocmcache();
runtime·callers(1, m->createstack, nelem(m->createstack)); runtime·callers(1, mp->createstack, nelem(mp->createstack));
// Add to runtime·allm so garbage collector doesn't free m // Add to runtime·allm so garbage collector doesn't free m
// when it is just in a register or thread-local storage. // when it is just in a register or thread-local storage.
m->alllink = runtime·allm; mp->alllink = runtime·allm;
// runtime·NumCgoCall() iterates over allm w/o schedlock, // runtime·NumCgoCall() iterates over allm w/o schedlock,
// so we need to publish it safely. // so we need to publish it safely.
runtime·atomicstorep(&runtime·allm, m); runtime·atomicstorep(&runtime·allm, mp);
} }
// Try to increment mcpu. Report whether succeeded. // Try to increment mcpu. Report whether succeeded.
...@@ -372,34 +372,34 @@ canaddmcpu(void) ...@@ -372,34 +372,34 @@ canaddmcpu(void)
// Put on `g' queue. Sched must be locked. // Put on `g' queue. Sched must be locked.
static void static void
gput(G *g) gput(G *gp)
{ {
M *m; M *mp;
// If g is wired, hand it off directly. // If g is wired, hand it off directly.
if((m = g->lockedm) != nil && canaddmcpu()) { if((mp = gp->lockedm) != nil && canaddmcpu()) {
mnextg(m, g); mnextg(mp, gp);
return; return;
} }
// If g is the idle goroutine for an m, hand it off. // If g is the idle goroutine for an m, hand it off.
if(g->idlem != nil) { if(gp->idlem != nil) {
if(g->idlem->idleg != nil) { if(gp->idlem->idleg != nil) {
runtime·printf("m%d idle out of sync: g%d g%d\n", runtime·printf("m%d idle out of sync: g%d g%d\n",
g->idlem->id, gp->idlem->id,
g->idlem->idleg->goid, g->goid); gp->idlem->idleg->goid, gp->goid);
runtime·throw("runtime: double idle"); runtime·throw("runtime: double idle");
} }
g->idlem->idleg = g; gp->idlem->idleg = gp;
return; return;
} }
g->schedlink = nil; gp->schedlink = nil;
if(runtime·sched.ghead == nil) if(runtime·sched.ghead == nil)
runtime·sched.ghead = g; runtime·sched.ghead = gp;
else else
runtime·sched.gtail->schedlink = g; runtime·sched.gtail->schedlink = gp;
runtime·sched.gtail = g; runtime·sched.gtail = gp;
// increment gwait. // increment gwait.
// if it transitions to nonzero, set atomic gwaiting bit. // if it transitions to nonzero, set atomic gwaiting bit.
...@@ -418,11 +418,11 @@ haveg(void) ...@@ -418,11 +418,11 @@ haveg(void)
static G* static G*
gget(void) gget(void)
{ {
G *g; G *gp;
g = runtime·sched.ghead; gp = runtime·sched.ghead;
if(g){ if(gp) {
runtime·sched.ghead = g->schedlink; runtime·sched.ghead = gp->schedlink;
if(runtime·sched.ghead == nil) if(runtime·sched.ghead == nil)
runtime·sched.gtail = nil; runtime·sched.gtail = nil;
// decrement gwait. // decrement gwait.
...@@ -430,45 +430,45 @@ gget(void) ...@@ -430,45 +430,45 @@ gget(void)
if(--runtime·sched.gwait == 0) if(--runtime·sched.gwait == 0)
runtime·xadd(&runtime·sched.atomic, -1<<gwaitingShift); runtime·xadd(&runtime·sched.atomic, -1<<gwaitingShift);
} else if(m->idleg != nil) { } else if(m->idleg != nil) {
g = m->idleg; gp = m->idleg;
m->idleg = nil; m->idleg = nil;
} }
return g; return gp;
} }
// Put on `m' list. Sched must be locked. // Put on `m' list. Sched must be locked.
static void static void
mput(M *m) mput(M *mp)
{ {
m->schedlink = runtime·sched.mhead; mp->schedlink = runtime·sched.mhead;
runtime·sched.mhead = m; runtime·sched.mhead = mp;
runtime·sched.mwait++; runtime·sched.mwait++;
} }
// Get an `m' to run `g'. Sched must be locked. // Get an `m' to run `g'. Sched must be locked.
static M* static M*
mget(G *g) mget(G *gp)
{ {
M *m; M *mp;
// if g has its own m, use it. // if g has its own m, use it.
if(g && (m = g->lockedm) != nil) if(gp && (mp = gp->lockedm) != nil)
return m; return mp;
// otherwise use general m pool. // otherwise use general m pool.
if((m = runtime·sched.mhead) != nil){ if((mp = runtime·sched.mhead) != nil) {
runtime·sched.mhead = m->schedlink; runtime·sched.mhead = mp->schedlink;
runtime·sched.mwait--; runtime·sched.mwait--;
} }
return m; return mp;
} }
// Mark g ready to run. // Mark g ready to run.
void void
runtime·ready(G *g) runtime·ready(G *gp)
{ {
schedlock(); schedlock();
readylocked(g); readylocked(gp);
schedunlock(); schedunlock();
} }
...@@ -476,23 +476,23 @@ runtime·ready(G *g) ...@@ -476,23 +476,23 @@ runtime·ready(G *g)
// G might be running already and about to stop. // G might be running already and about to stop.
// The sched lock protects g->status from changing underfoot. // The sched lock protects g->status from changing underfoot.
static void static void
readylocked(G *g) readylocked(G *gp)
{ {
if(g->m){ if(gp->m) {
// Running on another machine. // Running on another machine.
// Ready it when it stops. // Ready it when it stops.
g->readyonstop = 1; gp->readyonstop = 1;
return; return;
} }
// Mark runnable. // Mark runnable.
if(g->status == Grunnable || g->status == Grunning) { if(gp->status == Grunnable || gp->status == Grunning) {
runtime·printf("goroutine %d has status %d\n", g->goid, g->status); runtime·printf("goroutine %d has status %d\n", gp->goid, gp->status);
runtime·throw("bad g->status in ready"); runtime·throw("bad g->status in ready");
} }
g->status = Grunnable; gp->status = Grunnable;
gput(g); gput(gp);
matchmg(); matchmg();
} }
...@@ -505,24 +505,24 @@ nop(void) ...@@ -505,24 +505,24 @@ nop(void)
// debuggers can set a breakpoint here and catch all // debuggers can set a breakpoint here and catch all
// new goroutines. // new goroutines.
static void static void
newprocreadylocked(G *g) newprocreadylocked(G *gp)
{ {
nop(); // avoid inlining in 6l nop(); // avoid inlining in 6l
readylocked(g); readylocked(gp);
} }
// Pass g to m for running. // Pass g to m for running.
// Caller has already incremented mcpu. // Caller has already incremented mcpu.
static void static void
mnextg(M *m, G *g) mnextg(M *mp, G *gp)
{ {
runtime·sched.grunning++; runtime·sched.grunning++;
m->nextg = g; mp->nextg = gp;
if(m->waitnextg) { if(mp->waitnextg) {
m->waitnextg = 0; mp->waitnextg = 0;
if(mwakeup != nil) if(mwakeup != nil)
runtime·notewakeup(&mwakeup->havenextg); runtime·notewakeup(&mwakeup->havenextg);
mwakeup = m; mwakeup = mp;
} }
} }
...@@ -719,7 +719,7 @@ runtime·stoptheworld(void) ...@@ -719,7 +719,7 @@ runtime·stoptheworld(void)
void void
runtime·starttheworld(void) runtime·starttheworld(void)
{ {
M *m; M *mp;
int32 max; int32 max;
// Figure out how many CPUs GC could possibly use. // Figure out how many CPUs GC could possibly use.
...@@ -747,8 +747,8 @@ runtime·starttheworld(void) ...@@ -747,8 +747,8 @@ runtime·starttheworld(void)
// but m is not running a specific goroutine, // but m is not running a specific goroutine,
// so set the helpgc flag as a signal to m's // so set the helpgc flag as a signal to m's
// first schedule(nil) to mcpu-- and grunning--. // first schedule(nil) to mcpu-- and grunning--.
m = runtime·newm(); mp = runtime·newm();
m->helpgc = 1; mp->helpgc = 1;
runtime·sched.grunning++; runtime·sched.grunning++;
} }
schedunlock(); schedunlock();
...@@ -827,10 +827,10 @@ matchmg(void) ...@@ -827,10 +827,10 @@ matchmg(void)
M* M*
runtime·newm(void) runtime·newm(void)
{ {
M *m; M *mp;
m = runtime·malloc(sizeof(M)); mp = runtime·malloc(sizeof(M));
mcommoninit(m); mcommoninit(mp);
if(runtime·iscgo) { if(runtime·iscgo) {
CgoThreadStart ts; CgoThreadStart ts;
...@@ -838,21 +838,21 @@ runtime·newm(void) ...@@ -838,21 +838,21 @@ runtime·newm(void)
if(libcgo_thread_start == nil) if(libcgo_thread_start == nil)
runtime·throw("libcgo_thread_start missing"); runtime·throw("libcgo_thread_start missing");
// pthread_create will make us a stack. // pthread_create will make us a stack.
m->g0 = runtime·malg(-1); mp->g0 = runtime·malg(-1);
ts.m = m; ts.m = mp;
ts.g = m->g0; ts.g = mp->g0;
ts.fn = runtime·mstart; ts.fn = runtime·mstart;
runtime·asmcgocall(libcgo_thread_start, &ts); runtime·asmcgocall(libcgo_thread_start, &ts);
} else { } else {
if(Windows) if(Windows)
// windows will layout sched stack on os stack // windows will layout sched stack on os stack
m->g0 = runtime·malg(-1); mp->g0 = runtime·malg(-1);
else else
m->g0 = runtime·malg(8192); mp->g0 = runtime·malg(8192);
runtime·newosproc(m, m->g0, (byte*)m->g0->stackbase, runtime·mstart); runtime·newosproc(mp, mp->g0, (byte*)mp->g0->stackbase, runtime·mstart);
} }
return m; return mp;
} }
// One round of scheduler: find a goroutine and run it. // One round of scheduler: find a goroutine and run it.
...@@ -876,7 +876,7 @@ schedule(G *gp) ...@@ -876,7 +876,7 @@ schedule(G *gp)
if(atomic_mcpu(v) > maxgomaxprocs) if(atomic_mcpu(v) > maxgomaxprocs)
runtime·throw("negative mcpu in scheduler"); runtime·throw("negative mcpu in scheduler");
switch(gp->status){ switch(gp->status) {
case Grunnable: case Grunnable:
case Gdead: case Gdead:
// Shouldn't have been running! // Shouldn't have been running!
...@@ -898,7 +898,7 @@ schedule(G *gp) ...@@ -898,7 +898,7 @@ schedule(G *gp)
runtime·exit(0); runtime·exit(0);
break; break;
} }
if(gp->readyonstop){ if(gp->readyonstop) {
gp->readyonstop = 0; gp->readyonstop = 0;
readylocked(gp); readylocked(gp);
} }
...@@ -1281,7 +1281,7 @@ runtime·newproc1(byte *fn, byte *argp, int32 narg, int32 nret, void *callerpc) ...@@ -1281,7 +1281,7 @@ runtime·newproc1(byte *fn, byte *argp, int32 narg, int32 nret, void *callerpc)
schedlock(); schedlock();
if((newg = gfget()) != nil){ if((newg = gfget()) != nil) {
if(newg->stackguard - StackGuard != newg->stack0) if(newg->stackguard - StackGuard != newg->stack0)
runtime·throw("invalid stack in newg"); runtime·throw("invalid stack in newg");
} else { } else {
...@@ -1333,7 +1333,7 @@ runtime·deferproc(int32 siz, byte* fn, ...) ...@@ -1333,7 +1333,7 @@ runtime·deferproc(int32 siz, byte* fn, ...)
{ {
Defer *d; Defer *d;
int32 mallocsiz; int32 mallocsiz;
mallocsiz = sizeof(*d); mallocsiz = sizeof(*d);
if(siz > sizeof(d->args)) if(siz > sizeof(d->args))
mallocsiz += siz - sizeof(d->args); mallocsiz += siz - sizeof(d->args);
...@@ -1602,24 +1602,24 @@ nomatch: ...@@ -1602,24 +1602,24 @@ nomatch:
// Put on gfree list. Sched must be locked. // Put on gfree list. Sched must be locked.
static void static void
gfput(G *g) gfput(G *gp)
{ {
if(g->stackguard - StackGuard != g->stack0) if(gp->stackguard - StackGuard != gp->stack0)
runtime·throw("invalid stack in gfput"); runtime·throw("invalid stack in gfput");
g->schedlink = runtime·sched.gfree; gp->schedlink = runtime·sched.gfree;
runtime·sched.gfree = g; runtime·sched.gfree = gp;
} }
// Get from gfree list. Sched must be locked. // Get from gfree list. Sched must be locked.
static G* static G*
gfget(void) gfget(void)
{ {
G *g; G *gp;
g = runtime·sched.gfree; gp = runtime·sched.gfree;
if(g) if(gp)
runtime·sched.gfree = g->schedlink; runtime·sched.gfree = gp->schedlink;
return g; return gp;
} }
void void
......
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