diff options
author | Joost Lek <vlabakje@gmail.com> | 2019-06-17 08:10:17 (GMT) |
---|---|---|
committer | Victor Stinner <vstinner@redhat.com> | 2019-06-17 08:10:17 (GMT) |
commit | c5905f39bcf4ef895d42eede41bb5a2f071a501d (patch) | |
tree | 291b248c39f8e5cd4ea3ebe8955a0f2d7b25f0e8 /Lib/_dummy_thread.py | |
parent | 28fca0c422b425a6be43be31add0a5328c16b0b8 (diff) | |
download | cpython-c5905f39bcf4ef895d42eede41bb5a2f071a501d.zip cpython-c5905f39bcf4ef895d42eede41bb5a2f071a501d.tar.gz cpython-c5905f39bcf4ef895d42eede41bb5a2f071a501d.tar.bz2 |
bpo-36688: Adding an implementation of RLock in _dummy_thread (GH-12943)
Diffstat (limited to 'Lib/_dummy_thread.py')
-rw-r--r-- | Lib/_dummy_thread.py | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/Lib/_dummy_thread.py b/Lib/_dummy_thread.py index 2407f9b..6af68e5 100644 --- a/Lib/_dummy_thread.py +++ b/Lib/_dummy_thread.py @@ -14,7 +14,7 @@ Suggested usage is:: # Exports only things specified by thread documentation; # skipping obsolete synonyms allocate(), start_new(), exit_thread(). __all__ = ['error', 'start_new_thread', 'exit', 'get_ident', 'allocate_lock', - 'interrupt_main', 'LockType'] + 'interrupt_main', 'LockType', 'RLock'] # A dummy value TIMEOUT_MAX = 2**31 @@ -148,6 +148,36 @@ class LockType(object): hex(id(self)) ) + +class RLock(LockType): + """Dummy implementation of threading._RLock. + + Re-entrant lock can be aquired multiple times and needs to be released + just as many times. This dummy implemention does not check wheter the + current thread actually owns the lock, but does accounting on the call + counts. + """ + def __init__(self): + super().__init__() + self._levels = 0 + + def acquire(self, waitflag=None, timeout=-1): + """Aquire the lock, can be called multiple times in succession. + """ + locked = super().acquire(waitflag, timeout) + if locked: + self._levels += 1 + return locked + + def release(self): + """Release needs to be called once for every call to acquire(). + """ + if self._levels == 0: + raise error + if self._levels == 1: + super().release() + self._levels -= 1 + # Used to signal that interrupt_main was called in a "thread" _interrupt = False # True when not executing in a "thread" |