[pypy-commit] pypy fast-gil: Partial revert of some previous changes in this branch: trying to

arigo noreply at buildbot.pypy.org
Tue Jun 24 15:42:05 CEST 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: fast-gil
Changeset: r72189:b349a2e4c0b5
Date: 2014-06-24 14:14 +0200
http://bitbucket.org/pypy/pypy/changeset/b349a2e4c0b5/

Log:	Partial revert of some previous changes in this branch: trying to
	minimize the changes per branch

diff --git a/pypy/goal/targetpypystandalone.py b/pypy/goal/targetpypystandalone.py
--- a/pypy/goal/targetpypystandalone.py
+++ b/pypy/goal/targetpypystandalone.py
@@ -121,21 +121,25 @@
 
     @entrypoint('main', [rffi.CCHARP], c_name='pypy_execute_source')
     def pypy_execute_source(ll_source):
-        rffi.aroundstate.enter_callback()
+        after = rffi.aroundstate.after
+        if after: after()
         source = rffi.charp2str(ll_source)
         res = _pypy_execute_source(source)
-        rffi.aroundstate.leave_callback()
+        before = rffi.aroundstate.before
+        if before: before()
         return rffi.cast(rffi.INT, res)
 
     @entrypoint('main', [rffi.CCHARP, lltype.Signed],
                 c_name='pypy_execute_source_ptr')
     def pypy_execute_source_ptr(ll_source, ll_ptr):
-        rffi.aroundstate.enter_callback()
+        after = rffi.aroundstate.after
+        if after: after()
         source = rffi.charp2str(ll_source)
         space.setitem(w_globals, space.wrap('c_argument'),
                       space.wrap(ll_ptr))
         res = _pypy_execute_source(source)
-        rffi.aroundstate.leave_callback()
+        before = rffi.aroundstate.before
+        if before: before()
         return rffi.cast(rffi.INT, res)        
 
     @entrypoint('main', [], c_name='pypy_init_threads')
@@ -143,7 +147,8 @@
         if not space.config.objspace.usemodules.thread:
             return
         os_thread.setup_threads(space)
-        rffi.aroundstate.leave_callback()
+        before = rffi.aroundstate.before
+        if before: before()
 
     @entrypoint('main', [], c_name='pypy_thread_attach')
     def pypy_thread_attach():
@@ -154,7 +159,8 @@
         rthread.gc_thread_start()
         os_thread.bootstrapper.nbthreads += 1
         os_thread.bootstrapper.release()
-        rffi.aroundstate.leave_callback()
+        before = rffi.aroundstate.before
+        if before: before()
 
     w_globals = space.newdict()
     space.setitem(w_globals, space.wrap('__builtins__'),
diff --git a/pypy/module/cpyext/pystate.py b/pypy/module/cpyext/pystate.py
--- a/pypy/module/cpyext/pystate.py
+++ b/pypy/module/cpyext/pystate.py
@@ -29,7 +29,8 @@
     state = space.fromcache(InterpreterState)
     tstate = state.swap_thread_state(
         space, lltype.nullptr(PyThreadState.TO))
-    rffi.aroundstate.leave_callback()
+    if rffi.aroundstate.before:
+        rffi.aroundstate.before()
     return tstate
 
 @cpython_api([PyThreadState], lltype.Void)
@@ -39,7 +40,8 @@
     NULL.  If the lock has been created, the current thread must not have
     acquired it, otherwise deadlock ensues.  (This function is available even
     when thread support is disabled at compile time.)"""
-    rffi.aroundstate.enter_callback()
+    if rffi.aroundstate.after:
+        rffi.aroundstate.after()
     state = space.fromcache(InterpreterState)
     state.swap_thread_state(space, tstate)
 
@@ -186,7 +188,9 @@
     tstate, which should not be NULL.  The lock must have been created earlier.
     If this thread already has the lock, deadlock ensues.  This function is not
     available when thread support is disabled at compile time."""
-    rffi.aroundstate.enter_callback()
+    if rffi.aroundstate.after:
+        # After external call is before entering Python
+        rffi.aroundstate.after()
 
 @cpython_api([PyThreadState], lltype.Void)
 def PyEval_ReleaseThread(space, tstate):
@@ -196,7 +200,9 @@
     that it represents the current thread state --- if it isn't, a fatal error is
     reported. This function is not available when thread support is disabled at
     compile time."""
-    rffi.aroundstate.leave_callback()
+    if rffi.aroundstate.before:
+        # Before external call is after running Python
+        rffi.aroundstate.before()
 
 PyGILState_STATE = rffi.COpaquePtr('PyGILState_STATE',
                                    typedef='PyGILState_STATE',
@@ -204,12 +210,16 @@
 
 @cpython_api([], PyGILState_STATE, error=CANNOT_FAIL)
 def PyGILState_Ensure(space):
-    rffi.aroundstate.enter_callback()
+    if rffi.aroundstate.after:
+        # After external call is before entering Python
+        rffi.aroundstate.after()
     return 0
 
 @cpython_api([PyGILState_STATE], lltype.Void)
 def PyGILState_Release(space, state):
-    rffi.aroundstate.leave_callback()
+    if rffi.aroundstate.before:
+        # Before external call is after running Python
+        rffi.aroundstate.before()
 
 @cpython_api([], PyInterpreterState, error=CANNOT_FAIL)
 def PyInterpreterState_Head(space):
@@ -233,12 +243,12 @@
         raise NoThreads
     # PyThreadState_Get will allocate a new execution context,
     # we need to protect gc and other globals with the GIL.
-    rffi.aroundstate.enter_callback()
+    rffi.aroundstate.after()
     try:
         rthread.gc_thread_start()
         return PyThreadState_Get(space)
     finally:
-        rffi.aroundstate.leave_callback()
+        rffi.aroundstate.before()
 
 @cpython_api([PyThreadState], lltype.Void)
 def PyThreadState_Clear(space, tstate):
diff --git a/rpython/rlib/objectmodel.py b/rpython/rlib/objectmodel.py
--- a/rpython/rlib/objectmodel.py
+++ b/rpython/rlib/objectmodel.py
@@ -575,22 +575,18 @@
     raise TypeError, "hlinvoke is meant to be rtyped and not called direclty"
 
 def invoke_around_extcall(before, after):
-    """Call before() before any external function call, and after() after.
-    ONLY FOR TESTS!
-    """
-    # NOTE: the hooks are cleared during translation!
+    # NOTE: the hooks are cleared during translation!  To be effective
+    # in a compiled program they must be set at run-time.
     from rpython.rtyper.lltypesystem import rffi
     rffi.aroundstate.before = before
     rffi.aroundstate.after = after
-    rffi.aroundstate.enter_callback = after
-    rffi.aroundstate.leave_callback = before
-    # force the annotation of before() and after()
-    from rpython.rlib.nonconst import NonConstant
-    if NonConstant(0):
-        if before:
-            before()
-        if after:
-            after()
+    # the 'aroundstate' contains regular function and not ll pointers to them,
+    # but let's call llhelper() anyway to force their annotation
+    from rpython.rtyper.annlowlevel import llhelper
+    if before is not None:
+        llhelper(rffi.AroundFnPtr, before)
+    if after is not None:
+        llhelper(rffi.AroundFnPtr, after)
 
 def is_in_callback():
     from rpython.rtyper.lltypesystem import rffi
diff --git a/rpython/rtyper/lltypesystem/rffi.py b/rpython/rtyper/lltypesystem/rffi.py
--- a/rpython/rtyper/lltypesystem/rffi.py
+++ b/rpython/rtyper/lltypesystem/rffi.py
@@ -154,7 +154,7 @@
 
         argnames = ', '.join(['a%d' % i for i in range(len(args))])
         source = py.code.Source("""
-            def call_external_function(%(argnames)s)e
+            def call_external_function(%(argnames)s):
                 before = aroundstate.before
                 if before: before()
                 # NB. it is essential that no exception checking occurs here!
@@ -218,8 +218,13 @@
                     freeme = arg
             elif _isfunctype(TARGET) and not _isllptr(arg):
                 # XXX pass additional arguments
-                arg = llhelper(TARGET, _make_wrapper_for(
-                    TARGET, arg, invoke_around_handlers, callbackholder))
+                if invoke_around_handlers:
+                    arg = llhelper(TARGET, _make_wrapper_for(TARGET, arg,
+                                                             callbackholder,
+                                                             aroundstate))
+                else:
+                    arg = llhelper(TARGET, _make_wrapper_for(TARGET, arg,
+                                                             callbackholder))
             else:
                 SOURCE = lltype.typeOf(arg)
                 if SOURCE != TARGET:
@@ -258,8 +263,7 @@
     def __init__(self):
         self.callbacks = {}
 
-def _make_wrapper_for(TP, callable, invoke_around_handlers=True,
-                      callbackholder=None):
+def _make_wrapper_for(TP, callable, callbackholder=None, aroundstate=None):
     """ Function creating wrappers for callbacks. Note that this is
     cheating as we assume constant callbacks and we just memoize wrappers
     """
@@ -275,8 +279,10 @@
     args = ', '.join(['a%d' % i for i in range(len(TP.TO.ARGS))])
     source = py.code.Source(r"""
         def wrapper(%(args)s):    # no *args - no GIL for mallocing the tuple
-            if invoke_around_handlers:
-                aroundstate.enter_callback()
+            if aroundstate is not None:
+                after = aroundstate.after
+                if after:
+                    after()
             # from now on we hold the GIL
             stackcounter.stacks_counter += 1
             llop.gc_stack_bottom(lltype.Void)   # marker for trackgcroot.py
@@ -291,11 +297,13 @@
                     traceback.print_exc()
                 result = errorcode
             stackcounter.stacks_counter -= 1
-            if invoke_around_handlers:
-                aroundstate.leave_callback()
+            if aroundstate is not None:
+                before = aroundstate.before
+                if before:
+                    before()
             # here we don't hold the GIL any more. As in the wrapper() produced
             # by llexternal, it is essential that no exception checking occurs
-            # after the call to leave_callback().
+            # after the call to before().
             return result
     """ % locals())
     miniglobals = locals().copy()
@@ -303,30 +311,16 @@
     miniglobals['os'] = os
     miniglobals['we_are_translated'] = we_are_translated
     miniglobals['stackcounter'] = stackcounter
-    miniglobals['aroundstate'] = aroundstate
     exec source.compile() in miniglobals
     return miniglobals['wrapper']
 _make_wrapper_for._annspecialcase_ = 'specialize:memo'
 
-
-def _standard_gil_acquire():
-    if we_are_translated():
-        from rpython.rlib import rgil
-        rgil.gil_acquire()
-
-def _standard_gil_release():
-    if we_are_translated():
-        from rpython.rlib import rgil
-        rgil.gil_release()
-
 AroundFnPtr = lltype.Ptr(lltype.FuncType([], lltype.Void))
 
 class AroundState:
     def _cleanup_(self):
-        self.before         = _standard_gil_release
-        self.after          = _standard_gil_acquire
-        self.enter_callback = _standard_gil_acquire
-        self.leave_calback  = _standard_gil_release
+        self.before = None        # or a regular RPython function
+        self.after = None         # or a regular RPython function
 aroundstate = AroundState()
 aroundstate._cleanup_()
 
diff --git a/rpython/translator/c/src/thread.c b/rpython/translator/c/src/thread.c
--- a/rpython/translator/c/src/thread.c
+++ b/rpython/translator/c/src/thread.c
@@ -9,17 +9,9 @@
 #include "common_header.h"
 #endif
 
-
-/* More ugliness follows... */
-#ifdef PYPY_USE_ASMGCC
-#include "common_header.h"
-#include "structdef.h"
-#include "forwarddecl.h"
-#endif
-
-
 #ifdef _WIN32
 #include "src/thread_nt.c"
 #else
 #include "src/thread_pthread.c"
 #endif
+


More information about the pypy-commit mailing list