blob: 35d2e8a552b2a6adc36dabc110dcbd4c57fa3ef0 [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 +02006from time import sleep as _sleep
7try:
8 from time import monotonic as _time
9except ImportError:
10 from time import time as _time
Neil Schemenauerf607fc52003-11-05 23:03:00 +000011from traceback import format_exc as _format_exc
Antoine Pitrouc081c0c2011-07-15 22:12:24 +020012from _weakrefset import WeakSet
Guido van Rossum7f5013a1998-04-09 22:01:42 +000013
Benjamin Petersonb3085c92008-09-01 23:09:31 +000014# Note regarding PEP 8 compliant names
15# This threading model was originally inspired by Java, and inherited
16# the convention of camelCase function and method names from that
17# language. Those originaly names are not in any imminent danger of
18# being deprecated (even for Py3k),so this module provides them as an
19# alias for the PEP 8 compliant names
20# Note that using the new PEP 8 compliant names facilitates substitution
21# with the multiprocessing module, which doesn't provide the old
22# Java inspired names.
23
Benjamin Peterson672b8032008-06-11 19:14:14 +000024__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000025 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
Benjamin Peterson7761b952011-08-02 13:05:47 -050026 'Timer', 'ThreadError', 'setprofile', 'settrace', 'local', 'stack_size']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000027
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000028# Rename some stuff so "from threading import *" is safe
Georg Brandl2067bfd2008-05-25 13:05:15 +000029_start_new_thread = _thread.start_new_thread
30_allocate_lock = _thread.allocate_lock
Victor Stinner2a129742011-05-30 23:02:52 +020031get_ident = _thread.get_ident
Georg Brandl2067bfd2008-05-25 13:05:15 +000032ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000033try:
34 _CRLock = _thread.RLock
35except AttributeError:
36 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000037TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000038del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000039
Guido van Rossum7f5013a1998-04-09 22:01:42 +000040
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000041# Support for profile and trace hooks
42
43_profile_hook = None
44_trace_hook = None
45
46def setprofile(func):
47 global _profile_hook
48 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000049
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000050def settrace(func):
51 global _trace_hook
52 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000053
54# Synchronization classes
55
56Lock = _allocate_lock
57
Victor Stinner135b6d82012-03-03 01:32:57 +010058def RLock(*args, **kwargs):
59 if _CRLock is None:
60 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000061 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000062
Victor Stinner135b6d82012-03-03 01:32:57 +010063class _RLock:
Tim Petersb90f89a2001-01-15 03:26:36 +000064
Victor Stinner135b6d82012-03-03 01:32:57 +010065 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000066 self._block = _allocate_lock()
67 self._owner = None
68 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +000069
70 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000071 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +000072 try:
73 owner = _active[owner].name
74 except KeyError:
75 pass
76 return "<%s owner=%r count=%d>" % (
77 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000078
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000079 def acquire(self, blocking=True, timeout=-1):
Victor Stinner2a129742011-05-30 23:02:52 +020080 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +000081 if self._owner == me:
Raymond Hettinger720da572013-03-10 15:13:35 -070082 self._count += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000083 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000084 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000085 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +000086 self._owner = me
87 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000088 return rc
89
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000090 __enter__ = acquire
91
Guido van Rossum7f5013a1998-04-09 22:01:42 +000092 def release(self):
Victor Stinner2a129742011-05-30 23:02:52 +020093 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +000094 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +000095 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000096 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +000097 self._owner = None
98 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +000099
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000100 def __exit__(self, t, v, tb):
101 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000102
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000103 # Internal methods used by condition variables
104
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000105 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000106 self._block.acquire()
107 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000108
109 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200110 if self._count == 0:
111 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000112 count = self._count
113 self._count = 0
114 owner = self._owner
115 self._owner = None
116 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000117 return (count, owner)
118
119 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200120 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000121
Antoine Pitrou434736a2009-11-10 18:46:01 +0000122_PyRLock = _RLock
123
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000124
Victor Stinner135b6d82012-03-03 01:32:57 +0100125class Condition:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000126
Victor Stinner135b6d82012-03-03 01:32:57 +0100127 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000128 if lock is None:
129 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000130 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000131 # Export the lock's acquire() and release() methods
132 self.acquire = lock.acquire
133 self.release = lock.release
134 # If the lock defines _release_save() and/or _acquire_restore(),
135 # these override the default implementations (which just call
136 # release() and acquire() on the lock). Ditto for _is_owned().
137 try:
138 self._release_save = lock._release_save
139 except AttributeError:
140 pass
141 try:
142 self._acquire_restore = lock._acquire_restore
143 except AttributeError:
144 pass
145 try:
146 self._is_owned = lock._is_owned
147 except AttributeError:
148 pass
Guido van Rossumd0648992007-08-20 19:25:41 +0000149 self._waiters = []
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000150
Thomas Wouters477c8d52006-05-27 19:21:47 +0000151 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000152 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000153
Thomas Wouters477c8d52006-05-27 19:21:47 +0000154 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000155 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000156
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000157 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000158 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000159
160 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000161 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000162
163 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000164 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000165
166 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000167 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000168 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000169 if self._lock.acquire(0):
170 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000171 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000172 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000173 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000174
175 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000176 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000177 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000178 waiter = _allocate_lock()
179 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000180 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000181 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000182 try: # restore state no matter what (e.g., KeyboardInterrupt)
183 if timeout is None:
184 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000185 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000186 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000187 if timeout > 0:
188 gotit = waiter.acquire(True, timeout)
189 else:
190 gotit = waiter.acquire(False)
Tim Petersc951bf92001-04-02 20:15:57 +0000191 if not gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000192 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000193 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000194 except ValueError:
195 pass
Georg Brandlb9a43912010-10-28 09:03:20 +0000196 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000197 finally:
198 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000199
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000200 def wait_for(self, predicate, timeout=None):
201 endtime = None
202 waittime = timeout
203 result = predicate()
204 while not result:
205 if waittime is not None:
206 if endtime is None:
207 endtime = _time() + waittime
208 else:
209 waittime = endtime - _time()
210 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000211 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000212 self.wait(waittime)
213 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000214 return result
215
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000216 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000217 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000218 raise RuntimeError("cannot notify on un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000219 __waiters = self._waiters
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000220 waiters = __waiters[:n]
221 if not waiters:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000222 return
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000223 for waiter in waiters:
224 waiter.release()
225 try:
226 __waiters.remove(waiter)
227 except ValueError:
228 pass
229
Benjamin Peterson672b8032008-06-11 19:14:14 +0000230 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000231 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000232
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000233 notifyAll = notify_all
234
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000235
Victor Stinner135b6d82012-03-03 01:32:57 +0100236class Semaphore:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000237
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000238 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000239
Victor Stinner135b6d82012-03-03 01:32:57 +0100240 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000241 if value < 0:
242 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000243 self._cond = Condition(Lock())
244 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000245
Antoine Pitrou0454af92010-04-17 23:51:58 +0000246 def acquire(self, blocking=True, timeout=None):
247 if not blocking and timeout is not None:
248 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000249 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000250 endtime = None
Guido van Rossumd0648992007-08-20 19:25:41 +0000251 self._cond.acquire()
252 while self._value == 0:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000253 if not blocking:
254 break
Antoine Pitrou0454af92010-04-17 23:51:58 +0000255 if timeout is not None:
256 if endtime is None:
257 endtime = _time() + timeout
258 else:
259 timeout = endtime - _time()
260 if timeout <= 0:
261 break
262 self._cond.wait(timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000263 else:
Raymond Hettinger720da572013-03-10 15:13:35 -0700264 self._value -= 1
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000265 rc = True
Guido van Rossumd0648992007-08-20 19:25:41 +0000266 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000267 return rc
268
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000269 __enter__ = acquire
270
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000271 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000272 self._cond.acquire()
Raymond Hettinger720da572013-03-10 15:13:35 -0700273 self._value += 1
Guido van Rossumd0648992007-08-20 19:25:41 +0000274 self._cond.notify()
275 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000276
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000277 def __exit__(self, t, v, tb):
278 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000279
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000280
Éric Araujo0cdd4452011-07-28 00:28:28 +0200281class BoundedSemaphore(Semaphore):
Skip Montanaroe428bb72001-08-20 20:27:58 +0000282 """Semaphore that checks that # releases is <= # acquires"""
Victor Stinner135b6d82012-03-03 01:32:57 +0100283 def __init__(self, value=1):
284 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000285 self._initial_value = value
286
287 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000288 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000289 raise ValueError("Semaphore released too many times")
Éric Araujo0cdd4452011-07-28 00:28:28 +0200290 return Semaphore.release(self)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000291
292
Victor Stinner135b6d82012-03-03 01:32:57 +0100293class Event:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000294
295 # After Tim Peters' event class (without is_posted())
296
Victor Stinner135b6d82012-03-03 01:32:57 +0100297 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000298 self._cond = Condition(Lock())
299 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000300
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000301 def _reset_internal_locks(self):
302 # private! called by Thread._reset_internal_locks by _after_fork()
303 self._cond.__init__()
304
Benjamin Peterson672b8032008-06-11 19:14:14 +0000305 def is_set(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000306 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000307
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000308 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000309
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000310 def set(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000311 self._cond.acquire()
312 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000313 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000314 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000315 finally:
316 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000317
318 def clear(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000319 self._cond.acquire()
320 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000321 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000322 finally:
323 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000324
325 def wait(self, timeout=None):
Christian Heimes969fe572008-01-25 11:23:10 +0000326 self._cond.acquire()
327 try:
Charles-François Natalided03482012-01-07 18:24:56 +0100328 signaled = self._flag
329 if not signaled:
330 signaled = self._cond.wait(timeout)
331 return signaled
Christian Heimes969fe572008-01-25 11:23:10 +0000332 finally:
333 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000334
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000335
336# A barrier class. Inspired in part by the pthread_barrier_* api and
337# the CyclicBarrier class from Java. See
338# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
339# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
340# CyclicBarrier.html
341# for information.
342# We maintain two main states, 'filling' and 'draining' enabling the barrier
343# to be cyclic. Threads are not allowed into it until it has fully drained
344# since the previous cycle. In addition, a 'resetting' state exists which is
345# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300346# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100347class Barrier:
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000348 """
349 Barrier. Useful for synchronizing a fixed number of threads
350 at known synchronization points. Threads block on 'wait()' and are
351 simultaneously once they have all made that call.
352 """
Victor Stinner135b6d82012-03-03 01:32:57 +0100353 def __init__(self, parties, action=None, timeout=None):
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000354 """
355 Create a barrier, initialised to 'parties' threads.
356 'action' is a callable which, when supplied, will be called
357 by one of the threads after they have all entered the
358 barrier and just prior to releasing them all.
359 If a 'timeout' is provided, it is uses as the default for
360 all subsequent 'wait()' calls.
361 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000362 self._cond = Condition(Lock())
363 self._action = action
364 self._timeout = timeout
365 self._parties = parties
366 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
367 self._count = 0
368
369 def wait(self, timeout=None):
370 """
371 Wait for the barrier. When the specified number of threads have
372 started waiting, they are all simultaneously awoken. If an 'action'
373 was provided for the barrier, one of the threads will have executed
374 that callback prior to returning.
375 Returns an individual index number from 0 to 'parties-1'.
376 """
377 if timeout is None:
378 timeout = self._timeout
379 with self._cond:
380 self._enter() # Block while the barrier drains.
381 index = self._count
382 self._count += 1
383 try:
384 if index + 1 == self._parties:
385 # We release the barrier
386 self._release()
387 else:
388 # We wait until someone releases us
389 self._wait(timeout)
390 return index
391 finally:
392 self._count -= 1
393 # Wake up any threads waiting for barrier to drain.
394 self._exit()
395
396 # Block until the barrier is ready for us, or raise an exception
397 # if it is broken.
398 def _enter(self):
399 while self._state in (-1, 1):
400 # It is draining or resetting, wait until done
401 self._cond.wait()
402 #see if the barrier is in a broken state
403 if self._state < 0:
404 raise BrokenBarrierError
405 assert self._state == 0
406
407 # Optionally run the 'action' and release the threads waiting
408 # in the barrier.
409 def _release(self):
410 try:
411 if self._action:
412 self._action()
413 # enter draining state
414 self._state = 1
415 self._cond.notify_all()
416 except:
417 #an exception during the _action handler. Break and reraise
418 self._break()
419 raise
420
421 # Wait in the barrier until we are relased. Raise an exception
422 # if the barrier is reset or broken.
423 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000424 if not self._cond.wait_for(lambda : self._state != 0, timeout):
425 #timed out. Break the barrier
426 self._break()
427 raise BrokenBarrierError
428 if self._state < 0:
429 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000430 assert self._state == 1
431
432 # If we are the last thread to exit the barrier, signal any threads
433 # waiting for the barrier to drain.
434 def _exit(self):
435 if self._count == 0:
436 if self._state in (-1, 1):
437 #resetting or draining
438 self._state = 0
439 self._cond.notify_all()
440
441 def reset(self):
442 """
443 Reset the barrier to the initial state.
444 Any threads currently waiting will get the BrokenBarrier exception
445 raised.
446 """
447 with self._cond:
448 if self._count > 0:
449 if self._state == 0:
450 #reset the barrier, waking up threads
451 self._state = -1
452 elif self._state == -2:
453 #was broken, set it to reset state
454 #which clears when the last thread exits
455 self._state = -1
456 else:
457 self._state = 0
458 self._cond.notify_all()
459
460 def abort(self):
461 """
462 Place the barrier into a 'broken' state.
463 Useful in case of error. Any currently waiting threads and
464 threads attempting to 'wait()' will have BrokenBarrierError
465 raised.
466 """
467 with self._cond:
468 self._break()
469
470 def _break(self):
471 # An internal error was detected. The barrier is set to
472 # a broken state all parties awakened.
473 self._state = -2
474 self._cond.notify_all()
475
476 @property
477 def parties(self):
478 """
479 Return the number of threads required to trip the barrier.
480 """
481 return self._parties
482
483 @property
484 def n_waiting(self):
485 """
486 Return the number of threads that are currently waiting at the barrier.
487 """
488 # We don't need synchronization here since this is an ephemeral result
489 # anyway. It returns the correct value in the steady state.
490 if self._state == 0:
491 return self._count
492 return 0
493
494 @property
495 def broken(self):
496 """
497 Return True if the barrier is in a broken state
498 """
499 return self._state == -2
500
501#exception raised by the Barrier class
502class BrokenBarrierError(RuntimeError): pass
503
504
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000505# Helper to generate new thread names
506_counter = 0
507def _newname(template="Thread-%d"):
508 global _counter
Raymond Hettinger720da572013-03-10 15:13:35 -0700509 _counter += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000510 return template % _counter
511
512# Active thread administration
513_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000514_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000515_limbo = {}
516
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200517# For debug and leak testing
518_dangling = WeakSet()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000519
520# Main class for threads
521
Victor Stinner135b6d82012-03-03 01:32:57 +0100522class Thread:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000523
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000524 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000525 # Need to store a reference to sys.exc_info for printing
526 # out exceptions when a thread tries to use a global var. during interp.
527 # shutdown and thus raises an exception about trying to perform some
528 # operation on/with a NoneType
529 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000530 # Keep sys.exc_clear too to clear the exception just before
531 # allowing .join() to return.
532 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000533
534 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100535 args=(), kwargs=None, *, daemon=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000536 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000537 if kwargs is None:
538 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000539 self._target = target
540 self._name = str(name or _newname())
541 self._args = args
542 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000543 if daemon is not None:
544 self._daemonic = daemon
545 else:
546 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000547 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000548 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000549 self._stopped = False
550 self._block = Condition(Lock())
551 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000552 # sys.stderr is not stored in the class like
553 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000554 self._stderr = _sys.stderr
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200555 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000556
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000557 def _reset_internal_locks(self):
558 # private! Called by _after_fork() to reset our internal locks as
559 # they may be in an invalid state leading to a deadlock or crash.
560 if hasattr(self, '_block'): # DummyThread deletes _block
561 self._block.__init__()
562 self._started._reset_internal_locks()
563
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000564 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000565 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000566 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000567 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000568 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000569 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000570 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000571 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000572 status += " daemon"
573 if self._ident is not None:
574 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000575 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000576
577 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000578 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000579 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000580
Benjamin Peterson672b8032008-06-11 19:14:14 +0000581 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000582 raise RuntimeError("threads can only be started once")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000583 with _active_limbo_lock:
584 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000585 try:
586 _start_new_thread(self._bootstrap, ())
587 except Exception:
588 with _active_limbo_lock:
589 del _limbo[self]
590 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000591 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000592
593 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000594 try:
595 if self._target:
596 self._target(*self._args, **self._kwargs)
597 finally:
598 # Avoid a refcycle if the thread is running a function with
599 # an argument that has a member that points to the thread.
600 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000601
Guido van Rossumd0648992007-08-20 19:25:41 +0000602 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000603 # Wrapper around the real bootstrap code that ignores
604 # exceptions during interpreter cleanup. Those typically
605 # happen when a daemon thread wakes up at an unfortunate
606 # moment, finds the world around it destroyed, and raises some
607 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000608 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000609 # don't help anybody, and they confuse users, so we suppress
610 # them. We suppress them only when it appears that the world
611 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000612 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000613 # reported. Also, we only suppress them for daemonic threads;
614 # if a non-daemonic encounters this, something else is wrong.
615 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000616 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000617 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000618 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000619 return
620 raise
621
Benjamin Petersond23f8222009-04-05 19:13:16 +0000622 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200623 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000624
Guido van Rossumd0648992007-08-20 19:25:41 +0000625 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000626 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000627 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000628 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000629 with _active_limbo_lock:
630 _active[self._ident] = self
631 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000632
633 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000634 _sys.settrace(_trace_hook)
635 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000636 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000637
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000638 try:
639 self.run()
640 except SystemExit:
Victor Stinner135b6d82012-03-03 01:32:57 +0100641 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000642 except:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000643 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000644 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000645 # _sys) in case sys.stderr was redefined since the creation of
646 # self.
647 if _sys:
648 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000649 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000650 else:
651 # Do the best job possible w/o a huge amt. of code to
652 # approximate a traceback (code ideas from
653 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000654 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000655 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000656 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000657 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000658 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000659 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000660 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000661 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000662 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000663 ' File "%s", line %s, in %s' %
664 (exc_tb.tb_frame.f_code.co_filename,
665 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000666 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000667 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000668 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000669 # Make sure that exc_tb gets deleted since it is a memory
670 # hog; deleting everything else is just for thoroughness
671 finally:
672 del exc_type, exc_value, exc_tb
Christian Heimesbbe741d2008-03-28 10:53:29 +0000673 finally:
674 # Prevent a race in
675 # test_threading.test_no_refcycle_through_target when
676 # the exception keeps the target alive past when we
677 # assert that it's dead.
678 #XXX self.__exc_clear()
679 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000680 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000681 with _active_limbo_lock:
682 self._stop()
683 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000684 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000685 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200686 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000687 except:
688 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000689
Guido van Rossumd0648992007-08-20 19:25:41 +0000690 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000691 self._block.acquire()
692 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000693 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000694 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000695
Guido van Rossumd0648992007-08-20 19:25:41 +0000696 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000697 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000698
Georg Brandl2067bfd2008-05-25 13:05:15 +0000699 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000700 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000701 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000702 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000703 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
704 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000705 # len(_active) is always <= 1 here, and any Thread instance created
706 # overwrites the (if any) thread currently registered in _active.
707 #
708 # An instance of _MainThread is always created by 'threading'. This
709 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000710 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000711 # same key in the dict. So when the _MainThread instance created by
712 # 'threading' tries to clean itself up when atexit calls this method
713 # it gets a KeyError if another Thread instance was created.
714 #
715 # This all means that KeyError from trying to delete something from
716 # _active if dummy_threading is being used is a red herring. But
717 # since it isn't if dummy_threading is *not* being used then don't
718 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000719
Christian Heimes969fe572008-01-25 11:23:10 +0000720 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000721 with _active_limbo_lock:
Victor Stinner2a129742011-05-30 23:02:52 +0200722 del _active[get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000723 # There must not be any python code between the previous line
724 # and after the lock is released. Otherwise a tracing function
725 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000726 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000727 except KeyError:
728 if 'dummy_threading' not in _sys.modules:
729 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000730
731 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000732 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000733 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000734 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000735 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000736 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000737 raise RuntimeError("cannot join current thread")
738
Christian Heimes969fe572008-01-25 11:23:10 +0000739 self._block.acquire()
740 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000741 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000742 while not self._stopped:
743 self._block.wait()
Brett Cannonad07ff22005-11-23 02:15:50 +0000744 else:
745 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000746 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000747 delay = deadline - _time()
748 if delay <= 0:
Brett Cannonad07ff22005-11-23 02:15:50 +0000749 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000750 self._block.wait(delay)
Christian Heimes969fe572008-01-25 11:23:10 +0000751 finally:
752 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000753
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000754 @property
755 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000756 assert self._initialized, "Thread.__init__() not called"
757 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000758
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000759 @name.setter
760 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000761 assert self._initialized, "Thread.__init__() not called"
762 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000763
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000764 @property
765 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000766 assert self._initialized, "Thread.__init__() not called"
767 return self._ident
768
Benjamin Peterson672b8032008-06-11 19:14:14 +0000769 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000770 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000771 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000772
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000773 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000774
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000775 @property
776 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000777 assert self._initialized, "Thread.__init__() not called"
778 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000779
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000780 @daemon.setter
781 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000782 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000783 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000784 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000785 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000786 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000787
Benjamin Peterson6640d722008-08-18 18:16:46 +0000788 def isDaemon(self):
789 return self.daemon
790
791 def setDaemon(self, daemonic):
792 self.daemon = daemonic
793
794 def getName(self):
795 return self.name
796
797 def setName(self, name):
798 self.name = name
799
Martin v. Löwis44f86962001-09-05 13:44:54 +0000800# The timer class was contributed by Itamar Shtull-Trauring
801
Éric Araujo0cdd4452011-07-28 00:28:28 +0200802class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +0000803 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000804
Martin v. Löwis44f86962001-09-05 13:44:54 +0000805 t = Timer(30.0, f, args=[], kwargs={})
806 t.start()
807 t.cancel() # stop the timer's action if it's still waiting
808 """
Tim Petersb64bec32001-09-18 02:26:39 +0000809
Martin v. Löwis44f86962001-09-05 13:44:54 +0000810 def __init__(self, interval, function, args=[], kwargs={}):
811 Thread.__init__(self)
812 self.interval = interval
813 self.function = function
814 self.args = args
815 self.kwargs = kwargs
816 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000817
Martin v. Löwis44f86962001-09-05 13:44:54 +0000818 def cancel(self):
819 """Stop the timer if it hasn't finished yet"""
820 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000821
Martin v. Löwis44f86962001-09-05 13:44:54 +0000822 def run(self):
823 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000824 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000825 self.function(*self.args, **self.kwargs)
826 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000827
828# Special thread class to represent the main thread
829# This is garbage collected through an exit handler
830
831class _MainThread(Thread):
832
833 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000834 Thread.__init__(self, name="MainThread", daemon=False)
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000835 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000836 self._set_ident()
837 with _active_limbo_lock:
838 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000839
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000840 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000841 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000842 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000843 while t:
844 t.join()
845 t = _pickSomeNonDaemonThread()
Guido van Rossumd0648992007-08-20 19:25:41 +0000846 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000847
848def _pickSomeNonDaemonThread():
849 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000850 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000851 return t
852 return None
853
854
855# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000856# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000857# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000858# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000859# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000860# They are marked as daemon threads so we won't wait for them
861# when we exit (conform previous semantics).
862
863class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000864
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000865 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000866 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +0000867
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000868 # Thread._block consumes an OS-level locking primitive, which
Tim Peters711906e2005-01-08 07:30:42 +0000869 # can never be used by a _DummyThread. Since a _DummyThread
870 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +0000871 del self._block
Tim Peters711906e2005-01-08 07:30:42 +0000872
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000873 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000874 self._set_ident()
875 with _active_limbo_lock:
876 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000877
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +0200878 def _stop(self):
879 pass
880
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000881 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000882 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000883
884
885# Global API functions
886
Benjamin Peterson672b8032008-06-11 19:14:14 +0000887def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000888 try:
Victor Stinner2a129742011-05-30 23:02:52 +0200889 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000890 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000891 return _DummyThread()
892
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000893currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000894
Benjamin Peterson672b8032008-06-11 19:14:14 +0000895def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000896 with _active_limbo_lock:
897 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000898
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000899activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000900
Antoine Pitroubdec11f2009-11-05 13:49:14 +0000901def _enumerate():
902 # Same as enumerate(), but without the lock. Internal use only.
903 return list(_active.values()) + list(_limbo.values())
904
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000905def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000906 with _active_limbo_lock:
907 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000908
Georg Brandl2067bfd2008-05-25 13:05:15 +0000909from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000910
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000911# Create the main thread object,
912# and make it available for the interpreter
913# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000914
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000915_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000916
Jim Fultond15dc062004-07-14 19:11:50 +0000917# get thread-local implementation, either from the thread
918# module, or from the python fallback
919
920try:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000921 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +0000922except ImportError:
923 from _threading_local import local
924
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000925
Jesse Nollera8513972008-07-17 16:49:17 +0000926def _after_fork():
927 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
928 # is called from PyOS_AfterFork. Here we cleanup threading module state
929 # that should not exist after a fork.
930
931 # Reset _active_limbo_lock, in case we forked while the lock was held
932 # by another (non-forked) thread. http://bugs.python.org/issue874900
933 global _active_limbo_lock
934 _active_limbo_lock = _allocate_lock()
935
936 # fork() only copied the current thread; clear references to others.
937 new_active = {}
938 current = current_thread()
939 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000940 for thread in _active.values():
Charles-François Natalib055bf62011-12-18 18:45:16 +0100941 # Any lock/condition variable may be currently locked or in an
942 # invalid state, so we reinitialize them.
943 thread._reset_internal_locks()
Jesse Nollera8513972008-07-17 16:49:17 +0000944 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000945 # There is only one active thread. We reset the ident to
946 # its new value since it can have changed.
Victor Stinner2a129742011-05-30 23:02:52 +0200947 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000948 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +0000949 new_active[ident] = thread
950 else:
951 # All the others are already stopped.
Charles-François Natalib055bf62011-12-18 18:45:16 +0100952 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +0000953
954 _limbo.clear()
955 _active.clear()
956 _active.update(new_active)
957 assert len(_active) == 1