iddm/thread-priority

Easily set real-time priority

supercurio opened this issue ยท 6 comments

Hi vityafx, thanks for this useful crate :)
I'm trying to set the highest real-time highest priority for low-latency audio operation.

let thread_id = thread_native_id();
set_thread_priority(thread_id,
                    ThreadPriority::Max,
                    ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Fifo)).unwrap();

This always fails with: Err value: Pthread(22), whereas this is fine:

set_thread_priority(thread_id,
                    ThreadPriority::Min,
                    ThreadSchedulePolicy::Normal(NormalThreadSchedulePolicy::Normal)

Also this code has the result expected, but it's surprising to have a numeric value.

let policy = ThreadSchedulePolicy::Realtime(RealtimeThreadSchedulePolicy::Fifo);
let params = ScheduleParams { sched_priority: 3 };
set_thread_schedule_policy(thread_id, policy, params).unwrap();

How would you set the highest real-time priority ergonomically?

iddm commented

Thank you for reporting an issue! I am going to look at it as I have time. As of now, I know that the error you have (Pthread(22)) is this:

EINVAL prio is not valid for the scheduling policy of the specified thread.

So, there is something related to priority which I pass to the function. I am going to work on it more but a bit later, just posting this info here for letting you know I got your problem.

Thanks :)
I also noticed that the priority in the end, as shown by top when listing threads is not as high as another audio C++ program I was looking at (when using set_thread_schedule_policy)

iddm commented

I am sorry, I realized I don't have a time to work on it this week. I already have guesses why this can happen but I have not tested it yet. I'll try doing it in the end of weekend or next week.

iddm commented

Sorry, still have no time. Can't give you any forecast.

This is caused by set_thread_priority not setting the scheduling policy as documented. See #5 for a fix.

iddm commented

Thank you guys for participating in this. I have time now and see this has already been fixed. I am closing this, but if you have something to say or to fix in this issue, feel free to mention it.