blob: 5424db3dabc44b0691d651e6e78ffd6832597270 [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
Victor Stinner066e5b12019-06-14 18:55:22 +020038_is_main_interpreter = _thread._is_main_interpreter
Jake Teslerb121f632019-05-22 08:43:17 -070039try:
40 get_native_id = _thread.get_native_id
41 _HAVE_THREAD_NATIVE_ID = True
42 __all__.append('get_native_id')
43except AttributeError:
44 _HAVE_THREAD_NATIVE_ID = False
Georg Brandl2067bfd2008-05-25 13:05:15 +000045ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000046try:
47 _CRLock = _thread.RLock
48except AttributeError:
49 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000050TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000051del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000052
Guido van Rossum7f5013a1998-04-09 22:01:42 +000053
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000054# Support for profile and trace hooks
55
56_profile_hook = None
57_trace_hook = None
58
59def setprofile(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020060 """Set a profile function for all threads started from the threading module.
61
62 The func will be passed to sys.setprofile() for each thread, before its
63 run() method is called.
64
65 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000066 global _profile_hook
67 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000068
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000069def settrace(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020070 """Set a trace function for all threads started from the threading module.
71
72 The func will be passed to sys.settrace() for each thread, before its run()
73 method is called.
74
75 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000076 global _trace_hook
77 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000078
79# Synchronization classes
80
81Lock = _allocate_lock
82
Victor Stinner135b6d82012-03-03 01:32:57 +010083def RLock(*args, **kwargs):
Georg Brandlc30b59f2013-10-13 10:43:59 +020084 """Factory function that returns a new reentrant lock.
85
86 A reentrant lock must be released by the thread that acquired it. Once a
87 thread has acquired a reentrant lock, the same thread may acquire it again
88 without blocking; the thread must release it once for each time it has
89 acquired it.
90
91 """
Victor Stinner135b6d82012-03-03 01:32:57 +010092 if _CRLock is None:
93 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000094 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000095
Victor Stinner135b6d82012-03-03 01:32:57 +010096class _RLock:
Georg Brandlc30b59f2013-10-13 10:43:59 +020097 """This class implements reentrant lock objects.
98
99 A reentrant lock must be released by the thread that acquired it. Once a
100 thread has acquired a reentrant lock, the same thread may acquire it
101 again without blocking; the thread must release it once for each time it
102 has acquired it.
103
104 """
Tim Petersb90f89a2001-01-15 03:26:36 +0000105
Victor Stinner135b6d82012-03-03 01:32:57 +0100106 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000107 self._block = _allocate_lock()
108 self._owner = None
109 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000110
111 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000112 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000113 try:
114 owner = _active[owner].name
115 except KeyError:
116 pass
Raymond Hettinger62f4dad2014-05-25 18:22:35 -0700117 return "<%s %s.%s object owner=%r count=%d at %s>" % (
118 "locked" if self._block.locked() else "unlocked",
119 self.__class__.__module__,
120 self.__class__.__qualname__,
121 owner,
122 self._count,
123 hex(id(self))
124 )
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000125
Victor Stinner87255be2020-04-07 23:11:49 +0200126 def _at_fork_reinit(self):
127 self._block._at_fork_reinit()
128 self._owner = None
129 self._count = 0
130
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000131 def acquire(self, blocking=True, timeout=-1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200132 """Acquire a lock, blocking or non-blocking.
133
134 When invoked without arguments: if this thread already owns the lock,
135 increment the recursion level by one, and return immediately. Otherwise,
136 if another thread owns the lock, block until the lock is unlocked. Once
137 the lock is unlocked (not owned by any thread), then grab ownership, set
138 the recursion level to one, and return. If more than one thread is
139 blocked waiting until the lock is unlocked, only one at a time will be
140 able to grab ownership of the lock. There is no return value in this
141 case.
142
143 When invoked with the blocking argument set to true, do the same thing
144 as when called without arguments, and return true.
145
146 When invoked with the blocking argument set to false, do not block. If a
147 call without an argument would block, return false immediately;
148 otherwise, do the same thing as when called without arguments, and
149 return true.
150
151 When invoked with the floating-point timeout argument set to a positive
152 value, block for at most the number of seconds specified by timeout
153 and as long as the lock cannot be acquired. Return true if the lock has
154 been acquired, false if the timeout has elapsed.
155
156 """
Victor Stinner2a129742011-05-30 23:02:52 +0200157 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +0000158 if self._owner == me:
Raymond Hettinger720da572013-03-10 15:13:35 -0700159 self._count += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000160 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000161 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000162 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000163 self._owner = me
164 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000165 return rc
166
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000167 __enter__ = acquire
168
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000169 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200170 """Release a lock, decrementing the recursion level.
171
172 If after the decrement it is zero, reset the lock to unlocked (not owned
173 by any thread), and if any other threads are blocked waiting for the
174 lock to become unlocked, allow exactly one of them to proceed. If after
175 the decrement the recursion level is still nonzero, the lock remains
176 locked and owned by the calling thread.
177
178 Only call this method when the calling thread owns the lock. A
179 RuntimeError is raised if this method is called when the lock is
180 unlocked.
181
182 There is no return value.
183
184 """
Victor Stinner2a129742011-05-30 23:02:52 +0200185 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000186 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000187 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000188 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000189 self._owner = None
190 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000191
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000192 def __exit__(self, t, v, tb):
193 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000194
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000195 # Internal methods used by condition variables
196
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000197 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000198 self._block.acquire()
199 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000200
201 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200202 if self._count == 0:
203 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000204 count = self._count
205 self._count = 0
206 owner = self._owner
207 self._owner = None
208 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000209 return (count, owner)
210
211 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200212 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000213
Antoine Pitrou434736a2009-11-10 18:46:01 +0000214_PyRLock = _RLock
215
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000216
Victor Stinner135b6d82012-03-03 01:32:57 +0100217class Condition:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200218 """Class that implements a condition variable.
219
220 A condition variable allows one or more threads to wait until they are
221 notified by another thread.
222
223 If the lock argument is given and not None, it must be a Lock or RLock
224 object, and it is used as the underlying lock. Otherwise, a new RLock object
225 is created and used as the underlying lock.
226
227 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000228
Victor Stinner135b6d82012-03-03 01:32:57 +0100229 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000230 if lock is None:
231 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000232 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000233 # Export the lock's acquire() and release() methods
234 self.acquire = lock.acquire
235 self.release = lock.release
236 # If the lock defines _release_save() and/or _acquire_restore(),
237 # these override the default implementations (which just call
238 # release() and acquire() on the lock). Ditto for _is_owned().
239 try:
240 self._release_save = lock._release_save
241 except AttributeError:
242 pass
243 try:
244 self._acquire_restore = lock._acquire_restore
245 except AttributeError:
246 pass
247 try:
248 self._is_owned = lock._is_owned
249 except AttributeError:
250 pass
Raymond Hettingerec4b1742013-03-10 17:57:28 -0700251 self._waiters = _deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000252
Victor Stinner87255be2020-04-07 23:11:49 +0200253 def _at_fork_reinit(self):
254 self._lock._at_fork_reinit()
255 self._waiters.clear()
256
Thomas Wouters477c8d52006-05-27 19:21:47 +0000257 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000258 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000259
Thomas Wouters477c8d52006-05-27 19:21:47 +0000260 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000261 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000262
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000263 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000264 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000265
266 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000267 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000268
269 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000270 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000271
272 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000273 # Return True if lock is owned by current_thread.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300274 # This method is called only if _lock doesn't have _is_owned().
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +0300275 if self._lock.acquire(False):
Guido van Rossumd0648992007-08-20 19:25:41 +0000276 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000277 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000278 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000279 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000280
281 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200282 """Wait until notified or until a timeout occurs.
283
284 If the calling thread has not acquired the lock when this method is
285 called, a RuntimeError is raised.
286
287 This method releases the underlying lock, and then blocks until it is
288 awakened by a notify() or notify_all() call for the same condition
289 variable in another thread, or until the optional timeout occurs. Once
290 awakened or timed out, it re-acquires the lock and returns.
291
292 When the timeout argument is present and not None, it should be a
293 floating point number specifying a timeout for the operation in seconds
294 (or fractions thereof).
295
296 When the underlying lock is an RLock, it is not released using its
297 release() method, since this may not actually unlock the lock when it
298 was acquired multiple times recursively. Instead, an internal interface
299 of the RLock class is used, which really unlocks it even when it has
300 been recursively acquired several times. Another internal interface is
301 then used to restore the recursion level when the lock is reacquired.
302
303 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000304 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000305 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000306 waiter = _allocate_lock()
307 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000308 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000309 saved_state = self._release_save()
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200310 gotit = False
Tim Petersc951bf92001-04-02 20:15:57 +0000311 try: # restore state no matter what (e.g., KeyboardInterrupt)
312 if timeout is None:
313 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000314 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000315 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000316 if timeout > 0:
317 gotit = waiter.acquire(True, timeout)
318 else:
319 gotit = waiter.acquire(False)
Georg Brandlb9a43912010-10-28 09:03:20 +0000320 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000321 finally:
322 self._acquire_restore(saved_state)
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200323 if not gotit:
324 try:
325 self._waiters.remove(waiter)
326 except ValueError:
327 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000328
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000329 def wait_for(self, predicate, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200330 """Wait until a condition evaluates to True.
331
332 predicate should be a callable which result will be interpreted as a
333 boolean value. A timeout may be provided giving the maximum time to
334 wait.
335
336 """
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000337 endtime = None
338 waittime = timeout
339 result = predicate()
340 while not result:
341 if waittime is not None:
342 if endtime is None:
343 endtime = _time() + waittime
344 else:
345 waittime = endtime - _time()
346 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000347 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000348 self.wait(waittime)
349 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000350 return result
351
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000352 def notify(self, n=1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200353 """Wake up one or more threads waiting on this condition, if any.
354
355 If the calling thread has not acquired the lock when this method is
356 called, a RuntimeError is raised.
357
358 This method wakes up at most n of the threads waiting for the condition
359 variable; it is a no-op if no threads are waiting.
360
361 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000362 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000363 raise RuntimeError("cannot notify on un-acquired lock")
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700364 all_waiters = self._waiters
365 waiters_to_notify = _deque(_islice(all_waiters, n))
366 if not waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000367 return
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700368 for waiter in waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000369 waiter.release()
370 try:
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700371 all_waiters.remove(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000372 except ValueError:
373 pass
374
Benjamin Peterson672b8032008-06-11 19:14:14 +0000375 def notify_all(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200376 """Wake up all threads waiting on this condition.
377
378 If the calling thread has not acquired the lock when this method
379 is called, a RuntimeError is raised.
380
381 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000382 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000383
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000384 notifyAll = notify_all
385
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000386
Victor Stinner135b6d82012-03-03 01:32:57 +0100387class Semaphore:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200388 """This class implements semaphore objects.
389
390 Semaphores manage a counter representing the number of release() calls minus
391 the number of acquire() calls, plus an initial value. The acquire() method
392 blocks if necessary until it can return without making the counter
393 negative. If not given, value defaults to 1.
394
395 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000396
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000397 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000398
Victor Stinner135b6d82012-03-03 01:32:57 +0100399 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000400 if value < 0:
401 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000402 self._cond = Condition(Lock())
403 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000404
Antoine Pitrou0454af92010-04-17 23:51:58 +0000405 def acquire(self, blocking=True, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200406 """Acquire a semaphore, decrementing the internal counter by one.
407
408 When invoked without arguments: if the internal counter is larger than
409 zero on entry, decrement it by one and return immediately. If it is zero
410 on entry, block, waiting until some other thread has called release() to
411 make it larger than zero. This is done with proper interlocking so that
412 if multiple acquire() calls are blocked, release() will wake exactly one
413 of them up. The implementation may pick one at random, so the order in
414 which blocked threads are awakened should not be relied on. There is no
415 return value in this case.
416
417 When invoked with blocking set to true, do the same thing as when called
418 without arguments, and return true.
419
420 When invoked with blocking set to false, do not block. If a call without
421 an argument would block, return false immediately; otherwise, do the
422 same thing as when called without arguments, and return true.
423
424 When invoked with a timeout other than None, it will block for at
425 most timeout seconds. If acquire does not complete successfully in
426 that interval, return false. Return true otherwise.
427
428 """
Antoine Pitrou0454af92010-04-17 23:51:58 +0000429 if not blocking and timeout is not None:
430 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000431 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000432 endtime = None
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300433 with self._cond:
434 while self._value == 0:
435 if not blocking:
436 break
437 if timeout is not None:
438 if endtime is None:
439 endtime = _time() + timeout
440 else:
441 timeout = endtime - _time()
442 if timeout <= 0:
443 break
444 self._cond.wait(timeout)
445 else:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300446 self._value -= 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300447 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000448 return rc
449
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000450 __enter__ = acquire
451
Raymond Hettinger35f63012019-08-29 01:45:19 -0700452 def release(self, n=1):
453 """Release a semaphore, incrementing the internal counter by one or more.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200454
455 When the counter is zero on entry and another thread is waiting for it
456 to become larger than zero again, wake up that thread.
457
458 """
Raymond Hettinger35f63012019-08-29 01:45:19 -0700459 if n < 1:
460 raise ValueError('n must be one or more')
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300461 with self._cond:
Raymond Hettinger35f63012019-08-29 01:45:19 -0700462 self._value += n
463 for i in range(n):
464 self._cond.notify()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000465
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000466 def __exit__(self, t, v, tb):
467 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000468
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000469
Éric Araujo0cdd4452011-07-28 00:28:28 +0200470class BoundedSemaphore(Semaphore):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200471 """Implements a bounded semaphore.
472
473 A bounded semaphore checks to make sure its current value doesn't exceed its
474 initial value. If it does, ValueError is raised. In most situations
475 semaphores are used to guard resources with limited capacity.
476
477 If the semaphore is released too many times it's a sign of a bug. If not
478 given, value defaults to 1.
479
480 Like regular semaphores, bounded semaphores manage a counter representing
481 the number of release() calls minus the number of acquire() calls, plus an
482 initial value. The acquire() method blocks if necessary until it can return
483 without making the counter negative. If not given, value defaults to 1.
484
485 """
486
Victor Stinner135b6d82012-03-03 01:32:57 +0100487 def __init__(self, value=1):
488 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000489 self._initial_value = value
490
Raymond Hettinger35f63012019-08-29 01:45:19 -0700491 def release(self, n=1):
492 """Release a semaphore, incrementing the internal counter by one or more.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200493
494 When the counter is zero on entry and another thread is waiting for it
495 to become larger than zero again, wake up that thread.
496
497 If the number of releases exceeds the number of acquires,
498 raise a ValueError.
499
500 """
Raymond Hettinger35f63012019-08-29 01:45:19 -0700501 if n < 1:
502 raise ValueError('n must be one or more')
Tim Peters7634e1c2013-10-08 20:55:51 -0500503 with self._cond:
Raymond Hettinger35f63012019-08-29 01:45:19 -0700504 if self._value + n > self._initial_value:
Tim Peters7634e1c2013-10-08 20:55:51 -0500505 raise ValueError("Semaphore released too many times")
Raymond Hettinger35f63012019-08-29 01:45:19 -0700506 self._value += n
507 for i in range(n):
508 self._cond.notify()
Skip Montanaroe428bb72001-08-20 20:27:58 +0000509
510
Victor Stinner135b6d82012-03-03 01:32:57 +0100511class Event:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200512 """Class implementing event objects.
513
514 Events manage a flag that can be set to true with the set() method and reset
515 to false with the clear() method. The wait() method blocks until the flag is
516 true. The flag is initially false.
517
518 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000519
520 # After Tim Peters' event class (without is_posted())
521
Victor Stinner135b6d82012-03-03 01:32:57 +0100522 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000523 self._cond = Condition(Lock())
524 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000525
Victor Stinner87255be2020-04-07 23:11:49 +0200526 def _at_fork_reinit(self):
527 # Private method called by Thread._reset_internal_locks()
528 self._cond._at_fork_reinit()
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000529
Benjamin Peterson672b8032008-06-11 19:14:14 +0000530 def is_set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200531 """Return true if and only if the internal flag is true."""
Guido van Rossumd0648992007-08-20 19:25:41 +0000532 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000533
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000534 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000535
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000536 def set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200537 """Set the internal flag to true.
538
539 All threads waiting for it to become true are awakened. Threads
540 that call wait() once the flag is true will not block at all.
541
542 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700543 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000544 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000545 self._cond.notify_all()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000546
547 def clear(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200548 """Reset the internal flag to false.
549
550 Subsequently, threads calling wait() will block until set() is called to
551 set the internal flag to true again.
552
553 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700554 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000555 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000556
557 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200558 """Block until the internal flag is true.
559
560 If the internal flag is true on entry, return immediately. Otherwise,
561 block until another thread calls set() to set the flag to true, or until
562 the optional timeout occurs.
563
564 When the timeout argument is present and not None, it should be a
565 floating point number specifying a timeout for the operation in seconds
566 (or fractions thereof).
567
568 This method returns the internal flag on exit, so it will always return
569 True except if a timeout is given and the operation times out.
570
571 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700572 with self._cond:
Charles-François Natalided03482012-01-07 18:24:56 +0100573 signaled = self._flag
574 if not signaled:
575 signaled = self._cond.wait(timeout)
576 return signaled
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000577
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000578
579# A barrier class. Inspired in part by the pthread_barrier_* api and
580# the CyclicBarrier class from Java. See
581# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
582# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
583# CyclicBarrier.html
584# for information.
585# We maintain two main states, 'filling' and 'draining' enabling the barrier
586# to be cyclic. Threads are not allowed into it until it has fully drained
587# since the previous cycle. In addition, a 'resetting' state exists which is
588# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300589# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100590class Barrier:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200591 """Implements a Barrier.
592
593 Useful for synchronizing a fixed number of threads at known synchronization
Carl Bordum Hansen62fa51f2019-03-09 18:38:05 +0100594 points. Threads block on 'wait()' and are simultaneously awoken once they
595 have all made that call.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200596
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000597 """
Georg Brandlc30b59f2013-10-13 10:43:59 +0200598
Victor Stinner135b6d82012-03-03 01:32:57 +0100599 def __init__(self, parties, action=None, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200600 """Create a barrier, initialised to 'parties' threads.
601
602 'action' is a callable which, when supplied, will be called by one of
603 the threads after they have all entered the barrier and just prior to
Carl Bordum Hansen62fa51f2019-03-09 18:38:05 +0100604 releasing them all. If a 'timeout' is provided, it is used as the
Georg Brandlc30b59f2013-10-13 10:43:59 +0200605 default for all subsequent 'wait()' calls.
606
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000607 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000608 self._cond = Condition(Lock())
609 self._action = action
610 self._timeout = timeout
611 self._parties = parties
612 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
613 self._count = 0
614
615 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200616 """Wait for the barrier.
617
618 When the specified number of threads have started waiting, they are all
619 simultaneously awoken. If an 'action' was provided for the barrier, one
620 of the threads will have executed that callback prior to returning.
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000621 Returns an individual index number from 0 to 'parties-1'.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200622
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000623 """
624 if timeout is None:
625 timeout = self._timeout
626 with self._cond:
627 self._enter() # Block while the barrier drains.
628 index = self._count
629 self._count += 1
630 try:
631 if index + 1 == self._parties:
632 # We release the barrier
633 self._release()
634 else:
635 # We wait until someone releases us
636 self._wait(timeout)
637 return index
638 finally:
639 self._count -= 1
640 # Wake up any threads waiting for barrier to drain.
641 self._exit()
642
643 # Block until the barrier is ready for us, or raise an exception
644 # if it is broken.
645 def _enter(self):
646 while self._state in (-1, 1):
647 # It is draining or resetting, wait until done
648 self._cond.wait()
649 #see if the barrier is in a broken state
650 if self._state < 0:
651 raise BrokenBarrierError
652 assert self._state == 0
653
654 # Optionally run the 'action' and release the threads waiting
655 # in the barrier.
656 def _release(self):
657 try:
658 if self._action:
659 self._action()
660 # enter draining state
661 self._state = 1
662 self._cond.notify_all()
663 except:
664 #an exception during the _action handler. Break and reraise
665 self._break()
666 raise
667
Martin Panter69332c12016-08-04 13:07:31 +0000668 # Wait in the barrier until we are released. Raise an exception
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000669 # if the barrier is reset or broken.
670 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000671 if not self._cond.wait_for(lambda : self._state != 0, timeout):
672 #timed out. Break the barrier
673 self._break()
674 raise BrokenBarrierError
675 if self._state < 0:
676 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000677 assert self._state == 1
678
679 # If we are the last thread to exit the barrier, signal any threads
680 # waiting for the barrier to drain.
681 def _exit(self):
682 if self._count == 0:
683 if self._state in (-1, 1):
684 #resetting or draining
685 self._state = 0
686 self._cond.notify_all()
687
688 def reset(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200689 """Reset the barrier to the initial state.
690
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000691 Any threads currently waiting will get the BrokenBarrier exception
692 raised.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200693
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000694 """
695 with self._cond:
696 if self._count > 0:
697 if self._state == 0:
698 #reset the barrier, waking up threads
699 self._state = -1
700 elif self._state == -2:
701 #was broken, set it to reset state
702 #which clears when the last thread exits
703 self._state = -1
704 else:
705 self._state = 0
706 self._cond.notify_all()
707
708 def abort(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200709 """Place the barrier into a 'broken' state.
710
711 Useful in case of error. Any currently waiting threads and threads
712 attempting to 'wait()' will have BrokenBarrierError raised.
713
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000714 """
715 with self._cond:
716 self._break()
717
718 def _break(self):
719 # An internal error was detected. The barrier is set to
720 # a broken state all parties awakened.
721 self._state = -2
722 self._cond.notify_all()
723
724 @property
725 def parties(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200726 """Return the number of threads required to trip the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000727 return self._parties
728
729 @property
730 def n_waiting(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200731 """Return the number of threads currently waiting at the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000732 # We don't need synchronization here since this is an ephemeral result
733 # anyway. It returns the correct value in the steady state.
734 if self._state == 0:
735 return self._count
736 return 0
737
738 @property
739 def broken(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200740 """Return True if the barrier is in a broken state."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000741 return self._state == -2
742
Georg Brandlc30b59f2013-10-13 10:43:59 +0200743# exception raised by the Barrier class
744class BrokenBarrierError(RuntimeError):
745 pass
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000746
747
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000748# Helper to generate new thread names
R David Murrayb186f1df2014-10-04 17:43:54 -0400749_counter = _count().__next__
750_counter() # Consume 0 so first non-main thread has id 1.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000751def _newname(template="Thread-%d"):
R David Murrayb186f1df2014-10-04 17:43:54 -0400752 return template % _counter()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000753
754# Active thread administration
755_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000756_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000757_limbo = {}
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200758_dangling = WeakSet()
Victor Stinner468e5fe2019-06-13 01:30:17 +0200759# Set of Thread._tstate_lock locks of non-daemon threads used by _shutdown()
760# to wait until all Python thread states get deleted:
761# see Thread._set_tstate_lock().
762_shutdown_locks_lock = _allocate_lock()
763_shutdown_locks = set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000764
765# Main class for threads
766
Victor Stinner135b6d82012-03-03 01:32:57 +0100767class Thread:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200768 """A class that represents a thread of control.
769
770 This class can be safely subclassed in a limited fashion. There are two ways
771 to specify the activity: by passing a callable object to the constructor, or
772 by overriding the run() method in a subclass.
773
774 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000775
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300776 _initialized = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000777
778 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100779 args=(), kwargs=None, *, daemon=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200780 """This constructor should always be called with keyword arguments. Arguments are:
781
782 *group* should be None; reserved for future extension when a ThreadGroup
783 class is implemented.
784
785 *target* is the callable object to be invoked by the run()
786 method. Defaults to None, meaning nothing is called.
787
788 *name* is the thread name. By default, a unique name is constructed of
789 the form "Thread-N" where N is a small decimal number.
790
791 *args* is the argument tuple for the target invocation. Defaults to ().
792
793 *kwargs* is a dictionary of keyword arguments for the target
794 invocation. Defaults to {}.
795
796 If a subclass overrides the constructor, it must make sure to invoke
797 the base class constructor (Thread.__init__()) before doing anything
798 else to the thread.
799
800 """
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000801 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000802 if kwargs is None:
803 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000804 self._target = target
805 self._name = str(name or _newname())
806 self._args = args
807 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000808 if daemon is not None:
809 self._daemonic = daemon
810 else:
811 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000812 self._ident = None
Jake Teslerb121f632019-05-22 08:43:17 -0700813 if _HAVE_THREAD_NATIVE_ID:
814 self._native_id = None
Antoine Pitrou7b476992013-09-07 23:38:37 +0200815 self._tstate_lock = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000816 self._started = Event()
Tim Petersc363a232013-09-08 18:44:40 -0500817 self._is_stopped = False
Guido van Rossumd0648992007-08-20 19:25:41 +0000818 self._initialized = True
Victor Stinnercd590a72019-05-28 00:39:52 +0200819 # Copy of sys.stderr used by self._invoke_excepthook()
Guido van Rossumd0648992007-08-20 19:25:41 +0000820 self._stderr = _sys.stderr
Victor Stinnercd590a72019-05-28 00:39:52 +0200821 self._invoke_excepthook = _make_invoke_excepthook()
Antoine Pitrou5da7e792013-09-08 13:19:06 +0200822 # For debugging and _after_fork()
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200823 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000824
Antoine Pitrou7b476992013-09-07 23:38:37 +0200825 def _reset_internal_locks(self, is_alive):
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000826 # private! Called by _after_fork() to reset our internal locks as
827 # they may be in an invalid state leading to a deadlock or crash.
Victor Stinner87255be2020-04-07 23:11:49 +0200828 self._started._at_fork_reinit()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200829 if is_alive:
Victor Stinner87255be2020-04-07 23:11:49 +0200830 self._tstate_lock._at_fork_reinit()
831 self._tstate_lock.acquire()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200832 else:
833 # The thread isn't alive after fork: it doesn't have a tstate
834 # anymore.
Tim Petersb5e9ac92013-09-09 14:41:50 -0500835 self._is_stopped = True
Antoine Pitrou7b476992013-09-07 23:38:37 +0200836 self._tstate_lock = None
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000837
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000838 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000839 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000840 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000841 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000842 status = "started"
Tim Peters72460fa2013-09-09 18:48:24 -0500843 self.is_alive() # easy way to get ._is_stopped set when appropriate
Tim Petersc363a232013-09-08 18:44:40 -0500844 if self._is_stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000845 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000846 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000847 status += " daemon"
848 if self._ident is not None:
849 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000850 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000851
852 def start(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200853 """Start the thread's activity.
854
855 It must be called at most once per thread object. It arranges for the
856 object's run() method to be invoked in a separate thread of control.
857
858 This method will raise a RuntimeError if called more than once on the
859 same thread object.
860
861 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000862 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000863 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000864
Benjamin Peterson672b8032008-06-11 19:14:14 +0000865 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000866 raise RuntimeError("threads can only be started once")
Victor Stinner066e5b12019-06-14 18:55:22 +0200867
868 if self.daemon and not _is_main_interpreter():
869 raise RuntimeError("daemon thread are not supported "
870 "in subinterpreters")
871
Benjamin Petersond23f8222009-04-05 19:13:16 +0000872 with _active_limbo_lock:
873 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000874 try:
875 _start_new_thread(self._bootstrap, ())
876 except Exception:
877 with _active_limbo_lock:
878 del _limbo[self]
879 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000880 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000881
882 def run(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200883 """Method representing the thread's activity.
884
885 You may override this method in a subclass. The standard run() method
886 invokes the callable object passed to the object's constructor as the
887 target argument, if any, with sequential and keyword arguments taken
888 from the args and kwargs arguments, respectively.
889
890 """
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000891 try:
892 if self._target:
893 self._target(*self._args, **self._kwargs)
894 finally:
895 # Avoid a refcycle if the thread is running a function with
896 # an argument that has a member that points to the thread.
897 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000898
Guido van Rossumd0648992007-08-20 19:25:41 +0000899 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000900 # Wrapper around the real bootstrap code that ignores
901 # exceptions during interpreter cleanup. Those typically
902 # happen when a daemon thread wakes up at an unfortunate
903 # moment, finds the world around it destroyed, and raises some
904 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000905 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000906 # don't help anybody, and they confuse users, so we suppress
907 # them. We suppress them only when it appears that the world
908 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000909 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000910 # reported. Also, we only suppress them for daemonic threads;
911 # if a non-daemonic encounters this, something else is wrong.
912 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000913 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000914 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000915 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000916 return
917 raise
918
Benjamin Petersond23f8222009-04-05 19:13:16 +0000919 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200920 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000921
Jake Teslerb121f632019-05-22 08:43:17 -0700922 if _HAVE_THREAD_NATIVE_ID:
923 def _set_native_id(self):
924 self._native_id = get_native_id()
925
Antoine Pitrou7b476992013-09-07 23:38:37 +0200926 def _set_tstate_lock(self):
927 """
928 Set a lock object which will be released by the interpreter when
929 the underlying thread state (see pystate.h) gets deleted.
930 """
931 self._tstate_lock = _set_sentinel()
932 self._tstate_lock.acquire()
933
Victor Stinner468e5fe2019-06-13 01:30:17 +0200934 if not self.daemon:
935 with _shutdown_locks_lock:
936 _shutdown_locks.add(self._tstate_lock)
937
Guido van Rossumd0648992007-08-20 19:25:41 +0000938 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000939 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000940 self._set_ident()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200941 self._set_tstate_lock()
Jake Teslerb121f632019-05-22 08:43:17 -0700942 if _HAVE_THREAD_NATIVE_ID:
943 self._set_native_id()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000944 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000945 with _active_limbo_lock:
946 _active[self._ident] = self
947 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000948
949 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000950 _sys.settrace(_trace_hook)
951 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000952 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000953
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000954 try:
955 self.run()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000956 except:
Victor Stinnercd590a72019-05-28 00:39:52 +0200957 self._invoke_excepthook(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000958 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000959 with _active_limbo_lock:
Christian Heimes1af737c2008-01-23 08:24:23 +0000960 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000961 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000962 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200963 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000964 except:
965 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000966
Guido van Rossumd0648992007-08-20 19:25:41 +0000967 def _stop(self):
Tim Petersb5e9ac92013-09-09 14:41:50 -0500968 # After calling ._stop(), .is_alive() returns False and .join() returns
969 # immediately. ._tstate_lock must be released before calling ._stop().
970 #
971 # Normal case: C code at the end of the thread's life
972 # (release_sentinel in _threadmodule.c) releases ._tstate_lock, and
973 # that's detected by our ._wait_for_tstate_lock(), called by .join()
974 # and .is_alive(). Any number of threads _may_ call ._stop()
975 # simultaneously (for example, if multiple threads are blocked in
976 # .join() calls), and they're not serialized. That's harmless -
977 # they'll just make redundant rebindings of ._is_stopped and
978 # ._tstate_lock. Obscure: we rebind ._tstate_lock last so that the
979 # "assert self._is_stopped" in ._wait_for_tstate_lock() always works
980 # (the assert is executed only if ._tstate_lock is None).
981 #
982 # Special case: _main_thread releases ._tstate_lock via this
983 # module's _shutdown() function.
984 lock = self._tstate_lock
985 if lock is not None:
986 assert not lock.locked()
Tim Peters78755232013-09-09 13:47:16 -0500987 self._is_stopped = True
988 self._tstate_lock = None
Victor Stinner468e5fe2019-06-13 01:30:17 +0200989 if not self.daemon:
990 with _shutdown_locks_lock:
Victor Stinner6f75c872019-06-13 12:06:24 +0200991 _shutdown_locks.discard(lock)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000992
Guido van Rossumd0648992007-08-20 19:25:41 +0000993 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000994 "Remove current thread from the dict of currently running threads."
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200995 with _active_limbo_lock:
996 del _active[get_ident()]
997 # There must not be any python code between the previous line
998 # and after the lock is released. Otherwise a tracing function
999 # could try to acquire the lock again in the same thread, (in
1000 # current_thread()), and would block.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001001
1002 def join(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001003 """Wait until the thread terminates.
1004
1005 This blocks the calling thread until the thread whose join() method is
1006 called terminates -- either normally or through an unhandled exception
1007 or until the optional timeout occurs.
1008
1009 When the timeout argument is present and not None, it should be a
1010 floating point number specifying a timeout for the operation in seconds
1011 (or fractions thereof). As join() always returns None, you must call
Dong-hee Na36d9e9a2019-01-18 18:50:47 +09001012 is_alive() after join() to decide whether a timeout happened -- if the
Georg Brandlc30b59f2013-10-13 10:43:59 +02001013 thread is still alive, the join() call timed out.
1014
1015 When the timeout argument is not present or None, the operation will
1016 block until the thread terminates.
1017
1018 A thread can be join()ed many times.
1019
1020 join() raises a RuntimeError if an attempt is made to join the current
1021 thread as that would cause a deadlock. It is also an error to join() a
1022 thread before it has been started and attempts to do so raises the same
1023 exception.
1024
1025 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001026 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001027 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001028 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001029 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001030 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001031 raise RuntimeError("cannot join current thread")
Tim Peterse5bb0bf2013-10-25 20:46:51 -05001032
Tim Petersc363a232013-09-08 18:44:40 -05001033 if timeout is None:
1034 self._wait_for_tstate_lock()
Tim Peters7bad39f2013-10-25 22:33:52 -05001035 else:
1036 # the behavior of a negative timeout isn't documented, but
Tim Petersa577f1e2013-10-26 11:56:16 -05001037 # historically .join(timeout=x) for x<0 has acted as if timeout=0
Tim Peters7bad39f2013-10-25 22:33:52 -05001038 self._wait_for_tstate_lock(timeout=max(timeout, 0))
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001039
Tim Petersc363a232013-09-08 18:44:40 -05001040 def _wait_for_tstate_lock(self, block=True, timeout=-1):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001041 # Issue #18808: wait for the thread state to be gone.
Tim Petersc363a232013-09-08 18:44:40 -05001042 # At the end of the thread's life, after all knowledge of the thread
1043 # is removed from C data structures, C code releases our _tstate_lock.
Martin Panter46f50722016-05-26 05:35:26 +00001044 # This method passes its arguments to _tstate_lock.acquire().
Tim Petersc363a232013-09-08 18:44:40 -05001045 # If the lock is acquired, the C code is done, and self._stop() is
1046 # called. That sets ._is_stopped to True, and ._tstate_lock to None.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001047 lock = self._tstate_lock
Tim Petersc363a232013-09-08 18:44:40 -05001048 if lock is None: # already determined that the C code is done
1049 assert self._is_stopped
1050 elif lock.acquire(block, timeout):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001051 lock.release()
Tim Petersc363a232013-09-08 18:44:40 -05001052 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001053
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001054 @property
1055 def name(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001056 """A string used for identification purposes only.
1057
1058 It has no semantics. Multiple threads may be given the same name. The
1059 initial name is set by the constructor.
1060
1061 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001062 assert self._initialized, "Thread.__init__() not called"
1063 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001064
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001065 @name.setter
1066 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +00001067 assert self._initialized, "Thread.__init__() not called"
1068 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001069
Benjamin Peterson773c17b2008-08-18 16:45:31 +00001070 @property
1071 def ident(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001072 """Thread identifier of this thread or None if it has not been started.
1073
Skip Montanaro56343312018-05-18 13:38:36 -05001074 This is a nonzero integer. See the get_ident() function. Thread
Georg Brandlc30b59f2013-10-13 10:43:59 +02001075 identifiers may be recycled when a thread exits and another thread is
1076 created. The identifier is available even after the thread has exited.
1077
1078 """
Georg Brandl0c77a822008-06-10 16:37:50 +00001079 assert self._initialized, "Thread.__init__() not called"
1080 return self._ident
1081
Jake Teslerb121f632019-05-22 08:43:17 -07001082 if _HAVE_THREAD_NATIVE_ID:
1083 @property
1084 def native_id(self):
1085 """Native integral thread ID of this thread, or None if it has not been started.
1086
1087 This is a non-negative integer. See the get_native_id() function.
1088 This represents the Thread ID as reported by the kernel.
1089
1090 """
1091 assert self._initialized, "Thread.__init__() not called"
1092 return self._native_id
1093
Benjamin Peterson672b8032008-06-11 19:14:14 +00001094 def is_alive(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001095 """Return whether the thread is alive.
1096
1097 This method returns True just before the run() method starts until just
1098 after the run() method terminates. The module function enumerate()
1099 returns a list of all alive threads.
1100
1101 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001102 assert self._initialized, "Thread.__init__() not called"
Tim Petersc363a232013-09-08 18:44:40 -05001103 if self._is_stopped or not self._started.is_set():
Antoine Pitrou7b476992013-09-07 23:38:37 +02001104 return False
Antoine Pitrou7b476992013-09-07 23:38:37 +02001105 self._wait_for_tstate_lock(False)
Tim Petersc363a232013-09-08 18:44:40 -05001106 return not self._is_stopped
Tim Petersb90f89a2001-01-15 03:26:36 +00001107
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001108 @property
1109 def daemon(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001110 """A boolean value indicating whether this thread is a daemon thread.
1111
1112 This must be set before start() is called, otherwise RuntimeError is
1113 raised. Its initial value is inherited from the creating thread; the
1114 main thread is not a daemon thread and therefore all threads created in
1115 the main thread default to daemon = False.
1116
mbarkhaubb110cc2019-06-22 14:51:06 +02001117 The entire Python program exits when only daemon threads are left.
Georg Brandlc30b59f2013-10-13 10:43:59 +02001118
1119 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001120 assert self._initialized, "Thread.__init__() not called"
1121 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001122
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001123 @daemon.setter
1124 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +00001125 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001126 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001127 if self._started.is_set():
Antoine Pitrou10959072014-03-17 18:22:41 +01001128 raise RuntimeError("cannot set daemon status of active thread")
Guido van Rossumd0648992007-08-20 19:25:41 +00001129 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001130
Benjamin Peterson6640d722008-08-18 18:16:46 +00001131 def isDaemon(self):
1132 return self.daemon
1133
1134 def setDaemon(self, daemonic):
1135 self.daemon = daemonic
1136
1137 def getName(self):
1138 return self.name
1139
1140 def setName(self, name):
1141 self.name = name
1142
Victor Stinnercd590a72019-05-28 00:39:52 +02001143
1144try:
1145 from _thread import (_excepthook as excepthook,
1146 _ExceptHookArgs as ExceptHookArgs)
1147except ImportError:
1148 # Simple Python implementation if _thread._excepthook() is not available
1149 from traceback import print_exception as _print_exception
1150 from collections import namedtuple
1151
1152 _ExceptHookArgs = namedtuple(
1153 'ExceptHookArgs',
1154 'exc_type exc_value exc_traceback thread')
1155
1156 def ExceptHookArgs(args):
1157 return _ExceptHookArgs(*args)
1158
1159 def excepthook(args, /):
1160 """
1161 Handle uncaught Thread.run() exception.
1162 """
1163 if args.exc_type == SystemExit:
1164 # silently ignore SystemExit
1165 return
1166
1167 if _sys is not None and _sys.stderr is not None:
1168 stderr = _sys.stderr
1169 elif args.thread is not None:
1170 stderr = args.thread._stderr
1171 if stderr is None:
1172 # do nothing if sys.stderr is None and sys.stderr was None
1173 # when the thread was created
1174 return
1175 else:
1176 # do nothing if sys.stderr is None and args.thread is None
1177 return
1178
1179 if args.thread is not None:
1180 name = args.thread.name
1181 else:
1182 name = get_ident()
1183 print(f"Exception in thread {name}:",
1184 file=stderr, flush=True)
1185 _print_exception(args.exc_type, args.exc_value, args.exc_traceback,
1186 file=stderr)
1187 stderr.flush()
1188
1189
1190def _make_invoke_excepthook():
1191 # Create a local namespace to ensure that variables remain alive
1192 # when _invoke_excepthook() is called, even if it is called late during
1193 # Python shutdown. It is mostly needed for daemon threads.
1194
1195 old_excepthook = excepthook
1196 old_sys_excepthook = _sys.excepthook
1197 if old_excepthook is None:
1198 raise RuntimeError("threading.excepthook is None")
1199 if old_sys_excepthook is None:
1200 raise RuntimeError("sys.excepthook is None")
1201
1202 sys_exc_info = _sys.exc_info
1203 local_print = print
1204 local_sys = _sys
1205
1206 def invoke_excepthook(thread):
1207 global excepthook
1208 try:
1209 hook = excepthook
1210 if hook is None:
1211 hook = old_excepthook
1212
1213 args = ExceptHookArgs([*sys_exc_info(), thread])
1214
1215 hook(args)
1216 except Exception as exc:
1217 exc.__suppress_context__ = True
1218 del exc
1219
1220 if local_sys is not None and local_sys.stderr is not None:
1221 stderr = local_sys.stderr
1222 else:
1223 stderr = thread._stderr
1224
1225 local_print("Exception in threading.excepthook:",
1226 file=stderr, flush=True)
1227
1228 if local_sys is not None and local_sys.excepthook is not None:
1229 sys_excepthook = local_sys.excepthook
1230 else:
1231 sys_excepthook = old_sys_excepthook
1232
1233 sys_excepthook(*sys_exc_info())
1234 finally:
1235 # Break reference cycle (exception stored in a variable)
1236 args = None
1237
1238 return invoke_excepthook
1239
1240
Martin v. Löwis44f86962001-09-05 13:44:54 +00001241# The timer class was contributed by Itamar Shtull-Trauring
1242
Éric Araujo0cdd4452011-07-28 00:28:28 +02001243class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001244 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +00001245
Georg Brandlc30b59f2013-10-13 10:43:59 +02001246 t = Timer(30.0, f, args=None, kwargs=None)
1247 t.start()
1248 t.cancel() # stop the timer's action if it's still waiting
1249
Martin v. Löwis44f86962001-09-05 13:44:54 +00001250 """
Tim Petersb64bec32001-09-18 02:26:39 +00001251
R David Murray19aeb432013-03-30 17:19:38 -04001252 def __init__(self, interval, function, args=None, kwargs=None):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001253 Thread.__init__(self)
1254 self.interval = interval
1255 self.function = function
R David Murray19aeb432013-03-30 17:19:38 -04001256 self.args = args if args is not None else []
1257 self.kwargs = kwargs if kwargs is not None else {}
Martin v. Löwis44f86962001-09-05 13:44:54 +00001258 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +00001259
Martin v. Löwis44f86962001-09-05 13:44:54 +00001260 def cancel(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001261 """Stop the timer if it hasn't finished yet."""
Martin v. Löwis44f86962001-09-05 13:44:54 +00001262 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +00001263
Martin v. Löwis44f86962001-09-05 13:44:54 +00001264 def run(self):
1265 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +00001266 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +00001267 self.function(*self.args, **self.kwargs)
1268 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001269
Antoine Pitrou1023dbb2017-10-02 16:42:15 +02001270
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001271# Special thread class to represent the main thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001272
1273class _MainThread(Thread):
1274
1275 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001276 Thread.__init__(self, name="MainThread", daemon=False)
Tim Petersc363a232013-09-08 18:44:40 -05001277 self._set_tstate_lock()
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001278 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001279 self._set_ident()
Jake Teslerb121f632019-05-22 08:43:17 -07001280 if _HAVE_THREAD_NATIVE_ID:
1281 self._set_native_id()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001282 with _active_limbo_lock:
1283 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001284
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001285
1286# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +00001287# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001288# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +00001289# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001290# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001291# They are marked as daemon threads so we won't wait for them
1292# when we exit (conform previous semantics).
1293
1294class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +00001295
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001296 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001297 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +00001298
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001299 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001300 self._set_ident()
Jake Teslerb121f632019-05-22 08:43:17 -07001301 if _HAVE_THREAD_NATIVE_ID:
1302 self._set_native_id()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001303 with _active_limbo_lock:
1304 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001305
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +02001306 def _stop(self):
1307 pass
1308
Xiang Zhangf3a9fab2017-02-27 11:01:30 +08001309 def is_alive(self):
1310 assert not self._is_stopped and self._started.is_set()
1311 return True
1312
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001313 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001314 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001315
1316
1317# Global API functions
1318
Benjamin Peterson672b8032008-06-11 19:14:14 +00001319def current_thread():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001320 """Return the current Thread object, corresponding to the caller's thread of control.
1321
1322 If the caller's thread of control was not created through the threading
1323 module, a dummy thread object with limited functionality is returned.
1324
1325 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001326 try:
Victor Stinner2a129742011-05-30 23:02:52 +02001327 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001328 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001329 return _DummyThread()
1330
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001331currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001332
Benjamin Peterson672b8032008-06-11 19:14:14 +00001333def active_count():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001334 """Return the number of Thread objects currently alive.
1335
1336 The returned count is equal to the length of the list returned by
1337 enumerate().
1338
1339 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001340 with _active_limbo_lock:
1341 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001342
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001343activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001344
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001345def _enumerate():
1346 # Same as enumerate(), but without the lock. Internal use only.
1347 return list(_active.values()) + list(_limbo.values())
1348
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001349def enumerate():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001350 """Return a list of all Thread objects currently alive.
1351
1352 The list includes daemonic threads, dummy thread objects created by
1353 current_thread(), and the main thread. It excludes terminated threads and
1354 threads that have not yet been started.
1355
1356 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001357 with _active_limbo_lock:
1358 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001359
Kyle Stanleyb61b8182020-03-27 15:31:22 -04001360
1361_threading_atexits = []
1362_SHUTTING_DOWN = False
1363
1364def _register_atexit(func, *arg, **kwargs):
1365 """CPython internal: register *func* to be called before joining threads.
1366
1367 The registered *func* is called with its arguments just before all
1368 non-daemon threads are joined in `_shutdown()`. It provides a similar
1369 purpose to `atexit.register()`, but its functions are called prior to
1370 threading shutdown instead of interpreter shutdown.
1371
1372 For similarity to atexit, the registered functions are called in reverse.
1373 """
1374 if _SHUTTING_DOWN:
1375 raise RuntimeError("can't register atexit after shutdown")
1376
1377 call = functools.partial(func, *arg, **kwargs)
1378 _threading_atexits.append(call)
1379
1380
Georg Brandl2067bfd2008-05-25 13:05:15 +00001381from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001382
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001383# Create the main thread object,
1384# and make it available for the interpreter
1385# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001386
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001387_main_thread = _MainThread()
1388
1389def _shutdown():
Victor Stinner468e5fe2019-06-13 01:30:17 +02001390 """
1391 Wait until the Python thread state of all non-daemon threads get deleted.
1392 """
Tim Petersc363a232013-09-08 18:44:40 -05001393 # Obscure: other threads may be waiting to join _main_thread. That's
1394 # dubious, but some code does it. We can't wait for C code to release
1395 # the main thread's tstate_lock - that won't happen until the interpreter
1396 # is nearly dead. So we release it here. Note that just calling _stop()
1397 # isn't enough: other threads may already be waiting on _tstate_lock.
Antoine Pitrouee84a602017-08-16 20:53:28 +02001398 if _main_thread._is_stopped:
1399 # _shutdown() was already called
1400 return
Victor Stinner468e5fe2019-06-13 01:30:17 +02001401
Kyle Stanleyb61b8182020-03-27 15:31:22 -04001402 global _SHUTTING_DOWN
1403 _SHUTTING_DOWN = True
Victor Stinner468e5fe2019-06-13 01:30:17 +02001404 # Main thread
Tim Petersb5e9ac92013-09-09 14:41:50 -05001405 tlock = _main_thread._tstate_lock
1406 # The main thread isn't finished yet, so its thread state lock can't have
1407 # been released.
1408 assert tlock is not None
1409 assert tlock.locked()
1410 tlock.release()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001411 _main_thread._stop()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001412
Kyle Stanleyb61b8182020-03-27 15:31:22 -04001413 # Call registered threading atexit functions before threads are joined.
1414 # Order is reversed, similar to atexit.
1415 for atexit_call in reversed(_threading_atexits):
1416 atexit_call()
1417
Victor Stinner468e5fe2019-06-13 01:30:17 +02001418 # Join all non-deamon threads
1419 while True:
1420 with _shutdown_locks_lock:
1421 locks = list(_shutdown_locks)
1422 _shutdown_locks.clear()
1423
1424 if not locks:
1425 break
1426
1427 for lock in locks:
1428 # mimick Thread.join()
1429 lock.acquire()
1430 lock.release()
1431
1432 # new threads can be spawned while we were waiting for the other
1433 # threads to complete
1434
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001435
1436def main_thread():
Andrew Svetlovb1dd5572013-09-04 10:33:11 +03001437 """Return the main thread object.
1438
1439 In normal conditions, the main thread is the thread from which the
1440 Python interpreter was started.
1441 """
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001442 return _main_thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001443
Jim Fultond15dc062004-07-14 19:11:50 +00001444# get thread-local implementation, either from the thread
1445# module, or from the python fallback
1446
1447try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001448 from _thread import _local as local
Brett Cannoncd171c82013-07-04 17:43:24 -04001449except ImportError:
Jim Fultond15dc062004-07-14 19:11:50 +00001450 from _threading_local import local
1451
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001452
Jesse Nollera8513972008-07-17 16:49:17 +00001453def _after_fork():
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001454 """
1455 Cleanup threading module state that should not exist after a fork.
1456 """
Jesse Nollera8513972008-07-17 16:49:17 +00001457 # Reset _active_limbo_lock, in case we forked while the lock was held
1458 # by another (non-forked) thread. http://bugs.python.org/issue874900
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001459 global _active_limbo_lock, _main_thread
Victor Stinner468e5fe2019-06-13 01:30:17 +02001460 global _shutdown_locks_lock, _shutdown_locks
Jesse Nollera8513972008-07-17 16:49:17 +00001461 _active_limbo_lock = _allocate_lock()
1462
1463 # fork() only copied the current thread; clear references to others.
1464 new_active = {}
Victor Stinnerd8ff44c2020-03-27 17:50:42 +01001465
1466 try:
1467 current = _active[get_ident()]
1468 except KeyError:
1469 # fork() was called in a thread which was not spawned
1470 # by threading.Thread. For example, a thread spawned
1471 # by thread.start_new_thread().
1472 current = _MainThread()
1473
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001474 _main_thread = current
Victor Stinner468e5fe2019-06-13 01:30:17 +02001475
1476 # reset _shutdown() locks: threads re-register their _tstate_lock below
1477 _shutdown_locks_lock = _allocate_lock()
1478 _shutdown_locks = set()
1479
Jesse Nollera8513972008-07-17 16:49:17 +00001480 with _active_limbo_lock:
Antoine Pitrou5da7e792013-09-08 13:19:06 +02001481 # Dangling thread instances must still have their locks reset,
1482 # because someone may join() them.
1483 threads = set(_enumerate())
1484 threads.update(_dangling)
1485 for thread in threads:
Charles-François Natalib055bf62011-12-18 18:45:16 +01001486 # Any lock/condition variable may be currently locked or in an
1487 # invalid state, so we reinitialize them.
Jesse Nollera8513972008-07-17 16:49:17 +00001488 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001489 # There is only one active thread. We reset the ident to
1490 # its new value since it can have changed.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001491 thread._reset_internal_locks(True)
Victor Stinner2a129742011-05-30 23:02:52 +02001492 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001493 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +00001494 new_active[ident] = thread
1495 else:
1496 # All the others are already stopped.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001497 thread._reset_internal_locks(False)
Charles-François Natalib055bf62011-12-18 18:45:16 +01001498 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +00001499
1500 _limbo.clear()
1501 _active.clear()
1502 _active.update(new_active)
1503 assert len(_active) == 1
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001504
1505
Gregory P. Smith163468a2017-05-29 10:03:41 -07001506if hasattr(_os, "register_at_fork"):
1507 _os.register_at_fork(after_in_child=_after_fork)