AP_HAL_Linux: Scheduler: Use pthread_* over sched_* calls for setschedparam

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 <patrickelectric@gmail.com>
This commit is contained in:
Patrick José Pereira 2020-07-06 11:14:22 -03:00 committed by Lucas De Marchi
parent 11c19a2dde
commit 248daa85a1

View File

@ -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));
}