[Haskell-cafe] Relaxing atomicity of STM transactions

Antoine Latter aslatter at gmail.com
Tue Sep 28 23:17:50 EDT 2010


On Tue, Sep 28, 2010 at 9:19 PM, Brandon Moore
<brandon_m_moore at yahoo.com> wrote:
>
>
> On Sep 28, 2010, at 6:36 PM, Tom Hawkins <tomahawkins at gmail.com> wrote:
>
> Thanks for the responses, but I think I should explain a bit more.
> I'm not interested in being able to read the live value of a TVar at
> any arbitrary time (via. unsafeIOToSTM).  But rather I would like
> looslyReadTVar to have exactly the same semantics as readTVar, except
> that the STM runtime would not reject the transaction if the TVar is
> modified by another transaction before the atomic commit takes place.
>
> Given the current implementation, I think the easiest way to get those
> semantics is to lift the untracked readTVarIO into STM with unsafeIOToSTM.
>

Even though I thought it was awsome up above, it is a really unsafe
function with that implementation:

-------


import GHC.Conc
import Control.Monad

readTVarLoose :: TVar a -> STM a
readTVarLoose = unsafeIOToSTM . readTVarIO

testAction tv
    = do
  readTVar tv >>= writeTVar tv . succ
  (,) `fmap` readTVar tv `ap` readTVarLoose tv

main = do
  tv <- newTVarIO 3
  (a,b) <- atomically $ testAction tv
  print a
  print b
-------

What's happening is that readTVarIO doesn't know to hit the
transaction log for the "true" value of the TVar.

So we need something that will read previous entries in the
transaction log, but will not write to the transaction log.

You'll need a new primop for this, which would be implemented in rts/STM.c

It looks like you would take most of stmReadTVar from STM.c, and get
rid of everything that calls get_new_entry.

Antoine


More information about the Haskell-Cafe mailing list