<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: Semantics for a Synchronized Block Join Point</title>
	<link>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/</link>
	<description>Down To The Bone</description>
	<pubDate>Thu, 16 Oct 2008 07:11:26 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>

	<item>
		<title>by: Alex</title>
		<link>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3384</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3384</guid>
					<description>&lt;p&gt;I think such a discussion must be driven by concrete uses cases. Here are some:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;time to acquire the lock : need an advice just before the lock is acquired and one (possibly the same is there are sort of a proceed concept) just after it is acquired&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;time spend with the lock (ownership) : need an advice just before (or after) the lock is released&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;lock implementation : bypass the JMM lock and use something else - possibly with conditional logic such as &quot; if (t is distributed) { getADistributedLock(); proceedWithoutJMMLock(); releaseDistributedLockFinally(); } else { proceedWithJMMLock(); (or possibly proceed() and let the user add is own enclosing JMM synchronized(t) {proceed();} }&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How each of those would be implemented by the proposed semantics ?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I think such a discussion must be driven by concrete uses cases. Here are some:</p>

<ul>
<li><p>time to acquire the lock : need an advice just before the lock is acquired and one (possibly the same is there are sort of a proceed concept) just after it is acquired</p></li>
<li><p>time spend with the lock (ownership) : need an advice just before (or after) the lock is released</p></li>
<li><p>lock implementation : bypass the JMM lock and use something else - possibly with conditional logic such as &#8221; if (t is distributed) { getADistributedLock(); proceedWithoutJMMLock(); releaseDistributedLockFinally(); } else { proceedWithJMMLock(); (or possibly proceed() and let the user add is own enclosing JMM synchronized(t) {proceed();} }</p></li>
</ul>

<p>How each of those would be implemented by the proposed semantics ?</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: eu</title>
		<link>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3385</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3385</guid>
					<description>&lt;p&gt;It would be nice to be able to use annotation for variables and blocks inside of the method code. Unfortunately first case only supported on a source level and second case is not supported at all, which leave very little options to the runtime. Monitorenter/exit should work on to some extend and for more complicated cases one could use hack with try/finally block as I've siggested earlier. http://jroller.com/page/eu/20050608#expressing&lt;em&gt;pointcuts&lt;/em&gt;within&lt;em&gt;method&lt;/em&gt;code&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>It would be nice to be able to use annotation for variables and blocks inside of the method code. Unfortunately first case only supported on a source level and second case is not supported at all, which leave very little options to the runtime. Monitorenter/exit should work on to some extend and for more complicated cases one could use hack with try/finally block as I&#8217;ve siggested earlier. http://jroller.com/page/eu/20050608#expressing<em>pointcuts</em>within<em>method</em>code</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Sam Pullara</title>
		<link>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3386</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3386</guid>
					<description>&lt;p&gt;Couple of nits:&lt;/p&gt;

&lt;p&gt;1) Though the monitor enter/exit don't have to be paired in the bytecode you will find that JVMs will puke on them not being paired with the same method.  Also, the last time I checked many JVMs will puke if they are interleaved, eg:&lt;/p&gt;

&lt;p&gt;monitor1.enter()
monitor2.enter()
monitor1.exit();
monitor2.exit();&lt;/p&gt;

&lt;p&gt;Even though that construction would be very useful for navigating trees of information.&lt;/p&gt;

&lt;p&gt;2) The bytecode for synchronized methods vs. a method enclosed enitrely within a synchronized block are actually different and will go through different code paths in the JVM.&lt;/p&gt;

&lt;p&gt;My suggestion is the you have 2 new pointcuts, monitorEnter and monitorExit that you can apply before/after/around advice to.  Basically treat them as method calls whose target is the object that is being used as a monitor.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Couple of nits:</p>

<p>1) Though the monitor enter/exit don&#8217;t have to be paired in the bytecode you will find that JVMs will puke on them not being paired with the same method.  Also, the last time I checked many JVMs will puke if they are interleaved, eg:</p>

<p>monitor1.enter()
monitor2.enter()
monitor1.exit();
monitor2.exit();</p>

<p>Even though that construction would be very useful for navigating trees of information.</p>

<p>2) The bytecode for synchronized methods vs. a method enclosed enitrely within a synchronized block are actually different and will go through different code paths in the JVM.</p>

<p>My suggestion is the you have 2 new pointcuts, monitorEnter and monitorExit that you can apply before/after/around advice to.  Basically treat them as method calls whose target is the object that is being used as a monitor.</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Jonas</title>
		<link>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3387</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3387</guid>
					<description>&lt;blockquote&gt;
  &lt;p&gt;2) The bytecode for synchronized methods vs. a method enclosed enitrely within a synchronized 
  block are actually different and will go through different code paths in the JVM.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Right, this is the reason why call(synchronized &lt;em&gt;.&lt;/em&gt;(..)) and execution(synchronized &lt;em&gt;.&lt;/em&gt;(..)) are semantically different in regards to the locking (since the locking is not seen as part of the method body).&lt;/p&gt;

&lt;p&gt;But it is conceptually equal to locking on 'this' (and was stated there only to drive the discussion).&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;My suggestion is the you have 2 new pointcuts, monitorEnter and monitorExit that you can apply 
  before/after/around advice to. Basically treat them as method calls whose target is the object that is being used as a monitor.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I wanted to stay neutral, at least in the beginning of this discussion, but this (the 1:st solution in the post) is actually the one I prefer (probably the only one that makes sense). By the way, it is the only solution that addresses all use cases that Alex defined in his comment.&lt;/p&gt;

&lt;p&gt;Thanks all for your comments.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<blockquote>
  <p>2) The bytecode for synchronized methods vs. a method enclosed enitrely within a synchronized 
  block are actually different and will go through different code paths in the JVM.</p>
</blockquote>

<p>Right, this is the reason why call(synchronized <em>.</em>(..)) and execution(synchronized <em>.</em>(..)) are semantically different in regards to the locking (since the locking is not seen as part of the method body).</p>

<p>But it is conceptually equal to locking on &#8216;this&#8217; (and was stated there only to drive the discussion).</p>

<blockquote>
  <p>My suggestion is the you have 2 new pointcuts, monitorEnter and monitorExit that you can apply 
  before/after/around advice to. Basically treat them as method calls whose target is the object that is being used as a monitor.</p>
</blockquote>

<p>I wanted to stay neutral, at least in the beginning of this discussion, but this (the 1:st solution in the post) is actually the one I prefer (probably the only one that makes sense). By the way, it is the only solution that addresses all use cases that Alex defined in his comment.</p>

<p>Thanks all for your comments.</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: eu</title>
		<link>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3388</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3388</guid>
					<description>&lt;p&gt;By the way, it would be coll to add a support for named block is AspectJ compiler, so you would be able to access information aqbout method slicing in the runtime. The syntax could be like this:&lt;/p&gt;

&lt;p&gt;@SomeAnnotation(...) {...}&lt;/p&gt;

&lt;p&gt;@SomeAnnotation(...) synchronized(...) {...}&lt;/p&gt;

&lt;p&gt;for(...) @SomeAnnotation(...) {...}&lt;/p&gt;

&lt;p&gt;if() @SomeAnnotation(...) {...}
else @AnotherAnnotation(...) {...}&lt;/p&gt;

&lt;p&gt;try 
@SomeAnnotation(...) {
} catch() 
@AnotherAnnotation(...) {
}&lt;/p&gt;

&lt;p&gt;So, this can actually go into a custom attribute that would have start and end offset in the method bytecode (similar to try/catch) and then value of the annotation for this block. Perhaps such atribute can even make into Dolphin...&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>By the way, it would be coll to add a support for named block is AspectJ compiler, so you would be able to access information aqbout method slicing in the runtime. The syntax could be like this:</p>

<p>@SomeAnnotation(&#8230;) {&#8230;}</p>

<p>@SomeAnnotation(&#8230;) synchronized(&#8230;) {&#8230;}</p>

<p>for(&#8230;) @SomeAnnotation(&#8230;) {&#8230;}</p>

<p>if() @SomeAnnotation(&#8230;) {&#8230;}
else @AnotherAnnotation(&#8230;) {&#8230;}</p>

<p>try 
@SomeAnnotation(&#8230;) {
} catch() 
@AnotherAnnotation(&#8230;) {
}</p>

<p>So, this can actually go into a custom attribute that would have start and end offset in the method bytecode (similar to try/catch) and then value of the annotation for this block. Perhaps such atribute can even make into Dolphin&#8230;</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Jonas</title>
		<link>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3389</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3389</guid>
					<description>&lt;p&gt;Today I was actually swearing about why the JSR-175 spec does not support annotated blocks...(only one out of a long list of shortcomings). It feels like such a natural thing and would open up many more possibities.&lt;/p&gt;

&lt;p&gt;I really like your proposal, but it should not end up in the AJ compiler but in the real spec.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Today I was actually swearing about why the JSR-175 spec does not support annotated blocks&#8230;(only one out of a long list of shortcomings). It feels like such a natural thing and would open up many more possibities.</p>

<p>I really like your proposal, but it should not end up in the AJ compiler but in the real spec.</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Eric Bodden</title>
		<link>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3390</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3390</guid>
					<description>&lt;p&gt;Hmmm this is a nice proposal (which has been proposed many times before :-) ).&lt;/p&gt;

&lt;p&gt;Unfortunately you will run into problems with interleavind monitorenter/exit statements (see above), which &lt;em&gt;cannot&lt;/em&gt; be disambiduated statically. So you &lt;em&gt;would&lt;/em&gt; need runtime instrumentation for this! There is no general way around.&lt;/p&gt;

&lt;p&gt;W.r.t. the &quot;block&quot; pointcut, I think that a) it would be a huge overkill (why not extract a method if I want a semantic block?) and also some blocks are agaibn not statically defined by the bytecode, so for instance exception handlers, which have no defined end...&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hmmm this is a nice proposal (which has been proposed many times before <img src='http://jonasboner.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  ).</p>

<p>Unfortunately you will run into problems with interleavind monitorenter/exit statements (see above), which <em>cannot</em> be disambiduated statically. So you <em>would</em> need runtime instrumentation for this! There is no general way around.</p>

<p>W.r.t. the &#8220;block&#8221; pointcut, I think that a) it would be a huge overkill (why not extract a method if I want a semantic block?) and also some blocks are agaibn not statically defined by the bytecode, so for instance exception handlers, which have no defined end&#8230;</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: eu</title>
		<link>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3391</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3391</guid>
					<description>&lt;p&gt;Jonas, don't see why it can't be actually prototyped in AJ compiler... :-)&lt;/p&gt;

&lt;p&gt;Anyway, I've started a thread in Mustand discussion forum. Please leave a comment or two out there. http://forums.java.net/jive/thread.jspa?threadID=988&amp;#38;tstart=0&lt;/p&gt;

&lt;p&gt;By the way, Eric, with this functionality on a Java code level you should not have issues with an interleaved blocks because compiler will generate a proper ranges.&lt;/p&gt;

&lt;p&gt;I disagree about the overkill. Because all annotated blocks are visible on a method level it is actually more lightweigh option then extracting methods. It would also allow to add e.g. onException adviced only for annotated exeption blocks, which is not that easy to do with currently available pointcut definitions (especially if you have two nested exception handles on the same exception type).&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Jonas, don&#8217;t see why it can&#8217;t be actually prototyped in AJ compiler&#8230; <img src='http://jonasboner.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>

<p>Anyway, I&#8217;ve started a thread in Mustand discussion forum. Please leave a comment or two out there. http://forums.java.net/jive/thread.jspa?threadID=988&amp;tstart=0</p>

<p>By the way, Eric, with this functionality on a Java code level you should not have issues with an interleaved blocks because compiler will generate a proper ranges.</p>

<p>I disagree about the overkill. Because all annotated blocks are visible on a method level it is actually more lightweigh option then extracting methods. It would also allow to add e.g. onException adviced only for annotated exeption blocks, which is not that easy to do with currently available pointcut definitions (especially if you have two nested exception handles on the same exception type).</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Bruno Harbulot</title>
		<link>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3392</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3392</guid>
					<description>&lt;p&gt;Hello,
As pointed out by Laurie Hendren and Eric, it can be very difficult and possibly impossible to have a solution for the general case at compile-time.&lt;/p&gt;

&lt;p&gt;However, I think there might be a solution for a large number of cases.
This would consist of:
1/ building the exceptional basic-block graph for the method.&lt;/p&gt;

&lt;p&gt;2/ Finding the monitorenter instruction, and the block immediatly after (it should always be the case, since it's the beginning of a trap)&lt;/p&gt;

&lt;p&gt;3/ There should be (I guess) a couple of blocks (one &quot;normal&quot; and one handler for the finally/monitorexit) that &quot;post-dominate between them&quot; the monitorenter block (i.e. it is compulsory to go through one of them before returning from the method).&lt;/p&gt;

&lt;p&gt;4/ The subgraph between the monitorenter instruction, and the &quot;post-dominating&quot; monitorexits would then represent the synchonized block.&lt;/p&gt;

&lt;p&gt;(This would be a similar approach to the potential if-then-else join point I've suggested at the end of the paper on the join point for loops presented in FOAL 2005. I would not encourage an if-then-else join point, though.)&lt;/p&gt;

&lt;p&gt;One remaining problem would be to make sure that the monitorenter and monitorexits match the same reference.&lt;/p&gt;

&lt;p&gt;If you take this simple program:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public void run() {
    System.out.println(&quot;A&quot;) ;
    synchronized(this) {
        System.out.println(&quot;B&quot;) ;
    }
    System.out.println(&quot;C&quot;) ;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here is its jimple representation, based on the bytecode generated by Eclipse JDT (see Soot: )&lt;/p&gt;

&lt;p&gt;public void run() {
        Test r0, r2;
        java.io.PrintStream $r1, $r4, $r6;
        java.lang.Throwable $r5;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    r0 := @this: Test;
    $r1 = ;
    virtualinvoke $r1.(&quot;A&quot;);
    r2 = r0;
    entermonitor r0;

 label0:
    $r4 = ;
    virtualinvoke $r4.(&quot;B&quot;);
    exitmonitor r2;
    goto label2;

 label1:
    $r5 := @caughtexception;
    exitmonitor r2;
    throw $r5;

 label2:
    $r6 = ;
    virtualinvoke $r6.(&quot;C&quot;);
    return;

    catch java.lang.Throwable from label0 to label1 with label1;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Here, the problem is that r0 and r2 represent in fact the same reference. Because synchronised blocks are always within the same method, it should not be too difficult to see which variables used my the monitorenter/monitorexit contain the same reference.&lt;/p&gt;

&lt;p&gt;This would require further investigation, and perhaps a prototype implementation (in abc for example).&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hello,
As pointed out by Laurie Hendren and Eric, it can be very difficult and possibly impossible to have a solution for the general case at compile-time.</p>

<p>However, I think there might be a solution for a large number of cases.
This would consist of:
1/ building the exceptional basic-block graph for the method.</p>

<p>2/ Finding the monitorenter instruction, and the block immediatly after (it should always be the case, since it&#8217;s the beginning of a trap)</p>

<p>3/ There should be (I guess) a couple of blocks (one &#8220;normal&#8221; and one handler for the finally/monitorexit) that &#8220;post-dominate between them&#8221; the monitorenter block (i.e. it is compulsory to go through one of them before returning from the method).</p>

<p>4/ The subgraph between the monitorenter instruction, and the &#8220;post-dominating&#8221; monitorexits would then represent the synchonized block.</p>

<p>(This would be a similar approach to the potential if-then-else join point I&#8217;ve suggested at the end of the paper on the join point for loops presented in FOAL 2005. I would not encourage an if-then-else join point, though.)</p>

<p>One remaining problem would be to make sure that the monitorenter and monitorexits match the same reference.</p>

<p>If you take this simple program:</p>

<pre><code>public void run() {
    System.out.println("A") ;
    synchronized(this) {
        System.out.println("B") ;
    }
    System.out.println("C") ;
}
</code></pre>

<p>Here is its jimple representation, based on the bytecode generated by Eclipse JDT (see Soot: )</p>

<p>public void run() {
        Test r0, r2;
        java.io.PrintStream $r1, $r4, $r6;
        java.lang.Throwable $r5;</p>

<pre><code>    r0 := @this: Test;
    $r1 = ;
    virtualinvoke $r1.("A");
    r2 = r0;
    entermonitor r0;

 label0:
    $r4 = ;
    virtualinvoke $r4.("B");
    exitmonitor r2;
    goto label2;

 label1:
    $r5 := @caughtexception;
    exitmonitor r2;
    throw $r5;

 label2:
    $r6 = ;
    virtualinvoke $r6.("C");
    return;

    catch java.lang.Throwable from label0 to label1 with label1;
</code></pre>

<p>}</p>

<p>Here, the problem is that r0 and r2 represent in fact the same reference. Because synchronised blocks are always within the same method, it should not be too difficult to see which variables used my the monitorenter/monitorexit contain the same reference.</p>

<p>This would require further investigation, and perhaps a prototype implementation (in abc for example).</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Euxx</title>
		<link>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3393</link>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid>http://jonasboner.com/2005/07/18/semantics-for-a-synchronized-block-join-point/#comment-3393</guid>
					<description>&lt;p&gt;&lt;strong&gt;Expressing pointcuts within method code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Jonas Boner  blogged about  expressing &quot;synchronized&quot; constructs within method code in AOP semantics.&lt;/p&gt;

&lt;p&gt;Well, it shouldn't be difficult to build a dataflow tree for all synchronized blocks in any given method, and then address each block using some X&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p><strong>Expressing pointcuts within method code.</strong></p>

<p>Jonas Boner  blogged about  expressing &#8220;synchronized&#8221; constructs within method code in AOP semantics.</p>

<p>Well, it shouldn&#8217;t be difficult to build a dataflow tree for all synchronized blocks in any given method, and then address each block using some X</p>
]]></content:encoded>
				</item>
</channel>
</rss>
