on master help sign in
UnicodeWidthTable.Install: clean up lock use
commit84e391f56a0a5647912e78284b8971e81c0e0c4e authorJonathan Daugherty <cygnus@foobox.com> dateMon 9 Mar 2020, 10:24 (7 years ago) parent5772d6a
1 file changed+22−21
src/Graphics/Vty/UnicodeWidthTable/Install.hs+22−21
src/Graphics/Vty/UnicodeWidthTable/Install.hs+22−21
@@ -28,9 +28,11 @@ installLock = unsafePerformIO $ newMVar ()
2828--
2929-- This function is thread-safe.
3030isCustomTableReady :: IO Bool
31−isCustomTableReady =
32− E.bracket takeInstallLock (const releaseInstallLock) $
33− const $ (== 1) <$> c_isCustomTableReady
31+isCustomTableReady = withInstallLock $ (== 1) <$> c_isCustomTableReady
32+
33+withInstallLock :: IO a -> IO a
34+withInstallLock act =
35+ E.bracket takeInstallLock (const releaseInstallLock) $ const act
3436
3537takeInstallLock :: IO ()
3638takeInstallLock = takeMVar installLock
@@ -96,24 +98,23 @@ instance E.Exception TableInstallException
9698--
9799-- This function is thread-safe.
98100installUnicodeWidthTable :: UnicodeWidthTable -> IO ()
99−installUnicodeWidthTable table =
100− E.bracket takeInstallLock (const releaseInstallLock) $ const $ do
101− initResult <- initCustomTable tableSize
102− when (initResult /= 0) $
103− E.throw $ TableInitFailure initResult tableSize
104−
105− forM_ (unicodeWidthTableRanges table) $ \r -> do
106− result <- setCustomTableRange (rangeStart r)
107− (rangeSize r)
108− (rangeColumns r)
109−
110− when (result /= 0) $ do
111− deallocateCustomTable
112− E.throw $ TableRangeFailure result r
113−
114− actResult <- activateCustomTable
115− when (actResult /= 0) $
116− E.throw $ TableActivationFailure actResult
101+installUnicodeWidthTable table = withInstallLock $ do
102+ initResult <- initCustomTable tableSize
103+ when (initResult /= 0) $
104+ E.throw $ TableInitFailure initResult tableSize
105+
106+ forM_ (unicodeWidthTableRanges table) $ \r -> do
107+ result <- setCustomTableRange (rangeStart r)
108+ (rangeSize r)
109+ (rangeColumns r)
110+
111+ when (result /= 0) $ do
112+ deallocateCustomTable
113+ E.throw $ TableRangeFailure result r
114+
115+ actResult <- activateCustomTable
116+ when (actResult /= 0) $
117+ E.throw $ TableActivationFailure actResult
117118
118119------------------------------------------------------------------------
119120-- C imports