From 68f9f51cd7e20190e1ef2fd95beaf7c852f11c81 Mon Sep 17 00:00:00 2001 From: Jochen Keil Date: Wed, 22 Aug 2012 20:25:29 +0200 Subject: Refactor MVar SignalType to TMVar SignalType Replace MVar with TMVar from the STM package. This is common for ghc now. Since STM is used everywhere else in the src it also adds no additional dependencies. The main reason for this switch is, that readMVar, swapMVar, etc. are only atomically if there is no other producer for this MVar i.e. putMVar. For example readMVar is a combination of putMVar and takeMVar. Due to scheduling and readMVar's non-atomicity it is possible that values written to the MVar appear in the wrong order. Using TMVar fixes this problem, since it allows really atomical read/swap operations. --- src/Signal.hs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/Signal.hs') diff --git a/src/Signal.hs b/src/Signal.hs index a003859..44fe4f9 100644 --- a/src/Signal.hs +++ b/src/Signal.hs @@ -19,7 +19,7 @@ module Signal where import Data.Typeable (Typeable) -import Control.Concurrent +import Control.Concurrent.STM import Control.Exception hiding (handle) import System.Posix.Signals @@ -52,19 +52,19 @@ parseSignalType :: String -> Maybe SignalType parseSignalType = fmap fst . safeHead . reads -- | Signal handling -setupSignalHandler :: IO (MVar SignalType) +setupSignalHandler :: IO (TMVar SignalType) setupSignalHandler = do - tid <- newEmptyMVar + tid <- newEmptyTMVarIO installHandler sigUSR2 (Catch $ updatePosHandler tid) Nothing installHandler sigUSR1 (Catch $ changeScreenHandler tid) Nothing return tid -updatePosHandler :: MVar SignalType -> IO () +updatePosHandler :: TMVar SignalType -> IO () updatePosHandler sig = do - putMVar sig Reposition + atomically $ putTMVar sig Reposition return () -changeScreenHandler :: MVar SignalType -> IO () +changeScreenHandler :: TMVar SignalType -> IO () changeScreenHandler sig = do - putMVar sig ChangeScreen + atomically $ putTMVar sig ChangeScreen return () -- cgit v1.2.3