{-# LANGUAGE ScopedTypeVariables, CPP #-}
module Transient.Indeterminism (
choose, choose', chooseStream, collect, collect', group, groupByTime, burst
) where
import Transient.Internals hiding (retry)
import Data.IORef
import Control.Applicative
import Data.Monoid
import Control.Concurrent
import Control.Monad.State
import Control.Exception
import qualified Data.ByteString.Char8 as BS
choose :: [a] -> TransIO a
choose []= empty
choose xs = chooseStream xs >>= checkFinalize
chooseStream :: [a] -> TransIO (StreamData a)
chooseStream []= empty
chooseStream xs = do
evs <- liftIO $ newIORef xs
parallel $ do
es <- atomicModifyIORef evs $ \es -> let tes= tail es in (tes,es)
case es of
[x] -> x `seq` return $ SLast x
x:_ -> x `seq` return $ SMore x
choose' :: [a] -> TransIO a
choose' xs = foldl (<|>) empty $ map (async . return) xs
group :: Int -> TransIO a -> TransIO [a]
group num proc = do
v <- liftIO $ newIORef (0,[])
x <- proc
mn <- liftIO $ atomicModifyIORef v $ \(n,xs) ->
let n'=n +1
in if n'== num
then ((0,[]), Just $ x:xs)
else ((n', x:xs),Nothing)
case mn of
Nothing -> stop
Just xs -> return xs
collect :: Int -> TransIO a -> TransIO [a]
collect n = collect' n 0
collect' :: Int -> Int -> TransIO a -> TransIO [a]
collect' n t search= do
rv <- liftIO $ newEmptyMVar
results <- liftIO $ newIORef (0,[])
let worker = do
r <- abduce >> search
liftIO $ putMVar rv $ Just r
stop
timer= do
when (t > 0) $ do
async $ threadDelay t >> putMVar rv Nothing
empty
monitor= liftIO loop
where
loop = do
mr <- takeMVar rv
(n',rs) <- readIORef results
case mr of
Nothing -> return rs
Just r -> do
let n''= n' + 1
let rs'= r:rs
writeIORef results (n'',rs')
if (n > 0 && n'' >= n)
then return (rs')
else loop
`catch` \(_ :: BlockedIndefinitelyOnMVar) ->
readIORef results >>= return . snd
oneThread $ timer <|> worker <|> monitor
burst :: Int -> TransIO a -> TransIO (StreamData a)
burst timeout comp= do
r <- oneThread comp
return (SMore r) <|> (async (threadDelay timeout) >> return SDone)
groupByTime :: Monoid a => Int -> TransIO a -> TransIO a
groupByTime timeout comp= do
v <- liftIO $ newIORef mempty
gather v <|> run v
where
run v = do
x <- comp
liftIO $ atomicModifyIORef v $ \xs -> (xs <> x,())
empty
gather v= waitEvents $ do
threadDelay timeout
atomicModifyIORef v $ \xs -> (mempty , xs)