SF.net SVN: jython:[7117] trunk/jython/src/org/python/core/ ThreadStateMapping.java

[email protected] Thu, 09 Sep 2010 16:28:06 +0000
Newsgroups gmane.comp.lang.jython.cvs
Message-ID <[email protected]>
Revision: 7117
          http://jython.svn.sourceforge.net/jython/?rev=7117&view=rev
Author:   zyasoft
Date:     2010-09-09 16:28:05 +0000 (Thu, 09 Sep 2010)

Log Message:
-----------
More work on #1327. ThreadStateMapping has a bad implementation of
double-checked locking. The aspect of how it interacts with thread
locals makes it subtle, so I'm going to make the whole lookup
synchronized.

Going forward, we should reconsider the use of ThreadLocal to manage
the ThreadState mapping. Synchronizing this loses any performance from
using ThreadLocal, but we have to deal with the other issues seen
here. Also, I think we should expose the registration process more, in
the case of usage with thread pools.

Modified Paths:
--------------
    trunk/jython/src/org/python/core/ThreadStateMapping.java

Modified: trunk/jython/src/org/python/core/ThreadStateMapping.java
===================================================================
--- trunk/jython/src/org/python/core/ThreadStateMapping.java	2010-09-08 20:48:17 UTC (rev 7116)
+++ trunk/jython/src/org/python/core/ThreadStateMapping.java	2010-09-09 16:28:05 UTC (rev 7117)
@@ -9,33 +9,22 @@
                 }
             };
 
-    public ThreadState getThreadState(PySystemState newSystemState) {
+    public synchronized ThreadState getThreadState(PySystemState newSystemState) {
+        ThreadState[] threadLocal = cachedThreadState.get();
+        if (threadLocal[0] != null)
+            return threadLocal[0];
 
-        // usual double checked locking pattern
-        ThreadState ts = cachedThreadState.get()[0];
-
-        if (ts != null) {
-            return ts;
-        }
-
-        synchronized(this) {
-            ThreadState[] threadLocal = cachedThreadState.get();
-            if(threadLocal[0] != null)
-                return threadLocal[0];
-
-            Thread t = Thread.currentThread();
-            if (newSystemState == null) {
-                Py.writeDebug("threadstate", "no current system state");
-                if (Py.defaultSystemState == null) {
-                    PySystemState.initialize();
-                }
-                newSystemState = Py.defaultSystemState;
+        Thread t = Thread.currentThread();
+        if (newSystemState == null) {
+            Py.writeDebug("threadstate", "no current system state");
+            if (Py.defaultSystemState == null) {
+                PySystemState.initialize();
             }
-
-            ts = new ThreadState(t, newSystemState);
-
-            newSystemState.registerThreadState(threadLocal, ts);
-            return ts;
+            newSystemState = Py.defaultSystemState;
         }
+
+        ThreadState ts = new ThreadState(t, newSystemState);
+        newSystemState.registerThreadState(threadLocal, ts);
+        return ts;
     }
 }


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd