Transactional Memory for Smalltalk

26
Transactional Memory for Smalltalk Software Composition Group University of Bern Lukas Renggli Oscar Nierstrasz

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 of Transactional Memory for Smalltalk

Page 1: Transactional Memory for Smalltalk

Transactional Memoryfor Smalltalk

Software Composition GroupUniversity of Bern

Lukas RenggliOscar Nierstrasz

Page 2: Transactional Memory for Smalltalk

Concurrent Programmingin Smalltalk

Semaphore forMutualExclusion

RecursionLock new

Mutex new

SharedQueue new

Page 3: Transactional Memory for Smalltalk

Problems

Deadlocks

Starvation

Priority Inversion

Complexity

Page 4: Transactional Memory for Smalltalk

SoftwareTransactional Memory

Page 5: Transactional Memory for Smalltalk

Programmingwith Transactions

Page 6: Transactional Memory for Smalltalk

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

Transactional

tree := BTree new.

" writing "

[ tree at: #a put: 1 ] atomic.

" reading "

tree at: #a.

Page 8: Transactional Memory for Smalltalk

Inside Transactions

Page 9: Transactional Memory for Smalltalk

Our Approach

Full implementation in Smalltalk

Lazy code transformation

Method annotations

Context dependent code execution

Page 10: Transactional Memory for Smalltalk

Static Model

Page 11: Transactional Memory for Smalltalk

selector

parseTree

CompiledMethod

selector*

methods

/selector

/parseTree

AtomicMethod

atomicMethod

1

method

1

name

superclass

subclasses

instanceVariables

Class

Page 12: Transactional Memory for Smalltalk

Code Transformation

Instance Variables

Variable Bindings

Message Sends

Page 13: Transactional Memory for Smalltalk

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

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

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

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

Annotate methodsthat need special treatment

Infrastructural code

Exception handling

Execution contexts

Many primitives

Variable sized objects

Page 18: Transactional Memory for Smalltalk

Dynamic Model

Page 19: Transactional Memory for Smalltalk

applyhasChangedhasConflict

Change

object

*changes

Process0..1

currentTransaction

do: aBlockretry: aBlockcheckpointabort: anObject

escapeContext

Transaction

previousCopyworkingCopy

ObjectChange

applyBlockconflictTestBlock

CustomChange

*

Page 20: Transactional Memory for Smalltalk

Validation

Page 21: Transactional Memory for Smalltalk

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×

Page 22: Transactional Memory for Smalltalk

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

Future Work

Page 24: Transactional Memory for Smalltalk

Implement within a multi-core environment

Page 25: Transactional Memory for Smalltalk

Improve speed

Page 26: Transactional Memory for Smalltalk

Applications

Concurrency Contol

Source Code Loading

Context Oriented Programming