@@ -34,68 +34,6 @@ class InvalidUsageError(Exception):
34
34
"""Raised when an API is used in an invalid or unsupported manner."""
35
35
36
36
37
- def safe_lock_release_context (rlock ):
38
- # Python3 has a C-implementation of RLock, which doesn't have the thread
39
- # termination issues.
40
- return _placeholder_release ()
41
-
42
-
43
- @contextlib .contextmanager
44
- def _placeholder_release ():
45
- yield
46
-
47
-
48
- # pylint: disable=protected-access
49
- @contextlib .contextmanager
50
- def _safe_lock_release_py2 (rlock ):
51
- """Ensure that a threading.RLock is fully released for Python 2.
52
-
53
- The RLock release code is:
54
- https://github.com/python/cpython/blob/2.7/Lib/threading.py#L187
55
-
56
- The RLock object's release method does not release all of its state if an
57
- exception is raised in the middle of its operation. There are three pieces of
58
- internal state that must be cleaned up:
59
- - owning thread ident, an integer.
60
- - entry count, an integer that counts how many times the current owner has
61
- locked the RLock.
62
- - internal lock, a threading.Lock instance that handles blocking.
63
-
64
- Args:
65
- rlock: threading.RLock, lock to fully release.
66
-
67
- Yields:
68
- None.
69
- """
70
- assert isinstance (rlock , threading ._RLock )
71
- ident = _thread .get_ident ()
72
- expected_count = 0
73
- if rlock ._RLock__owner == ident :
74
- expected_count = rlock ._RLock__count
75
- try :
76
- yield
77
- except ThreadTerminationError :
78
- # Check if the current thread still owns the lock by checking if we can
79
- # acquire the underlying lock.
80
- if rlock ._RLock__block .acquire (0 ):
81
- # Lock is clean, so unlock and we are done.
82
- rlock ._RLock__block .release ()
83
- elif rlock ._RLock__owner == ident and expected_count > 0 :
84
- # The lock is still held up the stack, so make sure the count is accurate.
85
- if rlock ._RLock__count != expected_count :
86
- rlock ._RLock__count = expected_count
87
- elif rlock ._RLock__owner == ident or rlock ._RLock__owner is None :
88
- # The internal lock is still acquired, but either this thread or no thread
89
- # owns it, which means it needs to be hard reset.
90
- rlock ._RLock__owner = None
91
- rlock ._RLock__count = 0
92
- rlock ._RLock__block .release ()
93
- raise
94
-
95
-
96
- # pylint: enable=protected-access
97
-
98
-
99
37
def loop (_ = None , force = False ):
100
38
"""Causes a function to loop indefinitely."""
101
39
if not force :
0 commit comments