{-# LANGUAGE CPP #-}
module Control.Concurrent.Chan.Unagi.Bounded
#ifdef NOT_optimised
{-# WARNING "This library is unlikely to perform well on architectures other than i386/x64/aarch64" #-}
#endif
#if __GLASGOW_HASKELL__ < 708
{-# WARNING "Waking up blocked writers may be slower than desired in GHC<7.8 which makes readMVar non-blocking on full MVars. Nextidering upgrading." #-}
#endif
(
newChan
, InChan(), OutChan()
, readChan
, readChanOnException
, tryReadChan
, Element(..)
, getChanContents
, estimatedLength
, writeChan
, tryWriteChan
, writeList2Chan
, dupChan
) where
import Control.Concurrent.Chan.Unagi.Bounded.Internal
import Control.Concurrent.Chan.Unagi.NoBlocking.Types
import System.IO.Unsafe ( unsafeInterleaveIO )
newChan :: Int -> IO (InChan a, OutChan a)
newChan :: Int -> IO (InChan a, OutChan a)
newChan Int
size = Int -> Int -> IO (InChan a, OutChan a)
forall a. Int -> Int -> IO (InChan a, OutChan a)
newChanStarting (Int
forall a. Bounded a => a
maxBound Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
10) Int
size
getChanContents :: OutChan a -> IO [a]
getChanContents :: OutChan a -> IO [a]
getChanContents OutChan a
ch = IO [a] -> IO [a]
forall a. IO a -> IO a
unsafeInterleaveIO (do
a
x <- IO a -> IO a
forall a. IO a -> IO a
unsafeInterleaveIO (IO a -> IO a) -> IO a -> IO a
forall a b. (a -> b) -> a -> b
$ OutChan a -> IO a
forall a. OutChan a -> IO a
readChan OutChan a
ch
[a]
xs <- OutChan a -> IO [a]
forall a. OutChan a -> IO [a]
getChanContents OutChan a
ch
[a] -> IO [a]
forall (m :: * -> *) a. Monad m => a -> m a
return (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)
)
writeList2Chan :: InChan a -> [a] -> IO ()
{-# INLINABLE writeList2Chan #-}
writeList2Chan :: InChan a -> [a] -> IO ()
writeList2Chan InChan a
ch = [IO ()] -> IO ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ ([IO ()] -> IO ()) -> ([a] -> [IO ()]) -> [a] -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> IO ()) -> [a] -> [IO ()]
forall a b. (a -> b) -> [a] -> [b]
map (InChan a -> a -> IO ()
forall a. InChan a -> a -> IO ()
writeChan InChan a
ch)