[Haskell-cafe] STM commit hooks

Simon Marlow simonmar at microsoft.com
Fri Dec 2 06:15:09 EST 2005

On 29 November 2005 12:40, Einar Karttunen wrote:

> Hello
> I have been playing with STM and want to log transactions to disk.
> Defining a logging function like:
> log h act = unsafeIOToSTM $ hPrint h act
> works most the time. Aborts can be handled with:
> abort h = log h Abort >> retry
> atomic' h act = atomically (act `orElse` abort h)
> But is it possible to handle a commit?
> commit h = unsafeIOToSTM (hPrint h Commit >> hSync h)
> atomically2 h act = atomically ((act >> commit h) `orElse` abort h)
> This won't work because the transaction is validated and
> maybe aborted after the commit is logged to disk.
> Another alternative would be:
> atomically3 h act = atomically (act `orElse` abort h) >> atomically
> (commit h) 

If I'm understanding you correctly, what you need is to log the commit
operation before another thread commits and logs a transaction.  So you
just need to close this race hole at the '>>' in the above definition.
How about this:

  log_lock <- newTMVar ()

  atomically3 h act = do
    atomically (do act; takeTMVar log_lock `orElse` abort h)
    hPrint h Commit
    atomically (putTMVar log_lock ())

does that work?


More information about the Haskell-Cafe mailing list