time: fix .sleep() with `-gc boehm`

pull/9386/head
Delyan Angelov 2021-03-20 16:16:11 +02:00
parent 8efea1e1c8
commit 517c1841c1
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 11 additions and 2 deletions

View File

@ -4,6 +4,7 @@
module time
#include <time.h>
#include <errno.h>
struct C.tm {
tm_sec int
@ -139,6 +140,14 @@ pub fn wait(duration Duration) {
// sleep makes the calling thread sleep for a given duration (in nanoseconds).
pub fn sleep(duration Duration) {
ts := &C.timespec{duration / second, duration % second}
C.nanosleep(ts, C.NULL)
mut req := C.timespec{duration / second, duration % second}
rem := C.timespec{}
for C.nanosleep(&req, &rem) < 0 {
if C.errno == C.EINTR {
// Interrupted by a signal handler
req = rem
} else {
break
}
}
}