Joining threads to CoreAudio HAL AudioWorkgroup

We're trying to join our audio worker threads to a CoreAudio HAL audio workgroup, but haven't managed to this working yet.

Here's what we do:

  1. Fetch audio workgroup handle from the CoreAudio device:
UInt32 Count = sizeof(os_workgroup_t);
os_workgroup_t pWorkgroup = NULL;

::AudioDeviceGetProperty(SomeCoreAudioDeviceHandle, kAudioUnitScope_Global, 0, 
  kAudioDevicePropertyIOThreadOSWorkgroup, &Count, &pWorkgroup);

This succeeds on a M1 Mini for the "Apple Inc.: Mac mini Speakers" on OSX 11.1.

The returned handle looks fine as well:

[(NSObject*)pWorkgroup debugDescription] returns 
"{xref = 2, ref = 1, name = AudioHALC Workgroup}"
  1. Join some freshly created worker threads to the workgroup via:
os_workgroup_join_token_s JoinToken;
int Result = ::os_workgroup_join(pWorkgroup, &JoinToken);

The problem: Result from os_workgroup_join always is EINVAL, Invalid argument - whatever we do. Both arguments, the workgroup handle and the join token are definitely valid. And the device hasn't been stopped or reinitialized here, so the workgroup should not be cancelled?

Has anyone else managed to get this working? All examples out there seem to successfully use the AUHAL workgroup instead of the audio device HAL API.

Replies

Figured this out with the help of the technical Apple support.

The worker thread needs to have a real-time constraint set, before you can join them to the workgroup. This can be done viathread_policy_set specifying THREAD_TIME_CONSTRAINT_POLICY.

This isn't documented, but it actually makes sense as joining workgroup quite likely changes the thread time constraints.