SlideServe
Menu
Préférences
© SlideServe · v2026

Software Transactional Memory

jolie avatar
14 diapositives · 322 Views · Jul 10, 2014

Software Transactional Memory. Steve Severance – Alpha Heavy Industries. Motivation. Innovations in concurrency has not kept pace Still using locks Immutability has helped in Haskell. STM Basics. Transactional Concurrency Atomicity – Results are visible all at once

Software Transactional Memory - slide 1
01 / 14Software Transactional Memory
1 / 17

Télécharger la présentation

Software Transactional Memory
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Software Transactional Memory Steve Severance – Alpha Heavy Industries

  2. Motivation • Innovations in concurrency has not kept pace • Still using locks • Immutability has helped in Haskell

  3. STM Basics • Transactional Concurrency • Atomicity – Results are visible all at once • Isolation – Action unaffected by other threads • Everything you need is in the stm-2.4.2 package

  4. Haskell and STM • Exploits Three Haskell Features: • Purity (Transactions must be pure) • Monads • Immutability (Values are never modified) • Utilizes the Type System • Anything involved in the transaction is marked (TVar, TChan, STM, etc…)

  5. Basic Transaction Pattern • atomically $ do … • atomically evaluates the transaction main :: IO () main = do var <- newTVarIO False atomically $ do val <- readTVarvar if val == False then writeTVarvar True else return ()

  6. Primitives • TVar (Always has a Value) • TMVar(Analogous to Regular MVar) • TChan (Multicast Capabilities) • TQueue (FIFO Queue) • TBQueue (Bounded Queue) • TSem (Semaphore)

  7. Retrying • Use the retry function to retry the transaction • The transaction will only be run when one of the inputs has changed withdrawFunds :: TVar Account -> Money -> STM Money withdrawFundsaccountVar amount = do account <- readTVaraccountVar if (aBalance account) > amount then writeTVaraccountVar account{aBalance = (aBalance account) `subtract` amount} >> return amount else retry

  8. Choices • orElse runs a second option if the first retrys withdrawEvil :: TVar Account -> TChan Account -> Money -> STM Money withdrawEvilaccountVarnsfChan amount = do withdrawFundsaccountVar amount `orElse` withdrawNonSufficientFundsaccountVarnsfChan amount

  9. Using Alternative • You always wanted to use those cool operators • Use <|> instead of orElse

  10. Dealing with Exceptions • Revisiting the Atomic Guarantee • No need for rollback unlike MVar and Chan • STM can throw and catch

  11. Bounded Queues • TBQueue limits its size • Writers will block until space becomes available • Useful for performance and memory management

  12. Being Strict • Use NFData (deepseq) • We internally use special write functions to keep work from leaking across thread boundaries • Transaction Time writeTVar’ :: NFData a => TVar a -> a -> STM () {-# INLINE writeTVar' #-} writeTVar' varval = rnfval `seq` writeTVarvarval

  13. Other Languages • Clojure • Scala • C++ • C#/.Net

  14. References • Composable Memory Transactions • Beautiful Concurrency • STM Retrospective for .Net • Gists: • Sample 1