Index: src/main/java/common/org/apache/harmony/awt/wtk/Synchronizer.java =================================================================== --- src/main/java/common/org/apache/harmony/awt/wtk/Synchronizer.java (revision 527416) +++ src/main/java/common/org/apache/harmony/awt/wtk/Synchronizer.java (working copy) @@ -20,8 +20,10 @@ */ package org.apache.harmony.awt.wtk; -import java.util.Hashtable; +import java.util.IdentityHashMap; import java.util.LinkedList; +import java.util.Map; +import java.util.concurrent.locks.LockSupport; import org.apache.harmony.awt.internal.nls.Messages; @@ -64,7 +66,7 @@ */ protected Thread dispatchThread; - private final Hashtable storedStates = new Hashtable(); + private final Map storedStates = new IdentityHashMap(); /** * Acquire the lock for this synchronizer. Nested lock is supported. @@ -74,34 +76,26 @@ * Supposed to be used in Toolkit.lockAWT() only. */ public void lock() { - synchronized (this) { - Thread curThread = Thread.currentThread(); + final Thread curThread = Thread.currentThread(); - if (acquestCounter == 0) { - acquestCounter = 1; + synchronized (this) { + if (owner == null) { owner = curThread; + acquestCounter++; + return; + } else if (owner == curThread) { + acquestCounter++; + return; + } + + if (curThread == dispatchThread) { + waitQueue.addFirst(curThread); } else { - if (owner == curThread) { - acquestCounter++; - } else { - if (curThread == dispatchThread) { - waitQueue.addFirst(curThread); - } else { - waitQueue.addLast(curThread); - } - try { - wait(); - } catch (InterruptedException e) { - if (owner != curThread) { - waitQueue.remove(curThread); - // awt.1F=Waiting for resource access thread interrupted not from unlock method. - throw new RuntimeException(Messages - .getString("awt.1F")); //$NON-NLS-1$ - } - } - } + waitQueue.addLast(curThread); } } + + LockSupport.park(); } /** @@ -111,21 +105,23 @@ */ public void unlock() { synchronized (this) { - if (acquestCounter == 0) { + if (owner == null) { // awt.20=Can't unlock not locked resource. throw new RuntimeException(Messages.getString("awt.20")); //$NON-NLS-1$ } + if (owner != Thread.currentThread()) { // awt.21=Not owner can't unlock resource. throw new RuntimeException(Messages.getString("awt.21")); //$NON-NLS-1$ } acquestCounter--; + if (acquestCounter == 0) { - if (waitQueue.size() > 0) { - acquestCounter = 1; + if (!waitQueue.isEmpty()) { owner = waitQueue.removeFirst(); - owner.interrupt(); + acquestCounter++; + LockSupport.unpark(owner); } else { owner = null; } @@ -140,22 +136,21 @@ * Do not call it directly. */ public void storeStateAndFree() { - synchronized (this) { - Thread curThread = Thread.currentThread(); + final Thread curThread = Thread.currentThread(); - if (owner != curThread) { - // awt.22=Not owner can't free resource. - throw new RuntimeException(Messages.getString("awt.22")); //$NON-NLS-1$ - } - if (storedStates.containsKey(curThread)) { - // awt.23=One thread can't store state several times in a row. - throw new RuntimeException(Messages.getString("awt.23")); //$NON-NLS-1$ - } + if (owner != curThread) { + // awt.22=Not owner can't free resource. + throw new RuntimeException(Messages.getString("awt.22")); //$NON-NLS-1$ + } - storedStates.put(curThread, new Integer(acquestCounter)); - acquestCounter = 1; - unlock(); + if (storedStates.containsKey(curThread)) { + // awt.23=One thread can't store state several times in a row. + throw new RuntimeException(Messages.getString("awt.23")); //$NON-NLS-1$ } + + storedStates.put(curThread, new Integer(acquestCounter)); + acquestCounter = 1; + unlock(); } /** @@ -165,23 +160,24 @@ * Do not call it directly. */ public void lockAndRestoreState() { - synchronized (this) { - Thread curThread = Thread.currentThread(); + final Integer counter; + final Thread curThread = Thread.currentThread(); - if (owner == curThread) { - // awt.24=Owner can't overwrite resource state. Lock operations may be lost. - throw new RuntimeException( - Messages.getString("awt.24")); //$NON-NLS-1$ - } - if (!storedStates.containsKey(curThread)) { - // awt.25=No state stored for current thread. - throw new RuntimeException(Messages.getString("awt.25")); //$NON-NLS-1$ - } + if (owner == curThread) { + // awt.24=Owner can't overwrite resource state. Lock operations + // may be lost. + throw new RuntimeException(Messages.getString("awt.24")); //$NON-NLS-1$ + } - lock(); - acquestCounter = storedStates.get(curThread).intValue(); - storedStates.remove(curThread); + counter = storedStates.remove(curThread); + + if (counter == null) { + // awt.25=No state stored for current thread. + throw new RuntimeException(Messages.getString("awt.25")); //$NON-NLS-1$ } + + lock(); + acquestCounter = counter.intValue(); } /** @@ -196,5 +192,4 @@ this.dispatchThread = dispatchThread; } } - }