[Haskell-cafe] STM commit hooks

Einar Karttunen ekarttun at cs.helsinki.fi
Tue Nov 29 07:39:46 EST 2005


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)

But this does not work either. Given Trx1 and Trx2, the following may occur:

1) Trx1 commits
<thread switch>
2) Trx2 commits (and depends on Trx1)
3) Trx2 commit is logged to disk
<system crash>

This means that the log would be inconsistent. Is there a way to implement
the commit that works?

- Einar Karttunen

More information about the Haskell-Cafe mailing list