linguistic symbiosis between actors and threads

49
Linguistic Symbiosis between Actors and Threads Tom Van Cutsem Stijn Mostinckx Wolfgang De Meuter Programming Technology Lab Vrije Universiteit Brussel Brussels, Belgium International Conference on Dynamic Languages, August 27th 2007, Lugano

Upload: esug

Post on 15-Jan-2015

446 views

Category:

Technology


1 download

DESCRIPTION

Linguistic Symbiosis between Actors and Threads. Tom Van Cutsem, Stijn Mostinckx, Wolfgang De Meuter. ESUG 2007, Lugano

TRANSCRIPT

Page 1: Linguistic Symbiosis between Actors and Threads

Linguistic Symbiosis between Actors and Threads

Tom Van Cutsem Stijn Mostinckx Wolfgang De Meuter

Programming Technology LabVrije Universiteit Brussel

Brussels, Belgium

International Conference on Dynamic Languages, August 27th 2007, Lugano

Page 2: Linguistic Symbiosis between Actors and Threads

Overview

• AmbientTalk: OO DSL for mobile ad hoc networks

• Pure event-driven con-currency model (actors [Agha86])

• How to do a safe linguistic symbiosis between actors and threads?

2

Page 3: Linguistic Symbiosis between Actors and Threads

Actors vs. Threads

3

actor: { def obj := object: { def m() { ... } }

def button := Button.new(“Click Me”); button.addActionListener(object: { def actionPerformed(actionEvent) { obj.m(); } })

obj.m();}

Page 4: Linguistic Symbiosis between Actors and Threads

Actors vs. Threads

3

actor: { def obj := object: { def m() { ... } }

def button := Button.new(“Click Me”); button.addActionListener(object: { def actionPerformed(actionEvent) { obj.m(); } })

obj.m();}

Page 5: Linguistic Symbiosis between Actors and Threads

Event Loop Concurrency

• Events are executed serially

• Event notification is strictly asynchronous

• Event loops should have no shared state

4

Event Event Queue

Event Loop Event Handler

Page 6: Linguistic Symbiosis between Actors and Threads

Event loop concurrency

5

Actor

Message queue Event loop

Based on E programming language [Miller05]

Page 7: Linguistic Symbiosis between Actors and Threads

Event loop concurrency

5

Actor

Message queue Event loop

‘local’ object

Based on E programming language [Miller05]

Page 8: Linguistic Symbiosis between Actors and Threads

Event loop concurrency

5

Actor

Message queue Event loop

‘local’ object

obj

obj.m()

Based on E programming language [Miller05]

Page 9: Linguistic Symbiosis between Actors and Threads

Event loop concurrency

5

Actor

Message queue Event loop

‘local’ object ‘remote’ object

Based on E programming language [Miller05]

Page 10: Linguistic Symbiosis between Actors and Threads

Event loop concurrency

5

Actor

Message queue Event loop

‘local’ object ‘remote’ object

obj

obj<-m()

Based on E programming language [Miller05]

Page 11: Linguistic Symbiosis between Actors and Threads

Event loop concurrency

5

Actor

Message queue Event loop

‘local’ object ‘remote’ object

Actors cannot cause deadlock

No race conditions on objects

obj

obj<-m()

Based on E programming language [Miller05]

Page 12: Linguistic Symbiosis between Actors and Threads

AmbientTalk/Java

• AmbientTalk is implemented in Java

• Data mapping: cfr. JRuby, Jython, JScheme, LuaJava, JPiccola, ...

• Tight integration at the syntactic level

6

Based on Inter-language Reflection [Gybels et al 05]

def Button := jlobby.java.awt.Button;def button := Button.new(“Click Me”);button.addActionListener(object: { def actionPerformed(actionEvent) { ... }});button.setVisible(true);

Page 13: Linguistic Symbiosis between Actors and Threads

Actor/Thread Mapping

7

Page 14: Linguistic Symbiosis between Actors and Threads

Actor/Thread Mapping

7

?

Page 15: Linguistic Symbiosis between Actors and Threads

Actors as Threads

8

Actor

obj

def obj := object: { ... };

aJavaCollection.add(obj);

aJavaCollection

Page 16: Linguistic Symbiosis between Actors and Threads

Actors as Threads

8

Actor

obj

def obj := object: { ... };

aJavaCollection.add(obj);

aJavaCollection

add(obj)

Page 17: Linguistic Symbiosis between Actors and Threads

Actors as Threads

8

Actor

obj

def obj := object: { ... };

aJavaCollection.add(obj);

aJavaCollection

add(obj)

Page 18: Linguistic Symbiosis between Actors and Threads

Actors as Threads

8

Actor

obj

def obj := object: { ... };

aJavaCollection.add(obj);

synchronizedCol

add(obj)

Page 19: Linguistic Symbiosis between Actors and Threads

Actors as Threads

9

Actor

obj

def obj := object: { def compareTo(other) { ... }}

aJavaCollection.add(obj);

aJavaCollection

Page 20: Linguistic Symbiosis between Actors and Threads

Actors as Threads

9

Actor

obj

def obj := object: { def compareTo(other) { ... }}

aJavaCollection.add(obj);

aJavaCollection

add(obj)

Page 21: Linguistic Symbiosis between Actors and Threads

Actors as Threads

9

Actor

obj

def obj := object: { def compareTo(other) { ... }}

aJavaCollection.add(obj);

aJavaCollection

add(obj)

Page 22: Linguistic Symbiosis between Actors and Threads

Actors as Threads

9

Actor

obj

def obj := object: { def compareTo(other) { ... }}

aJavaCollection.add(obj);

aJavaCollection

add(obj)

compareTo(obj2)

Page 23: Linguistic Symbiosis between Actors and Threads

Actors as Threads

9

Actor

obj

def obj := object: { def compareTo(other) { ... }}

aJavaCollection.add(obj);

aJavaCollection

add(obj)

Page 24: Linguistic Symbiosis between Actors and Threads

Threads as Actors

10

def ambientTalkTest := object: { def countTestCases() { ... } def run(result) { ... }}

interface junit.framework.Test { public int countTestCases(); public void run(TestResult r);}

Page 25: Linguistic Symbiosis between Actors and Threads

Threads as Actors

10

def ambientTalkTest := object: { def countTestCases() { ... } def run(result) { ... }}

TestSuite suite = new TestSuite();ATObject atUnitTest = /* load ambienttalk test */;suite.addTest((Test) wrap(atUnitTest, Test.class));suite.addTest(aJavaUnitTest);junit.textuit.TestRunner.run(suite);

interface junit.framework.Test { public int countTestCases(); public void run(TestResult r);}

Page 26: Linguistic Symbiosis between Actors and Threads

Actor

ambientTalkTest

suite

Threads as Actors

10

Page 27: Linguistic Symbiosis between Actors and Threads

Actor

ambientTalkTest

suite

Threads as Actors

10

run(result)

Page 28: Linguistic Symbiosis between Actors and Threads

Actor

ambientTalkTest

suite

Threads as Actors

10

run(result)

Page 29: Linguistic Symbiosis between Actors and Threads

Actor

ambientTalkTest

suite

Threads as Actors

10

Page 30: Linguistic Symbiosis between Actors and Threads

Actor

ambientTalkTest

suite

Threads as Actors

10

wrapper

Page 31: Linguistic Symbiosis between Actors and Threads

Actor

ambientTalkTest

suite

Threads as Actors

10

wrapper

barrier.get()

Page 32: Linguistic Symbiosis between Actors and Threads

Actor

ambientTalkTest

suite

Threads as Actors

10

wrapper

barrier.get()

Page 33: Linguistic Symbiosis between Actors and Threads

Actor

ambientTalkTest

suite

Threads as Actors

10

wrapper

barrier.get()

Page 34: Linguistic Symbiosis between Actors and Threads

Threads as Actors

11

ActionListener l = ...;l.actionPerformed(actionEvent);

def button := Button.new(“Click Me”);button.addActionListener(object: { def actionPerformed(actionEvent) { ... }});

Page 35: Linguistic Symbiosis between Actors and Threads

Actor

buttonListener

button

Threads as Actors

11

ActionListener l = ...;l.actionPerformed(actionEvent);

def button := Button.new(“Click Me”);button.addActionListener(object: { def actionPerformed(actionEvent) { ... }});

Page 36: Linguistic Symbiosis between Actors and Threads

Actor

buttonListener

button

Threads as Actors

11

actionPerformed(ae)

ActionListener l = ...;l.actionPerformed(actionEvent);

def button := Button.new(“Click Me”);button.addActionListener(object: { def actionPerformed(actionEvent) { ... }});

Page 37: Linguistic Symbiosis between Actors and Threads

Actor

buttonListener

button

Threads as Actors

11

ActionListener l = ...;l.actionPerformed(actionEvent);

wrapper

def button := Button.new(“Click Me”);button.addActionListener(object: { def actionPerformed(actionEvent) { ... }});

Page 38: Linguistic Symbiosis between Actors and Threads

Actor

buttonListener

button

Threads as Actors

11

ActionListener l = ...;l.actionPerformed(actionEvent);

wrapper

def button := Button.new(“Click Me”);button.addActionListener(object: { def actionPerformed(actionEvent) { ... }});

Page 39: Linguistic Symbiosis between Actors and Threads

Actor

buttonListener

button

Threads as Actors

11

ActionListener l = ...;l.actionPerformed(actionEvent);

wrapper

def button := Button.new(“Click Me”);button.addActionListener(object: { def actionPerformed(actionEvent) { ... }});

Page 40: Linguistic Symbiosis between Actors and Threads

Actor

buttonListener

button

Threads as Actors

11

ActionListener l = ...;l.actionPerformed(actionEvent);

wrapper

def button := Button.new(“Click Me”);button.addActionListener(object: { def actionPerformed(actionEvent) { ... }});

Page 41: Linguistic Symbiosis between Actors and Threads

Actor

buttonListener

button

Threads as Actors

11

wrapper

interface I extends java.util.EventListener { public void event(...);}

Page 42: Linguistic Symbiosis between Actors and Threads

Summary

12

Page 43: Linguistic Symbiosis between Actors and Threads

Summary

12

collection.add(obj)

Page 44: Linguistic Symbiosis between Actors and Threads

Summary

12

obj.compareTo(obj2)

Page 45: Linguistic Symbiosis between Actors and Threads

Summary

12

Page 46: Linguistic Symbiosis between Actors and Threads

Summary

12

unitTest.run(reporter)

Page 47: Linguistic Symbiosis between Actors and Threads

Summary

12

listener.actionPerformed(ae)

Page 48: Linguistic Symbiosis between Actors and Threads

Experience

• AmbientTalk using Java: AWT and Swing for GUI construction

• Java using AmbientTalk: JEdit plugin for collaborative text editing

• Self/Squeak’s Morphic UI framework in AmbientTalk

13

Page 49: Linguistic Symbiosis between Actors and Threads

Conclusions

• AmbientTalk: object-oriented (distributed) event-driven programming

• Symbiotic Thread/Actor mapping:

• AmbientTalk invocations proceed immediately

• Automatic synchronization of Java invocations

• Support for Java “event notifications” (listeners)

14

http://prog.vub.ac.be/amop