basvandijk / concurrent-extra

Extra concurrency primitives
Other
17 stars 5 forks source link

Simplify `Broadcast`? #23

Open konsumlamm opened 1 year ago

konsumlamm commented 1 year ago

I think Broadcast could be (almost) simplified to newtype Broadcast a = Broadcast { unBroadcast :: MVar a }, where "silent" is represented by an empty MVar and "broadcasting" is represented by a full MVar. listen would then just be readMVar . unBroadcast and broadcast would be putMVar x . unBroadcast. However, signal might be more difficult, since it would putMVar and then immediately takeMVar. I'm not sure if the runtime guarantees that the threads blocked on the MVar can read it before signal continues.