Skip to content
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

NSThread: Fix threadPriority and setThreadPriority: on Android by using setpriority instead of pthread #436

Merged
merged 2 commits into from
Aug 13, 2024

Conversation

hmelder
Copy link
Contributor

@hmelder hmelder commented Aug 12, 2024

Android's pthread_setschedparam is currently broken, as it checks if the priority is in the range of the system's min and max priorities. The interval bounds are queried with sched_get_priority_min, and sched_get_priority_max which just return 0, regardless of the specified scheduling policy.

The solution is to use setpriority to set the thread priority. This is possible because on Linux, it is not a per-process setting as specified by POSIX but a per-thread setting (See the Bugs section in setpriority).

@hmelder hmelder requested a review from rfm as a code owner August 12, 2024 14:08
Copy link
Contributor

@rfm rfm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes perfect sense.

@hmelder
Copy link
Contributor Author

hmelder commented Aug 12, 2024

Android's pthread_setschedparam is currently broken

I have to correct myself. It is not broken (as seen here). On Linux, SCHED_OTHER does not have additional priorities which means that sched_get_priority_min and sched_get_priority_max always return 0.

However, user-space threads, created with pthread_create, are a 1-to-1 mapping to Linux LWPs (Light Weight Process). Adjusting the nice value (with nice() or setpriority()) will change the way the thread is handled.

@hmelder
Copy link
Contributor Author

hmelder commented Aug 12, 2024

After skimming through the newest Android Sources, I can indeed verify that setpriority is still used.

On Android there is setThreadPriority in android.os.Process which is implemented here.

The underlying function is called androidSetThreadPriority which in turn calls setpriority.

This is much better than interfacing with JNI.

@hmelder
Copy link
Contributor Author

hmelder commented Aug 12, 2024

@rfm how about doing that on Linux as well? Currently we do not prioritise anything because of this.

@hmelder
Copy link
Contributor Author

hmelder commented Aug 12, 2024

Here is a similar discussion: dotnet/runtime#91165

@triplef
Copy link
Member

triplef commented Aug 13, 2024

Another discussion about this in the NDK project: android/ndk#1255

If I understand it correctly it sounds like we should use setpriority().

@hmelder hmelder merged commit 162708f into master Aug 13, 2024
8 of 10 checks passed
@hmelder hmelder deleted the android_thread_sched branch August 13, 2024 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants