harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apetre...@apache.org
Subject svn commit: r575604 - /harmony/enhanced/classlib/trunk/modules/swing/src/main/java/common/javax/swing/text/AbstractDocument.java
Date Fri, 14 Sep 2007 10:16:54 GMT
Author: apetrenko
Date: Fri Sep 14 03:16:45 2007
New Revision: 575604

URL: http://svn.apache.org/viewvc?rev=575604&view=rev
Log:
Patch for HARMONY-4634 "[classlib][swing]SplitPane triggers the deadlock in AbstractDocument$ReadWriteLock"

Modified:
    harmony/enhanced/classlib/trunk/modules/swing/src/main/java/common/javax/swing/text/AbstractDocument.java

Modified: harmony/enhanced/classlib/trunk/modules/swing/src/main/java/common/javax/swing/text/AbstractDocument.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/swing/src/main/java/common/javax/swing/text/AbstractDocument.java?rev=575604&r1=575603&r2=575604&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/swing/src/main/java/common/javax/swing/text/AbstractDocument.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/swing/src/main/java/common/javax/swing/text/AbstractDocument.java
Fri Sep 14 03:16:45 2007
@@ -833,81 +833,103 @@
     }
 
     private static class ReadWriteLock {
-        private List<Thread> readers = new ArrayList<Thread>();
-        private Thread writer;
-        private int writerCount;
-        private boolean callingListeners;
+        private boolean            callingListeners;
+        private int                writeAcquestCounter;
+        private int                waitingWritersCounter;
+        private Thread             activeWriter;
+        private final List<Thread> activeReaders = new ArrayList<Thread>();
+        private final Object       lock          = new Object();
 
-        public ReadWriteLock() {
-            super();
+        public final Thread getCurrentWriter() {
+            return activeWriter;
         }
 
-        public final synchronized Thread getCurrentWriter() {
-            return writer;
-        }
-
-        public final synchronized void readLock() {
+        public final void readLock() {
             final Thread thread = Thread.currentThread();
 
-            if (writer != thread) {
-                while (writerCount > 0) {
+            if (thread == activeWriter) {
+                return;
+            }
+
+            synchronized (lock) {
+                while ((activeWriter != null) || (waitingWritersCounter > 0)) {
                     try {
-                        wait();
-                    } catch (final InterruptedException e) { }
+                        lock.wait();
+                    } catch (final InterruptedException e) {
+                        return;
+                    }
                 }
-            }
 
-            readers.add(thread);
+                activeReaders.add(thread);
+            }
         }
 
-        public final synchronized void readUnlock() {
-            final Thread currentThread = Thread.currentThread();
-            final int index = readers.indexOf(currentThread);
+        public final void readUnlock() {
+            final Thread thread = Thread.currentThread();
 
-            if (index == -1) {
-                throw new Error(Messages.getString("swing.err.10")); //$NON-NLS-1$
+            if (thread == activeWriter) {
+                return;
             }
 
-            readers.remove(index);
-            if (readers.size() == 0) {
-                notify();
+            synchronized (lock) {
+                if (!activeReaders.remove(thread)) {
+                    throw new Error(Messages.getString("swing.err.10")); //$NON-NLS-1$
+                }
+
+                lock.notifyAll();
             }
         }
 
-        public final synchronized void setCallingListeners(final boolean flag) {
+        public final void setCallingListeners(final boolean flag) {
             callingListeners = flag;
         }
 
-        public final synchronized void writeLock() {
+        public final void writeLock() {
             if (callingListeners) {
                 throw new IllegalStateException(Messages.getString("swing.7E")); //$NON-NLS-1$
             }
 
             final Thread thread = Thread.currentThread();
 
-            while (readers.size() > 0
-                   || writer != null && writer != thread) {
-                try {
-                    wait();
-                } catch (final InterruptedException e) { }
-            }
+            if (thread == activeWriter) {
+                writeAcquestCounter++;
+                return;
+            }
+
+            synchronized (lock) {
+                if ((activeReaders.size() > 0) || (activeWriter != null)) {
+
+                    waitingWritersCounter++;
+
+                    while ((activeReaders.size() > 0) || (activeWriter != null)) {
+                        try {
+                            lock.wait();
+                        } catch (final InterruptedException e) {
+                            waitingWritersCounter--;
+                            return;
+                        }
+                    }
+
+                    waitingWritersCounter--;
+                }
 
-            if (writerCount++ == 0) {
-                writer = thread;
+                writeAcquestCounter++;
+                activeWriter = thread;
             }
         }
 
-        public final synchronized void writeUnlock() {
-            if (writer != Thread.currentThread()) {
+        public final void writeUnlock() {
+            if (activeWriter != Thread.currentThread()) {
                 throw new Error(Messages.getString("swing.err.11")); //$NON-NLS-1$      
     }
             }
-            if (--writerCount == 0) {
-                writer = null;
-                readers.clear();
-                notifyAll();
+
+            if (--writeAcquestCounter == 0) {
+                synchronized (lock) {
+                    activeWriter = null;
+                    lock.notifyAll();
+                }
             }
         }
-
     }
 
     /**



Mime
View raw message