transactional memory for smalltalk
DESCRIPTION
Concurrency control is mostly based on locks and is therefore notoriously difficult to use. Even though some programming languages provide high-level constructs, these add complexity and potentially hard-to-detect bugs to the application. Transactional memory is an attractive mechanism that does not have the drawbacks of locks, however the underlying implementation is often difficult to integrate into an existing language. In this paper we show how we have introduced transactional semantics into Smalltalk by using the reflective facilities of the language. Our approach is based on method annotations, incremental parse tree transformations and an optimistic commit protocol. The implementation does not depend on modifications to the virtual machine and therefor can be changed at the language level. We report on a practical case study, benchmarks and further and on-going work.TRANSCRIPT
![Page 1: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/1.jpg)
Transactional Memoryfor Smalltalk
Software Composition GroupUniversity of Bern
Lukas RenggliOscar Nierstrasz
![Page 2: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/2.jpg)
Concurrent Programmingin Smalltalk
Semaphore forMutualExclusion
RecursionLock new
Mutex new
SharedQueue new
![Page 3: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/3.jpg)
Problems
Deadlocks
Starvation
Priority Inversion
Complexity
![Page 4: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/4.jpg)
SoftwareTransactional Memory
![Page 5: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/5.jpg)
Programmingwith Transactions
![Page 6: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/6.jpg)
Lock Based
tree := BTree new.
lock := Semaphore forMutualExclusion.
" writing "
lock critical: [ tree at: #a put: 1 ].
" reading "
lock critical: [ tree at: #a ].
![Page 7: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/7.jpg)
Transactional
tree := BTree new.
" writing "
[ tree at: #a put: 1 ] atomic.
" reading "
tree at: #a.
![Page 8: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/8.jpg)
Inside Transactions
![Page 9: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/9.jpg)
Our Approach
Full implementation in Smalltalk
Lazy code transformation
Method annotations
Context dependent code execution
![Page 10: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/10.jpg)
Static Model
![Page 11: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/11.jpg)
selector
parseTree
CompiledMethod
selector*
methods
/selector
/parseTree
AtomicMethod
atomicMethod
1
method
1
name
superclass
subclasses
instanceVariables
Class
![Page 12: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/12.jpg)
Code Transformation
Instance Variables
Variable Bindings
Message Sends
![Page 13: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/13.jpg)
Original Source Code
BTree>>at: aKey put: anObject
| leaf |
leaf := root leafForKey: aKey.
leaf insertKey: aKey value: anObject.
root := leaf root.
^ anObject
![Page 14: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/14.jpg)
Transformed Source Code
BTree>>__atomic__at: aKey put: anObject
| leaf |
leaf := (self atomicInstVarAt: 1)
__atomic__leafForKey: aKey.
leaf __atomic__insertKey: aKey value: anObject.
self atomicInstVarAt: 1 put: leaf __atomic__root.
^ anObject
![Page 15: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/15.jpg)
Transformed Source Code
BTree>>__atomic__at: aKey put: anObject
| leaf |
leaf := (self atomicInstVarAt: 1)
__atomic__leafForKey: aKey.
leaf __atomic__insertKey: aKey value: anObject.
self atomicInstVarAt: 1 put: leaf __atomic__root.
^ anObject
![Page 16: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/16.jpg)
Transformed Source Code
BTree>>__atomic__at: aKey put: anObject
| leaf |
leaf := (self atomicInstVarAt: 1)
__atomic__leafForKey: aKey.
leaf __atomic__insertKey: aKey value: anObject.
self atomicInstVarAt: 1 put: leaf __atomic__root.
^ anObject
![Page 17: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/17.jpg)
Annotate methodsthat need special treatment
Infrastructural code
Exception handling
Execution contexts
Many primitives
Variable sized objects
![Page 18: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/18.jpg)
Dynamic Model
![Page 19: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/19.jpg)
applyhasChangedhasConflict
Change
object
*changes
Process0..1
currentTransaction
do: aBlockretry: aBlockcheckpointabort: anObject
escapeContext
Transaction
previousCopyworkingCopy
ObjectChange
applyBlockconflictTestBlock
CustomChange
*
![Page 20: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/20.jpg)
Validation
![Page 21: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/21.jpg)
Speed Comparison
Method Invocation
Special Method Invocation
Instance Variable Read
Instance Variable Write
Indexed Variable Read
Indexed Variable Write
5× 10×
15×
20×
25×
17×
18×
19×
20×
2×
1×
![Page 22: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/22.jpg)
0.0
100.0
200.0
300.0
400.0
500.0
600.0
700.0
800.0
0 10 20 30 40 50 60 70 80 90 100
non-synchronized lock-based transactional
n
t [ms]
Performance of n concurrent edit operations in Pier
1 CPU
2 CPUs
4 CPUs
8 CPUs
![Page 23: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/23.jpg)
Future Work
![Page 24: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/24.jpg)
Implement within a multi-core environment
![Page 25: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/25.jpg)
Improve speed
![Page 26: Transactional Memory for Smalltalk](https://reader033.vdocuments.us/reader033/viewer/2022061210/548f4fc0b479590d2b8b50a9/html5/thumbnails/26.jpg)
Applications
Concurrency Contol
Source Code Loading
Context Oriented Programming