blob: 392ad141de60f0624e79b54240b00126fc5b488d [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
20
Guido van Rossum7f5013a1998-04-09 22:01:42 +000021# Rename some stuff so "from threading import *" is safe
Benjamin Peterson672b8032008-06-11 19:14:14 +000022__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
Tim Peters685e6972003-06-29 16:50:06 +000023 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
Thomas Wouters0e3f5912006-08-11 14:57:12 +000024 'Timer', 'setprofile', 'settrace', 'local', 'stack_size']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000025
Georg Brandl2067bfd2008-05-25 13:05:15 +000026_start_new_thread = _thread.start_new_thread
27_allocate_lock = _thread.allocate_lock
28_get_ident = _thread.get_ident
29ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000030try:
31 _CRLock = _thread.RLock
32except AttributeError:
33 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000034TIMEOUT_MAX = _thread.TIMEOUT_MAX
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).
39# All the major classes here derive from _Verbose. We force that to
40# be a new-style class so that all the major classes here are new-style.
41# This helps debugging (type(instance) is more revealing for instances
42# of new-style classes).
Guido van Rossum7f5013a1998-04-09 22:01:42 +000043
Tim Peters0939fac2003-07-01 19:28:44 +000044_VERBOSE = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +000045
46if __debug__:
47
Tim Peters59aba122003-07-01 20:01:55 +000048 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000049
50 def __init__(self, verbose=None):
51 if verbose is None:
52 verbose = _VERBOSE
Guido van Rossumd0648992007-08-20 19:25:41 +000053 self._verbose = verbose
Guido van Rossum7f5013a1998-04-09 22:01:42 +000054
55 def _note(self, format, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +000056 if self._verbose:
Guido van Rossum7f5013a1998-04-09 22:01:42 +000057 format = format % args
Antoine Pitrou401edd62010-12-17 17:42:16 +000058 # Issue #4188: calling current_thread() can incur an infinite
59 # recursion if it has to create a DummyThread on the fly.
60 ident = _get_ident()
61 try:
62 name = _active[ident].name
63 except KeyError:
64 name = "<OS thread %d>" % ident
65 format = "%s: %s\n" % (name, format)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000066 _sys.stderr.write(format)
67
68else:
69 # Disable this when using "python -O"
Tim Peters59aba122003-07-01 20:01:55 +000070 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000071 def __init__(self, verbose=None):
72 pass
73 def _note(self, *args):
74 pass
75
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000076# Support for profile and trace hooks
77
78_profile_hook = None
79_trace_hook = None
80
81def setprofile(func):
82 global _profile_hook
83 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000084
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000085def settrace(func):
86 global _trace_hook
87 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000088
89# Synchronization classes
90
91Lock = _allocate_lock
92
Antoine Pitrou434736a2009-11-10 18:46:01 +000093def RLock(verbose=None, *args, **kwargs):
94 if verbose is None:
95 verbose = _VERBOSE
96 if (__debug__ and verbose) or _CRLock is None:
97 return _PyRLock(verbose, *args, **kwargs)
98 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000099
100class _RLock(_Verbose):
Tim Petersb90f89a2001-01-15 03:26:36 +0000101
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000102 def __init__(self, verbose=None):
103 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000104 self._block = _allocate_lock()
105 self._owner = None
106 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000107
108 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000109 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000110 try:
111 owner = _active[owner].name
112 except KeyError:
113 pass
114 return "<%s owner=%r count=%d>" % (
115 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000116
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000117 def acquire(self, blocking=True, timeout=-1):
Antoine Pitroub0872682009-11-09 16:08:16 +0000118 me = _get_ident()
119 if self._owner == me:
Guido van Rossumd0648992007-08-20 19:25:41 +0000120 self._count = self._count + 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000121 if __debug__:
122 self._note("%s.acquire(%s): recursive success", self, blocking)
123 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000124 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000125 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000126 self._owner = me
127 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000128 if __debug__:
Brett Cannon90cece72005-01-27 22:48:30 +0000129 self._note("%s.acquire(%s): initial success", self, blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000130 else:
131 if __debug__:
132 self._note("%s.acquire(%s): failure", self, blocking)
133 return rc
134
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000135 __enter__ = acquire
136
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000137 def release(self):
Antoine Pitroub0872682009-11-09 16:08:16 +0000138 if self._owner != _get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000139 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000140 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000141 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000142 self._owner = None
143 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000144 if __debug__:
145 self._note("%s.release(): final release", self)
146 else:
147 if __debug__:
148 self._note("%s.release(): non-final release", self)
149
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000150 def __exit__(self, t, v, tb):
151 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000152
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000153 # Internal methods used by condition variables
154
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000155 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000156 self._block.acquire()
157 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000158 if __debug__:
159 self._note("%s._acquire_restore()", self)
160
161 def _release_save(self):
162 if __debug__:
163 self._note("%s._release_save()", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000164 count = self._count
165 self._count = 0
166 owner = self._owner
167 self._owner = None
168 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000169 return (count, owner)
170
171 def _is_owned(self):
Antoine Pitroub0872682009-11-09 16:08:16 +0000172 return self._owner == _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000173
Antoine Pitrou434736a2009-11-10 18:46:01 +0000174_PyRLock = _RLock
175
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000176
177def Condition(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000178 return _Condition(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000179
180class _Condition(_Verbose):
181
182 def __init__(self, lock=None, verbose=None):
183 _Verbose.__init__(self, verbose)
184 if lock is None:
185 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000186 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000187 # Export the lock's acquire() and release() methods
188 self.acquire = lock.acquire
189 self.release = lock.release
190 # If the lock defines _release_save() and/or _acquire_restore(),
191 # these override the default implementations (which just call
192 # release() and acquire() on the lock). Ditto for _is_owned().
193 try:
194 self._release_save = lock._release_save
195 except AttributeError:
196 pass
197 try:
198 self._acquire_restore = lock._acquire_restore
199 except AttributeError:
200 pass
201 try:
202 self._is_owned = lock._is_owned
203 except AttributeError:
204 pass
Guido van Rossumd0648992007-08-20 19:25:41 +0000205 self._waiters = []
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000206
Thomas Wouters477c8d52006-05-27 19:21:47 +0000207 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000208 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000209
Thomas Wouters477c8d52006-05-27 19:21:47 +0000210 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000211 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000212
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000213 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000214 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000215
216 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000217 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000218
219 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000220 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000221
222 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000223 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000224 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000225 if self._lock.acquire(0):
226 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000227 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000228 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000229 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000230
231 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000232 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000233 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000234 waiter = _allocate_lock()
235 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000236 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000237 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000238 try: # restore state no matter what (e.g., KeyboardInterrupt)
239 if timeout is None:
240 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000241 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000242 if __debug__:
Tim Petersc951bf92001-04-02 20:15:57 +0000243 self._note("%s.wait(): got it", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000244 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000245 if timeout > 0:
246 gotit = waiter.acquire(True, timeout)
247 else:
248 gotit = waiter.acquire(False)
Tim Petersc951bf92001-04-02 20:15:57 +0000249 if not gotit:
250 if __debug__:
251 self._note("%s.wait(%s): timed out", self, timeout)
252 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000253 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000254 except ValueError:
255 pass
256 else:
257 if __debug__:
258 self._note("%s.wait(%s): got it", self, timeout)
Georg Brandlb9a43912010-10-28 09:03:20 +0000259 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000260 finally:
261 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000262
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000263 def wait_for(self, predicate, timeout=None):
264 endtime = None
265 waittime = timeout
266 result = predicate()
267 while not result:
268 if waittime is not None:
269 if endtime is None:
270 endtime = _time() + waittime
271 else:
272 waittime = endtime - _time()
273 if waittime <= 0:
274 if __debug__:
275 self._note("%s.wait_for(%r, %r): Timed out.",
276 self, predicate, timeout)
277 break
278 if __debug__:
279 self._note("%s.wait_for(%r, %r): Waiting with timeout=%s.",
280 self, predicate, timeout, waittime)
281 self.wait(waittime)
282 result = predicate()
283 else:
284 if __debug__:
285 self._note("%s.wait_for(%r, %r): Success.",
286 self, predicate, timeout)
287 return result
288
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000289 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000290 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000291 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000292 __waiters = self._waiters
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000293 waiters = __waiters[:n]
294 if not waiters:
295 if __debug__:
296 self._note("%s.notify(): no waiters", self)
297 return
298 self._note("%s.notify(): notifying %d waiter%s", self, n,
299 n!=1 and "s" or "")
300 for waiter in waiters:
301 waiter.release()
302 try:
303 __waiters.remove(waiter)
304 except ValueError:
305 pass
306
Benjamin Peterson672b8032008-06-11 19:14:14 +0000307 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000308 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000309
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000310 notifyAll = notify_all
311
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000312
313def Semaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000314 return _Semaphore(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000315
316class _Semaphore(_Verbose):
317
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000318 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000319
320 def __init__(self, value=1, verbose=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000321 if value < 0:
322 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000323 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000324 self._cond = Condition(Lock())
325 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000326
Antoine Pitrou0454af92010-04-17 23:51:58 +0000327 def acquire(self, blocking=True, timeout=None):
328 if not blocking and timeout is not None:
329 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000330 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000331 endtime = None
Guido van Rossumd0648992007-08-20 19:25:41 +0000332 self._cond.acquire()
333 while self._value == 0:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000334 if not blocking:
335 break
Skip Montanarob446fc72001-08-19 04:25:24 +0000336 if __debug__:
337 self._note("%s.acquire(%s): blocked waiting, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000338 self, blocking, self._value)
Antoine Pitrou0454af92010-04-17 23:51:58 +0000339 if timeout is not None:
340 if endtime is None:
341 endtime = _time() + timeout
342 else:
343 timeout = endtime - _time()
344 if timeout <= 0:
345 break
346 self._cond.wait(timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000347 else:
Guido van Rossumd0648992007-08-20 19:25:41 +0000348 self._value = self._value - 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000349 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000350 self._note("%s.acquire: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000351 self, self._value)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000352 rc = True
Guido van Rossumd0648992007-08-20 19:25:41 +0000353 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000354 return rc
355
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000356 __enter__ = acquire
357
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000358 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000359 self._cond.acquire()
360 self._value = self._value + 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000361 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000362 self._note("%s.release: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000363 self, self._value)
364 self._cond.notify()
365 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000366
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000367 def __exit__(self, t, v, tb):
368 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000369
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000370
Skip Montanaroe428bb72001-08-20 20:27:58 +0000371def BoundedSemaphore(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000372 return _BoundedSemaphore(*args, **kwargs)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000373
374class _BoundedSemaphore(_Semaphore):
375 """Semaphore that checks that # releases is <= # acquires"""
376 def __init__(self, value=1, verbose=None):
377 _Semaphore.__init__(self, value, verbose)
378 self._initial_value = value
379
380 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000381 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000382 raise ValueError("Semaphore released too many times")
Skip Montanaroe428bb72001-08-20 20:27:58 +0000383 return _Semaphore.release(self)
384
385
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000386def Event(*args, **kwargs):
Guido van Rossum68468eb2003-02-27 20:14:51 +0000387 return _Event(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000388
389class _Event(_Verbose):
390
391 # After Tim Peters' event class (without is_posted())
392
393 def __init__(self, verbose=None):
394 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000395 self._cond = Condition(Lock())
396 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000397
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000398 def _reset_internal_locks(self):
399 # private! called by Thread._reset_internal_locks by _after_fork()
400 self._cond.__init__()
401
Benjamin Peterson672b8032008-06-11 19:14:14 +0000402 def is_set(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000403 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000404
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000405 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000406
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000407 def set(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000408 self._cond.acquire()
409 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000410 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000411 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000412 finally:
413 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000414
415 def clear(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000416 self._cond.acquire()
417 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000418 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000419 finally:
420 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000421
422 def wait(self, timeout=None):
Christian Heimes969fe572008-01-25 11:23:10 +0000423 self._cond.acquire()
424 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000425 if not self._flag:
426 self._cond.wait(timeout)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000427 return self._flag
Christian Heimes969fe572008-01-25 11:23:10 +0000428 finally:
429 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000430
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000431
432# A barrier class. Inspired in part by the pthread_barrier_* api and
433# the CyclicBarrier class from Java. See
434# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
435# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
436# CyclicBarrier.html
437# for information.
438# We maintain two main states, 'filling' and 'draining' enabling the barrier
439# to be cyclic. Threads are not allowed into it until it has fully drained
440# since the previous cycle. In addition, a 'resetting' state exists which is
441# similar to 'draining' except that threads leave with a BrokenBarrierError,
442# and a 'broken' state in which all threads get get the exception.
443class Barrier(_Verbose):
444 """
445 Barrier. Useful for synchronizing a fixed number of threads
446 at known synchronization points. Threads block on 'wait()' and are
447 simultaneously once they have all made that call.
448 """
449 def __init__(self, parties, action=None, timeout=None, verbose=None):
450 """
451 Create a barrier, initialised to 'parties' threads.
452 'action' is a callable which, when supplied, will be called
453 by one of the threads after they have all entered the
454 barrier and just prior to releasing them all.
455 If a 'timeout' is provided, it is uses as the default for
456 all subsequent 'wait()' calls.
457 """
458 _Verbose.__init__(self, verbose)
459 self._cond = Condition(Lock())
460 self._action = action
461 self._timeout = timeout
462 self._parties = parties
463 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
464 self._count = 0
465
466 def wait(self, timeout=None):
467 """
468 Wait for the barrier. When the specified number of threads have
469 started waiting, they are all simultaneously awoken. If an 'action'
470 was provided for the barrier, one of the threads will have executed
471 that callback prior to returning.
472 Returns an individual index number from 0 to 'parties-1'.
473 """
474 if timeout is None:
475 timeout = self._timeout
476 with self._cond:
477 self._enter() # Block while the barrier drains.
478 index = self._count
479 self._count += 1
480 try:
481 if index + 1 == self._parties:
482 # We release the barrier
483 self._release()
484 else:
485 # We wait until someone releases us
486 self._wait(timeout)
487 return index
488 finally:
489 self._count -= 1
490 # Wake up any threads waiting for barrier to drain.
491 self._exit()
492
493 # Block until the barrier is ready for us, or raise an exception
494 # if it is broken.
495 def _enter(self):
496 while self._state in (-1, 1):
497 # It is draining or resetting, wait until done
498 self._cond.wait()
499 #see if the barrier is in a broken state
500 if self._state < 0:
501 raise BrokenBarrierError
502 assert self._state == 0
503
504 # Optionally run the 'action' and release the threads waiting
505 # in the barrier.
506 def _release(self):
507 try:
508 if self._action:
509 self._action()
510 # enter draining state
511 self._state = 1
512 self._cond.notify_all()
513 except:
514 #an exception during the _action handler. Break and reraise
515 self._break()
516 raise
517
518 # Wait in the barrier until we are relased. Raise an exception
519 # if the barrier is reset or broken.
520 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000521 if not self._cond.wait_for(lambda : self._state != 0, timeout):
522 #timed out. Break the barrier
523 self._break()
524 raise BrokenBarrierError
525 if self._state < 0:
526 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000527 assert self._state == 1
528
529 # If we are the last thread to exit the barrier, signal any threads
530 # waiting for the barrier to drain.
531 def _exit(self):
532 if self._count == 0:
533 if self._state in (-1, 1):
534 #resetting or draining
535 self._state = 0
536 self._cond.notify_all()
537
538 def reset(self):
539 """
540 Reset the barrier to the initial state.
541 Any threads currently waiting will get the BrokenBarrier exception
542 raised.
543 """
544 with self._cond:
545 if self._count > 0:
546 if self._state == 0:
547 #reset the barrier, waking up threads
548 self._state = -1
549 elif self._state == -2:
550 #was broken, set it to reset state
551 #which clears when the last thread exits
552 self._state = -1
553 else:
554 self._state = 0
555 self._cond.notify_all()
556
557 def abort(self):
558 """
559 Place the barrier into a 'broken' state.
560 Useful in case of error. Any currently waiting threads and
561 threads attempting to 'wait()' will have BrokenBarrierError
562 raised.
563 """
564 with self._cond:
565 self._break()
566
567 def _break(self):
568 # An internal error was detected. The barrier is set to
569 # a broken state all parties awakened.
570 self._state = -2
571 self._cond.notify_all()
572
573 @property
574 def parties(self):
575 """
576 Return the number of threads required to trip the barrier.
577 """
578 return self._parties
579
580 @property
581 def n_waiting(self):
582 """
583 Return the number of threads that are currently waiting at the barrier.
584 """
585 # We don't need synchronization here since this is an ephemeral result
586 # anyway. It returns the correct value in the steady state.
587 if self._state == 0:
588 return self._count
589 return 0
590
591 @property
592 def broken(self):
593 """
594 Return True if the barrier is in a broken state
595 """
596 return self._state == -2
597
598#exception raised by the Barrier class
599class BrokenBarrierError(RuntimeError): pass
600
601
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000602# Helper to generate new thread names
603_counter = 0
604def _newname(template="Thread-%d"):
605 global _counter
606 _counter = _counter + 1
607 return template % _counter
608
609# Active thread administration
610_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000611_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000612_limbo = {}
613
614
615# Main class for threads
616
617class Thread(_Verbose):
618
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000619 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000620 # Need to store a reference to sys.exc_info for printing
621 # out exceptions when a thread tries to use a global var. during interp.
622 # shutdown and thus raises an exception about trying to perform some
623 # operation on/with a NoneType
624 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000625 # Keep sys.exc_clear too to clear the exception just before
626 # allowing .join() to return.
627 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000628
629 def __init__(self, group=None, target=None, name=None,
Georg Brandla4a8b822005-07-15 09:13:21 +0000630 args=(), kwargs=None, verbose=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000631 assert group is None, "group argument must be None for now"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000632 _Verbose.__init__(self, verbose)
Georg Brandla4a8b822005-07-15 09:13:21 +0000633 if kwargs is None:
634 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000635 self._target = target
636 self._name = str(name or _newname())
637 self._args = args
638 self._kwargs = kwargs
639 self._daemonic = self._set_daemon()
Georg Brandl0c77a822008-06-10 16:37:50 +0000640 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000641 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000642 self._stopped = False
643 self._block = Condition(Lock())
644 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000645 # sys.stderr is not stored in the class like
646 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000647 self._stderr = _sys.stderr
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000648
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000649 def _reset_internal_locks(self):
650 # private! Called by _after_fork() to reset our internal locks as
651 # they may be in an invalid state leading to a deadlock or crash.
652 if hasattr(self, '_block'): # DummyThread deletes _block
653 self._block.__init__()
654 self._started._reset_internal_locks()
655
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000656 def _set_daemon(self):
657 # Overridden in _MainThread and _DummyThread
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000658 return current_thread().daemon
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000659
660 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000661 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000662 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000663 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000664 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000665 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000666 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000667 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000668 status += " daemon"
669 if self._ident is not None:
670 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000671 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000672
673 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000674 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000675 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000676
Benjamin Peterson672b8032008-06-11 19:14:14 +0000677 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000678 raise RuntimeError("threads can only be started once")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000679 if __debug__:
680 self._note("%s.start(): starting thread", self)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000681 with _active_limbo_lock:
682 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000683 try:
684 _start_new_thread(self._bootstrap, ())
685 except Exception:
686 with _active_limbo_lock:
687 del _limbo[self]
688 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000689 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000690
691 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000692 try:
693 if self._target:
694 self._target(*self._args, **self._kwargs)
695 finally:
696 # Avoid a refcycle if the thread is running a function with
697 # an argument that has a member that points to the thread.
698 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000699
Guido van Rossumd0648992007-08-20 19:25:41 +0000700 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000701 # Wrapper around the real bootstrap code that ignores
702 # exceptions during interpreter cleanup. Those typically
703 # happen when a daemon thread wakes up at an unfortunate
704 # moment, finds the world around it destroyed, and raises some
705 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000706 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000707 # don't help anybody, and they confuse users, so we suppress
708 # them. We suppress them only when it appears that the world
709 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000710 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000711 # reported. Also, we only suppress them for daemonic threads;
712 # if a non-daemonic encounters this, something else is wrong.
713 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000714 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000715 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000716 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000717 return
718 raise
719
Benjamin Petersond23f8222009-04-05 19:13:16 +0000720 def _set_ident(self):
721 self._ident = _get_ident()
722
Guido van Rossumd0648992007-08-20 19:25:41 +0000723 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000724 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000725 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000726 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000727 with _active_limbo_lock:
728 _active[self._ident] = self
729 del _limbo[self]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000730 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000731 self._note("%s._bootstrap(): thread started", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000732
733 if _trace_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000734 self._note("%s._bootstrap(): registering trace hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000735 _sys.settrace(_trace_hook)
736 if _profile_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000737 self._note("%s._bootstrap(): registering profile hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000738 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000739
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000740 try:
741 self.run()
742 except SystemExit:
743 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000744 self._note("%s._bootstrap(): raised SystemExit", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000745 except:
746 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000747 self._note("%s._bootstrap(): unhandled exception", self)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000748 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000749 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000750 # _sys) in case sys.stderr was redefined since the creation of
751 # self.
752 if _sys:
753 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000754 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000755 else:
756 # Do the best job possible w/o a huge amt. of code to
757 # approximate a traceback (code ideas from
758 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000759 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000760 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000761 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000762 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000763 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000764 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000765 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000766 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000767 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000768 ' File "%s", line %s, in %s' %
769 (exc_tb.tb_frame.f_code.co_filename,
770 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000771 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000772 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000773 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000774 # Make sure that exc_tb gets deleted since it is a memory
775 # hog; deleting everything else is just for thoroughness
776 finally:
777 del exc_type, exc_value, exc_tb
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000778 else:
779 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000780 self._note("%s._bootstrap(): normal return", self)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000781 finally:
782 # Prevent a race in
783 # test_threading.test_no_refcycle_through_target when
784 # the exception keeps the target alive past when we
785 # assert that it's dead.
786 #XXX self.__exc_clear()
787 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000788 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000789 with _active_limbo_lock:
790 self._stop()
791 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000792 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000793 # grabs _active_limbo_lock.
794 del _active[_get_ident()]
795 except:
796 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000797
Guido van Rossumd0648992007-08-20 19:25:41 +0000798 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000799 self._block.acquire()
800 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000801 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000802 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000803
Guido van Rossumd0648992007-08-20 19:25:41 +0000804 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000805 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000806
Georg Brandl2067bfd2008-05-25 13:05:15 +0000807 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000808 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000809 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000810 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000811 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
812 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000813 # len(_active) is always <= 1 here, and any Thread instance created
814 # overwrites the (if any) thread currently registered in _active.
815 #
816 # An instance of _MainThread is always created by 'threading'. This
817 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000818 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000819 # same key in the dict. So when the _MainThread instance created by
820 # 'threading' tries to clean itself up when atexit calls this method
821 # it gets a KeyError if another Thread instance was created.
822 #
823 # This all means that KeyError from trying to delete something from
824 # _active if dummy_threading is being used is a red herring. But
825 # since it isn't if dummy_threading is *not* being used then don't
826 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000827
Christian Heimes969fe572008-01-25 11:23:10 +0000828 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000829 with _active_limbo_lock:
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000830 del _active[_get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000831 # There must not be any python code between the previous line
832 # and after the lock is released. Otherwise a tracing function
833 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000834 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000835 except KeyError:
836 if 'dummy_threading' not in _sys.modules:
837 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000838
839 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000840 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000841 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000842 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000843 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000844 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000845 raise RuntimeError("cannot join current thread")
846
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000847 if __debug__:
Guido van Rossumd0648992007-08-20 19:25:41 +0000848 if not self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000849 self._note("%s.join(): waiting until thread stops", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000850
851 self._block.acquire()
852 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000853 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000854 while not self._stopped:
855 self._block.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000856 if __debug__:
857 self._note("%s.join(): thread stopped", self)
Brett Cannonad07ff22005-11-23 02:15:50 +0000858 else:
859 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000860 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000861 delay = deadline - _time()
862 if delay <= 0:
863 if __debug__:
864 self._note("%s.join(): timed out", self)
865 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000866 self._block.wait(delay)
Brett Cannonad07ff22005-11-23 02:15:50 +0000867 else:
868 if __debug__:
869 self._note("%s.join(): thread stopped", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000870 finally:
871 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000872
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000873 @property
874 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000875 assert self._initialized, "Thread.__init__() not called"
876 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000877
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000878 @name.setter
879 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000880 assert self._initialized, "Thread.__init__() not called"
881 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000882
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000883 @property
884 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000885 assert self._initialized, "Thread.__init__() not called"
886 return self._ident
887
Benjamin Peterson672b8032008-06-11 19:14:14 +0000888 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000889 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000890 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000891
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000892 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000893
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000894 @property
895 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000896 assert self._initialized, "Thread.__init__() not called"
897 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000898
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000899 @daemon.setter
900 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000901 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000902 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000903 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000904 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000905 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000906
Benjamin Peterson6640d722008-08-18 18:16:46 +0000907 def isDaemon(self):
908 return self.daemon
909
910 def setDaemon(self, daemonic):
911 self.daemon = daemonic
912
913 def getName(self):
914 return self.name
915
916 def setName(self, name):
917 self.name = name
918
Martin v. Löwis44f86962001-09-05 13:44:54 +0000919# The timer class was contributed by Itamar Shtull-Trauring
920
921def Timer(*args, **kwargs):
922 return _Timer(*args, **kwargs)
923
924class _Timer(Thread):
925 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000926
Martin v. Löwis44f86962001-09-05 13:44:54 +0000927 t = Timer(30.0, f, args=[], kwargs={})
928 t.start()
929 t.cancel() # stop the timer's action if it's still waiting
930 """
Tim Petersb64bec32001-09-18 02:26:39 +0000931
Martin v. Löwis44f86962001-09-05 13:44:54 +0000932 def __init__(self, interval, function, args=[], kwargs={}):
933 Thread.__init__(self)
934 self.interval = interval
935 self.function = function
936 self.args = args
937 self.kwargs = kwargs
938 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000939
Martin v. Löwis44f86962001-09-05 13:44:54 +0000940 def cancel(self):
941 """Stop the timer if it hasn't finished yet"""
942 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000943
Martin v. Löwis44f86962001-09-05 13:44:54 +0000944 def run(self):
945 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000946 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000947 self.function(*self.args, **self.kwargs)
948 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000949
950# Special thread class to represent the main thread
951# This is garbage collected through an exit handler
952
953class _MainThread(Thread):
954
955 def __init__(self):
956 Thread.__init__(self, name="MainThread")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000957 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000958 self._set_ident()
959 with _active_limbo_lock:
960 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000961
962 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000963 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000964
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000965 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000966 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000967 t = _pickSomeNonDaemonThread()
968 if t:
969 if __debug__:
970 self._note("%s: waiting for other threads", self)
971 while t:
972 t.join()
973 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000974 if __debug__:
975 self._note("%s: exiting", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000976 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000977
978def _pickSomeNonDaemonThread():
979 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000980 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000981 return t
982 return None
983
984
985# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000986# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000987# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000988# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000989# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000990# They are marked as daemon threads so we won't wait for them
991# when we exit (conform previous semantics).
992
993class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000994
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000995 def __init__(self):
996 Thread.__init__(self, name=_newname("Dummy-%d"))
Tim Peters711906e2005-01-08 07:30:42 +0000997
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000998 # Thread._block consumes an OS-level locking primitive, which
Tim Peters711906e2005-01-08 07:30:42 +0000999 # can never be used by a _DummyThread. Since a _DummyThread
1000 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +00001001 del self._block
Tim Peters711906e2005-01-08 07:30:42 +00001002
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001003 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001004 self._set_ident()
1005 with _active_limbo_lock:
1006 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001007
1008 def _set_daemon(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001009 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001010
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001011 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001012 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001013
1014
1015# Global API functions
1016
Benjamin Peterson672b8032008-06-11 19:14:14 +00001017def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001018 try:
1019 return _active[_get_ident()]
1020 except KeyError:
Benjamin Peterson672b8032008-06-11 19:14:14 +00001021 ##print "current_thread(): no current thread for", _get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001022 return _DummyThread()
1023
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001024currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001025
Benjamin Peterson672b8032008-06-11 19:14:14 +00001026def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +00001027 with _active_limbo_lock:
1028 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001029
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001030activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001031
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001032def _enumerate():
1033 # Same as enumerate(), but without the lock. Internal use only.
1034 return list(_active.values()) + list(_limbo.values())
1035
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001036def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +00001037 with _active_limbo_lock:
1038 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001039
Georg Brandl2067bfd2008-05-25 13:05:15 +00001040from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001041
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001042# Create the main thread object,
1043# and make it available for the interpreter
1044# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001045
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001046_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001047
Jim Fultond15dc062004-07-14 19:11:50 +00001048# get thread-local implementation, either from the thread
1049# module, or from the python fallback
1050
1051try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001052 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +00001053except ImportError:
1054 from _threading_local import local
1055
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001056
Jesse Nollera8513972008-07-17 16:49:17 +00001057def _after_fork():
1058 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
1059 # is called from PyOS_AfterFork. Here we cleanup threading module state
1060 # that should not exist after a fork.
1061
1062 # Reset _active_limbo_lock, in case we forked while the lock was held
1063 # by another (non-forked) thread. http://bugs.python.org/issue874900
1064 global _active_limbo_lock
1065 _active_limbo_lock = _allocate_lock()
1066
1067 # fork() only copied the current thread; clear references to others.
1068 new_active = {}
1069 current = current_thread()
1070 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001071 for thread in _active.values():
Jesse Nollera8513972008-07-17 16:49:17 +00001072 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001073 # There is only one active thread. We reset the ident to
1074 # its new value since it can have changed.
1075 ident = _get_ident()
1076 thread._ident = ident
Gregory P. Smith96c886c2011-01-03 21:06:12 +00001077 # Any condition variables hanging off of the active thread may
1078 # be in an invalid state, so we reinitialize them.
Gregory P. Smith9bd4a242011-01-04 18:33:38 +00001079 thread._reset_internal_locks()
Jesse Nollera8513972008-07-17 16:49:17 +00001080 new_active[ident] = thread
1081 else:
1082 # All the others are already stopped.
1083 # We don't call _Thread__stop() because it tries to acquire
1084 # thread._Thread__block which could also have been held while
1085 # we forked.
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001086 thread._stopped = True
Jesse Nollera8513972008-07-17 16:49:17 +00001087
1088 _limbo.clear()
1089 _active.clear()
1090 _active.update(new_active)
1091 assert len(_active) == 1