Skip to content

Commit

Permalink
Allow alternate lock implementations to be used
Browse files Browse the repository at this point in the history
  • Loading branch information
reversefold authored and elarivie committed Jan 21, 2020
1 parent e8dc268 commit c8a56da
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions readerwriterlock/rwlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@

from typing import Optional
from types import TracebackType
import typing


class RWLockRead():
"""A Read/Write lock giving preference to Reader."""

def __init__(self) -> None:
def __init__(self, lock_factory: typing.Callable = threading.Lock) -> None:
"""Init."""
self.v_read_count = 0
self.c_resource = threading.Lock()
self.c_lock_read_count = threading.Lock()
self.c_resource = lock_factory()
self.c_lock_read_count = lock_factory()

class _aReader():
def __init__(self, p_RWLock: "RWLockRead") -> None:
Expand Down Expand Up @@ -99,15 +100,15 @@ def gen_wlock(self) -> "RWLockRead._aWriter":
class RWLockWrite():
"""A Read/Write lock giving preference to Writer."""

def __init__(self) -> None:
def __init__(self, lock_factory: typing.Callable = threading.Lock) -> None:
"""Init."""
self.v_read_count = 0
self.v_write_count = 0
self.c_lock_read_count = threading.Lock()
self.c_lock_write_count = threading.Lock()
self.c_lock_read_entry = threading.Lock()
self.c_lock_read_try = threading.Lock()
self.c_resource = threading.Lock()
self.c_lock_read_count = lock_factory()
self.c_lock_write_count = lock_factory()
self.c_lock_read_entry = lock_factory()
self.c_lock_read_try = lock_factory()
self.c_resource = lock_factory()

class _aReader():
def __init__(self, p_RWLock: "RWLockWrite") -> None:
Expand Down Expand Up @@ -224,12 +225,12 @@ def gen_wlock(self) -> "RWLockWrite._aWriter":
class RWLockFair():
"""A Read/Write lock giving fairness to both Reader and Writer."""

def __init__(self) -> None:
def __init__(self, lock_factory: typing.Callable = threading.Lock) -> None:
"""Init."""
self.v_read_count = 0
self.c_lock_read_count = threading.Lock()
self.c_lock_read = threading.Lock()
self.c_lock_write = threading.Lock()
self.c_lock_read_count = lock_factory()
self.c_lock_read = lock_factory()
self.c_lock_write = lock_factory()

class _aReader():
def __init__(self, p_RWLock: "RWLockFair") -> None:
Expand Down

0 comments on commit c8a56da

Please sign in to comment.