[Jython-checkins] jython: Remove last direct ThreadLocal to Jython runtime classes

jim.baker jython-checkins at python.org
Sat Jun 28 03:28:16 CEST 2014


http://hg.python.org/jython/rev/68912d04f6f5
changeset:   7326:68912d04f6f5
user:        Jim Baker <jim.baker at rackspace.com>
date:        Tue Jun 24 23:47:21 2014 -0600
summary:
  Remove last direct ThreadLocal to Jython runtime classes

files:
  src/org/python/util/PythonInterpreter.java |  37 ++++++---
  1 files changed, 24 insertions(+), 13 deletions(-)


diff --git a/src/org/python/util/PythonInterpreter.java b/src/org/python/util/PythonInterpreter.java
--- a/src/org/python/util/PythonInterpreter.java
+++ b/src/org/python/util/PythonInterpreter.java
@@ -31,7 +31,14 @@
     protected PySystemState systemState;
     PyObject globals;
 
-    protected ThreadLocal<PyObject> threadLocals;
+    protected final boolean useThreadLocalState;
+
+    protected static ThreadLocal<Object[]> threadLocals = new ThreadLocal<Object[]>() {
+        @Override
+        protected Object[] initialValue() {
+            return new Object[1];
+        }
+    };
 
     protected CompilerFlags cflags = new CompilerFlags();
 
@@ -103,9 +110,8 @@
         this.systemState = systemState;
         setSystemState();
 
-        if (useThreadLocalState) {
-            threadLocals = new ThreadLocal<PyObject>();
-        } else {
+        this.useThreadLocalState = useThreadLocalState;
+        if (!useThreadLocalState) {
             PyModule module = new PyModule("__main__", dict);
             systemState.modules.__setitem__("__main__", module);
         }
@@ -263,20 +269,24 @@
 
 
     public PyObject getLocals() {
-        if (threadLocals == null)
+        if (!useThreadLocalState) {
             return globals;
-
-        PyObject locals = threadLocals.get();
-        if (locals != null)
-            return locals;
-        return globals;
+        } else {
+            PyObject locals = (PyObject) threadLocals.get()[0];
+            if (locals != null) {
+                return locals;
+            }
+            return globals;
+        }
     }
 
     public void setLocals(PyObject d) {
-        if (threadLocals == null)
+        if (!useThreadLocalState) {
             globals = d;
-        else
-            threadLocals.set(d);
+        }
+        else {
+            threadLocals.get()[0] = d;
+        }
     }
 
     /**
@@ -352,6 +362,7 @@
         } catch (PyException pye) {
             // fall through
         }
+        threadLocals.remove();
         sys.cleanup();
     }
 }

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list