Skip to content

Commit

Permalink
Context manager protocol forminterpreter-wise rentrant locks implmeneted
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbueno committed Sep 30, 2024
1 parent e528f8c commit b5d0e9e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
19 changes: 16 additions & 3 deletions src/extrainterpreters/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __enter__(self):
while time.time() <= threshold:
if _atomic_byte_lock(self._lock_address):
break
time.sleep(TIME_RESOLUTION * 4)
else:
self._timeout = self._original_timeout
raise TimeoutError("Timeout trying to acquire lock")
Expand Down Expand Up @@ -104,13 +105,25 @@ def __init__(self):
self._lock = _CrossInterpreterStructLock(lock_str)

def acquire(self, blocking=True, timeout=-1):
pass
timeout = None if timeout == -1 or not blocking else timeout
self._lock.timeout(timeout)
self._lock.__enter__()
return

def release(self):
pass
self._lock.__exit__()

def __enter__(self):
self.acquire()
#self._lock.__enter__()
return self

def __exit__(self, *args):
self.release()
#self._lock.__exit__()

def locked(self):
return False
return bool(self._lock._entered)

def __getstate__(self):
return {"_lock": self._lock}
Expand Down
11 changes: 10 additions & 1 deletion tests/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ def test_locks_work_as_context_manager(LockCls):



def test_lock_cant_be_reacquired():
def test_lock_cant_be_reacquired_same_interpreter():
lock = Lock()

lock.acquire()

with pytest.raises(TimeoutError):
lock.acquire(timeout=0)

@pytest.mark.skip("to be implemented")
def test_lock_cant_be_reacquired_other_interpreter():
lock = Lock()

lock.acquire()
Expand Down

0 comments on commit b5d0e9e

Please sign in to comment.