aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJoe Shaw <[email protected]>2022-08-24 12:19:31 -0400
committerRon Evans <[email protected]>2022-08-25 11:30:33 +0200
commitf4395147039fc356277399853821cb772ebda883 (patch)
treec79d1018f1c14fb9dfc38b67e83ebd8eee88045c
parent303410d3fcfcf3df45bfe6c927a5b963caf3a394 (diff)
downloadtinygo-f4395147039fc356277399853821cb772ebda883.tar.gz
tinygo-f4395147039fc356277399853821cb772ebda883.zip
runtime: implement resetTimer
-rw-r--r--src/runtime/time.go9
-rw-r--r--testdata/timers.go13
-rw-r--r--testdata/timers.txt5
3 files changed, 27 insertions, 0 deletions
diff --git a/src/runtime/time.go b/src/runtime/time.go
index b14eae6a0..4fa3a418b 100644
--- a/src/runtime/time.go
+++ b/src/runtime/time.go
@@ -13,6 +13,7 @@ func (t *timerNode) whenTicks() timeUnit {
}
// Defined in the time package, implemented here in the runtime.
+//
//go:linkname startTimer time.startTimer
func startTimer(tim *timer) {
addTimer(&timerNode{
@@ -48,3 +49,11 @@ func timerCallback(tn *timerNode) {
func stopTimer(tim *timer) bool {
return removeTimer(tim)
}
+
+//go:linkname resetTimer time.resetTimer
+func resetTimer(tim *timer, when int64) bool {
+ tim.when = when
+ removed := removeTimer(tim)
+ startTimer(tim)
+ return removed
+}
diff --git a/testdata/timers.go b/testdata/timers.go
index d412abc86..13fb207be 100644
--- a/testdata/timers.go
+++ b/testdata/timers.go
@@ -38,4 +38,17 @@ func main() {
<-timer.C
println("waited on timer at 250ms")
time.Sleep(time.Millisecond * 250)
+
+ reset := timer.Reset(time.Millisecond * 250)
+ println("timer reset:", reset)
+ println("waiting on timer")
+ go func() {
+ time.Sleep(time.Millisecond * 125)
+ println(" - after 125ms")
+ time.Sleep(time.Millisecond * 250)
+ println(" - after 250ms")
+ }()
+ <-timer.C
+ println("waited on timer at 250ms")
+ time.Sleep(time.Millisecond * 250)
}
diff --git a/testdata/timers.txt b/testdata/timers.txt
index 783c402c2..544e15b06 100644
--- a/testdata/timers.txt
+++ b/testdata/timers.txt
@@ -9,3 +9,8 @@ waiting on timer
- after 125ms
waited on timer at 250ms
- after 250ms
+timer reset: false
+waiting on timer
+ - after 125ms
+waited on timer at 250ms
+ - after 250ms