This note documents the architecture boundary between Swift concurrency,
libdispatch, and the kernel workqueue layer in GCDX.
It exists to answer a recurring question:
If Swift concurrency on macOS uses a "cooperative pool", does that reduce the importance of
libdispatchand kernel workqueue integration?
The answer is no.
On macOS, Swift's cooperative execution model still depends heavily on
libdispatch, and libdispatch in turn depends heavily on XNU workqueue
support.
The clean mental model is:
- Swift owns jobs, tasks, continuations, and executor semantics.
libdispatchowns the queueing surface and the bridge between Swift jobs and worker threads.- the kernel workqueue owns worker-thread admission, QoS-aware concurrency limits, and thread provisioning.
So the "cooperative pool" is not an independent replacement for libdispatch.
It is a specialized execution mode implemented through libdispatch and backed
by kernel workqueue support.
GCDX is not trying to improve only the C library layer in isolation.
The actual value proposition is:
- provide real kernel-backed
pthread_workqueuesemantics on a FreeBSD-based system; - make
libdispatchbehave like a native system service rather than a compatibility library; - let higher-level consumers, especially Swift, inherit those semantics.
That means Swift is both:
- a major product target;
- one of the best integration tests for whether the lower C stack is behaving like a real platform.
Swift concurrency understands:
- tasks;
- task groups;
- continuations;
- priorities;
- cooperative yielding;
- executor selection.
But Swift does not create a separate kernel scheduler for these jobs.
On dispatch-backed platforms, the Swift runtime routes global-executor work into
libdispatch.
Useful local reference:
../nx/apple-opensource-libdispatch/private/queue_private.h/Users/me/wip-rnx/nx-/swift-source-vx-modified/workspace/swift/stdlib/public/Concurrency/DispatchGlobalExecutor.cpp
libdispatch is where Swift jobs become queue work associated with real worker
threads.
This layer is responsible for:
- enqueueing Swift jobs onto dispatch-managed queues;
- associating those queues with particular worker-pool behavior;
- asking the workqueue subsystem for more worker threads when needed;
- handling timer and delayed execution paths that Swift async code also relies on.
In Apple's source, the key interface is dispatch_async_swift_job.
The comments and call sites make it clear that Swift jobs are expected to run on cooperative queues, not on some wholly separate Swift-only scheduler detached from dispatch.
Useful local references:
../nx/apple-opensource-libdispatch/private/queue_private.h../nx/apple-opensource-libdispatch/src/queue.c
The kernel does not schedule Swift jobs directly. It schedules threads.
The workqueue layer is the piece that gives libdispatch a dynamic worker pool
with policy:
- how many workers may run;
- which QoS bucket they belong to;
- when more workers should be admitted;
- when pressure at one priority should constrain another;
- how cooperative worker classes differ from constrained and overcommit ones.
On Apple platforms, the cooperative pool is a first-class concept in the kernel workqueue implementation, not just a user-space convention.
Useful local references:
../nx/apple-opensource-xnu/bsd/pthread/pthread_workqueue.c../nx/apple-opensource-xnu/bsd/pthread/workqueue_syscalls.h
The word "cooperative" can be misleading.
It does not mean:
- Swift bypasses
libdispatch; - the kernel stops mattering;
- Swift manages all worker threads by itself.
It does mean:
- Swift jobs are expected to cooperate with the executor model by yielding when appropriate;
libdispatchuses a special cooperative queue mode for those jobs;- the kernel workqueue layer applies a distinct worker/admission policy for those cooperative threads.
So "cooperative" changes the policy and execution style, but it does not remove the dispatch/workqueue stack underneath it.
In the local Swift runtime source:
/Users/me/wip-rnx/nx-/swift-source-vx-modified/workspace/swift/stdlib/public/Concurrency/DispatchGlobalExecutor.cpp
the global executor code:
- uses
dispatch_async_swift_jobwhen available; - creates or selects cooperative dispatch queues;
- uses dispatch timer APIs such as
dispatch_source_createanddispatch_after_ffor delayed scheduling paths.
This is direct evidence that Swift's dispatch-backed executor path is still
rooted in libdispatch.
In:
../nx/apple-opensource-libdispatch/private/queue_private.h../nx/apple-opensource-libdispatch/src/queue.c
the Apple tree exposes:
dispatch_async_swift_job;- cooperative queue handling;
- cooperative root-queue logic;
- cooperative worker-thread requests via
_pthread_workqueue_add_cooperativethreads(...).
That is strong evidence that the cooperative executor path is a dispatch path, not a dispatch alternative.
In:
../nx/apple-opensource-xnu/bsd/pthread/workqueue_syscalls.h../nx/apple-opensource-xnu/bsd/pthread/pthread_workqueue.c
the XNU side defines:
WQ_FLAG_THREAD_COOPERATIVE;- cooperative queue scheduling counters;
- cooperative allowance logic;
- cooperative request selection and admission.
That is the kernel half of the same design.
Our project does not need to clone every Apple-specific runtime feature to be valuable.
But it does need to preserve the important architecture truth:
- higher-level async runtimes should not sit on an oblivious generic thread pool if the platform can provide better feedback;
libdispatchshould be able to request worker threads using a real platform mechanism;- the kernel should be able to apply pressure, QoS-aware limits, and admission policy;
- Swift should benefit from that through the dispatch layer.
This is why the current project sequence has been:
- build the kernel
TWQsubstrate; - build the
libthrbridge; - bring up a real
libdispatchTWQ path; - use Swift workloads to validate integrated behavior.
At this point, the project has already demonstrated the following on the FreeBSD-side execution lane:
- the kernel
TWQsubsystem exists and boots in the guest; libthrcan speak the workqueue-oriented ABI;- staged
libdispatchuses the real workqueue path; - backpressure and admission effects are observable from guest probes.
That means the core statement:
kernel feedback can shape dispatch behavior on this platform
is already proven.
The remaining work is about how completely and naturally that benefit reaches higher-level runtimes like Swift.
Swift work here should be understood in three different roles.
Swift is one of the main reasons to care about making libdispatch feel native
on this platform.
If Swift only sees a compatibility-thread-pool behavior, the platform remains second-class compared with macOS.
Swift exercises behaviors that simple C probes only partially cover:
- delayed child completion;
- task-group suspension and resume;
- executor-driven fan-out;
- timer and continuation interactions.
These are high-value integration tests for the lower C stack.
Swift is useful even when it exposes a lower-layer problem.
A Swift failure can help distinguish:
- kernel/TWQ bugs;
libthrbridge bugs;- staged
libdispatchbugs; - runtime-boundary mismatches between Swift and dispatch.
That is why the Swift probes in this repo are small and targeted instead of trying to prove everything at once.
There is one operational rule that must stay explicit:
The stock local Swift 6.3 libdispatch lane is not the same thing as the staged
TWQ-backed dispatch lane.
The stock Swift toolchain is still useful as:
- a runtime comparison lane;
- a control for "does this Swift workload work at all on FreeBSD Swift?"
But it is not proof that the workload used our real workqueue integration.
That distinction is documented in:
./m12-swift63-stockdispatch-boundary-progress.md
For any Swift workload that is meant to validate this port, TWQ counters must move. Otherwise the workload may only be exercising the stock dispatch runtime.
When evaluating Swift-related work in this repo, use this decision rule:
- if the question is "does the basic workqueue machinery function?", start with C probes;
- if the question is "does higher-level asynchronous behavior inherit the right semantics?", use Swift probes;
- if the question is "which layer is wrong?", compare stock and staged runtime lanes and require TWQ counter deltas before drawing conclusions.
Swift's cooperative executor on macOS is not evidence that libdispatch and
kernel workqueue features are less important.
It is evidence that Apple built a tighter integration stack:
- Swift defines the async work model;
libdispatchdefines the queueing and worker-request model;- the kernel workqueue defines the actual worker-pool policy.
That is exactly why this project's pthread_workqueue and libdispatch work
matters so much for Swift on a FreeBSD-based system.
./swift-integration-rationale.md./freebsd-swift63-toolchain-reference.md./m10-libdispatch-bringup-progress.md./m11-5-sustained-workload-progress.md./m11-apple-libdispatch-exploration.md./m12-swift-runtime-matrix-progress.md./m12-swift63-stockdispatch-boundary-progress.md