Skip to content

Basic Linux and Windows Thread Prioritisation for worker threads #842

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

hmelder
Copy link

@hmelder hmelder commented Aug 27, 2024

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

#include <stdio.h>
#include <windows.h>
#include <dispatch/dispatch.h>

int main (int arg,char **argv)
{
    void (^myBlock) (void) = ^{
            PWSTR desc;
            HANDLE current = GetCurrentThread();
            int prio = GetThreadPriority(current);
            GetThreadDescription(current, &desc);
            wprintf(L"priority %d descr %ls\n", prio, desc);
            LocalFree(desc);
    };

    dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
    dispatch_queue_t utilityQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
    dispatch_queue_t defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_queue_t highQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);

    dispatch_async(backgroundQueue, myBlock);
    dispatch_async(utilityQueue, myBlock);
    dispatch_async(defaultQueue, myBlock);
    dispatch_async(highQueue, myBlock);

    dispatch_main();

}
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

#include <stdio.h>
#include <dispatch/dispatch.h>
#include <pthread.h>
#include <sys/resource.h>

int main (int arg,char **argv)
{
    void (^myBlock) (void) = ^{
	    int prio = getpriority(PRIO_PROCESS, 0);
	    printf("block with priority %d \n", prio);
    };

    dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
    dispatch_queue_t utilityQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
    dispatch_queue_t defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_queue_t highQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);

    dispatch_async(backgroundQueue, myBlock);
    dispatch_async(utilityQueue, myBlock);
    dispatch_async(defaultQueue, myBlock);
    dispatch_async(highQueue, myBlock);

    dispatch_main();
}
block with priority 2
block with priority 0
block with priority 10
block with priority -2

@hmelder
Copy link
Author

hmelder commented Aug 27, 2024

We can probably move all platform-dependent thread prioritisation logic into a new function like _dispatch_set_priority() (analog to _dispatch_get_priority()). This function sets the TSD slot __TSD_THREAD_QOS_CLASS on Darwin, setpriority on Linux, and SetThreadPriority on Windows.

@triplef
Copy link
Contributor

triplef commented Sep 3, 2024

@compnerd @lxbndr would be great to get your thoughts on this. 🙏

@hmelder hmelder force-pushed the qos_linux branch 2 times, most recently from f43058d to 4be2a89 Compare November 18, 2024 09:33
@hmelder hmelder requested a review from compnerd November 18, 2024 09:36
hmelder added 12 commits June 3, 2025 10:14
The high-priority class should be reserved for threads that must respond to
time-critical events, user input threads should be THREAD_PRIORITY_NORMAL.
This function is a wrapper around SetThreadDescription, which
accepts UTF-8 strings.
Unconditionally free heap-allocated buffer, and only set thread
description if MultiByteToWideChar returns a non-zero integer.
@hmelder
Copy link
Author

hmelder commented Jun 3, 2025

Sorry for the long delay. @compnerd @3405691582 can you please have another look?

@hmelder hmelder requested review from compnerd and 3405691582 June 3, 2025 08:16
@compnerd
Copy link
Member

compnerd commented Jun 3, 2025

@swift-ci please test

@compnerd
Copy link
Member

compnerd commented Jun 3, 2025

@swift-ci please test Windows platform

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants