Movatterモバイル変換


[0]ホーム

URL:


{-# LANGUAGE Unsafe #-}{-# LANGUAGE NoImplicitPrelude           , MagicHash           , UnboxedTuples  #-}{-# OPTIONS_HADDOCK not-home #-}------------------------------------------------------------------------------- |-- Module      :  GHC.IO.Unsafe-- Copyright   :  (c) The University of Glasgow 1994-2002-- License     :  see libraries/base/LICENSE---- Maintainer  :  cvs-ghc@haskell.org-- Stability   :  internal-- Portability :  non-portable (GHC Extensions)---- Unsafe IO operations-------------------------------------------------------------------------------moduleGHC.IO.Unsafe(unsafePerformIO,unsafeInterleaveIO,unsafeDupablePerformIO,unsafeDupableInterleaveIO,noDuplicate,)whereimportGHC.Base{-Note [unsafePerformIO and strictness]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Consider this sub-expression (from tests/lib/should_run/memo002) unsafePerformIO (do { lockMemoTable                     ; let r = f x                     ; updateMemoTable x r                     ; unlockMemoTable                     ; return r })It's super-important that the `let r = f x` is lazy. If the demandanalyser sees that `r` is sure to be demanded, it'll use call-by-valuefor (f x), that will try to lock the already-locked table => deadlock.See #19181 and #19413.Now `r` doesn't look strict, because it's wrapped in a `return`.But if we were to define unsafePerformIO like this  unsafePerformIO (IO m) = case runRW# m of (# _, r #) -> rthen we'll push that `case` inside the argument to runRW#, giving  runRW# (\s -> case lockMemoTable s of s1 ->                let r = f x in                case updateMemoTable s1 of s2 ->                case unlockMemoTable s2 of _ ->                r)And now that `let` really does look strict.  No good!Solution: wrap the result of the unsafePerformIO in 'lazy', to concealit from the demand analyser:  unsafePerformIO (IO m) = case runRW# m of (# _, r #) -> lazy r                                                 ------>  ^^^^See also Note [lazyId magic] in GHC.Types.Id.Make-}{-|This is the \"back door\" into the 'IO' monad, allowing'IO' computation to be performed at any time.  Forthis to be safe, the 'IO' computation should befree of side effects and independent of its environment.If the I\/O computation wrapped in 'unsafePerformIO' performs sideeffects, then the relative order in which those side effects takeplace (relative to the main I\/O trunk, or other calls to'unsafePerformIO') is indeterminate.  Furthermore, when using'unsafePerformIO' to cause side-effects, you should take the followingprecautions to ensure the side effects are performed as many times asyou expect them to be.  Note that these precautions are necessary forGHC, but may not be sufficient, and other compilers may requiredifferent precautions:  * Use @{\-\# NOINLINE foo \#-\}@ as a pragma on any function @foo@        that calls 'unsafePerformIO'.  If the call is inlined,        the I\/O may be performed more than once.  * Use the compiler flag @-fno-cse@ to prevent common sub-expression        elimination being performed on the module, which might combine        two side effects that were meant to be separate.  A good example        is using multiple global variables (like @test@ in the example below).  * Make sure that the either you switch off let-floating (@-fno-full-laziness@), or that the        call to 'unsafePerformIO' cannot float outside a lambda.  For example,        if you say:        @           f x = unsafePerformIO (newIORef [])        @        you may get only one reference cell shared between all calls to @f@.        Better would be        @           f x = unsafePerformIO (newIORef [x])        @        because now it can't float outside the lambda.It is less well known that'unsafePerformIO' is not type safe.  For example:>     test :: IORef [a]>     test = unsafePerformIO $ newIORef []>>     main = do>             writeIORef test [42]>             bang <- readIORef test>             print (bang :: [Char])This program will core dump.  This problem with polymorphic referencesis well known in the ML community, and does not arise with normalmonadic use of references.  There is no easy way to make it impossibleonce you use 'unsafePerformIO'.  Indeed, it ispossible to write @coerce :: a -> b@ with thehelp of 'unsafePerformIO'.  So be careful!WARNING: If you're looking for "a way to get a 'String' from an 'IO String'",then 'unsafePerformIO' is not the way to go.  Learn about do-notation and the@<-@ syntax element before you proceed.-}unsafePerformIO::IOa->aunsafePerformIO :: forall a. IO a -> aunsafePerformIOIO am=IO a -> aforall a. IO a -> aunsafeDupablePerformIO(IO ()noDuplicateIO () -> IO a -> IO aforall a b. IO a -> IO b -> IO bforall (m :: * -> *) a b. Monad m => m a -> m b -> m b>>IO am){-|This version of 'unsafePerformIO' is more efficientbecause it omits the check that the IO is only being performed by asingle thread.  Hence, when you use 'unsafeDupablePerformIO',there is a possibility that the IO action may be performed multipletimes (on a multiprocessor), and you should therefore ensure thatit gives the same results each time. It may even happen that oneof the duplicated IO actions is only run partially, and then interruptedin the middle without an exception being raised. Therefore, functionslike 'Control.Exception.bracket' cannot be used safely within'unsafeDupablePerformIO'.@since 4.4.0.0-}unsafeDupablePerformIO::IOa->a-- See Note [unsafePerformIO and strictness]unsafeDupablePerformIO :: forall a. IO a -> aunsafeDupablePerformIO(IOState# RealWorld -> (# State# RealWorld, a #)m)=case(State# RealWorld -> (# State# RealWorld, a #))-> (# State# RealWorld, a #)forall o. (State# RealWorld -> o) -> orunRW#State# RealWorld -> (# State# RealWorld, a #)mof(#State# RealWorld_,aa#)->a -> aforall a. a -> alazyaa{-|'unsafeInterleaveIO' allows an 'IO' computation to be deferred lazily.When passed a value of type @IO a@, the 'IO' will only be performedwhen the value of the @a@ is demanded.  This is used to implement lazyfile reading, see 'System.IO.hGetContents'.-}{-# INLINEunsafeInterleaveIO#-}unsafeInterleaveIO::IOa->IOaunsafeInterleaveIO :: forall a. IO a -> IO aunsafeInterleaveIOIO am=IO a -> IO aforall a. IO a -> IO aunsafeDupableInterleaveIO(IO ()noDuplicateIO () -> IO a -> IO aforall a b. IO a -> IO b -> IO bforall (m :: * -> *) a b. Monad m => m a -> m b -> m b>>IO am)-- Note [unsafeDupableInterleaveIO should not be inlined]-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~---- We used to believe that INLINE on unsafeInterleaveIO was safe,-- because the state from this IO thread is passed explicitly to the-- interleaved IO, so it cannot be floated out and shared.---- HOWEVER, if the compiler figures out that r is used strictly here,-- then it will eliminate the thunk and the side effects in m will no-- longer be shared in the way the programmer was probably expecting,-- but can be performed many times.  In #5943, this broke our-- definition of fixIO, which contains----    ans <- unsafeInterleaveIO (takeMVar m)---- after inlining, we lose the sharing of the takeMVar, so the second-- time 'ans' was demanded we got a deadlock.  We could fix this with-- a readMVar, but it seems wrong for unsafeInterleaveIO to sometimes-- share and sometimes not (plus it probably breaks the noDuplicate).-- So now, we do not inline unsafeDupableInterleaveIO.{-|'unsafeDupableInterleaveIO' allows an 'IO' computation to be deferred lazily.When passed a value of type @IO a@, the 'IO' will only be performedwhen the value of the @a@ is demanded.The computation may be performed multiple times by different threads,possibly at the same time. To ensure that the computation is performedonly once, use 'unsafeInterleaveIO' instead.-}{-# NOINLINEunsafeDupableInterleaveIO#-}-- See Note [unsafeDupableInterleaveIO should not be inlined]unsafeDupableInterleaveIO::IOa->IOaunsafeDupableInterleaveIO :: forall a. IO a -> IO aunsafeDupableInterleaveIO(IOState# RealWorld -> (# State# RealWorld, a #)m)=(State# RealWorld -> (# State# RealWorld, a #)) -> IO aforall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO aIO(\State# RealWorlds->letr :: ar=caseState# RealWorld -> (# State# RealWorld, a #)mState# RealWorldsof(#State# RealWorld_,ares#)->aresin(#State# RealWorlds,ar#)){-|Ensures that the suspensions under evaluation by the current threadare unique; that is, the current thread is not evaluating anythingthat is also under evaluation by another thread that has also executed'noDuplicate'.This operation is used in the definition of 'unsafePerformIO' toprevent the IO action from being executed multiple times, which is usuallyundesirable.-}noDuplicate::IO()noDuplicate :: IO ()noDuplicate=(State# RealWorld -> (# State# RealWorld, () #)) -> IO ()forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO aIO((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()forall a b. (a -> b) -> a -> b$\State# RealWorlds->caseState# RealWorld -> State# RealWorldforall d. State# d -> State# dnoDuplicate#State# RealWorldsofState# RealWorlds'->(#State# RealWorlds',()#)

[8]ページ先頭

©2009-2025 Movatter.jp