blob: 28c214667148cc6ffa0d3e101b0d20b57932f5d6 [file] [log] [blame]
Jeremy Hylton92bb6e72002-08-14 19:25:42 +00001"""Thread module emulating a subset of Java's threading model."""
Guido van Rossum7f5013a1998-04-09 22:01:42 +00002
Fred Drakea8725952002-12-30 23:32:50 +00003import sys as _sys
Georg Brandl2067bfd2008-05-25 13:05:15 +00004import _thread
Fred Drakea8725952002-12-30 23:32:50 +00005
Fred Drakea8725952002-12-30 23:32:50 +00006from time import time as _time, sleep as _sleep
Neil Schemenauerf607fc52003-11-05 23:03:00 +00007from traceback import format_exc as _format_exc
Raymond Hettinger756b3f32004-01-29 06:37:52 +00008from collections import deque
Guido van Rossum7f5013a1998-04-09 22:01:42 +00009
Benjamin Petersonb3085c92008-09-01 23:09:31 +000010# Note regarding PEP 8 compliant names
11# This threading model was originally inspired by Java, and inherited
12# the convention of camelCase function and method names from that
13# language. Those originaly names are not in any imminent danger of
14# being deprecated (even for Py3k),so this module provides them as an
15# alias for the PEP 8 compliant names
16# Note that using the new PEP 8 compliant names facilitates substitution
17# with the multiprocessing module, which doesn't provide the old
18# Java inspired names.
19
Benjamin Peterson672b8032008-06-11 19:14:14 +000020__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000021 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
Victor Stinner754851f2011-04-19 23:58:51 +020022 'Timer', 'setprofile', 'settrace', 'local', 'stack_size', '_info']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000023
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000024# Rename some stuff so "from threading import *" is safe
Georg Brandl2067bfd2008-05-25 13:05:15 +000025_start_new_thread = _thread.start_new_thread
26_allocate_lock = _thread.allocate_lock
27_get_ident = _thread.get_ident
28ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000029try:
30 _CRLock = _thread.RLock
31except AttributeError:
32 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000033TIMEOUT_MAX = _thread.TIMEOUT_MAX
Victor Stinner754851f2011-04-19 23:58:51 +020034_info = _thread.info
Georg Brandl2067bfd2008-05-25 13:05:15 +000035del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000036
Guido van Rossum7f5013a1998-04-09 22:01:42 +000037
Tim Peters59aba122003-07-01 20:01:55 +000038# Debug support (adapted from ihooks.py).
Guido van Rossum7f5013a1998-04-09 22:01:42 +000039
Tim Peters0939fac2003-07-01 19:28:44 +000040_VERBOSE = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +000041
42if __debug__:
43
Tim Peters59aba122003-07-01 20:01:55 +000044 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000045
46 def __init__(self, verbose=None):
47 if verbose is None:
48 verbose = _VERBOSE
Guido van Rossumd0648992007-08-20 19:25:41 +000049 self._verbose = verbose
Guido van Rossum7f5013a1998-04-09 22:01:42 +000050
51 def _note(self, format, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +000052 if self._verbose:
Guido van Rossum7f5013a1998-04-09 22:01:42 +000053 format = format % args
Antoine Pitrou401edd62010-12-17 17:42:16 +000054 # Issue #4188: calling current_thread() can incur an infinite
55 # recursion if it has to create a DummyThread on the fly.
56 ident = _get_ident()
57 try:
58 name = _active[ident].name
59 except KeyError:
60 name = "<OS thread %d>" % ident
61 format = "%s: %s\n" % (name, format)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000062 _sys.stderr.write(format)
63
64else:
65 # Disable this when using "python -O"
Tim Peters59aba122003-07-01 20:01:55 +000066 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000067 def __init__(self, verbose=None):
68 pass
69 def _note(self, *args):
70 pass
71
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000072# Support for profile and trace hooks
73
74_profile_hook = None
75_trace_hook = None
76
77def setprofile(func):
78 global _profile_hook
79 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000080
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000081def settrace(func):
82 global _trace_hook
83 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000084
85# Synchronization classes
86
87Lock = _allocate_lock
88
Antoine Pitrou434736a2009-11-10 18:46:01 +000089def RLock(verbose=None, *args, **kwargs):
90 if verbose is None:
91 verbose = _VERBOSE
92 if (__debug__ and verbose) or _CRLock is None:
93 return _PyRLock(verbose, *args, **kwargs)
94 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000095
96class _RLock(_Verbose):
Tim Petersb90f89a2001-01-15 03:26:36 +000097
Guido van Rossum7f5013a1998-04-09 22:01:42 +000098 def __init__(self, verbose=None):
99 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000100 self._block = _allocate_lock()
101 self._owner = None
102 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000103
104 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000105 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000106 try:
107 owner = _active[owner].name
108 except KeyError:
109 pass
110 return "<%s owner=%r count=%d>" % (
111 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000112
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000113 def acquire(self, blocking=True, timeout=-1):
Antoine Pitroub0872682009-11-09 16:08:16 +0000114 me = _get_ident()
115 if self._owner == me:
Guido van Rossumd0648992007-08-20 19:25:41 +0000116 self._count = self._count + 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000117 if __debug__:
118 self._note("%s.acquire(%s): recursive success", self, blocking)
119 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000120 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000121 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000122 self._owner = me
123 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000124 if __debug__:
Brett Cannon90cece72005-01-27 22:48:30 +0000125 self._note("%s.acquire(%s): initial success", self, blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000126 else:
127 if __debug__:
128 self._note("%s.acquire(%s): failure", self, blocking)
129 return rc
130
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000131 __enter__ = acquire
132
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000133 def release(self):
Antoine Pitroub0872682009-11-09 16:08:16 +0000134 if self._owner != _get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000135 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000136 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000137 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000138 self._owner = None
139 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000140 if __debug__:
141 self._note("%s.release(): final release", self)
142 else:
143 if __debug__:
144 self._note("%s.release(): non-final release", self)
145
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000146 def __exit__(self, t, v, tb):
147 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000148
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000149 # Internal methods used by condition variables
150
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000151 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000152 self._block.acquire()
153 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000154 if __debug__:
155 self._note("%s._acquire_restore()", self)
156
157 def _release_save(self):
158 if __debug__:
159 self._note("%s._release_save()", self)
Victor Stinnerc2824d42011-04-24 23:41:33 +0200160 if self._count == 0:
161 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000162 count = self._count
163 self._count = 0
164 owner = self._owner
165 self._owner = None
166 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000167 return (count, owner)
168
169 def _is_owned(self):
Antoine Pitroub0872682009-11-09 16:08:16 +0000170 return self._owner == _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000171
Antoine Pitrou434736a2009-11-10 18:46:01 +0000172_PyRLock = _RLock
173
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000174
175def Condition(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000176 return _Condition(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000177
178class _Condition(_Verbose):
179
180 def __init__(self, lock=None, verbose=None):
181 _Verbose.__init__(self, verbose)
182 if lock is None:
183 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000184 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000185 # Export the lock's acquire() and release() methods
186 self.acquire = lock.acquire
187 self.release = lock.release
188 # If the lock defines _release_save() and/or _acquire_restore(),
189 # these override the default implementations (which just call
190 # release() and acquire() on the lock). Ditto for _is_owned().
191 try:
192 self._release_save = lock._release_save
193 except AttributeError:
194 pass
195 try:
196 self._acquire_restore = lock._acquire_restore
197 except AttributeError:
198 pass
199 try:
200 self._is_owned = lock._is_owned
201 except AttributeError:
202 pass
Guido van Rossumd0648992007-08-20 19:25:41 +0000203 self._waiters = []
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000204
Thomas Wouters477c8d52006-05-27 19:21:47 +0000205 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000206 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000207
Thomas Wouters477c8d52006-05-27 19:21:47 +0000208 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000209 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000210
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000211 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000212 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000213
214 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000215 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000216
217 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000218 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000219
220 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000221 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000222 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000223 if self._lock.acquire(0):
224 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000225 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000226 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000227 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000228
229 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000230 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000231 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000232 waiter = _allocate_lock()
233 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000234 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000235 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000236 try: # restore state no matter what (e.g., KeyboardInterrupt)
237 if timeout is None:
238 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000239 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000240 if __debug__:
Tim Petersc951bf92001-04-02 20:15:57 +0000241 self._note("%s.wait(): got it", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000242 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000243 if timeout > 0:
244 gotit = waiter.acquire(True, timeout)
245 else:
246 gotit = waiter.acquire(False)
Tim Petersc951bf92001-04-02 20:15:57 +0000247 if not gotit:
248 if __debug__:
249 self._note("%s.wait(%s): timed out", self, timeout)
250 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000251 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000252 except ValueError:
253 pass
254 else:
255 if __debug__:
256 self._note("%s.wait(%s): got it", self, timeout)
Georg Brandlb9a43912010-10-28 09:03:20 +0000257 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000258 finally:
259 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000260
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000261 def wait_for(self, predicate, timeout=None):
262 endtime = None
263 waittime = timeout
264 result = predicate()
265 while not result:
266 if waittime is not None:
267 if endtime is None:
268 endtime = _time() + waittime
269 else:
270 waittime = endtime - _time()
271 if waittime <= 0:
272 if __debug__:
273 self._note("%s.wait_for(%r, %r): Timed out.",
274 self, predicate, timeout)
275 break
276 if __debug__:
277 self._note("%s.wait_for(%r, %r): Waiting with timeout=%s.",
278 self, predicate, timeout, waittime)
279 self.wait(waittime)
280 result = predicate()
281 else:
282 if __debug__:
283 self._note("%s.wait_for(%r, %r): Success.",
284 self, predicate, timeout)
285 return result
286
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000287 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000288 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000289 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000290 __waiters = self._waiters
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000291 waiters = __waiters[:n]
292 if not waiters:
293 if __debug__:
294 self._note("%s.notify(): no waiters", self)
295 return
296 self._note("%s.notify(): notifying %d waiter%s", self, n,
297 n!=1 and "s" or "")
298 for waiter in waiters:
299 waiter.release()
300 try:
301 __waiters.remove(waiter)
302 except ValueError:
303 pass
304
Benjamin Peterson672b8032008-06-11 19:14:14 +0000305 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000306 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000307
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000308 notifyAll = notify_all
309
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000310
311def Semaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000312 return _Semaphore(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000313
314class _Semaphore(_Verbose):
315
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000316 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000317
318 def __init__(self, value=1, verbose=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000319 if value < 0:
320 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000321 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000322 self._cond = Condition(Lock())
323 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000324
Antoine Pitrou0454af92010-04-17 23:51:58 +0000325 def acquire(self, blocking=True, timeout=None):
326 if not blocking and timeout is not None:
327 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000328 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000329 endtime = None
Guido van Rossumd0648992007-08-20 19:25:41 +0000330 self._cond.acquire()
331 while self._value == 0:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000332 if not blocking:
333 break
Skip Montanarob446fc72001-08-19 04:25:24 +0000334 if __debug__:
335 self._note("%s.acquire(%s): blocked waiting, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000336 self, blocking, self._value)
Antoine Pitrou0454af92010-04-17 23:51:58 +0000337 if timeout is not None:
338 if endtime is None:
339 endtime = _time() + timeout
340 else:
341 timeout = endtime - _time()
342 if timeout <= 0:
343 break
344 self._cond.wait(timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000345 else:
Guido van Rossumd0648992007-08-20 19:25:41 +0000346 self._value = self._value - 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000347 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000348 self._note("%s.acquire: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000349 self, self._value)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000350 rc = True
Guido van Rossumd0648992007-08-20 19:25:41 +0000351 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000352 return rc
353
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000354 __enter__ = acquire
355
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000356 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000357 self._cond.acquire()
358 self._value = self._value + 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000359 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000360 self._note("%s.release: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000361 self, self._value)
362 self._cond.notify()
363 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000364
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000365 def __exit__(self, t, v, tb):
366 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000367
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000368
Skip Montanaroe428bb72001-08-20 20:27:58 +0000369def BoundedSemaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000370 return _BoundedSemaphore(*args, **kwargs)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000371
372class _BoundedSemaphore(_Semaphore):
373 """Semaphore that checks that # releases is <= # acquires"""
374 def __init__(self, value=1, verbose=None):
375 _Semaphore.__init__(self, value, verbose)
376 self._initial_value = value
377
378 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000379 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000380 raise ValueError("Semaphore released too many times")
Skip Montanaroe428bb72001-08-20 20:27:58 +0000381 return _Semaphore.release(self)
382
383
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000384def Event(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000385 return _Event(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000386
387class _Event(_Verbose):
388
389 # After Tim Peters' event class (without is_posted())
390
391 def __init__(self, verbose=None):
392 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000393 self._cond = Condition(Lock())
394 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000395
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000396 def _reset_internal_locks(self):
397 # private! called by Thread._reset_internal_locks by _after_fork()
398 self._cond.__init__()
399
Benjamin Peterson672b8032008-06-11 19:14:14 +0000400 def is_set(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000401 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000402
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000403 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000404
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000405 def set(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000406 self._cond.acquire()
407 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000408 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000409 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000410 finally:
411 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000412
413 def clear(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000414 self._cond.acquire()
415 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000416 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000417 finally:
418 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000419
420 def wait(self, timeout=None):
Christian Heimes969fe572008-01-25 11:23:10 +0000421 self._cond.acquire()
422 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000423 if not self._flag:
424 self._cond.wait(timeout)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000425 return self._flag
Christian Heimes969fe572008-01-25 11:23:10 +0000426 finally:
427 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000428
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000429
430# A barrier class. Inspired in part by the pthread_barrier_* api and
431# the CyclicBarrier class from Java. See
432# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
433# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
434# CyclicBarrier.html
435# for information.
436# We maintain two main states, 'filling' and 'draining' enabling the barrier
437# to be cyclic. Threads are not allowed into it until it has fully drained
438# since the previous cycle. In addition, a 'resetting' state exists which is
439# similar to 'draining' except that threads leave with a BrokenBarrierError,
440# and a 'broken' state in which all threads get get the exception.
441class Barrier(_Verbose):
442 """
443 Barrier. Useful for synchronizing a fixed number of threads
444 at known synchronization points. Threads block on 'wait()' and are
445 simultaneously once they have all made that call.
446 """
447 def __init__(self, parties, action=None, timeout=None, verbose=None):
448 """
449 Create a barrier, initialised to 'parties' threads.
450 'action' is a callable which, when supplied, will be called
451 by one of the threads after they have all entered the
452 barrier and just prior to releasing them all.
453 If a 'timeout' is provided, it is uses as the default for
454 all subsequent 'wait()' calls.
455 """
456 _Verbose.__init__(self, verbose)
457 self._cond = Condition(Lock())
458 self._action = action
459 self._timeout = timeout
460 self._parties = parties
461 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
462 self._count = 0
463
464 def wait(self, timeout=None):
465 """
466 Wait for the barrier. When the specified number of threads have
467 started waiting, they are all simultaneously awoken. If an 'action'
468 was provided for the barrier, one of the threads will have executed
469 that callback prior to returning.
470 Returns an individual index number from 0 to 'parties-1'.
471 """
472 if timeout is None:
473 timeout = self._timeout
474 with self._cond:
475 self._enter() # Block while the barrier drains.
476 index = self._count
477 self._count += 1
478 try:
479 if index + 1 == self._parties:
480 # We release the barrier
481 self._release()
482 else:
483 # We wait until someone releases us
484 self._wait(timeout)
485 return index
486 finally:
487 self._count -= 1
488 # Wake up any threads waiting for barrier to drain.
489 self._exit()
490
491 # Block until the barrier is ready for us, or raise an exception
492 # if it is broken.
493 def _enter(self):
494 while self._state in (-1, 1):
495 # It is draining or resetting, wait until done
496 self._cond.wait()
497 #see if the barrier is in a broken state
498 if self._state < 0:
499 raise BrokenBarrierError
500 assert self._state == 0
501
502 # Optionally run the 'action' and release the threads waiting
503 # in the barrier.
504 def _release(self):
505 try:
506 if self._action:
507 self._action()
508 # enter draining state
509 self._state = 1
510 self._cond.notify_all()
511 except:
512 #an exception during the _action handler. Break and reraise
513 self._break()
514 raise
515
516 # Wait in the barrier until we are relased. Raise an exception
517 # if the barrier is reset or broken.
518 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000519 if not self._cond.wait_for(lambda : self._state != 0, timeout):
520 #timed out. Break the barrier
521 self._break()
522 raise BrokenBarrierError
523 if self._state < 0:
524 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000525 assert self._state == 1
526
527 # If we are the last thread to exit the barrier, signal any threads
528 # waiting for the barrier to drain.
529 def _exit(self):
530 if self._count == 0:
531 if self._state in (-1, 1):
532 #resetting or draining
533 self._state = 0
534 self._cond.notify_all()
535
536 def reset(self):
537 """
538 Reset the barrier to the initial state.
539 Any threads currently waiting will get the BrokenBarrier exception
540 raised.
541 """
542 with self._cond:
543 if self._count > 0:
544 if self._state == 0:
545 #reset the barrier, waking up threads
546 self._state = -1
547 elif self._state == -2:
548 #was broken, set it to reset state
549 #which clears when the last thread exits
550 self._state = -1
551 else:
552 self._state = 0
553 self._cond.notify_all()
554
555 def abort(self):
556 """
557 Place the barrier into a 'broken' state.
558 Useful in case of error. Any currently waiting threads and
559 threads attempting to 'wait()' will have BrokenBarrierError
560 raised.
561 """
562 with self._cond:
563 self._break()
564
565 def _break(self):
566 # An internal error was detected. The barrier is set to
567 # a broken state all parties awakened.
568 self._state = -2
569 self._cond.notify_all()
570
571 @property
572 def parties(self):
573 """
574 Return the number of threads required to trip the barrier.
575 """
576 return self._parties
577
578 @property
579 def n_waiting(self):
580 """
581 Return the number of threads that are currently waiting at the barrier.
582 """
583 # We don't need synchronization here since this is an ephemeral result
584 # anyway. It returns the correct value in the steady state.
585 if self._state == 0:
586 return self._count
587 return 0
588
589 @property
590 def broken(self):
591 """
592 Return True if the barrier is in a broken state
593 """
594 return self._state == -2
595
596#exception raised by the Barrier class
597class BrokenBarrierError(RuntimeError): pass
598
599
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000600# Helper to generate new thread names
601_counter = 0
602def _newname(template="Thread-%d"):
603 global _counter
604 _counter = _counter + 1
605 return template % _counter
606
607# Active thread administration
608_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000609_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000610_limbo = {}
611
612
613# Main class for threads
614
615class Thread(_Verbose):
616
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000617 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000618 # Need to store a reference to sys.exc_info for printing
619 # out exceptions when a thread tries to use a global var. during interp.
620 # shutdown and thus raises an exception about trying to perform some
621 # operation on/with a NoneType
622 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000623 # Keep sys.exc_clear too to clear the exception just before
624 # allowing .join() to return.
625 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000626
627 def __init__(self, group=None, target=None, name=None,
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000628 args=(), kwargs=None, verbose=None, *, daemon=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000629 assert group is None, "group argument must be None for now"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000630 _Verbose.__init__(self, verbose)
Georg Brandla4a8b822005-07-15 09:13:21 +0000631 if kwargs is None:
632 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000633 self._target = target
634 self._name = str(name or _newname())
635 self._args = args
636 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000637 if daemon is not None:
638 self._daemonic = daemon
639 else:
640 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000641 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000642 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000643 self._stopped = False
644 self._block = Condition(Lock())
645 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000646 # sys.stderr is not stored in the class like
647 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000648 self._stderr = _sys.stderr
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000649
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000650 def _reset_internal_locks(self):
651 # private! Called by _after_fork() to reset our internal locks as
652 # they may be in an invalid state leading to a deadlock or crash.
653 if hasattr(self, '_block'): # DummyThread deletes _block
654 self._block.__init__()
655 self._started._reset_internal_locks()
656
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000657 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000658 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000659 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000660 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000661 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000662 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000663 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000664 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000665 status += " daemon"
666 if self._ident is not None:
667 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000668 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000669
670 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000671 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000672 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000673
Benjamin Peterson672b8032008-06-11 19:14:14 +0000674 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000675 raise RuntimeError("threads can only be started once")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000676 if __debug__:
677 self._note("%s.start(): starting thread", self)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000678 with _active_limbo_lock:
679 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000680 try:
681 _start_new_thread(self._bootstrap, ())
682 except Exception:
683 with _active_limbo_lock:
684 del _limbo[self]
685 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000686 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000687
688 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000689 try:
690 if self._target:
691 self._target(*self._args, **self._kwargs)
692 finally:
693 # Avoid a refcycle if the thread is running a function with
694 # an argument that has a member that points to the thread.
695 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000696
Guido van Rossumd0648992007-08-20 19:25:41 +0000697 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000698 # Wrapper around the real bootstrap code that ignores
699 # exceptions during interpreter cleanup. Those typically
700 # happen when a daemon thread wakes up at an unfortunate
701 # moment, finds the world around it destroyed, and raises some
702 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000703 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000704 # don't help anybody, and they confuse users, so we suppress
705 # them. We suppress them only when it appears that the world
706 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000707 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000708 # reported. Also, we only suppress them for daemonic threads;
709 # if a non-daemonic encounters this, something else is wrong.
710 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000711 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000712 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000713 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000714 return
715 raise
716
Benjamin Petersond23f8222009-04-05 19:13:16 +0000717 def _set_ident(self):
718 self._ident = _get_ident()
719
Guido van Rossumd0648992007-08-20 19:25:41 +0000720 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000721 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000722 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000723 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000724 with _active_limbo_lock:
725 _active[self._ident] = self
726 del _limbo[self]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000727 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000728 self._note("%s._bootstrap(): thread started", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000729
730 if _trace_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000731 self._note("%s._bootstrap(): registering trace hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000732 _sys.settrace(_trace_hook)
733 if _profile_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000734 self._note("%s._bootstrap(): registering profile hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000735 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000736
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000737 try:
738 self.run()
739 except SystemExit:
740 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000741 self._note("%s._bootstrap(): raised SystemExit", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000742 except:
743 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000744 self._note("%s._bootstrap(): unhandled exception", self)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000745 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000746 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000747 # _sys) in case sys.stderr was redefined since the creation of
748 # self.
749 if _sys:
750 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000751 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000752 else:
753 # Do the best job possible w/o a huge amt. of code to
754 # approximate a traceback (code ideas from
755 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000756 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000757 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000758 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000759 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000760 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000761 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000762 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000763 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000764 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000765 ' File "%s", line %s, in %s' %
766 (exc_tb.tb_frame.f_code.co_filename,
767 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000768 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000769 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000770 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000771 # Make sure that exc_tb gets deleted since it is a memory
772 # hog; deleting everything else is just for thoroughness
773 finally:
774 del exc_type, exc_value, exc_tb
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000775 else:
776 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000777 self._note("%s._bootstrap(): normal return", self)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000778 finally:
779 # Prevent a race in
780 # test_threading.test_no_refcycle_through_target when
781 # the exception keeps the target alive past when we
782 # assert that it's dead.
783 #XXX self.__exc_clear()
784 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000785 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000786 with _active_limbo_lock:
787 self._stop()
788 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000789 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000790 # grabs _active_limbo_lock.
791 del _active[_get_ident()]
792 except:
793 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000794
Guido van Rossumd0648992007-08-20 19:25:41 +0000795 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000796 self._block.acquire()
797 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000798 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000799 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000800
Guido van Rossumd0648992007-08-20 19:25:41 +0000801 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000802 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000803
Georg Brandl2067bfd2008-05-25 13:05:15 +0000804 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000805 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000806 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000807 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000808 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
809 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000810 # len(_active) is always <= 1 here, and any Thread instance created
811 # overwrites the (if any) thread currently registered in _active.
812 #
813 # An instance of _MainThread is always created by 'threading'. This
814 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000815 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000816 # same key in the dict. So when the _MainThread instance created by
817 # 'threading' tries to clean itself up when atexit calls this method
818 # it gets a KeyError if another Thread instance was created.
819 #
820 # This all means that KeyError from trying to delete something from
821 # _active if dummy_threading is being used is a red herring. But
822 # since it isn't if dummy_threading is *not* being used then don't
823 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000824
Christian Heimes969fe572008-01-25 11:23:10 +0000825 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000826 with _active_limbo_lock:
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000827 del _active[_get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000828 # There must not be any python code between the previous line
829 # and after the lock is released. Otherwise a tracing function
830 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000831 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000832 except KeyError:
833 if 'dummy_threading' not in _sys.modules:
834 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000835
836 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000837 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000838 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000839 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000840 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000841 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000842 raise RuntimeError("cannot join current thread")
843
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000844 if __debug__:
Guido van Rossumd0648992007-08-20 19:25:41 +0000845 if not self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000846 self._note("%s.join(): waiting until thread stops", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000847
848 self._block.acquire()
849 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000850 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000851 while not self._stopped:
852 self._block.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000853 if __debug__:
854 self._note("%s.join(): thread stopped", self)
Brett Cannonad07ff22005-11-23 02:15:50 +0000855 else:
856 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000857 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000858 delay = deadline - _time()
859 if delay <= 0:
860 if __debug__:
861 self._note("%s.join(): timed out", self)
862 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000863 self._block.wait(delay)
Brett Cannonad07ff22005-11-23 02:15:50 +0000864 else:
865 if __debug__:
866 self._note("%s.join(): thread stopped", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000867 finally:
868 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000869
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000870 @property
871 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000872 assert self._initialized, "Thread.__init__() not called"
873 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000874
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000875 @name.setter
876 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000877 assert self._initialized, "Thread.__init__() not called"
878 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000879
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000880 @property
881 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000882 assert self._initialized, "Thread.__init__() not called"
883 return self._ident
884
Benjamin Peterson672b8032008-06-11 19:14:14 +0000885 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000886 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000887 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000888
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000889 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000890
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000891 @property
892 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000893 assert self._initialized, "Thread.__init__() not called"
894 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000895
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000896 @daemon.setter
897 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000898 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000899 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000900 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000901 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000902 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000903
Benjamin Peterson6640d722008-08-18 18:16:46 +0000904 def isDaemon(self):
905 return self.daemon
906
907 def setDaemon(self, daemonic):
908 self.daemon = daemonic
909
910 def getName(self):
911 return self.name
912
913 def setName(self, name):
914 self.name = name
915
Martin v. Löwis44f86962001-09-05 13:44:54 +0000916# The timer class was contributed by Itamar Shtull-Trauring
917
918def Timer(*args, **kwargs):
919 return _Timer(*args, **kwargs)
920
921class _Timer(Thread):
922 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000923
Martin v. Löwis44f86962001-09-05 13:44:54 +0000924 t = Timer(30.0, f, args=[], kwargs={})
925 t.start()
926 t.cancel() # stop the timer's action if it's still waiting
927 """
Tim Petersb64bec32001-09-18 02:26:39 +0000928
Martin v. Löwis44f86962001-09-05 13:44:54 +0000929 def __init__(self, interval, function, args=[], kwargs={}):
930 Thread.__init__(self)
931 self.interval = interval
932 self.function = function
933 self.args = args
934 self.kwargs = kwargs
935 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000936
Martin v. Löwis44f86962001-09-05 13:44:54 +0000937 def cancel(self):
938 """Stop the timer if it hasn't finished yet"""
939 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000940
Martin v. Löwis44f86962001-09-05 13:44:54 +0000941 def run(self):
942 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000943 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000944 self.function(*self.args, **self.kwargs)
945 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000946
947# Special thread class to represent the main thread
948# This is garbage collected through an exit handler
949
950class _MainThread(Thread):
951
952 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000953 Thread.__init__(self, name="MainThread", daemon=False)
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000954 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000955 self._set_ident()
956 with _active_limbo_lock:
957 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000958
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000959 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000960 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000961 t = _pickSomeNonDaemonThread()
962 if t:
963 if __debug__:
964 self._note("%s: waiting for other threads", self)
965 while t:
966 t.join()
967 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000968 if __debug__:
969 self._note("%s: exiting", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000970 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000971
972def _pickSomeNonDaemonThread():
973 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000974 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000975 return t
976 return None
977
978
979# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000980# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000981# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000982# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000983# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000984# They are marked as daemon threads so we won't wait for them
985# when we exit (conform previous semantics).
986
987class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000988
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000989 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000990 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +0000991
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000992 # Thread._block consumes an OS-level locking primitive, which
Tim Peters711906e2005-01-08 07:30:42 +0000993 # can never be used by a _DummyThread. Since a _DummyThread
994 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +0000995 del self._block
Tim Peters711906e2005-01-08 07:30:42 +0000996
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000997 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000998 self._set_ident()
999 with _active_limbo_lock:
1000 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001001
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001002 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001003 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001004
1005
1006# Global API functions
1007
Benjamin Peterson672b8032008-06-11 19:14:14 +00001008def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001009 try:
1010 return _active[_get_ident()]
1011 except KeyError:
Benjamin Peterson672b8032008-06-11 19:14:14 +00001012 ##print "current_thread(): no current thread for", _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001013 return _DummyThread()
1014
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001015currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001016
Benjamin Peterson672b8032008-06-11 19:14:14 +00001017def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +00001018 with _active_limbo_lock:
1019 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001020
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001021activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001022
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001023def _enumerate():
1024 # Same as enumerate(), but without the lock. Internal use only.
1025 return list(_active.values()) + list(_limbo.values())
1026
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001027def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +00001028 with _active_limbo_lock:
1029 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001030
Georg Brandl2067bfd2008-05-25 13:05:15 +00001031from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001032
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001033# Create the main thread object,
1034# and make it available for the interpreter
1035# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001036
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001037_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001038
Jim Fultond15dc062004-07-14 19:11:50 +00001039# get thread-local implementation, either from the thread
1040# module, or from the python fallback
1041
1042try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001043 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +00001044except ImportError:
1045 from _threading_local import local
1046
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001047
Jesse Nollera8513972008-07-17 16:49:17 +00001048def _after_fork():
1049 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
1050 # is called from PyOS_AfterFork. Here we cleanup threading module state
1051 # that should not exist after a fork.
1052
1053 # Reset _active_limbo_lock, in case we forked while the lock was held
1054 # by another (non-forked) thread. http://bugs.python.org/issue874900
1055 global _active_limbo_lock
1056 _active_limbo_lock = _allocate_lock()
1057
1058 # fork() only copied the current thread; clear references to others.
1059 new_active = {}
1060 current = current_thread()
1061 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001062 for thread in _active.values():
Jesse Nollera8513972008-07-17 16:49:17 +00001063 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001064 # There is only one active thread. We reset the ident to
1065 # its new value since it can have changed.
1066 ident = _get_ident()
1067 thread._ident = ident
Gregory P. Smith96c886c2011-01-03 21:06:12 +00001068 # Any condition variables hanging off of the active thread may
1069 # be in an invalid state, so we reinitialize them.
Gregory P. Smith9bd4a242011-01-04 18:33:38 +00001070 thread._reset_internal_locks()
Jesse Nollera8513972008-07-17 16:49:17 +00001071 new_active[ident] = thread
1072 else:
1073 # All the others are already stopped.
1074 # We don't call _Thread__stop() because it tries to acquire
1075 # thread._Thread__block which could also have been held while
1076 # we forked.
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001077 thread._stopped = True
Jesse Nollera8513972008-07-17 16:49:17 +00001078
1079 _limbo.clear()
1080 _active.clear()
1081 _active.update(new_active)
1082 assert len(_active) == 1