summaryrefslogtreecommitdiffhomepage
path: root/src/Main.hs
diff options
context:
space:
mode:
authorDaniel Schüssler <933504+DanielSchuessler@users.noreply.github.com>2018-08-26 22:40:27 +0200
committerDaniel Schüssler <933504+DanielSchuessler@users.noreply.github.com>2018-10-06 19:53:57 +0200
commitb9a181870ce82c309613fba17edd9fd0b78b43cc (patch)
treeab325d0172724404944dbeaf9697d93e5bf46434 /src/Main.hs
parent31fa44e9aa4ba4d3db8688d785b766fd5e7cf8f1 (diff)
downloadxmobar-b9a181870ce82c309613fba17edd9fd0b78b43cc.tar.gz
xmobar-b9a181870ce82c309613fba17edd9fd0b78b43cc.tar.bz2
Signal termination to plugin threads and wait for them upon program exit
This is necessary to allow the plugin threads to run their cleanup actions.
Diffstat (limited to 'src/Main.hs')
-rw-r--r--src/Main.hs22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/Main.hs b/src/Main.hs
index 4f35b38..9a3a2e8 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -25,6 +25,7 @@ import Parsers
import Config
import XUtil
+import Data.Foldable (for_)
import Data.List (intercalate)
import qualified Data.Map as Map
@@ -37,6 +38,8 @@ import System.Exit
import System.Environment
import System.FilePath ((</>))
import System.Posix.Files
+import Control.Exception
+import Control.Concurrent.Async (Async, cancel)
import Control.Monad (unless)
import Text.Read (readMaybe)
@@ -63,12 +66,19 @@ main = do
fl <- mapM (initFont d) (additionalFonts conf)
cls <- mapM (parseTemplate conf) (splitTemplate conf)
sig <- setupSignalHandler
- vars <- mapM (mapM $ startCommand sig) cls
- (r,w) <- createWin d fs conf
- let ic = Map.empty
- to = textOffset conf
- ts = textOffsets conf ++ replicate (length fl) (-1)
- startLoop (XConf d r w (fs:fl) (to:ts) ic conf) sig vars
+ bracket (mapM (mapM $ startCommand sig) cls)
+ cleanupThreads
+ $ \vars -> do
+ (r,w) <- createWin d fs conf
+ let ic = Map.empty
+ to = textOffset conf
+ ts = textOffsets conf ++ replicate (length fl) (-1)
+ startLoop (XConf d r w (fs:fl) (to:ts) ic conf) sig vars
+
+cleanupThreads :: [[([Async ()], a)]] -> IO ()
+cleanupThreads vars =
+ for_ (concat vars) $ \(asyncs, _) ->
+ for_ asyncs cancel
-- | Splits the template in its parts
splitTemplate :: Config -> [String]