[commit: ghc] master: base: Make documentation of atomically more accurate (b8e324a)
git at git.haskell.org
git at git.haskell.org
Sat Nov 18 19:44:39 UTC 2017
Repository : ssh://git@git.haskell.org/ghc
On branch : master
Link : http://ghc.haskell.org/trac/ghc/changeset/b8e324a4ff7d04d55dc8d8d8b9e441225c9dd4f6/ghc
>---------------------------------------------------------------
commit b8e324a4ff7d04d55dc8d8d8b9e441225c9dd4f6
Author: Andrew Martin <andrew.thaddeus at gmail.com>
Date: Sat Nov 18 14:43:20 2017 -0500
base: Make documentation of atomically more accurate
>---------------------------------------------------------------
b8e324a4ff7d04d55dc8d8d8b9e441225c9dd4f6
libraries/base/GHC/Conc/Sync.hs | 23 ++++++++++++++++-------
1 file changed, 16 insertions(+), 7 deletions(-)
diff --git a/libraries/base/GHC/Conc/Sync.hs b/libraries/base/GHC/Conc/Sync.hs
index 06da99b..de77792 100644
--- a/libraries/base/GHC/Conc/Sync.hs
+++ b/libraries/base/GHC/Conc/Sync.hs
@@ -716,13 +716,22 @@ unsafeIOToSTM (IO m) = STM m
-- | Perform a series of STM actions atomically.
--
--- You cannot use 'atomically' inside an 'unsafePerformIO' or 'unsafeInterleaveIO'.
--- Any attempt to do so will result in a runtime error. (Reason: allowing
--- this would effectively allow a transaction inside a transaction, depending
--- on exactly when the thunk is evaluated.)
---
--- However, see 'newTVarIO', which can be called inside 'unsafePerformIO',
--- and which allows top-level TVars to be allocated.
+-- Using 'atomically' inside an 'unsafePerformIO' or 'unsafeInterleaveIO'
+-- subverts some of guarantees that STM provides. It makes it possible to
+-- run a transaction inside of another transaction, depending on when the
+-- thunk is evaluated. If a nested transaction is attempted, an exception
+-- is thrown by the runtime. It is possible to safely use 'atomically' inside
+-- 'unsafePerformIO' or 'unsafeInterleaveIO', but the typechecker does not
+-- rule out programs that may attempt nested transactions, meaning that
+-- the programmer must take special care to prevent these.
+--
+-- However, there are functions for creating transactional variables that
+-- can always be safely called in 'unsafePerformIO'. See: 'newTVarIO',
+-- 'newTChanIO', 'newBroadcastTChanIO', 'newTQueueIO', 'newTBQueueIO',
+-- and 'newTMVarIO'.
+--
+-- Using 'unsafePerformIO' inside of 'atomically' is also dangerous but for
+-- different reasons. See 'unsafeIOToSTM' for more on this.
atomically :: STM a -> IO a
atomically (STM m) = IO (\s -> (atomically# m) s )
More information about the ghc-commits
mailing list