blob: 24cc911c203c26262099b1b51bcfc087425c00ca [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
Fred Drakea8725952002-12-30 23:32:50 +00003import sys as _sys
Georg Brandl2067bfd2008-05-25 13:05:15 +00004import _thread
Fred Drakea8725952002-12-30 23:32:50 +00005
Victor Stinnerae586492014-09-02 23:18:25 +02006from time import monotonic as _time
Neil Schemenauerf607fc52003-11-05 23:03:00 +00007from traceback import format_exc as _format_exc
Antoine Pitrouc081c0c2011-07-15 22:12:24 +02008from _weakrefset import WeakSet
R David Murrayb186f1df2014-10-04 17:43:54 -04009from itertools import islice as _islice, count as _count
Raymond Hettingerec4b1742013-03-10 17:57:28 -070010try:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070011 from _collections import deque as _deque
Brett Cannoncd171c82013-07-04 17:43:24 -040012except ImportError:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070013 from collections import deque as _deque
Guido van Rossum7f5013a1998-04-09 22:01:42 +000014
Benjamin Petersonb3085c92008-09-01 23:09:31 +000015# Note regarding PEP 8 compliant names
16# This threading model was originally inspired by Java, and inherited
17# the convention of camelCase function and method names from that
Ezio Melotti30b9d5d2013-08-17 15:50:46 +030018# language. Those original names are not in any imminent danger of
Benjamin Petersonb3085c92008-09-01 23:09:31 +000019# being deprecated (even for Py3k),so this module provides them as an
20# alias for the PEP 8 compliant names
21# Note that using the new PEP 8 compliant names facilitates substitution
22# with the multiprocessing module, which doesn't provide the old
23# Java inspired names.
24
Benjamin Peterson672b8032008-06-11 19:14:14 +000025__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000026 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
Benjamin Peterson7761b952011-08-02 13:05:47 -050027 'Timer', 'ThreadError', 'setprofile', 'settrace', 'local', 'stack_size']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000028
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000029# Rename some stuff so "from threading import *" is safe
Georg Brandl2067bfd2008-05-25 13:05:15 +000030_start_new_thread = _thread.start_new_thread
31_allocate_lock = _thread.allocate_lock
Antoine Pitrou7b476992013-09-07 23:38:37 +020032_set_sentinel = _thread._set_sentinel
Victor Stinner2a129742011-05-30 23:02:52 +020033get_ident = _thread.get_ident
Georg Brandl2067bfd2008-05-25 13:05:15 +000034ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000035try:
36 _CRLock = _thread.RLock
37except AttributeError:
38 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000039TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000040del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000041
Guido van Rossum7f5013a1998-04-09 22:01:42 +000042
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000043# Support for profile and trace hooks
44
45_profile_hook = None
46_trace_hook = None
47
48def setprofile(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020049 """Set a profile function for all threads started from the threading module.
50
51 The func will be passed to sys.setprofile() for each thread, before its
52 run() method is called.
53
54 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000055 global _profile_hook
56 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000057
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000058def settrace(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020059 """Set a trace function for all threads started from the threading module.
60
61 The func will be passed to sys.settrace() for each thread, before its run()
62 method is called.
63
64 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000065 global _trace_hook
66 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000067
68# Synchronization classes
69
70Lock = _allocate_lock
71
Victor Stinner135b6d82012-03-03 01:32:57 +010072def RLock(*args, **kwargs):
Georg Brandlc30b59f2013-10-13 10:43:59 +020073 """Factory function that returns a new reentrant lock.
74
75 A reentrant lock must be released by the thread that acquired it. Once a
76 thread has acquired a reentrant lock, the same thread may acquire it again
77 without blocking; the thread must release it once for each time it has
78 acquired it.
79
80 """
Victor Stinner135b6d82012-03-03 01:32:57 +010081 if _CRLock is None:
82 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000083 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000084
Victor Stinner135b6d82012-03-03 01:32:57 +010085class _RLock:
Georg Brandlc30b59f2013-10-13 10:43:59 +020086 """This class implements reentrant lock objects.
87
88 A reentrant lock must be released by the thread that acquired it. Once a
89 thread has acquired a reentrant lock, the same thread may acquire it
90 again without blocking; the thread must release it once for each time it
91 has acquired it.
92
93 """
Tim Petersb90f89a2001-01-15 03:26:36 +000094
Victor Stinner135b6d82012-03-03 01:32:57 +010095 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000096 self._block = _allocate_lock()
97 self._owner = None
98 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +000099
100 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000101 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000102 try:
103 owner = _active[owner].name
104 except KeyError:
105 pass
Raymond Hettinger62f4dad2014-05-25 18:22:35 -0700106 return "<%s %s.%s object owner=%r count=%d at %s>" % (
107 "locked" if self._block.locked() else "unlocked",
108 self.__class__.__module__,
109 self.__class__.__qualname__,
110 owner,
111 self._count,
112 hex(id(self))
113 )
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000114
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000115 def acquire(self, blocking=True, timeout=-1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200116 """Acquire a lock, blocking or non-blocking.
117
118 When invoked without arguments: if this thread already owns the lock,
119 increment the recursion level by one, and return immediately. Otherwise,
120 if another thread owns the lock, block until the lock is unlocked. Once
121 the lock is unlocked (not owned by any thread), then grab ownership, set
122 the recursion level to one, and return. If more than one thread is
123 blocked waiting until the lock is unlocked, only one at a time will be
124 able to grab ownership of the lock. There is no return value in this
125 case.
126
127 When invoked with the blocking argument set to true, do the same thing
128 as when called without arguments, and return true.
129
130 When invoked with the blocking argument set to false, do not block. If a
131 call without an argument would block, return false immediately;
132 otherwise, do the same thing as when called without arguments, and
133 return true.
134
135 When invoked with the floating-point timeout argument set to a positive
136 value, block for at most the number of seconds specified by timeout
137 and as long as the lock cannot be acquired. Return true if the lock has
138 been acquired, false if the timeout has elapsed.
139
140 """
Victor Stinner2a129742011-05-30 23:02:52 +0200141 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +0000142 if self._owner == me:
Raymond Hettinger720da572013-03-10 15:13:35 -0700143 self._count += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000144 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000145 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000146 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000147 self._owner = me
148 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000149 return rc
150
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000151 __enter__ = acquire
152
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000153 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200154 """Release a lock, decrementing the recursion level.
155
156 If after the decrement it is zero, reset the lock to unlocked (not owned
157 by any thread), and if any other threads are blocked waiting for the
158 lock to become unlocked, allow exactly one of them to proceed. If after
159 the decrement the recursion level is still nonzero, the lock remains
160 locked and owned by the calling thread.
161
162 Only call this method when the calling thread owns the lock. A
163 RuntimeError is raised if this method is called when the lock is
164 unlocked.
165
166 There is no return value.
167
168 """
Victor Stinner2a129742011-05-30 23:02:52 +0200169 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000170 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000171 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000172 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000173 self._owner = None
174 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000175
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000176 def __exit__(self, t, v, tb):
177 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000178
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000179 # Internal methods used by condition variables
180
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000181 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000182 self._block.acquire()
183 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000184
185 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200186 if self._count == 0:
187 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000188 count = self._count
189 self._count = 0
190 owner = self._owner
191 self._owner = None
192 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000193 return (count, owner)
194
195 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200196 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000197
Antoine Pitrou434736a2009-11-10 18:46:01 +0000198_PyRLock = _RLock
199
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000200
Victor Stinner135b6d82012-03-03 01:32:57 +0100201class Condition:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200202 """Class that implements a condition variable.
203
204 A condition variable allows one or more threads to wait until they are
205 notified by another thread.
206
207 If the lock argument is given and not None, it must be a Lock or RLock
208 object, and it is used as the underlying lock. Otherwise, a new RLock object
209 is created and used as the underlying lock.
210
211 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000212
Victor Stinner135b6d82012-03-03 01:32:57 +0100213 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000214 if lock is None:
215 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000216 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000217 # Export the lock's acquire() and release() methods
218 self.acquire = lock.acquire
219 self.release = lock.release
220 # If the lock defines _release_save() and/or _acquire_restore(),
221 # these override the default implementations (which just call
222 # release() and acquire() on the lock). Ditto for _is_owned().
223 try:
224 self._release_save = lock._release_save
225 except AttributeError:
226 pass
227 try:
228 self._acquire_restore = lock._acquire_restore
229 except AttributeError:
230 pass
231 try:
232 self._is_owned = lock._is_owned
233 except AttributeError:
234 pass
Raymond Hettingerec4b1742013-03-10 17:57:28 -0700235 self._waiters = _deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000236
Thomas Wouters477c8d52006-05-27 19:21:47 +0000237 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000238 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000239
Thomas Wouters477c8d52006-05-27 19:21:47 +0000240 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000241 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000242
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000243 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000244 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000245
246 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000247 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000248
249 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000250 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000251
252 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000253 # Return True if lock is owned by current_thread.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300254 # This method is called only if _lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000255 if self._lock.acquire(0):
256 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000257 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000258 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000259 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000260
261 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200262 """Wait until notified or until a timeout occurs.
263
264 If the calling thread has not acquired the lock when this method is
265 called, a RuntimeError is raised.
266
267 This method releases the underlying lock, and then blocks until it is
268 awakened by a notify() or notify_all() call for the same condition
269 variable in another thread, or until the optional timeout occurs. Once
270 awakened or timed out, it re-acquires the lock and returns.
271
272 When the timeout argument is present and not None, it should be a
273 floating point number specifying a timeout for the operation in seconds
274 (or fractions thereof).
275
276 When the underlying lock is an RLock, it is not released using its
277 release() method, since this may not actually unlock the lock when it
278 was acquired multiple times recursively. Instead, an internal interface
279 of the RLock class is used, which really unlocks it even when it has
280 been recursively acquired several times. Another internal interface is
281 then used to restore the recursion level when the lock is reacquired.
282
283 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000284 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000285 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000286 waiter = _allocate_lock()
287 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000288 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000289 saved_state = self._release_save()
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200290 gotit = False
Tim Petersc951bf92001-04-02 20:15:57 +0000291 try: # restore state no matter what (e.g., KeyboardInterrupt)
292 if timeout is None:
293 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000294 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000295 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000296 if timeout > 0:
297 gotit = waiter.acquire(True, timeout)
298 else:
299 gotit = waiter.acquire(False)
Georg Brandlb9a43912010-10-28 09:03:20 +0000300 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000301 finally:
302 self._acquire_restore(saved_state)
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200303 if not gotit:
304 try:
305 self._waiters.remove(waiter)
306 except ValueError:
307 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000308
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000309 def wait_for(self, predicate, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200310 """Wait until a condition evaluates to True.
311
312 predicate should be a callable which result will be interpreted as a
313 boolean value. A timeout may be provided giving the maximum time to
314 wait.
315
316 """
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000317 endtime = None
318 waittime = timeout
319 result = predicate()
320 while not result:
321 if waittime is not None:
322 if endtime is None:
323 endtime = _time() + waittime
324 else:
325 waittime = endtime - _time()
326 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000327 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000328 self.wait(waittime)
329 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000330 return result
331
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000332 def notify(self, n=1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200333 """Wake up one or more threads waiting on this condition, if any.
334
335 If the calling thread has not acquired the lock when this method is
336 called, a RuntimeError is raised.
337
338 This method wakes up at most n of the threads waiting for the condition
339 variable; it is a no-op if no threads are waiting.
340
341 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000342 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000343 raise RuntimeError("cannot notify on un-acquired lock")
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700344 all_waiters = self._waiters
345 waiters_to_notify = _deque(_islice(all_waiters, n))
346 if not waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000347 return
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700348 for waiter in waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000349 waiter.release()
350 try:
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700351 all_waiters.remove(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000352 except ValueError:
353 pass
354
Benjamin Peterson672b8032008-06-11 19:14:14 +0000355 def notify_all(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200356 """Wake up all threads waiting on this condition.
357
358 If the calling thread has not acquired the lock when this method
359 is called, a RuntimeError is raised.
360
361 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000362 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000363
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000364 notifyAll = notify_all
365
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000366
Victor Stinner135b6d82012-03-03 01:32:57 +0100367class Semaphore:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200368 """This class implements semaphore objects.
369
370 Semaphores manage a counter representing the number of release() calls minus
371 the number of acquire() calls, plus an initial value. The acquire() method
372 blocks if necessary until it can return without making the counter
373 negative. If not given, value defaults to 1.
374
375 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000376
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000377 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000378
Victor Stinner135b6d82012-03-03 01:32:57 +0100379 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000380 if value < 0:
381 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000382 self._cond = Condition(Lock())
383 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000384
Antoine Pitrou0454af92010-04-17 23:51:58 +0000385 def acquire(self, blocking=True, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200386 """Acquire a semaphore, decrementing the internal counter by one.
387
388 When invoked without arguments: if the internal counter is larger than
389 zero on entry, decrement it by one and return immediately. If it is zero
390 on entry, block, waiting until some other thread has called release() to
391 make it larger than zero. This is done with proper interlocking so that
392 if multiple acquire() calls are blocked, release() will wake exactly one
393 of them up. The implementation may pick one at random, so the order in
394 which blocked threads are awakened should not be relied on. There is no
395 return value in this case.
396
397 When invoked with blocking set to true, do the same thing as when called
398 without arguments, and return true.
399
400 When invoked with blocking set to false, do not block. If a call without
401 an argument would block, return false immediately; otherwise, do the
402 same thing as when called without arguments, and return true.
403
404 When invoked with a timeout other than None, it will block for at
405 most timeout seconds. If acquire does not complete successfully in
406 that interval, return false. Return true otherwise.
407
408 """
Antoine Pitrou0454af92010-04-17 23:51:58 +0000409 if not blocking and timeout is not None:
410 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000411 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000412 endtime = None
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300413 with self._cond:
414 while self._value == 0:
415 if not blocking:
416 break
417 if timeout is not None:
418 if endtime is None:
419 endtime = _time() + timeout
420 else:
421 timeout = endtime - _time()
422 if timeout <= 0:
423 break
424 self._cond.wait(timeout)
425 else:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300426 self._value -= 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300427 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000428 return rc
429
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000430 __enter__ = acquire
431
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000432 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200433 """Release a semaphore, incrementing the internal counter by one.
434
435 When the counter is zero on entry and another thread is waiting for it
436 to become larger than zero again, wake up that thread.
437
438 """
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300439 with self._cond:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300440 self._value += 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300441 self._cond.notify()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000442
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000443 def __exit__(self, t, v, tb):
444 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000445
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000446
Éric Araujo0cdd4452011-07-28 00:28:28 +0200447class BoundedSemaphore(Semaphore):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200448 """Implements a bounded semaphore.
449
450 A bounded semaphore checks to make sure its current value doesn't exceed its
451 initial value. If it does, ValueError is raised. In most situations
452 semaphores are used to guard resources with limited capacity.
453
454 If the semaphore is released too many times it's a sign of a bug. If not
455 given, value defaults to 1.
456
457 Like regular semaphores, bounded semaphores manage a counter representing
458 the number of release() calls minus the number of acquire() calls, plus an
459 initial value. The acquire() method blocks if necessary until it can return
460 without making the counter negative. If not given, value defaults to 1.
461
462 """
463
Victor Stinner135b6d82012-03-03 01:32:57 +0100464 def __init__(self, value=1):
465 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000466 self._initial_value = value
467
468 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200469 """Release a semaphore, incrementing the internal counter by one.
470
471 When the counter is zero on entry and another thread is waiting for it
472 to become larger than zero again, wake up that thread.
473
474 If the number of releases exceeds the number of acquires,
475 raise a ValueError.
476
477 """
Tim Peters7634e1c2013-10-08 20:55:51 -0500478 with self._cond:
479 if self._value >= self._initial_value:
480 raise ValueError("Semaphore released too many times")
481 self._value += 1
482 self._cond.notify()
Skip Montanaroe428bb72001-08-20 20:27:58 +0000483
484
Victor Stinner135b6d82012-03-03 01:32:57 +0100485class Event:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200486 """Class implementing event objects.
487
488 Events manage a flag that can be set to true with the set() method and reset
489 to false with the clear() method. The wait() method blocks until the flag is
490 true. The flag is initially false.
491
492 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000493
494 # After Tim Peters' event class (without is_posted())
495
Victor Stinner135b6d82012-03-03 01:32:57 +0100496 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000497 self._cond = Condition(Lock())
498 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000499
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000500 def _reset_internal_locks(self):
501 # private! called by Thread._reset_internal_locks by _after_fork()
502 self._cond.__init__()
503
Benjamin Peterson672b8032008-06-11 19:14:14 +0000504 def is_set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200505 """Return true if and only if the internal flag is true."""
Guido van Rossumd0648992007-08-20 19:25:41 +0000506 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000507
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000508 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000509
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000510 def set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200511 """Set the internal flag to true.
512
513 All threads waiting for it to become true are awakened. Threads
514 that call wait() once the flag is true will not block at all.
515
516 """
Christian Heimes969fe572008-01-25 11:23:10 +0000517 self._cond.acquire()
518 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000519 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000520 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000521 finally:
522 self._cond.release()
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 """
Christian Heimes969fe572008-01-25 11:23:10 +0000531 self._cond.acquire()
532 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000533 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000534 finally:
535 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000536
537 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200538 """Block until the internal flag is true.
539
540 If the internal flag is true on entry, return immediately. Otherwise,
541 block until another thread calls set() to set the flag to true, or until
542 the optional timeout occurs.
543
544 When the timeout argument is present and not None, it should be a
545 floating point number specifying a timeout for the operation in seconds
546 (or fractions thereof).
547
548 This method returns the internal flag on exit, so it will always return
549 True except if a timeout is given and the operation times out.
550
551 """
Christian Heimes969fe572008-01-25 11:23:10 +0000552 self._cond.acquire()
553 try:
Charles-François Natalided03482012-01-07 18:24:56 +0100554 signaled = self._flag
555 if not signaled:
556 signaled = self._cond.wait(timeout)
557 return signaled
Christian Heimes969fe572008-01-25 11:23:10 +0000558 finally:
559 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000560
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000561
562# A barrier class. Inspired in part by the pthread_barrier_* api and
563# the CyclicBarrier class from Java. See
564# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
565# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
566# CyclicBarrier.html
567# for information.
568# We maintain two main states, 'filling' and 'draining' enabling the barrier
569# to be cyclic. Threads are not allowed into it until it has fully drained
570# since the previous cycle. In addition, a 'resetting' state exists which is
571# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300572# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100573class Barrier:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200574 """Implements a Barrier.
575
576 Useful for synchronizing a fixed number of threads at known synchronization
577 points. Threads block on 'wait()' and are simultaneously once they have all
578 made that call.
579
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000580 """
Georg Brandlc30b59f2013-10-13 10:43:59 +0200581
Victor Stinner135b6d82012-03-03 01:32:57 +0100582 def __init__(self, parties, action=None, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200583 """Create a barrier, initialised to 'parties' threads.
584
585 'action' is a callable which, when supplied, will be called by one of
586 the threads after they have all entered the barrier and just prior to
587 releasing them all. If a 'timeout' is provided, it is uses as the
588 default for all subsequent 'wait()' calls.
589
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000590 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000591 self._cond = Condition(Lock())
592 self._action = action
593 self._timeout = timeout
594 self._parties = parties
595 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
596 self._count = 0
597
598 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200599 """Wait for the barrier.
600
601 When the specified number of threads have started waiting, they are all
602 simultaneously awoken. If an 'action' was provided for the barrier, one
603 of the threads will have executed that callback prior to returning.
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000604 Returns an individual index number from 0 to 'parties-1'.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200605
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000606 """
607 if timeout is None:
608 timeout = self._timeout
609 with self._cond:
610 self._enter() # Block while the barrier drains.
611 index = self._count
612 self._count += 1
613 try:
614 if index + 1 == self._parties:
615 # We release the barrier
616 self._release()
617 else:
618 # We wait until someone releases us
619 self._wait(timeout)
620 return index
621 finally:
622 self._count -= 1
623 # Wake up any threads waiting for barrier to drain.
624 self._exit()
625
626 # Block until the barrier is ready for us, or raise an exception
627 # if it is broken.
628 def _enter(self):
629 while self._state in (-1, 1):
630 # It is draining or resetting, wait until done
631 self._cond.wait()
632 #see if the barrier is in a broken state
633 if self._state < 0:
634 raise BrokenBarrierError
635 assert self._state == 0
636
637 # Optionally run the 'action' and release the threads waiting
638 # in the barrier.
639 def _release(self):
640 try:
641 if self._action:
642 self._action()
643 # enter draining state
644 self._state = 1
645 self._cond.notify_all()
646 except:
647 #an exception during the _action handler. Break and reraise
648 self._break()
649 raise
650
651 # Wait in the barrier until we are relased. Raise an exception
652 # if the barrier is reset or broken.
653 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000654 if not self._cond.wait_for(lambda : self._state != 0, timeout):
655 #timed out. Break the barrier
656 self._break()
657 raise BrokenBarrierError
658 if self._state < 0:
659 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000660 assert self._state == 1
661
662 # If we are the last thread to exit the barrier, signal any threads
663 # waiting for the barrier to drain.
664 def _exit(self):
665 if self._count == 0:
666 if self._state in (-1, 1):
667 #resetting or draining
668 self._state = 0
669 self._cond.notify_all()
670
671 def reset(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200672 """Reset the barrier to the initial state.
673
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000674 Any threads currently waiting will get the BrokenBarrier exception
675 raised.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200676
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000677 """
678 with self._cond:
679 if self._count > 0:
680 if self._state == 0:
681 #reset the barrier, waking up threads
682 self._state = -1
683 elif self._state == -2:
684 #was broken, set it to reset state
685 #which clears when the last thread exits
686 self._state = -1
687 else:
688 self._state = 0
689 self._cond.notify_all()
690
691 def abort(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200692 """Place the barrier into a 'broken' state.
693
694 Useful in case of error. Any currently waiting threads and threads
695 attempting to 'wait()' will have BrokenBarrierError raised.
696
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000697 """
698 with self._cond:
699 self._break()
700
701 def _break(self):
702 # An internal error was detected. The barrier is set to
703 # a broken state all parties awakened.
704 self._state = -2
705 self._cond.notify_all()
706
707 @property
708 def parties(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200709 """Return the number of threads required to trip the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000710 return self._parties
711
712 @property
713 def n_waiting(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200714 """Return the number of threads currently waiting at the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000715 # We don't need synchronization here since this is an ephemeral result
716 # anyway. It returns the correct value in the steady state.
717 if self._state == 0:
718 return self._count
719 return 0
720
721 @property
722 def broken(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200723 """Return True if the barrier is in a broken state."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000724 return self._state == -2
725
Georg Brandlc30b59f2013-10-13 10:43:59 +0200726# exception raised by the Barrier class
727class BrokenBarrierError(RuntimeError):
728 pass
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000729
730
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000731# Helper to generate new thread names
R David Murrayb186f1df2014-10-04 17:43:54 -0400732_counter = _count().__next__
733_counter() # Consume 0 so first non-main thread has id 1.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000734def _newname(template="Thread-%d"):
R David Murrayb186f1df2014-10-04 17:43:54 -0400735 return template % _counter()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000736
737# Active thread administration
738_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000739_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000740_limbo = {}
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200741_dangling = WeakSet()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000742
743# Main class for threads
744
Victor Stinner135b6d82012-03-03 01:32:57 +0100745class Thread:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200746 """A class that represents a thread of control.
747
748 This class can be safely subclassed in a limited fashion. There are two ways
749 to specify the activity: by passing a callable object to the constructor, or
750 by overriding the run() method in a subclass.
751
752 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000753
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300754 _initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000755 # Need to store a reference to sys.exc_info for printing
756 # out exceptions when a thread tries to use a global var. during interp.
757 # shutdown and thus raises an exception about trying to perform some
758 # operation on/with a NoneType
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300759 _exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000760 # Keep sys.exc_clear too to clear the exception just before
761 # allowing .join() to return.
762 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000763
764 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100765 args=(), kwargs=None, *, daemon=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200766 """This constructor should always be called with keyword arguments. Arguments are:
767
768 *group* should be None; reserved for future extension when a ThreadGroup
769 class is implemented.
770
771 *target* is the callable object to be invoked by the run()
772 method. Defaults to None, meaning nothing is called.
773
774 *name* is the thread name. By default, a unique name is constructed of
775 the form "Thread-N" where N is a small decimal number.
776
777 *args* is the argument tuple for the target invocation. Defaults to ().
778
779 *kwargs* is a dictionary of keyword arguments for the target
780 invocation. Defaults to {}.
781
782 If a subclass overrides the constructor, it must make sure to invoke
783 the base class constructor (Thread.__init__()) before doing anything
784 else to the thread.
785
786 """
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000787 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000788 if kwargs is None:
789 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000790 self._target = target
791 self._name = str(name or _newname())
792 self._args = args
793 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000794 if daemon is not None:
795 self._daemonic = daemon
796 else:
797 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000798 self._ident = None
Antoine Pitrou7b476992013-09-07 23:38:37 +0200799 self._tstate_lock = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000800 self._started = Event()
Tim Petersc363a232013-09-08 18:44:40 -0500801 self._is_stopped = False
Guido van Rossumd0648992007-08-20 19:25:41 +0000802 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000803 # sys.stderr is not stored in the class like
804 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000805 self._stderr = _sys.stderr
Antoine Pitrou5da7e792013-09-08 13:19:06 +0200806 # For debugging and _after_fork()
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200807 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000808
Antoine Pitrou7b476992013-09-07 23:38:37 +0200809 def _reset_internal_locks(self, is_alive):
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000810 # private! Called by _after_fork() to reset our internal locks as
811 # they may be in an invalid state leading to a deadlock or crash.
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000812 self._started._reset_internal_locks()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200813 if is_alive:
814 self._set_tstate_lock()
815 else:
816 # The thread isn't alive after fork: it doesn't have a tstate
817 # anymore.
Tim Petersb5e9ac92013-09-09 14:41:50 -0500818 self._is_stopped = True
Antoine Pitrou7b476992013-09-07 23:38:37 +0200819 self._tstate_lock = None
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000820
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000821 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000822 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000823 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000824 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000825 status = "started"
Tim Peters72460fa2013-09-09 18:48:24 -0500826 self.is_alive() # easy way to get ._is_stopped set when appropriate
Tim Petersc363a232013-09-08 18:44:40 -0500827 if self._is_stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000828 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000829 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000830 status += " daemon"
831 if self._ident is not None:
832 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000833 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000834
835 def start(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200836 """Start the thread's activity.
837
838 It must be called at most once per thread object. It arranges for the
839 object's run() method to be invoked in a separate thread of control.
840
841 This method will raise a RuntimeError if called more than once on the
842 same thread object.
843
844 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000845 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000846 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000847
Benjamin Peterson672b8032008-06-11 19:14:14 +0000848 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000849 raise RuntimeError("threads can only be started once")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000850 with _active_limbo_lock:
851 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000852 try:
853 _start_new_thread(self._bootstrap, ())
854 except Exception:
855 with _active_limbo_lock:
856 del _limbo[self]
857 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000858 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000859
860 def run(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200861 """Method representing the thread's activity.
862
863 You may override this method in a subclass. The standard run() method
864 invokes the callable object passed to the object's constructor as the
865 target argument, if any, with sequential and keyword arguments taken
866 from the args and kwargs arguments, respectively.
867
868 """
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000869 try:
870 if self._target:
871 self._target(*self._args, **self._kwargs)
872 finally:
873 # Avoid a refcycle if the thread is running a function with
874 # an argument that has a member that points to the thread.
875 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000876
Guido van Rossumd0648992007-08-20 19:25:41 +0000877 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000878 # Wrapper around the real bootstrap code that ignores
879 # exceptions during interpreter cleanup. Those typically
880 # happen when a daemon thread wakes up at an unfortunate
881 # moment, finds the world around it destroyed, and raises some
882 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000883 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000884 # don't help anybody, and they confuse users, so we suppress
885 # them. We suppress them only when it appears that the world
886 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000887 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000888 # reported. Also, we only suppress them for daemonic threads;
889 # if a non-daemonic encounters this, something else is wrong.
890 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000891 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000892 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000893 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000894 return
895 raise
896
Benjamin Petersond23f8222009-04-05 19:13:16 +0000897 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200898 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000899
Antoine Pitrou7b476992013-09-07 23:38:37 +0200900 def _set_tstate_lock(self):
901 """
902 Set a lock object which will be released by the interpreter when
903 the underlying thread state (see pystate.h) gets deleted.
904 """
905 self._tstate_lock = _set_sentinel()
906 self._tstate_lock.acquire()
907
Guido van Rossumd0648992007-08-20 19:25:41 +0000908 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000909 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000910 self._set_ident()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200911 self._set_tstate_lock()
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" %
933 (self.name, _format_exc()), file=self._stderr)
934 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)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000953 # Make sure that exc_tb gets deleted since it is a memory
954 # hog; deleting everything else is just for thoroughness
955 finally:
956 del exc_type, exc_value, exc_tb
Christian Heimesbbe741d2008-03-28 10:53:29 +0000957 finally:
958 # Prevent a race in
959 # test_threading.test_no_refcycle_through_target when
960 # the exception keeps the target alive past when we
961 # assert that it's dead.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300962 #XXX self._exc_clear()
Christian Heimesbbe741d2008-03-28 10:53:29 +0000963 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000964 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000965 with _active_limbo_lock:
Christian Heimes1af737c2008-01-23 08:24:23 +0000966 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000967 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000968 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200969 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000970 except:
971 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000972
Guido van Rossumd0648992007-08-20 19:25:41 +0000973 def _stop(self):
Tim Petersb5e9ac92013-09-09 14:41:50 -0500974 # After calling ._stop(), .is_alive() returns False and .join() returns
975 # immediately. ._tstate_lock must be released before calling ._stop().
976 #
977 # Normal case: C code at the end of the thread's life
978 # (release_sentinel in _threadmodule.c) releases ._tstate_lock, and
979 # that's detected by our ._wait_for_tstate_lock(), called by .join()
980 # and .is_alive(). Any number of threads _may_ call ._stop()
981 # simultaneously (for example, if multiple threads are blocked in
982 # .join() calls), and they're not serialized. That's harmless -
983 # they'll just make redundant rebindings of ._is_stopped and
984 # ._tstate_lock. Obscure: we rebind ._tstate_lock last so that the
985 # "assert self._is_stopped" in ._wait_for_tstate_lock() always works
986 # (the assert is executed only if ._tstate_lock is None).
987 #
988 # Special case: _main_thread releases ._tstate_lock via this
989 # module's _shutdown() function.
990 lock = self._tstate_lock
991 if lock is not None:
992 assert not lock.locked()
Tim Peters78755232013-09-09 13:47:16 -0500993 self._is_stopped = True
994 self._tstate_lock = None
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000995
Guido van Rossumd0648992007-08-20 19:25:41 +0000996 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000997 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000998
Georg Brandl2067bfd2008-05-25 13:05:15 +0000999 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +00001000 #
Georg Brandl2067bfd2008-05-25 13:05:15 +00001001 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +00001002 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +00001003 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
1004 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +00001005 # len(_active) is always <= 1 here, and any Thread instance created
1006 # overwrites the (if any) thread currently registered in _active.
1007 #
1008 # An instance of _MainThread is always created by 'threading'. This
1009 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +00001010 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +00001011 # same key in the dict. So when the _MainThread instance created by
1012 # 'threading' tries to clean itself up when atexit calls this method
1013 # it gets a KeyError if another Thread instance was created.
1014 #
1015 # This all means that KeyError from trying to delete something from
1016 # _active if dummy_threading is being used is a red herring. But
1017 # since it isn't if dummy_threading is *not* being used then don't
1018 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +00001019
Christian Heimes969fe572008-01-25 11:23:10 +00001020 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001021 with _active_limbo_lock:
Victor Stinner2a129742011-05-30 23:02:52 +02001022 del _active[get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001023 # There must not be any python code between the previous line
1024 # and after the lock is released. Otherwise a tracing function
1025 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +00001026 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001027 except KeyError:
1028 if 'dummy_threading' not in _sys.modules:
1029 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001030
1031 def join(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001032 """Wait until the thread terminates.
1033
1034 This blocks the calling thread until the thread whose join() method is
1035 called terminates -- either normally or through an unhandled exception
1036 or until the optional timeout occurs.
1037
1038 When the timeout argument is present and not None, it should be a
1039 floating point number specifying a timeout for the operation in seconds
1040 (or fractions thereof). As join() always returns None, you must call
1041 isAlive() after join() to decide whether a timeout happened -- if the
1042 thread is still alive, the join() call timed out.
1043
1044 When the timeout argument is not present or None, the operation will
1045 block until the thread terminates.
1046
1047 A thread can be join()ed many times.
1048
1049 join() raises a RuntimeError if an attempt is made to join the current
1050 thread as that would cause a deadlock. It is also an error to join() a
1051 thread before it has been started and attempts to do so raises the same
1052 exception.
1053
1054 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001055 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001056 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001057 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001058 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001059 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001060 raise RuntimeError("cannot join current thread")
Tim Peterse5bb0bf2013-10-25 20:46:51 -05001061
Tim Petersc363a232013-09-08 18:44:40 -05001062 if timeout is None:
1063 self._wait_for_tstate_lock()
Tim Peters7bad39f2013-10-25 22:33:52 -05001064 else:
1065 # the behavior of a negative timeout isn't documented, but
Tim Petersa577f1e2013-10-26 11:56:16 -05001066 # historically .join(timeout=x) for x<0 has acted as if timeout=0
Tim Peters7bad39f2013-10-25 22:33:52 -05001067 self._wait_for_tstate_lock(timeout=max(timeout, 0))
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001068
Tim Petersc363a232013-09-08 18:44:40 -05001069 def _wait_for_tstate_lock(self, block=True, timeout=-1):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001070 # Issue #18808: wait for the thread state to be gone.
Tim Petersc363a232013-09-08 18:44:40 -05001071 # At the end of the thread's life, after all knowledge of the thread
1072 # is removed from C data structures, C code releases our _tstate_lock.
1073 # This method passes its arguments to _tstate_lock.aquire().
1074 # If the lock is acquired, the C code is done, and self._stop() is
1075 # called. That sets ._is_stopped to True, and ._tstate_lock to None.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001076 lock = self._tstate_lock
Tim Petersc363a232013-09-08 18:44:40 -05001077 if lock is None: # already determined that the C code is done
1078 assert self._is_stopped
1079 elif lock.acquire(block, timeout):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001080 lock.release()
Tim Petersc363a232013-09-08 18:44:40 -05001081 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001082
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001083 @property
1084 def name(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001085 """A string used for identification purposes only.
1086
1087 It has no semantics. Multiple threads may be given the same name. The
1088 initial name is set by the constructor.
1089
1090 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001091 assert self._initialized, "Thread.__init__() not called"
1092 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001093
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001094 @name.setter
1095 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +00001096 assert self._initialized, "Thread.__init__() not called"
1097 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001098
Benjamin Peterson773c17b2008-08-18 16:45:31 +00001099 @property
1100 def ident(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001101 """Thread identifier of this thread or None if it has not been started.
1102
1103 This is a nonzero integer. See the thread.get_ident() function. Thread
1104 identifiers may be recycled when a thread exits and another thread is
1105 created. The identifier is available even after the thread has exited.
1106
1107 """
Georg Brandl0c77a822008-06-10 16:37:50 +00001108 assert self._initialized, "Thread.__init__() not called"
1109 return self._ident
1110
Benjamin Peterson672b8032008-06-11 19:14:14 +00001111 def is_alive(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001112 """Return whether the thread is alive.
1113
1114 This method returns True just before the run() method starts until just
1115 after the run() method terminates. The module function enumerate()
1116 returns a list of all alive threads.
1117
1118 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001119 assert self._initialized, "Thread.__init__() not called"
Tim Petersc363a232013-09-08 18:44:40 -05001120 if self._is_stopped or not self._started.is_set():
Antoine Pitrou7b476992013-09-07 23:38:37 +02001121 return False
Antoine Pitrou7b476992013-09-07 23:38:37 +02001122 self._wait_for_tstate_lock(False)
Tim Petersc363a232013-09-08 18:44:40 -05001123 return not self._is_stopped
Tim Petersb90f89a2001-01-15 03:26:36 +00001124
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001125 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001126
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001127 @property
1128 def daemon(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001129 """A boolean value indicating whether this thread is a daemon thread.
1130
1131 This must be set before start() is called, otherwise RuntimeError is
1132 raised. Its initial value is inherited from the creating thread; the
1133 main thread is not a daemon thread and therefore all threads created in
1134 the main thread default to daemon = False.
1135
1136 The entire Python program exits when no alive non-daemon threads are
1137 left.
1138
1139 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001140 assert self._initialized, "Thread.__init__() not called"
1141 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001142
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001143 @daemon.setter
1144 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +00001145 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001146 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001147 if self._started.is_set():
Antoine Pitrou10959072014-03-17 18:22:41 +01001148 raise RuntimeError("cannot set daemon status of active thread")
Guido van Rossumd0648992007-08-20 19:25:41 +00001149 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001150
Benjamin Peterson6640d722008-08-18 18:16:46 +00001151 def isDaemon(self):
1152 return self.daemon
1153
1154 def setDaemon(self, daemonic):
1155 self.daemon = daemonic
1156
1157 def getName(self):
1158 return self.name
1159
1160 def setName(self, name):
1161 self.name = name
1162
Martin v. Löwis44f86962001-09-05 13:44:54 +00001163# The timer class was contributed by Itamar Shtull-Trauring
1164
Éric Araujo0cdd4452011-07-28 00:28:28 +02001165class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001166 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +00001167
Georg Brandlc30b59f2013-10-13 10:43:59 +02001168 t = Timer(30.0, f, args=None, kwargs=None)
1169 t.start()
1170 t.cancel() # stop the timer's action if it's still waiting
1171
Martin v. Löwis44f86962001-09-05 13:44:54 +00001172 """
Tim Petersb64bec32001-09-18 02:26:39 +00001173
R David Murray19aeb432013-03-30 17:19:38 -04001174 def __init__(self, interval, function, args=None, kwargs=None):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001175 Thread.__init__(self)
1176 self.interval = interval
1177 self.function = function
R David Murray19aeb432013-03-30 17:19:38 -04001178 self.args = args if args is not None else []
1179 self.kwargs = kwargs if kwargs is not None else {}
Martin v. Löwis44f86962001-09-05 13:44:54 +00001180 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +00001181
Martin v. Löwis44f86962001-09-05 13:44:54 +00001182 def cancel(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001183 """Stop the timer if it hasn't finished yet."""
Martin v. Löwis44f86962001-09-05 13:44:54 +00001184 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +00001185
Martin v. Löwis44f86962001-09-05 13:44:54 +00001186 def run(self):
1187 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +00001188 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +00001189 self.function(*self.args, **self.kwargs)
1190 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001191
1192# Special thread class to represent the main thread
1193# This is garbage collected through an exit handler
1194
1195class _MainThread(Thread):
1196
1197 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001198 Thread.__init__(self, name="MainThread", daemon=False)
Tim Petersc363a232013-09-08 18:44:40 -05001199 self._set_tstate_lock()
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001200 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001201 self._set_ident()
1202 with _active_limbo_lock:
1203 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001204
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001205
1206# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +00001207# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001208# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +00001209# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001210# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001211# They are marked as daemon threads so we won't wait for them
1212# when we exit (conform previous semantics).
1213
1214class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +00001215
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001216 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001217 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +00001218
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001219 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001220 self._set_ident()
1221 with _active_limbo_lock:
1222 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001223
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +02001224 def _stop(self):
1225 pass
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.
Tim Petersb5e9ac92013-09-09 14:41:50 -05001288 tlock = _main_thread._tstate_lock
1289 # The main thread isn't finished yet, so its thread state lock can't have
1290 # been released.
1291 assert tlock is not None
1292 assert tlock.locked()
1293 tlock.release()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001294 _main_thread._stop()
1295 t = _pickSomeNonDaemonThread()
1296 while t:
1297 t.join()
1298 t = _pickSomeNonDaemonThread()
1299 _main_thread._delete()
1300
1301def _pickSomeNonDaemonThread():
1302 for t in enumerate():
1303 if not t.daemon and t.is_alive():
1304 return t
1305 return None
1306
1307def main_thread():
Andrew Svetlovb1dd5572013-09-04 10:33:11 +03001308 """Return the main thread object.
1309
1310 In normal conditions, the main thread is the thread from which the
1311 Python interpreter was started.
1312 """
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001313 return _main_thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001314
Jim Fultond15dc062004-07-14 19:11:50 +00001315# get thread-local implementation, either from the thread
1316# module, or from the python fallback
1317
1318try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001319 from _thread import _local as local
Brett Cannoncd171c82013-07-04 17:43:24 -04001320except ImportError:
Jim Fultond15dc062004-07-14 19:11:50 +00001321 from _threading_local import local
1322
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001323
Jesse Nollera8513972008-07-17 16:49:17 +00001324def _after_fork():
1325 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
1326 # is called from PyOS_AfterFork. Here we cleanup threading module state
1327 # that should not exist after a fork.
1328
1329 # Reset _active_limbo_lock, in case we forked while the lock was held
1330 # by another (non-forked) thread. http://bugs.python.org/issue874900
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001331 global _active_limbo_lock, _main_thread
Jesse Nollera8513972008-07-17 16:49:17 +00001332 _active_limbo_lock = _allocate_lock()
1333
1334 # fork() only copied the current thread; clear references to others.
1335 new_active = {}
1336 current = current_thread()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001337 _main_thread = current
Jesse Nollera8513972008-07-17 16:49:17 +00001338 with _active_limbo_lock:
Antoine Pitrou5da7e792013-09-08 13:19:06 +02001339 # Dangling thread instances must still have their locks reset,
1340 # because someone may join() them.
1341 threads = set(_enumerate())
1342 threads.update(_dangling)
1343 for thread in threads:
Charles-François Natalib055bf62011-12-18 18:45:16 +01001344 # Any lock/condition variable may be currently locked or in an
1345 # invalid state, so we reinitialize them.
Jesse Nollera8513972008-07-17 16:49:17 +00001346 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001347 # There is only one active thread. We reset the ident to
1348 # its new value since it can have changed.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001349 thread._reset_internal_locks(True)
Victor Stinner2a129742011-05-30 23:02:52 +02001350 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001351 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +00001352 new_active[ident] = thread
1353 else:
1354 # All the others are already stopped.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001355 thread._reset_internal_locks(False)
Charles-François Natalib055bf62011-12-18 18:45:16 +01001356 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +00001357
1358 _limbo.clear()
1359 _active.clear()
1360 _active.update(new_active)
1361 assert len(_active) == 1