-
Notifications
You must be signed in to change notification settings - Fork 910
Try to use SCHED_RR when setting Posix's thread max priority #4654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a new API pj_thread_set_prio_max() to set thread priority to the maximum value, with support for specifying scheduling policies on POSIX systems. The change addresses an issue where PJMEDIA clock threads couldn't properly set maximum priority when using the SCHED_OTHER policy, which doesn't allow priority changes on Linux.
Key changes:
- Introduces new API
pj_thread_set_prio_max()with platform-specific implementations - Adds support for explicit scheduling policy selection via
pj_thread_prio_paramstructure - Refactors existing ALSA and clock thread code to use the new API instead of direct POSIX calls
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| pjlib/include/pj/os.h | Defines new types and API for thread priority management with policy control |
| pjlib/src/pj/os_core_unix.c | Implements new API for POSIX platforms with policy handling logic |
| pjlib/src/pj/os_core_win32.c | Provides Windows implementation (delegates to existing priority functions) |
| pjlib/src/pj/os_core_symbian.cpp | Adds stub implementation for Symbian platform |
| pjmedia/src/pjmedia/clock_thread.c | Updates clock thread to use new API with configurable policy |
| pjmedia/src/pjmedia-audiodev/alsa_dev.c | Migrates ALSA capture thread to use new API instead of direct pthread calls |
|
Considering setting thread prio is not really used a lot in the library (only in pjmedia clock?), perhaps the proposed solution is a bit overkill with the new functions & settings, also the API feels leaning towards POSIX-specific (with So, perhaps we can try with a bit simpler approach first? An idea (not sure if this is feasible though, especially the probing):
For ALSA sound dev, can be left as is (using platform specific API instead of pjlib), because ALSA is already platform specific. Also, perhaps apply higher thread prio to the playback thread? |
| if (max > 0) { | ||
| pj_status_t status; | ||
|
|
||
| status = pj_thread_set_prio(this_thread, max); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verify that the thread priority is successfully set, such as by using ps in Linux.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the output when priority using default policy:
PID COMMAND SCH RTPRIO PRI NI
6685 pjsua-x86_64-pc 0 - 19 0
6685 media 0 - 19 0
6685 ev_thread 0 - 19 0
6685 pjsua_0 0 - 19 0
6685 clock 0 - 19 0
6685 alsasound_playb 0 - 19 0
6685 alsasound_captu 0 - 19 0
This is when pjsua has elevated permission/SCED_RR is use:
PID COMMAND SCH RTPRIO PRI NI
6406 pjsua-x86_64-pc 0 - 19 0
6406 media 0 - 19 0
6406 ev_thread 0 - 19 0
6406 pjsua_0 0 - 19 0
6406 clock 2 99 139 -
6406 alsasound_playb 2 99 139 -
6406 alsasound_captu 2 99 139 -
command:
ps -o pid,comm,sched,rtprio,pri,ni -T -p <PID>
On
PJMEDIAclock-thread, the thread priority will be set to the maximum ifPJMEDIA_CLOCK_NO_HIGHEST_PRIOis used.However, on platform's using POSIX thread, the priority will be using the policy currently used by the thread (inherited from the calling thread).
If
SCHED_OTHERis used, the priority cannot be changed, so using other policy would be required.The patch will try to get the maximum thread priority using
SCHED_RRbefore falling back to original policy.Note that
alsa_dev.cis usingSCHED_RRto raise its capture thread priority. This patch will also raise the playback thread policy.