Skip to content

Commit

Permalink
AP_HAL_Linux: Scheduler: Use pthread_* over sched_* calls for setsche…
Browse files Browse the repository at this point in the history
…dparam

musl implements `sched_*` following the posix standard,
where `sched_setschedule` is used for process scheduling.
Linux implementation defines `sched_*` functions based in
the thread scheduler and not with the process.

Using `pthread_*` should be used to follow such standard.

Ref: https://pubs.opengroup.org/onlinepubs/9699919799/

From: https://www.openwall.com/lists/musl/2016/03/01/5

> ... Linux does not provide a way
> to set scheduling parameters for a _process_, only for threads. The
> sched_setscheduler syscall is documented as taking a pid but actually
> takes a thread id and only operates on that thread. glibc just ignores
> this and provides sched_* functions that do the wrong thing.

This can be fixed by using `pthread_setschedparam` and requesting the current
thread id via `pthread_self`.

Signed-off-by: Patrick José Pereira <[email protected]>
  • Loading branch information
patrickelectric authored and lucasdemarchi committed Aug 25, 2020
1 parent 11c19a2 commit 248daa8
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion libraries/AP_HAL_Linux/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void Scheduler::init_realtime()
mlockall(MCL_CURRENT|MCL_FUTURE);

struct sched_param param = { .sched_priority = APM_LINUX_MAIN_PRIORITY };
if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
if (pthread_setschedparam(pthread_self(), SCHED_FIFO, &param) == -1) {
AP_HAL::panic("Scheduler: failed to set scheduling parameters: %s",
strerror(errno));
}
Expand Down

0 comments on commit 248daa8

Please sign in to comment.