[Haskell-cafe] stm onCommit

Lana Black lanablack at amok.cc
Mon Jul 6 11:49:42 UTC 2015


On 22:54 Sun 05 Jul     , Silvio Frischknecht wrote:
> 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
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

You can't use atomically inside unsafePerformIO.


More information about the Haskell-Cafe mailing list