summaryrefslogtreecommitdiffhomepage
path: root/src/Signal.hs
diff options
context:
space:
mode:
authorJochen Keil <jochen.keil@gmail.com>2012-08-22 20:25:29 +0200
committerJochen Keil <jochen.keil@gmail.com>2012-08-22 20:25:29 +0200
commit68f9f51cd7e20190e1ef2fd95beaf7c852f11c81 (patch)
tree666f67f41cde4a7ab9a3ea5d59a9ad22854e45a3 /src/Signal.hs
parentd52278f48d98f116d3a1ebd21000bf80d90b8c9d (diff)
downloadxmobar-68f9f51cd7e20190e1ef2fd95beaf7c852f11c81.tar.gz
xmobar-68f9f51cd7e20190e1ef2fd95beaf7c852f11c81.tar.bz2
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.
Diffstat (limited to 'src/Signal.hs')
-rw-r--r--src/Signal.hs14
1 files changed, 7 insertions, 7 deletions
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 ()