Hi
I have a question about wasi-threads and epoch. So does a new epoch get created for each thread, or is the epoch shared between threads? From what I’ve observed, it seems like the epoch is shared since it’s stored in the Engine, and the same Engine is used when creating a child instance. If that’s the case, is there a way to create a separate epoch for each thread?
No, as you've correctly observed, the epoch counter is global to the engine.
To help further it might be useful to know what your goal is -- why do you need separate epoch counters for each thread? At least the intended use-case is for the epoch-bump to provide regular interruption in every running instance so that it async-yields back to the async event loop regularly; it's not meant to be a mechanism that does deterministic metering or fine-grained control in any way, so in that sense, having a single counter (with a single background thread to bump it periodically) is optimal. But maybe there are use-cases we haven't considered -- interrupting just one thread perhaps?
Yes, I’m trying to set up fine-grained control over threads using epochs. My goal is to have the ability to interrupt a specific thread while allowing other threads to continue running uninterrupted.
OK. In that case, there's no way to do this currently, and this would need a modification to Wasmtime -- you're welcome to work on this and submit a PR; perhaps a reasonable design point would be to have an epoch "mode" (global or per-Store) and set up the data structures accordingly. (I suspect you'd need to do the work as others likely don't have spare cycles for such a feature request at the moment, but I could be wrong.)
This could also be achieved with the current system perhaps if it's infrequent, e.g. you'd interrupt all threads but most threads could go back to normal with a custom epoch callback which checks state to say "was I supposed to wake up?"
Last updated: Jan 24 2025 at 00:11 UTC