Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving Chad.hs #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions Chan.hs → Chad.hs
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
module Chan where
module Chad where

import Control.Concurrent hiding (Chan)
import Control.Concurrent.STM hiding (TChan)
import Control.Concurrent hiding (Chad)
import Control.Concurrent.STM hiding (TChad)

type Stream a = MVar (Item a)
data Item a = Item a (Stream a)
data Chan a = Chan (MVar (Stream a)) (MVar (Stream a))
data Chad a = Chad (MVar (Stream a)) (MVar (Stream a))

newChan :: IO (Chan a)
newChan = do
newChad :: IO (Chad a)
newChad = do
hole <- newEmptyMVar
readVar <- newMVar hole
writeVar <- newMVar hole
return $ Chan readVar writeVar
return $ Chad readVar writeVar

writeChan :: Chan a -> a -> IO ()
writeChan (Chan _ writeVar) val = do
writeChad :: Chad a -> a -> IO ()
writeChad (Chad _ writeVar) val = do
newHole <- newEmptyMVar
oldHole <- takeMVar writeVar
putMVar oldHole $ Item val newHole
putMVar writeVar newHole

readChan :: Chan a -> IO a
readChan (Chan readVar _) = do
readChad :: Chad a -> IO a
readChad (Chad readVar _) = do
stream <- takeMVar readVar
-- If using multicast-channels then this must use readMVar
-- Otherwise takeMVar stream would be fine
Item val next <- readMVar stream
putMVar readVar next
return val

dupChan :: Chan a -> IO (Chan a)
dupChan (Chan _ writeVar) = do
dupChad :: Chad a -> IO (Chad a)
dupChad (Chad _ writeVar) = do
hole <- readMVar writeVar
newReadVar <- newMVar hole
return $ Chan newReadVar writeVar
return $ Chad newReadVar writeVar

type TVarList a = TVar (TList a)
data TList a = Nil
| TCons a (TVarList a)

data TChan a = TChan (TVar (TVarList a)) (TVar (TVarList a))
data TChad a = TChad (TVar (TVarList a)) (TVar (TVarList a))

newTChan :: STM (TChan a)
newTChan = do
newTChad :: STM (TChad a)
newTChad = do
hole <- newTVar Nil
read <- newTVar hole
write <- newTVar hole
return $ TChan read write
return $ TChad read write

writeTChan :: TChan a -> a -> STM ()
writeTChan (TChan _ write) val = do
writeTChad :: TChad a -> a -> STM ()
writeTChad (TChad _ write) val = do
newHole <- newTVar Nil
oldHole <- readTVar write
writeTVar oldHole $ TCons val newHole
writeTVar write newHole

readTChan :: TChan a -> STM a
readTChan (TChan read _) = do
readTChad :: TChad a -> STM a
readTChad (TChad read _) = do
stream <- readTVar read
head <- readTVar stream
case head of
Expand All @@ -66,14 +66,14 @@ readTChan (TChan read _) = do
writeTVar read next
return val

unGetTChan :: TChan a -> a -> STM ()
unGetTChan (TChan read _) val = do
unGetTChad :: TChad a -> a -> STM ()
unGetTChad (TChad read _) val = do
head <- readTVar read
newHead <- newTVar (TCons val head)
writeTVar read newHead

isEmptyTChan :: TChan a -> STM Bool
isEmptyTChan (TChan read _) = do
isEmptyTChad :: TChad a -> STM Bool
isEmptyTChad (TChad read _) = do
list <- readTVar read
head <- readTVar list
case head of
Expand Down