diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-04-22 19:54:16 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-04-22 19:54:16 (GMT) |
commit | b00b596c053cf21826b5fe342be0354333a102f5 (patch) | |
tree | bc6ae02e0f9e6bad52c6373de7f6c6ee4d76df82 /Lib/threading.py | |
parent | fcd9f222385c7855241a3f6bfe84b454e2373cdf (diff) | |
parent | 81a5855a2739797a9a41e03acecf9746baa3e882 (diff) | |
download | cpython-b00b596c053cf21826b5fe342be0354333a102f5.zip cpython-b00b596c053cf21826b5fe342be0354333a102f5.tar.gz cpython-b00b596c053cf21826b5fe342be0354333a102f5.tar.bz2 |
Issue #11714: Use 'with' statements to assure a Semaphore releases a
condition variable. Original patch by Thomas Rachel.
Diffstat (limited to 'Lib/threading.py')
-rw-r--r-- | Lib/threading.py | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/Lib/threading.py b/Lib/threading.py index e7f7df7..3526894 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -253,31 +253,29 @@ class Semaphore: raise ValueError("can't specify timeout for non-blocking acquire") rc = False endtime = None - self._cond.acquire() - while self._value == 0: - if not blocking: - break - if timeout is not None: - if endtime is None: - endtime = _time() + timeout - else: - timeout = endtime - _time() - if timeout <= 0: - break - self._cond.wait(timeout) - else: - self._value -= 1 - rc = True - self._cond.release() + with self._cond: + while self._value == 0: + if not blocking: + break + if timeout is not None: + if endtime is None: + endtime = _time() + timeout + else: + timeout = endtime - _time() + if timeout <= 0: + break + self._cond.wait(timeout) + else: + self._value -= 1 + rc = True return rc __enter__ = acquire def release(self): - self._cond.acquire() - self._value += 1 - self._cond.notify() - self._cond.release() + with self._cond: + self._value += 1 + self._cond.notify() def __exit__(self, t, v, tb): self.release() |