Concurrent Haskell

Concurrent Haskell

Concurrent Haskell extends[1] Haskell 98 with explicit concurrency. The two main concepts underpinning Concurrent Haskell are:

  • A primitive type MVar α implementing a bounded/single-place asynchronous channel, which is either empty or holds a value of type α.
  • The ability to spawn a concurrent thread via the forkIO primitive.

Built atop this is a collection of useful concurrency and synchronisation abstractions[2] such as unbounded channels, semaphores and sample variables.

Default Haskell threads have very low overheads: creation, context-switching and scheduling are all internal to the Haskell runtime. These Haskell-level threads are mapped onto a configurable number of OS-level threads, usually one per processor core.

Software Transactional Memory

The recently introduced Software Transactional Memory (STM)[3] extension[4] to the Glasgow Haskell Compiler reuses the process forking primitives of Concurrent Haskell. STM however:

  • eschews MVars in favour of TVars.
  • introduces the retry and orElse primitives, allowing alternative atomic actions to be composed together.

STM monad

The STM monad is an implementation of Software Transactional Memory in Haskell. It is implemented in the GHC compiler, and allows for mutable variables to be modified in transactions. An example of a transaction might be in a banking application. One function that would be needed would be a transfer function, which takes money from one account, and puts it into another account. In the IO monad, this might look like this:

type Account = IORef Integer
 
transfer :: Integer -> Account -> Account -> IO ()
transfer amount from to = do
    fromVal <- readIORef from
    toVal   <- readIORef to
    writeIORef from (fromVal - amount)
    writeIORef to (toVal + amount)

This might work some of the time, but causes problems in concurrent situations where multiple transfers might be taking place on the same account at the same time. If there were two transfers transferring money from account 'from', and both calls to transfer ran the

fromVal <- readIORef from

line before either of them had written their new values, it is possible that money would be put into the other two accounts, with only one of the amounts being transferred being removed from account 'from', thus creating a race condition. This would leave the state of the banking application in an inconsistent state.

A traditional solution to such a problem is locking. For instance, one could place locks around modifications to an account to ensure that credits and debits occur atomically. In Haskell, locking is accomplished with MVars:

type Account = MVar Integer
 
credit :: Integer -> Account -> IO ()
credit amount account = do
    current <- takeMVar account
    putMVar account (current + amount)
 
debit :: Integer -> Account -> IO ()
debit amount account = do
    current <- takeMVar account
    putMVar account (current - amount)

Using such procedures will ensure that money will never be lost or gained due to improper interleaving of reads and writes to any individual account. However, if one tries to compose them together to create a procedure like transfer:

transfer :: Integer -> Account -> Account -> IO ()
transfer amount from to = do
    debit amount from
    credit amount to

A race condition still exists: the first account may be debited, then execution of the thread may be suspended, leaving the accounts as a whole in an inconsistent state. Thus, additional locks must be added to ensure correctness of composite operations, and in the worst case, one might need to simply lock all accounts regardless of how many are used in a given operation.

To avoid this, we can use the STM monad, which allows us to write atomic transactions. This means that all operations inside the transaction fully complete, without any other threads modifying the variables that our transaction is using, or it fails, and the state is rolled back to where it was before the transaction was begun. In short, atomic transactions either complete fully, or it is as if they were never run at all. The lock-based code above translates in a relatively straight forward way:

type Account = TVar Integer
 
credit :: Integer -> Account -> STM ()
credit amount account = do
    current <- readTVar account
    writeTVar account (current + amount)
 
debit :: Integer -> Account -> STM ()
debit amount account = do
    current <- readTVar account
    writeTVar account (current - amount)
 
transfer :: Integer -> Account -> Account -> STM ()
transfer amount from to = do
    debit amount from
    credit amount to

The return types of STM () may be taken to indicate that we are composing scripts for transactions. When the time comes to actually execute such a transaction, a function atomically :: STM a -> IO a is used. The above implementation will make sure that no other transactions interfere with the variables it is using (from and to) while it is executing, allowing the developer to be sure that race conditions like that above are not encountered. More improvements can be made to make sure that some other "Business Logic" is maintained in the system, i.e. that the transaction should not try to take money from an account until there is enough money in it:

transfer :: Integer -> Account -> Account -> STM ()
transfer amount from to = do
    fromVal <- readTVar from
    if (fromVal - amount) >= 0
        then do
               debit amount from
               credit amount to
        else retry

Here the retry function has been used, which will roll back a transaction, and try it again. Retrying in STM is smart in that it won't try and run the transaction again until one of the variables it references during the transaction has been modified by some other transactional code. This makes the STM monad quite efficient.

An example program using the transfer function might look like this:

module Main where
 
import Control.Concurrent (forkIO)
import Control.Concurrent.STM
import Control.Monad (forever)
import System.Exit (exitSuccess)
 
type Account = TVar Integer
 
main = do
    bob <- newAccount 10000
    jill <- newAccount 4000
    repeatIO 2000 $ forkIO $ atomically $ transfer 1 bob jill
    forever $ do
        bobBalance <- atomically $ readTVar bob
        jillBalance <- atomically $ readTVar jill
        putStrLn ("Bob's balance: " ++ show bobBalance ++ ", Jill's balance: " ++ show jillBalance)
        if bobBalance == 8000
            then exitSuccess
            else putStrLn "Trying again."
 
repeatIO :: Integer -> IO a -> IO a
repeatIO 1 m = m
repeatIO n m = m >> repeatIO (n - 1) m
 
newAccount :: Integer -> IO Account
newAccount amount = newTVarIO amount
 
transfer :: Integer -> Account -> Account -> STM ()
transfer amount from to = do
    fromVal <- readTVar from
    if (fromVal - amount) >= 0
        then do
               debit amount from
               credit amount to
        else retry
 
credit :: Integer -> Account -> STM ()
credit amount account = do
    current <- readTVar account
    writeTVar account (current + amount)
 
debit :: Integer -> Account -> STM ()
debit amount account = do
    current <- readTVar account
    writeTVar account (current - amount)

Which should print out "Bill's balance: 8000, Jill's balance: 6000". Here the atomically function has been used to run STM actions in the IO monad.

References

  1. ^ Simon Peyton Jones, Andrew Gordon, and Sigbjorn Finne. Concurrent Haskell. ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (PoPL). 1996. (Some sections are out of date with respect to the current implementation.)
  2. ^ The Haskell Hierarchical Libraries, Control.Concurrent
  3. ^ Tim Harris, Simon Marlow, Simon Peyton Jones, and Maurice Herlihy. Composable Memory Transactions. ACM Symposium on Principles and Practice of Parallel Programming 2005 (PPoPP'05). 2005.
  4. ^ Control.Concurrent.STM



Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Concurrent computing — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent c …   Wikipedia

  • Haskell (programming language) — Haskell Paradigm(s) functional, lazy/non strict, modular Appeared in 1990 Designed by Simon Peyton Jones, Lennart Aug …   Wikipedia

  • Haskell — Auteur le comité Haskell Développeurs la communauté Haskell …   Wikipédia en Français

  • O'Haskell — is an object oriented, concurrent extension of the functional programming language Haskell. [ [http://www.cs.chalmers.se/ nordland/ohaskell/ O Haskell ] ] It was developed at Oregon Graduate Institute and Chalmers University of Technology.… …   Wikipedia

  • Software transactional memory — In computer science, software transactional memory (STM) is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing. It is an alternative to lock based synchronization. A… …   Wikipedia

  • Programmation concurrente — La programmation concurrente est un style de programmation tenant compte, dans un programme, de l existence de plusieurs piles sémantiques. Ces piles peuvent être appelées threads, processus ou tâches. Elles sont matérialisées en machine par une… …   Wikipédia en Français

  • Memoire transactionnelle logicielle — Mémoire transactionnelle logicielle En informatique, la mémoire transactionnelle logicielle, en anglais software transactional memory (STM), est un mécanisme de contrôle de concurrence analogue aux transactions de base de données pour contrôler l …   Wikipédia en Français

  • Mémoire Transactionnelle Logicielle — En informatique, la mémoire transactionnelle logicielle, en anglais software transactional memory (STM), est un mécanisme de contrôle de concurrence analogue aux transactions de base de données pour contrôler l accès à la mémoire partagée dans la …   Wikipédia en Français

  • Mémoire transactionnelle logicielle — En informatique, la mémoire transactionnelle logicielle, en anglais software transactional memory (STM), est un mécanisme de contrôle de concurrence analogue aux transactions de base de données pour contrôler l accès à la mémoire partagée dans la …   Wikipédia en Français

  • Id (programming language) — Id is a general purpose parallel programming language, developed by Arvind and Nikhil, at MIT, in the late 1970 and throughout the 1980s. The major subset of Id is a purely functional programming language with non strict semantics. Features… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”