[GHC] #6157: Support duplicating a TChan with the current content available.
GHC
cvs-ghc at haskell.org
Mon Jun 11 11:58:43 CEST 2012
#6157: Support duplicating a TChan with the current content available.
------------------------------+---------------------------------------------
Reporter: klao | Owner:
Type: feature request | Status: new
Priority: normal | Component: libraries (other)
Version: 7.4.2 | Keywords:
Os: Unknown/Multiple | Architecture: Unknown/Multiple
Failure: None/Unknown | Testcase:
Blockedby: | Blocking:
Related: |
------------------------------+---------------------------------------------
I propose adding a function to the Control.Concurrent.STM.TChan that
duplicates the channel, but with all current content available in the
duplicate channel too. Suggestion:
{{{
-- |Clone a 'TChan': similar to dupTChan, but the cloned channel starts
with the
-- same content available as the original channel.
cloneTChan :: TChan a -> STM (TChan a)
cloneTChan (TChan read write) = do
readpos <- readTVar read
new_read <- newTVar readpos
return (TChan new_read write)
}}}
A little bit of context for the proposal.
I was writing a function that started getting a bit complicated (I am
waiting of either this message ''or'' two other messages ''in order'', and
there might be some uninteresting messages before I get any of those...)
And I realized that this is a parsing problem and we have libraries for
those! But, then I had to realize that there is no way to implement for
example Parsec's Stream class with the currently available TChan methods.
With the proposed cloneTChan it's trivial:
{{{
instance Stream (TChan a) STM a where
uncons chan = do
chan' <- cloneTChan chan
x <- readTChan chan'
return $ Just (x,chan')
}}}
And I can imagine some other applications where the new function would
come handy.
The same thing also applies to Control.Concurrent.Chan, though there you
have other possibilities (eg. you can "convert" a {{{Chan a}}} into an
{{{[a]}}} with unsafeInterleaveIO and work on that, which is not a viable
option for TChan.)
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/6157>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the Glasgow-haskell-bugs
mailing list