[Libreoffice-commits] core.git: jurt/com

Noel Grandin noel at peralex.com
Thu Oct 15 02:28:37 PDT 2015


 jurt/com/sun/star/lib/uno/bridges/java_remote/java_remote_bridge.java |   35 ++++------
 1 file changed, 16 insertions(+), 19 deletions(-)

New commits:
commit 846135c4c9eaedd18b2e01826860337c1b269664
Author: Noel Grandin <noel at peralex.com>
Date:   Thu Oct 15 09:43:06 2015 +0200

    use AtomicInteger for _life_count
    
    (cid#1326323 Unguarded read)
    Just switch this to an AtomicInteger, it's cheaper, and doesn't require synchronization,
    so less chance of a deadlock.
    
    This is an API change since this is a protected field in a public class, but anyone
    messing with the internals of this class should have known better.
    
    Change-Id: Idafc760c2e9d83442b8209ad23d180acb8dccb20

diff --git a/jurt/com/sun/star/lib/uno/bridges/java_remote/java_remote_bridge.java b/jurt/com/sun/star/lib/uno/bridges/java_remote/java_remote_bridge.java
index 0dcc75d..c949b55 100644
--- a/jurt/com/sun/star/lib/uno/bridges/java_remote/java_remote_bridge.java
+++ b/jurt/com/sun/star/lib/uno/bridges/java_remote/java_remote_bridge.java
@@ -26,33 +26,34 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
-import com.sun.star.lib.util.DisposeListener;
-import com.sun.star.lib.util.DisposeNotifier;
 import com.sun.star.bridge.XBridge;
 import com.sun.star.bridge.XInstanceProvider;
 import com.sun.star.connection.XConnection;
+import com.sun.star.lang.DisposedException;
 import com.sun.star.lang.EventObject;
 import com.sun.star.lang.XComponent;
 import com.sun.star.lang.XEventListener;
-import com.sun.star.lang.DisposedException;
 import com.sun.star.lib.uno.environments.java.java_environment;
 import com.sun.star.lib.uno.environments.remote.IProtocol;
 import com.sun.star.lib.uno.environments.remote.IReceiver;
+import com.sun.star.lib.uno.environments.remote.IThreadPool;
 import com.sun.star.lib.uno.environments.remote.Job;
 import com.sun.star.lib.uno.environments.remote.Message;
 import com.sun.star.lib.uno.environments.remote.ThreadId;
 import com.sun.star.lib.uno.environments.remote.ThreadPoolManager;
-import com.sun.star.lib.uno.environments.remote.IThreadPool;
 import com.sun.star.lib.uno.typedesc.MethodDescription;
 import com.sun.star.lib.uno.typedesc.TypeDescription;
+import com.sun.star.lib.util.DisposeListener;
+import com.sun.star.lib.util.DisposeNotifier;
+import com.sun.star.uno.Any;
 import com.sun.star.uno.IBridge;
 import com.sun.star.uno.IEnvironment;
-import com.sun.star.uno.UnoRuntime;
-import com.sun.star.uno.XInterface;
 import com.sun.star.uno.Type;
 import com.sun.star.uno.TypeClass;
-import com.sun.star.uno.Any;
+import com.sun.star.uno.UnoRuntime;
+import com.sun.star.uno.XInterface;
 
 /**
  * This class implements a remote bridge.
@@ -156,7 +157,7 @@ public class java_remote_bridge
     protected IProtocol         _iProtocol;
     protected IEnvironment      _java_environment;
     protected MessageDispatcher _messageDispatcher;
-    protected int               _life_count = 0;    // determines if this bridge is alive, which is controlled by acquire and release calls
+    protected final AtomicInteger _life_count = new AtomicInteger();    // determines if this bridge is alive, which is controlled by acquire and release calls
 
     private final ArrayList<XEventListener> _listeners = new ArrayList<XEventListener>();
 
@@ -169,7 +170,7 @@ public class java_remote_bridge
      * This method is for testing only.
      */
     int getLifeCount() {
-        return _life_count;
+        return _life_count.get();
     }
 
     /**
@@ -447,10 +448,10 @@ public class java_remote_bridge
      *
      * @see com.sun.star.uno.IBridge#acquire
      */
-    public synchronized void acquire() {
-        ++ _life_count;
+    public void acquire() {
+        int x = _life_count.incrementAndGet();
 
-        if(DEBUG) System.err.println("##### " + getClass().getName() + ".acquire:" + _life_count);
+        if(DEBUG) System.err.println("##### " + getClass().getName() + ".acquire:" + x);
     }
 
     /**
@@ -461,12 +462,8 @@ public class java_remote_bridge
      * @see com.sun.star.uno.IBridge#release
      */
     public void release() {
-        boolean dispose;
-        synchronized (this) {
-            --_life_count;
-            dispose = _life_count <= 0;
-        }
-        if (dispose) {
+        int x = _life_count.decrementAndGet();
+        if (x <= 0) {
             dispose(new Throwable("end of life"));
         }
     }
@@ -521,7 +518,7 @@ public class java_remote_bridge
             proxyFactory.dispose();
 
             if (DEBUG) {
-                if (_life_count != 0) {
+                if (_life_count.get() != 0) {
                     System.err.println(getClass().getName()
                                        + ".dispose - life count (proxies left):"
                                        + _life_count);


More information about the Libreoffice-commits mailing list