Skip to content

Commit bb0cb0f

Browse files
committed
Pre-warm TBB thread pool to eliminate QoS priority inversion warnings
The observer pattern has an inherent race: TBB workers start at Default QoS and the OS can log a priority inversion warning before on_scheduler_entry fires. Fix: on first call, run a trivial parallel_for whose sole purpose is to call pthread_set_qos_class_self_np on each worker. Workers then carry User-initiated QoS into all subsequent Manifold operations. The observer remains as a safety net for any new workers TBB might spin up later.
1 parent a5787b9 commit bb0cb0f

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Sources/ManifoldBridge/src/bridge.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
#ifdef __APPLE__
66
#include <pthread.h>
77
#include <oneapi/tbb/task_scheduler_observer.h>
8+
#include <oneapi/tbb/parallel_for.h>
9+
#include <oneapi/tbb/blocked_range.h>
10+
#include <oneapi/tbb/info.h>
811

912
namespace {
1013
// Sets QoS on TBB worker threads to improve scheduling on Apple Silicon.
1114
// Reduces priority inversion warnings and allows workers to run on P-cores.
12-
// Called from Swift init methods to register the observer before TBB work begins.
1315
class AppleQoSObserver : public tbb::task_scheduler_observer {
1416
public:
1517
AppleQoSObserver() { observe(true); }
@@ -23,6 +25,16 @@ namespace {
2325
pthread_once_t qos_once = PTHREAD_ONCE_INIT;
2426
void doInitQoS() {
2527
static AppleQoSObserver observer;
28+
// Pre-warm the TBB thread pool so workers carry User-initiated QoS into
29+
// all subsequent Manifold operations. Without this, the observer fires only
30+
// after the OS has already scheduled workers at Default QoS, which is too
31+
// late to prevent priority inversion warnings on the very first operation.
32+
int n = tbb::info::default_concurrency();
33+
tbb::parallel_for(tbb::blocked_range<int>(0, n),
34+
[](const tbb::blocked_range<int>&) {
35+
pthread_set_qos_class_self_np(QOS_CLASS_USER_INITIATED, 0);
36+
}
37+
);
2638
}
2739
}
2840

0 commit comments

Comments
 (0)