diff options
Diffstat (limited to 'libraries/base/GHC')
-rw-r--r-- | libraries/base/GHC/Base.lhs | 13 | ||||
-rw-r--r-- | libraries/base/GHC/Event/Manager.hs | 7 | ||||
-rw-r--r-- | libraries/base/GHC/Event/Thread.hs | 11 | ||||
-rw-r--r-- | libraries/base/GHC/Event/TimerManager.hs | 2 | ||||
-rw-r--r-- | libraries/base/GHC/ForeignPtr.hs | 2 | ||||
-rw-r--r-- | libraries/base/GHC/IO/Handle.hs | 1 |
6 files changed, 26 insertions, 10 deletions
diff --git a/libraries/base/GHC/Base.lhs b/libraries/base/GHC/Base.lhs index 70f589729e..d8eb3e690f 100644 --- a/libraries/base/GHC/Base.lhs +++ b/libraries/base/GHC/Base.lhs @@ -505,6 +505,19 @@ when :: (Monad m) => Bool -> m () -> m () {-# SPECIALISE when :: Bool -> Maybe () -> Maybe () #-} when p s = if p then s else return () +-- | Evaluate each action in the sequence from left to right, +-- and collect the results. +sequence :: Monad m => [m a] -> m [a] +{-# INLINE sequence #-} +sequence ms = foldr k (return []) ms + where + k m m' = do { x <- m; xs <- m'; return (x:xs) } + +-- | @'mapM' f@ is equivalent to @'sequence' . 'map' f@. +mapM :: Monad m => (a -> m b) -> [a] -> m [b] +{-# INLINE mapM #-} +mapM f as = sequence (map f as) + -- | Promote a function to a monad. liftM :: (Monad m) => (a1 -> r) -> m a1 -> m r liftM f m1 = do { x1 <- m1; return (f x1) } diff --git a/libraries/base/GHC/Event/Manager.hs b/libraries/base/GHC/Event/Manager.hs index ea49c0d8cb..20413792db 100644 --- a/libraries/base/GHC/Event/Manager.hs +++ b/libraries/base/GHC/Event/Manager.hs @@ -52,8 +52,9 @@ module GHC.Event.Manager import Control.Concurrent.MVar (MVar, newMVar, readMVar, putMVar, tryPutMVar, takeMVar, withMVar) import Control.Exception (onException) -import Control.Monad (forM_, replicateM, void) import Data.Bits ((.&.)) +import Data.Foldable (forM_) +import Data.Functor (void) import Data.IORef (IORef, atomicModifyIORef', mkWeakIORef, newIORef, readIORef, writeIORef) import Data.Maybe (maybe) @@ -61,7 +62,7 @@ import GHC.Arr (Array, (!), listArray) import GHC.Base import GHC.Conc.Signal (runHandlers) import GHC.Conc.Sync (yield) -import GHC.List (filter) +import GHC.List (filter, replicate) import GHC.Num (Num(..)) import GHC.Real (fromIntegral) import GHC.Show (Show(..)) @@ -192,6 +193,8 @@ newWith oneShot be = do registerControlFd mgr (controlReadFd ctrl) evtRead registerControlFd mgr (wakeupReadFd ctrl) evtRead return mgr + where + replicateM n x = sequence (replicate n x) failOnInvalidFile :: String -> Fd -> IO Bool -> IO () failOnInvalidFile loc fd m = do diff --git a/libraries/base/GHC/Event/Thread.hs b/libraries/base/GHC/Event/Thread.hs index 6fe76895ad..482e41f172 100644 --- a/libraries/base/GHC/Event/Thread.hs +++ b/libraries/base/GHC/Event/Thread.hs @@ -16,14 +16,14 @@ module GHC.Event.Thread ) where import Control.Exception (finally, SomeException, toException) -import Control.Monad (forM, forM_, sequence_, zipWithM) +import Data.Foldable (forM_, mapM_, sequence_) import Data.IORef (IORef, newIORef, readIORef, writeIORef) import Data.Tuple (snd) import Foreign.C.Error (eBADF, errnoToIOError) import Foreign.C.Types (CInt(..), CUInt(..)) import Foreign.Ptr (Ptr) import GHC.Base -import GHC.List (zipWith3) +import GHC.List (zipWith, zipWith3) import GHC.Conc.Sync (TVar, ThreadId, ThreadStatus(..), atomically, forkIO, labelThread, modifyMVar_, withMVar, newTVar, sharedCAF, getNumCapabilities, threadCapability, myThreadId, forkOn, @@ -99,15 +99,16 @@ closeFdWith :: (Fd -> IO ()) -- ^ Action that performs the close. closeFdWith close fd = do eventManagerArray <- readIORef eventManager let (low, high) = boundsIOArray eventManagerArray - mgrs <- forM [low..high] $ \i -> do + mgrs <- flip mapM [low..high] $ \i -> do Just (_,!mgr) <- readIOArray eventManagerArray i return mgr mask_ $ do - tables <- forM mgrs $ \mgr -> takeMVar $ M.callbackTableVar mgr fd + tables <- flip mapM mgrs $ \mgr -> takeMVar $ M.callbackTableVar mgr fd cbApps <- zipWithM (\mgr table -> M.closeFd_ mgr table fd) mgrs tables close fd `finally` sequence_ (zipWith3 finish mgrs tables cbApps) where finish mgr table cbApp = putMVar (M.callbackTableVar mgr fd) table >> cbApp + zipWithM f xs ys = sequence (zipWith f xs ys) threadWait :: Event -> Fd -> IO () threadWait evt fd = mask_ $ do @@ -244,7 +245,7 @@ startIOManagerThreads = withMVar ioManagerLock $ \_ -> do eventManagerArray <- readIORef eventManager let (_, high) = boundsIOArray eventManagerArray - forM_ [0..high] (startIOManagerThread eventManagerArray) + mapM_ (startIOManagerThread eventManagerArray) [0..high] writeIORef numEnabledEventManagers (high+1) show_int :: Int -> String diff --git a/libraries/base/GHC/Event/TimerManager.hs b/libraries/base/GHC/Event/TimerManager.hs index bac4685d94..bf6339a4d8 100644 --- a/libraries/base/GHC/Event/TimerManager.hs +++ b/libraries/base/GHC/Event/TimerManager.hs @@ -39,7 +39,7 @@ module GHC.Event.TimerManager -- Imports import Control.Exception (finally) -import Control.Monad (sequence_) +import Data.Foldable (sequence_) import Data.IORef (IORef, atomicModifyIORef', mkWeakIORef, newIORef, readIORef, writeIORef) import GHC.Base diff --git a/libraries/base/GHC/ForeignPtr.hs b/libraries/base/GHC/ForeignPtr.hs index fe7293e41e..7943ef44d6 100644 --- a/libraries/base/GHC/ForeignPtr.hs +++ b/libraries/base/GHC/ForeignPtr.hs @@ -44,8 +44,8 @@ module GHC.ForeignPtr finalizeForeignPtr ) where -import Control.Monad ( sequence_ ) import Foreign.Storable +import Data.Foldable ( sequence_ ) import Data.Typeable import GHC.Show diff --git a/libraries/base/GHC/IO/Handle.hs b/libraries/base/GHC/IO/Handle.hs index 776618121f..5a8d570642 100644 --- a/libraries/base/GHC/IO/Handle.hs +++ b/libraries/base/GHC/IO/Handle.hs @@ -68,7 +68,6 @@ import GHC.Num import GHC.Real import Data.Maybe import Data.Typeable -import Control.Monad ( mapM ) -- --------------------------------------------------------------------------- -- Closing a handle |