blob: 2f6ac7036f5f62ca357164aefa51ab2e2ba430c2 [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
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02003import os as _os
Fred Drakea8725952002-12-30 23:32:50 +00004import sys as _sys
Georg Brandl2067bfd2008-05-25 13:05:15 +00005import _thread
Fred Drakea8725952002-12-30 23:32:50 +00006
Victor Stinnerae586492014-09-02 23:18:25 +02007from time import monotonic as _time
Antoine Pitrouc081c0c2011-07-15 22:12:24 +02008from _weakrefset import WeakSet
R David Murrayb186f1df2014-10-04 17:43:54 -04009from itertools import islice as _islice, count as _count
Raymond Hettingerec4b1742013-03-10 17:57:28 -070010try:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070011 from _collections import deque as _deque
Brett Cannoncd171c82013-07-04 17:43:24 -040012except ImportError:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070013 from collections import deque as _deque
Guido van Rossum7f5013a1998-04-09 22:01:42 +000014
Benjamin Petersonb3085c92008-09-01 23:09:31 +000015# Note regarding PEP 8 compliant names
16# This threading model was originally inspired by Java, and inherited
17# the convention of camelCase function and method names from that
Ezio Melotti30b9d5d2013-08-17 15:50:46 +030018# language. Those original names are not in any imminent danger of
Benjamin Petersonb3085c92008-09-01 23:09:31 +000019# being deprecated (even for Py3k),so this module provides them as an
20# alias for the PEP 8 compliant names
21# Note that using the new PEP 8 compliant names facilitates substitution
22# with the multiprocessing module, which doesn't provide the old
23# Java inspired names.
24
Victor Stinnerd12e7572019-05-21 12:44:57 +020025__all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
26 'enumerate', 'main_thread', 'TIMEOUT_MAX',
Martin Panter19e69c52015-11-14 12:46:42 +000027 'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
28 'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
Victor Stinnercd590a72019-05-28 00:39:52 +020029 'setprofile', 'settrace', 'local', 'stack_size',
30 'excepthook', 'ExceptHookArgs']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000031
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000032# Rename some stuff so "from threading import *" is safe
Georg Brandl2067bfd2008-05-25 13:05:15 +000033_start_new_thread = _thread.start_new_thread
34_allocate_lock = _thread.allocate_lock
Antoine Pitrou7b476992013-09-07 23:38:37 +020035_set_sentinel = _thread._set_sentinel
Victor Stinner2a129742011-05-30 23:02:52 +020036get_ident = _thread.get_ident
Jake Teslerb121f632019-05-22 08:43:17 -070037try:
38 get_native_id = _thread.get_native_id
39 _HAVE_THREAD_NATIVE_ID = True
40 __all__.append('get_native_id')
41except AttributeError:
42 _HAVE_THREAD_NATIVE_ID = False
Georg Brandl2067bfd2008-05-25 13:05:15 +000043ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000044try:
45 _CRLock = _thread.RLock
46except AttributeError:
47 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000048TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000049del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000050
Guido van Rossum7f5013a1998-04-09 22:01:42 +000051
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000052# Support for profile and trace hooks
53
54_profile_hook = None
55_trace_hook = None
56
57def setprofile(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020058 """Set a profile function for all threads started from the threading module.
59
60 The func will be passed to sys.setprofile() for each thread, before its
61 run() method is called.
62
63 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000064 global _profile_hook
65 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000066
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000067def settrace(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020068 """Set a trace function for all threads started from the threading module.
69
70 The func will be passed to sys.settrace() for each thread, before its run()
71 method is called.
72
73 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000074 global _trace_hook
75 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000076
77# Synchronization classes
78
79Lock = _allocate_lock
80
Victor Stinner135b6d82012-03-03 01:32:57 +010081def RLock(*args, **kwargs):
Georg Brandlc30b59f2013-10-13 10:43:59 +020082 """Factory function that returns a new reentrant lock.
83
84 A reentrant lock must be released by the thread that acquired it. Once a
85 thread has acquired a reentrant lock, the same thread may acquire it again
86 without blocking; the thread must release it once for each time it has
87 acquired it.
88
89 """
Victor Stinner135b6d82012-03-03 01:32:57 +010090 if _CRLock is None:
91 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000092 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000093
Victor Stinner135b6d82012-03-03 01:32:57 +010094class _RLock:
Georg Brandlc30b59f2013-10-13 10:43:59 +020095 """This class implements reentrant lock objects.
96
97 A reentrant lock must be released by the thread that acquired it. Once a
98 thread has acquired a reentrant lock, the same thread may acquire it
99 again without blocking; the thread must release it once for each time it
100 has acquired it.
101
102 """
Tim Petersb90f89a2001-01-15 03:26:36 +0000103
Victor Stinner135b6d82012-03-03 01:32:57 +0100104 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000105 self._block = _allocate_lock()
106 self._owner = None
107 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000108
109 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000110 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000111 try:
112 owner = _active[owner].name
113 except KeyError:
114 pass
Raymond Hettinger62f4dad2014-05-25 18:22:35 -0700115 return "<%s %s.%s object owner=%r count=%d at %s>" % (
116 "locked" if self._block.locked() else "unlocked",
117 self.__class__.__module__,
118 self.__class__.__qualname__,
119 owner,
120 self._count,
121 hex(id(self))
122 )
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000123
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000124 def acquire(self, blocking=True, timeout=-1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200125 """Acquire a lock, blocking or non-blocking.
126
127 When invoked without arguments: if this thread already owns the lock,
128 increment the recursion level by one, and return immediately. Otherwise,
129 if another thread owns the lock, block until the lock is unlocked. Once
130 the lock is unlocked (not owned by any thread), then grab ownership, set
131 the recursion level to one, and return. If more than one thread is
132 blocked waiting until the lock is unlocked, only one at a time will be
133 able to grab ownership of the lock. There is no return value in this
134 case.
135
136 When invoked with the blocking argument set to true, do the same thing
137 as when called without arguments, and return true.
138
139 When invoked with the blocking argument set to false, do not block. If a
140 call without an argument would block, return false immediately;
141 otherwise, do the same thing as when called without arguments, and
142 return true.
143
144 When invoked with the floating-point timeout argument set to a positive
145 value, block for at most the number of seconds specified by timeout
146 and as long as the lock cannot be acquired. Return true if the lock has
147 been acquired, false if the timeout has elapsed.
148
149 """
Victor Stinner2a129742011-05-30 23:02:52 +0200150 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +0000151 if self._owner == me:
Raymond Hettinger720da572013-03-10 15:13:35 -0700152 self._count += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000153 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000154 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000155 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000156 self._owner = me
157 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000158 return rc
159
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000160 __enter__ = acquire
161
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000162 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200163 """Release a lock, decrementing the recursion level.
164
165 If after the decrement it is zero, reset the lock to unlocked (not owned
166 by any thread), and if any other threads are blocked waiting for the
167 lock to become unlocked, allow exactly one of them to proceed. If after
168 the decrement the recursion level is still nonzero, the lock remains
169 locked and owned by the calling thread.
170
171 Only call this method when the calling thread owns the lock. A
172 RuntimeError is raised if this method is called when the lock is
173 unlocked.
174
175 There is no return value.
176
177 """
Victor Stinner2a129742011-05-30 23:02:52 +0200178 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000179 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000180 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000181 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000182 self._owner = None
183 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000184
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000185 def __exit__(self, t, v, tb):
186 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000187
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000188 # Internal methods used by condition variables
189
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000190 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000191 self._block.acquire()
192 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000193
194 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200195 if self._count == 0:
196 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000197 count = self._count
198 self._count = 0
199 owner = self._owner
200 self._owner = None
201 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000202 return (count, owner)
203
204 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200205 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000206
Antoine Pitrou434736a2009-11-10 18:46:01 +0000207_PyRLock = _RLock
208
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000209
Victor Stinner135b6d82012-03-03 01:32:57 +0100210class Condition:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200211 """Class that implements a condition variable.
212
213 A condition variable allows one or more threads to wait until they are
214 notified by another thread.
215
216 If the lock argument is given and not None, it must be a Lock or RLock
217 object, and it is used as the underlying lock. Otherwise, a new RLock object
218 is created and used as the underlying lock.
219
220 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000221
Victor Stinner135b6d82012-03-03 01:32:57 +0100222 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000223 if lock is None:
224 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000225 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000226 # Export the lock's acquire() and release() methods
227 self.acquire = lock.acquire
228 self.release = lock.release
229 # If the lock defines _release_save() and/or _acquire_restore(),
230 # these override the default implementations (which just call
231 # release() and acquire() on the lock). Ditto for _is_owned().
232 try:
233 self._release_save = lock._release_save
234 except AttributeError:
235 pass
236 try:
237 self._acquire_restore = lock._acquire_restore
238 except AttributeError:
239 pass
240 try:
241 self._is_owned = lock._is_owned
242 except AttributeError:
243 pass
Raymond Hettingerec4b1742013-03-10 17:57:28 -0700244 self._waiters = _deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000245
Thomas Wouters477c8d52006-05-27 19:21:47 +0000246 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000247 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000248
Thomas Wouters477c8d52006-05-27 19:21:47 +0000249 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000250 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000251
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000252 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000253 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000254
255 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000256 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000257
258 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000259 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000260
261 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000262 # Return True if lock is owned by current_thread.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300263 # This method is called only if _lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000264 if self._lock.acquire(0):
265 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000266 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000267 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000268 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000269
270 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200271 """Wait until notified or until a timeout occurs.
272
273 If the calling thread has not acquired the lock when this method is
274 called, a RuntimeError is raised.
275
276 This method releases the underlying lock, and then blocks until it is
277 awakened by a notify() or notify_all() call for the same condition
278 variable in another thread, or until the optional timeout occurs. Once
279 awakened or timed out, it re-acquires the lock and returns.
280
281 When the timeout argument is present and not None, it should be a
282 floating point number specifying a timeout for the operation in seconds
283 (or fractions thereof).
284
285 When the underlying lock is an RLock, it is not released using its
286 release() method, since this may not actually unlock the lock when it
287 was acquired multiple times recursively. Instead, an internal interface
288 of the RLock class is used, which really unlocks it even when it has
289 been recursively acquired several times. Another internal interface is
290 then used to restore the recursion level when the lock is reacquired.
291
292 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000293 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000294 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000295 waiter = _allocate_lock()
296 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000297 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000298 saved_state = self._release_save()
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200299 gotit = False
Tim Petersc951bf92001-04-02 20:15:57 +0000300 try: # restore state no matter what (e.g., KeyboardInterrupt)
301 if timeout is None:
302 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000303 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000304 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000305 if timeout > 0:
306 gotit = waiter.acquire(True, timeout)
307 else:
308 gotit = waiter.acquire(False)
Georg Brandlb9a43912010-10-28 09:03:20 +0000309 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000310 finally:
311 self._acquire_restore(saved_state)
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200312 if not gotit:
313 try:
314 self._waiters.remove(waiter)
315 except ValueError:
316 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000317
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000318 def wait_for(self, predicate, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200319 """Wait until a condition evaluates to True.
320
321 predicate should be a callable which result will be interpreted as a
322 boolean value. A timeout may be provided giving the maximum time to
323 wait.
324
325 """
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000326 endtime = None
327 waittime = timeout
328 result = predicate()
329 while not result:
330 if waittime is not None:
331 if endtime is None:
332 endtime = _time() + waittime
333 else:
334 waittime = endtime - _time()
335 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000336 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000337 self.wait(waittime)
338 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000339 return result
340
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000341 def notify(self, n=1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200342 """Wake up one or more threads waiting on this condition, if any.
343
344 If the calling thread has not acquired the lock when this method is
345 called, a RuntimeError is raised.
346
347 This method wakes up at most n of the threads waiting for the condition
348 variable; it is a no-op if no threads are waiting.
349
350 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000351 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000352 raise RuntimeError("cannot notify on un-acquired lock")
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700353 all_waiters = self._waiters
354 waiters_to_notify = _deque(_islice(all_waiters, n))
355 if not waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000356 return
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700357 for waiter in waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000358 waiter.release()
359 try:
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700360 all_waiters.remove(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000361 except ValueError:
362 pass
363
Benjamin Peterson672b8032008-06-11 19:14:14 +0000364 def notify_all(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200365 """Wake up all threads waiting on this condition.
366
367 If the calling thread has not acquired the lock when this method
368 is called, a RuntimeError is raised.
369
370 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000371 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000372
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000373 notifyAll = notify_all
374
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000375
Victor Stinner135b6d82012-03-03 01:32:57 +0100376class Semaphore:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200377 """This class implements semaphore objects.
378
379 Semaphores manage a counter representing the number of release() calls minus
380 the number of acquire() calls, plus an initial value. The acquire() method
381 blocks if necessary until it can return without making the counter
382 negative. If not given, value defaults to 1.
383
384 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000385
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000386 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000387
Victor Stinner135b6d82012-03-03 01:32:57 +0100388 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000389 if value < 0:
390 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000391 self._cond = Condition(Lock())
392 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000393
Antoine Pitrou0454af92010-04-17 23:51:58 +0000394 def acquire(self, blocking=True, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200395 """Acquire a semaphore, decrementing the internal counter by one.
396
397 When invoked without arguments: if the internal counter is larger than
398 zero on entry, decrement it by one and return immediately. If it is zero
399 on entry, block, waiting until some other thread has called release() to
400 make it larger than zero. This is done with proper interlocking so that
401 if multiple acquire() calls are blocked, release() will wake exactly one
402 of them up. The implementation may pick one at random, so the order in
403 which blocked threads are awakened should not be relied on. There is no
404 return value in this case.
405
406 When invoked with blocking set to true, do the same thing as when called
407 without arguments, and return true.
408
409 When invoked with blocking set to false, do not block. If a call without
410 an argument would block, return false immediately; otherwise, do the
411 same thing as when called without arguments, and return true.
412
413 When invoked with a timeout other than None, it will block for at
414 most timeout seconds. If acquire does not complete successfully in
415 that interval, return false. Return true otherwise.
416
417 """
Antoine Pitrou0454af92010-04-17 23:51:58 +0000418 if not blocking and timeout is not None:
419 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000420 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000421 endtime = None
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300422 with self._cond:
423 while self._value == 0:
424 if not blocking:
425 break
426 if timeout is not None:
427 if endtime is None:
428 endtime = _time() + timeout
429 else:
430 timeout = endtime - _time()
431 if timeout <= 0:
432 break
433 self._cond.wait(timeout)
434 else:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300435 self._value -= 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300436 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000437 return rc
438
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000439 __enter__ = acquire
440
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000441 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200442 """Release a semaphore, incrementing the internal counter by one.
443
444 When the counter is zero on entry and another thread is waiting for it
445 to become larger than zero again, wake up that thread.
446
447 """
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300448 with self._cond:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300449 self._value += 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300450 self._cond.notify()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000451
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000452 def __exit__(self, t, v, tb):
453 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000454
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000455
Éric Araujo0cdd4452011-07-28 00:28:28 +0200456class BoundedSemaphore(Semaphore):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200457 """Implements a bounded semaphore.
458
459 A bounded semaphore checks to make sure its current value doesn't exceed its
460 initial value. If it does, ValueError is raised. In most situations
461 semaphores are used to guard resources with limited capacity.
462
463 If the semaphore is released too many times it's a sign of a bug. If not
464 given, value defaults to 1.
465
466 Like regular semaphores, bounded semaphores manage a counter representing
467 the number of release() calls minus the number of acquire() calls, plus an
468 initial value. The acquire() method blocks if necessary until it can return
469 without making the counter negative. If not given, value defaults to 1.
470
471 """
472
Victor Stinner135b6d82012-03-03 01:32:57 +0100473 def __init__(self, value=1):
474 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000475 self._initial_value = value
476
477 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200478 """Release a semaphore, incrementing the internal counter by one.
479
480 When the counter is zero on entry and another thread is waiting for it
481 to become larger than zero again, wake up that thread.
482
483 If the number of releases exceeds the number of acquires,
484 raise a ValueError.
485
486 """
Tim Peters7634e1c2013-10-08 20:55:51 -0500487 with self._cond:
488 if self._value >= self._initial_value:
489 raise ValueError("Semaphore released too many times")
490 self._value += 1
491 self._cond.notify()
Skip Montanaroe428bb72001-08-20 20:27:58 +0000492
493
Victor Stinner135b6d82012-03-03 01:32:57 +0100494class Event:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200495 """Class implementing event objects.
496
497 Events manage a flag that can be set to true with the set() method and reset
498 to false with the clear() method. The wait() method blocks until the flag is
499 true. The flag is initially false.
500
501 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000502
503 # After Tim Peters' event class (without is_posted())
504
Victor Stinner135b6d82012-03-03 01:32:57 +0100505 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000506 self._cond = Condition(Lock())
507 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000508
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000509 def _reset_internal_locks(self):
510 # private! called by Thread._reset_internal_locks by _after_fork()
Benjamin Peterson15982aa2015-10-05 21:56:22 -0700511 self._cond.__init__(Lock())
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000512
Benjamin Peterson672b8032008-06-11 19:14:14 +0000513 def is_set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200514 """Return true if and only if the internal flag is true."""
Guido van Rossumd0648992007-08-20 19:25:41 +0000515 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000516
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000517 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000518
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000519 def set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200520 """Set the internal flag to true.
521
522 All threads waiting for it to become true are awakened. Threads
523 that call wait() once the flag is true will not block at all.
524
525 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700526 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000527 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000528 self._cond.notify_all()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000529
530 def clear(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200531 """Reset the internal flag to false.
532
533 Subsequently, threads calling wait() will block until set() is called to
534 set the internal flag to true again.
535
536 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700537 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000538 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000539
540 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200541 """Block until the internal flag is true.
542
543 If the internal flag is true on entry, return immediately. Otherwise,
544 block until another thread calls set() to set the flag to true, or until
545 the optional timeout occurs.
546
547 When the timeout argument is present and not None, it should be a
548 floating point number specifying a timeout for the operation in seconds
549 (or fractions thereof).
550
551 This method returns the internal flag on exit, so it will always return
552 True except if a timeout is given and the operation times out.
553
554 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700555 with self._cond:
Charles-François Natalided03482012-01-07 18:24:56 +0100556 signaled = self._flag
557 if not signaled:
558 signaled = self._cond.wait(timeout)
559 return signaled
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000560
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000561
562# A barrier class. Inspired in part by the pthread_barrier_* api and
563# the CyclicBarrier class from Java. See
564# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
565# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
566# CyclicBarrier.html
567# for information.
568# We maintain two main states, 'filling' and 'draining' enabling the barrier
569# to be cyclic. Threads are not allowed into it until it has fully drained
570# since the previous cycle. In addition, a 'resetting' state exists which is
571# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300572# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100573class Barrier:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200574 """Implements a Barrier.
575
576 Useful for synchronizing a fixed number of threads at known synchronization
Carl Bordum Hansen62fa51f2019-03-09 18:38:05 +0100577 points. Threads block on 'wait()' and are simultaneously awoken once they
578 have all made that call.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200579
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000580 """
Georg Brandlc30b59f2013-10-13 10:43:59 +0200581
Victor Stinner135b6d82012-03-03 01:32:57 +0100582 def __init__(self, parties, action=None, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200583 """Create a barrier, initialised to 'parties' threads.
584
585 'action' is a callable which, when supplied, will be called by one of
586 the threads after they have all entered the barrier and just prior to
Carl Bordum Hansen62fa51f2019-03-09 18:38:05 +0100587 releasing them all. If a 'timeout' is provided, it is used as the
Georg Brandlc30b59f2013-10-13 10:43:59 +0200588 default for all subsequent 'wait()' calls.
589
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000590 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000591 self._cond = Condition(Lock())
592 self._action = action
593 self._timeout = timeout
594 self._parties = parties
595 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
596 self._count = 0
597
598 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200599 """Wait for the barrier.
600
601 When the specified number of threads have started waiting, they are all
602 simultaneously awoken. If an 'action' was provided for the barrier, one
603 of the threads will have executed that callback prior to returning.
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000604 Returns an individual index number from 0 to 'parties-1'.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200605
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000606 """
607 if timeout is None:
608 timeout = self._timeout
609 with self._cond:
610 self._enter() # Block while the barrier drains.
611 index = self._count
612 self._count += 1
613 try:
614 if index + 1 == self._parties:
615 # We release the barrier
616 self._release()
617 else:
618 # We wait until someone releases us
619 self._wait(timeout)
620 return index
621 finally:
622 self._count -= 1
623 # Wake up any threads waiting for barrier to drain.
624 self._exit()
625
626 # Block until the barrier is ready for us, or raise an exception
627 # if it is broken.
628 def _enter(self):
629 while self._state in (-1, 1):
630 # It is draining or resetting, wait until done
631 self._cond.wait()
632 #see if the barrier is in a broken state
633 if self._state < 0:
634 raise BrokenBarrierError
635 assert self._state == 0
636
637 # Optionally run the 'action' and release the threads waiting
638 # in the barrier.
639 def _release(self):
640 try:
641 if self._action:
642 self._action()
643 # enter draining state
644 self._state = 1
645 self._cond.notify_all()
646 except:
647 #an exception during the _action handler. Break and reraise
648 self._break()
649 raise
650
Martin Panter69332c12016-08-04 13:07:31 +0000651 # Wait in the barrier until we are released. Raise an exception
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000652 # if the barrier is reset or broken.
653 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000654 if not self._cond.wait_for(lambda : self._state != 0, timeout):
655 #timed out. Break the barrier
656 self._break()
657 raise BrokenBarrierError
658 if self._state < 0:
659 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000660 assert self._state == 1
661
662 # If we are the last thread to exit the barrier, signal any threads
663 # waiting for the barrier to drain.
664 def _exit(self):
665 if self._count == 0:
666 if self._state in (-1, 1):
667 #resetting or draining
668 self._state = 0
669 self._cond.notify_all()
670
671 def reset(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200672 """Reset the barrier to the initial state.
673
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000674 Any threads currently waiting will get the BrokenBarrier exception
675 raised.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200676
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000677 """
678 with self._cond:
679 if self._count > 0:
680 if self._state == 0:
681 #reset the barrier, waking up threads
682 self._state = -1
683 elif self._state == -2:
684 #was broken, set it to reset state
685 #which clears when the last thread exits
686 self._state = -1
687 else:
688 self._state = 0
689 self._cond.notify_all()
690
691 def abort(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200692 """Place the barrier into a 'broken' state.
693
694 Useful in case of error. Any currently waiting threads and threads
695 attempting to 'wait()' will have BrokenBarrierError raised.
696
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000697 """
698 with self._cond:
699 self._break()
700
701 def _break(self):
702 # An internal error was detected. The barrier is set to
703 # a broken state all parties awakened.
704 self._state = -2
705 self._cond.notify_all()
706
707 @property
708 def parties(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200709 """Return the number of threads required to trip the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000710 return self._parties
711
712 @property
713 def n_waiting(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200714 """Return the number of threads currently waiting at the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000715 # We don't need synchronization here since this is an ephemeral result
716 # anyway. It returns the correct value in the steady state.
717 if self._state == 0:
718 return self._count
719 return 0
720
721 @property
722 def broken(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200723 """Return True if the barrier is in a broken state."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000724 return self._state == -2
725
Georg Brandlc30b59f2013-10-13 10:43:59 +0200726# exception raised by the Barrier class
727class BrokenBarrierError(RuntimeError):
728 pass
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000729
730
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000731# Helper to generate new thread names
R David Murrayb186f1df2014-10-04 17:43:54 -0400732_counter = _count().__next__
733_counter() # Consume 0 so first non-main thread has id 1.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000734def _newname(template="Thread-%d"):
R David Murrayb186f1df2014-10-04 17:43:54 -0400735 return template % _counter()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000736
737# Active thread administration
738_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000739_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000740_limbo = {}
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200741_dangling = WeakSet()
Victor Stinnere40a97a2019-06-13 13:44:23 +0200742# Set of Thread._tstate_lock locks of non-daemon threads used by _shutdown()
743# to wait until all Python thread states get deleted:
744# see Thread._set_tstate_lock().
745_shutdown_locks_lock = _allocate_lock()
746_shutdown_locks = set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000747
748# Main class for threads
749
Victor Stinner135b6d82012-03-03 01:32:57 +0100750class Thread:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200751 """A class that represents a thread of control.
752
753 This class can be safely subclassed in a limited fashion. There are two ways
754 to specify the activity: by passing a callable object to the constructor, or
755 by overriding the run() method in a subclass.
756
757 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000758
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300759 _initialized = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000760
761 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100762 args=(), kwargs=None, *, daemon=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200763 """This constructor should always be called with keyword arguments. Arguments are:
764
765 *group* should be None; reserved for future extension when a ThreadGroup
766 class is implemented.
767
768 *target* is the callable object to be invoked by the run()
769 method. Defaults to None, meaning nothing is called.
770
771 *name* is the thread name. By default, a unique name is constructed of
772 the form "Thread-N" where N is a small decimal number.
773
774 *args* is the argument tuple for the target invocation. Defaults to ().
775
776 *kwargs* is a dictionary of keyword arguments for the target
777 invocation. Defaults to {}.
778
779 If a subclass overrides the constructor, it must make sure to invoke
780 the base class constructor (Thread.__init__()) before doing anything
781 else to the thread.
782
783 """
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000784 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000785 if kwargs is None:
786 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000787 self._target = target
788 self._name = str(name or _newname())
789 self._args = args
790 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000791 if daemon is not None:
792 self._daemonic = daemon
793 else:
794 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000795 self._ident = None
Jake Teslerb121f632019-05-22 08:43:17 -0700796 if _HAVE_THREAD_NATIVE_ID:
797 self._native_id = None
Antoine Pitrou7b476992013-09-07 23:38:37 +0200798 self._tstate_lock = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000799 self._started = Event()
Tim Petersc363a232013-09-08 18:44:40 -0500800 self._is_stopped = False
Guido van Rossumd0648992007-08-20 19:25:41 +0000801 self._initialized = True
Victor Stinnercd590a72019-05-28 00:39:52 +0200802 # Copy of sys.stderr used by self._invoke_excepthook()
Guido van Rossumd0648992007-08-20 19:25:41 +0000803 self._stderr = _sys.stderr
Victor Stinnercd590a72019-05-28 00:39:52 +0200804 self._invoke_excepthook = _make_invoke_excepthook()
Antoine Pitrou5da7e792013-09-08 13:19:06 +0200805 # For debugging and _after_fork()
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200806 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000807
Antoine Pitrou7b476992013-09-07 23:38:37 +0200808 def _reset_internal_locks(self, is_alive):
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000809 # private! Called by _after_fork() to reset our internal locks as
810 # they may be in an invalid state leading to a deadlock or crash.
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000811 self._started._reset_internal_locks()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200812 if is_alive:
813 self._set_tstate_lock()
814 else:
815 # The thread isn't alive after fork: it doesn't have a tstate
816 # anymore.
Tim Petersb5e9ac92013-09-09 14:41:50 -0500817 self._is_stopped = True
Antoine Pitrou7b476992013-09-07 23:38:37 +0200818 self._tstate_lock = None
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000819
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000820 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000821 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000822 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000823 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000824 status = "started"
Tim Peters72460fa2013-09-09 18:48:24 -0500825 self.is_alive() # easy way to get ._is_stopped set when appropriate
Tim Petersc363a232013-09-08 18:44:40 -0500826 if self._is_stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000827 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000828 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000829 status += " daemon"
830 if self._ident is not None:
831 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000832 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000833
834 def start(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200835 """Start the thread's activity.
836
837 It must be called at most once per thread object. It arranges for the
838 object's run() method to be invoked in a separate thread of control.
839
840 This method will raise a RuntimeError if called more than once on the
841 same thread object.
842
843 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000844 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000845 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000846
Benjamin Peterson672b8032008-06-11 19:14:14 +0000847 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000848 raise RuntimeError("threads can only be started once")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000849 with _active_limbo_lock:
850 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000851 try:
852 _start_new_thread(self._bootstrap, ())
853 except Exception:
854 with _active_limbo_lock:
855 del _limbo[self]
856 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000857 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000858
859 def run(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200860 """Method representing the thread's activity.
861
862 You may override this method in a subclass. The standard run() method
863 invokes the callable object passed to the object's constructor as the
864 target argument, if any, with sequential and keyword arguments taken
865 from the args and kwargs arguments, respectively.
866
867 """
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000868 try:
869 if self._target:
870 self._target(*self._args, **self._kwargs)
871 finally:
872 # Avoid a refcycle if the thread is running a function with
873 # an argument that has a member that points to the thread.
874 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000875
Guido van Rossumd0648992007-08-20 19:25:41 +0000876 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000877 # Wrapper around the real bootstrap code that ignores
878 # exceptions during interpreter cleanup. Those typically
879 # happen when a daemon thread wakes up at an unfortunate
880 # moment, finds the world around it destroyed, and raises some
881 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000882 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000883 # don't help anybody, and they confuse users, so we suppress
884 # them. We suppress them only when it appears that the world
885 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000886 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000887 # reported. Also, we only suppress them for daemonic threads;
888 # if a non-daemonic encounters this, something else is wrong.
889 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000890 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000891 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000892 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000893 return
894 raise
895
Benjamin Petersond23f8222009-04-05 19:13:16 +0000896 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200897 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000898
Jake Teslerb121f632019-05-22 08:43:17 -0700899 if _HAVE_THREAD_NATIVE_ID:
900 def _set_native_id(self):
901 self._native_id = get_native_id()
902
Antoine Pitrou7b476992013-09-07 23:38:37 +0200903 def _set_tstate_lock(self):
904 """
905 Set a lock object which will be released by the interpreter when
906 the underlying thread state (see pystate.h) gets deleted.
907 """
908 self._tstate_lock = _set_sentinel()
909 self._tstate_lock.acquire()
910
Victor Stinnere40a97a2019-06-13 13:44:23 +0200911 if not self.daemon:
912 with _shutdown_locks_lock:
913 _shutdown_locks.add(self._tstate_lock)
914
Guido van Rossumd0648992007-08-20 19:25:41 +0000915 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000916 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000917 self._set_ident()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200918 self._set_tstate_lock()
Jake Teslerb121f632019-05-22 08:43:17 -0700919 if _HAVE_THREAD_NATIVE_ID:
920 self._set_native_id()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000921 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000922 with _active_limbo_lock:
923 _active[self._ident] = self
924 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000925
926 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000927 _sys.settrace(_trace_hook)
928 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000929 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000930
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000931 try:
932 self.run()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000933 except:
Victor Stinnercd590a72019-05-28 00:39:52 +0200934 self._invoke_excepthook(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000935 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000936 with _active_limbo_lock:
Christian Heimes1af737c2008-01-23 08:24:23 +0000937 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000938 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000939 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200940 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000941 except:
942 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000943
Guido van Rossumd0648992007-08-20 19:25:41 +0000944 def _stop(self):
Tim Petersb5e9ac92013-09-09 14:41:50 -0500945 # After calling ._stop(), .is_alive() returns False and .join() returns
946 # immediately. ._tstate_lock must be released before calling ._stop().
947 #
948 # Normal case: C code at the end of the thread's life
949 # (release_sentinel in _threadmodule.c) releases ._tstate_lock, and
950 # that's detected by our ._wait_for_tstate_lock(), called by .join()
951 # and .is_alive(). Any number of threads _may_ call ._stop()
952 # simultaneously (for example, if multiple threads are blocked in
953 # .join() calls), and they're not serialized. That's harmless -
954 # they'll just make redundant rebindings of ._is_stopped and
955 # ._tstate_lock. Obscure: we rebind ._tstate_lock last so that the
956 # "assert self._is_stopped" in ._wait_for_tstate_lock() always works
957 # (the assert is executed only if ._tstate_lock is None).
958 #
959 # Special case: _main_thread releases ._tstate_lock via this
960 # module's _shutdown() function.
961 lock = self._tstate_lock
962 if lock is not None:
963 assert not lock.locked()
Tim Peters78755232013-09-09 13:47:16 -0500964 self._is_stopped = True
965 self._tstate_lock = None
Victor Stinnere40a97a2019-06-13 13:44:23 +0200966 if not self.daemon:
967 with _shutdown_locks_lock:
968 _shutdown_locks.discard(lock)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000969
Guido van Rossumd0648992007-08-20 19:25:41 +0000970 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000971 "Remove current thread from the dict of currently running threads."
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200972 with _active_limbo_lock:
973 del _active[get_ident()]
974 # There must not be any python code between the previous line
975 # and after the lock is released. Otherwise a tracing function
976 # could try to acquire the lock again in the same thread, (in
977 # current_thread()), and would block.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000978
979 def join(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200980 """Wait until the thread terminates.
981
982 This blocks the calling thread until the thread whose join() method is
983 called terminates -- either normally or through an unhandled exception
984 or until the optional timeout occurs.
985
986 When the timeout argument is present and not None, it should be a
987 floating point number specifying a timeout for the operation in seconds
988 (or fractions thereof). As join() always returns None, you must call
Dong-hee Na36d9e9a2019-01-18 18:50:47 +0900989 is_alive() after join() to decide whether a timeout happened -- if the
Georg Brandlc30b59f2013-10-13 10:43:59 +0200990 thread is still alive, the join() call timed out.
991
992 When the timeout argument is not present or None, the operation will
993 block until the thread terminates.
994
995 A thread can be join()ed many times.
996
997 join() raises a RuntimeError if an attempt is made to join the current
998 thread as that would cause a deadlock. It is also an error to join() a
999 thread before it has been started and attempts to do so raises the same
1000 exception.
1001
1002 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001003 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001004 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001005 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001006 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001007 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001008 raise RuntimeError("cannot join current thread")
Tim Peterse5bb0bf2013-10-25 20:46:51 -05001009
Tim Petersc363a232013-09-08 18:44:40 -05001010 if timeout is None:
1011 self._wait_for_tstate_lock()
Tim Peters7bad39f2013-10-25 22:33:52 -05001012 else:
1013 # the behavior of a negative timeout isn't documented, but
Tim Petersa577f1e2013-10-26 11:56:16 -05001014 # historically .join(timeout=x) for x<0 has acted as if timeout=0
Tim Peters7bad39f2013-10-25 22:33:52 -05001015 self._wait_for_tstate_lock(timeout=max(timeout, 0))
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001016
Tim Petersc363a232013-09-08 18:44:40 -05001017 def _wait_for_tstate_lock(self, block=True, timeout=-1):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001018 # Issue #18808: wait for the thread state to be gone.
Tim Petersc363a232013-09-08 18:44:40 -05001019 # At the end of the thread's life, after all knowledge of the thread
1020 # is removed from C data structures, C code releases our _tstate_lock.
Martin Panter46f50722016-05-26 05:35:26 +00001021 # This method passes its arguments to _tstate_lock.acquire().
Tim Petersc363a232013-09-08 18:44:40 -05001022 # If the lock is acquired, the C code is done, and self._stop() is
1023 # called. That sets ._is_stopped to True, and ._tstate_lock to None.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001024 lock = self._tstate_lock
Tim Petersc363a232013-09-08 18:44:40 -05001025 if lock is None: # already determined that the C code is done
1026 assert self._is_stopped
1027 elif lock.acquire(block, timeout):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001028 lock.release()
Tim Petersc363a232013-09-08 18:44:40 -05001029 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001030
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001031 @property
1032 def name(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001033 """A string used for identification purposes only.
1034
1035 It has no semantics. Multiple threads may be given the same name. The
1036 initial name is set by the constructor.
1037
1038 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001039 assert self._initialized, "Thread.__init__() not called"
1040 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001041
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001042 @name.setter
1043 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +00001044 assert self._initialized, "Thread.__init__() not called"
1045 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001046
Benjamin Peterson773c17b2008-08-18 16:45:31 +00001047 @property
1048 def ident(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001049 """Thread identifier of this thread or None if it has not been started.
1050
Skip Montanaro56343312018-05-18 13:38:36 -05001051 This is a nonzero integer. See the get_ident() function. Thread
Georg Brandlc30b59f2013-10-13 10:43:59 +02001052 identifiers may be recycled when a thread exits and another thread is
1053 created. The identifier is available even after the thread has exited.
1054
1055 """
Georg Brandl0c77a822008-06-10 16:37:50 +00001056 assert self._initialized, "Thread.__init__() not called"
1057 return self._ident
1058
Jake Teslerb121f632019-05-22 08:43:17 -07001059 if _HAVE_THREAD_NATIVE_ID:
1060 @property
1061 def native_id(self):
1062 """Native integral thread ID of this thread, or None if it has not been started.
1063
1064 This is a non-negative integer. See the get_native_id() function.
1065 This represents the Thread ID as reported by the kernel.
1066
1067 """
1068 assert self._initialized, "Thread.__init__() not called"
1069 return self._native_id
1070
Benjamin Peterson672b8032008-06-11 19:14:14 +00001071 def is_alive(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001072 """Return whether the thread is alive.
1073
1074 This method returns True just before the run() method starts until just
1075 after the run() method terminates. The module function enumerate()
1076 returns a list of all alive threads.
1077
1078 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001079 assert self._initialized, "Thread.__init__() not called"
Tim Petersc363a232013-09-08 18:44:40 -05001080 if self._is_stopped or not self._started.is_set():
Antoine Pitrou7b476992013-09-07 23:38:37 +02001081 return False
Antoine Pitrou7b476992013-09-07 23:38:37 +02001082 self._wait_for_tstate_lock(False)
Tim Petersc363a232013-09-08 18:44:40 -05001083 return not self._is_stopped
Tim Petersb90f89a2001-01-15 03:26:36 +00001084
Dong-hee Na89669ff2019-01-17 21:14:45 +09001085 def isAlive(self):
1086 """Return whether the thread is alive.
1087
1088 This method is deprecated, use is_alive() instead.
1089 """
1090 import warnings
1091 warnings.warn('isAlive() is deprecated, use is_alive() instead',
1092 DeprecationWarning, stacklevel=2)
1093 return self.is_alive()
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001094
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001095 @property
1096 def daemon(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001097 """A boolean value indicating whether this thread is a daemon thread.
1098
1099 This must be set before start() is called, otherwise RuntimeError is
1100 raised. Its initial value is inherited from the creating thread; the
1101 main thread is not a daemon thread and therefore all threads created in
1102 the main thread default to daemon = False.
1103
Miss Islington (bot)ea380f62019-06-23 12:06:21 -07001104 The entire Python program exits when only daemon threads are left.
Georg Brandlc30b59f2013-10-13 10:43:59 +02001105
1106 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001107 assert self._initialized, "Thread.__init__() not called"
1108 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001109
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001110 @daemon.setter
1111 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +00001112 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001113 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001114 if self._started.is_set():
Antoine Pitrou10959072014-03-17 18:22:41 +01001115 raise RuntimeError("cannot set daemon status of active thread")
Guido van Rossumd0648992007-08-20 19:25:41 +00001116 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001117
Benjamin Peterson6640d722008-08-18 18:16:46 +00001118 def isDaemon(self):
1119 return self.daemon
1120
1121 def setDaemon(self, daemonic):
1122 self.daemon = daemonic
1123
1124 def getName(self):
1125 return self.name
1126
1127 def setName(self, name):
1128 self.name = name
1129
Victor Stinnercd590a72019-05-28 00:39:52 +02001130
1131try:
1132 from _thread import (_excepthook as excepthook,
1133 _ExceptHookArgs as ExceptHookArgs)
1134except ImportError:
1135 # Simple Python implementation if _thread._excepthook() is not available
1136 from traceback import print_exception as _print_exception
1137 from collections import namedtuple
1138
1139 _ExceptHookArgs = namedtuple(
1140 'ExceptHookArgs',
1141 'exc_type exc_value exc_traceback thread')
1142
1143 def ExceptHookArgs(args):
1144 return _ExceptHookArgs(*args)
1145
1146 def excepthook(args, /):
1147 """
1148 Handle uncaught Thread.run() exception.
1149 """
1150 if args.exc_type == SystemExit:
1151 # silently ignore SystemExit
1152 return
1153
1154 if _sys is not None and _sys.stderr is not None:
1155 stderr = _sys.stderr
1156 elif args.thread is not None:
1157 stderr = args.thread._stderr
1158 if stderr is None:
1159 # do nothing if sys.stderr is None and sys.stderr was None
1160 # when the thread was created
1161 return
1162 else:
1163 # do nothing if sys.stderr is None and args.thread is None
1164 return
1165
1166 if args.thread is not None:
1167 name = args.thread.name
1168 else:
1169 name = get_ident()
1170 print(f"Exception in thread {name}:",
1171 file=stderr, flush=True)
1172 _print_exception(args.exc_type, args.exc_value, args.exc_traceback,
1173 file=stderr)
1174 stderr.flush()
1175
1176
1177def _make_invoke_excepthook():
1178 # Create a local namespace to ensure that variables remain alive
1179 # when _invoke_excepthook() is called, even if it is called late during
1180 # Python shutdown. It is mostly needed for daemon threads.
1181
1182 old_excepthook = excepthook
1183 old_sys_excepthook = _sys.excepthook
1184 if old_excepthook is None:
1185 raise RuntimeError("threading.excepthook is None")
1186 if old_sys_excepthook is None:
1187 raise RuntimeError("sys.excepthook is None")
1188
1189 sys_exc_info = _sys.exc_info
1190 local_print = print
1191 local_sys = _sys
1192
1193 def invoke_excepthook(thread):
1194 global excepthook
1195 try:
1196 hook = excepthook
1197 if hook is None:
1198 hook = old_excepthook
1199
1200 args = ExceptHookArgs([*sys_exc_info(), thread])
1201
1202 hook(args)
1203 except Exception as exc:
1204 exc.__suppress_context__ = True
1205 del exc
1206
1207 if local_sys is not None and local_sys.stderr is not None:
1208 stderr = local_sys.stderr
1209 else:
1210 stderr = thread._stderr
1211
1212 local_print("Exception in threading.excepthook:",
1213 file=stderr, flush=True)
1214
1215 if local_sys is not None and local_sys.excepthook is not None:
1216 sys_excepthook = local_sys.excepthook
1217 else:
1218 sys_excepthook = old_sys_excepthook
1219
1220 sys_excepthook(*sys_exc_info())
1221 finally:
1222 # Break reference cycle (exception stored in a variable)
1223 args = None
1224
1225 return invoke_excepthook
1226
1227
Martin v. Löwis44f86962001-09-05 13:44:54 +00001228# The timer class was contributed by Itamar Shtull-Trauring
1229
Éric Araujo0cdd4452011-07-28 00:28:28 +02001230class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001231 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +00001232
Georg Brandlc30b59f2013-10-13 10:43:59 +02001233 t = Timer(30.0, f, args=None, kwargs=None)
1234 t.start()
1235 t.cancel() # stop the timer's action if it's still waiting
1236
Martin v. Löwis44f86962001-09-05 13:44:54 +00001237 """
Tim Petersb64bec32001-09-18 02:26:39 +00001238
R David Murray19aeb432013-03-30 17:19:38 -04001239 def __init__(self, interval, function, args=None, kwargs=None):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001240 Thread.__init__(self)
1241 self.interval = interval
1242 self.function = function
R David Murray19aeb432013-03-30 17:19:38 -04001243 self.args = args if args is not None else []
1244 self.kwargs = kwargs if kwargs is not None else {}
Martin v. Löwis44f86962001-09-05 13:44:54 +00001245 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +00001246
Martin v. Löwis44f86962001-09-05 13:44:54 +00001247 def cancel(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001248 """Stop the timer if it hasn't finished yet."""
Martin v. Löwis44f86962001-09-05 13:44:54 +00001249 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +00001250
Martin v. Löwis44f86962001-09-05 13:44:54 +00001251 def run(self):
1252 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +00001253 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +00001254 self.function(*self.args, **self.kwargs)
1255 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001256
Antoine Pitrou1023dbb2017-10-02 16:42:15 +02001257
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001258# Special thread class to represent the main thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001259
1260class _MainThread(Thread):
1261
1262 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001263 Thread.__init__(self, name="MainThread", daemon=False)
Tim Petersc363a232013-09-08 18:44:40 -05001264 self._set_tstate_lock()
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001265 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001266 self._set_ident()
Jake Teslerb121f632019-05-22 08:43:17 -07001267 if _HAVE_THREAD_NATIVE_ID:
1268 self._set_native_id()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001269 with _active_limbo_lock:
1270 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001271
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001272
1273# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +00001274# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001275# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +00001276# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001277# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001278# They are marked as daemon threads so we won't wait for them
1279# when we exit (conform previous semantics).
1280
1281class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +00001282
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001283 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001284 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +00001285
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001286 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001287 self._set_ident()
Jake Teslerb121f632019-05-22 08:43:17 -07001288 if _HAVE_THREAD_NATIVE_ID:
1289 self._set_native_id()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001290 with _active_limbo_lock:
1291 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001292
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +02001293 def _stop(self):
1294 pass
1295
Xiang Zhangf3a9fab2017-02-27 11:01:30 +08001296 def is_alive(self):
1297 assert not self._is_stopped and self._started.is_set()
1298 return True
1299
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001300 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001301 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001302
1303
1304# Global API functions
1305
Benjamin Peterson672b8032008-06-11 19:14:14 +00001306def current_thread():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001307 """Return the current Thread object, corresponding to the caller's thread of control.
1308
1309 If the caller's thread of control was not created through the threading
1310 module, a dummy thread object with limited functionality is returned.
1311
1312 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001313 try:
Victor Stinner2a129742011-05-30 23:02:52 +02001314 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001315 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001316 return _DummyThread()
1317
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001318currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001319
Benjamin Peterson672b8032008-06-11 19:14:14 +00001320def active_count():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001321 """Return the number of Thread objects currently alive.
1322
1323 The returned count is equal to the length of the list returned by
1324 enumerate().
1325
1326 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001327 with _active_limbo_lock:
1328 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001329
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001330activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001331
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001332def _enumerate():
1333 # Same as enumerate(), but without the lock. Internal use only.
1334 return list(_active.values()) + list(_limbo.values())
1335
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001336def enumerate():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001337 """Return a list of all Thread objects currently alive.
1338
1339 The list includes daemonic threads, dummy thread objects created by
1340 current_thread(), and the main thread. It excludes terminated threads and
1341 threads that have not yet been started.
1342
1343 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001344 with _active_limbo_lock:
1345 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001346
Georg Brandl2067bfd2008-05-25 13:05:15 +00001347from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001348
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001349# Create the main thread object,
1350# and make it available for the interpreter
1351# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001352
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001353_main_thread = _MainThread()
1354
1355def _shutdown():
Victor Stinnere40a97a2019-06-13 13:44:23 +02001356 """
1357 Wait until the Python thread state of all non-daemon threads get deleted.
1358 """
Tim Petersc363a232013-09-08 18:44:40 -05001359 # Obscure: other threads may be waiting to join _main_thread. That's
1360 # dubious, but some code does it. We can't wait for C code to release
1361 # the main thread's tstate_lock - that won't happen until the interpreter
1362 # is nearly dead. So we release it here. Note that just calling _stop()
1363 # isn't enough: other threads may already be waiting on _tstate_lock.
Antoine Pitrouee84a602017-08-16 20:53:28 +02001364 if _main_thread._is_stopped:
1365 # _shutdown() was already called
1366 return
Victor Stinnere40a97a2019-06-13 13:44:23 +02001367
1368 # Main thread
Tim Petersb5e9ac92013-09-09 14:41:50 -05001369 tlock = _main_thread._tstate_lock
1370 # The main thread isn't finished yet, so its thread state lock can't have
1371 # been released.
1372 assert tlock is not None
1373 assert tlock.locked()
1374 tlock.release()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001375 _main_thread._stop()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001376
Victor Stinnere40a97a2019-06-13 13:44:23 +02001377 # Join all non-deamon threads
1378 while True:
1379 with _shutdown_locks_lock:
1380 locks = list(_shutdown_locks)
1381 _shutdown_locks.clear()
1382
1383 if not locks:
1384 break
1385
1386 for lock in locks:
1387 # mimick Thread.join()
1388 lock.acquire()
1389 lock.release()
1390
1391 # new threads can be spawned while we were waiting for the other
1392 # threads to complete
1393
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001394
1395def main_thread():
Andrew Svetlovb1dd5572013-09-04 10:33:11 +03001396 """Return the main thread object.
1397
1398 In normal conditions, the main thread is the thread from which the
1399 Python interpreter was started.
1400 """
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001401 return _main_thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001402
Jim Fultond15dc062004-07-14 19:11:50 +00001403# get thread-local implementation, either from the thread
1404# module, or from the python fallback
1405
1406try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001407 from _thread import _local as local
Brett Cannoncd171c82013-07-04 17:43:24 -04001408except ImportError:
Jim Fultond15dc062004-07-14 19:11:50 +00001409 from _threading_local import local
1410
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001411
Jesse Nollera8513972008-07-17 16:49:17 +00001412def _after_fork():
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001413 """
1414 Cleanup threading module state that should not exist after a fork.
1415 """
Jesse Nollera8513972008-07-17 16:49:17 +00001416 # Reset _active_limbo_lock, in case we forked while the lock was held
1417 # by another (non-forked) thread. http://bugs.python.org/issue874900
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001418 global _active_limbo_lock, _main_thread
Victor Stinnere40a97a2019-06-13 13:44:23 +02001419 global _shutdown_locks_lock, _shutdown_locks
Jesse Nollera8513972008-07-17 16:49:17 +00001420 _active_limbo_lock = _allocate_lock()
1421
1422 # fork() only copied the current thread; clear references to others.
1423 new_active = {}
1424 current = current_thread()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001425 _main_thread = current
Victor Stinnere40a97a2019-06-13 13:44:23 +02001426
1427 # reset _shutdown() locks: threads re-register their _tstate_lock below
1428 _shutdown_locks_lock = _allocate_lock()
1429 _shutdown_locks = set()
1430
Jesse Nollera8513972008-07-17 16:49:17 +00001431 with _active_limbo_lock:
Antoine Pitrou5da7e792013-09-08 13:19:06 +02001432 # Dangling thread instances must still have their locks reset,
1433 # because someone may join() them.
1434 threads = set(_enumerate())
1435 threads.update(_dangling)
1436 for thread in threads:
Charles-François Natalib055bf62011-12-18 18:45:16 +01001437 # Any lock/condition variable may be currently locked or in an
1438 # invalid state, so we reinitialize them.
Jesse Nollera8513972008-07-17 16:49:17 +00001439 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001440 # There is only one active thread. We reset the ident to
1441 # its new value since it can have changed.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001442 thread._reset_internal_locks(True)
Victor Stinner2a129742011-05-30 23:02:52 +02001443 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001444 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +00001445 new_active[ident] = thread
1446 else:
1447 # All the others are already stopped.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001448 thread._reset_internal_locks(False)
Charles-François Natalib055bf62011-12-18 18:45:16 +01001449 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +00001450
1451 _limbo.clear()
1452 _active.clear()
1453 _active.update(new_active)
1454 assert len(_active) == 1
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001455
1456
Gregory P. Smith163468a2017-05-29 10:03:41 -07001457if hasattr(_os, "register_at_fork"):
1458 _os.register_at_fork(after_in_child=_after_fork)