[pypy-svn] r45899 - pypy/branch/pypy-more-rtti-inprogress/module/thread

arigo at codespeak.net arigo at codespeak.net
Tue Aug 21 16:57:52 CEST 2007


Author: arigo
Date: Tue Aug 21 16:57:52 2007
New Revision: 45899

Modified:
   pypy/branch/pypy-more-rtti-inprogress/module/thread/__init__.py
   pypy/branch/pypy-more-rtti-inprogress/module/thread/gil.py
   pypy/branch/pypy-more-rtti-inprogress/module/thread/os_thread.py
Log:
More subtle: we can't change space.threadlocals at run-time because the
space is frozen.  But we can put a GILThreadLocals with an initial
GIL=None.


Modified: pypy/branch/pypy-more-rtti-inprogress/module/thread/__init__.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/module/thread/__init__.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/module/thread/__init__.py	Tue Aug 21 16:57:52 2007
@@ -19,3 +19,11 @@
         '_local':                 'os_local.getlocaltype(space)',
         '_please_provide_import_lock': '(space.w_True)',   # for imp.py
     }
+
+    def __init__(self, space, *args):
+        "NOT_RPYTHON: patches space.threadlocals to use real threadlocals"
+        from pypy.module.thread import gil
+        MixedModule.__init__(self, space, *args)
+        prev = space.threadlocals.getvalue()
+        space.threadlocals = gil.GILThreadLocals()
+        space.threadlocals.setvalue(prev)

Modified: pypy/branch/pypy-more-rtti-inprogress/module/thread/gil.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/module/thread/gil.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/module/thread/gil.py	Tue Aug 21 16:57:52 2007
@@ -13,10 +13,18 @@
 
 class GILThreadLocals(OSThreadLocals):
     """A version of OSThreadLocals that enforces a GIL."""
+    GIL = None
 
-    def __init__(self):
-        OSThreadLocals.__init__(self)
-        self.GIL = thread.allocate_lock()
+    def setup_threads(self, space):
+        """Enable threads in the object space, if they haven't already been."""
+        if self.GIL is None:
+            self.GIL = thread.allocate_lock()
+            self.enter_thread(space)   # setup the main thread
+            # add the GIL-releasing callback as an action on the space
+            space.pending_actions.append(GILReleaseAction(self))
+            return True
+        else:
+            return False      # already set up
 
     def enter_thread(self, space):
         "Notification that the current thread is just starting: grab the GIL."

Modified: pypy/branch/pypy-more-rtti-inprogress/module/thread/os_thread.py
==============================================================================
--- pypy/branch/pypy-more-rtti-inprogress/module/thread/os_thread.py	(original)
+++ pypy/branch/pypy-more-rtti-inprogress/module/thread/os_thread.py	Tue Aug 21 16:57:52 2007
@@ -50,21 +50,13 @@
 
 
 def setup_threads(space):
-    """Enable threads in the object space, if they haven't already been."""
-    from pypy.module.thread.gil import GILThreadLocals, GILReleaseAction
-    from pypy.module.__builtin__.importing import importhook
-    if not isinstance(space.threadlocals, GILThreadLocals):
-        # patches space.threadlocals to use real threadlocals
-        prev = space.threadlocals.getvalue()
-        space.threadlocals = GILThreadLocals()
-        space.threadlocals.setvalue(prev)
-        space.threadlocals.enter_thread(space)   # setup the main thread
-        # add the GIL-releasing callback as an action on the space
-        space.pending_actions.append(GILReleaseAction(space.threadlocals))
+    if space.threadlocals.setup_threads(space):
         # the import lock is in imp.py, which registers a custom import
         # hook with this lock.
+        from pypy.module.__builtin__.importing import importhook
         importhook(space, 'imp')
 
+
 def start_new_thread(space, w_callable, w_args, w_kwargs=NoneNotWrapped):
     """Start a new thread and return its identifier.  The thread will call the
 function with positional arguments from the tuple args and keyword arguments



More information about the Pypy-commit mailing list