[commit: packages/random] master: Use atomicModifyIORef' (strict) (GHC #4218) (8a4dedd)
git at git.haskell.org
git at git.haskell.org
Thu Mar 19 15:44:56 UTC 2015
Repository : ssh://git@git.haskell.org/random
On branch : master
Link : http://git.haskell.org/packages/random.git/commitdiff/8a4dedd12e26cacacd971d1334be95f1e3860515
>---------------------------------------------------------------
commit 8a4dedd12e26cacacd971d1334be95f1e3860515
Author: Thomas Miedema <thomasmiedema at gmail.com>
Date: Thu Jul 10 21:01:44 2014 +0200
Use atomicModifyIORef' (strict) (GHC #4218)
>---------------------------------------------------------------
8a4dedd12e26cacacd971d1334be95f1e3860515
System/Random.hs | 4 ++--
random.cabal | 7 +++++++
tests/TestRandomIOs.hs | 20 ++++++++++++++++++++
3 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/System/Random.hs b/System/Random.hs
index 665dd78..7522499 100644
--- a/System/Random.hs
+++ b/System/Random.hs
@@ -553,7 +553,7 @@ theStdGen = unsafePerformIO $ do
-- |Applies 'split' to the current global random generator,
-- updates it with one of the results, and returns the other.
newStdGen :: IO StdGen
-newStdGen = atomicModifyIORef theStdGen split
+newStdGen = atomicModifyIORef' theStdGen split
{- |Uses the supplied function to get a value from the current global
random generator, and updates the global generator with the new generator
@@ -566,7 +566,7 @@ between 1 and 6:
-}
getStdRandom :: (StdGen -> (a,StdGen)) -> IO a
-getStdRandom f = atomicModifyIORef theStdGen (swap . f)
+getStdRandom f = atomicModifyIORef' theStdGen (swap . f)
where swap (v,g) = (g,v)
{- $references
diff --git a/random.cabal b/random.cabal
index 511ce29..1039057 100644
--- a/random.cabal
+++ b/random.cabal
@@ -45,3 +45,10 @@ Test-Suite TestRandomRs
ghc-options: -rtsopts -O2
-- TODO. Why does the following not work?
--test-options: +RTS -M1M -RTS
+
+Test-Suite TestRandomIOs
+ type: exitcode-stdio-1.0
+ main-is: TestRandomIOs.hs
+ hs-source-dirs: tests
+ build-depends: base >= 3 && < 5, random
+ ghc-options: -rtsopts -O2
diff --git a/tests/TestRandomIOs.hs b/tests/TestRandomIOs.hs
new file mode 100644
index 0000000..d8a00cc
--- /dev/null
+++ b/tests/TestRandomIOs.hs
@@ -0,0 +1,20 @@
+-- Test for ticket #4218 (TestRandomIOs):
+-- https://ghc.haskell.org/trac/ghc/ticket/4218
+--
+-- Used to fail with:
+--
+-- $ cabal test TestRandomIOs --test-options="+RTS -M1M -RTS"
+-- TestRandomIOs: Heap exhausted;
+
+module Main where
+
+import Control.Monad (replicateM)
+import System.Random (randomIO)
+
+-- Build a list of 5000 random ints in memory (IO Monad is strict), and print
+-- the last one.
+-- Should use less than 1Mb of heap space, or we are generating a list of
+-- unevaluated thunks.
+main = do
+ rs <- replicateM 5000 randomIO :: IO [Int]
+ print $ last rs
More information about the ghc-commits
mailing list