1
0
mirror of https://github.com/golang/go synced 2024-11-23 10:30:03 -07:00

runtime: add fake time support back.

Revived from CL 15690048.

Fixes #5356.

LGTM=rsc
R=adg, dvyukov, rsc
CC=golang-codereviews
https://golang.org/cl/101400043
This commit is contained in:
Shenghou Ma 2014-10-27 20:35:15 -04:00 committed by Russ Cox
parent 2eb1b65830
commit 2fe9482343
4 changed files with 51 additions and 8 deletions

View File

@ -2758,6 +2758,8 @@ static void
checkdead(void)
{
G *gp;
P *p;
M *mp;
int32 run, grunning, s;
uintptr i;
@ -2799,6 +2801,24 @@ checkdead(void)
runtime·unlock(&runtime·allglock);
if(grunning == 0) // possible if main goroutine calls runtime·Goexit()
runtime·throw("no goroutines (main called runtime.Goexit) - deadlock!");
// Maybe jump time forward for playground.
if((gp = runtime·timejump()) != nil) {
runtime·casgstatus(gp, Gwaiting, Grunnable);
globrunqput(gp);
p = pidleget();
if(p == nil)
runtime·throw("checkdead: no p for timer");
mp = mget();
if(mp == nil)
newm(nil, p);
else {
mp->nextp = p;
runtime·notewakeup(&mp->park);
}
return;
}
g->m->throwing = -1; // do not dump full stacks
runtime·throw("all goroutines are asleep - deadlock!");
}

View File

@ -25,6 +25,6 @@ TEXT _rt0_amd64p32_nacl(SB),NOSPLIT,$16
TEXT main(SB),NOSPLIT,$0
// Uncomment for fake time like on Go Playground.
//MOVQ $1257894000000000000, AX
//MOVQ AX, runtime·timens(SB)
MOVQ $1257894000000000000, AX
MOVQ AX, runtime·faketime(SB)
JMP runtime·rt0_go(SB)

View File

@ -60,7 +60,7 @@ TEXT syscall·naclWrite(SB), NOSPLIT, $24-20
TEXT runtime·write(SB),NOSPLIT,$16-20
// If using fake time and writing to stdout or stderr,
// emit playback header before actual data.
MOVQ runtime·timens(SB), AX
MOVQ runtime·faketime(SB), AX
CMPQ AX, $0
JEQ write
MOVL fd+0(FP), DI
@ -242,7 +242,7 @@ TEXT runtime·mmap(SB),NOSPLIT,$8
RET
TEXT time·now(SB),NOSPLIT,$16
MOVQ runtime·timens(SB), AX
MOVQ runtime·faketime(SB), AX
CMPQ AX, $0
JEQ realtime
MOVQ $0, DX
@ -277,7 +277,7 @@ TEXT runtime·nacl_clock_gettime(SB),NOSPLIT,$0
RET
TEXT runtime·nanotime(SB),NOSPLIT,$16
MOVQ runtime·timens(SB), AX
MOVQ runtime·faketime(SB), AX
CMPQ AX, $0
JEQ 3(PC)
MOVQ AX, ret+0(FP)

View File

@ -35,8 +35,8 @@ var timers struct {
t []*timer
}
// nacl fake time support.
var timens int64
// nacl fake time support - time in nanoseconds since 1970
var faketime int64
// Package time APIs.
// Godoc uses the comments in package time, not these.
@ -194,7 +194,7 @@ func timerproc() {
f(arg, seq)
lock(&timers.lock)
}
if delta < 0 {
if delta < 0 || faketime > 0 {
// No timers left - put goroutine to sleep.
timers.rescheduling = true
goparkunlock(&timers.lock, "timer goroutine (idle)")
@ -208,6 +208,29 @@ func timerproc() {
}
}
func timejump() *g {
if faketime == 0 {
return nil
}
lock(&timers.lock)
if !timers.created || len(timers.t) == 0 {
unlock(&timers.lock)
return nil
}
var gp *g
if faketime < timers.t[0].when {
faketime = timers.t[0].when
if timers.rescheduling {
timers.rescheduling = false
gp = timers.gp
}
}
unlock(&timers.lock)
return gp
}
// Heap maintenance algorithms.
func siftupTimer(i int) {