summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Gamari <bgamari.foss@gmail.com>2017-02-26 13:44:44 -0500
committerBen Gamari <ben@smart-cactus.org>2017-02-26 14:56:03 -0500
commitad617a3edf832b5368146e0bbf0cf2780d9355e1 (patch)
tree7e074a74390a6468ba6aec9f03bffe12a3910156
parent517ad201ff6ca1ef0d78c22f31de747709fb7c99 (diff)
downloadhaskell-ad617a3edf832b5368146e0bbf0cf2780d9355e1.tar.gz
Bring sanity to openTempFile
Test Plan: Run test of `openTempFile` under `strace` to verify reasonably random filenames Reviewers: austin, hvr, trofi, rwbarton Reviewed By: trofi, rwbarton Subscribers: rwbarton, thomie Differential Revision: https://phabricator.haskell.org/D3188
-rw-r--r--libraries/base/System/IO.hs13
1 files changed, 8 insertions, 5 deletions
diff --git a/libraries/base/System/IO.hs b/libraries/base/System/IO.hs
index 04e976a85b..656f852a19 100644
--- a/libraries/base/System/IO.hs
+++ b/libraries/base/System/IO.hs
@@ -233,6 +233,8 @@ import System.Posix.Types
import GHC.Base
import GHC.List
+import GHC.IORef
+import GHC.Num
import GHC.IO hiding ( bracket, onException )
import GHC.IO.IOMode
import GHC.IO.Handle.FD
@@ -508,15 +510,16 @@ openTempFile' loc tmp_dir template binary mode = findTempName
| last a == pathSeparator = a ++ b
| otherwise = a ++ [pathSeparator] ++ b
--- int rand(void) from <stdlib.h>, limited by RAND_MAX (small value, 32768)
-foreign import capi "stdlib.h rand" c_rand :: IO CInt
+tempCounter :: IORef Int
+tempCounter = unsafePerformIO $ newIORef 0
+{-# NOINLINE tempCounter #-}
-- build large digit-alike number
rand_string :: IO String
rand_string = do
- r1 <- c_rand
- r2 <- c_rand
- return $ show r1 ++ show r2
+ r1 <- c_getpid
+ r2 <- atomicModifyIORef tempCounter (\n -> (n+1, n))
+ return $ show r1 ++ "-" ++ show r2
data OpenNewFileResult
= NewFileCreated CInt