mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-16 10:43:29 +00:00
runtime: handle negative sleep times
This change fixes the edge case where a negative sleep time is provided. When this happens, the call now returns immediately (as specified by the docs for time.Sleep).
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
//go:build !scheduler.none
|
||||||
// +build !scheduler.none
|
// +build !scheduler.none
|
||||||
|
|
||||||
package runtime
|
package runtime
|
||||||
@@ -7,6 +8,10 @@ import "internal/task"
|
|||||||
// Pause the current task for a given time.
|
// Pause the current task for a given time.
|
||||||
//go:linkname sleep time.Sleep
|
//go:linkname sleep time.Sleep
|
||||||
func sleep(duration int64) {
|
func sleep(duration int64) {
|
||||||
|
if duration <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
addSleepTask(task.Current(), nanosecondsToTicks(duration))
|
addSleepTask(task.Current(), nanosecondsToTicks(duration))
|
||||||
task.Pause()
|
task.Pause()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
|
//go:build scheduler.none
|
||||||
// +build scheduler.none
|
// +build scheduler.none
|
||||||
|
|
||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
//go:linkname sleep time.Sleep
|
//go:linkname sleep time.Sleep
|
||||||
func sleep(duration int64) {
|
func sleep(duration int64) {
|
||||||
|
if duration <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
sleepTicks(nanosecondsToTicks(duration))
|
sleepTicks(nanosecondsToTicks(duration))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
+5
@@ -6,6 +6,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -29,6 +30,10 @@ func main() {
|
|||||||
fmt.Println("strings.IndexByte:", strings.IndexByte("asdf", 'd'))
|
fmt.Println("strings.IndexByte:", strings.IndexByte("asdf", 'd'))
|
||||||
fmt.Println("strings.Replace:", strings.Replace("An example string", " ", "-", -1))
|
fmt.Println("strings.Replace:", strings.Replace("An example string", " ", "-", -1))
|
||||||
|
|
||||||
|
// package time
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
time.Sleep(-1) // negative sleep should return immediately
|
||||||
|
|
||||||
// Exit the program normally.
|
// Exit the program normally.
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user