Bryan Donlan wrote:
{-# NOINLINE hDevRandom  #-}
hDevRandom = unsafePerformIO $ openFile "/dev/random" ReadMode

I wrote:
The NOINLINE guarantees that openFile is called only
once. But does it guarantee that openFile is NOT called
if we do not need it? We could check what the compilers
actually do, but I am not sure we have a guarantee here.

There's commentary in GHC/Conc.lhs that this is the case:
{-# NOINLINE pendingEvents #-}
{-# NOINLINE pendingDelays #-}
(pendingEvents,pendingDelays) = unsafePerformIO $ do
   startIOManagerThread
   reqs <- newIORef []
   dels <- newIORef []
   return (reqs, dels)
        -- the first time we schedule an IO request, the service thread
        -- will be created (cool, huh?)
I don't know if this is a documented guarentee however.

Hmm. I'm not sure what that comment means.
They are doing just what I did - creating only an
empty IORef, with the actual resource allocated only
when needed. Also, this is located inside a module that
is explicitly GHC-specific.

Regards,
Yitz
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to