You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ie...@apache.org on 2012/10/31 11:49:32 UTC

svn commit: r1404087 - /sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPoolManager.java

Author: ieb
Date: Wed Oct 31 10:49:32 2012
New Revision: 1404087

URL: http://svn.apache.org/viewvc?rev=1404087&view=rev
Log:
SLING-2535 wrapped the reference counter in a dedicated synchronised block to ensure that all access to the counter is safe. I think this will be safer than calling from within synchronised blocks as it doesnt rely on the caller remembering the methods need to be single threaded.

Modified:
    sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPoolManager.java

Modified: sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPoolManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPoolManager.java?rev=1404087&r1=1404086&r2=1404087&view=diff
==============================================================================
--- sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPoolManager.java (original)
+++ sling/trunk/bundles/commons/threads/src/main/java/org/apache/sling/commons/threads/impl/DefaultThreadPoolManager.java Wed Oct 31 10:49:32 2012
@@ -318,6 +318,12 @@ public class DefaultThreadPoolManager
 
         private BundleContext bundleContext;
 
+        /**
+         * This lock protects the counter which is volatile so must be
+         * protected.
+         */
+        private Object usagelock = new Object();
+
         public Entry(final String pid, final ThreadPoolConfig config, final String name, final BundleContext bundleContext) {
             this.pid = pid;
             this.config = config;
@@ -337,17 +343,21 @@ public class DefaultThreadPoolManager
         }
 
         public ThreadPoolFacade incUsage() {
-            if ( pool == null ) {
-                pool = new ThreadPoolFacade(new DefaultThreadPool(name, this.config));
+            synchronized (usagelock) {
+                if ( pool == null ) {
+                    pool = new ThreadPoolFacade(new DefaultThreadPool(name, this.config));
+                }
+                this.count++;
+                return pool;
             }
-            this.count++;
-            return pool;
         }
 
         public void decUsage() {
-            this.count--;
-            if ( this.count == 0 ) {
-                this.shutdown();
+            synchronized (usagelock) {
+                this.count--;
+                if ( this.count == 0 ) {
+                    this.shutdown();
+                }
             }
         }