{-# LANGUAGE NoImplicitPrelude #-}
module Entwine.Data.Pin (
Pin
, newPin
, checkPin
, waitForPin
, pullPin
) where
import Control.Concurrent.MVar (MVar, newEmptyMVar, tryTakeMVar
, readMVar, tryPutMVar)
import Entwine.P
import System.IO (IO)
newtype Pin =
Pin { pin :: MVar () }
newPin :: IO Pin
newPin =
Pin <$> newEmptyMVar
checkPin :: Pin -> IO Bool
checkPin =
fmap isJust . tryTakeMVar . pin
waitForPin :: Pin -> IO ()
waitForPin =
void . readMVar . pin
pullPin :: Pin -> IO ()
pullPin =
void . flip tryPutMVar () . pin