runtime: add support for time.NewTimer and time.NewTicker

This commit adds support for time.NewTimer and time.NewTicker. It also
adds support for the Stop() method on time.Timer, but doesn't (yet) add
support for the Reset() method.

The implementation has been carefully written so that programs that
don't use these timers will normally not see an increase in RAM or
binary size. None of the examples in the drivers repo change as a result
of this commit. This comes at the cost of slightly more complex code and
possibly slower execution of the timers when they are used.
This commit is contained in:
Kenneth Bell
2022-07-02 17:19:36 +01:00
committed by Ron Evans
parent 80c17c0f32
commit 24b45555bd
7 changed files with 258 additions and 3 deletions
+41
View File
@@ -0,0 +1,41 @@
package main
import "time"
func main() {
// Test ticker.
ticker := time.NewTicker(time.Millisecond * 160)
println("waiting on ticker")
go func() {
time.Sleep(time.Millisecond * 80)
println(" - after 80ms")
time.Sleep(time.Millisecond * 160)
println(" - after 240ms")
time.Sleep(time.Millisecond * 160)
println(" - after 400ms")
}()
<-ticker.C
println("waited on ticker at 160ms")
<-ticker.C
println("waited on ticker at 320ms")
ticker.Stop()
time.Sleep(time.Millisecond * 400)
select {
case <-ticker.C:
println("fail: ticker should have stopped!")
default:
println("ticker was stopped (didn't send anything after 400ms)")
}
timer := time.NewTimer(time.Millisecond * 160)
println("waiting on timer")
go func() {
time.Sleep(time.Millisecond * 80)
println(" - after 80ms")
time.Sleep(time.Millisecond * 160)
println(" - after 240ms")
}()
<-timer.C
println("waited on timer at 160ms")
time.Sleep(time.Millisecond * 160)
}
+11
View File
@@ -0,0 +1,11 @@
waiting on ticker
- after 80ms
waited on ticker at 160ms
- after 240ms
waited on ticker at 320ms
- after 400ms
ticker was stopped (didn't send anything after 400ms)
waiting on timer
- after 80ms
waited on timer at 160ms
- after 240ms