Weak pointers and STM

Simon Marlow marlowsd at gmail.com
Mon Dec 1 11:17:59 EST 2008

John Meacham wrote:
> Are there any caveats to using weak pointers and STM together? in
> particular, the two cases I am interested in are
> 1. is using 'deRefWeak' fully safe inside 'unsafeIOtoSTM'? As in, will
> it combine arbitrary STM actions with checking if a weak pointer is
> still valid atomically?

You certainly can't use STM to wait until the result of deRefWeak changes 
with retry.  but that's probably not what you're asking.  It's certainly 
not atomic, in that the result of deRefWeak might be different at different 
points in the transaction.  Hmm, what property is it you want here?

> 2. is using an atomically retry safe inside of a finalizer? Will it
> introduce any concurrency bottlenecks or can I just consider code run in
> a finalizer just like code run in any other thread? 

Yes. Once running, a finalizer is just like another thread, with one 
exception: we batch finalizers that start together in a single thread, so 
if these finalizers need to communicate with each other, a deadlock could 
ensue.  This is fixable without too much difficulty though, so let us know 
if it is a problem for you.

> I just wanted to be sure before I base an integral component of a projects design
> on these working properly.

I'd be wary about relying on unsafeIOToSTM in any significant way.  We know 
it has some pretty severe drawbacks, for one thing if the transaction is 
aborted then the IO computation is just discarded, not sent an exception or 
anything (I think we have a ticket filed for this).


More information about the Glasgow-haskell-users mailing list