• Kirill Smelkov's avatar
    golang: Expose Sema and Mutex as public Python and Cython/nogil API · 34b7a1f4
    Kirill Smelkov authored
    Until now libgolang had semaphore and mutex functionality, but
    implemented only internally and not exposed to users. Since for its
    pinner thread wendelin.core v2 needs not only nogil channels, but also
    nogil mutexes, timers and so on, it is now time to start providing that.
    
    We start with mutexes:
    
    - Expose Mutex from insides of libgolang.cpp to public API in
      libgolang.h . We actually expose both Mutex and Sema because
      semaphores are sometimes also useful for low-level synchronization,
      and because it is easier to export Mutex and Sema both at the same time.
    
    - Provide pyx/nogil API to those as sync.Mutex and sync.Sema.
    
    - Provide corresponding python wrappers.
    
    - Add Pyx-level test for semaphore wakeup when wakeup is done not by the
      same thread which did the original acquire. This is the test that was
      promised in 5142460d (libgolang/thread: Add links to upstream
      PyThread_release_lock bug), and it used to corrupt memory and deadlock
      on macOS due to CPython & PyPy runtime bugs:
    
        https://bugs.python.org/issue38106
          -> https://github.com/python/cpython/pull/16047
        https://bitbucket.org/pypy/pypy/issues/3072
    
    Note about python wrappers: At present, due to
    https://github.com/cython/cython/issues/3165, C-level panic is not
    properly translated into Py-level exception in (Py)Sema/Mutex constructors.
    This should not pose a real problem in practice though.
    34b7a1f4
sync_test.py 6.39 KB