This is the mail archive of the kawa@sources.redhat.com mailing list for the Kawa project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

another thread safety patch


Here's another patch that fixes one of the thread safety issues.
Specifically, when multiple threads try and load to the same procedure
at the same time when we were getting a NPE.

Regards,
Chris Dean


Index: kawa/lang/AutoloadProcedure.java
===================================================================
RCS file: /cvs/kawa/kawa/kawa/lang/AutoloadProcedure.java,v
retrieving revision 1.22
diff -u -w -r1.22 AutoloadProcedure.java
--- kawa/lang/AutoloadProcedure.java	21 Apr 2003 08:27:44 -0000	1.22
+++ kawa/lang/AutoloadProcedure.java	16 Jul 2004 05:20:45 -0000
@@ -72,39 +72,40 @@
   }
 
   /** Load the class named in className. */
-  void load ()
+  synchronized void load ()
   {
     Environment env = this.env != null ? this.env : Environment.getCurrent();
     String name = this.getName();
+    Procedure new_proc = null;
     try
       {
-	loaded = (Procedure) Class.forName (className).newInstance ();
-	if (loaded == this)
+	new_proc = (Procedure) Class.forName (className).newInstance ();
+	if (new_proc == this)
 	  throw_error("circularity detected");
-	if (loaded instanceof ModuleBody)
+	if (new_proc instanceof ModuleBody)
 	  {
-	    gnu.kawa.reflect.ClassMemberConstraint.defineAll(loaded, env);
-	    ((ModuleBody)loaded).run();
+	    gnu.kawa.reflect.ClassMemberConstraint.defineAll(new_proc, env);
+	    ((ModuleBody)new_proc).run();
 	    Object value = env.getSymbol(name).getProcedure();
 	    if (value == null
 		|| !(value instanceof Procedure))
 	      throw_error
 		("invalid ModuleBody class - does not define " + name);
-	    loaded = (Procedure) value;
+	    new_proc = (Procedure) value;
 	  }
 	else if (name != null)
 	  {
+	    if (new_proc.getName() == null)
+	      new_proc.setName(name);
 	    try
 	      {
 		// Should use something like isFunctionBound FIXME
 		if (env.getFunction(name) == this)
-		  env.putFunction(name, loaded);
+		  env.putFunction(name, new_proc);
 	      }
 	    catch (UnboundSymbol ex)
 	      {
 	      }
-	    if (loaded.getName() == null)
-	      loaded.setName(name);
 	  }
       }
     catch (ClassNotFoundException ex)
@@ -113,6 +114,8 @@
       { throw_error ("failed to instantiate class "); }
     catch (IllegalAccessException ex)
       { throw_error ("illegal access in class "); }
+
+    loaded = new_proc;
   }
 
   public Procedure getLoaded ()


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]