Is it allowed to use an os_activity_t instance created with os_activity_create from multiple threads?
In particular, would it be allowed to use os_activity_apply/os_activity_scope concurrently from multiple threads to associate separate chunks of work with the same activity?
My use case is an activity where I'm using Task.detached to create a detached unstructured Task, which (as can be expected) prevents inheritance of the current activity.
The bulk of the activity happens in the detached Task so I can just create the activity there but ideally I would like to also associate some of the setup work before spawning the Task with the same activity.
So I'm wondering if it is safe to create the activity, apply it to the setup including spawning the detached Task and then capture and apply the same activity inside the Task as well where it might be applied concurrently with the first use on the thread spawning the Task.
There are two parts to this:
- What works?
- What’s sensible?
AFAICT an OS activity is thread safe. You can happily create it in one thread and apply it in another.
However, there are reasons to be careful here. The doc comments in <os/activity.h> make it clear that the current activity — well stack of activities — is per-thread state. That raises a couple of concerns.
The first is just confusion on your part. If you start the same activity on multiple threads then you could easily get confused about what’s happening where. I think the OS will handle it just fine, so it’s really just a matter of whether it works for you.
The other concern is Swift concurrency. It uses a pool of worker threads and the thread running your task can change at any suspension point. That means you have to be careful with per-thread state. Specifically, you have to make sure you clean up any per-thread state that you apply before you suspend.
This comes naturally to os_activity_apply because the closure you supply is synchronous. OTOH, os_activity_scope_{enter,leave} open the door for problems.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"