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).
Этот коммит содержится в:
родитель
929cd767c1
коммит
c096f35224
3 изменённых файлов: 15 добавлений и 0 удалений
|
@ -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))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
5
testdata/stdlib.go
предоставленный
5
testdata/stdlib.go
предоставленный
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче