harlowja / fasteners

A python package that provides useful locks.
Apache License 2.0
248 stars 45 forks source link

Add aquire_read_lock et. al. methods to ReaderWriterLock #108

Closed psarka closed 1 year ago

ArneBachmannDLR commented 1 year ago

This PR works for me. I don't know the source code too well to make an assessment, but copying the lock.py and using this adapter, I saw no errors:

class LockAdapter:
  def __init__(_, lock:ReaderWriterLock, write:bool = True) -> None: _.lock = lock; _.write = write
  def acquire(_, block:bool = True, timeout:Union[float,int] = -1) -> bool: return (_.lock.acquire_write_lock if _.write else _.lock.acquire_read_lock)()
  def release(_) -> None:                                                          (_.lock.release_write_lock if _.write else _.lock.release_read_lock)()
  def __enter__(_:Self) -> 'LockAdapter':                                   _.acquire(); return _.lock
  def __exit__(_, exc_type, exc_val, exc_tb) -> bool:                       _.release(); return True

The interface of the readerwriterlock is a bit indirect - I need to create a RWLockFairD then create a gen_rlock and then use it as context manager or acquire/release. For inter-process locks, I will test it. For threads I will stick to fasteners, once you cut a new release.

Thanks and Cheers!