Skip to content

Commit 9ff06c6

Browse files
committed
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 109e076 commit 9ff06c6

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

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)