Fix mapping between sched_attr and SchedPolicy

This commit is contained in:
js2xxx
2025-06-18 11:48:11 +00:00
committed by Jianfeng Jiang
parent 0875cf6644
commit c5d57d5216
3 changed files with 50 additions and 5 deletions

View File

@ -69,16 +69,26 @@ impl TryFrom<SchedPolicy> for LinuxSchedAttr {
..Default::default()
},
// The SCHED_IDLE policy is mapped to the highest nice value of
// `SchedPolicy::Fair` instead of `SchedPolicy::Idle`. Tasks of the
// latter policy are invisible to the user API.
SchedPolicy::Fair(Nice::MAX) => LinuxSchedAttr {
sched_policy: SCHED_IDLE,
..Default::default()
},
SchedPolicy::Fair(nice) => LinuxSchedAttr {
sched_policy: SCHED_NORMAL,
sched_nice: nice.value().get().into(),
..Default::default()
},
SchedPolicy::Idle => LinuxSchedAttr {
sched_policy: SCHED_IDLE,
..Default::default()
},
SchedPolicy::Idle => {
return Err(Error::with_message(
Errno::EACCES,
"attr for idle tasks are not accessible",
))
}
})
}
}
@ -112,7 +122,10 @@ impl TryFrom<LinuxSchedAttr> for SchedPolicy {
.map_err(|msg| Error::with_message(Errno::EINVAL, msg))?,
)),
SCHED_IDLE => SchedPolicy::Idle,
// The SCHED_IDLE policy is mapped to the highest nice value of
// `SchedPolicy::Fair` instead of `SchedPolicy::Idle`. Tasks of the
// latter policy are invisible to the user API.
SCHED_IDLE => SchedPolicy::Fair(Nice::MAX),
_ => {
return Err(Error::with_message(

View File

@ -0,0 +1,31 @@
// SPDX-License-Identifier: MPL-2.0
#define _GNU_SOURCE
#include <assert.h>
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SCHED_IDLE 5
void *test(void *__arg)
{
struct sched_param param = { .sched_priority = 0 };
assert(sched_setscheduler(0, SCHED_IDLE, &param) == 0);
sleep(1);
return NULL;
}
int main()
{
pthread_t thread;
assert(pthread_create(&thread, NULL, test, NULL) == 0);
test(NULL);
pthread_join(thread, NULL);
printf("Test completed\n");
return 0;
}

View File

@ -39,6 +39,7 @@ pthread/pthread_test
pty/open_pty
pty/pty_blocking
sched/sched_attr
sched/sched_attr_idle
shm/posix_shm
signal_c/parent_death_signal
signal_c/signal_test