SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
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
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

Weitere ähnliche Inhalte

Ähnlich wie Quick into to Software Transactional Memory in Frege

.Net Enterprise Services and their Implementations
.Net Enterprise Services and their Implementations.Net Enterprise Services and their Implementations
.Net Enterprise Services and their Implementations
Kashif Aleem
 
Open XKE - POC d'une architecture distribuée de calculs financiers par Xavier...
Open XKE - POC d'une architecture distribuée de calculs financiers par Xavier...Open XKE - POC d'une architecture distribuée de calculs financiers par Xavier...
Open XKE - POC d'une architecture distribuée de calculs financiers par Xavier...
Publicis Sapient Engineering
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 

Ähnlich wie Quick into to Software Transactional Memory in Frege (20)

.Net Enterprise Services and their Implementations
.Net Enterprise Services and their Implementations.Net Enterprise Services and their Implementations
.Net Enterprise Services and their Implementations
 
Stm talk 2016-03-09
Stm talk 2016-03-09Stm talk 2016-03-09
Stm talk 2016-03-09
 
Understanding control-flow
Understanding control-flowUnderstanding control-flow
Understanding control-flow
 
The free lunch is over
The free lunch is overThe free lunch is over
The free lunch is over
 
class12_time.ppt
class12_time.pptclass12_time.ppt
class12_time.ppt
 
12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine12 Monkeys Inside JS Engine
12 Monkeys Inside JS Engine
 
Vb.net programs
Vb.net programsVb.net programs
Vb.net programs
 
Scala to assembly
Scala to assemblyScala to assembly
Scala to assembly
 
Starkware: Account Abstraction
Starkware: Account AbstractionStarkware: Account Abstraction
Starkware: Account Abstraction
 
Open XKE - POC d'une architecture distribuée de calculs financiers par Xavier...
Open XKE - POC d'une architecture distribuée de calculs financiers par Xavier...Open XKE - POC d'une architecture distribuée de calculs financiers par Xavier...
Open XKE - POC d'une architecture distribuée de calculs financiers par Xavier...
 
POC d'une architecture distribuee de calculs financiers
POC d'une architecture distribuee de calculs financiersPOC d'une architecture distribuee de calculs financiers
POC d'une architecture distribuee de calculs financiers
 
Pellucid stm
Pellucid stmPellucid stm
Pellucid stm
 
JavaScript 101
JavaScript 101JavaScript 101
JavaScript 101
 
Async Debugging - A Practical Guide to survive !
Async Debugging - A Practical Guide to survive !Async Debugging - A Practical Guide to survive !
Async Debugging - A Practical Guide to survive !
 
Loops (1)
Loops (1)Loops (1)
Loops (1)
 
Hot Code is Faster Code - Addressing JVM Warm-up
Hot Code is Faster Code - Addressing JVM Warm-upHot Code is Faster Code - Addressing JVM Warm-up
Hot Code is Faster Code - Addressing JVM Warm-up
 
Best-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiBest-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbai
 
5.pptx fundamental programing one branch
5.pptx fundamental programing one branch5.pptx fundamental programing one branch
5.pptx fundamental programing one branch
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Timer & Interrupt Atmega16
Timer & Interrupt Atmega16Timer & Interrupt Atmega16
Timer & Interrupt Atmega16
 

Mehr von Dierk König

UI Engineer - the missing profession, devoxx 2013
UI Engineer - the missing profession, devoxx 2013UI Engineer - the missing profession, devoxx 2013
UI Engineer - the missing profession, devoxx 2013
Dierk König
 
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and MobileOpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
OpenDolphin: Enterprise Apps for collaboration on Desktop, Web, and Mobile
Dierk König
 

Mehr von Dierk König (11)

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

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

Quick into to Software Transactional Memory in Frege