[Haskell-cafe] Re: DevRandom

Bryan Donlan bd.haskell at uguu.us
Thu Feb 1 02:02:55 EST 2007


Yitzchak Gale wrote:
> Bryan Donlan wrote:
>> This re-opens the device every time we need it.
>> How about opening once, when it's first needed?
> 
> Good idea.
> 
>> hDevRandom :: Handle
>> {-# NOINLINE hDevRandom  #-}
>> hDevRandom = unsafePerformIO $ openFile "/dev/random" ReadMode
>>
>> hDevURandom :: Handle
>> {-# NOINLINE hDevURandom  #-}
>> hDevURandom = unsafePerformIO $ openFile "/dev/urandom" ReadMode
> 
> 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.


More information about the Haskell-Cafe mailing list