[Haskell-cafe] stm onCommit

Silvio Frischknecht silvio.frischi at gmail.com
Sun Jul 5 20:54:43 UTC 2015


I need a onCommit functionality for stm. I know there is the
stm-io-hooks [1] package. But it seems so big for such a small thing and
then I have to translate everything to their monad.
lift lift lift ...

So I thought of a little hack to solve the issue and I'm wondering if
this is safe to use. My understanding of stm internals is very limited.
It's basically just a TChan with IO actions in it and there is another
thread waiting to execute anything inserted into it.

    import Control.Concurrent.STM.TChan

    onCommitChan :: TChan (IO ())
    {-# NOINLINE onCommit #-}
    onCommitChan = unsafePerformIO $ do
        chan <- newTChanIO
        forkIO $ forever $ atomically $ readTChan chan
        return chan

    onCommit :: IO () -> STM ()
    onCommit = writeTChan onCommitChan

It would be cool if an onCommit hook could be directly added to the stm
package.


Silvio


[1] https://hackage.haskell.org/package/stm-io-hooks-1.0.1


More information about the Haskell-Cafe mailing list