Skip to content

Commit f6230d3

Browse files
authored
NIOPosix on Darwin: inherit main thread QoS (#2944)
### Motivation: On Darwin, QoS (quality of service) of threads plays an important role, especially on Apple Silicon machines with P-cores and E-cores. If you spawn raw threads (like NIOPosix) and use a mechanism that doesn't support QoS propagation (like reading/writing to networks -- like NIOPosix does), it's recommended to default to the main thread's QoS. Otherwise you'll always be at the default QoS for "legacy" threads which means bad latencies, especially on Apple Silicon machines. In a follow-up PR #2943 we're adding better configurability for thread configuration. ### Modifications: Default to main thread QoS on Darwin. ### Result: Better latencies for applications with higher QoS classes.
1 parent be823e6 commit f6230d3

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Sources/NIOPosix/ThreadPosix.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ private func sysPthread_create(
4343
args: UnsafeMutableRawPointer?
4444
) -> CInt {
4545
#if canImport(Darwin)
46-
return pthread_create(handle, nil, destructor, args)
46+
var attr: pthread_attr_t = .init()
47+
pthread_attr_init(&attr)
48+
pthread_attr_set_qos_class_np(&attr, qos_class_main(), 0)
49+
let thread = pthread_create(handle, &attr, destructor, args)
50+
pthread_attr_destroy(&attr)
51+
return thread
4752
#else
4853
#if canImport(Musl)
4954
var handleLinux: OpaquePointer? = nil

0 commit comments

Comments
 (0)