pub(crate) fn spawn_worker(
slot: &Mutex<Option<JoinHandle<()>>>,
inner: &Arc<Inner>,
name: &str,
body: impl FnOnce() + Send + 'static,
) -> Option<ThreadId>Expand description
Locked takeover + spawn of a worker thread, shared by the capture and playback
starts. Returns the new thread’s id, or None if the spawn failed.
Under the lifecycle lock, in order:
- Stop and join any prior worker: sets the external stop INSIDE the lock,
immediately before
join(), then clearscapture_tid— the set-before-join ordering that makes the lost-stop invariant hold. - Reset the lifecycle state: clears
stop_stateback toSTOP_NONE(done ONLY here, under the lock, after the join and before the spawn, where no external stop can be in flight), forgets the previous run’slast_error, and arms the startup handshake atST_STARTING. - Spawn
bodyon a named thread: the thread applies a best-effortniceboost (audio must not stutter when the captured workload saturates the CPU; EPERM withoutCAP_SYS_NICEis silently a no-op), publishes its OS tid intocapture_tidfor the re-entrancy guard, runsbody, then clears the tid on exit.
The returned ThreadId is the identity a failed start later hands to
join_failed_start, so a losing start tears down only the thread it spawned.