blob: 418116faceb755b7a670fdbed99d352ee2c45351 [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
Martin Panter19e69c52015-11-14 12:46:42 +000026__all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
27 'enumerate', 'main_thread', 'TIMEOUT_MAX',
28 '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
Georg Brandl2067bfd2008-05-25 13:05:15 +000037ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000038try:
39 _CRLock = _thread.RLock
40except AttributeError:
41 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000042TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000043del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000044
Guido van Rossum7f5013a1998-04-09 22:01:42 +000045
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000046# Support for profile and trace hooks
47
48_profile_hook = None
49_trace_hook = None
50
51def setprofile(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020052 """Set a profile function for all threads started from the threading module.
53
54 The func will be passed to sys.setprofile() for each thread, before its
55 run() method is called.
56
57 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000058 global _profile_hook
59 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000060
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000061def settrace(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020062 """Set a trace function for all threads started from the threading module.
63
64 The func will be passed to sys.settrace() for each thread, before its run()
65 method is called.
66
67 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000068 global _trace_hook
69 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000070
71# Synchronization classes
72
73Lock = _allocate_lock
74
Victor Stinner135b6d82012-03-03 01:32:57 +010075def RLock(*args, **kwargs):
Georg Brandlc30b59f2013-10-13 10:43:59 +020076 """Factory function that returns a new reentrant lock.
77
78 A reentrant lock must be released by the thread that acquired it. Once a
79 thread has acquired a reentrant lock, the same thread may acquire it again
80 without blocking; the thread must release it once for each time it has
81 acquired it.
82
83 """
Victor Stinner135b6d82012-03-03 01:32:57 +010084 if _CRLock is None:
85 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000086 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000087
Victor Stinner135b6d82012-03-03 01:32:57 +010088class _RLock:
Georg Brandlc30b59f2013-10-13 10:43:59 +020089 """This class implements reentrant lock objects.
90
91 A reentrant lock must be released by the thread that acquired it. Once a
92 thread has acquired a reentrant lock, the same thread may acquire it
93 again without blocking; the thread must release it once for each time it
94 has acquired it.
95
96 """
Tim Petersb90f89a2001-01-15 03:26:36 +000097
Victor Stinner135b6d82012-03-03 01:32:57 +010098 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000099 self._block = _allocate_lock()
100 self._owner = None
101 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000102
103 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000104 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000105 try:
106 owner = _active[owner].name
107 except KeyError:
108 pass
Raymond Hettinger62f4dad2014-05-25 18:22:35 -0700109 return "<%s %s.%s object owner=%r count=%d at %s>" % (
110 "locked" if self._block.locked() else "unlocked",
111 self.__class__.__module__,
112 self.__class__.__qualname__,
113 owner,
114 self._count,
115 hex(id(self))
116 )
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000117
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000118 def acquire(self, blocking=True, timeout=-1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200119 """Acquire a lock, blocking or non-blocking.
120
121 When invoked without arguments: if this thread already owns the lock,
122 increment the recursion level by one, and return immediately. Otherwise,
123 if another thread owns the lock, block until the lock is unlocked. Once
124 the lock is unlocked (not owned by any thread), then grab ownership, set
125 the recursion level to one, and return. If more than one thread is
126 blocked waiting until the lock is unlocked, only one at a time will be
127 able to grab ownership of the lock. There is no return value in this
128 case.
129
130 When invoked with the blocking argument set to true, do the same thing
131 as when called without arguments, and return true.
132
133 When invoked with the blocking argument set to false, do not block. If a
134 call without an argument would block, return false immediately;
135 otherwise, do the same thing as when called without arguments, and
136 return true.
137
138 When invoked with the floating-point timeout argument set to a positive
139 value, block for at most the number of seconds specified by timeout
140 and as long as the lock cannot be acquired. Return true if the lock has
141 been acquired, false if the timeout has elapsed.
142
143 """
Victor Stinner2a129742011-05-30 23:02:52 +0200144 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +0000145 if self._owner == me:
Raymond Hettinger720da572013-03-10 15:13:35 -0700146 self._count += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000147 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000148 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000149 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000150 self._owner = me
151 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000152 return rc
153
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000154 __enter__ = acquire
155
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000156 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200157 """Release a lock, decrementing the recursion level.
158
159 If after the decrement it is zero, reset the lock to unlocked (not owned
160 by any thread), and if any other threads are blocked waiting for the
161 lock to become unlocked, allow exactly one of them to proceed. If after
162 the decrement the recursion level is still nonzero, the lock remains
163 locked and owned by the calling thread.
164
165 Only call this method when the calling thread owns the lock. A
166 RuntimeError is raised if this method is called when the lock is
167 unlocked.
168
169 There is no return value.
170
171 """
Victor Stinner2a129742011-05-30 23:02:52 +0200172 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000173 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000174 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000175 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000176 self._owner = None
177 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000178
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000179 def __exit__(self, t, v, tb):
180 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000181
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000182 # Internal methods used by condition variables
183
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000184 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000185 self._block.acquire()
186 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000187
188 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200189 if self._count == 0:
190 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000191 count = self._count
192 self._count = 0
193 owner = self._owner
194 self._owner = None
195 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000196 return (count, owner)
197
198 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200199 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000200
Antoine Pitrou434736a2009-11-10 18:46:01 +0000201_PyRLock = _RLock
202
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000203
Victor Stinner135b6d82012-03-03 01:32:57 +0100204class Condition:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200205 """Class that implements a condition variable.
206
207 A condition variable allows one or more threads to wait until they are
208 notified by another thread.
209
210 If the lock argument is given and not None, it must be a Lock or RLock
211 object, and it is used as the underlying lock. Otherwise, a new RLock object
212 is created and used as the underlying lock.
213
214 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000215
Victor Stinner135b6d82012-03-03 01:32:57 +0100216 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000217 if lock is None:
218 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000219 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000220 # Export the lock's acquire() and release() methods
221 self.acquire = lock.acquire
222 self.release = lock.release
223 # If the lock defines _release_save() and/or _acquire_restore(),
224 # these override the default implementations (which just call
225 # release() and acquire() on the lock). Ditto for _is_owned().
226 try:
227 self._release_save = lock._release_save
228 except AttributeError:
229 pass
230 try:
231 self._acquire_restore = lock._acquire_restore
232 except AttributeError:
233 pass
234 try:
235 self._is_owned = lock._is_owned
236 except AttributeError:
237 pass
Raymond Hettingerec4b1742013-03-10 17:57:28 -0700238 self._waiters = _deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000239
Thomas Wouters477c8d52006-05-27 19:21:47 +0000240 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000241 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000242
Thomas Wouters477c8d52006-05-27 19:21:47 +0000243 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000244 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000245
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000246 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000247 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000248
249 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000250 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000251
252 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000253 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000254
255 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000256 # Return True if lock is owned by current_thread.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300257 # This method is called only if _lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000258 if self._lock.acquire(0):
259 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000260 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000261 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000262 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000263
264 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200265 """Wait until notified or until a timeout occurs.
266
267 If the calling thread has not acquired the lock when this method is
268 called, a RuntimeError is raised.
269
270 This method releases the underlying lock, and then blocks until it is
271 awakened by a notify() or notify_all() call for the same condition
272 variable in another thread, or until the optional timeout occurs. Once
273 awakened or timed out, it re-acquires the lock and returns.
274
275 When the timeout argument is present and not None, it should be a
276 floating point number specifying a timeout for the operation in seconds
277 (or fractions thereof).
278
279 When the underlying lock is an RLock, it is not released using its
280 release() method, since this may not actually unlock the lock when it
281 was acquired multiple times recursively. Instead, an internal interface
282 of the RLock class is used, which really unlocks it even when it has
283 been recursively acquired several times. Another internal interface is
284 then used to restore the recursion level when the lock is reacquired.
285
286 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000287 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000288 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000289 waiter = _allocate_lock()
290 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000291 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000292 saved_state = self._release_save()
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200293 gotit = False
Tim Petersc951bf92001-04-02 20:15:57 +0000294 try: # restore state no matter what (e.g., KeyboardInterrupt)
295 if timeout is None:
296 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000297 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000298 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000299 if timeout > 0:
300 gotit = waiter.acquire(True, timeout)
301 else:
302 gotit = waiter.acquire(False)
Georg Brandlb9a43912010-10-28 09:03:20 +0000303 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000304 finally:
305 self._acquire_restore(saved_state)
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200306 if not gotit:
307 try:
308 self._waiters.remove(waiter)
309 except ValueError:
310 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000311
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000312 def wait_for(self, predicate, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200313 """Wait until a condition evaluates to True.
314
315 predicate should be a callable which result will be interpreted as a
316 boolean value. A timeout may be provided giving the maximum time to
317 wait.
318
319 """
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000320 endtime = None
321 waittime = timeout
322 result = predicate()
323 while not result:
324 if waittime is not None:
325 if endtime is None:
326 endtime = _time() + waittime
327 else:
328 waittime = endtime - _time()
329 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000330 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000331 self.wait(waittime)
332 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000333 return result
334
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000335 def notify(self, n=1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200336 """Wake up one or more threads waiting on this condition, if any.
337
338 If the calling thread has not acquired the lock when this method is
339 called, a RuntimeError is raised.
340
341 This method wakes up at most n of the threads waiting for the condition
342 variable; it is a no-op if no threads are waiting.
343
344 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000345 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000346 raise RuntimeError("cannot notify on un-acquired lock")
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700347 all_waiters = self._waiters
348 waiters_to_notify = _deque(_islice(all_waiters, n))
349 if not waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000350 return
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700351 for waiter in waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000352 waiter.release()
353 try:
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700354 all_waiters.remove(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000355 except ValueError:
356 pass
357
Benjamin Peterson672b8032008-06-11 19:14:14 +0000358 def notify_all(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200359 """Wake up all threads waiting on this condition.
360
361 If the calling thread has not acquired the lock when this method
362 is called, a RuntimeError is raised.
363
364 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000365 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000366
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000367 notifyAll = notify_all
368
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000369
Victor Stinner135b6d82012-03-03 01:32:57 +0100370class Semaphore:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200371 """This class implements semaphore objects.
372
373 Semaphores manage a counter representing the number of release() calls minus
374 the number of acquire() calls, plus an initial value. The acquire() method
375 blocks if necessary until it can return without making the counter
376 negative. If not given, value defaults to 1.
377
378 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000379
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000380 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000381
Victor Stinner135b6d82012-03-03 01:32:57 +0100382 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000383 if value < 0:
384 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000385 self._cond = Condition(Lock())
386 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000387
Antoine Pitrou0454af92010-04-17 23:51:58 +0000388 def acquire(self, blocking=True, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200389 """Acquire a semaphore, decrementing the internal counter by one.
390
391 When invoked without arguments: if the internal counter is larger than
392 zero on entry, decrement it by one and return immediately. If it is zero
393 on entry, block, waiting until some other thread has called release() to
394 make it larger than zero. This is done with proper interlocking so that
395 if multiple acquire() calls are blocked, release() will wake exactly one
396 of them up. The implementation may pick one at random, so the order in
397 which blocked threads are awakened should not be relied on. There is no
398 return value in this case.
399
400 When invoked with blocking set to true, do the same thing as when called
401 without arguments, and return true.
402
403 When invoked with blocking set to false, do not block. If a call without
404 an argument would block, return false immediately; otherwise, do the
405 same thing as when called without arguments, and return true.
406
407 When invoked with a timeout other than None, it will block for at
408 most timeout seconds. If acquire does not complete successfully in
409 that interval, return false. Return true otherwise.
410
411 """
Antoine Pitrou0454af92010-04-17 23:51:58 +0000412 if not blocking and timeout is not None:
413 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000414 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000415 endtime = None
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300416 with self._cond:
417 while self._value == 0:
418 if not blocking:
419 break
420 if timeout is not None:
421 if endtime is None:
422 endtime = _time() + timeout
423 else:
424 timeout = endtime - _time()
425 if timeout <= 0:
426 break
427 self._cond.wait(timeout)
428 else:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300429 self._value -= 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300430 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000431 return rc
432
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000433 __enter__ = acquire
434
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000435 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200436 """Release a semaphore, incrementing the internal counter by one.
437
438 When the counter is zero on entry and another thread is waiting for it
439 to become larger than zero again, wake up that thread.
440
441 """
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300442 with self._cond:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300443 self._value += 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300444 self._cond.notify()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000445
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000446 def __exit__(self, t, v, tb):
447 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000448
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000449
Éric Araujo0cdd4452011-07-28 00:28:28 +0200450class BoundedSemaphore(Semaphore):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200451 """Implements a bounded semaphore.
452
453 A bounded semaphore checks to make sure its current value doesn't exceed its
454 initial value. If it does, ValueError is raised. In most situations
455 semaphores are used to guard resources with limited capacity.
456
457 If the semaphore is released too many times it's a sign of a bug. If not
458 given, value defaults to 1.
459
460 Like regular semaphores, bounded semaphores manage a counter representing
461 the number of release() calls minus the number of acquire() calls, plus an
462 initial value. The acquire() method blocks if necessary until it can return
463 without making the counter negative. If not given, value defaults to 1.
464
465 """
466
Victor Stinner135b6d82012-03-03 01:32:57 +0100467 def __init__(self, value=1):
468 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000469 self._initial_value = value
470
471 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200472 """Release a semaphore, incrementing the internal counter by one.
473
474 When the counter is zero on entry and another thread is waiting for it
475 to become larger than zero again, wake up that thread.
476
477 If the number of releases exceeds the number of acquires,
478 raise a ValueError.
479
480 """
Tim Peters7634e1c2013-10-08 20:55:51 -0500481 with self._cond:
482 if self._value >= self._initial_value:
483 raise ValueError("Semaphore released too many times")
484 self._value += 1
485 self._cond.notify()
Skip Montanaroe428bb72001-08-20 20:27:58 +0000486
487
Victor Stinner135b6d82012-03-03 01:32:57 +0100488class Event:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200489 """Class implementing event objects.
490
491 Events manage a flag that can be set to true with the set() method and reset
492 to false with the clear() method. The wait() method blocks until the flag is
493 true. The flag is initially false.
494
495 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000496
497 # After Tim Peters' event class (without is_posted())
498
Victor Stinner135b6d82012-03-03 01:32:57 +0100499 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000500 self._cond = Condition(Lock())
501 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000502
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000503 def _reset_internal_locks(self):
504 # private! called by Thread._reset_internal_locks by _after_fork()
Benjamin Peterson15982aa2015-10-05 21:56:22 -0700505 self._cond.__init__(Lock())
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000506
Benjamin Peterson672b8032008-06-11 19:14:14 +0000507 def is_set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200508 """Return true if and only if the internal flag is true."""
Guido van Rossumd0648992007-08-20 19:25:41 +0000509 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000510
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000511 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000512
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000513 def set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200514 """Set the internal flag to true.
515
516 All threads waiting for it to become true are awakened. Threads
517 that call wait() once the flag is true will not block at all.
518
519 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700520 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000521 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000522 self._cond.notify_all()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000523
524 def clear(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200525 """Reset the internal flag to false.
526
527 Subsequently, threads calling wait() will block until set() is called to
528 set the internal flag to true again.
529
530 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700531 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000532 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000533
534 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200535 """Block until the internal flag is true.
536
537 If the internal flag is true on entry, return immediately. Otherwise,
538 block until another thread calls set() to set the flag to true, or until
539 the optional timeout occurs.
540
541 When the timeout argument is present and not None, it should be a
542 floating point number specifying a timeout for the operation in seconds
543 (or fractions thereof).
544
545 This method returns the internal flag on exit, so it will always return
546 True except if a timeout is given and the operation times out.
547
548 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700549 with self._cond:
Charles-François Natalided03482012-01-07 18:24:56 +0100550 signaled = self._flag
551 if not signaled:
552 signaled = self._cond.wait(timeout)
553 return signaled
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000554
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000555
556# A barrier class. Inspired in part by the pthread_barrier_* api and
557# the CyclicBarrier class from Java. See
558# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
559# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
560# CyclicBarrier.html
561# for information.
562# We maintain two main states, 'filling' and 'draining' enabling the barrier
563# to be cyclic. Threads are not allowed into it until it has fully drained
564# since the previous cycle. In addition, a 'resetting' state exists which is
565# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300566# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100567class Barrier:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200568 """Implements a Barrier.
569
570 Useful for synchronizing a fixed number of threads at known synchronization
571 points. Threads block on 'wait()' and are simultaneously once they have all
572 made that call.
573
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000574 """
Georg Brandlc30b59f2013-10-13 10:43:59 +0200575
Victor Stinner135b6d82012-03-03 01:32:57 +0100576 def __init__(self, parties, action=None, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200577 """Create a barrier, initialised to 'parties' threads.
578
579 'action' is a callable which, when supplied, will be called by one of
580 the threads after they have all entered the barrier and just prior to
581 releasing them all. If a 'timeout' is provided, it is uses as the
582 default for all subsequent 'wait()' calls.
583
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000584 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000585 self._cond = Condition(Lock())
586 self._action = action
587 self._timeout = timeout
588 self._parties = parties
589 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
590 self._count = 0
591
592 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200593 """Wait for the barrier.
594
595 When the specified number of threads have started waiting, they are all
596 simultaneously awoken. If an 'action' was provided for the barrier, one
597 of the threads will have executed that callback prior to returning.
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000598 Returns an individual index number from 0 to 'parties-1'.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200599
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000600 """
601 if timeout is None:
602 timeout = self._timeout
603 with self._cond:
604 self._enter() # Block while the barrier drains.
605 index = self._count
606 self._count += 1
607 try:
608 if index + 1 == self._parties:
609 # We release the barrier
610 self._release()
611 else:
612 # We wait until someone releases us
613 self._wait(timeout)
614 return index
615 finally:
616 self._count -= 1
617 # Wake up any threads waiting for barrier to drain.
618 self._exit()
619
620 # Block until the barrier is ready for us, or raise an exception
621 # if it is broken.
622 def _enter(self):
623 while self._state in (-1, 1):
624 # It is draining or resetting, wait until done
625 self._cond.wait()
626 #see if the barrier is in a broken state
627 if self._state < 0:
628 raise BrokenBarrierError
629 assert self._state == 0
630
631 # Optionally run the 'action' and release the threads waiting
632 # in the barrier.
633 def _release(self):
634 try:
635 if self._action:
636 self._action()
637 # enter draining state
638 self._state = 1
639 self._cond.notify_all()
640 except:
641 #an exception during the _action handler. Break and reraise
642 self._break()
643 raise
644
Martin Panter69332c12016-08-04 13:07:31 +0000645 # Wait in the barrier until we are released. Raise an exception
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000646 # if the barrier is reset or broken.
647 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000648 if not self._cond.wait_for(lambda : self._state != 0, timeout):
649 #timed out. Break the barrier
650 self._break()
651 raise BrokenBarrierError
652 if self._state < 0:
653 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000654 assert self._state == 1
655
656 # If we are the last thread to exit the barrier, signal any threads
657 # waiting for the barrier to drain.
658 def _exit(self):
659 if self._count == 0:
660 if self._state in (-1, 1):
661 #resetting or draining
662 self._state = 0
663 self._cond.notify_all()
664
665 def reset(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200666 """Reset the barrier to the initial state.
667
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000668 Any threads currently waiting will get the BrokenBarrier exception
669 raised.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200670
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000671 """
672 with self._cond:
673 if self._count > 0:
674 if self._state == 0:
675 #reset the barrier, waking up threads
676 self._state = -1
677 elif self._state == -2:
678 #was broken, set it to reset state
679 #which clears when the last thread exits
680 self._state = -1
681 else:
682 self._state = 0
683 self._cond.notify_all()
684
685 def abort(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200686 """Place the barrier into a 'broken' state.
687
688 Useful in case of error. Any currently waiting threads and threads
689 attempting to 'wait()' will have BrokenBarrierError raised.
690
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000691 """
692 with self._cond:
693 self._break()
694
695 def _break(self):
696 # An internal error was detected. The barrier is set to
697 # a broken state all parties awakened.
698 self._state = -2
699 self._cond.notify_all()
700
701 @property
702 def parties(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200703 """Return the number of threads required to trip the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000704 return self._parties
705
706 @property
707 def n_waiting(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200708 """Return the number of threads currently waiting at the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000709 # We don't need synchronization here since this is an ephemeral result
710 # anyway. It returns the correct value in the steady state.
711 if self._state == 0:
712 return self._count
713 return 0
714
715 @property
716 def broken(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200717 """Return True if the barrier is in a broken state."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000718 return self._state == -2
719
Georg Brandlc30b59f2013-10-13 10:43:59 +0200720# exception raised by the Barrier class
721class BrokenBarrierError(RuntimeError):
722 pass
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000723
724
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000725# Helper to generate new thread names
R David Murrayb186f1df2014-10-04 17:43:54 -0400726_counter = _count().__next__
727_counter() # Consume 0 so first non-main thread has id 1.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000728def _newname(template="Thread-%d"):
R David Murrayb186f1df2014-10-04 17:43:54 -0400729 return template % _counter()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000730
731# Active thread administration
732_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000733_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000734_limbo = {}
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200735_dangling = WeakSet()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000736
737# Main class for threads
738
Victor Stinner135b6d82012-03-03 01:32:57 +0100739class Thread:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200740 """A class that represents a thread of control.
741
742 This class can be safely subclassed in a limited fashion. There are two ways
743 to specify the activity: by passing a callable object to the constructor, or
744 by overriding the run() method in a subclass.
745
746 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000747
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300748 _initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000749 # Need to store a reference to sys.exc_info for printing
750 # out exceptions when a thread tries to use a global var. during interp.
751 # shutdown and thus raises an exception about trying to perform some
752 # operation on/with a NoneType
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300753 _exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000754 # Keep sys.exc_clear too to clear the exception just before
755 # allowing .join() to return.
756 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000757
758 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100759 args=(), kwargs=None, *, daemon=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200760 """This constructor should always be called with keyword arguments. Arguments are:
761
762 *group* should be None; reserved for future extension when a ThreadGroup
763 class is implemented.
764
765 *target* is the callable object to be invoked by the run()
766 method. Defaults to None, meaning nothing is called.
767
768 *name* is the thread name. By default, a unique name is constructed of
769 the form "Thread-N" where N is a small decimal number.
770
771 *args* is the argument tuple for the target invocation. Defaults to ().
772
773 *kwargs* is a dictionary of keyword arguments for the target
774 invocation. Defaults to {}.
775
776 If a subclass overrides the constructor, it must make sure to invoke
777 the base class constructor (Thread.__init__()) before doing anything
778 else to the thread.
779
780 """
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000781 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000782 if kwargs is None:
783 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000784 self._target = target
785 self._name = str(name or _newname())
786 self._args = args
787 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000788 if daemon is not None:
789 self._daemonic = daemon
790 else:
791 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000792 self._ident = None
Antoine Pitrou7b476992013-09-07 23:38:37 +0200793 self._tstate_lock = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000794 self._started = Event()
Tim Petersc363a232013-09-08 18:44:40 -0500795 self._is_stopped = False
Guido van Rossumd0648992007-08-20 19:25:41 +0000796 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000797 # sys.stderr is not stored in the class like
798 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000799 self._stderr = _sys.stderr
Antoine Pitrou5da7e792013-09-08 13:19:06 +0200800 # For debugging and _after_fork()
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200801 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000802
Antoine Pitrou7b476992013-09-07 23:38:37 +0200803 def _reset_internal_locks(self, is_alive):
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000804 # private! Called by _after_fork() to reset our internal locks as
805 # they may be in an invalid state leading to a deadlock or crash.
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000806 self._started._reset_internal_locks()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200807 if is_alive:
808 self._set_tstate_lock()
809 else:
810 # The thread isn't alive after fork: it doesn't have a tstate
811 # anymore.
Tim Petersb5e9ac92013-09-09 14:41:50 -0500812 self._is_stopped = True
Antoine Pitrou7b476992013-09-07 23:38:37 +0200813 self._tstate_lock = None
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000814
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000815 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000816 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000817 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000818 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000819 status = "started"
Tim Peters72460fa2013-09-09 18:48:24 -0500820 self.is_alive() # easy way to get ._is_stopped set when appropriate
Tim Petersc363a232013-09-08 18:44:40 -0500821 if self._is_stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000822 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000823 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000824 status += " daemon"
825 if self._ident is not None:
826 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000827 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000828
829 def start(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200830 """Start the thread's activity.
831
832 It must be called at most once per thread object. It arranges for the
833 object's run() method to be invoked in a separate thread of control.
834
835 This method will raise a RuntimeError if called more than once on the
836 same thread object.
837
838 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000839 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000840 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000841
Benjamin Peterson672b8032008-06-11 19:14:14 +0000842 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000843 raise RuntimeError("threads can only be started once")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000844 with _active_limbo_lock:
845 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000846 try:
847 _start_new_thread(self._bootstrap, ())
848 except Exception:
849 with _active_limbo_lock:
850 del _limbo[self]
851 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000852 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000853
854 def run(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200855 """Method representing the thread's activity.
856
857 You may override this method in a subclass. The standard run() method
858 invokes the callable object passed to the object's constructor as the
859 target argument, if any, with sequential and keyword arguments taken
860 from the args and kwargs arguments, respectively.
861
862 """
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000863 try:
864 if self._target:
865 self._target(*self._args, **self._kwargs)
866 finally:
867 # Avoid a refcycle if the thread is running a function with
868 # an argument that has a member that points to the thread.
869 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000870
Guido van Rossumd0648992007-08-20 19:25:41 +0000871 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000872 # Wrapper around the real bootstrap code that ignores
873 # exceptions during interpreter cleanup. Those typically
874 # happen when a daemon thread wakes up at an unfortunate
875 # moment, finds the world around it destroyed, and raises some
876 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000877 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000878 # don't help anybody, and they confuse users, so we suppress
879 # them. We suppress them only when it appears that the world
880 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000881 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000882 # reported. Also, we only suppress them for daemonic threads;
883 # if a non-daemonic encounters this, something else is wrong.
884 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000885 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000886 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000887 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000888 return
889 raise
890
Benjamin Petersond23f8222009-04-05 19:13:16 +0000891 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200892 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000893
Antoine Pitrou7b476992013-09-07 23:38:37 +0200894 def _set_tstate_lock(self):
895 """
896 Set a lock object which will be released by the interpreter when
897 the underlying thread state (see pystate.h) gets deleted.
898 """
899 self._tstate_lock = _set_sentinel()
900 self._tstate_lock.acquire()
901
Guido van Rossumd0648992007-08-20 19:25:41 +0000902 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000903 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000904 self._set_ident()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200905 self._set_tstate_lock()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000906 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000907 with _active_limbo_lock:
908 _active[self._ident] = self
909 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000910
911 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000912 _sys.settrace(_trace_hook)
913 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000914 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000915
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000916 try:
917 self.run()
918 except SystemExit:
Victor Stinner135b6d82012-03-03 01:32:57 +0100919 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000920 except:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000921 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000922 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000923 # _sys) in case sys.stderr was redefined since the creation of
924 # self.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300925 if _sys and _sys.stderr is not None:
926 print("Exception in thread %s:\n%s" %
Zachary Ware78b5ed92016-05-09 14:49:31 -0500927 (self.name, _format_exc()), file=_sys.stderr)
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300928 elif self._stderr is not None:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000929 # Do the best job possible w/o a huge amt. of code to
930 # approximate a traceback (code ideas from
931 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000932 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000933 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000934 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000935 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000936 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000937 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000938 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000939 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000940 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000941 ' File "%s", line %s, in %s' %
942 (exc_tb.tb_frame.f_code.co_filename,
943 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000944 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000945 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000946 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +0200947 self._stderr.flush()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000948 # Make sure that exc_tb gets deleted since it is a memory
949 # hog; deleting everything else is just for thoroughness
950 finally:
951 del exc_type, exc_value, exc_tb
Christian Heimesbbe741d2008-03-28 10:53:29 +0000952 finally:
953 # Prevent a race in
954 # test_threading.test_no_refcycle_through_target when
955 # the exception keeps the target alive past when we
956 # assert that it's dead.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300957 #XXX self._exc_clear()
Christian Heimesbbe741d2008-03-28 10:53:29 +0000958 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000959 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000960 with _active_limbo_lock:
Christian Heimes1af737c2008-01-23 08:24:23 +0000961 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000962 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000963 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200964 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000965 except:
966 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000967
Guido van Rossumd0648992007-08-20 19:25:41 +0000968 def _stop(self):
Tim Petersb5e9ac92013-09-09 14:41:50 -0500969 # After calling ._stop(), .is_alive() returns False and .join() returns
970 # immediately. ._tstate_lock must be released before calling ._stop().
971 #
972 # Normal case: C code at the end of the thread's life
973 # (release_sentinel in _threadmodule.c) releases ._tstate_lock, and
974 # that's detected by our ._wait_for_tstate_lock(), called by .join()
975 # and .is_alive(). Any number of threads _may_ call ._stop()
976 # simultaneously (for example, if multiple threads are blocked in
977 # .join() calls), and they're not serialized. That's harmless -
978 # they'll just make redundant rebindings of ._is_stopped and
979 # ._tstate_lock. Obscure: we rebind ._tstate_lock last so that the
980 # "assert self._is_stopped" in ._wait_for_tstate_lock() always works
981 # (the assert is executed only if ._tstate_lock is None).
982 #
983 # Special case: _main_thread releases ._tstate_lock via this
984 # module's _shutdown() function.
985 lock = self._tstate_lock
986 if lock is not None:
987 assert not lock.locked()
Tim Peters78755232013-09-09 13:47:16 -0500988 self._is_stopped = True
989 self._tstate_lock = None
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000990
Guido van Rossumd0648992007-08-20 19:25:41 +0000991 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000992 "Remove current thread from the dict of currently running threads."
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200993 with _active_limbo_lock:
994 del _active[get_ident()]
995 # There must not be any python code between the previous line
996 # and after the lock is released. Otherwise a tracing function
997 # could try to acquire the lock again in the same thread, (in
998 # current_thread()), and would block.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000999
1000 def join(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001001 """Wait until the thread terminates.
1002
1003 This blocks the calling thread until the thread whose join() method is
1004 called terminates -- either normally or through an unhandled exception
1005 or until the optional timeout occurs.
1006
1007 When the timeout argument is present and not None, it should be a
1008 floating point number specifying a timeout for the operation in seconds
1009 (or fractions thereof). As join() always returns None, you must call
1010 isAlive() after join() to decide whether a timeout happened -- if the
1011 thread is still alive, the join() call timed out.
1012
1013 When the timeout argument is not present or None, the operation will
1014 block until the thread terminates.
1015
1016 A thread can be join()ed many times.
1017
1018 join() raises a RuntimeError if an attempt is made to join the current
1019 thread as that would cause a deadlock. It is also an error to join() a
1020 thread before it has been started and attempts to do so raises the same
1021 exception.
1022
1023 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001024 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001025 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001026 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001027 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001028 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001029 raise RuntimeError("cannot join current thread")
Tim Peterse5bb0bf2013-10-25 20:46:51 -05001030
Tim Petersc363a232013-09-08 18:44:40 -05001031 if timeout is None:
1032 self._wait_for_tstate_lock()
Tim Peters7bad39f2013-10-25 22:33:52 -05001033 else:
1034 # the behavior of a negative timeout isn't documented, but
Tim Petersa577f1e2013-10-26 11:56:16 -05001035 # historically .join(timeout=x) for x<0 has acted as if timeout=0
Tim Peters7bad39f2013-10-25 22:33:52 -05001036 self._wait_for_tstate_lock(timeout=max(timeout, 0))
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001037
Tim Petersc363a232013-09-08 18:44:40 -05001038 def _wait_for_tstate_lock(self, block=True, timeout=-1):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001039 # Issue #18808: wait for the thread state to be gone.
Tim Petersc363a232013-09-08 18:44:40 -05001040 # At the end of the thread's life, after all knowledge of the thread
1041 # is removed from C data structures, C code releases our _tstate_lock.
Martin Panter46f50722016-05-26 05:35:26 +00001042 # This method passes its arguments to _tstate_lock.acquire().
Tim Petersc363a232013-09-08 18:44:40 -05001043 # If the lock is acquired, the C code is done, and self._stop() is
1044 # called. That sets ._is_stopped to True, and ._tstate_lock to None.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001045 lock = self._tstate_lock
Tim Petersc363a232013-09-08 18:44:40 -05001046 if lock is None: # already determined that the C code is done
1047 assert self._is_stopped
1048 elif lock.acquire(block, timeout):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001049 lock.release()
Tim Petersc363a232013-09-08 18:44:40 -05001050 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001051
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001052 @property
1053 def name(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001054 """A string used for identification purposes only.
1055
1056 It has no semantics. Multiple threads may be given the same name. The
1057 initial name is set by the constructor.
1058
1059 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001060 assert self._initialized, "Thread.__init__() not called"
1061 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001062
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001063 @name.setter
1064 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +00001065 assert self._initialized, "Thread.__init__() not called"
1066 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001067
Benjamin Peterson773c17b2008-08-18 16:45:31 +00001068 @property
1069 def ident(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001070 """Thread identifier of this thread or None if it has not been started.
1071
1072 This is a nonzero integer. See the thread.get_ident() function. Thread
1073 identifiers may be recycled when a thread exits and another thread is
1074 created. The identifier is available even after the thread has exited.
1075
1076 """
Georg Brandl0c77a822008-06-10 16:37:50 +00001077 assert self._initialized, "Thread.__init__() not called"
1078 return self._ident
1079
Benjamin Peterson672b8032008-06-11 19:14:14 +00001080 def is_alive(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001081 """Return whether the thread is alive.
1082
1083 This method returns True just before the run() method starts until just
1084 after the run() method terminates. The module function enumerate()
1085 returns a list of all alive threads.
1086
1087 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001088 assert self._initialized, "Thread.__init__() not called"
Tim Petersc363a232013-09-08 18:44:40 -05001089 if self._is_stopped or not self._started.is_set():
Antoine Pitrou7b476992013-09-07 23:38:37 +02001090 return False
Antoine Pitrou7b476992013-09-07 23:38:37 +02001091 self._wait_for_tstate_lock(False)
Tim Petersc363a232013-09-08 18:44:40 -05001092 return not self._is_stopped
Tim Petersb90f89a2001-01-15 03:26:36 +00001093
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001094 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001095
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001096 @property
1097 def daemon(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001098 """A boolean value indicating whether this thread is a daemon thread.
1099
1100 This must be set before start() is called, otherwise RuntimeError is
1101 raised. Its initial value is inherited from the creating thread; the
1102 main thread is not a daemon thread and therefore all threads created in
1103 the main thread default to daemon = False.
1104
1105 The entire Python program exits when no alive non-daemon threads are
1106 left.
1107
1108 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001109 assert self._initialized, "Thread.__init__() not called"
1110 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001111
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001112 @daemon.setter
1113 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +00001114 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001115 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001116 if self._started.is_set():
Antoine Pitrou10959072014-03-17 18:22:41 +01001117 raise RuntimeError("cannot set daemon status of active thread")
Guido van Rossumd0648992007-08-20 19:25:41 +00001118 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001119
Benjamin Peterson6640d722008-08-18 18:16:46 +00001120 def isDaemon(self):
1121 return self.daemon
1122
1123 def setDaemon(self, daemonic):
1124 self.daemon = daemonic
1125
1126 def getName(self):
1127 return self.name
1128
1129 def setName(self, name):
1130 self.name = name
1131
Martin v. Löwis44f86962001-09-05 13:44:54 +00001132# The timer class was contributed by Itamar Shtull-Trauring
1133
Éric Araujo0cdd4452011-07-28 00:28:28 +02001134class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001135 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +00001136
Georg Brandlc30b59f2013-10-13 10:43:59 +02001137 t = Timer(30.0, f, args=None, kwargs=None)
1138 t.start()
1139 t.cancel() # stop the timer's action if it's still waiting
1140
Martin v. Löwis44f86962001-09-05 13:44:54 +00001141 """
Tim Petersb64bec32001-09-18 02:26:39 +00001142
R David Murray19aeb432013-03-30 17:19:38 -04001143 def __init__(self, interval, function, args=None, kwargs=None):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001144 Thread.__init__(self)
1145 self.interval = interval
1146 self.function = function
R David Murray19aeb432013-03-30 17:19:38 -04001147 self.args = args if args is not None else []
1148 self.kwargs = kwargs if kwargs is not None else {}
Martin v. Löwis44f86962001-09-05 13:44:54 +00001149 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +00001150
Martin v. Löwis44f86962001-09-05 13:44:54 +00001151 def cancel(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001152 """Stop the timer if it hasn't finished yet."""
Martin v. Löwis44f86962001-09-05 13:44:54 +00001153 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +00001154
Martin v. Löwis44f86962001-09-05 13:44:54 +00001155 def run(self):
1156 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +00001157 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +00001158 self.function(*self.args, **self.kwargs)
1159 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001160
Antoine Pitrou1023dbb2017-10-02 16:42:15 +02001161
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001162# Special thread class to represent the main thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001163
1164class _MainThread(Thread):
1165
1166 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001167 Thread.__init__(self, name="MainThread", daemon=False)
Tim Petersc363a232013-09-08 18:44:40 -05001168 self._set_tstate_lock()
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001169 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001170 self._set_ident()
1171 with _active_limbo_lock:
1172 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001173
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001174
1175# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +00001176# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001177# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +00001178# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001179# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001180# They are marked as daemon threads so we won't wait for them
1181# when we exit (conform previous semantics).
1182
1183class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +00001184
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001185 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001186 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +00001187
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001188 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001189 self._set_ident()
1190 with _active_limbo_lock:
1191 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001192
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +02001193 def _stop(self):
1194 pass
1195
Xiang Zhangf3a9fab2017-02-27 11:01:30 +08001196 def is_alive(self):
1197 assert not self._is_stopped and self._started.is_set()
1198 return True
1199
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001200 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001201 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001202
1203
1204# Global API functions
1205
Benjamin Peterson672b8032008-06-11 19:14:14 +00001206def current_thread():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001207 """Return the current Thread object, corresponding to the caller's thread of control.
1208
1209 If the caller's thread of control was not created through the threading
1210 module, a dummy thread object with limited functionality is returned.
1211
1212 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001213 try:
Victor Stinner2a129742011-05-30 23:02:52 +02001214 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001215 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001216 return _DummyThread()
1217
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001218currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001219
Benjamin Peterson672b8032008-06-11 19:14:14 +00001220def active_count():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001221 """Return the number of Thread objects currently alive.
1222
1223 The returned count is equal to the length of the list returned by
1224 enumerate().
1225
1226 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001227 with _active_limbo_lock:
1228 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001229
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001230activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001231
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001232def _enumerate():
1233 # Same as enumerate(), but without the lock. Internal use only.
1234 return list(_active.values()) + list(_limbo.values())
1235
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001236def enumerate():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001237 """Return a list of all Thread objects currently alive.
1238
1239 The list includes daemonic threads, dummy thread objects created by
1240 current_thread(), and the main thread. It excludes terminated threads and
1241 threads that have not yet been started.
1242
1243 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001244 with _active_limbo_lock:
1245 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001246
Georg Brandl2067bfd2008-05-25 13:05:15 +00001247from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001248
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001249# Create the main thread object,
1250# and make it available for the interpreter
1251# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001252
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001253_main_thread = _MainThread()
1254
1255def _shutdown():
Tim Petersc363a232013-09-08 18:44:40 -05001256 # Obscure: other threads may be waiting to join _main_thread. That's
1257 # dubious, but some code does it. We can't wait for C code to release
1258 # the main thread's tstate_lock - that won't happen until the interpreter
1259 # is nearly dead. So we release it here. Note that just calling _stop()
1260 # isn't enough: other threads may already be waiting on _tstate_lock.
Antoine Pitrouee84a602017-08-16 20:53:28 +02001261 if _main_thread._is_stopped:
1262 # _shutdown() was already called
1263 return
Tim Petersb5e9ac92013-09-09 14:41:50 -05001264 tlock = _main_thread._tstate_lock
1265 # The main thread isn't finished yet, so its thread state lock can't have
1266 # been released.
1267 assert tlock is not None
1268 assert tlock.locked()
1269 tlock.release()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001270 _main_thread._stop()
1271 t = _pickSomeNonDaemonThread()
1272 while t:
1273 t.join()
1274 t = _pickSomeNonDaemonThread()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001275
1276def _pickSomeNonDaemonThread():
1277 for t in enumerate():
1278 if not t.daemon and t.is_alive():
1279 return t
1280 return None
1281
1282def main_thread():
Andrew Svetlovb1dd5572013-09-04 10:33:11 +03001283 """Return the main thread object.
1284
1285 In normal conditions, the main thread is the thread from which the
1286 Python interpreter was started.
1287 """
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001288 return _main_thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001289
Jim Fultond15dc062004-07-14 19:11:50 +00001290# get thread-local implementation, either from the thread
1291# module, or from the python fallback
1292
1293try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001294 from _thread import _local as local
Brett Cannoncd171c82013-07-04 17:43:24 -04001295except ImportError:
Jim Fultond15dc062004-07-14 19:11:50 +00001296 from _threading_local import local
1297
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001298
Jesse Nollera8513972008-07-17 16:49:17 +00001299def _after_fork():
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001300 """
1301 Cleanup threading module state that should not exist after a fork.
1302 """
Jesse Nollera8513972008-07-17 16:49:17 +00001303 # Reset _active_limbo_lock, in case we forked while the lock was held
1304 # by another (non-forked) thread. http://bugs.python.org/issue874900
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001305 global _active_limbo_lock, _main_thread
Jesse Nollera8513972008-07-17 16:49:17 +00001306 _active_limbo_lock = _allocate_lock()
1307
1308 # fork() only copied the current thread; clear references to others.
1309 new_active = {}
1310 current = current_thread()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001311 _main_thread = current
Jesse Nollera8513972008-07-17 16:49:17 +00001312 with _active_limbo_lock:
Antoine Pitrou5da7e792013-09-08 13:19:06 +02001313 # Dangling thread instances must still have their locks reset,
1314 # because someone may join() them.
1315 threads = set(_enumerate())
1316 threads.update(_dangling)
1317 for thread in threads:
Charles-François Natalib055bf62011-12-18 18:45:16 +01001318 # Any lock/condition variable may be currently locked or in an
1319 # invalid state, so we reinitialize them.
Jesse Nollera8513972008-07-17 16:49:17 +00001320 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001321 # There is only one active thread. We reset the ident to
1322 # its new value since it can have changed.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001323 thread._reset_internal_locks(True)
Victor Stinner2a129742011-05-30 23:02:52 +02001324 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001325 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +00001326 new_active[ident] = thread
1327 else:
1328 # All the others are already stopped.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001329 thread._reset_internal_locks(False)
Charles-François Natalib055bf62011-12-18 18:45:16 +01001330 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +00001331
1332 _limbo.clear()
1333 _active.clear()
1334 _active.update(new_active)
1335 assert len(_active) == 1
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001336
1337
Gregory P. Smith163468a2017-05-29 10:03:41 -07001338if hasattr(_os, "register_at_fork"):
1339 _os.register_at_fork(after_in_child=_after_fork)