db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r431603 - /db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/ConsistencyToken.java
Date Tue, 15 Aug 2006 14:20:50 GMT
Author: kahatlen
Date: Tue Aug 15 07:20:46 2006
New Revision: 431603

URL: http://svn.apache.org/viewvc?rev=431603&view=rev
Log:
DERBY-1688: ConsistencyToken.hashCode() is not thread safe

Rewrite the method to use a thread-safe approach as suggested by The
"Double-Checked Locking is Broken" Declaration.

Modified:
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/ConsistencyToken.java

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/ConsistencyToken.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/ConsistencyToken.java?rev=431603&r1=431602&r2=431603&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/ConsistencyToken.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/ConsistencyToken.java Tue Aug
15 07:20:46 2006
@@ -70,13 +70,23 @@
      * @return hash code
      */
     public int hashCode() {
-        if (hash == 0) {
+        // ConsistencyToken objects might be kept for a long time and are
+        // frequently used as keys in hash tables. Therefore, it is a good idea
+        // to cache their hash codes.
+        int h = hash;
+        if (h == 0) {
+            // The hash code has not been calculated yet (or perhaps the hash
+            // code actually is 0). Calculate a new one and cache it. No
+            // synchronization is needed since reads and writes of 32-bit
+            // primitive values are guaranteed to be atomic. See The
+            // "Double-Checked Locking is Broken" Declaration for details.
             int len = bytes.length;
             for (int i = 0; i < len; ++i) {
-                hash ^= bytes[i];
+                h ^= bytes[i];
             }
+            hash = h;
         }
-        return hash;
+        return h;
     }
 
     /**



Mime
View raw message