You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2012/07/16 23:17:29 UTC

svn commit: r1362263 - /cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/BusFactory.java

Author: dkulp
Date: Mon Jul 16 21:17:29 2012
New Revision: 1362263

URL: http://svn.apache.org/viewvc?rev=1362263&view=rev
Log:
Merged revisions 1362260 via  git cherry-pick from
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1362260 | dkulp | 2012-07-16 17:15:45 -0400 (Mon, 16 Jul 2012) | 4 lines

  More work on removing the sync block for the ThreadDefaultBus
  Just use a ThreadLocal for normal case except for the initial set on the
  Thread.

........

Modified:
    cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/BusFactory.java

Modified: cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/BusFactory.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/BusFactory.java?rev=1362263&r1=1362262&r2=1362263&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/BusFactory.java (original)
+++ cxf/branches/2.6.x-fixes/api/src/main/java/org/apache/cxf/BusFactory.java Mon Jul 16 21:17:29 2012
@@ -67,7 +67,14 @@ public abstract class BusFactory {
     public static final String DEFAULT_BUS_FACTORY = "org.apache.cxf.bus.CXFBusFactory";
 
     protected static Bus defaultBus;
-    protected static Map<Thread, Bus> threadBusses = new WeakHashMap<Thread, Bus>();
+    
+    static class BusHolder {
+        Bus bus;
+        boolean stale;
+    }
+    
+    protected static Map<Thread, BusHolder> threadBusses = new WeakHashMap<Thread, BusHolder>();
+    protected static ThreadLocal<BusHolder> threadBus = new ThreadLocal<BusHolder>();
 
     private static final Logger LOG = LogUtils.getL7dLogger(BusFactory.class, "APIMessages");
 
@@ -105,6 +112,20 @@ public abstract class BusFactory {
             return defaultBus;
         }
     }
+    
+    private static BusHolder getThreadBusHolder() {
+        BusHolder h = threadBus.get();
+        if (h == null || h.stale) {
+            h = new BusHolder();
+            Thread cur = Thread.currentThread();
+            synchronized (threadBusses) {
+                threadBusses.put(cur, h);
+            }
+            threadBus.set(h);
+        }
+        return h;
+    }
+    
 
     /**
      * Sets the default bus.
@@ -126,15 +147,16 @@ public abstract class BusFactory {
      * @param bus the default bus.
      */
     public static void setThreadDefaultBus(Bus bus) {
-        Thread cur = Thread.currentThread();
         if (bus == null) {
-            synchronized (threadBusses) {
-                threadBusses.remove(cur);
+            BusHolder h = threadBus.get();
+            if (h != null) {
+                h.bus = null;
+                h.stale = true;
+                threadBus.remove();
             }
         } else {
-            synchronized (threadBusses) {
-                threadBusses.put(cur, bus);
-            }
+            BusHolder b = getThreadBusHolder();
+            b.bus = bus;
         }
     }
     
@@ -145,16 +167,21 @@ public abstract class BusFactory {
      * @return the old thread default bus or null
      */
     public static Bus getAndSetThreadDefaultBus(Bus bus) {
-        Thread cur = Thread.currentThread();
         if (bus == null) {
-            synchronized (threadBusses) {
-                return threadBusses.remove(cur);
-            }
-        } else {
-            synchronized (threadBusses) {
-                return threadBusses.put(cur, bus);
+            BusHolder b = threadBus.get();
+            if (b != null) {
+                Bus orig = b.bus;
+                b.bus = null;
+                b.stale = true;
+                threadBus.remove();
+                return orig;
             }
+            return null;
         }
+        BusHolder b = getThreadBusHolder();
+        Bus old = b.bus;
+        b.bus = bus;
+        return old;
     }
 
     /**
@@ -173,27 +200,22 @@ public abstract class BusFactory {
      * @return the default bus.
      */
     public static Bus getThreadDefaultBus(boolean createIfNeeded) {
-        Bus threadBus;
-        Thread cur = Thread.currentThread();
-        synchronized (threadBusses) {
-            threadBus = threadBusses.get(cur);
-        }
-        if (createIfNeeded && threadBus == null) {
-            threadBus = createThreadBus();
+        if (createIfNeeded) {
+            BusHolder b = getThreadBusHolder();
+            if (b.bus == null) {
+                b.bus = createThreadBus();
+            }
+            return b.bus;
         }
-        return threadBus;
+        BusHolder b = threadBus.get();
+        return b == null ? null : b.bus;
     }
     private static synchronized Bus createThreadBus() {
-        Bus threadBus;
-        Thread cur = Thread.currentThread();
-        synchronized (threadBusses) {
-            threadBus = threadBusses.get(cur);
-        }
-        if (threadBus == null) {
-            threadBus = getDefaultBus(true);
-            threadBusses.put(cur, threadBus);
+        BusHolder b = getThreadBusHolder();
+        if (b.bus == null) {
+            b.bus = getDefaultBus(true);
         }
-        return threadBus;
+        return b.bus;
     }
     /**
      * Removes a bus from being a thread default bus for any thread.
@@ -206,10 +228,19 @@ public abstract class BusFactory {
      */
     public static void clearDefaultBusForAnyThread(final Bus bus) {
         synchronized (threadBusses) {
-            for (final Iterator<Bus> iterator = threadBusses.values().iterator();
+            for (final Iterator<BusHolder> iterator = threadBusses.values().iterator();
                 iterator.hasNext();) {
-                Bus itBus = iterator.next();
-                if (bus == null || itBus == null || bus.equals(itBus)) {
+                BusHolder itBus = iterator.next();
+                if (bus == null || itBus == null || itBus.bus == null
+                    || itBus.stale
+                    || bus.equals(itBus.bus)) {
+                    if (itBus != null) {
+                        itBus.bus = null;
+                        //mark as stale so if a thread asks again, it will create a new one
+                        itBus.stale = true;  
+                    }
+                    //This will remove the BusHolder from the only place that should
+                    //strongly reference it
                     iterator.remove();
                 }
             }
@@ -223,11 +254,9 @@ public abstract class BusFactory {
      * @return true if the bus was not set and is now set
      */
     public static synchronized boolean possiblySetDefaultBus(Bus bus) {
-        Thread cur = Thread.currentThread();
-        synchronized (threadBusses) {
-            if (threadBusses.get(cur) == null) {
-                threadBusses.put(cur, bus);
-            }
+        BusHolder b = getThreadBusHolder();
+        if (b.bus == null) {
+            b.bus = bus;
         }
         if (defaultBus == null) {
             defaultBus = bus;