From 459c4c517ec265d28e3c35f17f90288979f18c9f Mon Sep 17 00:00:00 2001
From: Tom Tromey <tromey@redhat.com>
Date: Mon, 11 Nov 2002 06:33:08 +0000
Subject: Container.java (validate): Use tree lock.

	* java/awt/Container.java (validate): Use tree lock.
	(getComponent): Likewise.
	(getComponents): Likewise.
	(addImpl): Likewise.
	(remove): Likewise.
	(removeAll): Likewise.
	(processEvent): Fixed indentation.
	(getComponentAt): Use tree lock.
	(findComponentAt): Likewise.
	(removeNotify): Likewise.
	(isAncestorOf): Likewise.
	(list): Likewise.
	(visitChildren): Likewise.
	(findNextFocusComponent): Likewise.
	(addNotifyContainerChildren): Likewise.
	(getAccessibleChildrenCount): Likewise.
	(getAccessibleChild): Likewise.

From-SVN: r59009
---
 libjava/java/awt/Container.java | 475 ++++++++++++++++++++++------------------
 1 file changed, 262 insertions(+), 213 deletions(-)

(limited to 'libjava/java/awt/Container.java')

diff --git a/libjava/java/awt/Container.java b/libjava/java/awt/Container.java
index d71a4bf..dd2390d 100644
--- a/libjava/java/awt/Container.java
+++ b/libjava/java/awt/Container.java
@@ -123,9 +123,12 @@ public class Container extends Component
    */
   public Component getComponent(int n)
   {
-    if (n < 0 || n >= ncomponents)
-      throw new ArrayIndexOutOfBoundsException("no such component");
-    return component[n];
+    synchronized (getTreeLock ())
+      {
+	if (n < 0 || n >= ncomponents)
+	  throw new ArrayIndexOutOfBoundsException("no such component");
+	return component[n];
+      }
   }
 
   /**
@@ -135,10 +138,13 @@ public class Container extends Component
    */
   public Component[] getComponents()
   {
-    Component[] result = new Component[ncomponents];
-    if (ncomponents > 0)
-      System.arraycopy(component, 0, result, 0, ncomponents);
-    return result;
+    synchronized (getTreeLock ())
+      {
+	Component[] result = new Component[ncomponents];
+	if (ncomponents > 0)
+	  System.arraycopy(component, 0, result, 0, ncomponents);
+	return result;
+      }
   }
 
   /**
@@ -260,69 +266,72 @@ public class Container extends Component
    */
   protected void addImpl(Component comp, Object constraints, int index)
   {
-    if (index > ncomponents
-        || (index < 0 && index != -1)
-        || comp instanceof Window
-        || (comp instanceof Container
-            && ((Container) comp).isAncestorOf(this)))
-      throw new IllegalArgumentException();
-
-    // Reparent component, and make sure component is instantiated if
-    // we are.
-    if (comp.parent != null)
-      comp.parent.remove(comp);
-    comp.parent = this;
-    if (peer != null)
-      {
-        comp.addNotify();
-
-        if (comp.isLightweight())
-          enableEvents(comp.eventMask);
-      }
-
-    invalidate();
-
-    if (component == null)
-      component = new Component[4]; // FIXME, better initial size?
-
-    // This isn't the most efficient implementation.  We could do less
-    // copying when growing the array.  It probably doesn't matter.
-    if (ncomponents >= component.length)
-      {
-        int nl = component.length * 2;
-        Component[] c = new Component[nl];
-        System.arraycopy(component, 0, c, 0, ncomponents);
-        component = c;
-      }
-    if (index == -1)
-      component[ncomponents++] = comp;
-    else
-      {
-        System.arraycopy(component, index, component, index + 1,
-                          ncomponents - index);
-        component[index] = comp;
-        ++ncomponents;
-      }
-
-    // Notify the layout manager.
-    if (layoutMgr != null)
+    synchronized (getTreeLock ())
       {
-        if (layoutMgr instanceof LayoutManager2)
-          {
-            LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
-            lm2.addLayoutComponent(comp, constraints);
-          }
-        else if (constraints instanceof String)
-          layoutMgr.addLayoutComponent((String) constraints, comp);
-        else
-          layoutMgr.addLayoutComponent(null, comp);
+	if (index > ncomponents
+	    || (index < 0 && index != -1)
+	    || comp instanceof Window
+	    || (comp instanceof Container
+		&& ((Container) comp).isAncestorOf(this)))
+	  throw new IllegalArgumentException();
+
+	// Reparent component, and make sure component is instantiated if
+	// we are.
+	if (comp.parent != null)
+	  comp.parent.remove(comp);
+	comp.parent = this;
+	if (peer != null)
+	  {
+	    comp.addNotify();
+
+	    if (comp.isLightweight())
+	      enableEvents(comp.eventMask);
+	  }
+
+	invalidate();
+
+	if (component == null)
+	  component = new Component[4]; // FIXME, better initial size?
+
+	// This isn't the most efficient implementation.  We could do less
+	// copying when growing the array.  It probably doesn't matter.
+	if (ncomponents >= component.length)
+	  {
+	    int nl = component.length * 2;
+	    Component[] c = new Component[nl];
+	    System.arraycopy(component, 0, c, 0, ncomponents);
+	    component = c;
+	  }
+	if (index == -1)
+	  component[ncomponents++] = comp;
+	else
+	  {
+	    System.arraycopy(component, index, component, index + 1,
+			     ncomponents - index);
+	    component[index] = comp;
+	    ++ncomponents;
+	  }
+
+	// Notify the layout manager.
+	if (layoutMgr != null)
+	  {
+	    if (layoutMgr instanceof LayoutManager2)
+	      {
+		LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
+		lm2.addLayoutComponent(comp, constraints);
+	      }
+	    else if (constraints instanceof String)
+	      layoutMgr.addLayoutComponent((String) constraints, comp);
+	    else
+	      layoutMgr.addLayoutComponent(null, comp);
+	  }
+
+	// Post event to notify of adding the container.
+	ContainerEvent ce = new ContainerEvent(this,
+					       ContainerEvent.COMPONENT_ADDED,
+					       comp);
+	getToolkit().getSystemEventQueue().postEvent(ce);
       }
-
-    // Post event to notify of adding the container.
-    ContainerEvent ce = new ContainerEvent(this,
-                                            ContainerEvent.COMPONENT_ADDED,
-                                            comp);
-    getToolkit().getSystemEventQueue().postEvent(ce);
   }
 
   /**
@@ -332,24 +341,27 @@ public class Container extends Component
    */
   public void remove(int index)
   {
-    Component r = component[index];
+    synchronized (getTreeLock ())
+      {
+	Component r = component[index];
 
-    r.removeNotify();
+	r.removeNotify();
 
-    System.arraycopy(component, index + 1, component, index,
-                      ncomponents - index - 1);
-    component[--ncomponents] = null;
+	System.arraycopy(component, index + 1, component, index,
+			 ncomponents - index - 1);
+	component[--ncomponents] = null;
 
-    invalidate();
+	invalidate();
 
-    if (layoutMgr != null)
-      layoutMgr.removeLayoutComponent(r);
+	if (layoutMgr != null)
+	  layoutMgr.removeLayoutComponent(r);
 
-    // Post event to notify of adding the container.
-    ContainerEvent ce = new ContainerEvent(this,
-                                            ContainerEvent.COMPONENT_REMOVED,
-                                            r);
-    getToolkit().getSystemEventQueue().postEvent(ce);
+	// Post event to notify of adding the container.
+	ContainerEvent ce = new ContainerEvent(this,
+					       ContainerEvent.COMPONENT_REMOVED,
+					       r);
+	getToolkit().getSystemEventQueue().postEvent(ce);
+      }
   }
 
   /**
@@ -359,13 +371,16 @@ public class Container extends Component
    */
   public void remove(Component comp)
   {
-    for (int i = 0; i < ncomponents; ++i)
+    synchronized (getTreeLock ())
       {
-        if (component[i] == comp)
-          {
-            remove(i);
-            break;
-          }
+	for (int i = 0; i < ncomponents; ++i)
+	  {
+	    if (component[i] == comp)
+	      {
+		remove(i);
+		break;
+	      }
+	  }
       }
   }
 
@@ -374,8 +389,11 @@ public class Container extends Component
    */
   public void removeAll()
   {
-    while (ncomponents > 0)
-      remove(0);
+    synchronized (getTreeLock ())
+      {
+	while (ncomponents > 0)
+	  remove(0);
+      }
   }
 
   /**
@@ -433,8 +451,7 @@ public class Container extends Component
    */
   public void validate()
   {
-    // FIXME: use the tree lock?
-    synchronized (this)
+    synchronized (getTreeLock ())
       {
         if (! isValid())
           {
@@ -713,7 +730,8 @@ public class Container extends Component
   {
     if (e instanceof ContainerEvent)
       processContainerEvent((ContainerEvent) e);
-    else super.processEvent(e);
+    else
+      super.processEvent(e);
   }
 
   /**
@@ -764,20 +782,23 @@ public class Container extends Component
    */
   public Component getComponentAt(int x, int y)
   {
-    if (! contains(x, y))
-      return null;
-    for (int i = 0; i < ncomponents; ++i)
+    synchronized (getTreeLock ())
       {
-        // Ignore invisible children...
-        if (!component[i].isVisible())
-          continue;
-
-        int x2 = x - component[i].x;
-        int y2 = y - component[i].y;
-        if (component[i].contains(x2, y2))
-          return component[i];
+	if (! contains(x, y))
+	  return null;
+	for (int i = 0; i < ncomponents; ++i)
+	  {
+	    // Ignore invisible children...
+	    if (!component[i].isVisible())
+	      continue;
+
+	    int x2 = x - component[i].x;
+	    int y2 = y - component[i].y;
+	    if (component[i].contains(x2, y2))
+	      return component[i];
+	  }
+	return this;
       }
-    return this;
   }
 
   /**
@@ -818,31 +839,34 @@ public class Container extends Component
 
   public Component findComponentAt(int x, int y)
   {
-    if (! contains(x, y))
-      return null;
-
-    for (int i = 0; i < ncomponents; ++i)
+    synchronized (getTreeLock ())
       {
-        // Ignore invisible children...
-        if (!component[i].isVisible())
-          continue;
-
-        int x2 = x - component[i].x;
-        int y2 = y - component[i].y;
-        // We don't do the contains() check right away because
-        // findComponentAt would redundantly do it first thing.
-        if (component[i] instanceof Container)
-          {
-            Container k = (Container) component[i];
-            Component r = k.findComponentAt(x2, y2);
-            if (r != null)
-              return r;
-          }
-        else if (component[i].contains(x2, y2))
-          return component[i];
+	if (! contains(x, y))
+	  return null;
+
+	for (int i = 0; i < ncomponents; ++i)
+	  {
+	    // Ignore invisible children...
+	    if (!component[i].isVisible())
+	      continue;
+
+	    int x2 = x - component[i].x;
+	    int y2 = y - component[i].y;
+	    // We don't do the contains() check right away because
+	    // findComponentAt would redundantly do it first thing.
+	    if (component[i] instanceof Container)
+	      {
+		Container k = (Container) component[i];
+		Component r = k.findComponentAt(x2, y2);
+		if (r != null)
+		  return r;
+	      }
+	    else if (component[i].contains(x2, y2))
+	      return component[i];
+	  }
+
+	return this;
       }
-
-    return this;
   }
 
   public Component findComponentAt(Point p)
@@ -868,9 +892,12 @@ public class Container extends Component
    */
   public void removeNotify()
   {
-    for (int i = 0; i < ncomponents; ++i)
-      component[i].removeNotify();
-    super.removeNotify();
+    synchronized (getTreeLock ())
+      {
+	for (int i = 0; i < ncomponents; ++i)
+	  component[i].removeNotify();
+	super.removeNotify();
+      }
   }
 
   /**
@@ -880,17 +907,20 @@ public class Container extends Component
    * @param component The component to test.
    *
    * @return <code>true</code> if this container is an ancestor of the
-   * specified component, <code>false</code>.
+   * specified component, <code>false</code> otherwise.
    */
   public boolean isAncestorOf(Component comp)
   {
-    while (true)
+    synchronized (getTreeLock ())
       {
-        if (comp == null)
-          return false;
-        if (comp == this)
-          return true;
-        comp = comp.getParent();
+	while (true)
+	  {
+	    if (comp == null)
+	      return false;
+	    if (comp == this)
+	      return true;
+	    comp = comp.getParent();
+	  }
       }
   }
 
@@ -918,9 +948,12 @@ public class Container extends Component
    */
   public void list(PrintStream out, int indent)
   {
-    super.list(out, indent);
-    for (int i = 0; i < ncomponents; ++i)
-      component[i].list(out, indent + 2);
+    synchronized (getTreeLock ())
+      {
+	super.list(out, indent);
+	for (int i = 0; i < ncomponents; ++i)
+	  component[i].list(out, indent + 2);
+      }
   }
 
   /**
@@ -932,9 +965,12 @@ public class Container extends Component
    */
   public void list(PrintWriter out, int indent)
   {
-    super.list(out, indent);
-    for (int i = 0; i < ncomponents; ++i)
-      component[i].list(out, indent + 2);
+    synchronized (getTreeLock ())
+      {
+	super.list(out, indent);
+	for (int i = 0; i < ncomponents; ++i)
+	  component[i].list(out, indent + 2);
+      }
   }
 
   public void setFocusTraversalKeys(int id, Set keys)
@@ -1006,16 +1042,17 @@ public class Container extends Component
   private void visitChildren(Graphics gfx, GfxVisitor visitor,
                              boolean lightweightOnly)
   {
-    // FIXME: do locking
-
-    for (int i = 0; i < ncomponents; ++i)
+    synchronized (getTreeLock ())
       {
-        Component comp = component[i];
-        boolean applicable = comp.isVisible()
-          && (comp.isLightweight() || !lightweightOnly);
-
-        if (applicable)
-          visitChild(gfx, visitor, comp);
+	for (int i = 0; i < ncomponents; ++i)
+	  {
+	    Component comp = component[i];
+	    boolean applicable = comp.isVisible()
+	      && (comp.isLightweight() || !lightweightOnly);
+
+	    if (applicable)
+	      visitChild(gfx, visitor, comp);
+	  }
       }
   }
 
@@ -1061,59 +1098,65 @@ public class Container extends Component
   // This is used to implement Component.transferFocus.
   Component findNextFocusComponent(Component child)
   {
-    int start, end;
-    if (child != null)
-      {
-        for (start = 0; start < ncomponents; ++start)
-          {
-            if (component[start] == child)
-              break;
-          }
-        end = start;
-        // This special case lets us be sure to terminate.
-        if (end == 0)
-          end = ncomponents;
-        ++start;
-      }
-    else
-      {
-        start = 0;
-        end = ncomponents;
-      }
-
-    for (int j = start; j != end; ++j)
+    synchronized (getTreeLock ())
       {
-        if (j >= ncomponents)
-          {
-            // The JCL says that we should wrap here.  However, that
-            // seems wrong.  To me it seems that focus order should be
-            // global within in given window.  So instead if we reach
-            // the end we try to look in our parent, if we have one.
-            if (parent != null)
-              return parent.findNextFocusComponent(this);
-            j -= ncomponents;
-          }
-        if (component[j] instanceof Container)
-          {
-            Component c = component[j];
-            c = c.findNextFocusComponent(null);
-            if (c != null)
-              return c;
-          }
-        else if (component[j].isFocusTraversable())
-          return component[j];
+	int start, end;
+	if (child != null)
+	  {
+	    for (start = 0; start < ncomponents; ++start)
+	      {
+		if (component[start] == child)
+		  break;
+	      }
+	    end = start;
+	    // This special case lets us be sure to terminate.
+	    if (end == 0)
+	      end = ncomponents;
+	    ++start;
+	  }
+	else
+	  {
+	    start = 0;
+	    end = ncomponents;
+	  }
+
+	for (int j = start; j != end; ++j)
+	  {
+	    if (j >= ncomponents)
+	      {
+		// The JCL says that we should wrap here.  However, that
+		// seems wrong.  To me it seems that focus order should be
+		// global within in given window.  So instead if we reach
+		// the end we try to look in our parent, if we have one.
+		if (parent != null)
+		  return parent.findNextFocusComponent(this);
+		j -= ncomponents;
+	      }
+	    if (component[j] instanceof Container)
+	      {
+		Component c = component[j];
+		c = c.findNextFocusComponent(null);
+		if (c != null)
+		  return c;
+	      }
+	    else if (component[j].isFocusTraversable())
+	      return component[j];
+	  }
+
+	return null;
       }
-
-    return null;
   }
 
   private void addNotifyContainerChildren()
   {
-    for (int i = ncomponents;  --i >= 0; )
+    synchronized (getTreeLock ())
       {
-        component[i].addNotify();
-        if (component[i].isLightweight())
-          enableEvents(component[i].eventMask);
+	for (int i = ncomponents;  --i >= 0; )
+	  {
+	    component[i].addNotify();
+	    if (component[i].isLightweight())
+	      enableEvents(component[i].eventMask);
+	  }
       }
   }
 
@@ -1190,12 +1233,15 @@ public class Container extends Component
      */
     public int getAccessibleChildrenCount()
     {
-      int count = 0;
-      int i = component == null ? 0 : component.length;
-      while (--i >= 0)
-        if (component[i] instanceof Accessible)
-          count++;
-      return count;
+      synchronized (getTreeLock ())
+	{
+	  int count = 0;
+	  int i = component == null ? 0 : component.length;
+	  while (--i >= 0)
+	    if (component[i] instanceof Accessible)
+	      count++;
+	  return count;
+	}
     }
 
     /**
@@ -1206,15 +1252,18 @@ public class Container extends Component
      */
     public Accessible getAccessibleChild(int i)
     {
-      if (component == null)
-        return null;
-      int index = -1;
-      while (i >= 0 && ++index < component.length)
-        if (component[index] instanceof Accessible)
-          i--;
-      if (i < 0)
-        return (Accessible) component[index];
-      return null;
+      synchronized (getTreeLock ())
+	{
+	  if (component == null)
+	    return null;
+	  int index = -1;
+	  while (i >= 0 && ++index < component.length)
+	    if (component[index] instanceof Accessible)
+	      i--;
+	  if (i < 0)
+	    return (Accessible) component[index];
+	  return null;
+	}
     }
 
     /**
-- 
cgit v1.1