blob: e29ee40adcee8d9fb4209af65e44a6314366d2c8 [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',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000022 'Timer', 'setprofile', 'settrace', 'local', 'stack_size']
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
Georg Brandl2067bfd2008-05-25 13:05:15 +000034del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000035
Guido van Rossum7f5013a1998-04-09 22:01:42 +000036
Tim Peters59aba122003-07-01 20:01:55 +000037# Debug support (adapted from ihooks.py).
38# All the major classes here derive from _Verbose. We force that to
39# be a new-style class so that all the major classes here are new-style.
40# This helps debugging (type(instance) is more revealing for instances
41# of new-style classes).
Guido van Rossum7f5013a1998-04-09 22:01:42 +000042
Tim Peters0939fac2003-07-01 19:28:44 +000043_VERBOSE = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +000044
45if __debug__:
46
Tim Peters59aba122003-07-01 20:01:55 +000047 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000048
49 def __init__(self, verbose=None):
50 if verbose is None:
51 verbose = _VERBOSE
Guido van Rossumd0648992007-08-20 19:25:41 +000052 self._verbose = verbose
Guido van Rossum7f5013a1998-04-09 22:01:42 +000053
54 def _note(self, format, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +000055 if self._verbose:
Guido van Rossum7f5013a1998-04-09 22:01:42 +000056 format = format % args
Antoine Pitrou401edd62010-12-17 17:42:16 +000057 # Issue #4188: calling current_thread() can incur an infinite
58 # recursion if it has to create a DummyThread on the fly.
59 ident = _get_ident()
60 try:
61 name = _active[ident].name
62 except KeyError:
63 name = "<OS thread %d>" % ident
64 format = "%s: %s\n" % (name, format)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000065 _sys.stderr.write(format)
66
67else:
68 # Disable this when using "python -O"
Tim Peters59aba122003-07-01 20:01:55 +000069 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000070 def __init__(self, verbose=None):
71 pass
72 def _note(self, *args):
73 pass
74
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000075# Support for profile and trace hooks
76
77_profile_hook = None
78_trace_hook = None
79
80def setprofile(func):
81 global _profile_hook
82 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000083
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000084def settrace(func):
85 global _trace_hook
86 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000087
88# Synchronization classes
89
90Lock = _allocate_lock
91
Antoine Pitrou434736a2009-11-10 18:46:01 +000092def RLock(verbose=None, *args, **kwargs):
93 if verbose is None:
94 verbose = _VERBOSE
95 if (__debug__ and verbose) or _CRLock is None:
96 return _PyRLock(verbose, *args, **kwargs)
97 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000098
99class _RLock(_Verbose):
Tim Petersb90f89a2001-01-15 03:26:36 +0000100
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000101 def __init__(self, verbose=None):
102 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000103 self._block = _allocate_lock()
104 self._owner = None
105 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000106
107 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000108 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000109 try:
110 owner = _active[owner].name
111 except KeyError:
112 pass
113 return "<%s owner=%r count=%d>" % (
114 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000115
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000116 def acquire(self, blocking=True, timeout=-1):
Antoine Pitroub0872682009-11-09 16:08:16 +0000117 me = _get_ident()
118 if self._owner == me:
Guido van Rossumd0648992007-08-20 19:25:41 +0000119 self._count = self._count + 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000120 if __debug__:
121 self._note("%s.acquire(%s): recursive success", self, blocking)
122 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000123 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000124 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000125 self._owner = me
126 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000127 if __debug__:
Brett Cannon90cece72005-01-27 22:48:30 +0000128 self._note("%s.acquire(%s): initial success", self, blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000129 else:
130 if __debug__:
131 self._note("%s.acquire(%s): failure", self, blocking)
132 return rc
133
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000134 __enter__ = acquire
135
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000136 def release(self):
Antoine Pitroub0872682009-11-09 16:08:16 +0000137 if self._owner != _get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000138 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000139 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000140 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000141 self._owner = None
142 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000143 if __debug__:
144 self._note("%s.release(): final release", self)
145 else:
146 if __debug__:
147 self._note("%s.release(): non-final release", self)
148
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000149 def __exit__(self, t, v, tb):
150 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000151
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000152 # Internal methods used by condition variables
153
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000154 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000155 self._block.acquire()
156 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000157 if __debug__:
158 self._note("%s._acquire_restore()", self)
159
160 def _release_save(self):
161 if __debug__:
162 self._note("%s._release_save()", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000163 count = self._count
164 self._count = 0
165 owner = self._owner
166 self._owner = None
167 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000168 return (count, owner)
169
170 def _is_owned(self):
Antoine Pitroub0872682009-11-09 16:08:16 +0000171 return self._owner == _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000172
Antoine Pitrou434736a2009-11-10 18:46:01 +0000173_PyRLock = _RLock
174
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000175
176def Condition(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000177 return _Condition(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000178
179class _Condition(_Verbose):
180
181 def __init__(self, lock=None, verbose=None):
182 _Verbose.__init__(self, verbose)
183 if lock is None:
184 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000185 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000186 # Export the lock's acquire() and release() methods
187 self.acquire = lock.acquire
188 self.release = lock.release
189 # If the lock defines _release_save() and/or _acquire_restore(),
190 # these override the default implementations (which just call
191 # release() and acquire() on the lock). Ditto for _is_owned().
192 try:
193 self._release_save = lock._release_save
194 except AttributeError:
195 pass
196 try:
197 self._acquire_restore = lock._acquire_restore
198 except AttributeError:
199 pass
200 try:
201 self._is_owned = lock._is_owned
202 except AttributeError:
203 pass
Guido van Rossumd0648992007-08-20 19:25:41 +0000204 self._waiters = []
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000205
Thomas Wouters477c8d52006-05-27 19:21:47 +0000206 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000207 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000208
Thomas Wouters477c8d52006-05-27 19:21:47 +0000209 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000210 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000211
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000212 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000213 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000214
215 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000216 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000217
218 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000219 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000220
221 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000222 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000223 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000224 if self._lock.acquire(0):
225 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000226 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000227 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000228 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000229
230 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000231 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000232 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000233 waiter = _allocate_lock()
234 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000235 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000236 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000237 try: # restore state no matter what (e.g., KeyboardInterrupt)
238 if timeout is None:
239 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000240 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000241 if __debug__:
Tim Petersc951bf92001-04-02 20:15:57 +0000242 self._note("%s.wait(): got it", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000243 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000244 if timeout > 0:
245 gotit = waiter.acquire(True, timeout)
246 else:
247 gotit = waiter.acquire(False)
Tim Petersc951bf92001-04-02 20:15:57 +0000248 if not gotit:
249 if __debug__:
250 self._note("%s.wait(%s): timed out", self, timeout)
251 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000252 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000253 except ValueError:
254 pass
255 else:
256 if __debug__:
257 self._note("%s.wait(%s): got it", self, timeout)
Georg Brandlb9a43912010-10-28 09:03:20 +0000258 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000259 finally:
260 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000261
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000262 def wait_for(self, predicate, timeout=None):
263 endtime = None
264 waittime = timeout
265 result = predicate()
266 while not result:
267 if waittime is not None:
268 if endtime is None:
269 endtime = _time() + waittime
270 else:
271 waittime = endtime - _time()
272 if waittime <= 0:
273 if __debug__:
274 self._note("%s.wait_for(%r, %r): Timed out.",
275 self, predicate, timeout)
276 break
277 if __debug__:
278 self._note("%s.wait_for(%r, %r): Waiting with timeout=%s.",
279 self, predicate, timeout, waittime)
280 self.wait(waittime)
281 result = predicate()
282 else:
283 if __debug__:
284 self._note("%s.wait_for(%r, %r): Success.",
285 self, predicate, timeout)
286 return result
287
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000288 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000289 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000290 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000291 __waiters = self._waiters
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000292 waiters = __waiters[:n]
293 if not waiters:
294 if __debug__:
295 self._note("%s.notify(): no waiters", self)
296 return
297 self._note("%s.notify(): notifying %d waiter%s", self, n,
298 n!=1 and "s" or "")
299 for waiter in waiters:
300 waiter.release()
301 try:
302 __waiters.remove(waiter)
303 except ValueError:
304 pass
305
Benjamin Peterson672b8032008-06-11 19:14:14 +0000306 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000307 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000308
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000309 notifyAll = notify_all
310
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000311
312def Semaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000313 return _Semaphore(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000314
315class _Semaphore(_Verbose):
316
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000317 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000318
319 def __init__(self, value=1, verbose=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000320 if value < 0:
321 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000322 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000323 self._cond = Condition(Lock())
324 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000325
Antoine Pitrou0454af92010-04-17 23:51:58 +0000326 def acquire(self, blocking=True, timeout=None):
327 if not blocking and timeout is not None:
328 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000329 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000330 endtime = None
Guido van Rossumd0648992007-08-20 19:25:41 +0000331 self._cond.acquire()
332 while self._value == 0:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000333 if not blocking:
334 break
Skip Montanarob446fc72001-08-19 04:25:24 +0000335 if __debug__:
336 self._note("%s.acquire(%s): blocked waiting, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000337 self, blocking, self._value)
Antoine Pitrou0454af92010-04-17 23:51:58 +0000338 if timeout is not None:
339 if endtime is None:
340 endtime = _time() + timeout
341 else:
342 timeout = endtime - _time()
343 if timeout <= 0:
344 break
345 self._cond.wait(timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000346 else:
Guido van Rossumd0648992007-08-20 19:25:41 +0000347 self._value = self._value - 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000348 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000349 self._note("%s.acquire: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000350 self, self._value)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000351 rc = True
Guido van Rossumd0648992007-08-20 19:25:41 +0000352 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000353 return rc
354
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000355 __enter__ = acquire
356
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000357 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000358 self._cond.acquire()
359 self._value = self._value + 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000360 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000361 self._note("%s.release: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000362 self, self._value)
363 self._cond.notify()
364 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000365
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000366 def __exit__(self, t, v, tb):
367 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000368
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000369
Skip Montanaroe428bb72001-08-20 20:27:58 +0000370def BoundedSemaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000371 return _BoundedSemaphore(*args, **kwargs)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000372
373class _BoundedSemaphore(_Semaphore):
374 """Semaphore that checks that # releases is <= # acquires"""
375 def __init__(self, value=1, verbose=None):
376 _Semaphore.__init__(self, value, verbose)
377 self._initial_value = value
378
379 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000380 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000381 raise ValueError("Semaphore released too many times")
Skip Montanaroe428bb72001-08-20 20:27:58 +0000382 return _Semaphore.release(self)
383
384
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000385def Event(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000386 return _Event(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000387
388class _Event(_Verbose):
389
390 # After Tim Peters' event class (without is_posted())
391
392 def __init__(self, verbose=None):
393 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000394 self._cond = Condition(Lock())
395 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000396
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000397 def _reset_internal_locks(self):
398 # private! called by Thread._reset_internal_locks by _after_fork()
399 self._cond.__init__()
400
Benjamin Peterson672b8032008-06-11 19:14:14 +0000401 def is_set(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000402 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000403
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000404 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000405
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000406 def set(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000407 self._cond.acquire()
408 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000409 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000410 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000411 finally:
412 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000413
414 def clear(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000415 self._cond.acquire()
416 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000417 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000418 finally:
419 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000420
421 def wait(self, timeout=None):
Christian Heimes969fe572008-01-25 11:23:10 +0000422 self._cond.acquire()
423 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000424 if not self._flag:
425 self._cond.wait(timeout)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000426 return self._flag
Christian Heimes969fe572008-01-25 11:23:10 +0000427 finally:
428 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000429
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000430
431# A barrier class. Inspired in part by the pthread_barrier_* api and
432# the CyclicBarrier class from Java. See
433# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
434# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
435# CyclicBarrier.html
436# for information.
437# We maintain two main states, 'filling' and 'draining' enabling the barrier
438# to be cyclic. Threads are not allowed into it until it has fully drained
439# since the previous cycle. In addition, a 'resetting' state exists which is
440# similar to 'draining' except that threads leave with a BrokenBarrierError,
441# and a 'broken' state in which all threads get get the exception.
442class Barrier(_Verbose):
443 """
444 Barrier. Useful for synchronizing a fixed number of threads
445 at known synchronization points. Threads block on 'wait()' and are
446 simultaneously once they have all made that call.
447 """
448 def __init__(self, parties, action=None, timeout=None, verbose=None):
449 """
450 Create a barrier, initialised to 'parties' threads.
451 'action' is a callable which, when supplied, will be called
452 by one of the threads after they have all entered the
453 barrier and just prior to releasing them all.
454 If a 'timeout' is provided, it is uses as the default for
455 all subsequent 'wait()' calls.
456 """
457 _Verbose.__init__(self, verbose)
458 self._cond = Condition(Lock())
459 self._action = action
460 self._timeout = timeout
461 self._parties = parties
462 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
463 self._count = 0
464
465 def wait(self, timeout=None):
466 """
467 Wait for the barrier. When the specified number of threads have
468 started waiting, they are all simultaneously awoken. If an 'action'
469 was provided for the barrier, one of the threads will have executed
470 that callback prior to returning.
471 Returns an individual index number from 0 to 'parties-1'.
472 """
473 if timeout is None:
474 timeout = self._timeout
475 with self._cond:
476 self._enter() # Block while the barrier drains.
477 index = self._count
478 self._count += 1
479 try:
480 if index + 1 == self._parties:
481 # We release the barrier
482 self._release()
483 else:
484 # We wait until someone releases us
485 self._wait(timeout)
486 return index
487 finally:
488 self._count -= 1
489 # Wake up any threads waiting for barrier to drain.
490 self._exit()
491
492 # Block until the barrier is ready for us, or raise an exception
493 # if it is broken.
494 def _enter(self):
495 while self._state in (-1, 1):
496 # It is draining or resetting, wait until done
497 self._cond.wait()
498 #see if the barrier is in a broken state
499 if self._state < 0:
500 raise BrokenBarrierError
501 assert self._state == 0
502
503 # Optionally run the 'action' and release the threads waiting
504 # in the barrier.
505 def _release(self):
506 try:
507 if self._action:
508 self._action()
509 # enter draining state
510 self._state = 1
511 self._cond.notify_all()
512 except:
513 #an exception during the _action handler. Break and reraise
514 self._break()
515 raise
516
517 # Wait in the barrier until we are relased. Raise an exception
518 # if the barrier is reset or broken.
519 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000520 if not self._cond.wait_for(lambda : self._state != 0, timeout):
521 #timed out. Break the barrier
522 self._break()
523 raise BrokenBarrierError
524 if self._state < 0:
525 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000526 assert self._state == 1
527
528 # If we are the last thread to exit the barrier, signal any threads
529 # waiting for the barrier to drain.
530 def _exit(self):
531 if self._count == 0:
532 if self._state in (-1, 1):
533 #resetting or draining
534 self._state = 0
535 self._cond.notify_all()
536
537 def reset(self):
538 """
539 Reset the barrier to the initial state.
540 Any threads currently waiting will get the BrokenBarrier exception
541 raised.
542 """
543 with self._cond:
544 if self._count > 0:
545 if self._state == 0:
546 #reset the barrier, waking up threads
547 self._state = -1
548 elif self._state == -2:
549 #was broken, set it to reset state
550 #which clears when the last thread exits
551 self._state = -1
552 else:
553 self._state = 0
554 self._cond.notify_all()
555
556 def abort(self):
557 """
558 Place the barrier into a 'broken' state.
559 Useful in case of error. Any currently waiting threads and
560 threads attempting to 'wait()' will have BrokenBarrierError
561 raised.
562 """
563 with self._cond:
564 self._break()
565
566 def _break(self):
567 # An internal error was detected. The barrier is set to
568 # a broken state all parties awakened.
569 self._state = -2
570 self._cond.notify_all()
571
572 @property
573 def parties(self):
574 """
575 Return the number of threads required to trip the barrier.
576 """
577 return self._parties
578
579 @property
580 def n_waiting(self):
581 """
582 Return the number of threads that are currently waiting at the barrier.
583 """
584 # We don't need synchronization here since this is an ephemeral result
585 # anyway. It returns the correct value in the steady state.
586 if self._state == 0:
587 return self._count
588 return 0
589
590 @property
591 def broken(self):
592 """
593 Return True if the barrier is in a broken state
594 """
595 return self._state == -2
596
597#exception raised by the Barrier class
598class BrokenBarrierError(RuntimeError): pass
599
600
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000601# Helper to generate new thread names
602_counter = 0
603def _newname(template="Thread-%d"):
604 global _counter
605 _counter = _counter + 1
606 return template % _counter
607
608# Active thread administration
609_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000610_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000611_limbo = {}
612
613
614# Main class for threads
615
616class Thread(_Verbose):
617
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000618 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000619 # Need to store a reference to sys.exc_info for printing
620 # out exceptions when a thread tries to use a global var. during interp.
621 # shutdown and thus raises an exception about trying to perform some
622 # operation on/with a NoneType
623 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000624 # Keep sys.exc_clear too to clear the exception just before
625 # allowing .join() to return.
626 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000627
628 def __init__(self, group=None, target=None, name=None,
Georg Brandla4a8b822005-07-15 09:13:21 +0000629 args=(), kwargs=None, verbose=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000630 assert group is None, "group argument must be None for now"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000631 _Verbose.__init__(self, verbose)
Georg Brandla4a8b822005-07-15 09:13:21 +0000632 if kwargs is None:
633 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000634 self._target = target
635 self._name = str(name or _newname())
636 self._args = args
637 self._kwargs = kwargs
638 self._daemonic = self._set_daemon()
Georg Brandl0c77a822008-06-10 16:37:50 +0000639 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000640 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000641 self._stopped = False
642 self._block = Condition(Lock())
643 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000644 # sys.stderr is not stored in the class like
645 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000646 self._stderr = _sys.stderr
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000647
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000648 def _reset_internal_locks(self):
649 # private! Called by _after_fork() to reset our internal locks as
650 # they may be in an invalid state leading to a deadlock or crash.
651 if hasattr(self, '_block'): # DummyThread deletes _block
652 self._block.__init__()
653 self._started._reset_internal_locks()
654
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000655 def _set_daemon(self):
656 # Overridden in _MainThread and _DummyThread
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000657 return current_thread().daemon
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000658
659 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000660 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000661 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000662 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000663 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000664 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000665 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000666 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000667 status += " daemon"
668 if self._ident is not None:
669 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000670 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000671
672 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000673 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000674 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000675
Benjamin Peterson672b8032008-06-11 19:14:14 +0000676 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000677 raise RuntimeError("threads can only be started once")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000678 if __debug__:
679 self._note("%s.start(): starting thread", self)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000680 with _active_limbo_lock:
681 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000682 try:
683 _start_new_thread(self._bootstrap, ())
684 except Exception:
685 with _active_limbo_lock:
686 del _limbo[self]
687 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000688 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000689
690 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000691 try:
692 if self._target:
693 self._target(*self._args, **self._kwargs)
694 finally:
695 # Avoid a refcycle if the thread is running a function with
696 # an argument that has a member that points to the thread.
697 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000698
Guido van Rossumd0648992007-08-20 19:25:41 +0000699 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000700 # Wrapper around the real bootstrap code that ignores
701 # exceptions during interpreter cleanup. Those typically
702 # happen when a daemon thread wakes up at an unfortunate
703 # moment, finds the world around it destroyed, and raises some
704 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000705 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000706 # don't help anybody, and they confuse users, so we suppress
707 # them. We suppress them only when it appears that the world
708 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000709 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000710 # reported. Also, we only suppress them for daemonic threads;
711 # if a non-daemonic encounters this, something else is wrong.
712 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000713 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000714 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000715 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000716 return
717 raise
718
Benjamin Petersond23f8222009-04-05 19:13:16 +0000719 def _set_ident(self):
720 self._ident = _get_ident()
721
Guido van Rossumd0648992007-08-20 19:25:41 +0000722 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000723 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000724 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000725 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000726 with _active_limbo_lock:
727 _active[self._ident] = self
728 del _limbo[self]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000729 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000730 self._note("%s._bootstrap(): thread started", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000731
732 if _trace_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000733 self._note("%s._bootstrap(): registering trace hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000734 _sys.settrace(_trace_hook)
735 if _profile_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000736 self._note("%s._bootstrap(): registering profile hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000737 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000738
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000739 try:
740 self.run()
741 except SystemExit:
742 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000743 self._note("%s._bootstrap(): raised SystemExit", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000744 except:
745 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000746 self._note("%s._bootstrap(): unhandled exception", self)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000747 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000748 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000749 # _sys) in case sys.stderr was redefined since the creation of
750 # self.
751 if _sys:
752 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000753 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000754 else:
755 # Do the best job possible w/o a huge amt. of code to
756 # approximate a traceback (code ideas from
757 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000758 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000759 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000760 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000761 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000762 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000763 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000764 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000765 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000766 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000767 ' File "%s", line %s, in %s' %
768 (exc_tb.tb_frame.f_code.co_filename,
769 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000770 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000771 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000772 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000773 # Make sure that exc_tb gets deleted since it is a memory
774 # hog; deleting everything else is just for thoroughness
775 finally:
776 del exc_type, exc_value, exc_tb
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000777 else:
778 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000779 self._note("%s._bootstrap(): normal return", self)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000780 finally:
781 # Prevent a race in
782 # test_threading.test_no_refcycle_through_target when
783 # the exception keeps the target alive past when we
784 # assert that it's dead.
785 #XXX self.__exc_clear()
786 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000787 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000788 with _active_limbo_lock:
789 self._stop()
790 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000791 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000792 # grabs _active_limbo_lock.
793 del _active[_get_ident()]
794 except:
795 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000796
Guido van Rossumd0648992007-08-20 19:25:41 +0000797 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000798 self._block.acquire()
799 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000800 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000801 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000802
Guido van Rossumd0648992007-08-20 19:25:41 +0000803 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000804 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000805
Georg Brandl2067bfd2008-05-25 13:05:15 +0000806 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000807 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000808 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000809 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000810 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
811 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000812 # len(_active) is always <= 1 here, and any Thread instance created
813 # overwrites the (if any) thread currently registered in _active.
814 #
815 # An instance of _MainThread is always created by 'threading'. This
816 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000817 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000818 # same key in the dict. So when the _MainThread instance created by
819 # 'threading' tries to clean itself up when atexit calls this method
820 # it gets a KeyError if another Thread instance was created.
821 #
822 # This all means that KeyError from trying to delete something from
823 # _active if dummy_threading is being used is a red herring. But
824 # since it isn't if dummy_threading is *not* being used then don't
825 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000826
Christian Heimes969fe572008-01-25 11:23:10 +0000827 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000828 with _active_limbo_lock:
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000829 del _active[_get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000830 # There must not be any python code between the previous line
831 # and after the lock is released. Otherwise a tracing function
832 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000833 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000834 except KeyError:
835 if 'dummy_threading' not in _sys.modules:
836 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000837
838 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000839 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000840 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000841 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000842 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000843 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000844 raise RuntimeError("cannot join current thread")
845
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000846 if __debug__:
Guido van Rossumd0648992007-08-20 19:25:41 +0000847 if not self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000848 self._note("%s.join(): waiting until thread stops", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000849
850 self._block.acquire()
851 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000852 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000853 while not self._stopped:
854 self._block.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000855 if __debug__:
856 self._note("%s.join(): thread stopped", self)
Brett Cannonad07ff22005-11-23 02:15:50 +0000857 else:
858 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000859 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000860 delay = deadline - _time()
861 if delay <= 0:
862 if __debug__:
863 self._note("%s.join(): timed out", self)
864 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000865 self._block.wait(delay)
Brett Cannonad07ff22005-11-23 02:15:50 +0000866 else:
867 if __debug__:
868 self._note("%s.join(): thread stopped", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000869 finally:
870 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000871
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000872 @property
873 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000874 assert self._initialized, "Thread.__init__() not called"
875 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000876
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000877 @name.setter
878 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000879 assert self._initialized, "Thread.__init__() not called"
880 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000881
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000882 @property
883 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000884 assert self._initialized, "Thread.__init__() not called"
885 return self._ident
886
Benjamin Peterson672b8032008-06-11 19:14:14 +0000887 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000888 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000889 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000890
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000891 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000892
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000893 @property
894 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000895 assert self._initialized, "Thread.__init__() not called"
896 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000897
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000898 @daemon.setter
899 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000900 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000901 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000902 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000903 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000904 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000905
Benjamin Peterson6640d722008-08-18 18:16:46 +0000906 def isDaemon(self):
907 return self.daemon
908
909 def setDaemon(self, daemonic):
910 self.daemon = daemonic
911
912 def getName(self):
913 return self.name
914
915 def setName(self, name):
916 self.name = name
917
Martin v. Löwis44f86962001-09-05 13:44:54 +0000918# The timer class was contributed by Itamar Shtull-Trauring
919
920def Timer(*args, **kwargs):
921 return _Timer(*args, **kwargs)
922
923class _Timer(Thread):
924 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000925
Martin v. Löwis44f86962001-09-05 13:44:54 +0000926 t = Timer(30.0, f, args=[], kwargs={})
927 t.start()
928 t.cancel() # stop the timer's action if it's still waiting
929 """
Tim Petersb64bec32001-09-18 02:26:39 +0000930
Martin v. Löwis44f86962001-09-05 13:44:54 +0000931 def __init__(self, interval, function, args=[], kwargs={}):
932 Thread.__init__(self)
933 self.interval = interval
934 self.function = function
935 self.args = args
936 self.kwargs = kwargs
937 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000938
Martin v. Löwis44f86962001-09-05 13:44:54 +0000939 def cancel(self):
940 """Stop the timer if it hasn't finished yet"""
941 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000942
Martin v. Löwis44f86962001-09-05 13:44:54 +0000943 def run(self):
944 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000945 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000946 self.function(*self.args, **self.kwargs)
947 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000948
949# Special thread class to represent the main thread
950# This is garbage collected through an exit handler
951
952class _MainThread(Thread):
953
954 def __init__(self):
955 Thread.__init__(self, name="MainThread")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000956 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000957 self._set_ident()
958 with _active_limbo_lock:
959 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000960
961 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000962 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000963
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000964 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000965 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000966 t = _pickSomeNonDaemonThread()
967 if t:
968 if __debug__:
969 self._note("%s: waiting for other threads", self)
970 while t:
971 t.join()
972 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000973 if __debug__:
974 self._note("%s: exiting", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000975 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000976
977def _pickSomeNonDaemonThread():
978 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000979 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000980 return t
981 return None
982
983
984# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000985# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000986# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000987# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000988# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000989# They are marked as daemon threads so we won't wait for them
990# when we exit (conform previous semantics).
991
992class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000993
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000994 def __init__(self):
995 Thread.__init__(self, name=_newname("Dummy-%d"))
Tim Peters711906e2005-01-08 07:30:42 +0000996
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000997 # Thread._block consumes an OS-level locking primitive, which
Tim Peters711906e2005-01-08 07:30:42 +0000998 # can never be used by a _DummyThread. Since a _DummyThread
999 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +00001000 del self._block
Tim Peters711906e2005-01-08 07:30:42 +00001001
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001002 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001003 self._set_ident()
1004 with _active_limbo_lock:
1005 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001006
1007 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001008 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001009
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001010 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001011 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001012
1013
1014# Global API functions
1015
Benjamin Peterson672b8032008-06-11 19:14:14 +00001016def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001017 try:
1018 return _active[_get_ident()]
1019 except KeyError:
Benjamin Peterson672b8032008-06-11 19:14:14 +00001020 ##print "current_thread(): no current thread for", _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001021 return _DummyThread()
1022
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001023currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001024
Benjamin Peterson672b8032008-06-11 19:14:14 +00001025def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +00001026 with _active_limbo_lock:
1027 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001028
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001029activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001030
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001031def _enumerate():
1032 # Same as enumerate(), but without the lock. Internal use only.
1033 return list(_active.values()) + list(_limbo.values())
1034
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001035def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +00001036 with _active_limbo_lock:
1037 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001038
Georg Brandl2067bfd2008-05-25 13:05:15 +00001039from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001040
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001041# Create the main thread object,
1042# and make it available for the interpreter
1043# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001044
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001045_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001046
Jim Fultond15dc062004-07-14 19:11:50 +00001047# get thread-local implementation, either from the thread
1048# module, or from the python fallback
1049
1050try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001051 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +00001052except ImportError:
1053 from _threading_local import local
1054
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001055
Jesse Nollera8513972008-07-17 16:49:17 +00001056def _after_fork():
1057 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
1058 # is called from PyOS_AfterFork. Here we cleanup threading module state
1059 # that should not exist after a fork.
1060
1061 # Reset _active_limbo_lock, in case we forked while the lock was held
1062 # by another (non-forked) thread. http://bugs.python.org/issue874900
1063 global _active_limbo_lock
1064 _active_limbo_lock = _allocate_lock()
1065
1066 # fork() only copied the current thread; clear references to others.
1067 new_active = {}
1068 current = current_thread()
1069 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001070 for thread in _active.values():
Jesse Nollera8513972008-07-17 16:49:17 +00001071 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001072 # There is only one active thread. We reset the ident to
1073 # its new value since it can have changed.
1074 ident = _get_ident()
1075 thread._ident = ident
Gregory P. Smith96c886c2011-01-03 21:06:12 +00001076 # Any condition variables hanging off of the active thread may
1077 # be in an invalid state, so we reinitialize them.
Gregory P. Smith9bd4a242011-01-04 18:33:38 +00001078 thread._reset_internal_locks()
Jesse Nollera8513972008-07-17 16:49:17 +00001079 new_active[ident] = thread
1080 else:
1081 # All the others are already stopped.
1082 # We don't call _Thread__stop() because it tries to acquire
1083 # thread._Thread__block which could also have been held while
1084 # we forked.
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001085 thread._stopped = True
Jesse Nollera8513972008-07-17 16:49:17 +00001086
1087 _limbo.clear()
1088 _active.clear()
1089 _active.update(new_active)
1090 assert len(_active) == 1