Support duplicating a TChan with the current content available.
|Reported by:||klao||Owned by:||simonmar|
|Type of failure:||None/Unknown||Difficulty:||Unknown|
|Test Case:||stm/tests/cloneTChan001||Blocked By:|
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.)
Change History (3)
comment:1 Changed 17 months ago by igloo
- Difficulty set to Unknown
- Milestone set to 7.8.1
- Owner set to simonmar
comment:2 Changed 13 months ago by simonmar
- Status changed from new to merge
- Test Case set to stm/tests/cloneTChan001