From df462a84129f8373a71f9f65f8ecce9c984fc2ad Mon Sep 17 00:00:00 2001 From: Brian Koopman Date: Mon, 7 Oct 2024 13:48:57 -0400 Subject: [PATCH] Replace YieldingLock with ocs TimeoutLock --- socs/agents/lakeshore372/agent.py | 60 ++----------------------------- 1 file changed, 2 insertions(+), 58 deletions(-) diff --git a/socs/agents/lakeshore372/agent.py b/socs/agents/lakeshore372/agent.py index b1bc30c59..fe2bac4fd 100644 --- a/socs/agents/lakeshore372/agent.py +++ b/socs/agents/lakeshore372/agent.py @@ -1,8 +1,6 @@ import argparse import os -import threading import time -from contextlib import contextmanager import numpy as np import txaio @@ -20,60 +18,6 @@ def still_power_to_perc(power, res, lead, max_volts): return 100 * volt / max_volts -class YieldingLock: - """A lock protected by a lock. This braided arrangement guarantees - that a thread waiting on the lock will get priority over a thread - that has just released the lock and wants to reacquire it. - - The typical use case is a Process that wants to hold the lock as - much as possible, but occasionally release the lock (without - sleeping for long) so another thread can access a resource. The - method release_and_acquire() is provided to make this a one-liner. - - """ - - def __init__(self, default_timeout=None): - self.job = None - self._next = threading.Lock() - self._active = threading.Lock() - self._default_timeout = default_timeout - - def acquire(self, timeout=None, job=None): - if timeout is None: - timeout = self._default_timeout - if timeout is None or timeout == 0.: - kw = {'blocking': False} - else: - kw = {'blocking': True, 'timeout': timeout} - result = False - if self._next.acquire(**kw): - if self._active.acquire(**kw): - self.job = job - result = True - self._next.release() - return result - - def release(self): - self.job = None - return self._active.release() - - def release_and_acquire(self, timeout=None): - job = self.job - self.release() - return self.acquire(timeout=timeout, job=job) - - @contextmanager - def acquire_timeout(self, timeout=None, job='unnamed'): - result = self.acquire(timeout=timeout, job=job) - if result: - try: - yield result - finally: - self.release() - else: - yield result - - class LS372_Agent: """Agent to connect to a single Lakeshore 372 device. @@ -104,10 +48,10 @@ def __init__(self, agent, name, ip, dwell_time_delay=0, # self._lock is held by the acq Process only when accessing # the hardware but released occasionally so that (short) Tasks - # may run. Use a YieldingLock to guarantee that a waiting + # may run. Use a TimeoutLock to guarantee that a waiting # Task gets activated preferentially, even if the acq thread # immediately tries to reacquire. - self._lock = YieldingLock(default_timeout=5) + self._lock = TimeoutLock(default_timeout=5) self.name = name self.ip = ip