blob: 2a0d8ade48648877c484e3b5f0022e1050482725 [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
Antoine Pitrouc081c0c2011-07-15 22:12:24 +02008from _weakrefset import WeakSet
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',
Benjamin Peterson7761b952011-08-02 13:05:47 -050022 'Timer', 'ThreadError', '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
Victor Stinner2a129742011-05-30 23:02:52 +020027get_ident = _thread.get_ident
Georg Brandl2067bfd2008-05-25 13:05:15 +000028ThreadError = _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).
Guido van Rossum7f5013a1998-04-09 22:01:42 +000038
Tim Peters0939fac2003-07-01 19:28:44 +000039_VERBOSE = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +000040
41if __debug__:
42
Tim Peters59aba122003-07-01 20:01:55 +000043 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000044
45 def __init__(self, verbose=None):
46 if verbose is None:
47 verbose = _VERBOSE
Guido van Rossumd0648992007-08-20 19:25:41 +000048 self._verbose = verbose
Guido van Rossum7f5013a1998-04-09 22:01:42 +000049
50 def _note(self, format, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +000051 if self._verbose:
Guido van Rossum7f5013a1998-04-09 22:01:42 +000052 format = format % args
Antoine Pitrou401edd62010-12-17 17:42:16 +000053 # Issue #4188: calling current_thread() can incur an infinite
54 # recursion if it has to create a DummyThread on the fly.
Victor Stinner2a129742011-05-30 23:02:52 +020055 ident = get_ident()
Antoine Pitrou401edd62010-12-17 17:42:16 +000056 try:
57 name = _active[ident].name
58 except KeyError:
59 name = "<OS thread %d>" % ident
60 format = "%s: %s\n" % (name, format)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000061 _sys.stderr.write(format)
62
63else:
64 # Disable this when using "python -O"
Tim Peters59aba122003-07-01 20:01:55 +000065 class _Verbose(object):
Guido van Rossum7f5013a1998-04-09 22:01:42 +000066 def __init__(self, verbose=None):
67 pass
68 def _note(self, *args):
69 pass
70
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000071# Support for profile and trace hooks
72
73_profile_hook = None
74_trace_hook = None
75
76def setprofile(func):
77 global _profile_hook
78 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000079
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000080def settrace(func):
81 global _trace_hook
82 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000083
84# Synchronization classes
85
86Lock = _allocate_lock
87
Antoine Pitrou434736a2009-11-10 18:46:01 +000088def RLock(verbose=None, *args, **kwargs):
89 if verbose is None:
90 verbose = _VERBOSE
91 if (__debug__ and verbose) or _CRLock is None:
92 return _PyRLock(verbose, *args, **kwargs)
93 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000094
95class _RLock(_Verbose):
Tim Petersb90f89a2001-01-15 03:26:36 +000096
Guido van Rossum7f5013a1998-04-09 22:01:42 +000097 def __init__(self, verbose=None):
98 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +000099 self._block = _allocate_lock()
100 self._owner = None
101 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000102
103 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000104 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000105 try:
106 owner = _active[owner].name
107 except KeyError:
108 pass
109 return "<%s owner=%r count=%d>" % (
110 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000111
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000112 def acquire(self, blocking=True, timeout=-1):
Victor Stinner2a129742011-05-30 23:02:52 +0200113 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +0000114 if self._owner == me:
Guido van Rossumd0648992007-08-20 19:25:41 +0000115 self._count = self._count + 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000116 if __debug__:
117 self._note("%s.acquire(%s): recursive success", self, blocking)
118 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000119 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000120 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000121 self._owner = me
122 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000123 if __debug__:
Brett Cannon90cece72005-01-27 22:48:30 +0000124 self._note("%s.acquire(%s): initial success", self, blocking)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000125 else:
126 if __debug__:
127 self._note("%s.acquire(%s): failure", self, blocking)
128 return rc
129
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000130 __enter__ = acquire
131
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000132 def release(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200133 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000134 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000135 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000136 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000137 self._owner = None
138 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000139 if __debug__:
140 self._note("%s.release(): final release", self)
141 else:
142 if __debug__:
143 self._note("%s.release(): non-final release", self)
144
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000145 def __exit__(self, t, v, tb):
146 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000147
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000148 # Internal methods used by condition variables
149
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000150 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000151 self._block.acquire()
152 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000153 if __debug__:
154 self._note("%s._acquire_restore()", self)
155
156 def _release_save(self):
157 if __debug__:
158 self._note("%s._release_save()", self)
Victor Stinnerc2824d42011-04-24 23:41:33 +0200159 if self._count == 0:
160 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000161 count = self._count
162 self._count = 0
163 owner = self._owner
164 self._owner = None
165 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000166 return (count, owner)
167
168 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200169 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000170
Antoine Pitrou434736a2009-11-10 18:46:01 +0000171_PyRLock = _RLock
172
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000173
Éric Araujo0cdd4452011-07-28 00:28:28 +0200174class Condition(_Verbose):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000175
176 def __init__(self, lock=None, verbose=None):
177 _Verbose.__init__(self, verbose)
178 if lock is None:
179 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000180 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000181 # Export the lock's acquire() and release() methods
182 self.acquire = lock.acquire
183 self.release = lock.release
184 # If the lock defines _release_save() and/or _acquire_restore(),
185 # these override the default implementations (which just call
186 # release() and acquire() on the lock). Ditto for _is_owned().
187 try:
188 self._release_save = lock._release_save
189 except AttributeError:
190 pass
191 try:
192 self._acquire_restore = lock._acquire_restore
193 except AttributeError:
194 pass
195 try:
196 self._is_owned = lock._is_owned
197 except AttributeError:
198 pass
Guido van Rossumd0648992007-08-20 19:25:41 +0000199 self._waiters = []
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000200
Thomas Wouters477c8d52006-05-27 19:21:47 +0000201 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000202 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000203
Thomas Wouters477c8d52006-05-27 19:21:47 +0000204 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000205 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000206
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000207 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000208 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000209
210 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000211 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000212
213 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000214 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000215
216 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000217 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000218 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000219 if self._lock.acquire(0):
220 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000221 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000222 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000223 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000224
225 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000226 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000227 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000228 waiter = _allocate_lock()
229 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000230 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000231 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000232 try: # restore state no matter what (e.g., KeyboardInterrupt)
233 if timeout is None:
234 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000235 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000236 if __debug__:
Tim Petersc951bf92001-04-02 20:15:57 +0000237 self._note("%s.wait(): got it", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000238 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000239 if timeout > 0:
240 gotit = waiter.acquire(True, timeout)
241 else:
242 gotit = waiter.acquire(False)
Tim Petersc951bf92001-04-02 20:15:57 +0000243 if not gotit:
244 if __debug__:
245 self._note("%s.wait(%s): timed out", self, timeout)
246 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000247 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000248 except ValueError:
249 pass
250 else:
251 if __debug__:
252 self._note("%s.wait(%s): got it", self, timeout)
Georg Brandlb9a43912010-10-28 09:03:20 +0000253 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000254 finally:
255 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000256
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000257 def wait_for(self, predicate, timeout=None):
258 endtime = None
259 waittime = timeout
260 result = predicate()
261 while not result:
262 if waittime is not None:
263 if endtime is None:
264 endtime = _time() + waittime
265 else:
266 waittime = endtime - _time()
267 if waittime <= 0:
268 if __debug__:
269 self._note("%s.wait_for(%r, %r): Timed out.",
270 self, predicate, timeout)
271 break
272 if __debug__:
273 self._note("%s.wait_for(%r, %r): Waiting with timeout=%s.",
274 self, predicate, timeout, waittime)
275 self.wait(waittime)
276 result = predicate()
277 else:
278 if __debug__:
279 self._note("%s.wait_for(%r, %r): Success.",
280 self, predicate, timeout)
281 return result
282
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000283 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000284 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000285 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000286 __waiters = self._waiters
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000287 waiters = __waiters[:n]
288 if not waiters:
289 if __debug__:
290 self._note("%s.notify(): no waiters", self)
291 return
292 self._note("%s.notify(): notifying %d waiter%s", self, n,
293 n!=1 and "s" or "")
294 for waiter in waiters:
295 waiter.release()
296 try:
297 __waiters.remove(waiter)
298 except ValueError:
299 pass
300
Benjamin Peterson672b8032008-06-11 19:14:14 +0000301 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000302 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000303
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000304 notifyAll = notify_all
305
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000306
Éric Araujo0cdd4452011-07-28 00:28:28 +0200307class Semaphore(_Verbose):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000308
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000309 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000310
311 def __init__(self, value=1, verbose=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000312 if value < 0:
313 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000314 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000315 self._cond = Condition(Lock())
316 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000317
Antoine Pitrou0454af92010-04-17 23:51:58 +0000318 def acquire(self, blocking=True, timeout=None):
319 if not blocking and timeout is not None:
320 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000321 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000322 endtime = None
Guido van Rossumd0648992007-08-20 19:25:41 +0000323 self._cond.acquire()
324 while self._value == 0:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000325 if not blocking:
326 break
Skip Montanarob446fc72001-08-19 04:25:24 +0000327 if __debug__:
328 self._note("%s.acquire(%s): blocked waiting, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000329 self, blocking, self._value)
Antoine Pitrou0454af92010-04-17 23:51:58 +0000330 if timeout is not None:
331 if endtime is None:
332 endtime = _time() + timeout
333 else:
334 timeout = endtime - _time()
335 if timeout <= 0:
336 break
337 self._cond.wait(timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000338 else:
Guido van Rossumd0648992007-08-20 19:25:41 +0000339 self._value = self._value - 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000340 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000341 self._note("%s.acquire: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000342 self, self._value)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000343 rc = True
Guido van Rossumd0648992007-08-20 19:25:41 +0000344 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000345 return rc
346
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000347 __enter__ = acquire
348
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000349 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000350 self._cond.acquire()
351 self._value = self._value + 1
Skip Montanarob446fc72001-08-19 04:25:24 +0000352 if __debug__:
Skip Montanaroae8454a2001-08-19 05:53:47 +0000353 self._note("%s.release: success, value=%s",
Guido van Rossumd0648992007-08-20 19:25:41 +0000354 self, self._value)
355 self._cond.notify()
356 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000357
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000358 def __exit__(self, t, v, tb):
359 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000360
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000361
Éric Araujo0cdd4452011-07-28 00:28:28 +0200362class BoundedSemaphore(Semaphore):
Skip Montanaroe428bb72001-08-20 20:27:58 +0000363 """Semaphore that checks that # releases is <= # acquires"""
364 def __init__(self, value=1, verbose=None):
Éric Araujo0cdd4452011-07-28 00:28:28 +0200365 Semaphore.__init__(self, value, verbose)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000366 self._initial_value = value
367
368 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000369 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000370 raise ValueError("Semaphore released too many times")
Éric Araujo0cdd4452011-07-28 00:28:28 +0200371 return Semaphore.release(self)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000372
373
Éric Araujo0cdd4452011-07-28 00:28:28 +0200374class Event(_Verbose):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000375
376 # After Tim Peters' event class (without is_posted())
377
378 def __init__(self, verbose=None):
379 _Verbose.__init__(self, verbose)
Guido van Rossumd0648992007-08-20 19:25:41 +0000380 self._cond = Condition(Lock())
381 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000382
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000383 def _reset_internal_locks(self):
384 # private! called by Thread._reset_internal_locks by _after_fork()
385 self._cond.__init__()
386
Benjamin Peterson672b8032008-06-11 19:14:14 +0000387 def is_set(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000388 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000389
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000390 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000391
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000392 def set(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000393 self._cond.acquire()
394 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000395 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000396 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000397 finally:
398 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000399
400 def clear(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000401 self._cond.acquire()
402 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000403 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000404 finally:
405 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000406
407 def wait(self, timeout=None):
Christian Heimes969fe572008-01-25 11:23:10 +0000408 self._cond.acquire()
409 try:
Charles-François Natalided03482012-01-07 18:24:56 +0100410 signaled = self._flag
411 if not signaled:
412 signaled = self._cond.wait(timeout)
413 return signaled
Christian Heimes969fe572008-01-25 11:23:10 +0000414 finally:
415 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000416
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000417
418# A barrier class. Inspired in part by the pthread_barrier_* api and
419# the CyclicBarrier class from Java. See
420# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
421# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
422# CyclicBarrier.html
423# for information.
424# We maintain two main states, 'filling' and 'draining' enabling the barrier
425# to be cyclic. Threads are not allowed into it until it has fully drained
426# since the previous cycle. In addition, a 'resetting' state exists which is
427# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300428# and a 'broken' state in which all threads get the exception.
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000429class Barrier(_Verbose):
430 """
431 Barrier. Useful for synchronizing a fixed number of threads
432 at known synchronization points. Threads block on 'wait()' and are
433 simultaneously once they have all made that call.
434 """
435 def __init__(self, parties, action=None, timeout=None, verbose=None):
436 """
437 Create a barrier, initialised to 'parties' threads.
438 'action' is a callable which, when supplied, will be called
439 by one of the threads after they have all entered the
440 barrier and just prior to releasing them all.
441 If a 'timeout' is provided, it is uses as the default for
442 all subsequent 'wait()' calls.
443 """
444 _Verbose.__init__(self, verbose)
445 self._cond = Condition(Lock())
446 self._action = action
447 self._timeout = timeout
448 self._parties = parties
449 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
450 self._count = 0
451
452 def wait(self, timeout=None):
453 """
454 Wait for the barrier. When the specified number of threads have
455 started waiting, they are all simultaneously awoken. If an 'action'
456 was provided for the barrier, one of the threads will have executed
457 that callback prior to returning.
458 Returns an individual index number from 0 to 'parties-1'.
459 """
460 if timeout is None:
461 timeout = self._timeout
462 with self._cond:
463 self._enter() # Block while the barrier drains.
464 index = self._count
465 self._count += 1
466 try:
467 if index + 1 == self._parties:
468 # We release the barrier
469 self._release()
470 else:
471 # We wait until someone releases us
472 self._wait(timeout)
473 return index
474 finally:
475 self._count -= 1
476 # Wake up any threads waiting for barrier to drain.
477 self._exit()
478
479 # Block until the barrier is ready for us, or raise an exception
480 # if it is broken.
481 def _enter(self):
482 while self._state in (-1, 1):
483 # It is draining or resetting, wait until done
484 self._cond.wait()
485 #see if the barrier is in a broken state
486 if self._state < 0:
487 raise BrokenBarrierError
488 assert self._state == 0
489
490 # Optionally run the 'action' and release the threads waiting
491 # in the barrier.
492 def _release(self):
493 try:
494 if self._action:
495 self._action()
496 # enter draining state
497 self._state = 1
498 self._cond.notify_all()
499 except:
500 #an exception during the _action handler. Break and reraise
501 self._break()
502 raise
503
504 # Wait in the barrier until we are relased. Raise an exception
505 # if the barrier is reset or broken.
506 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000507 if not self._cond.wait_for(lambda : self._state != 0, timeout):
508 #timed out. Break the barrier
509 self._break()
510 raise BrokenBarrierError
511 if self._state < 0:
512 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000513 assert self._state == 1
514
515 # If we are the last thread to exit the barrier, signal any threads
516 # waiting for the barrier to drain.
517 def _exit(self):
518 if self._count == 0:
519 if self._state in (-1, 1):
520 #resetting or draining
521 self._state = 0
522 self._cond.notify_all()
523
524 def reset(self):
525 """
526 Reset the barrier to the initial state.
527 Any threads currently waiting will get the BrokenBarrier exception
528 raised.
529 """
530 with self._cond:
531 if self._count > 0:
532 if self._state == 0:
533 #reset the barrier, waking up threads
534 self._state = -1
535 elif self._state == -2:
536 #was broken, set it to reset state
537 #which clears when the last thread exits
538 self._state = -1
539 else:
540 self._state = 0
541 self._cond.notify_all()
542
543 def abort(self):
544 """
545 Place the barrier into a 'broken' state.
546 Useful in case of error. Any currently waiting threads and
547 threads attempting to 'wait()' will have BrokenBarrierError
548 raised.
549 """
550 with self._cond:
551 self._break()
552
553 def _break(self):
554 # An internal error was detected. The barrier is set to
555 # a broken state all parties awakened.
556 self._state = -2
557 self._cond.notify_all()
558
559 @property
560 def parties(self):
561 """
562 Return the number of threads required to trip the barrier.
563 """
564 return self._parties
565
566 @property
567 def n_waiting(self):
568 """
569 Return the number of threads that are currently waiting at the barrier.
570 """
571 # We don't need synchronization here since this is an ephemeral result
572 # anyway. It returns the correct value in the steady state.
573 if self._state == 0:
574 return self._count
575 return 0
576
577 @property
578 def broken(self):
579 """
580 Return True if the barrier is in a broken state
581 """
582 return self._state == -2
583
584#exception raised by the Barrier class
585class BrokenBarrierError(RuntimeError): pass
586
587
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000588# Helper to generate new thread names
589_counter = 0
590def _newname(template="Thread-%d"):
591 global _counter
592 _counter = _counter + 1
593 return template % _counter
594
595# Active thread administration
596_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000597_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000598_limbo = {}
599
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200600# For debug and leak testing
601_dangling = WeakSet()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000602
603# Main class for threads
604
605class Thread(_Verbose):
606
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000607 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000608 # Need to store a reference to sys.exc_info for printing
609 # out exceptions when a thread tries to use a global var. during interp.
610 # shutdown and thus raises an exception about trying to perform some
611 # operation on/with a NoneType
612 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000613 # Keep sys.exc_clear too to clear the exception just before
614 # allowing .join() to return.
615 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000616
617 def __init__(self, group=None, target=None, name=None,
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000618 args=(), kwargs=None, verbose=None, *, daemon=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000619 assert group is None, "group argument must be None for now"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000620 _Verbose.__init__(self, verbose)
Georg Brandla4a8b822005-07-15 09:13:21 +0000621 if kwargs is None:
622 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000623 self._target = target
624 self._name = str(name or _newname())
625 self._args = args
626 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000627 if daemon is not None:
628 self._daemonic = daemon
629 else:
630 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000631 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000632 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000633 self._stopped = False
634 self._block = Condition(Lock())
635 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000636 # sys.stderr is not stored in the class like
637 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000638 self._stderr = _sys.stderr
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200639 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000640
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000641 def _reset_internal_locks(self):
642 # private! Called by _after_fork() to reset our internal locks as
643 # they may be in an invalid state leading to a deadlock or crash.
644 if hasattr(self, '_block'): # DummyThread deletes _block
645 self._block.__init__()
646 self._started._reset_internal_locks()
647
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000648 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000649 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000650 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000651 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000652 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000653 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000654 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000655 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000656 status += " daemon"
657 if self._ident is not None:
658 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000659 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000660
661 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000662 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000663 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000664
Benjamin Peterson672b8032008-06-11 19:14:14 +0000665 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000666 raise RuntimeError("threads can only be started once")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000667 if __debug__:
668 self._note("%s.start(): starting thread", self)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000669 with _active_limbo_lock:
670 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000671 try:
672 _start_new_thread(self._bootstrap, ())
673 except Exception:
674 with _active_limbo_lock:
675 del _limbo[self]
676 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000677 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000678
679 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000680 try:
681 if self._target:
682 self._target(*self._args, **self._kwargs)
683 finally:
684 # Avoid a refcycle if the thread is running a function with
685 # an argument that has a member that points to the thread.
686 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000687
Guido van Rossumd0648992007-08-20 19:25:41 +0000688 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000689 # Wrapper around the real bootstrap code that ignores
690 # exceptions during interpreter cleanup. Those typically
691 # happen when a daemon thread wakes up at an unfortunate
692 # moment, finds the world around it destroyed, and raises some
693 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000694 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000695 # don't help anybody, and they confuse users, so we suppress
696 # them. We suppress them only when it appears that the world
697 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000698 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000699 # reported. Also, we only suppress them for daemonic threads;
700 # if a non-daemonic encounters this, something else is wrong.
701 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000702 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000703 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000704 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000705 return
706 raise
707
Benjamin Petersond23f8222009-04-05 19:13:16 +0000708 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200709 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000710
Guido van Rossumd0648992007-08-20 19:25:41 +0000711 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000712 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000713 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000714 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000715 with _active_limbo_lock:
716 _active[self._ident] = self
717 del _limbo[self]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000718 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000719 self._note("%s._bootstrap(): thread started", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000720
721 if _trace_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000722 self._note("%s._bootstrap(): registering trace hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000723 _sys.settrace(_trace_hook)
724 if _profile_hook:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000725 self._note("%s._bootstrap(): registering profile hook", self)
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000726 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000727
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000728 try:
729 self.run()
730 except SystemExit:
731 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000732 self._note("%s._bootstrap(): raised SystemExit", self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000733 except:
734 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000735 self._note("%s._bootstrap(): unhandled exception", self)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000736 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000737 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000738 # _sys) in case sys.stderr was redefined since the creation of
739 # self.
740 if _sys:
741 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000742 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000743 else:
744 # Do the best job possible w/o a huge amt. of code to
745 # approximate a traceback (code ideas from
746 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000747 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000748 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000749 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000750 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000751 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000752 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000753 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000754 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000755 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000756 ' File "%s", line %s, in %s' %
757 (exc_tb.tb_frame.f_code.co_filename,
758 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000759 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000760 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000761 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000762 # Make sure that exc_tb gets deleted since it is a memory
763 # hog; deleting everything else is just for thoroughness
764 finally:
765 del exc_type, exc_value, exc_tb
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000766 else:
767 if __debug__:
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000768 self._note("%s._bootstrap(): normal return", self)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000769 finally:
770 # Prevent a race in
771 # test_threading.test_no_refcycle_through_target when
772 # the exception keeps the target alive past when we
773 # assert that it's dead.
774 #XXX self.__exc_clear()
775 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000776 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000777 with _active_limbo_lock:
778 self._stop()
779 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000780 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000781 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200782 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000783 except:
784 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000785
Guido van Rossumd0648992007-08-20 19:25:41 +0000786 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000787 self._block.acquire()
788 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000789 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000790 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000791
Guido van Rossumd0648992007-08-20 19:25:41 +0000792 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000793 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000794
Georg Brandl2067bfd2008-05-25 13:05:15 +0000795 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000796 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000797 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000798 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000799 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
800 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000801 # len(_active) is always <= 1 here, and any Thread instance created
802 # overwrites the (if any) thread currently registered in _active.
803 #
804 # An instance of _MainThread is always created by 'threading'. This
805 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000806 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000807 # same key in the dict. So when the _MainThread instance created by
808 # 'threading' tries to clean itself up when atexit calls this method
809 # it gets a KeyError if another Thread instance was created.
810 #
811 # This all means that KeyError from trying to delete something from
812 # _active if dummy_threading is being used is a red herring. But
813 # since it isn't if dummy_threading is *not* being used then don't
814 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000815
Christian Heimes969fe572008-01-25 11:23:10 +0000816 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000817 with _active_limbo_lock:
Victor Stinner2a129742011-05-30 23:02:52 +0200818 del _active[get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000819 # There must not be any python code between the previous line
820 # and after the lock is released. Otherwise a tracing function
821 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000822 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000823 except KeyError:
824 if 'dummy_threading' not in _sys.modules:
825 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000826
827 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000828 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000829 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000830 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000831 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000832 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000833 raise RuntimeError("cannot join current thread")
834
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000835 if __debug__:
Guido van Rossumd0648992007-08-20 19:25:41 +0000836 if not self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000837 self._note("%s.join(): waiting until thread stops", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000838
839 self._block.acquire()
840 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000841 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000842 while not self._stopped:
843 self._block.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000844 if __debug__:
845 self._note("%s.join(): thread stopped", self)
Brett Cannonad07ff22005-11-23 02:15:50 +0000846 else:
847 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000848 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000849 delay = deadline - _time()
850 if delay <= 0:
851 if __debug__:
852 self._note("%s.join(): timed out", self)
853 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000854 self._block.wait(delay)
Brett Cannonad07ff22005-11-23 02:15:50 +0000855 else:
856 if __debug__:
857 self._note("%s.join(): thread stopped", self)
Christian Heimes969fe572008-01-25 11:23:10 +0000858 finally:
859 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000860
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000861 @property
862 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000863 assert self._initialized, "Thread.__init__() not called"
864 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000865
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000866 @name.setter
867 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000868 assert self._initialized, "Thread.__init__() not called"
869 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000870
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000871 @property
872 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000873 assert self._initialized, "Thread.__init__() not called"
874 return self._ident
875
Benjamin Peterson672b8032008-06-11 19:14:14 +0000876 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000877 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000878 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000879
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000880 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000881
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000882 @property
883 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000884 assert self._initialized, "Thread.__init__() not called"
885 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000886
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000887 @daemon.setter
888 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000889 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000890 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000891 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000892 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000893 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000894
Benjamin Peterson6640d722008-08-18 18:16:46 +0000895 def isDaemon(self):
896 return self.daemon
897
898 def setDaemon(self, daemonic):
899 self.daemon = daemonic
900
901 def getName(self):
902 return self.name
903
904 def setName(self, name):
905 self.name = name
906
Martin v. Löwis44f86962001-09-05 13:44:54 +0000907# The timer class was contributed by Itamar Shtull-Trauring
908
Éric Araujo0cdd4452011-07-28 00:28:28 +0200909class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +0000910 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000911
Martin v. Löwis44f86962001-09-05 13:44:54 +0000912 t = Timer(30.0, f, args=[], kwargs={})
913 t.start()
914 t.cancel() # stop the timer's action if it's still waiting
915 """
Tim Petersb64bec32001-09-18 02:26:39 +0000916
Martin v. Löwis44f86962001-09-05 13:44:54 +0000917 def __init__(self, interval, function, args=[], kwargs={}):
918 Thread.__init__(self)
919 self.interval = interval
920 self.function = function
921 self.args = args
922 self.kwargs = kwargs
923 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000924
Martin v. Löwis44f86962001-09-05 13:44:54 +0000925 def cancel(self):
926 """Stop the timer if it hasn't finished yet"""
927 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000928
Martin v. Löwis44f86962001-09-05 13:44:54 +0000929 def run(self):
930 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000931 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000932 self.function(*self.args, **self.kwargs)
933 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000934
935# Special thread class to represent the main thread
936# This is garbage collected through an exit handler
937
938class _MainThread(Thread):
939
940 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000941 Thread.__init__(self, name="MainThread", daemon=False)
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000942 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000943 self._set_ident()
944 with _active_limbo_lock:
945 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000946
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000947 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000948 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000949 t = _pickSomeNonDaemonThread()
950 if t:
951 if __debug__:
952 self._note("%s: waiting for other threads", self)
953 while t:
954 t.join()
955 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000956 if __debug__:
957 self._note("%s: exiting", self)
Guido van Rossumd0648992007-08-20 19:25:41 +0000958 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000959
960def _pickSomeNonDaemonThread():
961 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000962 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000963 return t
964 return None
965
966
967# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000968# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000969# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000970# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000971# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000972# They are marked as daemon threads so we won't wait for them
973# when we exit (conform previous semantics).
974
975class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000976
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000977 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000978 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +0000979
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000980 # Thread._block consumes an OS-level locking primitive, which
Tim Peters711906e2005-01-08 07:30:42 +0000981 # can never be used by a _DummyThread. Since a _DummyThread
982 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +0000983 del self._block
Tim Peters711906e2005-01-08 07:30:42 +0000984
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000985 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000986 self._set_ident()
987 with _active_limbo_lock:
988 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000989
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000990 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000991 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000992
993
994# Global API functions
995
Benjamin Peterson672b8032008-06-11 19:14:14 +0000996def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000997 try:
Victor Stinner2a129742011-05-30 23:02:52 +0200998 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000999 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001000 return _DummyThread()
1001
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001002currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001003
Benjamin Peterson672b8032008-06-11 19:14:14 +00001004def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +00001005 with _active_limbo_lock:
1006 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001007
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001008activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001009
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001010def _enumerate():
1011 # Same as enumerate(), but without the lock. Internal use only.
1012 return list(_active.values()) + list(_limbo.values())
1013
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001014def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +00001015 with _active_limbo_lock:
1016 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001017
Georg Brandl2067bfd2008-05-25 13:05:15 +00001018from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001019
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001020# Create the main thread object,
1021# and make it available for the interpreter
1022# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001023
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001024_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001025
Jim Fultond15dc062004-07-14 19:11:50 +00001026# get thread-local implementation, either from the thread
1027# module, or from the python fallback
1028
1029try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001030 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +00001031except ImportError:
1032 from _threading_local import local
1033
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001034
Jesse Nollera8513972008-07-17 16:49:17 +00001035def _after_fork():
1036 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
1037 # is called from PyOS_AfterFork. Here we cleanup threading module state
1038 # that should not exist after a fork.
1039
1040 # Reset _active_limbo_lock, in case we forked while the lock was held
1041 # by another (non-forked) thread. http://bugs.python.org/issue874900
1042 global _active_limbo_lock
1043 _active_limbo_lock = _allocate_lock()
1044
1045 # fork() only copied the current thread; clear references to others.
1046 new_active = {}
1047 current = current_thread()
1048 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001049 for thread in _active.values():
Charles-François Natalib055bf62011-12-18 18:45:16 +01001050 # Any lock/condition variable may be currently locked or in an
1051 # invalid state, so we reinitialize them.
1052 thread._reset_internal_locks()
Jesse Nollera8513972008-07-17 16:49:17 +00001053 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001054 # There is only one active thread. We reset the ident to
1055 # its new value since it can have changed.
Victor Stinner2a129742011-05-30 23:02:52 +02001056 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001057 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +00001058 new_active[ident] = thread
1059 else:
1060 # All the others are already stopped.
Charles-François Natalib055bf62011-12-18 18:45:16 +01001061 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +00001062
1063 _limbo.clear()
1064 _active.clear()
1065 _active.update(new_active)
1066 assert len(_active) == 1