takeOneMVar?

Simon Marlow marlowsd at gmail.com
Thu Feb 14 22:37:25 CET 2013


On 14/02/13 20:54, Alexander Kjeldaas wrote:

> I ended up staring at the PrimOps.cmm file today, and porting tryPutMVar
> to C so it can be used from the RTS.
>
> During my staring, it occurred to me that it should be possible to
> implement a wait-on-multiple MVars with mostly no overhead.  I am
> guessing that this would be desirable, but I am not sure.
>
> My rough idea is like this:
>
> -- wait for all MVars, return the value of one of them, and the
> corresponding index
> takeOneMVar :: [MVar a] -> IO (a, Int)
>
> This is implemented by this change:
[snip]

I've occasionally wondered whether we could do this.  So I think you'll 
have some difficulty implementing the code that blocks, because you have 
to atomically add your queue element to multiple MVars.  You could lock 
all the MVars, but you'll need to sort them to avoid lock-order 
problems, or do an STM-like two-phase locking thing.  It could get 
pretty hairy.

Also, what does the primop look like?  Lists aren't a primitive type, 
and you can't put MVar# into an Array# (kind mismatch).

Don't forget to think about throwTo.  Though I suspect that's not too 
hard (just invalidate the queue).

Cheers,
	Simon





More information about the ghc-devs mailing list