aboutsummaryrefslogtreecommitdiffstats
path: root/mpm
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2018-02-14 15:33:09 -0800
committerBrent Stapleton <bstapleton@g.hmc.edu>2018-10-23 10:13:00 -0700
commitf23c8cd1407a04c75deb1c26860a9262d1924086 (patch)
tree2dddb5803af049f83992c92cc78af19808ef1091 /mpm
parent84f9234d60056831876227aa4b4bbcdd3785aed5 (diff)
downloaduhd-f23c8cd1407a04c75deb1c26860a9262d1924086.tar.gz
uhd-f23c8cd1407a04c75deb1c26860a9262d1924086.tar.bz2
uhd-f23c8cd1407a04c75deb1c26860a9262d1924086.zip
mpm: Add lock_guard() function
This allows sharing mutexes between C++ and Python, and uses the with statement to provide a locked-out context.
Diffstat (limited to 'mpm')
-rw-r--r--mpm/python/usrp_mpm/mpmutils.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/mpm/python/usrp_mpm/mpmutils.py b/mpm/python/usrp_mpm/mpmutils.py
index 151713988..a0716d1da 100644
--- a/mpm/python/usrp_mpm/mpmutils.py
+++ b/mpm/python/usrp_mpm/mpmutils.py
@@ -8,6 +8,7 @@ Miscellaneous utilities for MPM
"""
import time
+from contextlib import contextmanager
def poll_with_timeout(state_check, timeout_ms, interval_ms):
"""
@@ -163,3 +164,24 @@ def async_exec(parent, method_name, *args):
while not awaitable_method():
time.sleep(0.1)
+@contextmanager
+def lock_guard(lockable):
+ """Context-based lock guard
+
+ Use this in a with statement to lock out the following scope. Example:
+ >>> with lock_guard(some_mutex):
+ >>> thread_sensitive_function()
+
+ In this snippet, we assume that some_mutex is a lockable object, and
+ implements lock() and unlock() member functions. Everything within the
+ with context will then be serialized.
+
+ This is a useful mechanic for sharing mutexes between Python and C++.
+
+ Arguments:
+ lockable -- Must have a .lock() and .unlock() method
+ """
+ lockable.lock()
+ yield
+ lockable.unlock()
+