[Haskell-cafe] synchronous channels in STM
Arnar Birgisson
arnarbi at gmail.com
Thu Oct 9 05:12:01 EDT 2008
On Thu, Oct 9, 2008 at 10:50, roger peppe <rogpeppe at gmail.com> wrote:
> On Thu, Oct 9, 2008 at 9:15 AM, Ryan Ingram <ryani.spam at gmail.com> wrote:
>> I don't think what you want is possible if both sides are in STM.
>> Other authors have posted solutions where one side or the other of the
>> transaction is in I/O, but wholly inside STM it's not possible.
>
> Thanks, that's what I thought, although I wasn't sure of it, being
> new to both Haskell and STM.
>
> Presumably this result means that it's not possible to implement
> any bounded-buffer-type interface within (rather than on top of) STM.
>
> Isn't that a rather serious restriction?
Sorry, I come into this discussion late. One-place buffers, or MVars,
are indeed implemented over STM in the orignal paper [1]. Is that what
you seek? Copied from the paper, it looks like this:
type MVar a = TVar (Maybe a)
newEmptyMVar :: STM (MVar a)
newEmptyMVar = newTVar Nothing
takeMVar :: MVar a -> STM a
takeMVar mv
= do v <- readTVar mv
case v of
Nothing -> retry
Just val -> do writeTVar mv Nothing
return val
putMVar :: MVar a -> a -> STM ()
putMVar mv val
= do v <- readTVar mv
case v of
Nothing -> writeTVar mv (Just val)
Just _ -> retry
Again, sorry if I'm missing your point. Note that transactions cannot
"block" like threads, when they retry - they do block, but when they
are unblocked they are retried from the top. This is naturally due to
the requirement that a transaction must not be affected by something
that happens concurrently.
[1] also goes to implement buffered, multi-item, multi-cast channels.
[1] Tim Harris, Simon Marlow, Simon Peyton Jones, Maurice Herlihy.
Composable Memory Transactions.
cheers,
Arnar
More information about the Haskell-Cafe
mailing list