Skip to content

Commit

Permalink
Default to infinitely blocking locks
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Jun 15, 2020
1 parent c425e88 commit 1ee6afe
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/labthings/core/lock.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from gevent.hub import getcurrent
from gevent.lock import RLock as _RLock

import logging

from .exceptions import LockError

sentinel = object()

class RLock(_RLock):
def locked(self):
Expand All @@ -22,16 +25,16 @@ class StrictLock:
timeout (int): Time in seconds acquisition will wait before raising an exception
"""

def __init__(self, timeout=1, name=None):
def __init__(self, timeout=None, name=None):
self._lock = RLock()
self.timeout = timeout
self.name = name

def locked(self):
return self._lock.locked()

def acquire(self, blocking=True, timeout=None, _strict=True):
if not timeout:
def acquire(self, blocking=True, timeout=sentinel, _strict=True):
if timeout is sentinel:
timeout = self.timeout
result = self._lock.acquire(blocking, timeout=timeout)
if _strict and not result:
Expand Down Expand Up @@ -74,12 +77,12 @@ class CompositeLock:
timeout (int): Time in seconds acquisition will wait before raising an exception
"""

def __init__(self, locks, timeout=1):
def __init__(self, locks, timeout=None):
self.locks = locks
self.timeout = timeout

def acquire(self, blocking=True, timeout=None):
if not timeout:
def acquire(self, blocking=True, timeout=sentinel):
if timeout is sentinel:
timeout = self.timeout

lock_all = all(
Expand All @@ -89,6 +92,7 @@ def acquire(self, blocking=True, timeout=None):

if not lock_all:
self._emergency_release()
logging.error(f"Unable to acquire {self} within {timeout} seconds")
raise LockError("ACQUIRE_ERROR", self)

return True
Expand Down

0 comments on commit 1ee6afe

Please sign in to comment.