| 1 | module os |
| 2 | |
| 3 | pub struct C.timespec { |
| 4 | pub mut: |
| 5 | tv_sec i64 |
| 6 | tv_nsec i64 |
| 7 | } |
| 8 | |
| 9 | fn C.nanosleep(req &C.timespec, rem &C.timespec) i32 |
| 10 | |
| 11 | // sleep_ms provides a cross platform way to sleep, without having to `import time` for a time.sleep/1 call. |
| 12 | fn sleep_ms(ms i64) { |
| 13 | $if windows { |
| 14 | C.Sleep(u32(ms)) |
| 15 | } $else { |
| 16 | mut req := C.timespec{ms / 1000, 1_000_000 * (ms % 1000)} |
| 17 | rem := C.timespec{} |
| 18 | for C.nanosleep(&req, &rem) < 0 { |
| 19 | if C.errno == C.EINTR { |
| 20 | req = rem |
| 21 | } else { |
| 22 | break |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |