-
Notifications
You must be signed in to change notification settings - Fork 459
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
Basic Linux and Windows Thread Prioritisation for worker threads #842
base: main
Are you sure you want to change the base?
Conversation
We can probably move all platform-dependent thread prioritisation logic into a new function like |
case DISPATCH_QOS_USER_INITIATED: | ||
return THREAD_PRIORITY_ABOVE_NORMAL; | ||
case DISPATCH_QOS_USER_INTERACTIVE: | ||
return THREAD_PRIORITY_HIGHEST; |
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 somewhat scary for me. This can unintentionally starve the system. Generally speaking, user input threads should be THREAD_PRIORITY_NORMAL
.
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.
Oh you are right. So should we just return THREAD_PRIORITY_NORMAL
for INITIATED and INTERACTIVE?
Use HIGH_PRIORITY_CLASS with care. If a thread runs at the highest priority level for extended periods, other threads in the system will not get processor time. If several threads are set at high priority at the same time, the threads lose their effectiveness. The high-priority class should be reserved for threads that must respond to time-critical events.
https://learn.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities
|
||
return wideCharStr; | ||
} | ||
#endif |
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.
Can we inline this into a wrapper on Windows for SetThreadDescription
? That would allow us to avoid the extra free.
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.
Something like _dispatch_win32_set_thread_description(HANDLE hThread, const char *threadDescription)
?
This PR implements basic thread prioritisation on Linux and Windows and fixes a bug in
dispatch_get_global_queue
where the wrong QOS type is used.Note that this only sets the initial thread priority by translating QoS to thread priorities. Relative priorities are ignored for now. Later QoS overrides and other priority adjustments have no effect.
See the comments for implementation details.
Result on Windows
hugo@YellowBox MSYS /tmp # ./a.exe priority 0 descr com.apple.root.default-qos priority -1 descr com.apple.root.utility-qos priority -4 descr com.apple.root.background-qos priority 1 descr com.apple.root.user-initiated-qos
Result on Linux