You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by fh...@apache.org on 2010/12/21 17:14:25 UTC

svn commit: r1051539 - /tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java

Author: fhanik
Date: Tue Dec 21 16:14:24 2010
New Revision: 1051539

URL: http://svn.apache.org/viewvc?rev=1051539&view=rev
Log:
make shared variable volatile

Modified:
    tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java

Modified: tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java?rev=1051539&r1=1051538&r2=1051539&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java Tue Dec 21 16:14:24 2010
@@ -49,7 +49,7 @@ public class CounterLatch {
 
     private final Sync sync;
     private final AtomicLong count;
-    private long signal;
+    private volatile long signal;
     private volatile boolean released = false;
     
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: svn commit: r1051539 - /tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java

Posted by Jeremy Boynes <jb...@apache.org>.
Doug Lea also has information here:
	http://g.oswego.edu/dl/jmm/cookbook.html
and Intel's doc is here:
	http://download.intel.com/design/processor/manuals/253668.pdf
Section 8 has the info on the memory model.

However, as the value is immutable and only set in the constructor, IMO final is a more accurate description. It can be made volatile later if it actually becomes mutable.

On Dec 30, 2010, at 12:27 PM, Rainer Jung wrote:

> On 30.12.2010 15:28, sebb wrote:
>> On 29 December 2010 02:17, Filip Hanik - Dev Lists<de...@hanik.com>  wrote:
>>> On 12/28/2010 5:02 PM, sebb wrote:
>>>> On 22 December 2010 16:04, Filip Hanik - Dev Lists<de...@hanik.com>
>>>>> On 12/21/2010 7:29 PM, sebb wrote:
>>>>>> It would be cheaper to make it final as it's only ever set in the ctor
>>>>>> and is immutable.
>>>>> 
>>>>> nothing cheaper, volatile are only expensive to write, not read.
>>>> 
>>>> That's not my understanding of how volatile works.
>>> 
>>> there is a great book on concurrency that explains how the bus and cpu
>>> architectures handle these, if I remember the title of it, I will send it to
>>> you
>> 
>> Please do.
> 
> Maybe Filiup meant that one:
> 
> Multicore Application Programming: For Windows, Linux, and
> Oracle Solaris (Developer's Library)
> Darryl Gove
> ISBN-10: 0321711378
> ISBN-13: 978-0321711373
> 
> At least I recently found some intersting information in there.
> 
> Regards,
> 
> Rainer
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: dev-help@tomcat.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: svn commit: r1051539 - /tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java

Posted by Rainer Jung <ra...@kippdata.de>.
On 30.12.2010 15:28, sebb wrote:
> On 29 December 2010 02:17, Filip Hanik - Dev Lists<de...@hanik.com>  wrote:
>> On 12/28/2010 5:02 PM, sebb wrote:
>>> On 22 December 2010 16:04, Filip Hanik - Dev Lists<de...@hanik.com>
>>>> On 12/21/2010 7:29 PM, sebb wrote:
>>>>> It would be cheaper to make it final as it's only ever set in the ctor
>>>>> and is immutable.
>>>>
>>>> nothing cheaper, volatile are only expensive to write, not read.
>>>
>>> That's not my understanding of how volatile works.
>>
>> there is a great book on concurrency that explains how the bus and cpu
>> architectures handle these, if I remember the title of it, I will send it to
>> you
>
> Please do.

Maybe Filiup meant that one:

Multicore Application Programming: For Windows, Linux, and
Oracle Solaris (Developer's Library)
Darryl Gove
ISBN-10: 0321711378
ISBN-13: 978-0321711373

At least I recently found some intersting information in there.

Regards,

Rainer

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: svn commit: r1051539 - /tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java

Posted by sebb <se...@gmail.com>.
On 29 December 2010 02:17, Filip Hanik - Dev Lists <de...@hanik.com> wrote:
>
>
> On 12/28/2010 5:02 PM, sebb wrote:
>>
>> On 22 December 2010 16:04, Filip Hanik - Dev Lists<de...@hanik.com>
>>  wrote:
>>>
>>> On 12/21/2010 7:29 PM, sebb wrote:
>>>>>
>>>>> -    private long signal;
>>>>> +    private volatile long signal;
>>>>
>>>> It would be cheaper to make it final as it's only ever set in the ctor
>>>> and is immutable.
>>>
>>> nothing cheaper, volatile are only expensive to write, not read.
>>
>> That's not my understanding of how volatile works.
>
> there is a great book on concurrency that explains how the bus and cpu
> architectures handle these, if I remember the title of it, I will send it to
> you

Please do.

I've worked on HP Alpha shared memory systems where writes by one
processor may be seen in a different order on another; it would be
interesting to see how this is treated in the book.

>>
>> AIUI, the reader thread always needs to refresh the value of the field
>> in case another thread has updated it.
>>
>> Whereas a final field can be safely cached in local memory.
>>

As you write above, the volatile qualifier affects how the compiler
handles writes.
However, the volatile qualifier also affects read operations.

The JMM allows threads to cache values in local memory; also shared
memory may need memory barriers to ensure the reader sees what the
last writer wrote.
The volatile qualifier means that the compiler has to generate extra
instructions to ensure that the value - whether cached or not - is
current.

On the other hand, the value of a final field can safely be cached.
The compiler can just use the cached - or shared - value directly.

[Furthermore, Java 1.5+ compilers are not allowed to reorder certain
operations with respect to volatile reads. This may also impact
performance.]

>>> If maxConnections becomes a dynamically adjustable attribute, so will
>>> this one
>>> have to be.
>>
>> True, but at present it is immutable.
>>
>>>>>     private volatile boolean released = false;
>>>>>
>>>>>     /**
>>>>>
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
>>>>> For additional commands, e-mail: dev-help@tomcat.apache.org
>>>>>
>>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
>>>> For additional commands, e-mail: dev-help@tomcat.apache.org
>>>>
>>>>
>>>>
>>>> -----
>>>> No virus found in this message.
>>>> Checked by AVG - www.avg.com
>>>> Version: 10.0.1170 / Virus Database: 1435/3329 - Release Date: 12/21/10
>>>>
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: dev-help@tomcat.apache.org
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: dev-help@tomcat.apache.org
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: dev-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: svn commit: r1051539 - /tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java

Posted by Filip Hanik - Dev Lists <de...@hanik.com>.

On 12/28/2010 5:02 PM, sebb wrote:
> On 22 December 2010 16:04, Filip Hanik - Dev Lists<de...@hanik.com>  wrote:
>> On 12/21/2010 7:29 PM, sebb wrote:
>>>> -    private long signal;
>>>> +    private volatile long signal;
>>> It would be cheaper to make it final as it's only ever set in the ctor
>>> and is immutable.
>> nothing cheaper, volatile are only expensive to write, not read.
> That's not my understanding of how volatile works.
there is a great book on concurrency that explains how the bus and cpu architectures handle these, if I remember the title of it, I will 
send it to you
> AIUI, the reader thread always needs to refresh the value of the field
> in case another thread has updated it.
>
> Whereas a final field can be safely cached in local memory.
>
>> If maxConnections becomes a dynamically adjustable attribute, so will this one
>> have to be.
> True, but at present it is immutable.
>
>>>>      private volatile boolean released = false;
>>>>
>>>>      /**
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
>>>> For additional commands, e-mail: dev-help@tomcat.apache.org
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: dev-help@tomcat.apache.org
>>>
>>>
>>>
>>> -----
>>> No virus found in this message.
>>> Checked by AVG - www.avg.com
>>> Version: 10.0.1170 / Virus Database: 1435/3329 - Release Date: 12/21/10
>>>
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: dev-help@tomcat.apache.org
>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: dev-help@tomcat.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: svn commit: r1051539 - /tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java

Posted by sebb <se...@gmail.com>.
On 22 December 2010 16:04, Filip Hanik - Dev Lists <de...@hanik.com> wrote:
> On 12/21/2010 7:29 PM, sebb wrote:
>>
>>> -    private long signal;
>>> +    private volatile long signal;
>>
>> It would be cheaper to make it final as it's only ever set in the ctor
>> and is immutable.
>
> nothing cheaper, volatile are only expensive to write, not read.

That's not my understanding of how volatile works.

AIUI, the reader thread always needs to refresh the value of the field
in case another thread has updated it.

Whereas a final field can be safely cached in local memory.

> If maxConnections becomes a dynamically adjustable attribute, so will this one
> have to be.

True, but at present it is immutable.

>>>
>>>     private volatile boolean released = false;
>>>
>>>     /**
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: dev-help@tomcat.apache.org
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: dev-help@tomcat.apache.org
>>
>>
>>
>> -----
>> No virus found in this message.
>> Checked by AVG - www.avg.com
>> Version: 10.0.1170 / Virus Database: 1435/3329 - Release Date: 12/21/10
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: dev-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: svn commit: r1051539 - /tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java

Posted by Filip Hanik - Dev Lists <de...@hanik.com>.
On 12/21/2010 7:29 PM, sebb wrote:
>
>> -    private long signal;
>> +    private volatile long signal;
> It would be cheaper to make it final as it's only ever set in the ctor
> and is immutable.
nothing cheaper, volatile are only expensive to write, not read. if maxConnections becomes a dynamically adjustable attribute, so will this 
one have to be.
>>      private volatile boolean released = false;
>>
>>      /**
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: dev-help@tomcat.apache.org
>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: dev-help@tomcat.apache.org
>
>
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 10.0.1170 / Virus Database: 1435/3329 - Release Date: 12/21/10
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: svn commit: r1051539 - /tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java

Posted by sebb <se...@gmail.com>.
On 21 December 2010 16:14,  <fh...@apache.org> wrote:
> Author: fhanik
> Date: Tue Dec 21 16:14:24 2010
> New Revision: 1051539
>
> URL: http://svn.apache.org/viewvc?rev=1051539&view=rev
> Log:
> make shared variable volatile
>
> Modified:
>    tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java
>
> Modified: tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java
> URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java?rev=1051539&r1=1051538&r2=1051539&view=diff
> ==============================================================================
> --- tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java (original)
> +++ tomcat/trunk/java/org/apache/tomcat/util/threads/CounterLatch.java Tue Dec 21 16:14:24 2010
> @@ -49,7 +49,7 @@ public class CounterLatch {
>
>     private final Sync sync;
>     private final AtomicLong count;
> -    private long signal;
> +    private volatile long signal;

It would be cheaper to make it final as it's only ever set in the ctor
and is immutable.

>     private volatile boolean released = false;
>
>     /**
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: dev-help@tomcat.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org