SlideShare a Scribd company logo
STM
Software Transactional Memory
in Frege - a purely functional JVM language
Quickie
OOP 2016
Dierk König
Canoo
mittie
Dierk König
Canoo
mittie
Silly Clock
10010
Silly Clock
10010 every ms
Silly Clock
10010 every ms
on overflow
Silly Clock
10010 every ms
on overflow
every so often
Transactional Variables
10010
CounterCounter
millissecs
TVar Int
Create new TVar
type Counter = TVar Int
newCounter :: STM Counter
newCounter = TVar.new 0
STM action type
Read & Write TVar
tick :: Counter -> STM ()
tick counter = do
value <- counter.read
counter.write (value + 1)
STM action (no IO)
Check transaction invariant
maxTick :: Counter -> Int -> STM ()
maxTick counter max = do
tick counter
value <- counter.read
check (value <= max)
Check transaction invariant
maxTick :: Counter -> Int -> STM ()
maxTick counter max = do
tick counter
value <- counter.read
check (value <= max)
Composition !
Consistent update
onOverflow :: Counter->Counter->Int->STM ()
onOverflow counter overflowCounter max = do
value <- counter.read
check (value == max)
tick overflowCounter
reset counter
Composition !
Type of reset enforced
reset :: Counter -> STM ()
reset counter = counter.write 0
must be STM action
Atomically
report :: Counter -> Counter -> IO ()
report millis secs = do
(millisValue, secsValue) <- atomically $ do
a <- millis.read
b <- secs.read
return (a, b)
println $ show secsValue ++ " " ++ show millisValue
Transaction demarcation

calls STM action inside IO action
Starting the threads
main _ = do
millis <- atomically newCounter
secs <- atomically newCounter
milliOverflow = 1000
runTicker = maxTick millis milliOverflow
runSec = onOverflow millis secs milliOverflow
forkOS $ forever (atomically runTicker >> Thread.sleep 1)
forkOS $ forever (atomically runSec )
forever (report millis secs >> Thread.sleep 100)
STM
Works like compare and swap but for all
TVars inside an atomically function

plus invariants (check)

plus alternatives (orElse)

plus proper exception handling

No locks -> No deadlocks
First one wins
isolated work commit
fail
retry
STM
Replaces all your locks just like GC
replaces manual memory management

Makes compositional and modular code

Is optimistic and thus works best in low
contention scenarios

Is not a panacea (sorry).
Classic: Account transfer
deposit :: Account -> Int -> STM ()
deposit account amount = do
balance <- account.read
account.write (balance + amount)
withdraw :: Account -> Int -> STM ()
withdraw account amount =
deposit account (- amount) -- composed
limitedWithdraw :: Account -> Int -> STM ()
limitedWithdraw account amount = do
withdraw account amount -- composed
balance <- account.read
check (balance >= 0)
transfer :: Account -> Account -> Int -> STM ()
transfer from to amount = do
limitedWithdraw from amount -- composed
deposit to amount -- composed
Classic: Ant colony
Ants

Food
Pheromones
Evaporation
Reporter
Quick into to Software Transactional Memory in Frege
STM relies upon
STM relies upon
No access to TVars outside transactions

No side effects inside transactions
STM relies upon
No access to TVars outside transactions

No side effects inside transactions
TYPE SYSTEM (pure FP)

Developer discipline (everybody else)
STM relies upon
No access to TVars outside transactions

No side effects inside transactions
TYPE SYSTEM (pure FP)

Developer discipline (everybody else)
www.frege-lang.org 

is the only option on the JVM
Dierk König
canoo
mittie
Credits
Volker Steiss
master thesis
Simon Peyton-Jones
Beautiful concurrency
Dierk König
canoo
mittie
Please give feedback!
Credits
Volker Steiss
master thesis
Simon Peyton-Jones
Beautiful concurrency

More Related Content

PDF
Software Transactional Memory (STM) in Frege
PPTX
More Perl Basics
PPTX
Lecture 3
PPTX
Database Recovery
PPTX
Lecture 4
PDF
Inteligencia artificial 12
PPT
C++ control loops
PPT
Java Programming: Loops
Software Transactional Memory (STM) in Frege
More Perl Basics
Lecture 3
Database Recovery
Lecture 4
Inteligencia artificial 12
C++ control loops
Java Programming: Loops

Similar to Quick into to Software Transactional Memory in Frege (20)

PPTX
Pellucid stm
PDF
Implementing STM in Java
PPTX
Crafting a Ready-to-Go STM
PDF
Synchronizing Parallel Tasks Using STM
PPT
Deuce STM - CMP'09
PDF
Hybrid STM/HTM for Nested Transactions on OpenJDK
PPTX
opt-mem-trx
PDF
Research Review Slides
PPTX
Stm talk 2016-03-09
PPTX
[COSCUP 2022] 腳踏多條船-利用 Coroutine在 Software Transactional Memory上進行動態排程
ODP
Software Transactioneel Geheugen
PPT
Prelim Slides
ODP
Paractical Solutions for Multicore Programming
PDF
Designing for Concurrency
PPTX
STM in Haskell
PPT
Clojure concurrency
PDF
Adaptive Thread Scheduling Techniques for Improving Scalability of Software T...
PPT
Os9
PPTX
Transactional Memory
Pellucid stm
Implementing STM in Java
Crafting a Ready-to-Go STM
Synchronizing Parallel Tasks Using STM
Deuce STM - CMP'09
Hybrid STM/HTM for Nested Transactions on OpenJDK
opt-mem-trx
Research Review Slides
Stm talk 2016-03-09
[COSCUP 2022] 腳踏多條船-利用 Coroutine在 Software Transactional Memory上進行動態排程
Software Transactioneel Geheugen
Prelim Slides
Paractical Solutions for Multicore Programming
Designing for Concurrency
STM in Haskell
Clojure concurrency
Adaptive Thread Scheduling Techniques for Improving Scalability of Software T...
Os9
Transactional Memory
Ad

More from Dierk König (11)

PDF
OpenDolphin with GroovyFX Workshop at GreachConf, Madrid
PDF
Greach, GroovyFx Workshop
PDF
Monads from Definition
PDF
FregeFX - JavaFX with Frege, a Haskell for the JVM
PDF
Frege Tutorial at JavaOne 2015
PDF
Frege - consequently functional programming for the JVM
PDF
FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss)
PDF
FregeDay: Design and Implementation of the language (Ingo Wechsung)
PDF
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
PDF
UI Engineer - the missing profession, devoxx 2013
PDF
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
OpenDolphin with GroovyFX Workshop at GreachConf, Madrid
Greach, GroovyFx Workshop
Monads from Definition
FregeFX - JavaFX with Frege, a Haskell for the JVM
Frege Tutorial at JavaOne 2015
Frege - consequently functional programming for the JVM
FregeDay: Parallelism in Frege compared to GHC Haskell (Volker Steiss)
FregeDay: Design and Implementation of the language (Ingo Wechsung)
FregeDay: Roadmap for resolving differences between Haskell and Frege (Ingo W...
UI Engineer - the missing profession, devoxx 2013
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
Ad

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Spectral efficient network and resource selection model in 5G networks
PPT
Teaching material agriculture food technology
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Encapsulation_ Review paper, used for researhc scholars
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
KodekX | Application Modernization Development
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
cuic standard and advanced reporting.pdf
Programs and apps: productivity, graphics, security and other tools
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Chapter 3 Spatial Domain Image Processing.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectral efficient network and resource selection model in 5G networks
Teaching material agriculture food technology
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
20250228 LYD VKU AI Blended-Learning.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
cuic standard and advanced reporting.pdf

Quick into to Software Transactional Memory in Frege