-- |This module uses Haskell concurrency libraries to build an extremely simple
-- event system that should perform better than the Control.Event module
-- but does not provide features such as STM action scheduling.
module Control.Event.Relative
	( EventId
	, addEvent
	, delEvent
	) where

import Prelude hiding (catch)
import Control.Concurrent
import Control.Exception
import Control.Concurrent.MVar

type EventId = (ThreadId, MVar ())

-- |'addEvent delay action' will delay
-- for 'delay' miliseconds then execute 'action'. An EventId
-- is returned, allowing the event to be canceled.
addEvent :: Int -> IO () -> IO EventId
addEvent delay event = do
	m <- newEmptyMVar
	t <- forkIO (eventThread m)
	return (t,m)
  where
    eventThread m = do
	threadDelay delay
	forkIO event
	putMVar m ()

-- |'delEvent eid' deletes the event and returns
-- 'True' if the event was _probably_ deleted*.  If 'False' is returned
-- then the time definately elapsed and the action was forked off.
--
-- * There is a small possibility the delEvent occured after the forkIO
-- but before the signalling MVar was filled, thus causing this uncertainty.
delEvent :: EventId -> IO Bool
delEvent (t,m) =
	killThread t >>
	isEmptyMVar m