Post on 15-Jan-2015
description
transcript
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
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
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();}
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();}
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
Event loop concurrency
5
Actor
Message queue Event loop
Based on E programming language [Miller05]
Event loop concurrency
5
Actor
Message queue Event loop
‘local’ object
Based on E programming language [Miller05]
Event loop concurrency
5
Actor
Message queue Event loop
‘local’ object
obj
obj.m()
Based on E programming language [Miller05]
Event loop concurrency
5
Actor
Message queue Event loop
‘local’ object ‘remote’ object
Based on E programming language [Miller05]
Event loop concurrency
5
Actor
Message queue Event loop
‘local’ object ‘remote’ object
obj
obj<-m()
Based on E programming language [Miller05]
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]
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);
Actor/Thread Mapping
7
Actor/Thread Mapping
7
?
Actors as Threads
8
Actor
obj
def obj := object: { ... };
aJavaCollection.add(obj);
aJavaCollection
Actors as Threads
8
Actor
obj
def obj := object: { ... };
aJavaCollection.add(obj);
aJavaCollection
add(obj)
Actors as Threads
8
Actor
obj
def obj := object: { ... };
aJavaCollection.add(obj);
aJavaCollection
add(obj)
Actors as Threads
8
Actor
obj
def obj := object: { ... };
aJavaCollection.add(obj);
synchronizedCol
add(obj)
Actors as Threads
9
Actor
obj
def obj := object: { def compareTo(other) { ... }}
aJavaCollection.add(obj);
aJavaCollection
Actors as Threads
9
Actor
obj
def obj := object: { def compareTo(other) { ... }}
aJavaCollection.add(obj);
aJavaCollection
add(obj)
Actors as Threads
9
Actor
obj
def obj := object: { def compareTo(other) { ... }}
aJavaCollection.add(obj);
aJavaCollection
add(obj)
Actors as Threads
9
Actor
obj
def obj := object: { def compareTo(other) { ... }}
aJavaCollection.add(obj);
aJavaCollection
add(obj)
compareTo(obj2)
Actors as Threads
9
Actor
obj
def obj := object: { def compareTo(other) { ... }}
aJavaCollection.add(obj);
aJavaCollection
add(obj)
Threads as Actors
10
def ambientTalkTest := object: { def countTestCases() { ... } def run(result) { ... }}
interface junit.framework.Test { public int countTestCases(); public void run(TestResult r);}
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);}
Actor
ambientTalkTest
suite
Threads as Actors
10
Actor
ambientTalkTest
suite
Threads as Actors
10
run(result)
Actor
ambientTalkTest
suite
Threads as Actors
10
run(result)
Actor
ambientTalkTest
suite
Threads as Actors
10
Actor
ambientTalkTest
suite
Threads as Actors
10
wrapper
Actor
ambientTalkTest
suite
Threads as Actors
10
wrapper
barrier.get()
Actor
ambientTalkTest
suite
Threads as Actors
10
wrapper
barrier.get()
Actor
ambientTalkTest
suite
Threads as Actors
10
wrapper
barrier.get()
Threads as Actors
11
ActionListener l = ...;l.actionPerformed(actionEvent);
def button := Button.new(“Click Me”);button.addActionListener(object: { def actionPerformed(actionEvent) { ... }});
Actor
buttonListener
button
Threads as Actors
11
ActionListener l = ...;l.actionPerformed(actionEvent);
def button := Button.new(“Click Me”);button.addActionListener(object: { def actionPerformed(actionEvent) { ... }});
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) { ... }});
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) { ... }});
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) { ... }});
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) { ... }});
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) { ... }});
Actor
buttonListener
button
Threads as Actors
11
wrapper
interface I extends java.util.EventListener { public void event(...);}
Summary
12
Summary
12
collection.add(obj)
Summary
12
obj.compareTo(obj2)
Summary
12
Summary
12
unitTest.run(reporter)
Summary
12
listener.actionPerformed(ae)
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
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