acme-missiles 0.2.1 → 0.3
raw patch · 3 files changed
+60/−56 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Acme.Missiles: launchMissilesSTM :: STM ()
- Acme.Missiles: withMissilesDo :: IO a -> IO a
+ Acme.Missiles.STM: launchMissilesSTM :: STM ()
+ Acme.Missiles.STM: withMissilesDo :: IO a -> IO a
Files
- Acme/Missiles.hs +0/−54
- Acme/Missiles/STM.hs +58/−0
- acme-missiles.cabal +2/−2
Acme/Missiles.hs view
@@ -11,62 +11,8 @@ module Acme.Missiles ( launchMissiles,-- -- * Launching missiles in the 'STM' monad- withMissilesDo,- launchMissilesSTM, ) where -import Control.Concurrent-import Control.Concurrent.STM-import Control.Exception (bracket)-import Control.Monad (forever)-import System.IO.Unsafe (unsafePerformIO)- -- | Cause serious international side effects. launchMissiles :: IO () launchMissiles = putStrLn "Nuclear launch detected."---- | Perform initialization needed to launch missiles in the 'STM' monad.-withMissilesDo :: IO a -> IO a-withMissilesDo action =- bracket (forkIO doLaunching)- killThread- (\_ -> action)- where- doLaunching = forever $ do- atomically $ do- n <- readTVar missileCommand- if n > 0- then writeTVar missileCommand (n - 1)- else retry- launchMissiles---- | Launch missiles within an 'STM' computation. Even if the memory--- transaction is retried, only one salvo of missiles will be launched.------ Example:------ >import Acme.Missiles--- >import Control.Concurrent--- >import Control.Concurrent.STM--- >--- >main :: IO ()--- >main = withMissilesDo $ do--- > xv <- atomically $ newTVar (2 :: Int)--- > yv <- atomically $ newTVar (1 :: Int)--- > atomically $ do--- > x <- readTVar xv--- > y <- readTVar yv--- > if x > y--- > then launchMissilesSTM--- > else return ()--- > threadDelay 100000-launchMissilesSTM :: STM ()-launchMissilesSTM = do- n <- readTVar missileCommand- writeTVar missileCommand $! n + 1--missileCommand :: TVar Integer-{-# NOINLINE missileCommand #-}-missileCommand = unsafePerformIO (newTVarIO 0)
+ Acme/Missiles/STM.hs view
@@ -0,0 +1,58 @@+module Acme.Missiles.STM (+ -- * Launching missiles in the 'STM' monad+ withMissilesDo,+ launchMissilesSTM,+) where++import Acme.Missiles (launchMissiles)++import Control.Concurrent (forkIO, killThread)+import Control.Concurrent.STM (STM, atomically, retry,+ TVar, newTVarIO, readTVar, writeTVar)+import Control.Exception (bracket)+import Control.Monad (forever)+import System.IO.Unsafe (unsafePerformIO)++-- | Perform initialization needed to launch missiles in the 'STM' monad.+withMissilesDo :: IO a -> IO a+withMissilesDo action =+ bracket (forkIO doLaunching)+ killThread+ (\_ -> action)+ where+ doLaunching = forever $ do+ atomically $ do+ n <- readTVar missileCommand+ if n > 0+ then writeTVar missileCommand (n - 1)+ else retry+ launchMissiles++-- | Launch missiles within an 'STM' computation. Even if the memory+-- transaction is retried, only one salvo of missiles will be launched.+--+-- Example:+--+-- >import Acme.Missiles+-- >import Control.Concurrent+-- >import Control.Concurrent.STM+-- >+-- >main :: IO ()+-- >main = withMissilesDo $ do+-- > xv <- atomically $ newTVar (2 :: Int)+-- > yv <- atomically $ newTVar (1 :: Int)+-- > atomically $ do+-- > x <- readTVar xv+-- > y <- readTVar yv+-- > if x > y+-- > then launchMissilesSTM+-- > else return ()+-- > threadDelay 100000+launchMissilesSTM :: STM ()+launchMissilesSTM = do+ n <- readTVar missileCommand+ writeTVar missileCommand $! n + 1++missileCommand :: TVar Integer+{-# NOINLINE missileCommand #-}+missileCommand = unsafePerformIO (newTVarIO 0)
acme-missiles.cabal view
@@ -1,5 +1,5 @@ name: acme-missiles-version: 0.2.1+version: 0.3 synopsis: Cause serious international side effects. description: The highly effectful 'launchMissiles' action, as mentioned in /Beautiful concurrency/,@@ -7,12 +7,12 @@ license: PublicDomain author: Joey Adams maintainer: joeyadams3.14159@gmail.com--- copyright: category: Acme build-type: Simple cabal-version: >=1.8 library exposed-modules: Acme.Missiles+ Acme.Missiles.STM build-depends: base >= 3 && < 5, stm ghc-options: -Wall