Flow Java: Declarative Concurrency for Java Frej Drejhammar SICS - Swedish Institute of Computer Science and KTH - Royal Institute of Technology, Stockholm, Sweden Christian Schulte (KTH), Per Brand(SICS), and Seif Haridi (KTH, SICS) Flow Java: Declarative Concurrency for Java – p.1/29 Constraints for Concurrency Constraints describe data structures used for control (in contrast to Prolog) Logic variables as dataflow variables unconstrained suspension constrained resumption automatic synchronization Well established idea entailment resumption [Maher, 87] CCP [Saraswat, 90] Flow Java: Declarative Concurrency for Java – p.2/29 Technology Transfer Automatic synchronization for Java starting point: logic variables Constraint part limited (so far) token equality no structural equality Why here: see where ideas emerging from constraint programming can have impact Flow Java: Declarative Concurrency for Java – p.3/29 Goals Automatic synchronization Flow Java: Declarative Concurrency for Java – p.4/29 Goals Automatic synchronization single assignment variables (logic variables) Flow Java: Declarative Concurrency for Java – p.4/29 Goals Automatic synchronization single assignment variables (logic variables) futures (read only variants) for security Flow Java: Declarative Concurrency for Java – p.4/29 Goals Automatic synchronization single assignment variables (logic variables) futures (read only variants) for security Conservative extension Flow Java: Declarative Concurrency for Java – p.4/29 Goals Automatic synchronization single assignment variables (logic variables) futures (read only variants) for security Conservative extension concurrency model Flow Java: Declarative Concurrency for Java – p.4/29 Goals Automatic synchronization single assignment variables (logic variables) futures (read only variants) for security Conservative extension concurrency model model of object-orientation Flow Java: Declarative Concurrency for Java – p.4/29 Goals Automatic synchronization single assignment variables (logic variables) futures (read only variants) for security Conservative extension concurrency model model of object-orientation parameter passing Flow Java: Declarative Concurrency for Java – p.4/29 Overview Flow Java Single assignment variables Binding Synchronization Futures Aliasing Implementation Runtime Compilation Optimizations Evaluation Related Work Conclusion Flow Java: Declarative Concurrency for Java – p.5/29 Single Assignment Variables single Object s; Introduce s as a single assignment variable (SAV) of type Object Flow Java: Declarative Concurrency for Java – p.6/29 Single Assignment Variables single Object s; Introduce s as a single assignment variable (SAV) of type Object SAV typed Flow Java: Declarative Concurrency for Java – p.6/29 Single Assignment Variables single Object s; Introduce s as a single assignment variable (SAV) of type Object SAV typed place holder for object Flow Java: Declarative Concurrency for Java – p.6/29 Single Assignment Variables single Object s; Introduce s as a single assignment variable (SAV) of type Object SAV typed place holder for object SAV initially unbound Flow Java: Declarative Concurrency for Java – p.6/29 Binding SAV of type t bound to objects of type t (more detail in paper) Flow Java: Declarative Concurrency for Java – p.7/29 Binding SAV of type t bound to objects of type t (more detail in paper) Binding @= Object o = new Object(); s @= o; Flow Java: Declarative Concurrency for Java – p.7/29 Binding SAV of type t bound to objects of type t (more detail in paper) Binding @= Object o = new Object(); s @= o; s equivalent to o in any subsequent computation Flow Java: Declarative Concurrency for Java – p.7/29 Binding SAV of type t bound to objects of type t (more detail in paper) Binding @= Object o = new Object(); s @= o; s equivalent to o in any subsequent computation s bound (determined ) Flow Java: Declarative Concurrency for Java – p.7/29 Binding SAV of type t bound to objects of type t (more detail in paper) Binding @= Object o = new Object(); s @= o; s equivalent to o in any subsequent computation s bound (determined ) Already bound: exception, if bound to different object Flow Java: Declarative Concurrency for Java – p.7/29 Synchronization Accessing unbound SAV suspends executing thread Flow Java: Declarative Concurrency for Java – p.8/29 Synchronization Accessing unbound SAV suspends executing thread field access Flow Java: Declarative Concurrency for Java – p.8/29 Synchronization Accessing unbound SAV suspends executing thread field access method invocation Flow Java: Declarative Concurrency for Java – p.8/29 Synchronization Accessing unbound SAV suspends executing thread field access method invocation Automatic resumption when bound Flow Java: Declarative Concurrency for Java – p.8/29 Restricting SAV to Objects Ease of implementation Flow Java: Declarative Concurrency for Java – p.9/29 Restricting SAV to Objects Ease of implementation Follows Java philosophy Flow Java: Declarative Concurrency for Java – p.9/29 Restricting SAV to Objects Ease of implementation Follows Java philosophy No fundamental restriction Flow Java: Declarative Concurrency for Java – p.9/29 Restricting SAV to Objects Ease of implementation Follows Java philosophy No fundamental restriction int Integer Flow Java: Declarative Concurrency for Java – p.9/29 Example: Spawning a Computation class Spawn implements Runnable { private single Object result; private Spawn(single Object r) { result = r; } public void run() { result @= computation(); } ... single Object r; new Thread(new Spawn(r)).start(); System.out.println(r); } Flow Java: Declarative Concurrency for Java – p.10/29 Flaw in Spawn Thread using r can compromise abstraction: unintentionally / maliciously bind r Flow Java: Declarative Concurrency for Java – p.11/29 Flaw in Spawn Thread using r can compromise abstraction: unintentionally / maliciously bind r Solution: Use futures read-only variant of SAVs Flow Java: Declarative Concurrency for Java – p.11/29 Futures Each SAV x has associated future f, invariants: Flow Java: Declarative Concurrency for Java – p.12/29 Futures Each SAV x has associated future f, invariants: f bound x bound f bound to o x bound to o x of type single t f of type t Flow Java: Declarative Concurrency for Java – p.12/29 Futures Each SAV x has associated future f, invariants: f bound x bound f bound to o x bound to o x of type single t f of type t Synchronization as on SAVs Flow Java: Declarative Concurrency for Java – p.12/29 Futures Each SAV x has associated future f, invariants: f bound x bound f bound to o x bound to o x of type single t f of type t Synchronization as on SAVs Bind a @= b suspends if b is undetermined Flow Java: Declarative Concurrency for Java – p.12/29 Obtaining Futures Explicit type conversion single t t Flow Java: Declarative Concurrency for Java – p.13/29 Obtaining Futures Explicit type conversion single t t Implicit type conversion void m(Object o) ... single Object s; m(s) Flow Java: Declarative Concurrency for Java – p.13/29 Obtaining Futures Explicit type conversion single t t Implicit type conversion void m(Object o) ... single Object s; m(s) Essential for integration with non Flow Java code Flow Java: Declarative Concurrency for Java – p.13/29 Secure Spawn Add method to Spawn public static Object spawn() { single Object r; new Thread(new Spawn(r)).start(); return r; // implicit conversion } Flow Java: Declarative Concurrency for Java – p.14/29 Aliasing A SAV can be aliased to other SAVs Flow Java: Declarative Concurrency for Java – p.15/29 Aliasing A SAV can be aliased to other SAVs While still unbound Flow Java: Declarative Concurrency for Java – p.15/29 Aliasing A SAV can be aliased to other SAVs While still unbound x and y SAV: x @= y aliases x and y Flow Java: Declarative Concurrency for Java – p.15/29 Aliasing A SAV can be aliased to other SAVs While still unbound x and y SAV: x @= y aliases x and y Binding either x or y to o binds both SAVs to o Flow Java: Declarative Concurrency for Java – p.15/29 Aliasing A SAV can be aliased to other SAVs While still unbound x and y SAV: x @= y aliases x and y Binding either x or y to o binds both SAVs to o Associated futures as well Flow Java: Declarative Concurrency for Java – p.15/29 Aliasing A SAV can be aliased to other SAVs While still unbound x and y SAV: x @= y aliases x and y Binding either x or y to o binds both SAVs to o Associated futures as well equality test == extended Flow Java: Declarative Concurrency for Java – p.15/29 Aliasing A SAV can be aliased to other SAVs While still unbound x and y SAV: x @= y aliases x and y Binding either x or y to o binds both SAVs to o Associated futures as well equality test == extended x == y immediately returns true if x and y aliased Flow Java: Declarative Concurrency for Java – p.15/29 Aliasing A SAV can be aliased to other SAVs While still unbound x and y SAV: x @= y aliases x and y Binding either x or y to o binds both SAVs to o Associated futures as well equality test == extended x == y immediately returns true if x and y aliased Otherwise suspends until both are bound Flow Java: Declarative Concurrency for Java – p.15/29 Overview Flow Java Single assignment variables Binding Synchronization Futures Aliasing Implementation Runtime Compilation Optimizations Evaluation Related Work Conclusion Flow Java: Declarative Concurrency for Java – p.16/29 Implementation Based on GCJ Flow Java: Declarative Concurrency for Java – p.17/29 Implementation Based on GCJ Moderate extension lines Flow Java: Declarative Concurrency for Java – p.17/29 Implementation Based on GCJ Moderate extension lines Available from http://www.sics.se/~frej/flow_java Flow Java: Declarative Concurrency for Java – p.17/29 Implementation Based on GCJ Moderate extension lines Available from http://www.sics.se/~frej/flow_java Builds on Java’s framework for synchronized methods Flow Java: Declarative Concurrency for Java – p.17/29 Implementation Based on GCJ Moderate extension lines Available from http://www.sics.se/~frej/flow_java Builds on Java’s framework for synchronized methods SAV / future distinction maintained by compiler, synchronization object at runtime (SO) Flow Java: Declarative Concurrency for Java – p.17/29 Unbound SO o sync. object sync. vtab vptr class rptr method UNB method void fsync() { this = WaitDet(this); jump(f); } Flow Java: Declarative Concurrency for Java – p.18/29 Bound SO o sync. object vtab vptr class rptr method vptr rptr method void f() { field } field object Flow Java: Declarative Concurrency for Java – p.19/29 Runtime System Primitives @=, ==, != Flow Java: Declarative Concurrency for Java – p.20/29 Runtime System Primitives @=, ==, != Suspension uses wait() and notifyAll() of SO Flow Java: Declarative Concurrency for Java – p.20/29 Runtime System Primitives @=, ==, != Suspension uses wait() and notifyAll() of SO Aliasing: chains of SOs Flow Java: Declarative Concurrency for Java – p.20/29 Runtime System Primitives @=, ==, != Suspension uses wait() and notifyAll() of SO Aliasing: chains of SOs Defined ordering: atomic bind/alias deadlock-free suspension/resumption Flow Java: Declarative Concurrency for Java – p.20/29 Compilation SOs initialized when entering scope Flow Java: Declarative Concurrency for Java – p.21/29 Compilation SOs initialized when entering scope Field access wrapped to ensure suspension: WaitDet(a).foo = 1 a.foo = 1 Flow Java: Declarative Concurrency for Java – p.21/29 Compilation SOs initialized when entering scope Field access wrapped to ensure suspension: WaitDet(a).foo = 1 a.foo = 1 Static method calls wrap object reference (the synchronization vtab not used) Flow Java: Declarative Concurrency for Java – p.21/29 Optimizations Path compression in primitives Flow Java: Declarative Concurrency for Java – p.22/29 Optimizations Path compression in primitives this never wrapped Flow Java: Declarative Concurrency for Java – p.22/29 Optimizations Path compression in primitives this never wrapped CSE: WaitDet(), disregarding suspension, is side effect free: WaitDet(o).a + WaitDet(o).b t = WaitDet(o); t.a + t.b Flow Java: Declarative Concurrency for Java – p.22/29 Overview Flow Java Single assignment variables Binding Synchronization Futures Aliasing Implementation Runtime Compilation Optimizations Evaluation Related Work Conclusion Flow Java: Declarative Concurrency for Java – p.23/29 Evaluation Java Grande benchmark suite Flow Java: Declarative Concurrency for Java – p.24/29 Evaluation Java Grande benchmark suite 10% to 40% overhead Flow Java: Declarative Concurrency for Java – p.24/29 Evaluation Java Grande benchmark suite 10% to 40% overhead Pathological cases 75% Flow Java: Declarative Concurrency for Java – p.24/29 Evaluation Java Grande benchmark suite 10% to 40% overhead Pathological cases 75% Synthetic benchmarks comparing Flow Java to Java Flow Java: Declarative Concurrency for Java – p.24/29 Evaluation Java Grande benchmark suite 10% to 40% overhead Pathological cases 75% Synthetic benchmarks comparing Flow Java to Java Spawn 2% slower Flow Java: Declarative Concurrency for Java – p.24/29 Evaluation Java Grande benchmark suite 10% to 40% overhead Pathological cases 75% Synthetic benchmarks comparing Flow Java to Java Spawn 2% slower Producer/Consumer 6% faster Flow Java: Declarative Concurrency for Java – p.24/29 Related work Concurrent Logic/Constraint Programming Main difference Flow Java: Declarative Concurrency for Java – p.25/29 Related work Concurrent Logic/Constraint Programming Main difference no terms/constraints Flow Java: Declarative Concurrency for Java – p.25/29 Related work Concurrent Logic/Constraint Programming Main difference no terms/constraints futures and types Flow Java: Declarative Concurrency for Java – p.25/29 Related work Concurrent Logic/Constraint Programming Main difference no terms/constraints futures and types predefined concurrency model Flow Java: Declarative Concurrency for Java – p.25/29 Related work Concurrent Logic/Constraint Programming Main difference no terms/constraints futures and types predefined concurrency model Close relative: Oz Flow Java: Declarative Concurrency for Java – p.25/29 Related work Concurrent Logic/Constraint Programming Main difference no terms/constraints futures and types predefined concurrency model Close relative: Oz also futures Flow Java: Declarative Concurrency for Java – p.25/29 Related work Concurrent Logic/Constraint Programming Main difference no terms/constraints futures and types predefined concurrency model Close relative: Oz also futures no types: no implicit conversion from SAV to future Flow Java: Declarative Concurrency for Java – p.25/29 Language Extensions Multilisp: [Halstead 1985] futures and thread creation is combined, no single-assignment variables and no aliasing Flow Java: Declarative Concurrency for Java – p.26/29 Language Extensions Multilisp: [Halstead 1985] futures and thread creation is combined, no single-assignment variables and no aliasing Alice: [UdS] SML with single assignment variables (without aliasing) and futures Flow Java: Declarative Concurrency for Java – p.26/29 Language Extensions Multilisp: [Halstead 1985] futures and thread creation is combined, no single-assignment variables and no aliasing Alice: [UdS] SML with single assignment variables (without aliasing) and futures Extended Ada: [Thornley 1994,1995] with single assignment variables, not conservative Flow Java: Declarative Concurrency for Java – p.26/29 Language Extensions Multilisp: [Halstead 1985] futures and thread creation is combined, no single-assignment variables and no aliasing Alice: [UdS] SML with single assignment variables (without aliasing) and futures Extended Ada: [Thornley 1994,1995] with single assignment variables, not conservative CC++: [Chandy, Kesselman, 93] No futures, no aliasing, no thread creation. Call with SAV arguments suspends Flow Java: Declarative Concurrency for Java – p.26/29 Language Extensions Id: [Arvind ea, 1989] I-structures as arrays of dataflow variables Flow Java: Declarative Concurrency for Java – p.27/29 Extensions to Java/C # Decaf: [Sargeant 2000], logic variables as in Flow Java but not futures. Not conservative. Flow Java: Declarative Concurrency for Java – p.28/29 Extensions to Java/C # Decaf: [Sargeant 2000], logic variables as in Flow Java but not futures. Not conservative. Chords for C#: [Benton ea, 2002] Translation into C# without Chords. Flow Java: Declarative Concurrency for Java – p.28/29 Conclusions and Future Work Conservative extension Flow Java: Declarative Concurrency for Java – p.29/29 Conclusions and Future Work Conservative extension Simplifies concurrent programming Flow Java: Declarative Concurrency for Java – p.29/29 Conclusions and Future Work Conservative extension Simplifies concurrent programming Extensions straightforward make few assumptions of object representation Flow Java: Declarative Concurrency for Java – p.29/29 Conclusions and Future Work Conservative extension Simplifies concurrent programming Extensions straightforward make few assumptions of object representation Moderate efficiency penalty Flow Java: Declarative Concurrency for Java – p.29/29 Conclusions and Future Work Conservative extension Simplifies concurrent programming Extensions straightforward make few assumptions of object representation Moderate efficiency penalty First step towards a platform for high-level, efficient, accessible, distributed and parallel programming Flow Java: Declarative Concurrency for Java – p.29/29