blob: 80f809ce2fc03da2c74532f91d77e027ecd0eae6 [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 Stinnerec895392012-04-29 02:41:27 +02006try:
7 from time import monotonic as _time
Brett Cannoncd171c82013-07-04 17:43:24 -04008except ImportError:
Victor Stinnerec895392012-04-29 02:41:27 +02009 from time import time as _time
Neil Schemenauerf607fc52003-11-05 23:03:00 +000010from traceback import format_exc as _format_exc
Antoine Pitrouc081c0c2011-07-15 22:12:24 +020011from _weakrefset import WeakSet
R David Murrayb186f1df2014-10-04 17:43:54 -040012from itertools import islice as _islice, count as _count
Raymond Hettingerec4b1742013-03-10 17:57:28 -070013try:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070014 from _collections import deque as _deque
Brett Cannoncd171c82013-07-04 17:43:24 -040015except ImportError:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070016 from collections import deque as _deque
Guido van Rossum7f5013a1998-04-09 22:01:42 +000017
Benjamin Petersonb3085c92008-09-01 23:09:31 +000018# Note regarding PEP 8 compliant names
19# This threading model was originally inspired by Java, and inherited
20# the convention of camelCase function and method names from that
Ezio Melotti30b9d5d2013-08-17 15:50:46 +030021# language. Those original names are not in any imminent danger of
Benjamin Petersonb3085c92008-09-01 23:09:31 +000022# being deprecated (even for Py3k),so this module provides them as an
23# alias for the PEP 8 compliant names
24# Note that using the new PEP 8 compliant names facilitates substitution
25# with the multiprocessing module, which doesn't provide the old
26# Java inspired names.
27
Benjamin Peterson672b8032008-06-11 19:14:14 +000028__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000029 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
Benjamin Peterson7761b952011-08-02 13:05:47 -050030 'Timer', 'ThreadError', '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
109 return "<%s owner=%r count=%d>" % (
110 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000111
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000112 def acquire(self, blocking=True, timeout=-1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200113 """Acquire a lock, blocking or non-blocking.
114
115 When invoked without arguments: if this thread already owns the lock,
116 increment the recursion level by one, and return immediately. Otherwise,
117 if another thread owns the lock, block until the lock is unlocked. Once
118 the lock is unlocked (not owned by any thread), then grab ownership, set
119 the recursion level to one, and return. If more than one thread is
120 blocked waiting until the lock is unlocked, only one at a time will be
121 able to grab ownership of the lock. There is no return value in this
122 case.
123
124 When invoked with the blocking argument set to true, do the same thing
125 as when called without arguments, and return true.
126
127 When invoked with the blocking argument set to false, do not block. If a
128 call without an argument would block, return false immediately;
129 otherwise, do the same thing as when called without arguments, and
130 return true.
131
132 When invoked with the floating-point timeout argument set to a positive
133 value, block for at most the number of seconds specified by timeout
134 and as long as the lock cannot be acquired. Return true if the lock has
135 been acquired, false if the timeout has elapsed.
136
137 """
Victor Stinner2a129742011-05-30 23:02:52 +0200138 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +0000139 if self._owner == me:
Raymond Hettinger720da572013-03-10 15:13:35 -0700140 self._count += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000141 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000142 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000143 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000144 self._owner = me
145 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000146 return rc
147
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000148 __enter__ = acquire
149
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000150 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200151 """Release a lock, decrementing the recursion level.
152
153 If after the decrement it is zero, reset the lock to unlocked (not owned
154 by any thread), and if any other threads are blocked waiting for the
155 lock to become unlocked, allow exactly one of them to proceed. If after
156 the decrement the recursion level is still nonzero, the lock remains
157 locked and owned by the calling thread.
158
159 Only call this method when the calling thread owns the lock. A
160 RuntimeError is raised if this method is called when the lock is
161 unlocked.
162
163 There is no return value.
164
165 """
Victor Stinner2a129742011-05-30 23:02:52 +0200166 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000167 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000168 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000169 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000170 self._owner = None
171 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000172
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000173 def __exit__(self, t, v, tb):
174 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000175
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000176 # Internal methods used by condition variables
177
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000178 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000179 self._block.acquire()
180 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000181
182 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200183 if self._count == 0:
184 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000185 count = self._count
186 self._count = 0
187 owner = self._owner
188 self._owner = None
189 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000190 return (count, owner)
191
192 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200193 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000194
Antoine Pitrou434736a2009-11-10 18:46:01 +0000195_PyRLock = _RLock
196
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000197
Victor Stinner135b6d82012-03-03 01:32:57 +0100198class Condition:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200199 """Class that implements a condition variable.
200
201 A condition variable allows one or more threads to wait until they are
202 notified by another thread.
203
204 If the lock argument is given and not None, it must be a Lock or RLock
205 object, and it is used as the underlying lock. Otherwise, a new RLock object
206 is created and used as the underlying lock.
207
208 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000209
Victor Stinner135b6d82012-03-03 01:32:57 +0100210 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000211 if lock is None:
212 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000213 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000214 # Export the lock's acquire() and release() methods
215 self.acquire = lock.acquire
216 self.release = lock.release
217 # If the lock defines _release_save() and/or _acquire_restore(),
218 # these override the default implementations (which just call
219 # release() and acquire() on the lock). Ditto for _is_owned().
220 try:
221 self._release_save = lock._release_save
222 except AttributeError:
223 pass
224 try:
225 self._acquire_restore = lock._acquire_restore
226 except AttributeError:
227 pass
228 try:
229 self._is_owned = lock._is_owned
230 except AttributeError:
231 pass
Raymond Hettingerec4b1742013-03-10 17:57:28 -0700232 self._waiters = _deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000233
Thomas Wouters477c8d52006-05-27 19:21:47 +0000234 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000235 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000236
Thomas Wouters477c8d52006-05-27 19:21:47 +0000237 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000238 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000239
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000240 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000241 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000242
243 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000244 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000245
246 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000247 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000248
249 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000250 # Return True if lock is owned by current_thread.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300251 # This method is called only if _lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000252 if self._lock.acquire(0):
253 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000254 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000255 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000256 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000257
258 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200259 """Wait until notified or until a timeout occurs.
260
261 If the calling thread has not acquired the lock when this method is
262 called, a RuntimeError is raised.
263
264 This method releases the underlying lock, and then blocks until it is
265 awakened by a notify() or notify_all() call for the same condition
266 variable in another thread, or until the optional timeout occurs. Once
267 awakened or timed out, it re-acquires the lock and returns.
268
269 When the timeout argument is present and not None, it should be a
270 floating point number specifying a timeout for the operation in seconds
271 (or fractions thereof).
272
273 When the underlying lock is an RLock, it is not released using its
274 release() method, since this may not actually unlock the lock when it
275 was acquired multiple times recursively. Instead, an internal interface
276 of the RLock class is used, which really unlocks it even when it has
277 been recursively acquired several times. Another internal interface is
278 then used to restore the recursion level when the lock is reacquired.
279
280 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000281 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000282 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000283 waiter = _allocate_lock()
284 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000285 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000286 saved_state = self._release_save()
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200287 gotit = False
Tim Petersc951bf92001-04-02 20:15:57 +0000288 try: # restore state no matter what (e.g., KeyboardInterrupt)
289 if timeout is None:
290 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000291 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000292 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000293 if timeout > 0:
294 gotit = waiter.acquire(True, timeout)
295 else:
296 gotit = waiter.acquire(False)
Georg Brandlb9a43912010-10-28 09:03:20 +0000297 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000298 finally:
299 self._acquire_restore(saved_state)
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200300 if not gotit:
301 try:
302 self._waiters.remove(waiter)
303 except ValueError:
304 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000305
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000306 def wait_for(self, predicate, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200307 """Wait until a condition evaluates to True.
308
309 predicate should be a callable which result will be interpreted as a
310 boolean value. A timeout may be provided giving the maximum time to
311 wait.
312
313 """
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000314 endtime = None
315 waittime = timeout
316 result = predicate()
317 while not result:
318 if waittime is not None:
319 if endtime is None:
320 endtime = _time() + waittime
321 else:
322 waittime = endtime - _time()
323 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000324 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000325 self.wait(waittime)
326 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000327 return result
328
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000329 def notify(self, n=1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200330 """Wake up one or more threads waiting on this condition, if any.
331
332 If the calling thread has not acquired the lock when this method is
333 called, a RuntimeError is raised.
334
335 This method wakes up at most n of the threads waiting for the condition
336 variable; it is a no-op if no threads are waiting.
337
338 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000339 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000340 raise RuntimeError("cannot notify on un-acquired lock")
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700341 all_waiters = self._waiters
342 waiters_to_notify = _deque(_islice(all_waiters, n))
343 if not waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000344 return
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700345 for waiter in waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000346 waiter.release()
347 try:
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700348 all_waiters.remove(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000349 except ValueError:
350 pass
351
Benjamin Peterson672b8032008-06-11 19:14:14 +0000352 def notify_all(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200353 """Wake up all threads waiting on this condition.
354
355 If the calling thread has not acquired the lock when this method
356 is called, a RuntimeError is raised.
357
358 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000359 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000360
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000361 notifyAll = notify_all
362
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000363
Victor Stinner135b6d82012-03-03 01:32:57 +0100364class Semaphore:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200365 """This class implements semaphore objects.
366
367 Semaphores manage a counter representing the number of release() calls minus
368 the number of acquire() calls, plus an initial value. The acquire() method
369 blocks if necessary until it can return without making the counter
370 negative. If not given, value defaults to 1.
371
372 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000373
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000374 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000375
Victor Stinner135b6d82012-03-03 01:32:57 +0100376 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000377 if value < 0:
378 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000379 self._cond = Condition(Lock())
380 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000381
Antoine Pitrou0454af92010-04-17 23:51:58 +0000382 def acquire(self, blocking=True, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200383 """Acquire a semaphore, decrementing the internal counter by one.
384
385 When invoked without arguments: if the internal counter is larger than
386 zero on entry, decrement it by one and return immediately. If it is zero
387 on entry, block, waiting until some other thread has called release() to
388 make it larger than zero. This is done with proper interlocking so that
389 if multiple acquire() calls are blocked, release() will wake exactly one
390 of them up. The implementation may pick one at random, so the order in
391 which blocked threads are awakened should not be relied on. There is no
392 return value in this case.
393
394 When invoked with blocking set to true, do the same thing as when called
395 without arguments, and return true.
396
397 When invoked with blocking set to false, do not block. If a call without
398 an argument would block, return false immediately; otherwise, do the
399 same thing as when called without arguments, and return true.
400
401 When invoked with a timeout other than None, it will block for at
402 most timeout seconds. If acquire does not complete successfully in
403 that interval, return false. Return true otherwise.
404
405 """
Antoine Pitrou0454af92010-04-17 23:51:58 +0000406 if not blocking and timeout is not None:
407 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000408 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000409 endtime = None
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300410 with self._cond:
411 while self._value == 0:
412 if not blocking:
413 break
414 if timeout is not None:
415 if endtime is None:
416 endtime = _time() + timeout
417 else:
418 timeout = endtime - _time()
419 if timeout <= 0:
420 break
421 self._cond.wait(timeout)
422 else:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300423 self._value -= 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300424 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000425 return rc
426
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000427 __enter__ = acquire
428
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000429 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200430 """Release a semaphore, incrementing the internal counter by one.
431
432 When the counter is zero on entry and another thread is waiting for it
433 to become larger than zero again, wake up that thread.
434
435 """
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300436 with self._cond:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300437 self._value += 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300438 self._cond.notify()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000439
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000440 def __exit__(self, t, v, tb):
441 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000442
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000443
Éric Araujo0cdd4452011-07-28 00:28:28 +0200444class BoundedSemaphore(Semaphore):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200445 """Implements a bounded semaphore.
446
447 A bounded semaphore checks to make sure its current value doesn't exceed its
448 initial value. If it does, ValueError is raised. In most situations
449 semaphores are used to guard resources with limited capacity.
450
451 If the semaphore is released too many times it's a sign of a bug. If not
452 given, value defaults to 1.
453
454 Like regular semaphores, bounded semaphores manage a counter representing
455 the number of release() calls minus the number of acquire() calls, plus an
456 initial value. The acquire() method blocks if necessary until it can return
457 without making the counter negative. If not given, value defaults to 1.
458
459 """
460
Victor Stinner135b6d82012-03-03 01:32:57 +0100461 def __init__(self, value=1):
462 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000463 self._initial_value = value
464
465 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200466 """Release a semaphore, incrementing the internal counter by one.
467
468 When the counter is zero on entry and another thread is waiting for it
469 to become larger than zero again, wake up that thread.
470
471 If the number of releases exceeds the number of acquires,
472 raise a ValueError.
473
474 """
Tim Peters7634e1c2013-10-08 20:55:51 -0500475 with self._cond:
476 if self._value >= self._initial_value:
477 raise ValueError("Semaphore released too many times")
478 self._value += 1
479 self._cond.notify()
Skip Montanaroe428bb72001-08-20 20:27:58 +0000480
481
Victor Stinner135b6d82012-03-03 01:32:57 +0100482class Event:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200483 """Class implementing event objects.
484
485 Events manage a flag that can be set to true with the set() method and reset
486 to false with the clear() method. The wait() method blocks until the flag is
487 true. The flag is initially false.
488
489 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000490
491 # After Tim Peters' event class (without is_posted())
492
Victor Stinner135b6d82012-03-03 01:32:57 +0100493 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000494 self._cond = Condition(Lock())
495 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000496
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000497 def _reset_internal_locks(self):
498 # private! called by Thread._reset_internal_locks by _after_fork()
Benjamin Peterson15982aa2015-10-05 21:56:22 -0700499 self._cond.__init__(Lock())
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000500
Benjamin Peterson672b8032008-06-11 19:14:14 +0000501 def is_set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200502 """Return true if and only if the internal flag is true."""
Guido van Rossumd0648992007-08-20 19:25:41 +0000503 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000504
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000505 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000506
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000507 def set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200508 """Set the internal flag to true.
509
510 All threads waiting for it to become true are awakened. Threads
511 that call wait() once the flag is true will not block at all.
512
513 """
Christian Heimes969fe572008-01-25 11:23:10 +0000514 self._cond.acquire()
515 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000516 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000517 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000518 finally:
519 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000520
521 def clear(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200522 """Reset the internal flag to false.
523
524 Subsequently, threads calling wait() will block until set() is called to
525 set the internal flag to true again.
526
527 """
Christian Heimes969fe572008-01-25 11:23:10 +0000528 self._cond.acquire()
529 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000530 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000531 finally:
532 self._cond.release()
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 """
Christian Heimes969fe572008-01-25 11:23:10 +0000549 self._cond.acquire()
550 try:
Charles-François Natalided03482012-01-07 18:24:56 +0100551 signaled = self._flag
552 if not signaled:
553 signaled = self._cond.wait(timeout)
554 return signaled
Christian Heimes969fe572008-01-25 11:23:10 +0000555 finally:
556 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000557
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000558
559# A barrier class. Inspired in part by the pthread_barrier_* api and
560# the CyclicBarrier class from Java. See
561# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
562# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
563# CyclicBarrier.html
564# for information.
565# We maintain two main states, 'filling' and 'draining' enabling the barrier
566# to be cyclic. Threads are not allowed into it until it has fully drained
567# since the previous cycle. In addition, a 'resetting' state exists which is
568# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300569# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100570class Barrier:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200571 """Implements a Barrier.
572
573 Useful for synchronizing a fixed number of threads at known synchronization
574 points. Threads block on 'wait()' and are simultaneously once they have all
575 made that call.
576
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000577 """
Georg Brandlc30b59f2013-10-13 10:43:59 +0200578
Victor Stinner135b6d82012-03-03 01:32:57 +0100579 def __init__(self, parties, action=None, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200580 """Create a barrier, initialised to 'parties' threads.
581
582 'action' is a callable which, when supplied, will be called by one of
583 the threads after they have all entered the barrier and just prior to
584 releasing them all. If a 'timeout' is provided, it is uses as the
585 default for all subsequent 'wait()' calls.
586
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000587 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000588 self._cond = Condition(Lock())
589 self._action = action
590 self._timeout = timeout
591 self._parties = parties
592 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
593 self._count = 0
594
595 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200596 """Wait for the barrier.
597
598 When the specified number of threads have started waiting, they are all
599 simultaneously awoken. If an 'action' was provided for the barrier, one
600 of the threads will have executed that callback prior to returning.
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000601 Returns an individual index number from 0 to 'parties-1'.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200602
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000603 """
604 if timeout is None:
605 timeout = self._timeout
606 with self._cond:
607 self._enter() # Block while the barrier drains.
608 index = self._count
609 self._count += 1
610 try:
611 if index + 1 == self._parties:
612 # We release the barrier
613 self._release()
614 else:
615 # We wait until someone releases us
616 self._wait(timeout)
617 return index
618 finally:
619 self._count -= 1
620 # Wake up any threads waiting for barrier to drain.
621 self._exit()
622
623 # Block until the barrier is ready for us, or raise an exception
624 # if it is broken.
625 def _enter(self):
626 while self._state in (-1, 1):
627 # It is draining or resetting, wait until done
628 self._cond.wait()
629 #see if the barrier is in a broken state
630 if self._state < 0:
631 raise BrokenBarrierError
632 assert self._state == 0
633
634 # Optionally run the 'action' and release the threads waiting
635 # in the barrier.
636 def _release(self):
637 try:
638 if self._action:
639 self._action()
640 # enter draining state
641 self._state = 1
642 self._cond.notify_all()
643 except:
644 #an exception during the _action handler. Break and reraise
645 self._break()
646 raise
647
648 # Wait in the barrier until we are relased. Raise an exception
649 # if the barrier is reset or broken.
650 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000651 if not self._cond.wait_for(lambda : self._state != 0, timeout):
652 #timed out. Break the barrier
653 self._break()
654 raise BrokenBarrierError
655 if self._state < 0:
656 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000657 assert self._state == 1
658
659 # If we are the last thread to exit the barrier, signal any threads
660 # waiting for the barrier to drain.
661 def _exit(self):
662 if self._count == 0:
663 if self._state in (-1, 1):
664 #resetting or draining
665 self._state = 0
666 self._cond.notify_all()
667
668 def reset(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200669 """Reset the barrier to the initial state.
670
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000671 Any threads currently waiting will get the BrokenBarrier exception
672 raised.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200673
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000674 """
675 with self._cond:
676 if self._count > 0:
677 if self._state == 0:
678 #reset the barrier, waking up threads
679 self._state = -1
680 elif self._state == -2:
681 #was broken, set it to reset state
682 #which clears when the last thread exits
683 self._state = -1
684 else:
685 self._state = 0
686 self._cond.notify_all()
687
688 def abort(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200689 """Place the barrier into a 'broken' state.
690
691 Useful in case of error. Any currently waiting threads and threads
692 attempting to 'wait()' will have BrokenBarrierError raised.
693
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000694 """
695 with self._cond:
696 self._break()
697
698 def _break(self):
699 # An internal error was detected. The barrier is set to
700 # a broken state all parties awakened.
701 self._state = -2
702 self._cond.notify_all()
703
704 @property
705 def parties(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200706 """Return the number of threads required to trip the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000707 return self._parties
708
709 @property
710 def n_waiting(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200711 """Return the number of threads currently waiting at the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000712 # We don't need synchronization here since this is an ephemeral result
713 # anyway. It returns the correct value in the steady state.
714 if self._state == 0:
715 return self._count
716 return 0
717
718 @property
719 def broken(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200720 """Return True if the barrier is in a broken state."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000721 return self._state == -2
722
Georg Brandlc30b59f2013-10-13 10:43:59 +0200723# exception raised by the Barrier class
724class BrokenBarrierError(RuntimeError):
725 pass
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000726
727
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000728# Helper to generate new thread names
R David Murrayb186f1df2014-10-04 17:43:54 -0400729_counter = _count().__next__
730_counter() # Consume 0 so first non-main thread has id 1.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000731def _newname(template="Thread-%d"):
R David Murrayb186f1df2014-10-04 17:43:54 -0400732 return template % _counter()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000733
734# Active thread administration
735_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000736_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000737_limbo = {}
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200738_dangling = WeakSet()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000739
740# Main class for threads
741
Victor Stinner135b6d82012-03-03 01:32:57 +0100742class Thread:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200743 """A class that represents a thread of control.
744
745 This class can be safely subclassed in a limited fashion. There are two ways
746 to specify the activity: by passing a callable object to the constructor, or
747 by overriding the run() method in a subclass.
748
749 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000750
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300751 _initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000752 # Need to store a reference to sys.exc_info for printing
753 # out exceptions when a thread tries to use a global var. during interp.
754 # shutdown and thus raises an exception about trying to perform some
755 # operation on/with a NoneType
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300756 _exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000757 # Keep sys.exc_clear too to clear the exception just before
758 # allowing .join() to return.
759 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000760
761 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100762 args=(), kwargs=None, *, daemon=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200763 """This constructor should always be called with keyword arguments. Arguments are:
764
765 *group* should be None; reserved for future extension when a ThreadGroup
766 class is implemented.
767
768 *target* is the callable object to be invoked by the run()
769 method. Defaults to None, meaning nothing is called.
770
771 *name* is the thread name. By default, a unique name is constructed of
772 the form "Thread-N" where N is a small decimal number.
773
774 *args* is the argument tuple for the target invocation. Defaults to ().
775
776 *kwargs* is a dictionary of keyword arguments for the target
777 invocation. Defaults to {}.
778
779 If a subclass overrides the constructor, it must make sure to invoke
780 the base class constructor (Thread.__init__()) before doing anything
781 else to the thread.
782
783 """
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000784 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000785 if kwargs is None:
786 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000787 self._target = target
788 self._name = str(name or _newname())
789 self._args = args
790 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000791 if daemon is not None:
792 self._daemonic = daemon
793 else:
794 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000795 self._ident = None
Antoine Pitrou7b476992013-09-07 23:38:37 +0200796 self._tstate_lock = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000797 self._started = Event()
Tim Petersc363a232013-09-08 18:44:40 -0500798 self._is_stopped = False
Guido van Rossumd0648992007-08-20 19:25:41 +0000799 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000800 # sys.stderr is not stored in the class like
801 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000802 self._stderr = _sys.stderr
Antoine Pitrou5da7e792013-09-08 13:19:06 +0200803 # For debugging and _after_fork()
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200804 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000805
Antoine Pitrou7b476992013-09-07 23:38:37 +0200806 def _reset_internal_locks(self, is_alive):
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000807 # private! Called by _after_fork() to reset our internal locks as
808 # they may be in an invalid state leading to a deadlock or crash.
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000809 self._started._reset_internal_locks()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200810 if is_alive:
811 self._set_tstate_lock()
812 else:
813 # The thread isn't alive after fork: it doesn't have a tstate
814 # anymore.
Tim Petersb5e9ac92013-09-09 14:41:50 -0500815 self._is_stopped = True
Antoine Pitrou7b476992013-09-07 23:38:37 +0200816 self._tstate_lock = None
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000817
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000818 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000819 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000820 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000821 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000822 status = "started"
Tim Peters72460fa2013-09-09 18:48:24 -0500823 self.is_alive() # easy way to get ._is_stopped set when appropriate
Tim Petersc363a232013-09-08 18:44:40 -0500824 if self._is_stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000825 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000826 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000827 status += " daemon"
828 if self._ident is not None:
829 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000830 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000831
832 def start(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200833 """Start the thread's activity.
834
835 It must be called at most once per thread object. It arranges for the
836 object's run() method to be invoked in a separate thread of control.
837
838 This method will raise a RuntimeError if called more than once on the
839 same thread object.
840
841 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000842 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000843 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000844
Benjamin Peterson672b8032008-06-11 19:14:14 +0000845 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000846 raise RuntimeError("threads can only be started once")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000847 with _active_limbo_lock:
848 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000849 try:
850 _start_new_thread(self._bootstrap, ())
851 except Exception:
852 with _active_limbo_lock:
853 del _limbo[self]
854 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000855 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000856
857 def run(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200858 """Method representing the thread's activity.
859
860 You may override this method in a subclass. The standard run() method
861 invokes the callable object passed to the object's constructor as the
862 target argument, if any, with sequential and keyword arguments taken
863 from the args and kwargs arguments, respectively.
864
865 """
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000866 try:
867 if self._target:
868 self._target(*self._args, **self._kwargs)
869 finally:
870 # Avoid a refcycle if the thread is running a function with
871 # an argument that has a member that points to the thread.
872 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000873
Guido van Rossumd0648992007-08-20 19:25:41 +0000874 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000875 # Wrapper around the real bootstrap code that ignores
876 # exceptions during interpreter cleanup. Those typically
877 # happen when a daemon thread wakes up at an unfortunate
878 # moment, finds the world around it destroyed, and raises some
879 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000880 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000881 # don't help anybody, and they confuse users, so we suppress
882 # them. We suppress them only when it appears that the world
883 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000884 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000885 # reported. Also, we only suppress them for daemonic threads;
886 # if a non-daemonic encounters this, something else is wrong.
887 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000888 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000889 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000890 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000891 return
892 raise
893
Benjamin Petersond23f8222009-04-05 19:13:16 +0000894 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200895 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000896
Antoine Pitrou7b476992013-09-07 23:38:37 +0200897 def _set_tstate_lock(self):
898 """
899 Set a lock object which will be released by the interpreter when
900 the underlying thread state (see pystate.h) gets deleted.
901 """
902 self._tstate_lock = _set_sentinel()
903 self._tstate_lock.acquire()
904
Guido van Rossumd0648992007-08-20 19:25:41 +0000905 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000906 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000907 self._set_ident()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200908 self._set_tstate_lock()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000909 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000910 with _active_limbo_lock:
911 _active[self._ident] = self
912 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000913
914 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000915 _sys.settrace(_trace_hook)
916 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000917 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000918
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000919 try:
920 self.run()
921 except SystemExit:
Victor Stinner135b6d82012-03-03 01:32:57 +0100922 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000923 except:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000924 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000925 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000926 # _sys) in case sys.stderr was redefined since the creation of
927 # self.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300928 if _sys and _sys.stderr is not None:
929 print("Exception in thread %s:\n%s" %
930 (self.name, _format_exc()), file=self._stderr)
931 elif self._stderr is not None:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000932 # Do the best job possible w/o a huge amt. of code to
933 # approximate a traceback (code ideas from
934 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000935 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000936 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000937 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000938 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000939 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000940 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000941 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000942 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000943 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000944 ' File "%s", line %s, in %s' %
945 (exc_tb.tb_frame.f_code.co_filename,
946 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000947 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000948 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000949 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000950 # Make sure that exc_tb gets deleted since it is a memory
951 # hog; deleting everything else is just for thoroughness
952 finally:
953 del exc_type, exc_value, exc_tb
Christian Heimesbbe741d2008-03-28 10:53:29 +0000954 finally:
955 # Prevent a race in
956 # test_threading.test_no_refcycle_through_target when
957 # the exception keeps the target alive past when we
958 # assert that it's dead.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300959 #XXX self._exc_clear()
Christian Heimesbbe741d2008-03-28 10:53:29 +0000960 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000961 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000962 with _active_limbo_lock:
Christian Heimes1af737c2008-01-23 08:24:23 +0000963 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000964 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000965 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200966 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000967 except:
968 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000969
Guido van Rossumd0648992007-08-20 19:25:41 +0000970 def _stop(self):
Tim Petersb5e9ac92013-09-09 14:41:50 -0500971 # After calling ._stop(), .is_alive() returns False and .join() returns
972 # immediately. ._tstate_lock must be released before calling ._stop().
973 #
974 # Normal case: C code at the end of the thread's life
975 # (release_sentinel in _threadmodule.c) releases ._tstate_lock, and
976 # that's detected by our ._wait_for_tstate_lock(), called by .join()
977 # and .is_alive(). Any number of threads _may_ call ._stop()
978 # simultaneously (for example, if multiple threads are blocked in
979 # .join() calls), and they're not serialized. That's harmless -
980 # they'll just make redundant rebindings of ._is_stopped and
981 # ._tstate_lock. Obscure: we rebind ._tstate_lock last so that the
982 # "assert self._is_stopped" in ._wait_for_tstate_lock() always works
983 # (the assert is executed only if ._tstate_lock is None).
984 #
985 # Special case: _main_thread releases ._tstate_lock via this
986 # module's _shutdown() function.
987 lock = self._tstate_lock
988 if lock is not None:
989 assert not lock.locked()
Tim Peters78755232013-09-09 13:47:16 -0500990 self._is_stopped = True
991 self._tstate_lock = None
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000992
Guido van Rossumd0648992007-08-20 19:25:41 +0000993 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000994 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000995
Georg Brandl2067bfd2008-05-25 13:05:15 +0000996 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000997 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000998 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000999 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +00001000 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
1001 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +00001002 # len(_active) is always <= 1 here, and any Thread instance created
1003 # overwrites the (if any) thread currently registered in _active.
1004 #
1005 # An instance of _MainThread is always created by 'threading'. This
1006 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +00001007 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +00001008 # same key in the dict. So when the _MainThread instance created by
1009 # 'threading' tries to clean itself up when atexit calls this method
1010 # it gets a KeyError if another Thread instance was created.
1011 #
1012 # This all means that KeyError from trying to delete something from
1013 # _active if dummy_threading is being used is a red herring. But
1014 # since it isn't if dummy_threading is *not* being used then don't
1015 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +00001016
Christian Heimes969fe572008-01-25 11:23:10 +00001017 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001018 with _active_limbo_lock:
Victor Stinner2a129742011-05-30 23:02:52 +02001019 del _active[get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001020 # There must not be any python code between the previous line
1021 # and after the lock is released. Otherwise a tracing function
1022 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +00001023 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +00001024 except KeyError:
1025 if 'dummy_threading' not in _sys.modules:
1026 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001027
1028 def join(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001029 """Wait until the thread terminates.
1030
1031 This blocks the calling thread until the thread whose join() method is
1032 called terminates -- either normally or through an unhandled exception
1033 or until the optional timeout occurs.
1034
1035 When the timeout argument is present and not None, it should be a
1036 floating point number specifying a timeout for the operation in seconds
1037 (or fractions thereof). As join() always returns None, you must call
1038 isAlive() after join() to decide whether a timeout happened -- if the
1039 thread is still alive, the join() call timed out.
1040
1041 When the timeout argument is not present or None, the operation will
1042 block until the thread terminates.
1043
1044 A thread can be join()ed many times.
1045
1046 join() raises a RuntimeError if an attempt is made to join the current
1047 thread as that would cause a deadlock. It is also an error to join() a
1048 thread before it has been started and attempts to do so raises the same
1049 exception.
1050
1051 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001052 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001053 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001054 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001055 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001056 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001057 raise RuntimeError("cannot join current thread")
Tim Peterse5bb0bf2013-10-25 20:46:51 -05001058
Tim Petersc363a232013-09-08 18:44:40 -05001059 if timeout is None:
1060 self._wait_for_tstate_lock()
Tim Peters7bad39f2013-10-25 22:33:52 -05001061 else:
1062 # the behavior of a negative timeout isn't documented, but
Tim Petersa577f1e2013-10-26 11:56:16 -05001063 # historically .join(timeout=x) for x<0 has acted as if timeout=0
Tim Peters7bad39f2013-10-25 22:33:52 -05001064 self._wait_for_tstate_lock(timeout=max(timeout, 0))
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001065
Tim Petersc363a232013-09-08 18:44:40 -05001066 def _wait_for_tstate_lock(self, block=True, timeout=-1):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001067 # Issue #18808: wait for the thread state to be gone.
Tim Petersc363a232013-09-08 18:44:40 -05001068 # At the end of the thread's life, after all knowledge of the thread
1069 # is removed from C data structures, C code releases our _tstate_lock.
1070 # This method passes its arguments to _tstate_lock.aquire().
1071 # If the lock is acquired, the C code is done, and self._stop() is
1072 # called. That sets ._is_stopped to True, and ._tstate_lock to None.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001073 lock = self._tstate_lock
Tim Petersc363a232013-09-08 18:44:40 -05001074 if lock is None: # already determined that the C code is done
1075 assert self._is_stopped
1076 elif lock.acquire(block, timeout):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001077 lock.release()
Tim Petersc363a232013-09-08 18:44:40 -05001078 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001079
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001080 @property
1081 def name(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001082 """A string used for identification purposes only.
1083
1084 It has no semantics. Multiple threads may be given the same name. The
1085 initial name is set by the constructor.
1086
1087 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001088 assert self._initialized, "Thread.__init__() not called"
1089 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001090
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001091 @name.setter
1092 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +00001093 assert self._initialized, "Thread.__init__() not called"
1094 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001095
Benjamin Peterson773c17b2008-08-18 16:45:31 +00001096 @property
1097 def ident(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001098 """Thread identifier of this thread or None if it has not been started.
1099
1100 This is a nonzero integer. See the thread.get_ident() function. Thread
1101 identifiers may be recycled when a thread exits and another thread is
1102 created. The identifier is available even after the thread has exited.
1103
1104 """
Georg Brandl0c77a822008-06-10 16:37:50 +00001105 assert self._initialized, "Thread.__init__() not called"
1106 return self._ident
1107
Benjamin Peterson672b8032008-06-11 19:14:14 +00001108 def is_alive(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001109 """Return whether the thread is alive.
1110
1111 This method returns True just before the run() method starts until just
1112 after the run() method terminates. The module function enumerate()
1113 returns a list of all alive threads.
1114
1115 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001116 assert self._initialized, "Thread.__init__() not called"
Tim Petersc363a232013-09-08 18:44:40 -05001117 if self._is_stopped or not self._started.is_set():
Antoine Pitrou7b476992013-09-07 23:38:37 +02001118 return False
Antoine Pitrou7b476992013-09-07 23:38:37 +02001119 self._wait_for_tstate_lock(False)
Tim Petersc363a232013-09-08 18:44:40 -05001120 return not self._is_stopped
Tim Petersb90f89a2001-01-15 03:26:36 +00001121
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001122 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001123
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001124 @property
1125 def daemon(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001126 """A boolean value indicating whether this thread is a daemon thread.
1127
1128 This must be set before start() is called, otherwise RuntimeError is
1129 raised. Its initial value is inherited from the creating thread; the
1130 main thread is not a daemon thread and therefore all threads created in
1131 the main thread default to daemon = False.
1132
1133 The entire Python program exits when no alive non-daemon threads are
1134 left.
1135
1136 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001137 assert self._initialized, "Thread.__init__() not called"
1138 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001139
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001140 @daemon.setter
1141 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +00001142 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001143 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001144 if self._started.is_set():
Antoine Pitrou10959072014-03-17 18:22:41 +01001145 raise RuntimeError("cannot set daemon status of active thread")
Guido van Rossumd0648992007-08-20 19:25:41 +00001146 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001147
Benjamin Peterson6640d722008-08-18 18:16:46 +00001148 def isDaemon(self):
1149 return self.daemon
1150
1151 def setDaemon(self, daemonic):
1152 self.daemon = daemonic
1153
1154 def getName(self):
1155 return self.name
1156
1157 def setName(self, name):
1158 self.name = name
1159
Martin v. Löwis44f86962001-09-05 13:44:54 +00001160# The timer class was contributed by Itamar Shtull-Trauring
1161
Éric Araujo0cdd4452011-07-28 00:28:28 +02001162class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001163 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +00001164
Georg Brandlc30b59f2013-10-13 10:43:59 +02001165 t = Timer(30.0, f, args=None, kwargs=None)
1166 t.start()
1167 t.cancel() # stop the timer's action if it's still waiting
1168
Martin v. Löwis44f86962001-09-05 13:44:54 +00001169 """
Tim Petersb64bec32001-09-18 02:26:39 +00001170
R David Murray19aeb432013-03-30 17:19:38 -04001171 def __init__(self, interval, function, args=None, kwargs=None):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001172 Thread.__init__(self)
1173 self.interval = interval
1174 self.function = function
R David Murray19aeb432013-03-30 17:19:38 -04001175 self.args = args if args is not None else []
1176 self.kwargs = kwargs if kwargs is not None else {}
Martin v. Löwis44f86962001-09-05 13:44:54 +00001177 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +00001178
Martin v. Löwis44f86962001-09-05 13:44:54 +00001179 def cancel(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001180 """Stop the timer if it hasn't finished yet."""
Martin v. Löwis44f86962001-09-05 13:44:54 +00001181 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +00001182
Martin v. Löwis44f86962001-09-05 13:44:54 +00001183 def run(self):
1184 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +00001185 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +00001186 self.function(*self.args, **self.kwargs)
1187 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001188
1189# Special thread class to represent the main thread
1190# This is garbage collected through an exit handler
1191
1192class _MainThread(Thread):
1193
1194 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001195 Thread.__init__(self, name="MainThread", daemon=False)
Tim Petersc363a232013-09-08 18:44:40 -05001196 self._set_tstate_lock()
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001197 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001198 self._set_ident()
1199 with _active_limbo_lock:
1200 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001201
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001202
1203# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +00001204# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001205# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +00001206# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001207# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001208# They are marked as daemon threads so we won't wait for them
1209# when we exit (conform previous semantics).
1210
1211class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +00001212
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001213 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001214 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +00001215
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001216 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001217 self._set_ident()
1218 with _active_limbo_lock:
1219 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001220
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +02001221 def _stop(self):
1222 pass
1223
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001224 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001225 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001226
1227
1228# Global API functions
1229
Benjamin Peterson672b8032008-06-11 19:14:14 +00001230def current_thread():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001231 """Return the current Thread object, corresponding to the caller's thread of control.
1232
1233 If the caller's thread of control was not created through the threading
1234 module, a dummy thread object with limited functionality is returned.
1235
1236 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001237 try:
Victor Stinner2a129742011-05-30 23:02:52 +02001238 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001239 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001240 return _DummyThread()
1241
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001242currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001243
Benjamin Peterson672b8032008-06-11 19:14:14 +00001244def active_count():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001245 """Return the number of Thread objects currently alive.
1246
1247 The returned count is equal to the length of the list returned by
1248 enumerate().
1249
1250 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001251 with _active_limbo_lock:
1252 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001253
Benjamin Petersonb3085c92008-09-01 23:09:31 +00001254activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001255
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001256def _enumerate():
1257 # Same as enumerate(), but without the lock. Internal use only.
1258 return list(_active.values()) + list(_limbo.values())
1259
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001260def enumerate():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001261 """Return a list of all Thread objects currently alive.
1262
1263 The list includes daemonic threads, dummy thread objects created by
1264 current_thread(), and the main thread. It excludes terminated threads and
1265 threads that have not yet been started.
1266
1267 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001268 with _active_limbo_lock:
1269 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001270
Georg Brandl2067bfd2008-05-25 13:05:15 +00001271from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001272
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001273# Create the main thread object,
1274# and make it available for the interpreter
1275# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001276
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001277_main_thread = _MainThread()
1278
1279def _shutdown():
Tim Petersc363a232013-09-08 18:44:40 -05001280 # Obscure: other threads may be waiting to join _main_thread. That's
1281 # dubious, but some code does it. We can't wait for C code to release
1282 # the main thread's tstate_lock - that won't happen until the interpreter
1283 # is nearly dead. So we release it here. Note that just calling _stop()
1284 # isn't enough: other threads may already be waiting on _tstate_lock.
Tim Petersb5e9ac92013-09-09 14:41:50 -05001285 tlock = _main_thread._tstate_lock
1286 # The main thread isn't finished yet, so its thread state lock can't have
1287 # been released.
1288 assert tlock is not None
1289 assert tlock.locked()
1290 tlock.release()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001291 _main_thread._stop()
1292 t = _pickSomeNonDaemonThread()
1293 while t:
1294 t.join()
1295 t = _pickSomeNonDaemonThread()
1296 _main_thread._delete()
1297
1298def _pickSomeNonDaemonThread():
1299 for t in enumerate():
1300 if not t.daemon and t.is_alive():
1301 return t
1302 return None
1303
1304def main_thread():
Andrew Svetlovb1dd5572013-09-04 10:33:11 +03001305 """Return the main thread object.
1306
1307 In normal conditions, the main thread is the thread from which the
1308 Python interpreter was started.
1309 """
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001310 return _main_thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001311
Jim Fultond15dc062004-07-14 19:11:50 +00001312# get thread-local implementation, either from the thread
1313# module, or from the python fallback
1314
1315try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001316 from _thread import _local as local
Brett Cannoncd171c82013-07-04 17:43:24 -04001317except ImportError:
Jim Fultond15dc062004-07-14 19:11:50 +00001318 from _threading_local import local
1319
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001320
Jesse Nollera8513972008-07-17 16:49:17 +00001321def _after_fork():
1322 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
1323 # is called from PyOS_AfterFork. Here we cleanup threading module state
1324 # that should not exist after a fork.
1325
1326 # Reset _active_limbo_lock, in case we forked while the lock was held
1327 # by another (non-forked) thread. http://bugs.python.org/issue874900
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001328 global _active_limbo_lock, _main_thread
Jesse Nollera8513972008-07-17 16:49:17 +00001329 _active_limbo_lock = _allocate_lock()
1330
1331 # fork() only copied the current thread; clear references to others.
1332 new_active = {}
1333 current = current_thread()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001334 _main_thread = current
Jesse Nollera8513972008-07-17 16:49:17 +00001335 with _active_limbo_lock:
Antoine Pitrou5da7e792013-09-08 13:19:06 +02001336 # Dangling thread instances must still have their locks reset,
1337 # because someone may join() them.
1338 threads = set(_enumerate())
1339 threads.update(_dangling)
1340 for thread in threads:
Charles-François Natalib055bf62011-12-18 18:45:16 +01001341 # Any lock/condition variable may be currently locked or in an
1342 # invalid state, so we reinitialize them.
Jesse Nollera8513972008-07-17 16:49:17 +00001343 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001344 # There is only one active thread. We reset the ident to
1345 # its new value since it can have changed.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001346 thread._reset_internal_locks(True)
Victor Stinner2a129742011-05-30 23:02:52 +02001347 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001348 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +00001349 new_active[ident] = thread
1350 else:
1351 # All the others are already stopped.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001352 thread._reset_internal_locks(False)
Charles-François Natalib055bf62011-12-18 18:45:16 +01001353 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +00001354
1355 _limbo.clear()
1356 _active.clear()
1357 _active.update(new_active)
1358 assert len(_active) == 1