Skip to content

Conversation

@trengginas
Copy link
Member

@trengginas trengginas commented Oct 27, 2025

On PJMEDIA clock-thread, the thread priority will be set to the maximum if PJMEDIA_CLOCK_NO_HIGHEST_PRIO is 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).

Linux allows the static priority range 1 to 99 for the SCHED_FIFO
       and SCHED_RR policies, and the priority 0 for the remaining
       policies.

If SCHED_OTHER is 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_RR before falling back to original policy.
Note that alsa_dev.c is using SCHED_RR to raise its capture thread priority. This patch will also raise the playback thread policy.

Copy link
Contributor

Copilot AI left a 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_param structure
  • 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

@trengginas trengginas marked this pull request as ready for review October 28, 2025 02:18
@nanangizz
Copy link
Member

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 policy setting), while it requires a specific/uncommon previlege (root) to use the capability fully.

So, perhaps we can try with a bit simpler approach first? An idea (not sure if this is feasible though, especially the probing):

  • modify pj_thread_get_prio_max() implementation (for POSIX) to first probe whether try SCHED_RR is allowed, if yes, return the max prio for SCHED_RR policy.
  • modify pj_thread_set_prio() (for POSIX) to use SCHED_RR policy if the specified prio is > 0, if it fails, fallback to the current implementation.

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);
Copy link
Member

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.

Copy link
Member Author

@trengginas trengginas Nov 6, 2025

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>

@trengginas trengginas changed the title Add API to set the thread maximum priority Try to use SCHED_RR when setting Posix's thread max priority Nov 6, 2025
@trengginas trengginas merged commit e69191e into master Nov 12, 2025
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants