blob: 06c77f70fe74f59e186ddf751a795336f82fef70 [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
Kyle Stanleyb61b8182020-03-27 15:31:22 -04006import functools
Fred Drakea8725952002-12-30 23:32:50 +00007
Victor Stinnerae586492014-09-02 23:18:25 +02008from time import monotonic as _time
Antoine Pitrouc081c0c2011-07-15 22:12:24 +02009from _weakrefset import WeakSet
R David Murrayb186f1df2014-10-04 17:43:54 -040010from itertools import islice as _islice, count as _count
Raymond Hettingerec4b1742013-03-10 17:57:28 -070011try:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070012 from _collections import deque as _deque
Brett Cannoncd171c82013-07-04 17:43:24 -040013except ImportError:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070014 from collections import deque as _deque
Guido van Rossum7f5013a1998-04-09 22:01:42 +000015
Benjamin Petersonb3085c92008-09-01 23:09:31 +000016# Note regarding PEP 8 compliant names
17# This threading model was originally inspired by Java, and inherited
18# the convention of camelCase function and method names from that
Ezio Melotti30b9d5d2013-08-17 15:50:46 +030019# language. Those original names are not in any imminent danger of
Benjamin Petersonb3085c92008-09-01 23:09:31 +000020# being deprecated (even for Py3k),so this module provides them as an
21# alias for the PEP 8 compliant names
22# Note that using the new PEP 8 compliant names facilitates substitution
23# with the multiprocessing module, which doesn't provide the old
24# Java inspired names.
25
Victor Stinnerd12e7572019-05-21 12:44:57 +020026__all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
27 'enumerate', 'main_thread', 'TIMEOUT_MAX',
Martin Panter19e69c52015-11-14 12:46:42 +000028 'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
29 'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
Victor Stinnercd590a72019-05-28 00:39:52 +020030 'setprofile', 'settrace', 'local', 'stack_size',
31 'excepthook', 'ExceptHookArgs']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000032
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000033# Rename some stuff so "from threading import *" is safe
Georg Brandl2067bfd2008-05-25 13:05:15 +000034_start_new_thread = _thread.start_new_thread
35_allocate_lock = _thread.allocate_lock
Antoine Pitrou7b476992013-09-07 23:38:37 +020036_set_sentinel = _thread._set_sentinel
Victor Stinner2a129742011-05-30 23:02:52 +020037get_ident = _thread.get_ident
Jake Teslerb121f632019-05-22 08:43:17 -070038try:
39 get_native_id = _thread.get_native_id
40 _HAVE_THREAD_NATIVE_ID = True
41 __all__.append('get_native_id')
42except AttributeError:
43 _HAVE_THREAD_NATIVE_ID = False
Georg Brandl2067bfd2008-05-25 13:05:15 +000044ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000045try:
46 _CRLock = _thread.RLock
47except AttributeError:
48 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000049TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000050del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000051
Guido van Rossum7f5013a1998-04-09 22:01:42 +000052
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000053# Support for profile and trace hooks
54
55_profile_hook = None
56_trace_hook = None
57
58def setprofile(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020059 """Set a profile function for all threads started from the threading module.
60
61 The func will be passed to sys.setprofile() for each thread, before its
62 run() method is called.
63
64 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000065 global _profile_hook
66 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000067
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000068def settrace(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020069 """Set a trace function for all threads started from the threading module.
70
71 The func will be passed to sys.settrace() for each thread, before its run()
72 method is called.
73
74 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000075 global _trace_hook
76 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000077
78# Synchronization classes
79
80Lock = _allocate_lock
81
Victor Stinner135b6d82012-03-03 01:32:57 +010082def RLock(*args, **kwargs):
Georg Brandlc30b59f2013-10-13 10:43:59 +020083 """Factory function that returns a new reentrant lock.
84
85 A reentrant lock must be released by the thread that acquired it. Once a
86 thread has acquired a reentrant lock, the same thread may acquire it again
87 without blocking; the thread must release it once for each time it has
88 acquired it.
89
90 """
Victor Stinner135b6d82012-03-03 01:32:57 +010091 if _CRLock is None:
92 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000093 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000094
Victor Stinner135b6d82012-03-03 01:32:57 +010095class _RLock:
Georg Brandlc30b59f2013-10-13 10:43:59 +020096 """This class implements reentrant lock objects.
97
98 A reentrant lock must be released by the thread that acquired it. Once a
99 thread has acquired a reentrant lock, the same thread may acquire it
100 again without blocking; the thread must release it once for each time it
101 has acquired it.
102
103 """
Tim Petersb90f89a2001-01-15 03:26:36 +0000104
Victor Stinner135b6d82012-03-03 01:32:57 +0100105 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000106 self._block = _allocate_lock()
107 self._owner = None
108 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000109
110 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000111 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000112 try:
113 owner = _active[owner].name
114 except KeyError:
115 pass
Raymond Hettinger62f4dad2014-05-25 18:22:35 -0700116 return "<%s %s.%s object owner=%r count=%d at %s>" % (
117 "locked" if self._block.locked() else "unlocked",
118 self.__class__.__module__,
119 self.__class__.__qualname__,
120 owner,
121 self._count,
122 hex(id(self))
123 )
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000124
Victor Stinner87255be2020-04-07 23:11:49 +0200125 def _at_fork_reinit(self):
126 self._block._at_fork_reinit()
127 self._owner = None
128 self._count = 0
129
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000130 def acquire(self, blocking=True, timeout=-1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200131 """Acquire a lock, blocking or non-blocking.
132
133 When invoked without arguments: if this thread already owns the lock,
134 increment the recursion level by one, and return immediately. Otherwise,
135 if another thread owns the lock, block until the lock is unlocked. Once
136 the lock is unlocked (not owned by any thread), then grab ownership, set
137 the recursion level to one, and return. If more than one thread is
138 blocked waiting until the lock is unlocked, only one at a time will be
139 able to grab ownership of the lock. There is no return value in this
140 case.
141
142 When invoked with the blocking argument set to true, do the same thing
143 as when called without arguments, and return true.
144
145 When invoked with the blocking argument set to false, do not block. If a
146 call without an argument would block, return false immediately;
147 otherwise, do the same thing as when called without arguments, and
148 return true.
149
150 When invoked with the floating-point timeout argument set to a positive
151 value, block for at most the number of seconds specified by timeout
152 and as long as the lock cannot be acquired. Return true if the lock has
153 been acquired, false if the timeout has elapsed.
154
155 """
Victor Stinner2a129742011-05-30 23:02:52 +0200156 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +0000157 if self._owner == me:
Raymond Hettinger720da572013-03-10 15:13:35 -0700158 self._count += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000159 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000160 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000161 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000162 self._owner = me
163 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000164 return rc
165
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000166 __enter__ = acquire
167
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000168 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200169 """Release a lock, decrementing the recursion level.
170
171 If after the decrement it is zero, reset the lock to unlocked (not owned
172 by any thread), and if any other threads are blocked waiting for the
173 lock to become unlocked, allow exactly one of them to proceed. If after
174 the decrement the recursion level is still nonzero, the lock remains
175 locked and owned by the calling thread.
176
177 Only call this method when the calling thread owns the lock. A
178 RuntimeError is raised if this method is called when the lock is
179 unlocked.
180
181 There is no return value.
182
183 """
Victor Stinner2a129742011-05-30 23:02:52 +0200184 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000185 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000186 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000187 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000188 self._owner = None
189 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000190
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000191 def __exit__(self, t, v, tb):
192 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000193
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000194 # Internal methods used by condition variables
195
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000196 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000197 self._block.acquire()
198 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000199
200 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200201 if self._count == 0:
202 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000203 count = self._count
204 self._count = 0
205 owner = self._owner
206 self._owner = None
207 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000208 return (count, owner)
209
210 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200211 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000212
Antoine Pitrou434736a2009-11-10 18:46:01 +0000213_PyRLock = _RLock
214
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000215
Victor Stinner135b6d82012-03-03 01:32:57 +0100216class Condition:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200217 """Class that implements a condition variable.
218
219 A condition variable allows one or more threads to wait until they are
220 notified by another thread.
221
222 If the lock argument is given and not None, it must be a Lock or RLock
223 object, and it is used as the underlying lock. Otherwise, a new RLock object
224 is created and used as the underlying lock.
225
226 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000227
Victor Stinner135b6d82012-03-03 01:32:57 +0100228 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000229 if lock is None:
230 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000231 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000232 # Export the lock's acquire() and release() methods
233 self.acquire = lock.acquire
234 self.release = lock.release
235 # If the lock defines _release_save() and/or _acquire_restore(),
236 # these override the default implementations (which just call
237 # release() and acquire() on the lock). Ditto for _is_owned().
238 try:
239 self._release_save = lock._release_save
240 except AttributeError:
241 pass
242 try:
243 self._acquire_restore = lock._acquire_restore
244 except AttributeError:
245 pass
246 try:
247 self._is_owned = lock._is_owned
248 except AttributeError:
249 pass
Raymond Hettingerec4b1742013-03-10 17:57:28 -0700250 self._waiters = _deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000251
Victor Stinner87255be2020-04-07 23:11:49 +0200252 def _at_fork_reinit(self):
253 self._lock._at_fork_reinit()
254 self._waiters.clear()
255
Thomas Wouters477c8d52006-05-27 19:21:47 +0000256 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000257 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000258
Thomas Wouters477c8d52006-05-27 19:21:47 +0000259 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000260 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000261
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000262 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000263 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000264
265 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000266 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000267
268 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000269 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000270
271 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000272 # Return True if lock is owned by current_thread.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300273 # This method is called only if _lock doesn't have _is_owned().
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +0300274 if self._lock.acquire(False):
Guido van Rossumd0648992007-08-20 19:25:41 +0000275 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000276 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000277 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000278 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000279
280 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200281 """Wait until notified or until a timeout occurs.
282
283 If the calling thread has not acquired the lock when this method is
284 called, a RuntimeError is raised.
285
286 This method releases the underlying lock, and then blocks until it is
287 awakened by a notify() or notify_all() call for the same condition
288 variable in another thread, or until the optional timeout occurs. Once
289 awakened or timed out, it re-acquires the lock and returns.
290
291 When the timeout argument is present and not None, it should be a
292 floating point number specifying a timeout for the operation in seconds
293 (or fractions thereof).
294
295 When the underlying lock is an RLock, it is not released using its
296 release() method, since this may not actually unlock the lock when it
297 was acquired multiple times recursively. Instead, an internal interface
298 of the RLock class is used, which really unlocks it even when it has
299 been recursively acquired several times. Another internal interface is
300 then used to restore the recursion level when the lock is reacquired.
301
302 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000303 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000304 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000305 waiter = _allocate_lock()
306 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000307 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000308 saved_state = self._release_save()
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200309 gotit = False
Tim Petersc951bf92001-04-02 20:15:57 +0000310 try: # restore state no matter what (e.g., KeyboardInterrupt)
311 if timeout is None:
312 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000313 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000314 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000315 if timeout > 0:
316 gotit = waiter.acquire(True, timeout)
317 else:
318 gotit = waiter.acquire(False)
Georg Brandlb9a43912010-10-28 09:03:20 +0000319 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000320 finally:
321 self._acquire_restore(saved_state)
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200322 if not gotit:
323 try:
324 self._waiters.remove(waiter)
325 except ValueError:
326 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000327
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000328 def wait_for(self, predicate, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200329 """Wait until a condition evaluates to True.
330
331 predicate should be a callable which result will be interpreted as a
332 boolean value. A timeout may be provided giving the maximum time to
333 wait.
334
335 """
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000336 endtime = None
337 waittime = timeout
338 result = predicate()
339 while not result:
340 if waittime is not None:
341 if endtime is None:
342 endtime = _time() + waittime
343 else:
344 waittime = endtime - _time()
345 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000346 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000347 self.wait(waittime)
348 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000349 return result
350
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000351 def notify(self, n=1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200352 """Wake up one or more threads waiting on this condition, if any.
353
354 If the calling thread has not acquired the lock when this method is
355 called, a RuntimeError is raised.
356
357 This method wakes up at most n of the threads waiting for the condition
358 variable; it is a no-op if no threads are waiting.
359
360 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000361 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000362 raise RuntimeError("cannot notify on un-acquired lock")
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700363 all_waiters = self._waiters
364 waiters_to_notify = _deque(_islice(all_waiters, n))
365 if not waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000366 return
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700367 for waiter in waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000368 waiter.release()
369 try:
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700370 all_waiters.remove(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000371 except ValueError:
372 pass
373
Benjamin Peterson672b8032008-06-11 19:14:14 +0000374 def notify_all(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200375 """Wake up all threads waiting on this condition.
376
377 If the calling thread has not acquired the lock when this method
378 is called, a RuntimeError is raised.
379
380 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000381 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000382
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000383 notifyAll = notify_all
384
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000385
Victor Stinner135b6d82012-03-03 01:32:57 +0100386class Semaphore:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200387 """This class implements semaphore objects.
388
389 Semaphores manage a counter representing the number of release() calls minus
390 the number of acquire() calls, plus an initial value. The acquire() method
391 blocks if necessary until it can return without making the counter
392 negative. If not given, value defaults to 1.
393
394 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000395
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000396 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000397
Victor Stinner135b6d82012-03-03 01:32:57 +0100398 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000399 if value < 0:
400 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000401 self._cond = Condition(Lock())
402 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000403
Antoine Pitrou0454af92010-04-17 23:51:58 +0000404 def acquire(self, blocking=True, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200405 """Acquire a semaphore, decrementing the internal counter by one.
406
407 When invoked without arguments: if the internal counter is larger than
408 zero on entry, decrement it by one and return immediately. If it is zero
409 on entry, block, waiting until some other thread has called release() to
410 make it larger than zero. This is done with proper interlocking so that
411 if multiple acquire() calls are blocked, release() will wake exactly one
412 of them up. The implementation may pick one at random, so the order in
413 which blocked threads are awakened should not be relied on. There is no
414 return value in this case.
415
416 When invoked with blocking set to true, do the same thing as when called
417 without arguments, and return true.
418
419 When invoked with blocking set to false, do not block. If a call without
420 an argument would block, return false immediately; otherwise, do the
421 same thing as when called without arguments, and return true.
422
423 When invoked with a timeout other than None, it will block for at
424 most timeout seconds. If acquire does not complete successfully in
425 that interval, return false. Return true otherwise.
426
427 """
Antoine Pitrou0454af92010-04-17 23:51:58 +0000428 if not blocking and timeout is not None:
429 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000430 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000431 endtime = None
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300432 with self._cond:
433 while self._value == 0:
434 if not blocking:
435 break
436 if timeout is not None:
437 if endtime is None:
438 endtime = _time() + timeout
439 else:
440 timeout = endtime - _time()
441 if timeout <= 0:
442 break
443 self._cond.wait(timeout)
444 else:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300445 self._value -= 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300446 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000447 return rc
448
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000449 __enter__ = acquire
450
Raymond Hettinger35f63012019-08-29 01:45:19 -0700451 def release(self, n=1):
452 """Release a semaphore, incrementing the internal counter by one or more.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200453
454 When the counter is zero on entry and another thread is waiting for it
455 to become larger than zero again, wake up that thread.
456
457 """
Raymond Hettinger35f63012019-08-29 01:45:19 -0700458 if n < 1:
459 raise ValueError('n must be one or more')
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300460 with self._cond:
Raymond Hettinger35f63012019-08-29 01:45:19 -0700461 self._value += n
462 for i in range(n):
463 self._cond.notify()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000464
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000465 def __exit__(self, t, v, tb):
466 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000467
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000468
Éric Araujo0cdd4452011-07-28 00:28:28 +0200469class BoundedSemaphore(Semaphore):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200470 """Implements a bounded semaphore.
471
472 A bounded semaphore checks to make sure its current value doesn't exceed its
473 initial value. If it does, ValueError is raised. In most situations
474 semaphores are used to guard resources with limited capacity.
475
476 If the semaphore is released too many times it's a sign of a bug. If not
477 given, value defaults to 1.
478
479 Like regular semaphores, bounded semaphores manage a counter representing
480 the number of release() calls minus the number of acquire() calls, plus an
481 initial value. The acquire() method blocks if necessary until it can return
482 without making the counter negative. If not given, value defaults to 1.
483
484 """
485
Victor Stinner135b6d82012-03-03 01:32:57 +0100486 def __init__(self, value=1):
487 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000488 self._initial_value = value
489
Raymond Hettinger35f63012019-08-29 01:45:19 -0700490 def release(self, n=1):
491 """Release a semaphore, incrementing the internal counter by one or more.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200492
493 When the counter is zero on entry and another thread is waiting for it
494 to become larger than zero again, wake up that thread.
495
496 If the number of releases exceeds the number of acquires,
497 raise a ValueError.
498
499 """
Raymond Hettinger35f63012019-08-29 01:45:19 -0700500 if n < 1:
501 raise ValueError('n must be one or more')
Tim Peters7634e1c2013-10-08 20:55:51 -0500502 with self._cond:
Raymond Hettinger35f63012019-08-29 01:45:19 -0700503 if self._value + n > self._initial_value:
Tim Peters7634e1c2013-10-08 20:55:51 -0500504 raise ValueError("Semaphore released too many times")
Raymond Hettinger35f63012019-08-29 01:45:19 -0700505 self._value += n
506 for i in range(n):
507 self._cond.notify()
Skip Montanaroe428bb72001-08-20 20:27:58 +0000508
509
Victor Stinner135b6d82012-03-03 01:32:57 +0100510class Event:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200511 """Class implementing event objects.
512
513 Events manage a flag that can be set to true with the set() method and reset
514 to false with the clear() method. The wait() method blocks until the flag is
515 true. The flag is initially false.
516
517 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000518
519 # After Tim Peters' event class (without is_posted())
520
Victor Stinner135b6d82012-03-03 01:32:57 +0100521 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000522 self._cond = Condition(Lock())
523 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000524
Victor Stinner87255be2020-04-07 23:11:49 +0200525 def _at_fork_reinit(self):
526 # Private method called by Thread._reset_internal_locks()
527 self._cond._at_fork_reinit()
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000528
Benjamin Peterson672b8032008-06-11 19:14:14 +0000529 def is_set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200530 """Return true if and only if the internal flag is true."""
Guido van Rossumd0648992007-08-20 19:25:41 +0000531 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000532
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000533 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000534
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000535 def set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200536 """Set the internal flag to true.
537
538 All threads waiting for it to become true are awakened. Threads
539 that call wait() once the flag is true will not block at all.
540
541 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700542 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000543 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000544 self._cond.notify_all()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000545
546 def clear(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200547 """Reset the internal flag to false.
548
549 Subsequently, threads calling wait() will block until set() is called to
550 set the internal flag to true again.
551
552 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700553 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000554 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000555
556 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200557 """Block until the internal flag is true.
558
559 If the internal flag is true on entry, return immediately. Otherwise,
560 block until another thread calls set() to set the flag to true, or until
561 the optional timeout occurs.
562
563 When the timeout argument is present and not None, it should be a
564 floating point number specifying a timeout for the operation in seconds
565 (or fractions thereof).
566
567 This method returns the internal flag on exit, so it will always return
568 True except if a timeout is given and the operation times out.
569
570 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700571 with self._cond:
Charles-François Natalided03482012-01-07 18:24:56 +0100572 signaled = self._flag
573 if not signaled:
574 signaled = self._cond.wait(timeout)
575 return signaled
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000576
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000577
578# A barrier class. Inspired in part by the pthread_barrier_* api and
579# the CyclicBarrier class from Java. See
580# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
581# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
582# CyclicBarrier.html
583# for information.
584# We maintain two main states, 'filling' and 'draining' enabling the barrier
585# to be cyclic. Threads are not allowed into it until it has fully drained
586# since the previous cycle. In addition, a 'resetting' state exists which is
587# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300588# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100589class Barrier:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200590 """Implements a Barrier.
591
592 Useful for synchronizing a fixed number of threads at known synchronization
Carl Bordum Hansen62fa51f2019-03-09 18:38:05 +0100593 points. Threads block on 'wait()' and are simultaneously awoken once they
594 have all made that call.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200595
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000596 """
Georg Brandlc30b59f2013-10-13 10:43:59 +0200597
Victor Stinner135b6d82012-03-03 01:32:57 +0100598 def __init__(self, parties, action=None, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200599 """Create a barrier, initialised to 'parties' threads.
600
601 'action' is a callable which, when supplied, will be called by one of
602 the threads after they have all entered the barrier and just prior to
Carl Bordum Hansen62fa51f2019-03-09 18:38:05 +0100603 releasing them all. If a 'timeout' is provided, it is used as the
Georg Brandlc30b59f2013-10-13 10:43:59 +0200604 default for all subsequent 'wait()' calls.
605
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000606 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000607 self._cond = Condition(Lock())
608 self._action = action
609 self._timeout = timeout
610 self._parties = parties
611 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
612 self._count = 0
613
614 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200615 """Wait for the barrier.
616
617 When the specified number of threads have started waiting, they are all
618 simultaneously awoken. If an 'action' was provided for the barrier, one
619 of the threads will have executed that callback prior to returning.
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000620 Returns an individual index number from 0 to 'parties-1'.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200621
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000622 """
623 if timeout is None:
624 timeout = self._timeout
625 with self._cond:
626 self._enter() # Block while the barrier drains.
627 index = self._count
628 self._count += 1
629 try:
630 if index + 1 == self._parties:
631 # We release the barrier
632 self._release()
633 else:
634 # We wait until someone releases us
635 self._wait(timeout)
636 return index
637 finally:
638 self._count -= 1
639 # Wake up any threads waiting for barrier to drain.
640 self._exit()
641
642 # Block until the barrier is ready for us, or raise an exception
643 # if it is broken.
644 def _enter(self):
645 while self._state in (-1, 1):
646 # It is draining or resetting, wait until done
647 self._cond.wait()
648 #see if the barrier is in a broken state
649 if self._state < 0:
650 raise BrokenBarrierError
651 assert self._state == 0
652
653 # Optionally run the 'action' and release the threads waiting
654 # in the barrier.
655 def _release(self):
656 try:
657 if self._action:
658 self._action()
659 # enter draining state
660 self._state = 1
661 self._cond.notify_all()
662 except:
663 #an exception during the _action handler. Break and reraise
664 self._break()
665 raise
666
Martin Panter69332c12016-08-04 13:07:31 +0000667 # Wait in the barrier until we are released. Raise an exception
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000668 # if the barrier is reset or broken.
669 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000670 if not self._cond.wait_for(lambda : self._state != 0, timeout):
671 #timed out. Break the barrier
672 self._break()
673 raise BrokenBarrierError
674 if self._state < 0:
675 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000676 assert self._state == 1
677
678 # If we are the last thread to exit the barrier, signal any threads
679 # waiting for the barrier to drain.
680 def _exit(self):
681 if self._count == 0:
682 if self._state in (-1, 1):
683 #resetting or draining
684 self._state = 0
685 self._cond.notify_all()
686
687 def reset(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200688 """Reset the barrier to the initial state.
689
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000690 Any threads currently waiting will get the BrokenBarrier exception
691 raised.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200692
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000693 """
694 with self._cond:
695 if self._count > 0:
696 if self._state == 0:
697 #reset the barrier, waking up threads
698 self._state = -1
699 elif self._state == -2:
700 #was broken, set it to reset state
701 #which clears when the last thread exits
702 self._state = -1
703 else:
704 self._state = 0
705 self._cond.notify_all()
706
707 def abort(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200708 """Place the barrier into a 'broken' state.
709
710 Useful in case of error. Any currently waiting threads and threads
711 attempting to 'wait()' will have BrokenBarrierError raised.
712
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000713 """
714 with self._cond:
715 self._break()
716
717 def _break(self):
718 # An internal error was detected. The barrier is set to
719 # a broken state all parties awakened.
720 self._state = -2
721 self._cond.notify_all()
722
723 @property
724 def parties(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200725 """Return the number of threads required to trip the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000726 return self._parties
727
728 @property
729 def n_waiting(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200730 """Return the number of threads currently waiting at the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000731 # We don't need synchronization here since this is an ephemeral result
732 # anyway. It returns the correct value in the steady state.
733 if self._state == 0:
734 return self._count
735 return 0
736
737 @property
738 def broken(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200739 """Return True if the barrier is in a broken state."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000740 return self._state == -2
741
Georg Brandlc30b59f2013-10-13 10:43:59 +0200742# exception raised by the Barrier class
743class BrokenBarrierError(RuntimeError):
744 pass
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000745
746
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000747# Helper to generate new thread names
Victor Stinner98c16c92020-09-23 23:21:19 +0200748_counter = _count(1).__next__
749def _newname(name_template):
750 return name_template % _counter()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000751
752# Active thread administration
753_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000754_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000755_limbo = {}
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200756_dangling = WeakSet()
Victor Stinner468e5fe2019-06-13 01:30:17 +0200757# Set of Thread._tstate_lock locks of non-daemon threads used by _shutdown()
758# to wait until all Python thread states get deleted:
759# see Thread._set_tstate_lock().
760_shutdown_locks_lock = _allocate_lock()
761_shutdown_locks = set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000762
763# Main class for threads
764
Victor Stinner135b6d82012-03-03 01:32:57 +0100765class Thread:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200766 """A class that represents a thread of control.
767
768 This class can be safely subclassed in a limited fashion. There are two ways
769 to specify the activity: by passing a callable object to the constructor, or
770 by overriding the run() method in a subclass.
771
772 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000773
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300774 _initialized = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000775
776 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100777 args=(), kwargs=None, *, daemon=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200778 """This constructor should always be called with keyword arguments. Arguments are:
779
780 *group* should be None; reserved for future extension when a ThreadGroup
781 class is implemented.
782
783 *target* is the callable object to be invoked by the run()
784 method. Defaults to None, meaning nothing is called.
785
786 *name* is the thread name. By default, a unique name is constructed of
787 the form "Thread-N" where N is a small decimal number.
788
789 *args* is the argument tuple for the target invocation. Defaults to ().
790
791 *kwargs* is a dictionary of keyword arguments for the target
792 invocation. Defaults to {}.
793
794 If a subclass overrides the constructor, it must make sure to invoke
795 the base class constructor (Thread.__init__()) before doing anything
796 else to the thread.
797
798 """
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000799 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000800 if kwargs is None:
801 kwargs = {}
Victor Stinner98c16c92020-09-23 23:21:19 +0200802 if name:
803 name = str(name)
804 else:
805 name = _newname("Thread-%d")
806 if target is not None:
807 try:
808 target_name = target.__name__
809 name += f" ({target_name})"
810 except AttributeError:
811 pass
812
Guido van Rossumd0648992007-08-20 19:25:41 +0000813 self._target = target
Victor Stinner98c16c92020-09-23 23:21:19 +0200814 self._name = name
Guido van Rossumd0648992007-08-20 19:25:41 +0000815 self._args = args
816 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000817 if daemon is not None:
818 self._daemonic = daemon
819 else:
820 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000821 self._ident = None
Jake Teslerb121f632019-05-22 08:43:17 -0700822 if _HAVE_THREAD_NATIVE_ID:
823 self._native_id = None
Antoine Pitrou7b476992013-09-07 23:38:37 +0200824 self._tstate_lock = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000825 self._started = Event()
Tim Petersc363a232013-09-08 18:44:40 -0500826 self._is_stopped = False
Guido van Rossumd0648992007-08-20 19:25:41 +0000827 self._initialized = True
Victor Stinnercd590a72019-05-28 00:39:52 +0200828 # Copy of sys.stderr used by self._invoke_excepthook()
Guido van Rossumd0648992007-08-20 19:25:41 +0000829 self._stderr = _sys.stderr
Victor Stinnercd590a72019-05-28 00:39:52 +0200830 self._invoke_excepthook = _make_invoke_excepthook()
Antoine Pitrou5da7e792013-09-08 13:19:06 +0200831 # For debugging and _after_fork()
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200832 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000833
Antoine Pitrou7b476992013-09-07 23:38:37 +0200834 def _reset_internal_locks(self, is_alive):
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000835 # private! Called by _after_fork() to reset our internal locks as
836 # they may be in an invalid state leading to a deadlock or crash.
Victor Stinner87255be2020-04-07 23:11:49 +0200837 self._started._at_fork_reinit()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200838 if is_alive:
Victor Stinner87255be2020-04-07 23:11:49 +0200839 self._tstate_lock._at_fork_reinit()
840 self._tstate_lock.acquire()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200841 else:
842 # The thread isn't alive after fork: it doesn't have a tstate
843 # anymore.
Tim Petersb5e9ac92013-09-09 14:41:50 -0500844 self._is_stopped = True
Antoine Pitrou7b476992013-09-07 23:38:37 +0200845 self._tstate_lock = None
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000846
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000847 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000848 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000849 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000850 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000851 status = "started"
Tim Peters72460fa2013-09-09 18:48:24 -0500852 self.is_alive() # easy way to get ._is_stopped set when appropriate
Tim Petersc363a232013-09-08 18:44:40 -0500853 if self._is_stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000854 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000855 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000856 status += " daemon"
857 if self._ident is not None:
858 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000859 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000860
861 def start(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200862 """Start the thread's activity.
863
864 It must be called at most once per thread object. It arranges for the
865 object's run() method to be invoked in a separate thread of control.
866
867 This method will raise a RuntimeError if called more than once on the
868 same thread object.
869
870 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000871 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000872 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000873
Benjamin Peterson672b8032008-06-11 19:14:14 +0000874 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000875 raise RuntimeError("threads can only be started once")
Victor Stinner066e5b12019-06-14 18:55:22 +0200876
Benjamin Petersond23f8222009-04-05 19:13:16 +0000877 with _active_limbo_lock:
878 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000879 try:
880 _start_new_thread(self._bootstrap, ())
881 except Exception:
882 with _active_limbo_lock:
883 del _limbo[self]
884 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000885 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000886
887 def run(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200888 """Method representing the thread's activity.
889
890 You may override this method in a subclass. The standard run() method
891 invokes the callable object passed to the object's constructor as the
892 target argument, if any, with sequential and keyword arguments taken
893 from the args and kwargs arguments, respectively.
894
895 """
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000896 try:
897 if self._target:
898 self._target(*self._args, **self._kwargs)
899 finally:
900 # Avoid a refcycle if the thread is running a function with
901 # an argument that has a member that points to the thread.
902 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000903
Guido van Rossumd0648992007-08-20 19:25:41 +0000904 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000905 # Wrapper around the real bootstrap code that ignores
906 # exceptions during interpreter cleanup. Those typically
907 # happen when a daemon thread wakes up at an unfortunate
908 # moment, finds the world around it destroyed, and raises some
909 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000910 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000911 # don't help anybody, and they confuse users, so we suppress
912 # them. We suppress them only when it appears that the world
913 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000914 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000915 # reported. Also, we only suppress them for daemonic threads;
916 # if a non-daemonic encounters this, something else is wrong.
917 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000918 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000919 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000920 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000921 return
922 raise
923
Benjamin Petersond23f8222009-04-05 19:13:16 +0000924 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200925 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000926
Jake Teslerb121f632019-05-22 08:43:17 -0700927 if _HAVE_THREAD_NATIVE_ID:
928 def _set_native_id(self):
929 self._native_id = get_native_id()
930
Antoine Pitrou7b476992013-09-07 23:38:37 +0200931 def _set_tstate_lock(self):
932 """
933 Set a lock object which will be released by the interpreter when
934 the underlying thread state (see pystate.h) gets deleted.
935 """
936 self._tstate_lock = _set_sentinel()
937 self._tstate_lock.acquire()
938
Victor Stinner468e5fe2019-06-13 01:30:17 +0200939 if not self.daemon:
940 with _shutdown_locks_lock:
941 _shutdown_locks.add(self._tstate_lock)
942
Guido van Rossumd0648992007-08-20 19:25:41 +0000943 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000944 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000945 self._set_ident()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200946 self._set_tstate_lock()
Jake Teslerb121f632019-05-22 08:43:17 -0700947 if _HAVE_THREAD_NATIVE_ID:
948 self._set_native_id()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000949 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000950 with _active_limbo_lock:
951 _active[self._ident] = self
952 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000953
954 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000955 _sys.settrace(_trace_hook)
956 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000957 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000958
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000959 try:
960 self.run()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000961 except:
Victor Stinnercd590a72019-05-28 00:39:52 +0200962 self._invoke_excepthook(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000963 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000964 with _active_limbo_lock:
Christian Heimes1af737c2008-01-23 08:24:23 +0000965 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000966 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000967 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200968 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000969 except:
970 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000971
Guido van Rossumd0648992007-08-20 19:25:41 +0000972 def _stop(self):
Tim Petersb5e9ac92013-09-09 14:41:50 -0500973 # After calling ._stop(), .is_alive() returns False and .join() returns
974 # immediately. ._tstate_lock must be released before calling ._stop().
975 #
976 # Normal case: C code at the end of the thread's life
977 # (release_sentinel in _threadmodule.c) releases ._tstate_lock, and
978 # that's detected by our ._wait_for_tstate_lock(), called by .join()
979 # and .is_alive(). Any number of threads _may_ call ._stop()
980 # simultaneously (for example, if multiple threads are blocked in
981 # .join() calls), and they're not serialized. That's harmless -
982 # they'll just make redundant rebindings of ._is_stopped and
983 # ._tstate_lock. Obscure: we rebind ._tstate_lock last so that the
984 # "assert self._is_stopped" in ._wait_for_tstate_lock() always works
985 # (the assert is executed only if ._tstate_lock is None).
986 #
987 # Special case: _main_thread releases ._tstate_lock via this
988 # module's _shutdown() function.
989 lock = self._tstate_lock
990 if lock is not None:
991 assert not lock.locked()
Tim Peters78755232013-09-09 13:47:16 -0500992 self._is_stopped = True
993 self._tstate_lock = None
Victor Stinner468e5fe2019-06-13 01:30:17 +0200994 if not self.daemon:
995 with _shutdown_locks_lock:
Victor Stinner6f75c872019-06-13 12:06:24 +0200996 _shutdown_locks.discard(lock)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000997
Guido van Rossumd0648992007-08-20 19:25:41 +0000998 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000999 "Remove current thread from the dict of currently running threads."
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001000 with _active_limbo_lock:
1001 del _active[get_ident()]
1002 # There must not be any python code between the previous line
1003 # and after the lock is released. Otherwise a tracing function
1004 # could try to acquire the lock again in the same thread, (in
1005 # current_thread()), and would block.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001006
1007 def join(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001008 """Wait until the thread terminates.
1009
1010 This blocks the calling thread until the thread whose join() method is
1011 called terminates -- either normally or through an unhandled exception
1012 or until the optional timeout occurs.
1013
1014 When the timeout argument is present and not None, it should be a
1015 floating point number specifying a timeout for the operation in seconds
1016 (or fractions thereof). As join() always returns None, you must call
Dong-hee Na36d9e9a2019-01-18 18:50:47 +09001017 is_alive() after join() to decide whether a timeout happened -- if the
Georg Brandlc30b59f2013-10-13 10:43:59 +02001018 thread is still alive, the join() call timed out.
1019
1020 When the timeout argument is not present or None, the operation will
1021 block until the thread terminates.
1022
1023 A thread can be join()ed many times.
1024
1025 join() raises a RuntimeError if an attempt is made to join the current
1026 thread as that would cause a deadlock. It is also an error to join() a
1027 thread before it has been started and attempts to do so raises the same
1028 exception.
1029
1030 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001031 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001032 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001033 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001034 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001035 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001036 raise RuntimeError("cannot join current thread")
Tim Peterse5bb0bf2013-10-25 20:46:51 -05001037
Tim Petersc363a232013-09-08 18:44:40 -05001038 if timeout is None:
1039 self._wait_for_tstate_lock()
Tim Peters7bad39f2013-10-25 22:33:52 -05001040 else:
1041 # the behavior of a negative timeout isn't documented, but
Tim Petersa577f1e2013-10-26 11:56:16 -05001042 # historically .join(timeout=x) for x<0 has acted as if timeout=0
Tim Peters7bad39f2013-10-25 22:33:52 -05001043 self._wait_for_tstate_lock(timeout=max(timeout, 0))
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001044
Tim Petersc363a232013-09-08 18:44:40 -05001045 def _wait_for_tstate_lock(self, block=True, timeout=-1):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001046 # Issue #18808: wait for the thread state to be gone.
Tim Petersc363a232013-09-08 18:44:40 -05001047 # At the end of the thread's life, after all knowledge of the thread
1048 # is removed from C data structures, C code releases our _tstate_lock.
Martin Panter46f50722016-05-26 05:35:26 +00001049 # This method passes its arguments to _tstate_lock.acquire().
Tim Petersc363a232013-09-08 18:44:40 -05001050 # If the lock is acquired, the C code is done, and self._stop() is
1051 # called. That sets ._is_stopped to True, and ._tstate_lock to None.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001052 lock = self._tstate_lock
Tim Petersc363a232013-09-08 18:44:40 -05001053 if lock is None: # already determined that the C code is done
1054 assert self._is_stopped
1055 elif lock.acquire(block, timeout):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001056 lock.release()
Tim Petersc363a232013-09-08 18:44:40 -05001057 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001058
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001059 @property
1060 def name(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001061 """A string used for identification purposes only.
1062
1063 It has no semantics. Multiple threads may be given the same name. The
1064 initial name is set by the constructor.
1065
1066 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001067 assert self._initialized, "Thread.__init__() not called"
1068 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001069
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001070 @name.setter
1071 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +00001072 assert self._initialized, "Thread.__init__() not called"
1073 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001074
Benjamin Peterson773c17b2008-08-18 16:45:31 +00001075 @property
1076 def ident(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001077 """Thread identifier of this thread or None if it has not been started.
1078
Skip Montanaro56343312018-05-18 13:38:36 -05001079 This is a nonzero integer. See the get_ident() function. Thread
Georg Brandlc30b59f2013-10-13 10:43:59 +02001080 identifiers may be recycled when a thread exits and another thread is
1081 created. The identifier is available even after the thread has exited.
1082
1083 """
Georg Brandl0c77a822008-06-10 16:37:50 +00001084 assert self._initialized, "Thread.__init__() not called"
1085 return self._ident
1086
Jake Teslerb121f632019-05-22 08:43:17 -07001087 if _HAVE_THREAD_NATIVE_ID:
1088 @property
1089 def native_id(self):
1090 """Native integral thread ID of this thread, or None if it has not been started.
1091
1092 This is a non-negative integer. See the get_native_id() function.
1093 This represents the Thread ID as reported by the kernel.
1094
1095 """
1096 assert self._initialized, "Thread.__init__() not called"
1097 return self._native_id
1098
Benjamin Peterson672b8032008-06-11 19:14:14 +00001099 def is_alive(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001100 """Return whether the thread is alive.
1101
1102 This method returns True just before the run() method starts until just
1103 after the run() method terminates. The module function enumerate()
1104 returns a list of all alive threads.
1105
1106 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001107 assert self._initialized, "Thread.__init__() not called"
Tim Petersc363a232013-09-08 18:44:40 -05001108 if self._is_stopped or not self._started.is_set():
Antoine Pitrou7b476992013-09-07 23:38:37 +02001109 return False
Antoine Pitrou7b476992013-09-07 23:38:37 +02001110 self._wait_for_tstate_lock(False)
Tim Petersc363a232013-09-08 18:44:40 -05001111 return not self._is_stopped
Tim Petersb90f89a2001-01-15 03:26:36 +00001112
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001113 @property
1114 def daemon(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001115 """A boolean value indicating whether this thread is a daemon thread.
1116
1117 This must be set before start() is called, otherwise RuntimeError is
1118 raised. Its initial value is inherited from the creating thread; the
1119 main thread is not a daemon thread and therefore all threads created in
1120 the main thread default to daemon = False.
1121
mbarkhaubb110cc2019-06-22 14:51:06 +02001122 The entire Python program exits when only daemon threads are left.
Georg Brandlc30b59f2013-10-13 10:43:59 +02001123
1124 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001125 assert self._initialized, "Thread.__init__() not called"
1126 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001127
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001128 @daemon.setter
1129 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +00001130 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001131 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001132 if self._started.is_set():
Antoine Pitrou10959072014-03-17 18:22:41 +01001133 raise RuntimeError("cannot set daemon status of active thread")
Guido van Rossumd0648992007-08-20 19:25:41 +00001134 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001135
Benjamin Peterson6640d722008-08-18 18:16:46 +00001136 def isDaemon(self):
1137 return self.daemon
1138
1139 def setDaemon(self, daemonic):
1140 self.daemon = daemonic
1141
1142 def getName(self):
1143 return self.name
1144
1145 def setName(self, name):
1146 self.name = name
1147
Victor Stinnercd590a72019-05-28 00:39:52 +02001148
1149try:
1150 from _thread import (_excepthook as excepthook,
1151 _ExceptHookArgs as ExceptHookArgs)
1152except ImportError:
1153 # Simple Python implementation if _thread._excepthook() is not available
1154 from traceback import print_exception as _print_exception
1155 from collections import namedtuple
1156
1157 _ExceptHookArgs = namedtuple(
1158 'ExceptHookArgs',
1159 'exc_type exc_value exc_traceback thread')
1160
1161 def ExceptHookArgs(args):
1162 return _ExceptHookArgs(*args)
1163
1164 def excepthook(args, /):
1165 """
1166 Handle uncaught Thread.run() exception.
1167 """
1168 if args.exc_type == SystemExit:
1169 # silently ignore SystemExit
1170 return
1171
1172 if _sys is not None and _sys.stderr is not None:
1173 stderr = _sys.stderr
1174 elif args.thread is not None:
1175 stderr = args.thread._stderr
1176 if stderr is None:
1177 # do nothing if sys.stderr is None and sys.stderr was None
1178 # when the thread was created
1179 return
1180 else:
1181 # do nothing if sys.stderr is None and args.thread is None
1182 return
1183
1184 if args.thread is not None:
1185 name = args.thread.name
1186 else:
1187 name = get_ident()
1188 print(f"Exception in thread {name}:",
1189 file=stderr, flush=True)
1190 _print_exception(args.exc_type, args.exc_value, args.exc_traceback,
1191 file=stderr)
1192 stderr.flush()
1193
1194
1195def _make_invoke_excepthook():
1196 # Create a local namespace to ensure that variables remain alive
1197 # when _invoke_excepthook() is called, even if it is called late during
1198 # Python shutdown. It is mostly needed for daemon threads.
1199
1200 old_excepthook = excepthook
1201 old_sys_excepthook = _sys.excepthook
1202 if old_excepthook is None:
1203 raise RuntimeError("threading.excepthook is None")
1204 if old_sys_excepthook is None:
1205 raise RuntimeError("sys.excepthook is None")
1206
1207 sys_exc_info = _sys.exc_info
1208 local_print = print
1209 local_sys = _sys
1210
1211 def invoke_excepthook(thread):
1212 global excepthook
1213 try:
1214 hook = excepthook
1215 if hook is None:
1216 hook = old_excepthook
1217
1218 args = ExceptHookArgs([*sys_exc_info(), thread])
1219
1220 hook(args)
1221 except Exception as exc:
1222 exc.__suppress_context__ = True
1223 del exc
1224
1225 if local_sys is not None and local_sys.stderr is not None:
1226 stderr = local_sys.stderr
1227 else:
1228 stderr = thread._stderr
1229
1230 local_print("Exception in threading.excepthook:",
1231 file=stderr, flush=True)
1232
1233 if local_sys is not None and local_sys.excepthook is not None:
1234 sys_excepthook = local_sys.excepthook
1235 else:
1236 sys_excepthook = old_sys_excepthook
1237
1238 sys_excepthook(*sys_exc_info())
1239 finally:
1240 # Break reference cycle (exception stored in a variable)
1241 args = None
1242
1243 return invoke_excepthook
1244
1245
Martin v. Löwis44f86962001-09-05 13:44:54 +00001246# The timer class was contributed by Itamar Shtull-Trauring
1247
Éric Araujo0cdd4452011-07-28 00:28:28 +02001248class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001249 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +00001250
Georg Brandlc30b59f2013-10-13 10:43:59 +02001251 t = Timer(30.0, f, args=None, kwargs=None)
1252 t.start()
1253 t.cancel() # stop the timer's action if it's still waiting
1254
Martin v. Löwis44f86962001-09-05 13:44:54 +00001255 """
Tim Petersb64bec32001-09-18 02:26:39 +00001256
R David Murray19aeb432013-03-30 17:19:38 -04001257 def __init__(self, interval, function, args=None, kwargs=None):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001258 Thread.__init__(self)
1259 self.interval = interval
1260 self.function = function
R David Murray19aeb432013-03-30 17:19:38 -04001261 self.args = args if args is not None else []
1262 self.kwargs = kwargs if kwargs is not None else {}
Martin v. Löwis44f86962001-09-05 13:44:54 +00001263 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +00001264
Martin v. Löwis44f86962001-09-05 13:44:54 +00001265 def cancel(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001266 """Stop the timer if it hasn't finished yet."""
Martin v. Löwis44f86962001-09-05 13:44:54 +00001267 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +00001268
Martin v. Löwis44f86962001-09-05 13:44:54 +00001269 def run(self):
1270 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +00001271 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +00001272 self.function(*self.args, **self.kwargs)
1273 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001274
Antoine Pitrou1023dbb2017-10-02 16:42:15 +02001275
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001276# Special thread class to represent the main thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001277
1278class _MainThread(Thread):
1279
1280 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001281 Thread.__init__(self, name="MainThread", daemon=False)
Tim Petersc363a232013-09-08 18:44:40 -05001282 self._set_tstate_lock()
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001283 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001284 self._set_ident()
Jake Teslerb121f632019-05-22 08:43:17 -07001285 if _HAVE_THREAD_NATIVE_ID:
1286 self._set_native_id()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001287 with _active_limbo_lock:
1288 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001289
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001290
1291# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +00001292# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001293# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +00001294# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001295# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001296# They are marked as daemon threads so we won't wait for them
1297# when we exit (conform previous semantics).
1298
1299class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +00001300
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001301 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001302 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +00001303
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001304 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001305 self._set_ident()
Jake Teslerb121f632019-05-22 08:43:17 -07001306 if _HAVE_THREAD_NATIVE_ID:
1307 self._set_native_id()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001308 with _active_limbo_lock:
1309 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001310
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +02001311 def _stop(self):
1312 pass
1313
Xiang Zhangf3a9fab2017-02-27 11:01:30 +08001314 def is_alive(self):
1315 assert not self._is_stopped and self._started.is_set()
1316 return True
1317
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001318 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001319 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001320
1321
1322# Global API functions
1323
Benjamin Peterson672b8032008-06-11 19:14:14 +00001324def current_thread():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001325 """Return the current Thread object, corresponding to the caller's thread of control.
1326
1327 If the caller's thread of control was not created through the threading
1328 module, a dummy thread object with limited functionality is returned.
1329
1330 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001331 try:
Victor Stinner2a129742011-05-30 23:02:52 +02001332 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001333 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001334 return _DummyThread()
1335
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001336currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001337
Benjamin Peterson672b8032008-06-11 19:14:14 +00001338def active_count():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001339 """Return the number of Thread objects currently alive.
1340
1341 The returned count is equal to the length of the list returned by
1342 enumerate().
1343
1344 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001345 with _active_limbo_lock:
1346 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001347
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001348activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001349
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001350def _enumerate():
1351 # Same as enumerate(), but without the lock. Internal use only.
1352 return list(_active.values()) + list(_limbo.values())
1353
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001354def enumerate():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001355 """Return a list of all Thread objects currently alive.
1356
1357 The list includes daemonic threads, dummy thread objects created by
1358 current_thread(), and the main thread. It excludes terminated threads and
1359 threads that have not yet been started.
1360
1361 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001362 with _active_limbo_lock:
1363 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001364
Kyle Stanleyb61b8182020-03-27 15:31:22 -04001365
1366_threading_atexits = []
1367_SHUTTING_DOWN = False
1368
1369def _register_atexit(func, *arg, **kwargs):
1370 """CPython internal: register *func* to be called before joining threads.
1371
1372 The registered *func* is called with its arguments just before all
1373 non-daemon threads are joined in `_shutdown()`. It provides a similar
1374 purpose to `atexit.register()`, but its functions are called prior to
1375 threading shutdown instead of interpreter shutdown.
1376
1377 For similarity to atexit, the registered functions are called in reverse.
1378 """
1379 if _SHUTTING_DOWN:
1380 raise RuntimeError("can't register atexit after shutdown")
1381
1382 call = functools.partial(func, *arg, **kwargs)
1383 _threading_atexits.append(call)
1384
1385
Georg Brandl2067bfd2008-05-25 13:05:15 +00001386from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001387
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001388# Create the main thread object,
1389# and make it available for the interpreter
1390# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001391
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001392_main_thread = _MainThread()
1393
1394def _shutdown():
Victor Stinner468e5fe2019-06-13 01:30:17 +02001395 """
1396 Wait until the Python thread state of all non-daemon threads get deleted.
1397 """
Tim Petersc363a232013-09-08 18:44:40 -05001398 # Obscure: other threads may be waiting to join _main_thread. That's
1399 # dubious, but some code does it. We can't wait for C code to release
1400 # the main thread's tstate_lock - that won't happen until the interpreter
1401 # is nearly dead. So we release it here. Note that just calling _stop()
1402 # isn't enough: other threads may already be waiting on _tstate_lock.
Antoine Pitrouee84a602017-08-16 20:53:28 +02001403 if _main_thread._is_stopped:
1404 # _shutdown() was already called
1405 return
Victor Stinner468e5fe2019-06-13 01:30:17 +02001406
Kyle Stanleyb61b8182020-03-27 15:31:22 -04001407 global _SHUTTING_DOWN
1408 _SHUTTING_DOWN = True
Victor Stinner468e5fe2019-06-13 01:30:17 +02001409 # Main thread
Tim Petersb5e9ac92013-09-09 14:41:50 -05001410 tlock = _main_thread._tstate_lock
1411 # The main thread isn't finished yet, so its thread state lock can't have
1412 # been released.
1413 assert tlock is not None
1414 assert tlock.locked()
1415 tlock.release()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001416 _main_thread._stop()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001417
Kyle Stanleyb61b8182020-03-27 15:31:22 -04001418 # Call registered threading atexit functions before threads are joined.
1419 # Order is reversed, similar to atexit.
1420 for atexit_call in reversed(_threading_atexits):
1421 atexit_call()
1422
Victor Stinner468e5fe2019-06-13 01:30:17 +02001423 # Join all non-deamon threads
1424 while True:
1425 with _shutdown_locks_lock:
1426 locks = list(_shutdown_locks)
1427 _shutdown_locks.clear()
1428
1429 if not locks:
1430 break
1431
1432 for lock in locks:
1433 # mimick Thread.join()
1434 lock.acquire()
1435 lock.release()
1436
1437 # new threads can be spawned while we were waiting for the other
1438 # threads to complete
1439
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001440
1441def main_thread():
Andrew Svetlovb1dd5572013-09-04 10:33:11 +03001442 """Return the main thread object.
1443
1444 In normal conditions, the main thread is the thread from which the
1445 Python interpreter was started.
1446 """
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001447 return _main_thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001448
Jim Fultond15dc062004-07-14 19:11:50 +00001449# get thread-local implementation, either from the thread
1450# module, or from the python fallback
1451
1452try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001453 from _thread import _local as local
Brett Cannoncd171c82013-07-04 17:43:24 -04001454except ImportError:
Jim Fultond15dc062004-07-14 19:11:50 +00001455 from _threading_local import local
1456
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001457
Jesse Nollera8513972008-07-17 16:49:17 +00001458def _after_fork():
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001459 """
1460 Cleanup threading module state that should not exist after a fork.
1461 """
Jesse Nollera8513972008-07-17 16:49:17 +00001462 # Reset _active_limbo_lock, in case we forked while the lock was held
1463 # by another (non-forked) thread. http://bugs.python.org/issue874900
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001464 global _active_limbo_lock, _main_thread
Victor Stinner468e5fe2019-06-13 01:30:17 +02001465 global _shutdown_locks_lock, _shutdown_locks
Jesse Nollera8513972008-07-17 16:49:17 +00001466 _active_limbo_lock = _allocate_lock()
1467
1468 # fork() only copied the current thread; clear references to others.
1469 new_active = {}
Victor Stinnerd8ff44c2020-03-27 17:50:42 +01001470
1471 try:
1472 current = _active[get_ident()]
1473 except KeyError:
1474 # fork() was called in a thread which was not spawned
1475 # by threading.Thread. For example, a thread spawned
1476 # by thread.start_new_thread().
1477 current = _MainThread()
1478
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001479 _main_thread = current
Victor Stinner468e5fe2019-06-13 01:30:17 +02001480
1481 # reset _shutdown() locks: threads re-register their _tstate_lock below
1482 _shutdown_locks_lock = _allocate_lock()
1483 _shutdown_locks = set()
1484
Jesse Nollera8513972008-07-17 16:49:17 +00001485 with _active_limbo_lock:
Antoine Pitrou5da7e792013-09-08 13:19:06 +02001486 # Dangling thread instances must still have their locks reset,
1487 # because someone may join() them.
1488 threads = set(_enumerate())
1489 threads.update(_dangling)
1490 for thread in threads:
Charles-François Natalib055bf62011-12-18 18:45:16 +01001491 # Any lock/condition variable may be currently locked or in an
1492 # invalid state, so we reinitialize them.
Jesse Nollera8513972008-07-17 16:49:17 +00001493 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001494 # There is only one active thread. We reset the ident to
1495 # its new value since it can have changed.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001496 thread._reset_internal_locks(True)
Victor Stinner2a129742011-05-30 23:02:52 +02001497 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001498 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +00001499 new_active[ident] = thread
1500 else:
1501 # All the others are already stopped.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001502 thread._reset_internal_locks(False)
Charles-François Natalib055bf62011-12-18 18:45:16 +01001503 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +00001504
1505 _limbo.clear()
1506 _active.clear()
1507 _active.update(new_active)
1508 assert len(_active) == 1
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001509
1510
Gregory P. Smith163468a2017-05-29 10:03:41 -07001511if hasattr(_os, "register_at_fork"):
1512 _os.register_at_fork(after_in_child=_after_fork)