blob: 3137e495b2504c9f8ea6b2296e8107e7df40cf9f [file] [log] [blame]
Jeremy Hylton92bb6e72002-08-14 19:25:42 +00001"""Thread module emulating a subset of Java's threading model."""
Guido van Rossum7f5013a1998-04-09 22:01:42 +00002
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02003import os as _os
Fred Drakea8725952002-12-30 23:32:50 +00004import sys as _sys
Georg Brandl2067bfd2008-05-25 13:05:15 +00005import _thread
Fred Drakea8725952002-12-30 23:32:50 +00006
Victor Stinnerae586492014-09-02 23:18:25 +02007from time import monotonic as _time
Neil Schemenauerf607fc52003-11-05 23:03:00 +00008from traceback import format_exc as _format_exc
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
Jake Tesler4959c332019-05-12 10:08:24 -070026__all__ = ['get_ident', 'get_native_id', 'active_count', 'Condition',
27 'current_thread', 'enumerate', 'main_thread', 'TIMEOUT_MAX',
Martin Panter19e69c52015-11-14 12:46:42 +000028 'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
29 'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
30 'setprofile', 'settrace', 'local', 'stack_size']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000031
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000032# Rename some stuff so "from threading import *" is safe
Georg Brandl2067bfd2008-05-25 13:05:15 +000033_start_new_thread = _thread.start_new_thread
34_allocate_lock = _thread.allocate_lock
Antoine Pitrou7b476992013-09-07 23:38:37 +020035_set_sentinel = _thread._set_sentinel
Victor Stinner2a129742011-05-30 23:02:52 +020036get_ident = _thread.get_ident
Jake Tesler4959c332019-05-12 10:08:24 -070037get_native_id = _thread.get_native_id
Georg Brandl2067bfd2008-05-25 13:05:15 +000038ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000039try:
40 _CRLock = _thread.RLock
41except AttributeError:
42 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000043TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000044del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000045
Guido van Rossum7f5013a1998-04-09 22:01:42 +000046
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000047# Support for profile and trace hooks
48
49_profile_hook = None
50_trace_hook = None
51
52def setprofile(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020053 """Set a profile function for all threads started from the threading module.
54
55 The func will be passed to sys.setprofile() for each thread, before its
56 run() method is called.
57
58 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000059 global _profile_hook
60 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000061
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000062def settrace(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020063 """Set a trace function for all threads started from the threading module.
64
65 The func will be passed to sys.settrace() for each thread, before its run()
66 method is called.
67
68 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000069 global _trace_hook
70 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000071
72# Synchronization classes
73
74Lock = _allocate_lock
75
Victor Stinner135b6d82012-03-03 01:32:57 +010076def RLock(*args, **kwargs):
Georg Brandlc30b59f2013-10-13 10:43:59 +020077 """Factory function that returns a new reentrant lock.
78
79 A reentrant lock must be released by the thread that acquired it. Once a
80 thread has acquired a reentrant lock, the same thread may acquire it again
81 without blocking; the thread must release it once for each time it has
82 acquired it.
83
84 """
Victor Stinner135b6d82012-03-03 01:32:57 +010085 if _CRLock is None:
86 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000087 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000088
Victor Stinner135b6d82012-03-03 01:32:57 +010089class _RLock:
Georg Brandlc30b59f2013-10-13 10:43:59 +020090 """This class implements reentrant lock objects.
91
92 A reentrant lock must be released by the thread that acquired it. Once a
93 thread has acquired a reentrant lock, the same thread may acquire it
94 again without blocking; the thread must release it once for each time it
95 has acquired it.
96
97 """
Tim Petersb90f89a2001-01-15 03:26:36 +000098
Victor Stinner135b6d82012-03-03 01:32:57 +010099 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000100 self._block = _allocate_lock()
101 self._owner = None
102 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000103
104 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000105 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000106 try:
107 owner = _active[owner].name
108 except KeyError:
109 pass
Raymond Hettinger62f4dad2014-05-25 18:22:35 -0700110 return "<%s %s.%s object owner=%r count=%d at %s>" % (
111 "locked" if self._block.locked() else "unlocked",
112 self.__class__.__module__,
113 self.__class__.__qualname__,
114 owner,
115 self._count,
116 hex(id(self))
117 )
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000118
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000119 def acquire(self, blocking=True, timeout=-1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200120 """Acquire a lock, blocking or non-blocking.
121
122 When invoked without arguments: if this thread already owns the lock,
123 increment the recursion level by one, and return immediately. Otherwise,
124 if another thread owns the lock, block until the lock is unlocked. Once
125 the lock is unlocked (not owned by any thread), then grab ownership, set
126 the recursion level to one, and return. If more than one thread is
127 blocked waiting until the lock is unlocked, only one at a time will be
128 able to grab ownership of the lock. There is no return value in this
129 case.
130
131 When invoked with the blocking argument set to true, do the same thing
132 as when called without arguments, and return true.
133
134 When invoked with the blocking argument set to false, do not block. If a
135 call without an argument would block, return false immediately;
136 otherwise, do the same thing as when called without arguments, and
137 return true.
138
139 When invoked with the floating-point timeout argument set to a positive
140 value, block for at most the number of seconds specified by timeout
141 and as long as the lock cannot be acquired. Return true if the lock has
142 been acquired, false if the timeout has elapsed.
143
144 """
Victor Stinner2a129742011-05-30 23:02:52 +0200145 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +0000146 if self._owner == me:
Raymond Hettinger720da572013-03-10 15:13:35 -0700147 self._count += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000148 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000149 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000150 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000151 self._owner = me
152 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000153 return rc
154
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000155 __enter__ = acquire
156
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000157 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200158 """Release a lock, decrementing the recursion level.
159
160 If after the decrement it is zero, reset the lock to unlocked (not owned
161 by any thread), and if any other threads are blocked waiting for the
162 lock to become unlocked, allow exactly one of them to proceed. If after
163 the decrement the recursion level is still nonzero, the lock remains
164 locked and owned by the calling thread.
165
166 Only call this method when the calling thread owns the lock. A
167 RuntimeError is raised if this method is called when the lock is
168 unlocked.
169
170 There is no return value.
171
172 """
Victor Stinner2a129742011-05-30 23:02:52 +0200173 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000174 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000175 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000176 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000177 self._owner = None
178 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000179
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000180 def __exit__(self, t, v, tb):
181 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000182
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000183 # Internal methods used by condition variables
184
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000185 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000186 self._block.acquire()
187 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000188
189 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200190 if self._count == 0:
191 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000192 count = self._count
193 self._count = 0
194 owner = self._owner
195 self._owner = None
196 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000197 return (count, owner)
198
199 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200200 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000201
Antoine Pitrou434736a2009-11-10 18:46:01 +0000202_PyRLock = _RLock
203
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000204
Victor Stinner135b6d82012-03-03 01:32:57 +0100205class Condition:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200206 """Class that implements a condition variable.
207
208 A condition variable allows one or more threads to wait until they are
209 notified by another thread.
210
211 If the lock argument is given and not None, it must be a Lock or RLock
212 object, and it is used as the underlying lock. Otherwise, a new RLock object
213 is created and used as the underlying lock.
214
215 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000216
Victor Stinner135b6d82012-03-03 01:32:57 +0100217 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000218 if lock is None:
219 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000220 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000221 # Export the lock's acquire() and release() methods
222 self.acquire = lock.acquire
223 self.release = lock.release
224 # If the lock defines _release_save() and/or _acquire_restore(),
225 # these override the default implementations (which just call
226 # release() and acquire() on the lock). Ditto for _is_owned().
227 try:
228 self._release_save = lock._release_save
229 except AttributeError:
230 pass
231 try:
232 self._acquire_restore = lock._acquire_restore
233 except AttributeError:
234 pass
235 try:
236 self._is_owned = lock._is_owned
237 except AttributeError:
238 pass
Raymond Hettingerec4b1742013-03-10 17:57:28 -0700239 self._waiters = _deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000240
Thomas Wouters477c8d52006-05-27 19:21:47 +0000241 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000242 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000243
Thomas Wouters477c8d52006-05-27 19:21:47 +0000244 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000245 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000246
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000247 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000248 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000249
250 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000251 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000252
253 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000254 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000255
256 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000257 # Return True if lock is owned by current_thread.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300258 # This method is called only if _lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000259 if self._lock.acquire(0):
260 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000261 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000262 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000263 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000264
265 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200266 """Wait until notified or until a timeout occurs.
267
268 If the calling thread has not acquired the lock when this method is
269 called, a RuntimeError is raised.
270
271 This method releases the underlying lock, and then blocks until it is
272 awakened by a notify() or notify_all() call for the same condition
273 variable in another thread, or until the optional timeout occurs. Once
274 awakened or timed out, it re-acquires the lock and returns.
275
276 When the timeout argument is present and not None, it should be a
277 floating point number specifying a timeout for the operation in seconds
278 (or fractions thereof).
279
280 When the underlying lock is an RLock, it is not released using its
281 release() method, since this may not actually unlock the lock when it
282 was acquired multiple times recursively. Instead, an internal interface
283 of the RLock class is used, which really unlocks it even when it has
284 been recursively acquired several times. Another internal interface is
285 then used to restore the recursion level when the lock is reacquired.
286
287 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000288 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000289 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000290 waiter = _allocate_lock()
291 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000292 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000293 saved_state = self._release_save()
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200294 gotit = False
Tim Petersc951bf92001-04-02 20:15:57 +0000295 try: # restore state no matter what (e.g., KeyboardInterrupt)
296 if timeout is None:
297 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000298 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000299 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000300 if timeout > 0:
301 gotit = waiter.acquire(True, timeout)
302 else:
303 gotit = waiter.acquire(False)
Georg Brandlb9a43912010-10-28 09:03:20 +0000304 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000305 finally:
306 self._acquire_restore(saved_state)
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200307 if not gotit:
308 try:
309 self._waiters.remove(waiter)
310 except ValueError:
311 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000312
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000313 def wait_for(self, predicate, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200314 """Wait until a condition evaluates to True.
315
316 predicate should be a callable which result will be interpreted as a
317 boolean value. A timeout may be provided giving the maximum time to
318 wait.
319
320 """
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000321 endtime = None
322 waittime = timeout
323 result = predicate()
324 while not result:
325 if waittime is not None:
326 if endtime is None:
327 endtime = _time() + waittime
328 else:
329 waittime = endtime - _time()
330 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000331 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000332 self.wait(waittime)
333 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000334 return result
335
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000336 def notify(self, n=1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200337 """Wake up one or more threads waiting on this condition, if any.
338
339 If the calling thread has not acquired the lock when this method is
340 called, a RuntimeError is raised.
341
342 This method wakes up at most n of the threads waiting for the condition
343 variable; it is a no-op if no threads are waiting.
344
345 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000346 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000347 raise RuntimeError("cannot notify on un-acquired lock")
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700348 all_waiters = self._waiters
349 waiters_to_notify = _deque(_islice(all_waiters, n))
350 if not waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000351 return
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700352 for waiter in waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000353 waiter.release()
354 try:
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700355 all_waiters.remove(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000356 except ValueError:
357 pass
358
Benjamin Peterson672b8032008-06-11 19:14:14 +0000359 def notify_all(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200360 """Wake up all threads waiting on this condition.
361
362 If the calling thread has not acquired the lock when this method
363 is called, a RuntimeError is raised.
364
365 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000366 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000367
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000368 notifyAll = notify_all
369
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000370
Victor Stinner135b6d82012-03-03 01:32:57 +0100371class Semaphore:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200372 """This class implements semaphore objects.
373
374 Semaphores manage a counter representing the number of release() calls minus
375 the number of acquire() calls, plus an initial value. The acquire() method
376 blocks if necessary until it can return without making the counter
377 negative. If not given, value defaults to 1.
378
379 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000380
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000381 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000382
Victor Stinner135b6d82012-03-03 01:32:57 +0100383 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000384 if value < 0:
385 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000386 self._cond = Condition(Lock())
387 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000388
Antoine Pitrou0454af92010-04-17 23:51:58 +0000389 def acquire(self, blocking=True, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200390 """Acquire a semaphore, decrementing the internal counter by one.
391
392 When invoked without arguments: if the internal counter is larger than
393 zero on entry, decrement it by one and return immediately. If it is zero
394 on entry, block, waiting until some other thread has called release() to
395 make it larger than zero. This is done with proper interlocking so that
396 if multiple acquire() calls are blocked, release() will wake exactly one
397 of them up. The implementation may pick one at random, so the order in
398 which blocked threads are awakened should not be relied on. There is no
399 return value in this case.
400
401 When invoked with blocking set to true, do the same thing as when called
402 without arguments, and return true.
403
404 When invoked with blocking set to false, do not block. If a call without
405 an argument would block, return false immediately; otherwise, do the
406 same thing as when called without arguments, and return true.
407
408 When invoked with a timeout other than None, it will block for at
409 most timeout seconds. If acquire does not complete successfully in
410 that interval, return false. Return true otherwise.
411
412 """
Antoine Pitrou0454af92010-04-17 23:51:58 +0000413 if not blocking and timeout is not None:
414 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000415 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000416 endtime = None
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300417 with self._cond:
418 while self._value == 0:
419 if not blocking:
420 break
421 if timeout is not None:
422 if endtime is None:
423 endtime = _time() + timeout
424 else:
425 timeout = endtime - _time()
426 if timeout <= 0:
427 break
428 self._cond.wait(timeout)
429 else:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300430 self._value -= 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300431 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000432 return rc
433
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000434 __enter__ = acquire
435
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000436 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200437 """Release a semaphore, incrementing the internal counter by one.
438
439 When the counter is zero on entry and another thread is waiting for it
440 to become larger than zero again, wake up that thread.
441
442 """
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300443 with self._cond:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300444 self._value += 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300445 self._cond.notify()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000446
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000447 def __exit__(self, t, v, tb):
448 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000449
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000450
Éric Araujo0cdd4452011-07-28 00:28:28 +0200451class BoundedSemaphore(Semaphore):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200452 """Implements a bounded semaphore.
453
454 A bounded semaphore checks to make sure its current value doesn't exceed its
455 initial value. If it does, ValueError is raised. In most situations
456 semaphores are used to guard resources with limited capacity.
457
458 If the semaphore is released too many times it's a sign of a bug. If not
459 given, value defaults to 1.
460
461 Like regular semaphores, bounded semaphores manage a counter representing
462 the number of release() calls minus the number of acquire() calls, plus an
463 initial value. The acquire() method blocks if necessary until it can return
464 without making the counter negative. If not given, value defaults to 1.
465
466 """
467
Victor Stinner135b6d82012-03-03 01:32:57 +0100468 def __init__(self, value=1):
469 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000470 self._initial_value = value
471
472 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200473 """Release a semaphore, incrementing the internal counter by one.
474
475 When the counter is zero on entry and another thread is waiting for it
476 to become larger than zero again, wake up that thread.
477
478 If the number of releases exceeds the number of acquires,
479 raise a ValueError.
480
481 """
Tim Peters7634e1c2013-10-08 20:55:51 -0500482 with self._cond:
483 if self._value >= self._initial_value:
484 raise ValueError("Semaphore released too many times")
485 self._value += 1
486 self._cond.notify()
Skip Montanaroe428bb72001-08-20 20:27:58 +0000487
488
Victor Stinner135b6d82012-03-03 01:32:57 +0100489class Event:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200490 """Class implementing event objects.
491
492 Events manage a flag that can be set to true with the set() method and reset
493 to false with the clear() method. The wait() method blocks until the flag is
494 true. The flag is initially false.
495
496 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000497
498 # After Tim Peters' event class (without is_posted())
499
Victor Stinner135b6d82012-03-03 01:32:57 +0100500 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000501 self._cond = Condition(Lock())
502 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000503
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000504 def _reset_internal_locks(self):
505 # private! called by Thread._reset_internal_locks by _after_fork()
Benjamin Peterson15982aa2015-10-05 21:56:22 -0700506 self._cond.__init__(Lock())
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000507
Benjamin Peterson672b8032008-06-11 19:14:14 +0000508 def is_set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200509 """Return true if and only if the internal flag is true."""
Guido van Rossumd0648992007-08-20 19:25:41 +0000510 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000511
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000512 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000513
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000514 def set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200515 """Set the internal flag to true.
516
517 All threads waiting for it to become true are awakened. Threads
518 that call wait() once the flag is true will not block at all.
519
520 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700521 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000522 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000523 self._cond.notify_all()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000524
525 def clear(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200526 """Reset the internal flag to false.
527
528 Subsequently, threads calling wait() will block until set() is called to
529 set the internal flag to true again.
530
531 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700532 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000533 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000534
535 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200536 """Block until the internal flag is true.
537
538 If the internal flag is true on entry, return immediately. Otherwise,
539 block until another thread calls set() to set the flag to true, or until
540 the optional timeout occurs.
541
542 When the timeout argument is present and not None, it should be a
543 floating point number specifying a timeout for the operation in seconds
544 (or fractions thereof).
545
546 This method returns the internal flag on exit, so it will always return
547 True except if a timeout is given and the operation times out.
548
549 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700550 with self._cond:
Charles-François Natalided03482012-01-07 18:24:56 +0100551 signaled = self._flag
552 if not signaled:
553 signaled = self._cond.wait(timeout)
554 return signaled
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000555
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000556
557# A barrier class. Inspired in part by the pthread_barrier_* api and
558# the CyclicBarrier class from Java. See
559# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
560# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
561# CyclicBarrier.html
562# for information.
563# We maintain two main states, 'filling' and 'draining' enabling the barrier
564# to be cyclic. Threads are not allowed into it until it has fully drained
565# since the previous cycle. In addition, a 'resetting' state exists which is
566# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300567# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100568class Barrier:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200569 """Implements a Barrier.
570
571 Useful for synchronizing a fixed number of threads at known synchronization
Carl Bordum Hansen62fa51f2019-03-09 18:38:05 +0100572 points. Threads block on 'wait()' and are simultaneously awoken once they
573 have all made that call.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200574
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000575 """
Georg Brandlc30b59f2013-10-13 10:43:59 +0200576
Victor Stinner135b6d82012-03-03 01:32:57 +0100577 def __init__(self, parties, action=None, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200578 """Create a barrier, initialised to 'parties' threads.
579
580 'action' is a callable which, when supplied, will be called by one of
581 the threads after they have all entered the barrier and just prior to
Carl Bordum Hansen62fa51f2019-03-09 18:38:05 +0100582 releasing them all. If a 'timeout' is provided, it is used as the
Georg Brandlc30b59f2013-10-13 10:43:59 +0200583 default for all subsequent 'wait()' calls.
584
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000585 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000586 self._cond = Condition(Lock())
587 self._action = action
588 self._timeout = timeout
589 self._parties = parties
590 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
591 self._count = 0
592
593 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200594 """Wait for the barrier.
595
596 When the specified number of threads have started waiting, they are all
597 simultaneously awoken. If an 'action' was provided for the barrier, one
598 of the threads will have executed that callback prior to returning.
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000599 Returns an individual index number from 0 to 'parties-1'.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200600
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000601 """
602 if timeout is None:
603 timeout = self._timeout
604 with self._cond:
605 self._enter() # Block while the barrier drains.
606 index = self._count
607 self._count += 1
608 try:
609 if index + 1 == self._parties:
610 # We release the barrier
611 self._release()
612 else:
613 # We wait until someone releases us
614 self._wait(timeout)
615 return index
616 finally:
617 self._count -= 1
618 # Wake up any threads waiting for barrier to drain.
619 self._exit()
620
621 # Block until the barrier is ready for us, or raise an exception
622 # if it is broken.
623 def _enter(self):
624 while self._state in (-1, 1):
625 # It is draining or resetting, wait until done
626 self._cond.wait()
627 #see if the barrier is in a broken state
628 if self._state < 0:
629 raise BrokenBarrierError
630 assert self._state == 0
631
632 # Optionally run the 'action' and release the threads waiting
633 # in the barrier.
634 def _release(self):
635 try:
636 if self._action:
637 self._action()
638 # enter draining state
639 self._state = 1
640 self._cond.notify_all()
641 except:
642 #an exception during the _action handler. Break and reraise
643 self._break()
644 raise
645
Martin Panter69332c12016-08-04 13:07:31 +0000646 # Wait in the barrier until we are released. Raise an exception
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000647 # if the barrier is reset or broken.
648 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000649 if not self._cond.wait_for(lambda : self._state != 0, timeout):
650 #timed out. Break the barrier
651 self._break()
652 raise BrokenBarrierError
653 if self._state < 0:
654 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000655 assert self._state == 1
656
657 # If we are the last thread to exit the barrier, signal any threads
658 # waiting for the barrier to drain.
659 def _exit(self):
660 if self._count == 0:
661 if self._state in (-1, 1):
662 #resetting or draining
663 self._state = 0
664 self._cond.notify_all()
665
666 def reset(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200667 """Reset the barrier to the initial state.
668
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000669 Any threads currently waiting will get the BrokenBarrier exception
670 raised.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200671
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000672 """
673 with self._cond:
674 if self._count > 0:
675 if self._state == 0:
676 #reset the barrier, waking up threads
677 self._state = -1
678 elif self._state == -2:
679 #was broken, set it to reset state
680 #which clears when the last thread exits
681 self._state = -1
682 else:
683 self._state = 0
684 self._cond.notify_all()
685
686 def abort(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200687 """Place the barrier into a 'broken' state.
688
689 Useful in case of error. Any currently waiting threads and threads
690 attempting to 'wait()' will have BrokenBarrierError raised.
691
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000692 """
693 with self._cond:
694 self._break()
695
696 def _break(self):
697 # An internal error was detected. The barrier is set to
698 # a broken state all parties awakened.
699 self._state = -2
700 self._cond.notify_all()
701
702 @property
703 def parties(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200704 """Return the number of threads required to trip the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000705 return self._parties
706
707 @property
708 def n_waiting(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200709 """Return the number of threads currently waiting at the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000710 # We don't need synchronization here since this is an ephemeral result
711 # anyway. It returns the correct value in the steady state.
712 if self._state == 0:
713 return self._count
714 return 0
715
716 @property
717 def broken(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200718 """Return True if the barrier is in a broken state."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000719 return self._state == -2
720
Georg Brandlc30b59f2013-10-13 10:43:59 +0200721# exception raised by the Barrier class
722class BrokenBarrierError(RuntimeError):
723 pass
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000724
725
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000726# Helper to generate new thread names
R David Murrayb186f1df2014-10-04 17:43:54 -0400727_counter = _count().__next__
728_counter() # Consume 0 so first non-main thread has id 1.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000729def _newname(template="Thread-%d"):
R David Murrayb186f1df2014-10-04 17:43:54 -0400730 return template % _counter()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000731
732# Active thread administration
733_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000734_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000735_limbo = {}
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200736_dangling = WeakSet()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000737
738# Main class for threads
739
Victor Stinner135b6d82012-03-03 01:32:57 +0100740class Thread:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200741 """A class that represents a thread of control.
742
743 This class can be safely subclassed in a limited fashion. There are two ways
744 to specify the activity: by passing a callable object to the constructor, or
745 by overriding the run() method in a subclass.
746
747 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000748
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300749 _initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000750 # Need to store a reference to sys.exc_info for printing
751 # out exceptions when a thread tries to use a global var. during interp.
752 # shutdown and thus raises an exception about trying to perform some
753 # operation on/with a NoneType
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300754 _exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000755 # Keep sys.exc_clear too to clear the exception just before
756 # allowing .join() to return.
757 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000758
759 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100760 args=(), kwargs=None, *, daemon=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200761 """This constructor should always be called with keyword arguments. Arguments are:
762
763 *group* should be None; reserved for future extension when a ThreadGroup
764 class is implemented.
765
766 *target* is the callable object to be invoked by the run()
767 method. Defaults to None, meaning nothing is called.
768
769 *name* is the thread name. By default, a unique name is constructed of
770 the form "Thread-N" where N is a small decimal number.
771
772 *args* is the argument tuple for the target invocation. Defaults to ().
773
774 *kwargs* is a dictionary of keyword arguments for the target
775 invocation. Defaults to {}.
776
777 If a subclass overrides the constructor, it must make sure to invoke
778 the base class constructor (Thread.__init__()) before doing anything
779 else to the thread.
780
781 """
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000782 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000783 if kwargs is None:
784 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000785 self._target = target
786 self._name = str(name or _newname())
787 self._args = args
788 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000789 if daemon is not None:
790 self._daemonic = daemon
791 else:
792 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000793 self._ident = None
Jake Tesler4959c332019-05-12 10:08:24 -0700794 self._native_id = 0
Antoine Pitrou7b476992013-09-07 23:38:37 +0200795 self._tstate_lock = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000796 self._started = Event()
Tim Petersc363a232013-09-08 18:44:40 -0500797 self._is_stopped = False
Guido van Rossumd0648992007-08-20 19:25:41 +0000798 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000799 # sys.stderr is not stored in the class like
800 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000801 self._stderr = _sys.stderr
Antoine Pitrou5da7e792013-09-08 13:19:06 +0200802 # For debugging and _after_fork()
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200803 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000804
Antoine Pitrou7b476992013-09-07 23:38:37 +0200805 def _reset_internal_locks(self, is_alive):
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000806 # private! Called by _after_fork() to reset our internal locks as
807 # they may be in an invalid state leading to a deadlock or crash.
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000808 self._started._reset_internal_locks()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200809 if is_alive:
810 self._set_tstate_lock()
811 else:
812 # The thread isn't alive after fork: it doesn't have a tstate
813 # anymore.
Tim Petersb5e9ac92013-09-09 14:41:50 -0500814 self._is_stopped = True
Antoine Pitrou7b476992013-09-07 23:38:37 +0200815 self._tstate_lock = None
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000816
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000817 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000818 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000819 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000820 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000821 status = "started"
Tim Peters72460fa2013-09-09 18:48:24 -0500822 self.is_alive() # easy way to get ._is_stopped set when appropriate
Tim Petersc363a232013-09-08 18:44:40 -0500823 if self._is_stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000824 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000825 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000826 status += " daemon"
827 if self._ident is not None:
828 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000829 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000830
831 def start(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200832 """Start the thread's activity.
833
834 It must be called at most once per thread object. It arranges for the
835 object's run() method to be invoked in a separate thread of control.
836
837 This method will raise a RuntimeError if called more than once on the
838 same thread object.
839
840 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000841 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000842 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000843
Benjamin Peterson672b8032008-06-11 19:14:14 +0000844 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000845 raise RuntimeError("threads can only be started once")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000846 with _active_limbo_lock:
847 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000848 try:
849 _start_new_thread(self._bootstrap, ())
850 except Exception:
851 with _active_limbo_lock:
852 del _limbo[self]
853 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000854 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000855
856 def run(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200857 """Method representing the thread's activity.
858
859 You may override this method in a subclass. The standard run() method
860 invokes the callable object passed to the object's constructor as the
861 target argument, if any, with sequential and keyword arguments taken
862 from the args and kwargs arguments, respectively.
863
864 """
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000865 try:
866 if self._target:
867 self._target(*self._args, **self._kwargs)
868 finally:
869 # Avoid a refcycle if the thread is running a function with
870 # an argument that has a member that points to the thread.
871 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000872
Guido van Rossumd0648992007-08-20 19:25:41 +0000873 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000874 # Wrapper around the real bootstrap code that ignores
875 # exceptions during interpreter cleanup. Those typically
876 # happen when a daemon thread wakes up at an unfortunate
877 # moment, finds the world around it destroyed, and raises some
878 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000879 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000880 # don't help anybody, and they confuse users, so we suppress
881 # them. We suppress them only when it appears that the world
882 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000883 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000884 # reported. Also, we only suppress them for daemonic threads;
885 # if a non-daemonic encounters this, something else is wrong.
886 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000887 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000888 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000889 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000890 return
891 raise
892
Benjamin Petersond23f8222009-04-05 19:13:16 +0000893 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200894 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000895
Jake Tesler4959c332019-05-12 10:08:24 -0700896 def _set_native_id(self):
897 self._native_id = get_native_id()
898
Antoine Pitrou7b476992013-09-07 23:38:37 +0200899 def _set_tstate_lock(self):
900 """
901 Set a lock object which will be released by the interpreter when
902 the underlying thread state (see pystate.h) gets deleted.
903 """
904 self._tstate_lock = _set_sentinel()
905 self._tstate_lock.acquire()
906
Guido van Rossumd0648992007-08-20 19:25:41 +0000907 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000908 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000909 self._set_ident()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200910 self._set_tstate_lock()
Jake Tesler4959c332019-05-12 10:08:24 -0700911 self._set_native_id()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000912 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000913 with _active_limbo_lock:
914 _active[self._ident] = self
915 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000916
917 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000918 _sys.settrace(_trace_hook)
919 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000920 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000921
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000922 try:
923 self.run()
924 except SystemExit:
Victor Stinner135b6d82012-03-03 01:32:57 +0100925 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000926 except:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000927 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000928 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000929 # _sys) in case sys.stderr was redefined since the creation of
930 # self.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300931 if _sys and _sys.stderr is not None:
932 print("Exception in thread %s:\n%s" %
Zachary Ware78b5ed92016-05-09 14:49:31 -0500933 (self.name, _format_exc()), file=_sys.stderr)
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300934 elif self._stderr is not None:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000935 # Do the best job possible w/o a huge amt. of code to
936 # approximate a traceback (code ideas from
937 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000938 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000939 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000940 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000941 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000942 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000943 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000944 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000945 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000946 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000947 ' File "%s", line %s, in %s' %
948 (exc_tb.tb_frame.f_code.co_filename,
949 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000950 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000951 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000952 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +0200953 self._stderr.flush()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000954 # Make sure that exc_tb gets deleted since it is a memory
955 # hog; deleting everything else is just for thoroughness
956 finally:
957 del exc_type, exc_value, exc_tb
Christian Heimesbbe741d2008-03-28 10:53:29 +0000958 finally:
959 # Prevent a race in
960 # test_threading.test_no_refcycle_through_target when
961 # the exception keeps the target alive past when we
962 # assert that it's dead.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300963 #XXX self._exc_clear()
Christian Heimesbbe741d2008-03-28 10:53:29 +0000964 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000965 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000966 with _active_limbo_lock:
Christian Heimes1af737c2008-01-23 08:24:23 +0000967 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000968 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000969 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200970 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000971 except:
972 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000973
Guido van Rossumd0648992007-08-20 19:25:41 +0000974 def _stop(self):
Tim Petersb5e9ac92013-09-09 14:41:50 -0500975 # After calling ._stop(), .is_alive() returns False and .join() returns
976 # immediately. ._tstate_lock must be released before calling ._stop().
977 #
978 # Normal case: C code at the end of the thread's life
979 # (release_sentinel in _threadmodule.c) releases ._tstate_lock, and
980 # that's detected by our ._wait_for_tstate_lock(), called by .join()
981 # and .is_alive(). Any number of threads _may_ call ._stop()
982 # simultaneously (for example, if multiple threads are blocked in
983 # .join() calls), and they're not serialized. That's harmless -
984 # they'll just make redundant rebindings of ._is_stopped and
985 # ._tstate_lock. Obscure: we rebind ._tstate_lock last so that the
986 # "assert self._is_stopped" in ._wait_for_tstate_lock() always works
987 # (the assert is executed only if ._tstate_lock is None).
988 #
989 # Special case: _main_thread releases ._tstate_lock via this
990 # module's _shutdown() function.
991 lock = self._tstate_lock
992 if lock is not None:
993 assert not lock.locked()
Tim Peters78755232013-09-09 13:47:16 -0500994 self._is_stopped = True
995 self._tstate_lock = None
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000996
Guido van Rossumd0648992007-08-20 19:25:41 +0000997 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000998 "Remove current thread from the dict of currently running threads."
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200999 with _active_limbo_lock:
1000 del _active[get_ident()]
1001 # There must not be any python code between the previous line
1002 # and after the lock is released. Otherwise a tracing function
1003 # could try to acquire the lock again in the same thread, (in
1004 # current_thread()), and would block.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001005
1006 def join(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001007 """Wait until the thread terminates.
1008
1009 This blocks the calling thread until the thread whose join() method is
1010 called terminates -- either normally or through an unhandled exception
1011 or until the optional timeout occurs.
1012
1013 When the timeout argument is present and not None, it should be a
1014 floating point number specifying a timeout for the operation in seconds
1015 (or fractions thereof). As join() always returns None, you must call
Dong-hee Na36d9e9a2019-01-18 18:50:47 +09001016 is_alive() after join() to decide whether a timeout happened -- if the
Georg Brandlc30b59f2013-10-13 10:43:59 +02001017 thread is still alive, the join() call timed out.
1018
1019 When the timeout argument is not present or None, the operation will
1020 block until the thread terminates.
1021
1022 A thread can be join()ed many times.
1023
1024 join() raises a RuntimeError if an attempt is made to join the current
1025 thread as that would cause a deadlock. It is also an error to join() a
1026 thread before it has been started and attempts to do so raises the same
1027 exception.
1028
1029 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001030 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001031 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001032 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001033 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001034 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001035 raise RuntimeError("cannot join current thread")
Tim Peterse5bb0bf2013-10-25 20:46:51 -05001036
Tim Petersc363a232013-09-08 18:44:40 -05001037 if timeout is None:
1038 self._wait_for_tstate_lock()
Tim Peters7bad39f2013-10-25 22:33:52 -05001039 else:
1040 # the behavior of a negative timeout isn't documented, but
Tim Petersa577f1e2013-10-26 11:56:16 -05001041 # historically .join(timeout=x) for x<0 has acted as if timeout=0
Tim Peters7bad39f2013-10-25 22:33:52 -05001042 self._wait_for_tstate_lock(timeout=max(timeout, 0))
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001043
Tim Petersc363a232013-09-08 18:44:40 -05001044 def _wait_for_tstate_lock(self, block=True, timeout=-1):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001045 # Issue #18808: wait for the thread state to be gone.
Tim Petersc363a232013-09-08 18:44:40 -05001046 # At the end of the thread's life, after all knowledge of the thread
1047 # is removed from C data structures, C code releases our _tstate_lock.
Martin Panter46f50722016-05-26 05:35:26 +00001048 # This method passes its arguments to _tstate_lock.acquire().
Tim Petersc363a232013-09-08 18:44:40 -05001049 # If the lock is acquired, the C code is done, and self._stop() is
1050 # called. That sets ._is_stopped to True, and ._tstate_lock to None.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001051 lock = self._tstate_lock
Tim Petersc363a232013-09-08 18:44:40 -05001052 if lock is None: # already determined that the C code is done
1053 assert self._is_stopped
1054 elif lock.acquire(block, timeout):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001055 lock.release()
Tim Petersc363a232013-09-08 18:44:40 -05001056 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001057
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001058 @property
1059 def name(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001060 """A string used for identification purposes only.
1061
1062 It has no semantics. Multiple threads may be given the same name. The
1063 initial name is set by the constructor.
1064
1065 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001066 assert self._initialized, "Thread.__init__() not called"
1067 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001068
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001069 @name.setter
1070 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +00001071 assert self._initialized, "Thread.__init__() not called"
1072 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001073
Benjamin Peterson773c17b2008-08-18 16:45:31 +00001074 @property
1075 def ident(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001076 """Thread identifier of this thread or None if it has not been started.
1077
Skip Montanaro56343312018-05-18 13:38:36 -05001078 This is a nonzero integer. See the get_ident() function. Thread
Georg Brandlc30b59f2013-10-13 10:43:59 +02001079 identifiers may be recycled when a thread exits and another thread is
1080 created. The identifier is available even after the thread has exited.
1081
1082 """
Georg Brandl0c77a822008-06-10 16:37:50 +00001083 assert self._initialized, "Thread.__init__() not called"
1084 return self._ident
1085
Jake Tesler4959c332019-05-12 10:08:24 -07001086 @property
1087 def native_id(self):
1088 """Native integral thread ID of this thread or 0 if it has not been started.
1089
1090 This is a non-negative integer. See the get_native_id() function.
1091 This represents the Thread ID as reported by the kernel.
1092
1093 """
1094 assert self._initialized, "Thread.__init__() not called"
1095 return self._native_id
1096
Benjamin Peterson672b8032008-06-11 19:14:14 +00001097 def is_alive(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001098 """Return whether the thread is alive.
1099
1100 This method returns True just before the run() method starts until just
1101 after the run() method terminates. The module function enumerate()
1102 returns a list of all alive threads.
1103
1104 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001105 assert self._initialized, "Thread.__init__() not called"
Tim Petersc363a232013-09-08 18:44:40 -05001106 if self._is_stopped or not self._started.is_set():
Antoine Pitrou7b476992013-09-07 23:38:37 +02001107 return False
Antoine Pitrou7b476992013-09-07 23:38:37 +02001108 self._wait_for_tstate_lock(False)
Tim Petersc363a232013-09-08 18:44:40 -05001109 return not self._is_stopped
Tim Petersb90f89a2001-01-15 03:26:36 +00001110
Dong-hee Na89669ff2019-01-17 21:14:45 +09001111 def isAlive(self):
1112 """Return whether the thread is alive.
1113
1114 This method is deprecated, use is_alive() instead.
1115 """
1116 import warnings
1117 warnings.warn('isAlive() is deprecated, use is_alive() instead',
1118 DeprecationWarning, stacklevel=2)
1119 return self.is_alive()
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001120
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001121 @property
1122 def daemon(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001123 """A boolean value indicating whether this thread is a daemon thread.
1124
1125 This must be set before start() is called, otherwise RuntimeError is
1126 raised. Its initial value is inherited from the creating thread; the
1127 main thread is not a daemon thread and therefore all threads created in
1128 the main thread default to daemon = False.
1129
1130 The entire Python program exits when no alive non-daemon threads are
1131 left.
1132
1133 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001134 assert self._initialized, "Thread.__init__() not called"
1135 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001136
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001137 @daemon.setter
1138 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +00001139 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001140 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001141 if self._started.is_set():
Antoine Pitrou10959072014-03-17 18:22:41 +01001142 raise RuntimeError("cannot set daemon status of active thread")
Guido van Rossumd0648992007-08-20 19:25:41 +00001143 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001144
Benjamin Peterson6640d722008-08-18 18:16:46 +00001145 def isDaemon(self):
1146 return self.daemon
1147
1148 def setDaemon(self, daemonic):
1149 self.daemon = daemonic
1150
1151 def getName(self):
1152 return self.name
1153
1154 def setName(self, name):
1155 self.name = name
1156
Martin v. Löwis44f86962001-09-05 13:44:54 +00001157# The timer class was contributed by Itamar Shtull-Trauring
1158
Éric Araujo0cdd4452011-07-28 00:28:28 +02001159class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001160 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +00001161
Georg Brandlc30b59f2013-10-13 10:43:59 +02001162 t = Timer(30.0, f, args=None, kwargs=None)
1163 t.start()
1164 t.cancel() # stop the timer's action if it's still waiting
1165
Martin v. Löwis44f86962001-09-05 13:44:54 +00001166 """
Tim Petersb64bec32001-09-18 02:26:39 +00001167
R David Murray19aeb432013-03-30 17:19:38 -04001168 def __init__(self, interval, function, args=None, kwargs=None):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001169 Thread.__init__(self)
1170 self.interval = interval
1171 self.function = function
R David Murray19aeb432013-03-30 17:19:38 -04001172 self.args = args if args is not None else []
1173 self.kwargs = kwargs if kwargs is not None else {}
Martin v. Löwis44f86962001-09-05 13:44:54 +00001174 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +00001175
Martin v. Löwis44f86962001-09-05 13:44:54 +00001176 def cancel(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001177 """Stop the timer if it hasn't finished yet."""
Martin v. Löwis44f86962001-09-05 13:44:54 +00001178 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +00001179
Martin v. Löwis44f86962001-09-05 13:44:54 +00001180 def run(self):
1181 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +00001182 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +00001183 self.function(*self.args, **self.kwargs)
1184 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001185
Antoine Pitrou1023dbb2017-10-02 16:42:15 +02001186
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001187# Special thread class to represent the main thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001188
1189class _MainThread(Thread):
1190
1191 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001192 Thread.__init__(self, name="MainThread", daemon=False)
Tim Petersc363a232013-09-08 18:44:40 -05001193 self._set_tstate_lock()
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001194 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001195 self._set_ident()
Jake Tesler4959c332019-05-12 10:08:24 -07001196 self._set_native_id()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001197 with _active_limbo_lock:
1198 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001199
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001200
1201# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +00001202# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001203# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +00001204# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001205# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001206# They are marked as daemon threads so we won't wait for them
1207# when we exit (conform previous semantics).
1208
1209class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +00001210
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001211 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001212 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +00001213
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001214 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001215 self._set_ident()
Jake Tesler4959c332019-05-12 10:08:24 -07001216 self._set_native_id()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001217 with _active_limbo_lock:
1218 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001219
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +02001220 def _stop(self):
1221 pass
1222
Xiang Zhangf3a9fab2017-02-27 11:01:30 +08001223 def is_alive(self):
1224 assert not self._is_stopped and self._started.is_set()
1225 return True
1226
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001227 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001228 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001229
1230
1231# Global API functions
1232
Benjamin Peterson672b8032008-06-11 19:14:14 +00001233def current_thread():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001234 """Return the current Thread object, corresponding to the caller's thread of control.
1235
1236 If the caller's thread of control was not created through the threading
1237 module, a dummy thread object with limited functionality is returned.
1238
1239 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001240 try:
Victor Stinner2a129742011-05-30 23:02:52 +02001241 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001242 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001243 return _DummyThread()
1244
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001245currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001246
Benjamin Peterson672b8032008-06-11 19:14:14 +00001247def active_count():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001248 """Return the number of Thread objects currently alive.
1249
1250 The returned count is equal to the length of the list returned by
1251 enumerate().
1252
1253 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001254 with _active_limbo_lock:
1255 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001256
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001257activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001258
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001259def _enumerate():
1260 # Same as enumerate(), but without the lock. Internal use only.
1261 return list(_active.values()) + list(_limbo.values())
1262
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001263def enumerate():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001264 """Return a list of all Thread objects currently alive.
1265
1266 The list includes daemonic threads, dummy thread objects created by
1267 current_thread(), and the main thread. It excludes terminated threads and
1268 threads that have not yet been started.
1269
1270 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001271 with _active_limbo_lock:
1272 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001273
Georg Brandl2067bfd2008-05-25 13:05:15 +00001274from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001275
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001276# Create the main thread object,
1277# and make it available for the interpreter
1278# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001279
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001280_main_thread = _MainThread()
1281
1282def _shutdown():
Tim Petersc363a232013-09-08 18:44:40 -05001283 # Obscure: other threads may be waiting to join _main_thread. That's
1284 # dubious, but some code does it. We can't wait for C code to release
1285 # the main thread's tstate_lock - that won't happen until the interpreter
1286 # is nearly dead. So we release it here. Note that just calling _stop()
1287 # isn't enough: other threads may already be waiting on _tstate_lock.
Antoine Pitrouee84a602017-08-16 20:53:28 +02001288 if _main_thread._is_stopped:
1289 # _shutdown() was already called
1290 return
Tim Petersb5e9ac92013-09-09 14:41:50 -05001291 tlock = _main_thread._tstate_lock
1292 # The main thread isn't finished yet, so its thread state lock can't have
1293 # been released.
1294 assert tlock is not None
1295 assert tlock.locked()
1296 tlock.release()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001297 _main_thread._stop()
1298 t = _pickSomeNonDaemonThread()
1299 while t:
1300 t.join()
1301 t = _pickSomeNonDaemonThread()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001302
1303def _pickSomeNonDaemonThread():
1304 for t in enumerate():
1305 if not t.daemon and t.is_alive():
1306 return t
1307 return None
1308
1309def main_thread():
Andrew Svetlovb1dd5572013-09-04 10:33:11 +03001310 """Return the main thread object.
1311
1312 In normal conditions, the main thread is the thread from which the
1313 Python interpreter was started.
1314 """
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001315 return _main_thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001316
Jim Fultond15dc062004-07-14 19:11:50 +00001317# get thread-local implementation, either from the thread
1318# module, or from the python fallback
1319
1320try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001321 from _thread import _local as local
Brett Cannoncd171c82013-07-04 17:43:24 -04001322except ImportError:
Jim Fultond15dc062004-07-14 19:11:50 +00001323 from _threading_local import local
1324
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001325
Jesse Nollera8513972008-07-17 16:49:17 +00001326def _after_fork():
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001327 """
1328 Cleanup threading module state that should not exist after a fork.
1329 """
Jesse Nollera8513972008-07-17 16:49:17 +00001330 # Reset _active_limbo_lock, in case we forked while the lock was held
1331 # by another (non-forked) thread. http://bugs.python.org/issue874900
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001332 global _active_limbo_lock, _main_thread
Jesse Nollera8513972008-07-17 16:49:17 +00001333 _active_limbo_lock = _allocate_lock()
1334
1335 # fork() only copied the current thread; clear references to others.
1336 new_active = {}
1337 current = current_thread()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001338 _main_thread = current
Jesse Nollera8513972008-07-17 16:49:17 +00001339 with _active_limbo_lock:
Antoine Pitrou5da7e792013-09-08 13:19:06 +02001340 # Dangling thread instances must still have their locks reset,
1341 # because someone may join() them.
1342 threads = set(_enumerate())
1343 threads.update(_dangling)
1344 for thread in threads:
Charles-François Natalib055bf62011-12-18 18:45:16 +01001345 # Any lock/condition variable may be currently locked or in an
1346 # invalid state, so we reinitialize them.
Jesse Nollera8513972008-07-17 16:49:17 +00001347 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001348 # There is only one active thread. We reset the ident to
1349 # its new value since it can have changed.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001350 thread._reset_internal_locks(True)
Victor Stinner2a129742011-05-30 23:02:52 +02001351 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001352 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +00001353 new_active[ident] = thread
1354 else:
1355 # All the others are already stopped.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001356 thread._reset_internal_locks(False)
Charles-François Natalib055bf62011-12-18 18:45:16 +01001357 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +00001358
1359 _limbo.clear()
1360 _active.clear()
1361 _active.update(new_active)
1362 assert len(_active) == 1
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001363
1364
Gregory P. Smith163468a2017-05-29 10:03:41 -07001365if hasattr(_os, "register_at_fork"):
1366 _os.register_at_fork(after_in_child=_after_fork)