Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(68)

Delta Between Two Patch Sets: Lib/threading.py

Issue 22043: Use a monotonic clock to compute timeouts
Left Patch Set: Created 3 years ago
Right Patch Set: Created 3 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_selectors.py ('k') | Lib/trace.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """Thread module emulating a subset of Java's threading model.""" 1 """Thread module emulating a subset of Java's threading model."""
2 2
3 import sys as _sys 3 import sys as _sys
4 import _thread 4 import _thread
5 5
6 from time import monotonic as _time 6 from time import monotonic as _time
7 from traceback import format_exc as _format_exc 7 from traceback import format_exc as _format_exc
8 from _weakrefset import WeakSet 8 from _weakrefset import WeakSet
9 from itertools import islice as _islice 9 from itertools import islice as _islice
10 try: 10 try:
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 been recursively acquired several times. Another internal interface is 280 been recursively acquired several times. Another internal interface is
281 then used to restore the recursion level when the lock is reacquired. 281 then used to restore the recursion level when the lock is reacquired.
282 282
283 """ 283 """
284 if not self._is_owned(): 284 if not self._is_owned():
285 raise RuntimeError("cannot wait on un-acquired lock") 285 raise RuntimeError("cannot wait on un-acquired lock")
286 waiter = _allocate_lock() 286 waiter = _allocate_lock()
287 waiter.acquire() 287 waiter.acquire()
288 self._waiters.append(waiter) 288 self._waiters.append(waiter)
289 saved_state = self._release_save() 289 saved_state = self._release_save()
290 gotit = False
290 try: # restore state no matter what (e.g., KeyboardInterrupt) 291 try: # restore state no matter what (e.g., KeyboardInterrupt)
291 if timeout is None: 292 if timeout is None:
292 waiter.acquire() 293 waiter.acquire()
293 gotit = True 294 gotit = True
294 else: 295 else:
295 if timeout > 0: 296 if timeout > 0:
296 gotit = waiter.acquire(True, timeout) 297 gotit = waiter.acquire(True, timeout)
297 else: 298 else:
298 gotit = waiter.acquire(False) 299 gotit = waiter.acquire(False)
299 if not gotit:
300 try:
301 self._waiters.remove(waiter)
302 except ValueError:
303 pass
304 return gotit 300 return gotit
305 finally: 301 finally:
306 self._acquire_restore(saved_state) 302 self._acquire_restore(saved_state)
303 if not gotit:
304 try:
305 self._waiters.remove(waiter)
306 except ValueError:
307 pass
307 308
308 def wait_for(self, predicate, timeout=None): 309 def wait_for(self, predicate, timeout=None):
309 """Wait until a condition evaluates to True. 310 """Wait until a condition evaluates to True.
310 311
311 predicate should be a callable which result will be interpreted as a 312 predicate should be a callable which result will be interpreted as a
312 boolean value. A timeout may be provided giving the maximum time to 313 boolean value. A timeout may be provided giving the maximum time to
313 wait. 314 wait.
314 315
315 """ 316 """
316 endtime = None 317 endtime = None
(...skipping 1035 matching lines...) Expand 10 before | Expand all | Expand 10 after
1352 new_active[ident] = thread 1353 new_active[ident] = thread
1353 else: 1354 else:
1354 # All the others are already stopped. 1355 # All the others are already stopped.
1355 thread._reset_internal_locks(False) 1356 thread._reset_internal_locks(False)
1356 thread._stop() 1357 thread._stop()
1357 1358
1358 _limbo.clear() 1359 _limbo.clear()
1359 _active.clear() 1360 _active.clear()
1360 _active.update(new_active) 1361 _active.update(new_active)
1361 assert len(_active) == 1 1362 assert len(_active) == 1
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7