Author: Armin Rigo <[email protected]>
Branch: fast-gil
Changeset: r72197:bbab1875b9e5
Date: 2014-06-24 18:04 +0200
http://bitbucket.org/pypy/pypy/changeset/bbab1875b9e5/

Log:    More fixes

diff --git a/pypy/module/thread/gil.py b/pypy/module/thread/gil.py
--- a/pypy/module/thread/gil.py
+++ b/pypy/module/thread/gil.py
@@ -7,7 +7,7 @@
 # all but one will be blocked.  The other threads get a chance to run
 # from time to time, using the periodic action GILReleaseAction.
 
-from rpython.rlib import rthread
+from rpython.rlib import rthread, rgil
 from pypy.module.thread.error import wrap_thread_error
 from pypy.interpreter.executioncontext import PeriodicAsyncAction
 from pypy.module.thread.threadlocals import OSThreadLocals
@@ -25,8 +25,7 @@
                                                   use_bytecode_counter=True)
 
     def _initialize_gil(self, space):
-        if not rthread.gil_allocate():
-            raise wrap_thread_error(space, "can't allocate GIL")
+        rgil.gil_allocate()
 
     def setup_threads(self, space):
         """Enable threads in the object space, if they haven't already been."""
@@ -72,14 +71,14 @@
     # this function must not raise, in such a way that the exception
     # transformer knows that it cannot raise!
     e = get_errno()
-    rthread.gil_release()
+    rgil.gil_release()
     set_errno(e)
 before_external_call._gctransformer_hint_cannot_collect_ = True
 before_external_call._dont_reach_me_in_del_ = True
 
 def after_external_call():
     e = get_errno()
-    rthread.gil_acquire()
+    rgil.gil_acquire()
     rthread.gc_thread_run()
     after_thread_switch()
     set_errno(e)
@@ -97,7 +96,7 @@
     # explicitly release the gil, in a way that tries to give more
     # priority to other threads (as opposed to continuing to run in
     # the same thread).
-    if rthread.gil_yield_thread():
+    if rgil.gil_yield_thread():
         rthread.gc_thread_run()
         after_thread_switch()
 do_yield_thread._gctransformer_hint_close_stack_ = True
diff --git a/rpython/rlib/rgil.py b/rpython/rlib/rgil.py
--- a/rpython/rlib/rgil.py
+++ b/rpython/rlib/rgil.py
@@ -11,13 +11,17 @@
     includes = ['src/thread.h'],
     separate_module_files = [translator_c_dir / 'src' / 'thread.c'],
     include_dirs = [translator_c_dir],
-    export_symbols = ['RPyGilYieldThread', 'RPyGilRelease',
+    export_symbols = ['RPyGilAllocate', 'RPyGilYieldThread', 'RPyGilRelease',
                       'RPyGilAcquire', 'RPyFetchFastGil'])
 
 llexternal = rffi.llexternal
 
 
-gil_yield_thread  = llexternal('RPyGilYieldThread', [], lltype.Void,
+gil_allocate      = llexternal('RPyGilAllocate', [], lltype.Void,
+                               _nowrapper=True, sandboxsafe=True,
+                               compilation_info=eci)
+
+gil_yield_thread  = llexternal('RPyGilYieldThread', [], lltype.Signed,
                                _nowrapper=True, sandboxsafe=True,
                                compilation_info=eci)
 
diff --git a/rpython/rlib/test/test_rthread.py 
b/rpython/rlib/test/test_rthread.py
--- a/rpython/rlib/test/test_rthread.py
+++ b/rpython/rlib/test/test_rthread.py
@@ -82,6 +82,7 @@
 
     def test_gc_locking(self):
         import time
+        from rpython.rlib.objectmodel import invoke_around_extcall
         from rpython.rlib.debug import ll_assert
 
         class State:
diff --git a/rpython/translator/c/src/thread.h 
b/rpython/translator/c/src/thread.h
--- a/rpython/translator/c/src/thread.h
+++ b/rpython/translator/c/src/thread.h
@@ -24,8 +24,8 @@
 
 #endif /* !_WIN32 */
 
-
-void RPyGilYieldThread(void);
+void RPyGilAllocate(void);
+long RPyGilYieldThread(void);
 void RPyGilAcquire(void);
 
 #ifdef PYPY_USE_ASMGCC
diff --git a/rpython/translator/c/src/thread_pthread.c 
b/rpython/translator/c/src/thread_pthread.c
--- a/rpython/translator/c/src/thread_pthread.c
+++ b/rpython/translator/c/src/thread_pthread.c
@@ -522,19 +522,16 @@
 long rpy_fastgil = 1;
 static pthread_mutex_t mutex_gil_stealer;
 static pthread_mutex_t mutex_gil;
-static pthread_once_t mutex_gil_once = PTHREAD_ONCE_INIT;
+long rpy_lock_ready = 0;
 
-static void init_mutex_gil(void)
+void RPyGilAllocate(void)
 {
+    assert(RPY_FASTGIL_LOCKED(rpy_fastgil));
     ASSERT_STATUS(pthread_mutex_init(&mutex_gil_stealer,
                                      pthread_mutexattr_default));
     ASSERT_STATUS(pthread_mutex_init(&mutex_gil, pthread_mutexattr_default));
     ASSERT_STATUS(pthread_mutex_lock(&mutex_gil));
-}
-
-static inline void prepare_mutexes(void)
-{
-    pthread_once(&mutex_gil_once, &init_mutex_gil);
+    rpy_lock_ready = 1;
 }
 
 static inline void timespec_add(struct timespec *t, long incr)
@@ -567,7 +564,7 @@
            first-in-first-out order, this will nicely give the threads
            a round-robin chance.
         */
-        prepare_mutexes();
+        assert(rpy_lock_ready);
         ASSERT_STATUS(pthread_mutex_lock(&mutex_gil_stealer));
 
         /* We are now the stealer thread.  Steals! */
@@ -638,13 +635,14 @@
 }
 */
 
-void RPyGilYieldThread(void)
+long RPyGilYieldThread(void)
 {
     assert(RPY_FASTGIL_LOCKED(rpy_fastgil));
+    if (!rpy_lock_ready)
+        return 0;
 
     /* Explicitly release the 'mutex_gil'.
      */
-    prepare_mutexes();
     ASSERT_STATUS(pthread_mutex_unlock(&mutex_gil));
 
     /* Now nobody has got the GIL, because 'mutex_gil' is released (but
@@ -654,4 +652,5 @@
        its pthread_mutex_lock() and pthread_mutex_timedlock() now.
      */
     RPyGilAcquire();
+    return 1;
 }
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to