STM and unsafePerformIO

Remi Turk rturk at science.uva.nl
Wed Aug 3 07:05:25 EDT 2005


On Wed, Aug 03, 2005 at 12:50:54PM +0200, Robert van Herk wrote:
> Hello All,
> 
> I think I've read somewhere that STM doesn't like unsafePerformIO. 
> However, I would like to use a global STM variable. Something like this:
> 
> module Main where
> import GHC.Conc
> import System.IO.Unsafe
> 
> tSid = unsafePerformIO (atomically (newTVar 0))
> 
> tickSessionID :: STM Int
> tickSessionID =
>  do sid <- readTVar tSid
>     writeTVar tSid (sid + 1)
>     return sid
> 
> main = atomically tickSessionID
> 
> 
> 
> But, when I try this, the evaluation of main causes a segmentation 
> fault. Is there a workaround for this bug?
> 
> Regards,
> Robert

It probably dies not because of unsafePerformIO per se, but
because STM doesn't understand nested transactions, and
unsafePerformIO here results in a nested transaction. Using the
following main works for me, as it forces both "atomically"'s to
be evaluated sequentially:

main = tSid `seq` atomically tickSessionID


See also
http://haskell.org/pipermail/glasgow-haskell-users/2005-June/008615.html
and
http://sourceforge.net/tracker/index.php?func=detail&aid=1235728&group_id=8032&atid=108032

Happy hacking,
Remi

P.S. Could you find out (and fix) what inserts those spurious *'s in your code?

-- 
Nobody can be exactly like me. Even I have trouble doing it.


More information about the Glasgow-haskell-users mailing list