A Synchronized Block Join Point for Aspect J

A Synchronized Block Join Point for Aspect. J Chenchen Xi, Bruno Harbulot and John Gurd The University of Manchester Brussels, April 2008

Introduction v The goal of So. C in parallel programming § loop join point v Full control of the thread behavior § Thread: : notify()/notify. All()/wait(). v Distributed Java programs, § sharing data among multiple threads § sharing among clusters of JVMs § sharing among clusters of physical computers. v Replace lock-based synchronization § Transactional Memory 2

Synchronized Block vs. Synchronized Method Synchronized block Synchronized method class Foo { public void bar 1() {. . . } } class Foo { public synchronized void bar 1() {. . . } } class Example { public static void main(String arg[ ]) { Foo objecta = new Foo() ; Foo objectb = new Foo() ; synchronized (objecta) { objecta. bar 1() ; } objectb. bar 1() ; } } class Example { public static void main(String arg[ ]){ Foo objecta = new Foo() ; Foo objectb = new Foo() ; objecta. bar 1() ; objectb. bar 2() ; } } 3

Example of Synchronized Block in Java and Jimple class Example { public static void main(String arg[ ]) { Foo objecta = new Foo() ; Foo objectb = new Foo() ; synchronized(objecta) { objecta. bar 1() ; } objectb. bar 1() ; } } Java arg : = @parameter 0: java. lang. String[]; $r 0 = new Foo; specialinvoke $r 0. <Foo: void <init>()>(); objecta = $r 0; $r 1 = new Foo; specialinvoke $r 1. <Foo: void <init>()>(); objectb = $r 1; entermonitor objecta; label 0: virtualinvoke objecta. <Foo: void bar 1()>(); exitmonitor objecta; label 1: goto label 5; label 2: $r 2 : = @caughtexception; label 3: exitmonitor objecta; label 4: throw $r 2; label 5: return; Jimple 4

Control-flow Graph entermonitor $r 0 0: //in the synchronized block exitmonitor $r 0; 1: goto label 5 a; label 5 ; 5 a: 5: return; nop; 2: $r 1 : = @caughtexception; 3: exitmonitor $r 0; 4: throw $r 1; 5 b: return; 5

Semantics for a Synchronized Block Join Point Jimple Code arg : = @parameter 0: java. lang. String[]; $r 0 = new Foo; Lock based specialinvoke $r 0. <Foo: void <init>()>(); lock (Type t) && withincode (* objecta main(. . )) && args(t) objecta = = $r 0; $r 1 = new unlock (Type t) && withincode $r 1 (* main(. . )) = new Foo; && args(t) specialinvoke $r 1. <Foo: void <init>()>(); objectb = $r 1; Block based entermonitor objecta; synchronized (Type t) && withincode (* main(. . )) && args(t) label 0: synchronized_body( Type t) && withincode (* main(. . )) && args(object) virtualinvoke objecta. <Foo: void bar 1()>(); exitmonitor objecta; label 1: Pointcuts and Advice goto label 5; label 2: void around(Object t): $r 2 : = @caughtexception; synchronize(t) && withincode(* main(. . )) && args(t) { proceed(t) ; } label 3: void around(Object t): exitmonitor objecta; synchronize_body(t) &&label 4: withincode(* main(. . )) && args(t) { proceed(t) ; } throw $r 2; void around(Object t): label 5: synchronize(t) && withincode(* return; main(. . )) && args(t) { rm_proceed(t) ; } catch java. lang. Throwable from label 0 to label 1 with label 2; catch java. lang. Throwable from label 3 to label 4 with label 2; 6

Example to Change Lock Implementation. void around(Object obj): synchronized(obj) && args(obj) { if( is. Distributed(obj)) { Cluster. Manager. aquire. Distributed. Lock(obj) ; rm_proceed() ; Cluster. Manager. release. Distributed. Lock(obj) ; } else proceed() ; } * J. Boner and E. Kuleshov. Clustering the Java Virtual Machine using Aspect-Oriented Programming. In AOSD ’ 07: Industry Track. 7

Example to Check CPU Usage and Re-entrant Risk. void around(Object obj): synchronized(obj) && args(obj) { /* before proceed(), limits the CPU usage */ double received. CPUUsage = System. Information. get. Process. CPUUsage (m_prev. Snapshot, event); assert (received. CPUUsage < 50) ; /* before proceed(), check re-entrant locking, handle re-entrant locking if it happens */ if( !Thread. holds. Lock(obj)) proceed(obj) ; else if (is. Legal. Lock(obj)) rm_proceed(obj) ; else throw new Exception("Acquired illegal Lock") ; } * L. Stepanian. Inlining Java Native calls at runtime. In VEE ’ 05. 8

Converting Synchronized Blocks into Transactions String intern() { synchronized(map) { Object o = map. get(this) ; if(o!=null) return (String) o ; map. put(this, this) ; return this ; } } void around(Object map): synchronize(map) && withincode(* *. intern(. . )){ atomic{ rm_proceed(map) ; } } * A. Adl-Tabatabai, C. Kozyrakis and B. Saha. Unlocking Concurrency: Multicore programming with transactional memory. Queue, v. 4 n. 10, 2007. 9

10
- Slides: 10