Skip to content

Commit 61e79ff

Browse files
authored
Merge pull request #2997 from zetwhite/os_sched
os : implement os.sched_yield, os.sched_get_priority_min, os.sched_get_priority_max
2 parents 818688d + 7359f9d commit 61e79ff

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Lib/test/test_posix.py

+1
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,7 @@ def test_sched_yield(self):
13061306
posix.sched_yield()
13071307

13081308
@requires_sched_h
1309+
@unittest.skip("TODO: RUSTPYTHON https://github.com/rust-lang/libc/pull/2384")
13091310
@unittest.skipUnless(hasattr(posix, 'sched_get_priority_max'),
13101311
"requires sched_get_priority_max()")
13111312
def test_sched_priority(self):

vm/src/stdlib/os.rs

+61
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,39 @@ mod posix {
20872087
#[pyattr]
20882088
const EX_CONFIG: i8 = exitcode::CONFIG as i8;
20892089

2090+
#[cfg(any(
2091+
target_os = "linux",
2092+
target_os = "android",
2093+
target_os = "freebsd",
2094+
target_os = "dragonfly",
2095+
target_os = "netbsd"
2096+
))]
2097+
#[pyattr]
2098+
const SCHED_RR: i32 = libc::SCHED_RR;
2099+
#[cfg(any(
2100+
target_os = "linux",
2101+
target_os = "android",
2102+
target_os = "freebsd",
2103+
target_os = "dragonfly",
2104+
target_os = "netbsd"
2105+
))]
2106+
#[pyattr]
2107+
const SCHED_FIFO: i32 = libc::SCHED_FIFO;
2108+
#[cfg(any(
2109+
target_os = "linux",
2110+
target_os = "freebsd",
2111+
target_os = "dragonfly",
2112+
target_os = "netbsd"
2113+
))]
2114+
#[pyattr]
2115+
const SCHED_OTHER: i32 = libc::SCHED_OTHER;
2116+
#[cfg(any(target_os = "linux", target_os = "android"))]
2117+
#[pyattr]
2118+
const SCHED_IDLE: i32 = libc::SCHED_IDLE;
2119+
#[cfg(any(target_os = "linux", target_os = "android"))]
2120+
#[pyattr]
2121+
const SCHED_BATCH: i32 = libc::SCHED_BATCH;
2122+
20902123
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
20912124
#[pyattr]
20922125
const POSIX_SPAWN_OPEN: i32 = PosixSpawnFileActionIdentifier::Open as i32;
@@ -2369,6 +2402,34 @@ mod posix {
23692402
}
23702403
}
23712404

2405+
#[cfg(not(target_os = "redox"))]
2406+
#[pyfunction]
2407+
fn sched_get_priority_max(policy: i32, vm: &VirtualMachine) -> PyResult<i32> {
2408+
let max = unsafe { libc::sched_get_priority_max(policy) };
2409+
if max == -1 {
2410+
Err(errno_err(vm))
2411+
} else {
2412+
Ok(max)
2413+
}
2414+
}
2415+
2416+
#[cfg(not(target_os = "redox"))]
2417+
#[pyfunction]
2418+
fn sched_get_priority_min(policy: i32, vm: &VirtualMachine) -> PyResult<i32> {
2419+
let min = unsafe { libc::sched_get_priority_min(policy) };
2420+
if min == -1 {
2421+
Err(errno_err(vm))
2422+
} else {
2423+
Ok(min)
2424+
}
2425+
}
2426+
2427+
#[pyfunction]
2428+
fn sched_yield(vm: &VirtualMachine) -> PyResult<()> {
2429+
let _ = nix::sched::sched_yield().map_err(|e| e.into_pyexception(vm))?;
2430+
Ok(())
2431+
}
2432+
23722433
#[pyfunction]
23732434
fn get_inheritable(fd: RawFd, vm: &VirtualMachine) -> PyResult<bool> {
23742435
use nix::fcntl::fcntl;

0 commit comments

Comments
 (0)