Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
A Synchronized Block Join Point
for AspectJ
Chenchen Xi, Bruno Harbulot and John Gurd
The University of Manchester
Brussels, April 2008
Introduction
The goal of SoC in parallel programming
loop join point
Full control of the thread behavior
Thread:: notify()/notifyAll()/wait().
Distributed Java programs,
sharing data among multiple threads
sharing among clusters of JVMs
sharing among clusters of physical computers.
Replace lock-based synchronization
Transactional Memory
2
Synchronized Block vs. Synchronized Method
Synchronized block
Synchronized method
class Foo {
public void bar1() {...}
}
class Foo {
public synchronized void bar1() {...}
}
class Example {
public static void main(String arg[ ]) {
Foo objecta = new Foo() ;
Foo objectb = new Foo() ;
synchronized (objecta) {
objecta.bar1() ; }
objectb.bar1() ;
}
}
class Example {
public static void main(String arg[ ]){
Foo objecta = new Foo() ;
Foo objectb = new Foo() ;
objecta.bar1() ;
objectb.bar2() ;
}
}
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.bar1() ; }
objectb.bar1() ;
}
}
Java
arg := @parameter0: java.lang.String[];
$r0 = new Foo;
specialinvoke $r0.<Foo: void <init>()>();
objecta = $r0;
$r1 = new Foo;
specialinvoke $r1.<Foo: void <init>()>();
objectb = $r1;
entermonitor objecta;
label0:
virtualinvoke objecta.<Foo: void bar1()>();
exitmonitor objecta;
label1:
goto label5;
label2:
$r2 := @caughtexception;
label3:
exitmonitor objecta;
label4:
throw $r2;
label5:
return;
Jimple
4
Control-flow Graph
entermonitor $r0
0:
2:
//in the synchronized block
exitmonitor $r0;
$r1 := @caughtexception;
3:
exitmonitor $r0;
1:
goto label5a;
label5 ;
4:
5a:
5:
return;
nop;
throw $r1;
5b:
return;
5
Semantics for a Synchronized Block Join Point
Jimple Code
arg
arg :=
:= @parameter0:
@parameter0: java.lang.String[];
java.lang.String[];
$r0
=
new
Foo;
$r0 = new Foo;
Lock based
specialinvoke
specialinvoke $r0.<Foo:
$r0.<Foo: void
void <init>()>();
<init>()>();
lock (Type t) && withincode (* main(..))
&&
args(t)
objecta
objecta =
= $r0;
$r0;
$r1
=
new
unlock (Type t) && withincode $r1
(* main(..))
= new Foo;
Foo;&& args(t)
specialinvoke
specialinvoke $r1.<Foo:
$r1.<Foo: void
void <init>()>();
<init>()>();
objectb
=
$r1;
objectb = $r1;
Block based
entermonitor objecta;
synchronized (Type t) && withincode
(* main(..)) && args(t)
label0:
label0:
synchronized_body( Type t) &&virtualinvoke
withincodeobjecta.<Foo:
(* main(..))
&&
args(object)
void
bar1()>();
virtualinvoke
objecta.<Foo:
void
bar1()>();
exitmonitor objecta;
label1:
label1:
Pointcuts and Advice
goto
goto label5;
label5;
label2:
void around(Object t):
$r2 := @caughtexception;
synchronize(t) && withincode(*
main(..)) && args(t) { proceed(t) ; }
label3:
void around(Object t):
exitmonitor objecta;
synchronize_body(t) &&label4:
withincode(* main(..)) && args(t) { proceed(t) ; }
throw $r2;
void around(Object t):
label5:
label5:
synchronize(t) && withincode(*
return;
return; main(..)) && args(t) { rm_proceed(t) ; }
catch java.lang.Throwable from label0 to label1 with label2;
catch java.lang.Throwable from label3 to label4 with label2;
6
Example to Change Lock Implementation.
void around(Object obj): synchronized(obj) && args(obj) {
if( isDistributed(obj)) {
ClusterManager.aquireDistributedLock(obj) ;
rm_proceed() ;
ClusterManager.releaseDistributedLock(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 receivedCPUUsage = SystemInformation.
getProcessCPUUsage (m_prevSnapshot, event);
assert (receivedCPUUsage < 50) ;
/* before proceed(), check re-entrant locking,
handle re-entrant locking if it happens */
if( !Thread.holdsLock(obj))
proceed(obj) ;
else if (isLegalLock(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