packages feed

acme-missiles 0.1.1 → 0.2

raw patch · 2 files changed

+51/−9 lines, 2 filesdep +stmPVP ok

version bump matches the API change (PVP)

Dependencies added: stm

API changes (from Hackage documentation)

+ Acme.Missiles: launchMissilesSTM :: STM ()
+ Acme.Missiles: withMissilesDo :: IO a -> IO a

Files

Acme/Missiles.hs view
@@ -10,15 +10,57 @@ --     <http://research.microsoft.com/en-us/um/people/simonpj/papers/stm/index.htm#beautiful>  module Acme.Missiles (-    launchMissiles+    launchMissiles,++    -- * Launching missiles in the 'STM' monad+    withMissilesDo,+    launchMissilesSTM, ) where -import Control.Concurrent (forkIO, threadDelay)-import Foreign.Marshal.Error (void)+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 =-    void $ forkIO $ do-        threadDelay 1000000-        putStrLn "Nuclear launch detected."+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 $ takeTMVar missileCommand+            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 = putTMVar missileCommand ()++missileCommand :: TMVar ()+{-# NOINLINE missileCommand #-}+missileCommand = unsafePerformIO newEmptyTMVarIO
acme-missiles.cabal view
@@ -1,5 +1,5 @@ name:                acme-missiles-version:             0.1.1+version:             0.2 synopsis:            Cause serious international side effects. description:     The highly effectful 'launchMissiles' action, as mentioned in /Beautiful concurrency/,@@ -14,5 +14,5 @@  library   exposed-modules:     Acme.Missiles-  build-depends:       base >= 3 && < 5+  build-depends:       base >= 3 && < 5, stm   ghc-options:         -Wall