input-output-hk / io-sim

Haskell's IO simulator which closely follows core packages (base, async, stm).
https://hackage.haskell.org/package/io-sim
Apache License 2.0
37 stars 15 forks source link

Feature Request: Implement `threadStatus` #12

Closed MaximilianAlgehed closed 2 years ago

MaximilianAlgehed commented 2 years ago

The threadStatus function is missing from the io-classes interface. We need it for an example in quickcheck-dynamic but it is not urgent.

coot commented 2 years ago

John:

It would be good to keep IOSim and IOSimPOR in step. Remember in that case that threadStatus races with anything that changes the status of the target thread, such as an uncaught exception, or an STM transaction in that thread that blocks. Or an STM transaction in a third thread what wakes up the target thread. (Not as bad as it sounds, IOSimPOR computes a wake-up effect for each thread step for this kind of purpose). Also the vector clock of the caller has to be updated to reflect a 'read' from the target thread. This stuff is very easy to get wrong.

Marcin:

IOSim and IOSimPOR are using the same free monad, the difference is in the interpreter. You would need to add a constructor to SimA and then extend the schedule functions in Control.Monad.IOSim.Internal and Control.Monad.IOSimPOR.Internal modules.

IOSim runs one thread at a time until it's blocked or it terminates. Even threadDelay is implemented with using STM (we have a draft PR which changes that, but we haven't finished exactly the IOSimPOR part). I think this will result in that ThreadRunning is only returned when you ask the current thread (it could be a property to quick-check).

We don't store information about threads that terminated, at least in IOSim (in IOSimPOR I think it's slightly different). We have to be careful with extending the overall state to minimize performance impact on large simulations (we have a bunch of such in ouroboros-network). io-sim has some micro benchmarks too.

IOSim can only block on STM or throwTo; We have a PR which implements MVars using TMVars, so one cannot relay on BlockedOnMVar without extending TVar type.We don't store the reason why a thread is blocked, but extending that would be easy.