45
Attached and Detached Closures in Actors Elias Castegren Dave Clarke, Kiko Fernandez-Reyes, Tobias Wrigstad, Albert Mingkun Yang KTH Royal Institute of Technology Uppsala University

Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Attached and Detached Closures in Actors

Elias CastegrenDave Clarke, Kiko Fernandez-Reyes,

Tobias Wrigstad, Albert Mingkun YangKTH Royal Institute of

TechnologyUppsala University

Page 2: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

What Tobias Said

• The Encore programming language • Object orientation + actors • Guarantees safe sharing of objects between actors

• Handling both concurrency and parallelism in the actor model

• Lessons learned & Open questions

!2

Page 3: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

This Talk

• State-capturing closures in an actor-setting

• Current and future solutions in Encore

• Terminology for discussing closure semantics

!3

Page 4: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

We All Like Actors

!4

Page 5: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

We All Like Actors

!5

Page 6: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Some of Us Like Functional Programming

• Functional programming plays nicely with the actor model • Algebraic data-types • Immutability • Higher-order functions • …

• Examples include Erlang and Elixir

!6

data List a = | Nil | Cons a (List a)

! x : t.x + 42

Page 7: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Some of Us Also Like Object Orientation

• Actor programming is familiar to OO programmers• Actors can be thought of as ”active” objects• Sending Messages ≈ Calling Methods

• OO relies heavily on mutable state and aliasing

⇒Sharing

⇒⇒

!7

Page 8: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Some of Us Also Like Object Orientation

• Actor programming is familiar to OO programmers • Actors can be thought of as ”active” objects • Sending Messages ≈ Calling Methods

• OO relies heavily on mutable state and aliasing

⇒Sharing

⇒Data-races

⇒Loss of actor isolation!

!8

Page 9: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Making Actors and OO Play Nice

• Capability-based languages/systems, type systems • Encore • Pony [Clebsch et al.] • LaCasa (for Scala) [Haller & Loiko] • Joelle [Östlund et al.]

• Relying on delegation of method calls • e.g. far references in AmbientTalk [Dedecker et al.]

• Relying on copying of (passive) objects • e.g. Proactive [Caromel et al.]

!9

Page 10: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Encore Primer/Reminder

!10

active class Actor var count : int val other : Actor

def work() : unit val fut = this.other ! compute()

val result = get fut this.print(result) end

def print(v : Data) : unit this.count += 1 … // Print the value end … end

Actors introduced via classes

Message passing

Synchronisation via futures

Page 11: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Capabilities for Concurrency Control

• Every reference carries a capability (tracked by the type system) • linear — No aliases, transfer semantics • local — Local to its creating actor • read — Read-only reference (no mutable aliases) • active — Actor reference (asynchronous communication) • …

!11

Page 12: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Capabilities for Concurrency Control

• Every reference carries a capability (tracked by the type system) • linear — No aliases, transfer semantics • local — Local to its creating actor • read — Read-only reference (no mutable aliases) • active — Actor reference (asynchronous communication) • …

!11

local class Counter var cnt : int … end

linear class List var first : Node … end

var c = new Counter actor ! foo(c)

var l = new List actor ! bar(consume l)

Can’t share local object

Page 13: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Avoiding Blocking on Futures (Chaining)

!12

active class Actor var count : int val other : Actor

def work() : unit val fut = this.other ! compute()

val result = get fut this.print(result) end

def print(v : Data) : unit this.count += 1 … // Print the value end … end

def work_noblock() : unit val fut = this.other ! compute() fut ~~> fun (v : Data) => this.print(v) end

Who runs this closure?

Induces waiting times

Page 14: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Who Runs a Closure?

!13

def work_noblock() : unit val fut = this.other ! compute() fut ~~> fun (v : Data) => this.print(v) end

F!

Page 15: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Who Runs a Closure?

!14

def work_noblock() : unit val fut = this.other ! compute() fut ~~> fun (v : Data) => this.print(v) end

F!

def work_noblock2() : unit val fut = this.other ! compute() fut ~~> fun (v : Data) => this ! print(v) end

Page 16: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Attached and Detached Closures

• An attached closure is always run by its creating actor • A detached closure can be run by any actor

!15

!

Page 17: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Attached and Detached Closures

• An attached closure is always run by its creating actor • A detached closure can be run by any actor

!15

!

Page 18: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Attached and Detached Closures

• An attached closure is always run by its creating actor • A detached closure can be run by any actor

!15

!

fun (v : Data) => this.print(v)

Page 19: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Closures and Capabilities in Encore

• A closure mirrors the (non-sharable) capabilities it captures

!16

fun (v : Data) => this.print(v) : (Data -> unit)

local

Page 20: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Closures and Capabilities in Encore

• A closure mirrors the (non-sharable) capabilities it captures

!16

fun (v : Data) => this.print(v) : (Data -> unit)local

Page 21: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Closures and Capabilities in Encore

• A closure mirrors the (non-sharable) capabilities it captures

!17

fun (v : Data) => this.print(v) : (Data -> unit)local

fun (v : Data) => this ! print(v) : (Data -> unit)

active

Page 22: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Closures and Capabilities in Encore

• A closure mirrors the (non-sharable) capabilities it captures

!17

fun (v : Data) => this.print(v) : (Data -> unit)local

fun (v : Data) => this ! print(v) : (Data -> unit)active

Page 23: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Closures and Capabilities in Encore

• A closure mirrors the (non-sharable) capabilities it captures

!17

fun (v : Data) => this.print(v) : (Data -> unit)local

fun (v : Data) => this ! print(v) : (Data -> unit)

Page 24: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Labeling Closures as Attached/Detached

!18

def work_noblock() : unit val fut = this.other ! compute() fut ~~> fun (v : Data) => this.print(v) end

def work_noblock2() : unit val fut = this.other ! compute() fut ~~> fun (v : Data) => this ! print(v) end

Captures local state: must be attached!

Only captures safe state: can be detached!

Page 25: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Categorising Closures

• Tetheredness ∈ {attached, detached}

• Execution ∈ {synchronous, asynchronous}• Sharability ∈ {sharable, unsharable}

!19Tetheredness

Execution

Sharability

Page 26: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Categorising Closures

!20

Tetheredness Execution Sharability CommentAttached Synchronous Sharable Explicitly pass back closure to ownerAttached Synchronous Unsharable Current Encore implementationAttached Asynchronous Sharable Encore, when chainingAttached Asynchronous Unsharable Delaying operationsDetached Synchronous Sharable Safe ”normal” closures in EncoreDetached Synchronous Unsharable Not useful?Detached Asynchronous Sharable Task paralellismDetached Asynchronous Unsharable Not useful?

Page 27: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Categorising Closures

!20

Tetheredness Execution Sharability CommentAttached Synchronous Sharable Explicitly pass back closure to ownerAttached Synchronous Unsharable Current Encore implementationAttached Asynchronous Sharable Encore, when chainingAttached Asynchronous Unsharable Delaying operationsDetached Synchronous Sharable Safe ”normal” closures in EncoreDetached Synchronous Unsharable Not useful?Detached Asynchronous Sharable Task paralellismDetached Asynchronous Unsharable Not useful?

fun (v : Data) => v.foo() + 1

fun (v : Data) => this ! print(v)

Page 28: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Categorising Closures

!21

Tetheredness Execution Sharability CommentAttached Synchronous Sharable Explicitly pass back closure to ownerAttached Synchronous Unsharable Current Encore implementationAttached Asynchronous Sharable Encore, when chainingAttached Asynchronous Unsharable Delaying operationsDetached Synchronous Sharable Safe ”normal” closures in EncoreDetached Synchronous Unsharable Not useful?Detached Asynchronous Sharable Task paralellismDetached Asynchronous Unsharable Not useful?

fun (v : Data) => this.print(v)

Page 29: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Categorising Closures

!22

Tetheredness Execution Sharability CommentAttached Synchronous Sharable Explicitly pass back closure to ownerAttached Synchronous Unsharable Current Encore implementationAttached Asynchronous Sharable Encore, when chainingAttached Asynchronous Unsharable Delaying operationsDetached Synchronous Sharable Safe ”normal” closures in EncoreDetached Synchronous Unsharable Not useful?Detached Asynchronous Sharable Task paralellismDetached Asynchronous Unsharable Not useful?

fut ~~> fun (v : Data) => this.print(v)

Page 30: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Categorising Closures

!23

Tetheredness Execution Sharability CommentAttached Synchronous Sharable Explicitly pass back closure to ownerAttached Synchronous Unsharable Current Encore implementationAttached Asynchronous Sharable Encore, when chainingAttached Asynchronous Unsharable Delaying operationsDetached Synchronous Sharable Safe ”normal” closures in EncoreDetached Synchronous Unsharable Not useful?Detached Asynchronous Sharable Task paralellismDetached Asynchronous Unsharable Not useful?

async (x.foo())

Page 31: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Categorising Closures

!24

Tetheredness Execution Sharability CommentAttached Synchronous Sharable Explicitly pass back closure to ownerAttached Synchronous Unsharable Current Encore implementationAttached Asynchronous Sharable Encore, when chainingAttached Asynchronous Unsharable Delaying operationsDetached Synchronous Sharable Safe ”normal” closures in EncoreDetached Synchronous Unsharable Not useful?Detached Asynchronous Sharable Task paralellismDetached Asynchronous Unsharable Not useful?

Page 32: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Categorising Closures

!25

Tetheredness Execution Sharability CommentAttached Synchronous Sharable Explicitly pass back closure to ownerAttached Synchronous Unsharable Current Encore implementationAttached Asynchronous Sharable Encore, when chainingAttached Asynchronous Unsharable Delaying operationsDetached Synchronous Sharable Safe ”normal” closures in EncoreDetached Synchronous Unsharable Not useful?Detached Asynchronous Sharable Task paralellismDetached Asynchronous Unsharable Not useful?

Page 33: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Related Work (closures)

!26

Scala/Akka All closures detached, synchronous and sharable (unsafe)

Pony Synchronous, detached/sharable or attached/unsharable

AmbientTalk All closures attached, far references are asynchronous

ProActive Attached, synchronous and sharable (deep copy)

Erlang No mutable state

ABS No closures (functions passed by name)

Page 34: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Open Questions

• Sharing attached closures

• Deadlocking on attached closures

• Reasoning about timing and scheduling

!27

def run(fn : int -> int) : int fn(42) end

def deadlock(a : Actor) : unit var fut = a ! msg() ~~> fun(v) => … var value = get fut end

Page 35: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Open Questions

• Sharing attached closures

• Deadlocking on attached closures

• Reasoning about timing and scheduling

!27

def run(fn : int -> int) : int fn(42) end

def deadlock(a : Actor) : unit var fut = a ! msg() ~~> fun(v) => … var value = get fut end

active

Page 36: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Open Questions

• Sharing attached closures

• Deadlocking on attached closures

• Reasoning about timing and scheduling

!28

def run(fn : int -> int) : int fn(42) end

def deadlock(a : Actor) : unit var fut = a ! msg() ~~> fun(v) => … var value = get fut end

active

def nondeterministic(a : Actor) : unit val oldCount = this.count var fut = a ! msg()

fut ~~> fun (v : Data) => this.count += 1

if oldCount == this.count then … end end

Page 37: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Conclusion

• Closures capturing state can be made to play nicely with actors • Attached closures must be run by their creating actor • Detached closures can be run by anyone • Some closures must be run asynchronously

• Encore’s existing type system can express both kinds of closures

• More work needed to reason about runtime behaviour

!29

Page 38: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Attached and Detached Closures in Actors

Thank you!

Page 39: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Attached and Detached Closures in Actors

!31

Tetheredness Execution Sharability Comment

Attached Synchronous Sharable Explicitly pass back closure to owner

Attached Synchronous Unsharable Current Encore implementation

Attached Asynchronous Sharable Encore, when chaining

Attached Asynchronous Unsharable Delaying operations

Detached Synchronous Sharable Safe ”normal” closures in Encore

Detached Synchronous Unsharable Not useful?

Detached Asynchronous Sharable Task paralellism

Detached Asynchronous Unsharable Not useful?

Page 40: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Capturing Linear Capabilities

!32

var x = new LinearThing() var f = fun () => x var x1 = f() var x2 = f()

var x = new LinearThing() var f = fun () => x.foo() async f() async f()

var x = new LinearThing() var a = new Actor() var f = fun () => a ! send(x) f() f()

Page 41: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Bestowed References (Far References)

!33

Page 42: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Bestowed References (Far References)

!34

Page 43: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Bestowed References (Far References)

!34

x ! foo()

Page 44: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Bestowed References (Far References)

!34

λ _ . x.foo() x ! foo()

Page 45: Attached and Detached Closures in Actors · • The Encore programming language • Object orientation + actors ... Pony Synchronous, detached/sharable or attached/unsharable AmbientTalk

Await and Continuations

!35

def foo(a : Actor) : unit var fut = a ! compute() var result = await fut this.print(result) end

def foo(a : Actor) : unit var fut = a ! compute() fut ~~> fun (result : Data) => this.print(result) end