all: rewrite sleep function

time.Sleep now compiles on all systems, so lets use that.
Additionally, do a few improvements in time unit handling for the
scheduler. This should lead to somewhat longer sleep durations without
wrapping (on some platforms).

Some examples got smaller, some got bigger. In particular, code using
the scheduler got bigger and the blinky1 example got smaller (especially
on Arduino: 380 -> 314 bytes).
This commit is contained in:
Ayke van Laethem
2018-09-15 01:08:31 +02:00
parent 1ac67cf8de
commit 2a20c0c7f0
12 changed files with 75 additions and 72 deletions
+17 -7
View File
@@ -6,14 +6,16 @@ import (
"unsafe"
)
const Microsecond = 1
func _Cfunc_putchar(c int) int
func _Cfunc_usleep(usec uint) int
func _Cfunc_calloc(nmemb, size uintptr) unsafe.Pointer
func _Cfunc_exit(status int)
func _Cfunc_clock_gettime(clk_id uint, ts *timespec)
type timeUnit int64
const tickMicros = 1
// TODO: Linux/amd64-specific
type timespec struct {
tv_sec int64
@@ -26,18 +28,26 @@ func putchar(c byte) {
_Cfunc_putchar(int(c))
}
func sleep(d Duration) {
_Cfunc_usleep(uint(d))
//go:linkname sleep time.Sleep
func sleep(d int64) {
_Cfunc_usleep(uint(d) / 1000)
}
// Return monotonic time in microseconds.
func sleepTicks(d timeUnit) {
sleep(int64(d))
}
// Return monotonic time in nanoseconds.
//
// TODO: use nanoseconds?
// TODO: noescape
func monotime() uint64 {
ts := timespec{}
_Cfunc_clock_gettime(CLOCK_MONOTONIC_RAW, &ts)
return uint64(ts.tv_sec)*1000*1000 + uint64(ts.tv_nsec)/1000
return uint64(ts.tv_sec)*1000*1000*1000 + uint64(ts.tv_nsec)
}
func ticks() timeUnit {
return timeUnit(monotime())
}
func abort() {