on master help sign in
UnicodeWidthTable.Install: clean up lock use
84e391fJonathan Daugherty
src/Graphics/Vty/UnicodeWidthTable/Install.hs+22−21
@@ -28,9 +28,11 @@ installLock = unsafePerformIO $ newMVar ()
28--
28--
29-- This function is thread-safe.
29-- This function is thread-safe.
30isCustomTableReady :: IO Bool
30isCustomTableReady :: IO Bool
31−isCustomTableReady =
31+isCustomTableReady = withInstallLock $ (== 1) <$> c_isCustomTableReady
32+
33+withInstallLock :: IO a -> IO a
34+withInstallLock act =
32− E.bracket takeInstallLock (const releaseInstallLock) $
35+ E.bracket takeInstallLock (const releaseInstallLock) $ const act
33− const $ (== 1) <$> c_isCustomTableReady
34
36
35takeInstallLock :: IO ()
37takeInstallLock :: IO ()
36takeInstallLock = takeMVar installLock
38takeInstallLock = takeMVar installLock
@@ -96,24 +98,23 @@ instance E.Exception TableInstallException
96--
98--
97-- This function is thread-safe.
99-- This function is thread-safe.
98installUnicodeWidthTable :: UnicodeWidthTable -> IO ()
100installUnicodeWidthTable :: UnicodeWidthTable -> IO ()
99−installUnicodeWidthTable table =
101+installUnicodeWidthTable table = withInstallLock $ do
100− E.bracket takeInstallLock (const releaseInstallLock) $ const $ do
101− initResult <- initCustomTable tableSize
102+ initResult <- initCustomTable tableSize
102− when (initResult /= 0) $
103+ when (initResult /= 0) $
103− E.throw $ TableInitFailure initResult tableSize
104+ E.throw $ TableInitFailure initResult tableSize
104−
105+
105− forM_ (unicodeWidthTableRanges table) $ \r -> do
106+ forM_ (unicodeWidthTableRanges table) $ \r -> do
106− result <- setCustomTableRange (rangeStart r)
107+ result <- setCustomTableRange (rangeStart r)
107− (rangeSize r)
108+ (rangeSize r)
108− (rangeColumns r)
109+ (rangeColumns r)
109−
110+
110− when (result /= 0) $ do
111+ when (result /= 0) $ do
111− deallocateCustomTable
112+ deallocateCustomTable
112− E.throw $ TableRangeFailure result r
113+ E.throw $ TableRangeFailure result r
113−
114+
114− actResult <- activateCustomTable
115+ actResult <- activateCustomTable
115− when (actResult /= 0) $
116+ when (actResult /= 0) $
116− E.throw $ TableActivationFailure actResult
117+ E.throw $ TableActivationFailure actResult
117
118
118------------------------------------------------------------------------
119------------------------------------------------------------------------
119-- C imports
120-- C imports