module Control.Concurrent.Worker (
Worker(..), WorkerStopped(..),
startWorker, workerAlive, workerDone,
sendTask, stopWorker, joinWorker, syncTask,
inWorkerWith, inWorker,
module Control.Concurrent.Async
) where
import Control.Concurrent.MVar
import Control.Monad.IO.Class
import Control.Monad.Catch
import Control.Monad.Except
import Data.Maybe (isNothing)
import Data.Typeable
import Control.Concurrent.FiniteChan
import Control.Concurrent.Async
data Worker m = Worker {
workerChan :: Chan (Async (), m ()),
workerTask :: MVar (Async ()) }
data WorkerStopped = WorkerStopped deriving (Show, Typeable)
instance Exception WorkerStopped
startWorker :: MonadIO m => (m () -> IO ()) -> (m () -> m ()) -> (m () -> m ()) -> IO (Worker m)
startWorker run initialize handleErrs = do
ch <- newChan
taskVar <- newEmptyMVar
let
job = onException (run $ initialize go) $ do
closeChan ch
abort
go = do
t <- fmap snd <$> liftIO (getChan ch)
maybe (return ()) (\f' -> handleErrs f' >> go) t
abort = do
a <- fmap fst <$> liftIO (getChan ch)
maybe (return ()) (\a' -> liftIO (cancel a') >> abort) a
async job >>= putMVar taskVar
return $ Worker ch taskVar
workerAlive :: Worker m -> IO Bool
workerAlive w = do
task <- readMVar $ workerTask w
isNothing <$> poll task
workerDone :: Worker m -> IO Bool
workerDone = doneChan . workerChan
sendTask :: (MonadCatch m, MonadIO m) => Worker m -> m a -> IO (Async a)
sendTask w act = mfix $ \async' -> do
var <- newEmptyMVar
let
act' = (act >>= liftIO . putMVar var . Right) `catch` onError
onError :: MonadIO m => SomeException -> m ()
onError = liftIO . putMVar var . Left
f = do
p <- sendChan (workerChan w) (void async', void act')
unless p $ putMVar var (Left $ SomeException WorkerStopped)
r <- takeMVar var
either throwM return r
async f
stopWorker :: Worker m -> IO ()
stopWorker = closeChan . workerChan
joinWorker :: Worker m -> IO ()
joinWorker w = do
stopWorker w
async' <- readMVar $ workerTask w
void $ waitCatch async'
syncTask :: (MonadCatch m, MonadIO m) => Worker m -> IO ()
syncTask w = sendTask w (return ()) >>= void . wait
inWorkerWith :: (MonadIO m, MonadCatch m, MonadIO n) => (SomeException -> n a) -> Worker m -> m a -> n a
inWorkerWith err w act = liftIO (sendTask w act) >>= (liftIO . waitCatch >=> either err return)
inWorker :: (MonadIO m, MonadCatch m) => Worker m -> m a -> IO a
inWorker w act = sendTask w act >>= liftIO . wait