blob: ab9302c15324297c0e0eb08ebb4cb01e3c83e44c [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
Victor Stinnerec895392012-04-29 02:41:27 +02006from time import sleep as _sleep
7try:
8 from time import monotonic as _time
9except ImportError:
10 from time import time as _time
Neil Schemenauerf607fc52003-11-05 23:03:00 +000011from traceback import format_exc as _format_exc
Antoine Pitrouc081c0c2011-07-15 22:12:24 +020012from _weakrefset import WeakSet
Guido van Rossum7f5013a1998-04-09 22:01:42 +000013
Benjamin Petersonb3085c92008-09-01 23:09:31 +000014# Note regarding PEP 8 compliant names
15# This threading model was originally inspired by Java, and inherited
16# the convention of camelCase function and method names from that
17# language. Those originaly names are not in any imminent danger of
18# being deprecated (even for Py3k),so this module provides them as an
19# alias for the PEP 8 compliant names
20# Note that using the new PEP 8 compliant names facilitates substitution
21# with the multiprocessing module, which doesn't provide the old
22# Java inspired names.
23
Benjamin Peterson672b8032008-06-11 19:14:14 +000024__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000025 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
Benjamin Peterson7761b952011-08-02 13:05:47 -050026 'Timer', 'ThreadError', 'setprofile', 'settrace', 'local', 'stack_size']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000027
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000028# Rename some stuff so "from threading import *" is safe
Georg Brandl2067bfd2008-05-25 13:05:15 +000029_start_new_thread = _thread.start_new_thread
30_allocate_lock = _thread.allocate_lock
Victor Stinner2a129742011-05-30 23:02:52 +020031get_ident = _thread.get_ident
Georg Brandl2067bfd2008-05-25 13:05:15 +000032ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000033try:
34 _CRLock = _thread.RLock
35except AttributeError:
36 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000037TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000038del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000039
Guido van Rossum7f5013a1998-04-09 22:01:42 +000040
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000041# Support for profile and trace hooks
42
43_profile_hook = None
44_trace_hook = None
45
46def setprofile(func):
47 global _profile_hook
48 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000049
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000050def settrace(func):
51 global _trace_hook
52 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000053
54# Synchronization classes
55
56Lock = _allocate_lock
57
Victor Stinner135b6d82012-03-03 01:32:57 +010058def RLock(*args, **kwargs):
59 if _CRLock is None:
60 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000061 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000062
Victor Stinner135b6d82012-03-03 01:32:57 +010063class _RLock:
Tim Petersb90f89a2001-01-15 03:26:36 +000064
Victor Stinner135b6d82012-03-03 01:32:57 +010065 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000066 self._block = _allocate_lock()
67 self._owner = None
68 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +000069
70 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000071 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +000072 try:
73 owner = _active[owner].name
74 except KeyError:
75 pass
76 return "<%s owner=%r count=%d>" % (
77 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000078
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000079 def acquire(self, blocking=True, timeout=-1):
Victor Stinner2a129742011-05-30 23:02:52 +020080 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +000081 if self._owner == me:
Guido van Rossumd0648992007-08-20 19:25:41 +000082 self._count = self._count + 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000083 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000084 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000085 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +000086 self._owner = me
87 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000088 return rc
89
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000090 __enter__ = acquire
91
Guido van Rossum7f5013a1998-04-09 22:01:42 +000092 def release(self):
Victor Stinner2a129742011-05-30 23:02:52 +020093 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +000094 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +000095 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000096 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +000097 self._owner = None
98 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +000099
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000100 def __exit__(self, t, v, tb):
101 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000102
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000103 # Internal methods used by condition variables
104
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000105 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000106 self._block.acquire()
107 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000108
109 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200110 if self._count == 0:
111 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000112 count = self._count
113 self._count = 0
114 owner = self._owner
115 self._owner = None
116 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000117 return (count, owner)
118
119 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200120 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000121
Antoine Pitrou434736a2009-11-10 18:46:01 +0000122_PyRLock = _RLock
123
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000124
Victor Stinner135b6d82012-03-03 01:32:57 +0100125class Condition:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000126
Victor Stinner135b6d82012-03-03 01:32:57 +0100127 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000128 if lock is None:
129 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000130 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000131 # Export the lock's acquire() and release() methods
132 self.acquire = lock.acquire
133 self.release = lock.release
134 # If the lock defines _release_save() and/or _acquire_restore(),
135 # these override the default implementations (which just call
136 # release() and acquire() on the lock). Ditto for _is_owned().
137 try:
138 self._release_save = lock._release_save
139 except AttributeError:
140 pass
141 try:
142 self._acquire_restore = lock._acquire_restore
143 except AttributeError:
144 pass
145 try:
146 self._is_owned = lock._is_owned
147 except AttributeError:
148 pass
Guido van Rossumd0648992007-08-20 19:25:41 +0000149 self._waiters = []
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000150
Thomas Wouters477c8d52006-05-27 19:21:47 +0000151 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000152 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000153
Thomas Wouters477c8d52006-05-27 19:21:47 +0000154 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000155 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000156
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000157 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000158 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000159
160 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000161 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000162
163 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000164 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000165
166 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000167 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000168 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000169 if self._lock.acquire(0):
170 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000171 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000172 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000173 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000174
175 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000176 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000177 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000178 waiter = _allocate_lock()
179 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000180 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000181 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000182 try: # restore state no matter what (e.g., KeyboardInterrupt)
183 if timeout is None:
184 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000185 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000186 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000187 if timeout > 0:
188 gotit = waiter.acquire(True, timeout)
189 else:
190 gotit = waiter.acquire(False)
Tim Petersc951bf92001-04-02 20:15:57 +0000191 if not gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000192 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000193 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000194 except ValueError:
195 pass
Georg Brandlb9a43912010-10-28 09:03:20 +0000196 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000197 finally:
198 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000199
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000200 def wait_for(self, predicate, timeout=None):
201 endtime = None
202 waittime = timeout
203 result = predicate()
204 while not result:
205 if waittime is not None:
206 if endtime is None:
207 endtime = _time() + waittime
208 else:
209 waittime = endtime - _time()
210 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000211 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000212 self.wait(waittime)
213 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000214 return result
215
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000216 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000217 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000218 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000219 __waiters = self._waiters
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000220 waiters = __waiters[:n]
221 if not waiters:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000222 return
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000223 for waiter in waiters:
224 waiter.release()
225 try:
226 __waiters.remove(waiter)
227 except ValueError:
228 pass
229
Benjamin Peterson672b8032008-06-11 19:14:14 +0000230 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000231 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000232
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000233 notifyAll = notify_all
234
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000235
Victor Stinner135b6d82012-03-03 01:32:57 +0100236class Semaphore:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000237
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000238 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000239
Victor Stinner135b6d82012-03-03 01:32:57 +0100240 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000241 if value < 0:
242 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000243 self._cond = Condition(Lock())
244 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000245
Antoine Pitrou0454af92010-04-17 23:51:58 +0000246 def acquire(self, blocking=True, timeout=None):
247 if not blocking and timeout is not None:
248 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000249 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000250 endtime = None
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300251 with self._cond:
252 while self._value == 0:
253 if not blocking:
254 break
255 if timeout is not None:
256 if endtime is None:
257 endtime = _time() + timeout
258 else:
259 timeout = endtime - _time()
260 if timeout <= 0:
261 break
262 self._cond.wait(timeout)
263 else:
264 self._value = self._value - 1
265 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000266 return rc
267
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000268 __enter__ = acquire
269
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000270 def release(self):
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300271 with self._cond:
272 self._value = self._value + 1
273 self._cond.notify()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000274
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000275 def __exit__(self, t, v, tb):
276 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000277
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000278
Éric Araujo0cdd4452011-07-28 00:28:28 +0200279class BoundedSemaphore(Semaphore):
Skip Montanaroe428bb72001-08-20 20:27:58 +0000280 """Semaphore that checks that # releases is <= # acquires"""
Victor Stinner135b6d82012-03-03 01:32:57 +0100281 def __init__(self, value=1):
282 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000283 self._initial_value = value
284
285 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000286 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000287 raise ValueError("Semaphore released too many times")
Éric Araujo0cdd4452011-07-28 00:28:28 +0200288 return Semaphore.release(self)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000289
290
Victor Stinner135b6d82012-03-03 01:32:57 +0100291class Event:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000292
293 # After Tim Peters' event class (without is_posted())
294
Victor Stinner135b6d82012-03-03 01:32:57 +0100295 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000296 self._cond = Condition(Lock())
297 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000298
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000299 def _reset_internal_locks(self):
300 # private! called by Thread._reset_internal_locks by _after_fork()
301 self._cond.__init__()
302
Benjamin Peterson672b8032008-06-11 19:14:14 +0000303 def is_set(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000304 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000305
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000306 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000307
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000308 def set(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000309 self._cond.acquire()
310 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000311 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000312 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000313 finally:
314 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000315
316 def clear(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000317 self._cond.acquire()
318 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000319 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000320 finally:
321 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000322
323 def wait(self, timeout=None):
Christian Heimes969fe572008-01-25 11:23:10 +0000324 self._cond.acquire()
325 try:
Charles-François Natalided03482012-01-07 18:24:56 +0100326 signaled = self._flag
327 if not signaled:
328 signaled = self._cond.wait(timeout)
329 return signaled
Christian Heimes969fe572008-01-25 11:23:10 +0000330 finally:
331 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000332
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000333
334# A barrier class. Inspired in part by the pthread_barrier_* api and
335# the CyclicBarrier class from Java. See
336# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
337# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
338# CyclicBarrier.html
339# for information.
340# We maintain two main states, 'filling' and 'draining' enabling the barrier
341# to be cyclic. Threads are not allowed into it until it has fully drained
342# since the previous cycle. In addition, a 'resetting' state exists which is
343# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300344# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100345class Barrier:
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000346 """
347 Barrier. Useful for synchronizing a fixed number of threads
348 at known synchronization points. Threads block on 'wait()' and are
349 simultaneously once they have all made that call.
350 """
Victor Stinner135b6d82012-03-03 01:32:57 +0100351 def __init__(self, parties, action=None, timeout=None):
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000352 """
353 Create a barrier, initialised to 'parties' threads.
354 'action' is a callable which, when supplied, will be called
355 by one of the threads after they have all entered the
356 barrier and just prior to releasing them all.
357 If a 'timeout' is provided, it is uses as the default for
358 all subsequent 'wait()' calls.
359 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000360 self._cond = Condition(Lock())
361 self._action = action
362 self._timeout = timeout
363 self._parties = parties
364 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
365 self._count = 0
366
367 def wait(self, timeout=None):
368 """
369 Wait for the barrier. When the specified number of threads have
370 started waiting, they are all simultaneously awoken. If an 'action'
371 was provided for the barrier, one of the threads will have executed
372 that callback prior to returning.
373 Returns an individual index number from 0 to 'parties-1'.
374 """
375 if timeout is None:
376 timeout = self._timeout
377 with self._cond:
378 self._enter() # Block while the barrier drains.
379 index = self._count
380 self._count += 1
381 try:
382 if index + 1 == self._parties:
383 # We release the barrier
384 self._release()
385 else:
386 # We wait until someone releases us
387 self._wait(timeout)
388 return index
389 finally:
390 self._count -= 1
391 # Wake up any threads waiting for barrier to drain.
392 self._exit()
393
394 # Block until the barrier is ready for us, or raise an exception
395 # if it is broken.
396 def _enter(self):
397 while self._state in (-1, 1):
398 # It is draining or resetting, wait until done
399 self._cond.wait()
400 #see if the barrier is in a broken state
401 if self._state < 0:
402 raise BrokenBarrierError
403 assert self._state == 0
404
405 # Optionally run the 'action' and release the threads waiting
406 # in the barrier.
407 def _release(self):
408 try:
409 if self._action:
410 self._action()
411 # enter draining state
412 self._state = 1
413 self._cond.notify_all()
414 except:
415 #an exception during the _action handler. Break and reraise
416 self._break()
417 raise
418
419 # Wait in the barrier until we are relased. Raise an exception
420 # if the barrier is reset or broken.
421 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000422 if not self._cond.wait_for(lambda : self._state != 0, timeout):
423 #timed out. Break the barrier
424 self._break()
425 raise BrokenBarrierError
426 if self._state < 0:
427 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000428 assert self._state == 1
429
430 # If we are the last thread to exit the barrier, signal any threads
431 # waiting for the barrier to drain.
432 def _exit(self):
433 if self._count == 0:
434 if self._state in (-1, 1):
435 #resetting or draining
436 self._state = 0
437 self._cond.notify_all()
438
439 def reset(self):
440 """
441 Reset the barrier to the initial state.
442 Any threads currently waiting will get the BrokenBarrier exception
443 raised.
444 """
445 with self._cond:
446 if self._count > 0:
447 if self._state == 0:
448 #reset the barrier, waking up threads
449 self._state = -1
450 elif self._state == -2:
451 #was broken, set it to reset state
452 #which clears when the last thread exits
453 self._state = -1
454 else:
455 self._state = 0
456 self._cond.notify_all()
457
458 def abort(self):
459 """
460 Place the barrier into a 'broken' state.
461 Useful in case of error. Any currently waiting threads and
462 threads attempting to 'wait()' will have BrokenBarrierError
463 raised.
464 """
465 with self._cond:
466 self._break()
467
468 def _break(self):
469 # An internal error was detected. The barrier is set to
470 # a broken state all parties awakened.
471 self._state = -2
472 self._cond.notify_all()
473
474 @property
475 def parties(self):
476 """
477 Return the number of threads required to trip the barrier.
478 """
479 return self._parties
480
481 @property
482 def n_waiting(self):
483 """
484 Return the number of threads that are currently waiting at the barrier.
485 """
486 # We don't need synchronization here since this is an ephemeral result
487 # anyway. It returns the correct value in the steady state.
488 if self._state == 0:
489 return self._count
490 return 0
491
492 @property
493 def broken(self):
494 """
495 Return True if the barrier is in a broken state
496 """
497 return self._state == -2
498
499#exception raised by the Barrier class
500class BrokenBarrierError(RuntimeError): pass
501
502
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000503# Helper to generate new thread names
504_counter = 0
505def _newname(template="Thread-%d"):
506 global _counter
507 _counter = _counter + 1
508 return template % _counter
509
510# Active thread administration
511_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000512_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000513_limbo = {}
514
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200515# For debug and leak testing
516_dangling = WeakSet()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000517
518# Main class for threads
519
Victor Stinner135b6d82012-03-03 01:32:57 +0100520class Thread:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000521
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000522 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000523 # Need to store a reference to sys.exc_info for printing
524 # out exceptions when a thread tries to use a global var. during interp.
525 # shutdown and thus raises an exception about trying to perform some
526 # operation on/with a NoneType
527 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000528 # Keep sys.exc_clear too to clear the exception just before
529 # allowing .join() to return.
530 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000531
532 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100533 args=(), kwargs=None, *, daemon=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000534 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000535 if kwargs is None:
536 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000537 self._target = target
538 self._name = str(name or _newname())
539 self._args = args
540 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000541 if daemon is not None:
542 self._daemonic = daemon
543 else:
544 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000545 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000546 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000547 self._stopped = False
548 self._block = Condition(Lock())
549 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000550 # sys.stderr is not stored in the class like
551 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000552 self._stderr = _sys.stderr
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200553 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000554
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000555 def _reset_internal_locks(self):
556 # private! Called by _after_fork() to reset our internal locks as
557 # they may be in an invalid state leading to a deadlock or crash.
558 if hasattr(self, '_block'): # DummyThread deletes _block
559 self._block.__init__()
560 self._started._reset_internal_locks()
561
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000562 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000563 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000564 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000565 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000566 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000567 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000568 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000569 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000570 status += " daemon"
571 if self._ident is not None:
572 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000573 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000574
575 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000576 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000577 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000578
Benjamin Peterson672b8032008-06-11 19:14:14 +0000579 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000580 raise RuntimeError("threads can only be started once")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000581 with _active_limbo_lock:
582 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000583 try:
584 _start_new_thread(self._bootstrap, ())
585 except Exception:
586 with _active_limbo_lock:
587 del _limbo[self]
588 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000589 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000590
591 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000592 try:
593 if self._target:
594 self._target(*self._args, **self._kwargs)
595 finally:
596 # Avoid a refcycle if the thread is running a function with
597 # an argument that has a member that points to the thread.
598 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000599
Guido van Rossumd0648992007-08-20 19:25:41 +0000600 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000601 # Wrapper around the real bootstrap code that ignores
602 # exceptions during interpreter cleanup. Those typically
603 # happen when a daemon thread wakes up at an unfortunate
604 # moment, finds the world around it destroyed, and raises some
605 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000606 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000607 # don't help anybody, and they confuse users, so we suppress
608 # them. We suppress them only when it appears that the world
609 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000610 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000611 # reported. Also, we only suppress them for daemonic threads;
612 # if a non-daemonic encounters this, something else is wrong.
613 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000614 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000615 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000616 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000617 return
618 raise
619
Benjamin Petersond23f8222009-04-05 19:13:16 +0000620 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200621 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000622
Guido van Rossumd0648992007-08-20 19:25:41 +0000623 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000624 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000625 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000626 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000627 with _active_limbo_lock:
628 _active[self._ident] = self
629 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000630
631 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000632 _sys.settrace(_trace_hook)
633 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000634 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000635
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000636 try:
637 self.run()
638 except SystemExit:
Victor Stinner135b6d82012-03-03 01:32:57 +0100639 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000640 except:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000641 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000642 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000643 # _sys) in case sys.stderr was redefined since the creation of
644 # self.
645 if _sys:
646 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000647 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000648 else:
649 # Do the best job possible w/o a huge amt. of code to
650 # approximate a traceback (code ideas from
651 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000652 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000653 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000654 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000655 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000656 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000657 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000658 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000659 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000660 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000661 ' File "%s", line %s, in %s' %
662 (exc_tb.tb_frame.f_code.co_filename,
663 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000664 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000665 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000666 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000667 # Make sure that exc_tb gets deleted since it is a memory
668 # hog; deleting everything else is just for thoroughness
669 finally:
670 del exc_type, exc_value, exc_tb
Christian Heimesbbe741d2008-03-28 10:53:29 +0000671 finally:
672 # Prevent a race in
673 # test_threading.test_no_refcycle_through_target when
674 # the exception keeps the target alive past when we
675 # assert that it's dead.
676 #XXX self.__exc_clear()
677 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000678 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000679 with _active_limbo_lock:
680 self._stop()
681 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000682 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000683 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200684 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000685 except:
686 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000687
Guido van Rossumd0648992007-08-20 19:25:41 +0000688 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000689 self._block.acquire()
690 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000691 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000692 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000693
Guido van Rossumd0648992007-08-20 19:25:41 +0000694 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000695 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000696
Georg Brandl2067bfd2008-05-25 13:05:15 +0000697 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000698 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000699 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000700 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000701 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
702 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000703 # len(_active) is always <= 1 here, and any Thread instance created
704 # overwrites the (if any) thread currently registered in _active.
705 #
706 # An instance of _MainThread is always created by 'threading'. This
707 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000708 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000709 # same key in the dict. So when the _MainThread instance created by
710 # 'threading' tries to clean itself up when atexit calls this method
711 # it gets a KeyError if another Thread instance was created.
712 #
713 # This all means that KeyError from trying to delete something from
714 # _active if dummy_threading is being used is a red herring. But
715 # since it isn't if dummy_threading is *not* being used then don't
716 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000717
Christian Heimes969fe572008-01-25 11:23:10 +0000718 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000719 with _active_limbo_lock:
Victor Stinner2a129742011-05-30 23:02:52 +0200720 del _active[get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000721 # There must not be any python code between the previous line
722 # and after the lock is released. Otherwise a tracing function
723 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000724 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000725 except KeyError:
726 if 'dummy_threading' not in _sys.modules:
727 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000728
729 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000730 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000731 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000732 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000733 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000734 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000735 raise RuntimeError("cannot join current thread")
736
Christian Heimes969fe572008-01-25 11:23:10 +0000737 self._block.acquire()
738 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000739 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000740 while not self._stopped:
741 self._block.wait()
Brett Cannonad07ff22005-11-23 02:15:50 +0000742 else:
743 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000744 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000745 delay = deadline - _time()
746 if delay <= 0:
Brett Cannonad07ff22005-11-23 02:15:50 +0000747 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000748 self._block.wait(delay)
Christian Heimes969fe572008-01-25 11:23:10 +0000749 finally:
750 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000751
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000752 @property
753 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000754 assert self._initialized, "Thread.__init__() not called"
755 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000756
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000757 @name.setter
758 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000759 assert self._initialized, "Thread.__init__() not called"
760 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000761
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000762 @property
763 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000764 assert self._initialized, "Thread.__init__() not called"
765 return self._ident
766
Benjamin Peterson672b8032008-06-11 19:14:14 +0000767 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000768 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000769 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000770
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000771 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000772
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000773 @property
774 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000775 assert self._initialized, "Thread.__init__() not called"
776 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000777
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000778 @daemon.setter
779 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000780 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000781 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000782 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000783 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000784 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000785
Benjamin Peterson6640d722008-08-18 18:16:46 +0000786 def isDaemon(self):
787 return self.daemon
788
789 def setDaemon(self, daemonic):
790 self.daemon = daemonic
791
792 def getName(self):
793 return self.name
794
795 def setName(self, name):
796 self.name = name
797
Martin v. Löwis44f86962001-09-05 13:44:54 +0000798# The timer class was contributed by Itamar Shtull-Trauring
799
Éric Araujo0cdd4452011-07-28 00:28:28 +0200800class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +0000801 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000802
R David Murray19aeb432013-03-30 17:19:38 -0400803 t = Timer(30.0, f, args=None, kwargs=None)
Martin v. Löwis44f86962001-09-05 13:44:54 +0000804 t.start()
805 t.cancel() # stop the timer's action if it's still waiting
806 """
Tim Petersb64bec32001-09-18 02:26:39 +0000807
R David Murray19aeb432013-03-30 17:19:38 -0400808 def __init__(self, interval, function, args=None, kwargs=None):
Martin v. Löwis44f86962001-09-05 13:44:54 +0000809 Thread.__init__(self)
810 self.interval = interval
811 self.function = function
R David Murray19aeb432013-03-30 17:19:38 -0400812 self.args = args if args is not None else []
813 self.kwargs = kwargs if kwargs is not None else {}
Martin v. Löwis44f86962001-09-05 13:44:54 +0000814 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000815
Martin v. Löwis44f86962001-09-05 13:44:54 +0000816 def cancel(self):
817 """Stop the timer if it hasn't finished yet"""
818 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000819
Martin v. Löwis44f86962001-09-05 13:44:54 +0000820 def run(self):
821 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000822 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000823 self.function(*self.args, **self.kwargs)
824 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000825
826# Special thread class to represent the main thread
827# This is garbage collected through an exit handler
828
829class _MainThread(Thread):
830
831 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000832 Thread.__init__(self, name="MainThread", daemon=False)
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000833 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000834 self._set_ident()
835 with _active_limbo_lock:
836 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000837
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000838 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000839 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000840 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000841 while t:
842 t.join()
843 t = _pickSomeNonDaemonThread()
Guido van Rossumd0648992007-08-20 19:25:41 +0000844 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000845
846def _pickSomeNonDaemonThread():
847 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000848 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000849 return t
850 return None
851
852
853# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000854# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000855# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000856# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000857# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000858# They are marked as daemon threads so we won't wait for them
859# when we exit (conform previous semantics).
860
861class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000862
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000863 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000864 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +0000865
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000866 # Thread._block consumes an OS-level locking primitive, which
Tim Peters711906e2005-01-08 07:30:42 +0000867 # can never be used by a _DummyThread. Since a _DummyThread
868 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +0000869 del self._block
Tim Peters711906e2005-01-08 07:30:42 +0000870
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000871 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000872 self._set_ident()
873 with _active_limbo_lock:
874 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000875
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +0200876 def _stop(self):
877 pass
878
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000879 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000880 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000881
882
883# Global API functions
884
Benjamin Peterson672b8032008-06-11 19:14:14 +0000885def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000886 try:
Victor Stinner2a129742011-05-30 23:02:52 +0200887 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000888 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000889 return _DummyThread()
890
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000891currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000892
Benjamin Peterson672b8032008-06-11 19:14:14 +0000893def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000894 with _active_limbo_lock:
895 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000896
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000897activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000898
Antoine Pitroubdec11f2009-11-05 13:49:14 +0000899def _enumerate():
900 # Same as enumerate(), but without the lock. Internal use only.
901 return list(_active.values()) + list(_limbo.values())
902
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000903def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000904 with _active_limbo_lock:
905 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000906
Georg Brandl2067bfd2008-05-25 13:05:15 +0000907from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000908
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000909# Create the main thread object,
910# and make it available for the interpreter
911# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000912
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000913_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000914
Jim Fultond15dc062004-07-14 19:11:50 +0000915# get thread-local implementation, either from the thread
916# module, or from the python fallback
917
918try:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000919 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +0000920except ImportError:
921 from _threading_local import local
922
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000923
Jesse Nollera8513972008-07-17 16:49:17 +0000924def _after_fork():
925 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
926 # is called from PyOS_AfterFork. Here we cleanup threading module state
927 # that should not exist after a fork.
928
929 # Reset _active_limbo_lock, in case we forked while the lock was held
930 # by another (non-forked) thread. http://bugs.python.org/issue874900
931 global _active_limbo_lock
932 _active_limbo_lock = _allocate_lock()
933
934 # fork() only copied the current thread; clear references to others.
935 new_active = {}
936 current = current_thread()
937 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000938 for thread in _active.values():
Charles-François Natalib055bf62011-12-18 18:45:16 +0100939 # Any lock/condition variable may be currently locked or in an
940 # invalid state, so we reinitialize them.
941 thread._reset_internal_locks()
Jesse Nollera8513972008-07-17 16:49:17 +0000942 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000943 # There is only one active thread. We reset the ident to
944 # its new value since it can have changed.
Victor Stinner2a129742011-05-30 23:02:52 +0200945 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000946 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +0000947 new_active[ident] = thread
948 else:
949 # All the others are already stopped.
Charles-François Natalib055bf62011-12-18 18:45:16 +0100950 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +0000951
952 _limbo.clear()
953 _active.clear()
954 _active.update(new_active)
955 assert len(_active) == 1