Skip to content

Commit 8872d5e

Browse files
aykevldeadprogram
authored andcommitted
runtime: fix sleep duration for long sleeps
Long sleep durations weren't implemented correctly, by simply casting to a smaller number of bits. What we want is a saturating downcast, which is what I've used here instead. This should fix #3356.
1 parent faf4552 commit 8872d5e

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-2
lines changed

builder/sizes_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func TestBinarySize(t *testing.T) {
4343
tests := []sizeTest{
4444
// microcontrollers
4545
{"hifive1b", "examples/echo", 3884, 280, 0, 2268},
46-
{"microbit", "examples/serial", 2852, 360, 8, 2272},
47-
{"wioterminal", "examples/pininterrupt", 7337, 1491, 116, 6912},
46+
{"microbit", "examples/serial", 2844, 360, 8, 2272},
47+
{"wioterminal", "examples/pininterrupt", 7349, 1491, 116, 6912},
4848

4949
// TODO: also check wasm. Right now this is difficult, because
5050
// wasm binaries are run through wasm-opt and therefore the

src/runtime/runtime_atsamd21.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ func nanosecondsToTicks(ns int64) timeUnit {
273273
func sleepTicks(d timeUnit) {
274274
for d != 0 {
275275
ticks := uint32(d)
276+
if d > 0xffff_ffff {
277+
ticks = 0xffff_ffff
278+
}
276279
if !timerSleep(ticks) {
277280
// Bail out early to handle a non-time interrupt.
278281
return

src/runtime/runtime_atsamd51.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ func nanosecondsToTicks(ns int64) timeUnit {
266266
func sleepTicks(d timeUnit) {
267267
for d != 0 {
268268
ticks := uint32(d)
269+
if d > 0xffff_ffff {
270+
ticks = 0xffff_ffff
271+
}
269272
if !timerSleep(ticks) {
270273
return
271274
}

src/runtime/runtime_nrf.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func buffered() int {
7878
func sleepTicks(d timeUnit) {
7979
for d != 0 {
8080
ticks := uint32(d) & 0x7fffff // 23 bits (to be on the safe side)
81+
if d > 0x7fffff {
82+
ticks = 0x7fffff
83+
}
8184
rtc_sleep(ticks)
8285
d -= timeUnit(ticks)
8386
}

src/runtime/runtime_nrf52840.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ func buffered() int {
8181
func sleepTicks(d timeUnit) {
8282
for d != 0 {
8383
ticks := uint32(d) & 0x7fffff // 23 bits (to be on the safe side)
84+
if d > 0x7fffff {
85+
ticks = 0x7fffff
86+
}
8487
rtc_sleep(ticks)
8588
d -= timeUnit(ticks)
8689
}

0 commit comments

Comments
 (0)