{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
module FRP.Rhine.Schedule.Concurrently where
import Control.Concurrent
import FRP.Rhine
concurrently :: (Clock IO cl1, Clock IO cl2, TimeDomainOf cl1 ~ TimeDomainOf cl2) => Schedule IO cl1 cl2
concurrently = Schedule $ \cl1 cl2 -> do
iMVar <- newEmptyMVar
mvar <- newEmptyMVar
_ <- forkIO $ do
(runningClock, initTime) <- startClock cl1
putMVar iMVar initTime
reactimate $ runningClock >>> second (arr Left) >>> arrM (putMVar mvar)
_ <- forkIO $ do
(runningClock, initTime) <- startClock cl2
putMVar iMVar initTime
reactimate $ runningClock >>> second (arr Right) >>> arrM (putMVar mvar)
initTime <- takeMVar iMVar
_ <- takeMVar iMVar
return (arrM_ $ takeMVar mvar, initTime)