[pypy-commit] pypy default: add a method to check if a lock is acquired without releasing the gil

plan_rich pypy.commits at gmail.com
Thu Oct 13 09:33:34 EDT 2016


Author: Richard Plangger <planrichi at gmail.com>
Branch: 
Changeset: r87752:b2587367f3c7
Date: 2016-10-13 15:32 +0200
http://bitbucket.org/pypy/pypy/changeset/b2587367f3c7/

Log:	add a method to check if a lock is acquired without releasing the
	gil

diff --git a/rpython/rlib/rthread.py b/rpython/rlib/rthread.py
--- a/rpython/rlib/rthread.py
+++ b/rpython/rlib/rthread.py
@@ -129,6 +129,9 @@
     def acquire(self, flag):
         return True
 
+    def is_acquired(self):
+        return False
+
     def release(self):
         pass
 
@@ -164,6 +167,15 @@
             res = rffi.cast(lltype.Signed, res)
             return bool(res)
 
+    def is_acquired(self):
+        """ check if the lock is acquired (does not release the GIL) """
+        res = c_thread_acquirelock_timed_NOAUTO(
+            self._lock,
+            rffi.cast(rffi.LONGLONG, 0),
+            rffi.cast(rffi.INT, 0))
+        res = rffi.cast(lltype.Signed, res)
+        return not bool(res)
+
     def acquire_timed(self, timeout):
         """Timeout is in microseconds.  Returns 0 in case of failure,
         1 in case it works, 2 if interrupted by a signal."""
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
@@ -16,6 +16,14 @@
     res = ok1 and not ok2 and ok3
     assert res == 1
 
+def test_lock_is_aquired():
+    l = allocate_lock()
+    ok1 = l.acquire(True)
+    assert l.is_acquired() == True
+    assert l.is_acquired() == True
+    l.release()
+    assert l.is_acquired() == False
+
 def test_thread_error():
     l = allocate_lock()
     try:


More information about the pypy-commit mailing list