diff options
author | Fernando Nasser <fnasser@redhat.com> | 2004-01-19 17:38:36 +0000 |
---|---|---|
committer | Fernando Nasser <fnasser@gcc.gnu.org> | 2004-01-19 17:38:36 +0000 |
commit | 605d10f7f2c06334b8259cb59e920b1459454161 (patch) | |
tree | bdcd342767edb16816790bda374b2cd9aab08828 /libjava/java/awt/EventQueue.java | |
parent | 8f9f8d71c7d262a532f15248f0fef672ea465161 (diff) | |
download | gcc-605d10f7f2c06334b8259cb59e920b1459454161.zip gcc-605d10f7f2c06334b8259cb59e920b1459454161.tar.gz gcc-605d10f7f2c06334b8259cb59e920b1459454161.tar.bz2 |
EventQueue.java (pop): Prevent racing condition to add events to the queue out of order by acquiring...
* java/awt/EventQueue.java (pop): Prevent racing condition to add
events to the queue out of order by acquiring locks in the proper
order and not by releasing one before acquiring the other.
From-SVN: r76161
Diffstat (limited to 'libjava/java/awt/EventQueue.java')
-rw-r--r-- | libjava/java/awt/EventQueue.java | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/libjava/java/awt/EventQueue.java b/libjava/java/awt/EventQueue.java index 4cb0703..7df40ed 100644 --- a/libjava/java/awt/EventQueue.java +++ b/libjava/java/awt/EventQueue.java @@ -358,32 +358,34 @@ public class EventQueue if (prev == null) throw new EmptyStackException(); - // Don't synchronize both this and prev at the same time, or deadlock could - // occur. + /* The order is important here, we must get the prev lock first, + or deadlock could occur as callers usually get here following + prev's next pointer, and thus obtain prev's lock before trying + to get this lock. */ synchronized (prev) { prev.next = next; if (next != null) next.prev = prev; - } - synchronized (this) - { - int i = next_out; - while (i != next_in) + synchronized (this) { - prev.postEvent(queue[i]); - next_out = i; - if (++i == queue.length) - i = 0; - } - // Empty the queue so it can be reused - next_in = 0; - next_out = 0; + int i = next_out; + while (i != next_in) + { + prev.postEvent(queue[i]); + next_out = i; + if (++i == queue.length) + i = 0; + } + // Empty the queue so it can be reused + next_in = 0; + next_out = 0; - // Tell our EventDispatchThread that it can end execution - dispatchThread.interrupt (); - dispatchThread = null; + // Tell our EventDispatchThread that it can end execution + dispatchThread.interrupt (); + dispatchThread = null; + } } } |