Skip to content

Commit 72d612c

Browse files
committed
time: merge platform-specific sleep implementations
1 parent 3aa18a9 commit 72d612c

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

vm/src/stdlib/time.rs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ unsafe extern "C" {
3434
#[pymodule(name = "time", with(platform))]
3535
mod decl {
3636
use crate::{
37-
PyObjectRef, PyResult, TryFromObject, VirtualMachine,
37+
PyObjectRef, PyResult, TryFromObject, VirtualMachine, AsObject,
3838
builtins::{PyStrRef, PyTypeRef},
3939
function::{Either, FuncArgs, OptionalArg},
4040
types::PyStructSequence,
@@ -88,7 +88,6 @@ mod decl {
8888
duration_since_system_now(vm)
8989
}
9090

91-
#[cfg(not(unix))]
9291
#[pyfunction]
9392
fn sleep(seconds: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
9493
let dur = seconds.try_into_value::<Duration>(vm).map_err(|e| {
@@ -102,7 +101,23 @@ mod decl {
102101
e
103102
})?;
104103

105-
std::thread::sleep(dur);
104+
#[cfg(unix)]
105+
{
106+
// this is basically std::thread::sleep, but that catches interrupts and we don't want to;
107+
let ts = nix::sys::time::TimeSpec::from(dur);
108+
let res = unsafe { libc::nanosleep(ts.as_ref(), std::ptr::null_mut()) };
109+
let interrupted = res == -1 && nix::Error::last_raw() == libc::EINTR;
110+
111+
if interrupted {
112+
vm.check_signals()?;
113+
}
114+
}
115+
116+
#[cfg(not(unix))]
117+
{
118+
std::thread::sleep(dur);
119+
}
120+
106121
Ok(())
107122
}
108123

@@ -702,30 +717,6 @@ mod platform {
702717
get_clock_time(ClockId::CLOCK_MONOTONIC, vm)
703718
}
704719

705-
#[pyfunction]
706-
fn sleep(seconds: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
707-
let dur = seconds.try_into_value::<Duration>(vm).map_err(|e| {
708-
if e.class().is(vm.ctx.exceptions.value_error) {
709-
if let Some(s) = e.args().first().and_then(|arg| arg.str(vm).ok()) {
710-
if s.as_str() == "negative duration" {
711-
return vm.new_value_error("sleep length must be non-negative");
712-
}
713-
}
714-
}
715-
e
716-
})?;
717-
718-
let ts = TimeSpec::from(dur);
719-
let res = unsafe { libc::nanosleep(ts.as_ref(), std::ptr::null_mut()) };
720-
let interrupted = res == -1 && nix::Error::last_raw() == libc::EINTR;
721-
722-
if interrupted {
723-
vm.check_signals()?;
724-
}
725-
726-
Ok(())
727-
}
728-
729720
#[cfg(not(any(
730721
target_os = "illumos",
731722
target_os = "netbsd",

0 commit comments

Comments
 (0)