Re: STM and unsafePerformIO

2005-08-03 Thread Robert van Herk
Oh I see! Your sollutions are indeed a lot better than implementating 
all my code using MVars :-).


Thanks,

Robert

Remi Turk wrote:

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?

 


___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


RE: STM and unsafePerformIO

2005-08-03 Thread Simon Marlow
On 03 August 2005 11:51, Robert van Herk wrote:

> 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?

You're ok as long as the global variable doesn't get evaluated inside
another atomically.  The problem is that nested atomically isn't
supported; the RTS gets confused.

For example, you could say 
  
   main = do
  evaluate tSid
  ...

to force evaluation of tSid before doing anything else.

Cheers,
Simon
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: STM and unsafePerformIO

2005-08-03 Thread Remi Turk
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.
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: STM and unsafePerformIO

2005-08-03 Thread Robert van Herk

Never mind, I can probably use an MVar to work around this...

Robert

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
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users