[Haskell-cafe] Re: DevRandom

Yitzchak Gale gale at sefer.org
Thu Feb 1 04:37:01 EST 2007

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.


More information about the Haskell-Cafe mailing list