blob: 441c7bd9032e5b2f65c4adda93b435e1ac4666c8 [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
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000037# Support for profile and trace hooks
38
39_profile_hook = None
40_trace_hook = None
41
42def setprofile(func):
43 global _profile_hook
44 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000045
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000046def settrace(func):
47 global _trace_hook
48 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000049
50# Synchronization classes
51
52Lock = _allocate_lock
53
Victor Stinner135b6d82012-03-03 01:32:57 +010054def RLock(*args, **kwargs):
55 if _CRLock is None:
56 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000057 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000058
Victor Stinner135b6d82012-03-03 01:32:57 +010059class _RLock:
Tim Petersb90f89a2001-01-15 03:26:36 +000060
Victor Stinner135b6d82012-03-03 01:32:57 +010061 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000062 self._block = _allocate_lock()
63 self._owner = None
64 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +000065
66 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000067 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +000068 try:
69 owner = _active[owner].name
70 except KeyError:
71 pass
72 return "<%s owner=%r count=%d>" % (
73 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000074
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000075 def acquire(self, blocking=True, timeout=-1):
Victor Stinner2a129742011-05-30 23:02:52 +020076 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +000077 if self._owner == me:
Guido van Rossumd0648992007-08-20 19:25:41 +000078 self._count = self._count + 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000079 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000080 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000081 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +000082 self._owner = me
83 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000084 return rc
85
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000086 __enter__ = acquire
87
Guido van Rossum7f5013a1998-04-09 22:01:42 +000088 def release(self):
Victor Stinner2a129742011-05-30 23:02:52 +020089 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +000090 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +000091 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000092 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +000093 self._owner = None
94 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +000095
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000096 def __exit__(self, t, v, tb):
97 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000098
Guido van Rossum7f5013a1998-04-09 22:01:42 +000099 # Internal methods used by condition variables
100
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000101 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000102 self._block.acquire()
103 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000104
105 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200106 if self._count == 0:
107 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000108 count = self._count
109 self._count = 0
110 owner = self._owner
111 self._owner = None
112 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000113 return (count, owner)
114
115 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200116 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000117
Antoine Pitrou434736a2009-11-10 18:46:01 +0000118_PyRLock = _RLock
119
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000120
Victor Stinner135b6d82012-03-03 01:32:57 +0100121class Condition:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000122
Victor Stinner135b6d82012-03-03 01:32:57 +0100123 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000124 if lock is None:
125 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000126 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000127 # Export the lock's acquire() and release() methods
128 self.acquire = lock.acquire
129 self.release = lock.release
130 # If the lock defines _release_save() and/or _acquire_restore(),
131 # these override the default implementations (which just call
132 # release() and acquire() on the lock). Ditto for _is_owned().
133 try:
134 self._release_save = lock._release_save
135 except AttributeError:
136 pass
137 try:
138 self._acquire_restore = lock._acquire_restore
139 except AttributeError:
140 pass
141 try:
142 self._is_owned = lock._is_owned
143 except AttributeError:
144 pass
Guido van Rossumd0648992007-08-20 19:25:41 +0000145 self._waiters = []
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000146
Thomas Wouters477c8d52006-05-27 19:21:47 +0000147 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000148 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000149
Thomas Wouters477c8d52006-05-27 19:21:47 +0000150 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000151 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000152
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000153 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000154 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000155
156 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000157 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000158
159 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000160 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000161
162 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000163 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000164 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000165 if self._lock.acquire(0):
166 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000167 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000168 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000169 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000170
171 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000172 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000173 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000174 waiter = _allocate_lock()
175 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000176 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000177 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000178 try: # restore state no matter what (e.g., KeyboardInterrupt)
179 if timeout is None:
180 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000181 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000182 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000183 if timeout > 0:
184 gotit = waiter.acquire(True, timeout)
185 else:
186 gotit = waiter.acquire(False)
Tim Petersc951bf92001-04-02 20:15:57 +0000187 if not gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000188 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000189 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000190 except ValueError:
191 pass
Georg Brandlb9a43912010-10-28 09:03:20 +0000192 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000193 finally:
194 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000195
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000196 def wait_for(self, predicate, timeout=None):
197 endtime = None
198 waittime = timeout
199 result = predicate()
200 while not result:
201 if waittime is not None:
202 if endtime is None:
203 endtime = _time() + waittime
204 else:
205 waittime = endtime - _time()
206 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000207 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000208 self.wait(waittime)
209 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000210 return result
211
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000212 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000213 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000214 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000215 __waiters = self._waiters
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000216 waiters = __waiters[:n]
217 if not waiters:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000218 return
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000219 for waiter in waiters:
220 waiter.release()
221 try:
222 __waiters.remove(waiter)
223 except ValueError:
224 pass
225
Benjamin Peterson672b8032008-06-11 19:14:14 +0000226 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000227 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000228
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000229 notifyAll = notify_all
230
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000231
Victor Stinner135b6d82012-03-03 01:32:57 +0100232class Semaphore:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000233
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000234 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000235
Victor Stinner135b6d82012-03-03 01:32:57 +0100236 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000237 if value < 0:
238 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000239 self._cond = Condition(Lock())
240 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000241
Antoine Pitrou0454af92010-04-17 23:51:58 +0000242 def acquire(self, blocking=True, timeout=None):
243 if not blocking and timeout is not None:
244 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000245 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000246 endtime = None
Guido van Rossumd0648992007-08-20 19:25:41 +0000247 self._cond.acquire()
248 while self._value == 0:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000249 if not blocking:
250 break
Antoine Pitrou0454af92010-04-17 23:51:58 +0000251 if timeout is not None:
252 if endtime is None:
253 endtime = _time() + timeout
254 else:
255 timeout = endtime - _time()
256 if timeout <= 0:
257 break
258 self._cond.wait(timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000259 else:
Guido van Rossumd0648992007-08-20 19:25:41 +0000260 self._value = self._value - 1
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000261 rc = True
Guido van Rossumd0648992007-08-20 19:25:41 +0000262 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000263 return rc
264
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000265 __enter__ = acquire
266
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000267 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000268 self._cond.acquire()
269 self._value = self._value + 1
Guido van Rossumd0648992007-08-20 19:25:41 +0000270 self._cond.notify()
271 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000272
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000273 def __exit__(self, t, v, tb):
274 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000275
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000276
Éric Araujo0cdd4452011-07-28 00:28:28 +0200277class BoundedSemaphore(Semaphore):
Skip Montanaroe428bb72001-08-20 20:27:58 +0000278 """Semaphore that checks that # releases is <= # acquires"""
Victor Stinner135b6d82012-03-03 01:32:57 +0100279 def __init__(self, value=1):
280 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000281 self._initial_value = value
282
283 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000284 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000285 raise ValueError("Semaphore released too many times")
Éric Araujo0cdd4452011-07-28 00:28:28 +0200286 return Semaphore.release(self)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000287
288
Victor Stinner135b6d82012-03-03 01:32:57 +0100289class Event:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000290
291 # After Tim Peters' event class (without is_posted())
292
Victor Stinner135b6d82012-03-03 01:32:57 +0100293 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000294 self._cond = Condition(Lock())
295 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000296
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000297 def _reset_internal_locks(self):
298 # private! called by Thread._reset_internal_locks by _after_fork()
299 self._cond.__init__()
300
Benjamin Peterson672b8032008-06-11 19:14:14 +0000301 def is_set(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000302 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000303
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000304 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000305
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000306 def set(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000307 self._cond.acquire()
308 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000309 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000310 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000311 finally:
312 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000313
314 def clear(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000315 self._cond.acquire()
316 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000317 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000318 finally:
319 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000320
321 def wait(self, timeout=None):
Christian Heimes969fe572008-01-25 11:23:10 +0000322 self._cond.acquire()
323 try:
Charles-François Natalided03482012-01-07 18:24:56 +0100324 signaled = self._flag
325 if not signaled:
326 signaled = self._cond.wait(timeout)
327 return signaled
Christian Heimes969fe572008-01-25 11:23:10 +0000328 finally:
329 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000330
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000331
332# A barrier class. Inspired in part by the pthread_barrier_* api and
333# the CyclicBarrier class from Java. See
334# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
335# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
336# CyclicBarrier.html
337# for information.
338# We maintain two main states, 'filling' and 'draining' enabling the barrier
339# to be cyclic. Threads are not allowed into it until it has fully drained
340# since the previous cycle. In addition, a 'resetting' state exists which is
341# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300342# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100343class Barrier:
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000344 """
345 Barrier. Useful for synchronizing a fixed number of threads
346 at known synchronization points. Threads block on 'wait()' and are
347 simultaneously once they have all made that call.
348 """
Victor Stinner135b6d82012-03-03 01:32:57 +0100349 def __init__(self, parties, action=None, timeout=None):
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000350 """
351 Create a barrier, initialised to 'parties' threads.
352 'action' is a callable which, when supplied, will be called
353 by one of the threads after they have all entered the
354 barrier and just prior to releasing them all.
355 If a 'timeout' is provided, it is uses as the default for
356 all subsequent 'wait()' calls.
357 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000358 self._cond = Condition(Lock())
359 self._action = action
360 self._timeout = timeout
361 self._parties = parties
362 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
363 self._count = 0
364
365 def wait(self, timeout=None):
366 """
367 Wait for the barrier. When the specified number of threads have
368 started waiting, they are all simultaneously awoken. If an 'action'
369 was provided for the barrier, one of the threads will have executed
370 that callback prior to returning.
371 Returns an individual index number from 0 to 'parties-1'.
372 """
373 if timeout is None:
374 timeout = self._timeout
375 with self._cond:
376 self._enter() # Block while the barrier drains.
377 index = self._count
378 self._count += 1
379 try:
380 if index + 1 == self._parties:
381 # We release the barrier
382 self._release()
383 else:
384 # We wait until someone releases us
385 self._wait(timeout)
386 return index
387 finally:
388 self._count -= 1
389 # Wake up any threads waiting for barrier to drain.
390 self._exit()
391
392 # Block until the barrier is ready for us, or raise an exception
393 # if it is broken.
394 def _enter(self):
395 while self._state in (-1, 1):
396 # It is draining or resetting, wait until done
397 self._cond.wait()
398 #see if the barrier is in a broken state
399 if self._state < 0:
400 raise BrokenBarrierError
401 assert self._state == 0
402
403 # Optionally run the 'action' and release the threads waiting
404 # in the barrier.
405 def _release(self):
406 try:
407 if self._action:
408 self._action()
409 # enter draining state
410 self._state = 1
411 self._cond.notify_all()
412 except:
413 #an exception during the _action handler. Break and reraise
414 self._break()
415 raise
416
417 # Wait in the barrier until we are relased. Raise an exception
418 # if the barrier is reset or broken.
419 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000420 if not self._cond.wait_for(lambda : self._state != 0, timeout):
421 #timed out. Break the barrier
422 self._break()
423 raise BrokenBarrierError
424 if self._state < 0:
425 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000426 assert self._state == 1
427
428 # If we are the last thread to exit the barrier, signal any threads
429 # waiting for the barrier to drain.
430 def _exit(self):
431 if self._count == 0:
432 if self._state in (-1, 1):
433 #resetting or draining
434 self._state = 0
435 self._cond.notify_all()
436
437 def reset(self):
438 """
439 Reset the barrier to the initial state.
440 Any threads currently waiting will get the BrokenBarrier exception
441 raised.
442 """
443 with self._cond:
444 if self._count > 0:
445 if self._state == 0:
446 #reset the barrier, waking up threads
447 self._state = -1
448 elif self._state == -2:
449 #was broken, set it to reset state
450 #which clears when the last thread exits
451 self._state = -1
452 else:
453 self._state = 0
454 self._cond.notify_all()
455
456 def abort(self):
457 """
458 Place the barrier into a 'broken' state.
459 Useful in case of error. Any currently waiting threads and
460 threads attempting to 'wait()' will have BrokenBarrierError
461 raised.
462 """
463 with self._cond:
464 self._break()
465
466 def _break(self):
467 # An internal error was detected. The barrier is set to
468 # a broken state all parties awakened.
469 self._state = -2
470 self._cond.notify_all()
471
472 @property
473 def parties(self):
474 """
475 Return the number of threads required to trip the barrier.
476 """
477 return self._parties
478
479 @property
480 def n_waiting(self):
481 """
482 Return the number of threads that are currently waiting at the barrier.
483 """
484 # We don't need synchronization here since this is an ephemeral result
485 # anyway. It returns the correct value in the steady state.
486 if self._state == 0:
487 return self._count
488 return 0
489
490 @property
491 def broken(self):
492 """
493 Return True if the barrier is in a broken state
494 """
495 return self._state == -2
496
497#exception raised by the Barrier class
498class BrokenBarrierError(RuntimeError): pass
499
500
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000501# Helper to generate new thread names
502_counter = 0
503def _newname(template="Thread-%d"):
504 global _counter
505 _counter = _counter + 1
506 return template % _counter
507
508# Active thread administration
509_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000510_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000511_limbo = {}
512
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200513# For debug and leak testing
514_dangling = WeakSet()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000515
516# Main class for threads
517
Victor Stinner135b6d82012-03-03 01:32:57 +0100518class Thread:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000519
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000520 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000521 # Need to store a reference to sys.exc_info for printing
522 # out exceptions when a thread tries to use a global var. during interp.
523 # shutdown and thus raises an exception about trying to perform some
524 # operation on/with a NoneType
525 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000526 # Keep sys.exc_clear too to clear the exception just before
527 # allowing .join() to return.
528 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000529
530 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100531 args=(), kwargs=None, *, daemon=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000532 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000533 if kwargs is None:
534 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000535 self._target = target
536 self._name = str(name or _newname())
537 self._args = args
538 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000539 if daemon is not None:
540 self._daemonic = daemon
541 else:
542 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000543 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000544 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000545 self._stopped = False
546 self._block = Condition(Lock())
547 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000548 # sys.stderr is not stored in the class like
549 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000550 self._stderr = _sys.stderr
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200551 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000552
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000553 def _reset_internal_locks(self):
554 # private! Called by _after_fork() to reset our internal locks as
555 # they may be in an invalid state leading to a deadlock or crash.
556 if hasattr(self, '_block'): # DummyThread deletes _block
557 self._block.__init__()
558 self._started._reset_internal_locks()
559
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000560 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000561 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000562 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000563 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000564 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000565 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000566 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000567 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000568 status += " daemon"
569 if self._ident is not None:
570 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000571 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000572
573 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000574 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000575 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000576
Benjamin Peterson672b8032008-06-11 19:14:14 +0000577 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000578 raise RuntimeError("threads can only be started once")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000579 with _active_limbo_lock:
580 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000581 try:
582 _start_new_thread(self._bootstrap, ())
583 except Exception:
584 with _active_limbo_lock:
585 del _limbo[self]
586 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000587 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000588
589 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000590 try:
591 if self._target:
592 self._target(*self._args, **self._kwargs)
593 finally:
594 # Avoid a refcycle if the thread is running a function with
595 # an argument that has a member that points to the thread.
596 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000597
Guido van Rossumd0648992007-08-20 19:25:41 +0000598 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000599 # Wrapper around the real bootstrap code that ignores
600 # exceptions during interpreter cleanup. Those typically
601 # happen when a daemon thread wakes up at an unfortunate
602 # moment, finds the world around it destroyed, and raises some
603 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000604 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000605 # don't help anybody, and they confuse users, so we suppress
606 # them. We suppress them only when it appears that the world
607 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000608 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000609 # reported. Also, we only suppress them for daemonic threads;
610 # if a non-daemonic encounters this, something else is wrong.
611 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000612 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000613 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000614 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000615 return
616 raise
617
Benjamin Petersond23f8222009-04-05 19:13:16 +0000618 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200619 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000620
Guido van Rossumd0648992007-08-20 19:25:41 +0000621 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000622 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000623 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000624 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000625 with _active_limbo_lock:
626 _active[self._ident] = self
627 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000628
629 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000630 _sys.settrace(_trace_hook)
631 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000632 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000633
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000634 try:
635 self.run()
636 except SystemExit:
Victor Stinner135b6d82012-03-03 01:32:57 +0100637 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000638 except:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000639 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000640 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000641 # _sys) in case sys.stderr was redefined since the creation of
642 # self.
643 if _sys:
644 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000645 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000646 else:
647 # Do the best job possible w/o a huge amt. of code to
648 # approximate a traceback (code ideas from
649 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000650 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000651 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000652 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000653 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000654 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000655 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000656 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000657 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000658 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000659 ' File "%s", line %s, in %s' %
660 (exc_tb.tb_frame.f_code.co_filename,
661 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000662 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000663 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000664 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000665 # Make sure that exc_tb gets deleted since it is a memory
666 # hog; deleting everything else is just for thoroughness
667 finally:
668 del exc_type, exc_value, exc_tb
Christian Heimesbbe741d2008-03-28 10:53:29 +0000669 finally:
670 # Prevent a race in
671 # test_threading.test_no_refcycle_through_target when
672 # the exception keeps the target alive past when we
673 # assert that it's dead.
674 #XXX self.__exc_clear()
675 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000676 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000677 with _active_limbo_lock:
678 self._stop()
679 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000680 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000681 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200682 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000683 except:
684 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000685
Guido van Rossumd0648992007-08-20 19:25:41 +0000686 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000687 self._block.acquire()
688 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000689 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000690 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000691
Guido van Rossumd0648992007-08-20 19:25:41 +0000692 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000693 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000694
Georg Brandl2067bfd2008-05-25 13:05:15 +0000695 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000696 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000697 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000698 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000699 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
700 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000701 # len(_active) is always <= 1 here, and any Thread instance created
702 # overwrites the (if any) thread currently registered in _active.
703 #
704 # An instance of _MainThread is always created by 'threading'. This
705 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000706 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000707 # same key in the dict. So when the _MainThread instance created by
708 # 'threading' tries to clean itself up when atexit calls this method
709 # it gets a KeyError if another Thread instance was created.
710 #
711 # This all means that KeyError from trying to delete something from
712 # _active if dummy_threading is being used is a red herring. But
713 # since it isn't if dummy_threading is *not* being used then don't
714 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000715
Christian Heimes969fe572008-01-25 11:23:10 +0000716 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000717 with _active_limbo_lock:
Victor Stinner2a129742011-05-30 23:02:52 +0200718 del _active[get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000719 # There must not be any python code between the previous line
720 # and after the lock is released. Otherwise a tracing function
721 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000722 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000723 except KeyError:
724 if 'dummy_threading' not in _sys.modules:
725 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000726
727 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000728 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000729 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000730 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000731 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000732 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000733 raise RuntimeError("cannot join current thread")
734
Christian Heimes969fe572008-01-25 11:23:10 +0000735 self._block.acquire()
736 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000737 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000738 while not self._stopped:
739 self._block.wait()
Brett Cannonad07ff22005-11-23 02:15:50 +0000740 else:
741 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000742 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000743 delay = deadline - _time()
744 if delay <= 0:
Brett Cannonad07ff22005-11-23 02:15:50 +0000745 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000746 self._block.wait(delay)
Christian Heimes969fe572008-01-25 11:23:10 +0000747 finally:
748 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000749
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000750 @property
751 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000752 assert self._initialized, "Thread.__init__() not called"
753 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000754
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000755 @name.setter
756 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000757 assert self._initialized, "Thread.__init__() not called"
758 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000759
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000760 @property
761 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000762 assert self._initialized, "Thread.__init__() not called"
763 return self._ident
764
Benjamin Peterson672b8032008-06-11 19:14:14 +0000765 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000766 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000767 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000768
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000769 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000770
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000771 @property
772 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000773 assert self._initialized, "Thread.__init__() not called"
774 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000775
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000776 @daemon.setter
777 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000778 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000779 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000780 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000781 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000782 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000783
Benjamin Peterson6640d722008-08-18 18:16:46 +0000784 def isDaemon(self):
785 return self.daemon
786
787 def setDaemon(self, daemonic):
788 self.daemon = daemonic
789
790 def getName(self):
791 return self.name
792
793 def setName(self, name):
794 self.name = name
795
Martin v. Löwis44f86962001-09-05 13:44:54 +0000796# The timer class was contributed by Itamar Shtull-Trauring
797
Éric Araujo0cdd4452011-07-28 00:28:28 +0200798class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +0000799 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000800
Martin v. Löwis44f86962001-09-05 13:44:54 +0000801 t = Timer(30.0, f, args=[], kwargs={})
802 t.start()
803 t.cancel() # stop the timer's action if it's still waiting
804 """
Tim Petersb64bec32001-09-18 02:26:39 +0000805
Martin v. Löwis44f86962001-09-05 13:44:54 +0000806 def __init__(self, interval, function, args=[], kwargs={}):
807 Thread.__init__(self)
808 self.interval = interval
809 self.function = function
810 self.args = args
811 self.kwargs = kwargs
812 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000813
Martin v. Löwis44f86962001-09-05 13:44:54 +0000814 def cancel(self):
815 """Stop the timer if it hasn't finished yet"""
816 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000817
Martin v. Löwis44f86962001-09-05 13:44:54 +0000818 def run(self):
819 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000820 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000821 self.function(*self.args, **self.kwargs)
822 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000823
824# Special thread class to represent the main thread
825# This is garbage collected through an exit handler
826
827class _MainThread(Thread):
828
829 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000830 Thread.__init__(self, name="MainThread", daemon=False)
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000831 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000832 self._set_ident()
833 with _active_limbo_lock:
834 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000835
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000836 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000837 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000838 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000839 while t:
840 t.join()
841 t = _pickSomeNonDaemonThread()
Guido van Rossumd0648992007-08-20 19:25:41 +0000842 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000843
844def _pickSomeNonDaemonThread():
845 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000846 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000847 return t
848 return None
849
850
851# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000852# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000853# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000854# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000855# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000856# They are marked as daemon threads so we won't wait for them
857# when we exit (conform previous semantics).
858
859class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000860
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000861 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000862 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +0000863
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000864 # Thread._block consumes an OS-level locking primitive, which
Tim Peters711906e2005-01-08 07:30:42 +0000865 # can never be used by a _DummyThread. Since a _DummyThread
866 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +0000867 del self._block
Tim Peters711906e2005-01-08 07:30:42 +0000868
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000869 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000870 self._set_ident()
871 with _active_limbo_lock:
872 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000873
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000874 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000875 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000876
877
878# Global API functions
879
Benjamin Peterson672b8032008-06-11 19:14:14 +0000880def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000881 try:
Victor Stinner2a129742011-05-30 23:02:52 +0200882 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000883 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000884 return _DummyThread()
885
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000886currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000887
Benjamin Peterson672b8032008-06-11 19:14:14 +0000888def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000889 with _active_limbo_lock:
890 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000891
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000892activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000893
Antoine Pitroubdec11f2009-11-05 13:49:14 +0000894def _enumerate():
895 # Same as enumerate(), but without the lock. Internal use only.
896 return list(_active.values()) + list(_limbo.values())
897
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000898def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000899 with _active_limbo_lock:
900 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000901
Georg Brandl2067bfd2008-05-25 13:05:15 +0000902from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000903
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000904# Create the main thread object,
905# and make it available for the interpreter
906# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000907
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000908_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000909
Jim Fultond15dc062004-07-14 19:11:50 +0000910# get thread-local implementation, either from the thread
911# module, or from the python fallback
912
913try:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000914 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +0000915except ImportError:
916 from _threading_local import local
917
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000918
Jesse Nollera8513972008-07-17 16:49:17 +0000919def _after_fork():
920 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
921 # is called from PyOS_AfterFork. Here we cleanup threading module state
922 # that should not exist after a fork.
923
924 # Reset _active_limbo_lock, in case we forked while the lock was held
925 # by another (non-forked) thread. http://bugs.python.org/issue874900
926 global _active_limbo_lock
927 _active_limbo_lock = _allocate_lock()
928
929 # fork() only copied the current thread; clear references to others.
930 new_active = {}
931 current = current_thread()
932 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000933 for thread in _active.values():
Charles-François Natalib055bf62011-12-18 18:45:16 +0100934 # Any lock/condition variable may be currently locked or in an
935 # invalid state, so we reinitialize them.
936 thread._reset_internal_locks()
Jesse Nollera8513972008-07-17 16:49:17 +0000937 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000938 # There is only one active thread. We reset the ident to
939 # its new value since it can have changed.
Victor Stinner2a129742011-05-30 23:02:52 +0200940 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000941 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +0000942 new_active[ident] = thread
943 else:
944 # All the others are already stopped.
Charles-François Natalib055bf62011-12-18 18:45:16 +0100945 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +0000946
947 _limbo.clear()
948 _active.clear()
949 _active.update(new_active)
950 assert len(_active) == 1