
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.