pipeline: give Mutex and ReMutex more Pythonic semantics

This allows using mutices in with-blocks and wraps up the functionality of acquire() and try_acquire() into a single acquire(blocking=True).

Furthermore, the GIL is no longer released in cases of no contention.
This commit is contained in:
rdb
2019-05-12 19:53:27 +02:00
parent 2e9bd0f241
commit c4a01ac564
12 changed files with 235 additions and 29 deletions

View File

@@ -26,13 +26,12 @@ def test_cvar_notify_locked():
m = Mutex()
cv = ConditionVarFull(m)
m.acquire()
cv.notify()
m.release()
with m:
cv.notify()
with m:
cv.notify_all()
m.acquire()
cv.notify_all()
m.release()
del cv

View File

@@ -2,6 +2,7 @@ from panda3d.core import Mutex, ReMutex
from panda3d import core
from random import random
import pytest
import sys
def test_mutex_acquire_release():
@@ -34,6 +35,19 @@ def test_mutex_try_acquire():
m.release()
def test_mutex_with():
m = Mutex()
rc = sys.getrefcount(m)
with m:
assert m.debug_is_locked()
with m:
assert m.debug_is_locked()
assert rc == sys.getrefcount(m)
@pytest.mark.skipif(not core.Thread.is_threading_supported(),
reason="Threading support disabled")
def test_mutex_contention():
@@ -124,3 +138,15 @@ def test_remutex_try_acquire():
m.release()
m.release()
def test_remutex_with():
m = ReMutex()
rc = sys.getrefcount(m)
with m:
assert m.debug_is_locked()
with m:
assert m.debug_is_locked()
assert m.debug_is_locked()
assert rc == sys.getrefcount(m)