blob: 415a993b0aa90a1760d67b010b62a70e3cef9c71 [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
Raymond Hettingerec4b1742013-03-10 17:57:28 -070013try:
14 from _itertools import islice as _slice
15 from _collections import deque as _deque
16except ImportError:
17 from itertools import islice as _islice
18 from collections import deque as _deque
Guido van Rossum7f5013a1998-04-09 22:01:42 +000019
Benjamin Petersonb3085c92008-09-01 23:09:31 +000020# Note regarding PEP 8 compliant names
21# This threading model was originally inspired by Java, and inherited
22# the convention of camelCase function and method names from that
23# language. Those originaly names are not in any imminent danger of
24# being deprecated (even for Py3k),so this module provides them as an
25# alias for the PEP 8 compliant names
26# Note that using the new PEP 8 compliant names facilitates substitution
27# with the multiprocessing module, which doesn't provide the old
28# Java inspired names.
29
Benjamin Peterson672b8032008-06-11 19:14:14 +000030__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000031 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
Benjamin Peterson7761b952011-08-02 13:05:47 -050032 'Timer', 'ThreadError', 'setprofile', 'settrace', 'local', 'stack_size']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000033
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000034# Rename some stuff so "from threading import *" is safe
Georg Brandl2067bfd2008-05-25 13:05:15 +000035_start_new_thread = _thread.start_new_thread
36_allocate_lock = _thread.allocate_lock
Victor Stinner2a129742011-05-30 23:02:52 +020037get_ident = _thread.get_ident
Georg Brandl2067bfd2008-05-25 13:05:15 +000038ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000039try:
40 _CRLock = _thread.RLock
41except AttributeError:
42 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000043TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000044del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000045
Guido van Rossum7f5013a1998-04-09 22:01:42 +000046
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000047# Support for profile and trace hooks
48
49_profile_hook = None
50_trace_hook = None
51
52def setprofile(func):
53 global _profile_hook
54 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000055
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000056def settrace(func):
57 global _trace_hook
58 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000059
60# Synchronization classes
61
62Lock = _allocate_lock
63
Victor Stinner135b6d82012-03-03 01:32:57 +010064def RLock(*args, **kwargs):
65 if _CRLock is None:
66 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +000067 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000068
Victor Stinner135b6d82012-03-03 01:32:57 +010069class _RLock:
Tim Petersb90f89a2001-01-15 03:26:36 +000070
Victor Stinner135b6d82012-03-03 01:32:57 +010071 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000072 self._block = _allocate_lock()
73 self._owner = None
74 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +000075
76 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +000077 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +000078 try:
79 owner = _active[owner].name
80 except KeyError:
81 pass
82 return "<%s owner=%r count=%d>" % (
83 self.__class__.__name__, owner, self._count)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000084
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000085 def acquire(self, blocking=True, timeout=-1):
Victor Stinner2a129742011-05-30 23:02:52 +020086 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +000087 if self._owner == me:
Raymond Hettinger720da572013-03-10 15:13:35 -070088 self._count += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000089 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000090 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +000091 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +000092 self._owner = me
93 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +000094 return rc
95
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000096 __enter__ = acquire
97
Guido van Rossum7f5013a1998-04-09 22:01:42 +000098 def release(self):
Victor Stinner2a129742011-05-30 23:02:52 +020099 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000100 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000101 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000102 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000103 self._owner = None
104 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000105
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000106 def __exit__(self, t, v, tb):
107 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000108
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000109 # Internal methods used by condition variables
110
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000111 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000112 self._block.acquire()
113 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000114
115 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200116 if self._count == 0:
117 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000118 count = self._count
119 self._count = 0
120 owner = self._owner
121 self._owner = None
122 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000123 return (count, owner)
124
125 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200126 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000127
Antoine Pitrou434736a2009-11-10 18:46:01 +0000128_PyRLock = _RLock
129
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000130
Victor Stinner135b6d82012-03-03 01:32:57 +0100131class Condition:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000132
Victor Stinner135b6d82012-03-03 01:32:57 +0100133 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000134 if lock is None:
135 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000136 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000137 # Export the lock's acquire() and release() methods
138 self.acquire = lock.acquire
139 self.release = lock.release
140 # If the lock defines _release_save() and/or _acquire_restore(),
141 # these override the default implementations (which just call
142 # release() and acquire() on the lock). Ditto for _is_owned().
143 try:
144 self._release_save = lock._release_save
145 except AttributeError:
146 pass
147 try:
148 self._acquire_restore = lock._acquire_restore
149 except AttributeError:
150 pass
151 try:
152 self._is_owned = lock._is_owned
153 except AttributeError:
154 pass
Raymond Hettingerec4b1742013-03-10 17:57:28 -0700155 self._waiters = _deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000156
Thomas Wouters477c8d52006-05-27 19:21:47 +0000157 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000158 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000159
Thomas Wouters477c8d52006-05-27 19:21:47 +0000160 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000161 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000162
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000163 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000164 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000165
166 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000167 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000168
169 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000170 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000171
172 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000173 # Return True if lock is owned by current_thread.
Jeremy Hyltonaf7fde72002-08-14 17:43:59 +0000174 # This method is called only if __lock doesn't have _is_owned().
Guido van Rossumd0648992007-08-20 19:25:41 +0000175 if self._lock.acquire(0):
176 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000177 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000178 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000179 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000180
181 def wait(self, timeout=None):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000182 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000183 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000184 waiter = _allocate_lock()
185 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000186 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000187 saved_state = self._release_save()
Tim Petersc951bf92001-04-02 20:15:57 +0000188 try: # restore state no matter what (e.g., KeyboardInterrupt)
189 if timeout is None:
190 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000191 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000192 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000193 if timeout > 0:
194 gotit = waiter.acquire(True, timeout)
195 else:
196 gotit = waiter.acquire(False)
Tim Petersc951bf92001-04-02 20:15:57 +0000197 if not gotit:
Tim Petersc951bf92001-04-02 20:15:57 +0000198 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000199 self._waiters.remove(waiter)
Tim Petersc951bf92001-04-02 20:15:57 +0000200 except ValueError:
201 pass
Georg Brandlb9a43912010-10-28 09:03:20 +0000202 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000203 finally:
204 self._acquire_restore(saved_state)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000205
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000206 def wait_for(self, predicate, timeout=None):
207 endtime = None
208 waittime = timeout
209 result = predicate()
210 while not result:
211 if waittime is not None:
212 if endtime is None:
213 endtime = _time() + waittime
214 else:
215 waittime = endtime - _time()
216 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000217 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000218 self.wait(waittime)
219 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000220 return result
221
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000222 def notify(self, n=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000223 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000224 raise RuntimeError("cannot notify on un-acquired lock")
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700225 all_waiters = self._waiters
226 waiters_to_notify = _deque(_islice(all_waiters, n))
227 if not waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000228 return
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700229 for waiter in waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000230 waiter.release()
231 try:
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700232 all_waiters.remove(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000233 except ValueError:
234 pass
235
Benjamin Peterson672b8032008-06-11 19:14:14 +0000236 def notify_all(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000237 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000238
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000239 notifyAll = notify_all
240
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000241
Victor Stinner135b6d82012-03-03 01:32:57 +0100242class Semaphore:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000243
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000244 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000245
Victor Stinner135b6d82012-03-03 01:32:57 +0100246 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000247 if value < 0:
248 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000249 self._cond = Condition(Lock())
250 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000251
Antoine Pitrou0454af92010-04-17 23:51:58 +0000252 def acquire(self, blocking=True, timeout=None):
253 if not blocking and timeout is not None:
254 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000255 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000256 endtime = None
Guido van Rossumd0648992007-08-20 19:25:41 +0000257 self._cond.acquire()
258 while self._value == 0:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000259 if not blocking:
260 break
Antoine Pitrou0454af92010-04-17 23:51:58 +0000261 if timeout is not None:
262 if endtime is None:
263 endtime = _time() + timeout
264 else:
265 timeout = endtime - _time()
266 if timeout <= 0:
267 break
268 self._cond.wait(timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000269 else:
Raymond Hettinger720da572013-03-10 15:13:35 -0700270 self._value -= 1
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000271 rc = True
Guido van Rossumd0648992007-08-20 19:25:41 +0000272 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000273 return rc
274
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000275 __enter__ = acquire
276
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000277 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000278 self._cond.acquire()
Raymond Hettinger720da572013-03-10 15:13:35 -0700279 self._value += 1
Guido van Rossumd0648992007-08-20 19:25:41 +0000280 self._cond.notify()
281 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000282
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000283 def __exit__(self, t, v, tb):
284 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000285
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000286
Éric Araujo0cdd4452011-07-28 00:28:28 +0200287class BoundedSemaphore(Semaphore):
Skip Montanaroe428bb72001-08-20 20:27:58 +0000288 """Semaphore that checks that # releases is <= # acquires"""
Victor Stinner135b6d82012-03-03 01:32:57 +0100289 def __init__(self, value=1):
290 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000291 self._initial_value = value
292
293 def release(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000294 if self._value >= self._initial_value:
Collin Winterce36ad82007-08-30 01:19:48 +0000295 raise ValueError("Semaphore released too many times")
Éric Araujo0cdd4452011-07-28 00:28:28 +0200296 return Semaphore.release(self)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000297
298
Victor Stinner135b6d82012-03-03 01:32:57 +0100299class Event:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000300
301 # After Tim Peters' event class (without is_posted())
302
Victor Stinner135b6d82012-03-03 01:32:57 +0100303 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000304 self._cond = Condition(Lock())
305 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000306
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000307 def _reset_internal_locks(self):
308 # private! called by Thread._reset_internal_locks by _after_fork()
309 self._cond.__init__()
310
Benjamin Peterson672b8032008-06-11 19:14:14 +0000311 def is_set(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000312 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000313
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000314 isSet = is_set
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000315
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000316 def set(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000317 self._cond.acquire()
318 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000319 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000320 self._cond.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000321 finally:
322 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000323
324 def clear(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000325 self._cond.acquire()
326 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000327 self._flag = False
Christian Heimes969fe572008-01-25 11:23:10 +0000328 finally:
329 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000330
331 def wait(self, timeout=None):
Christian Heimes969fe572008-01-25 11:23:10 +0000332 self._cond.acquire()
333 try:
Charles-François Natalided03482012-01-07 18:24:56 +0100334 signaled = self._flag
335 if not signaled:
336 signaled = self._cond.wait(timeout)
337 return signaled
Christian Heimes969fe572008-01-25 11:23:10 +0000338 finally:
339 self._cond.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000340
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000341
342# A barrier class. Inspired in part by the pthread_barrier_* api and
343# the CyclicBarrier class from Java. See
344# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
345# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
346# CyclicBarrier.html
347# for information.
348# We maintain two main states, 'filling' and 'draining' enabling the barrier
349# to be cyclic. Threads are not allowed into it until it has fully drained
350# since the previous cycle. In addition, a 'resetting' state exists which is
351# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300352# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100353class Barrier:
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000354 """
355 Barrier. Useful for synchronizing a fixed number of threads
356 at known synchronization points. Threads block on 'wait()' and are
357 simultaneously once they have all made that call.
358 """
Victor Stinner135b6d82012-03-03 01:32:57 +0100359 def __init__(self, parties, action=None, timeout=None):
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000360 """
361 Create a barrier, initialised to 'parties' threads.
362 'action' is a callable which, when supplied, will be called
363 by one of the threads after they have all entered the
364 barrier and just prior to releasing them all.
365 If a 'timeout' is provided, it is uses as the default for
366 all subsequent 'wait()' calls.
367 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000368 self._cond = Condition(Lock())
369 self._action = action
370 self._timeout = timeout
371 self._parties = parties
372 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
373 self._count = 0
374
375 def wait(self, timeout=None):
376 """
377 Wait for the barrier. When the specified number of threads have
378 started waiting, they are all simultaneously awoken. If an 'action'
379 was provided for the barrier, one of the threads will have executed
380 that callback prior to returning.
381 Returns an individual index number from 0 to 'parties-1'.
382 """
383 if timeout is None:
384 timeout = self._timeout
385 with self._cond:
386 self._enter() # Block while the barrier drains.
387 index = self._count
388 self._count += 1
389 try:
390 if index + 1 == self._parties:
391 # We release the barrier
392 self._release()
393 else:
394 # We wait until someone releases us
395 self._wait(timeout)
396 return index
397 finally:
398 self._count -= 1
399 # Wake up any threads waiting for barrier to drain.
400 self._exit()
401
402 # Block until the barrier is ready for us, or raise an exception
403 # if it is broken.
404 def _enter(self):
405 while self._state in (-1, 1):
406 # It is draining or resetting, wait until done
407 self._cond.wait()
408 #see if the barrier is in a broken state
409 if self._state < 0:
410 raise BrokenBarrierError
411 assert self._state == 0
412
413 # Optionally run the 'action' and release the threads waiting
414 # in the barrier.
415 def _release(self):
416 try:
417 if self._action:
418 self._action()
419 # enter draining state
420 self._state = 1
421 self._cond.notify_all()
422 except:
423 #an exception during the _action handler. Break and reraise
424 self._break()
425 raise
426
427 # Wait in the barrier until we are relased. Raise an exception
428 # if the barrier is reset or broken.
429 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000430 if not self._cond.wait_for(lambda : self._state != 0, timeout):
431 #timed out. Break the barrier
432 self._break()
433 raise BrokenBarrierError
434 if self._state < 0:
435 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000436 assert self._state == 1
437
438 # If we are the last thread to exit the barrier, signal any threads
439 # waiting for the barrier to drain.
440 def _exit(self):
441 if self._count == 0:
442 if self._state in (-1, 1):
443 #resetting or draining
444 self._state = 0
445 self._cond.notify_all()
446
447 def reset(self):
448 """
449 Reset the barrier to the initial state.
450 Any threads currently waiting will get the BrokenBarrier exception
451 raised.
452 """
453 with self._cond:
454 if self._count > 0:
455 if self._state == 0:
456 #reset the barrier, waking up threads
457 self._state = -1
458 elif self._state == -2:
459 #was broken, set it to reset state
460 #which clears when the last thread exits
461 self._state = -1
462 else:
463 self._state = 0
464 self._cond.notify_all()
465
466 def abort(self):
467 """
468 Place the barrier into a 'broken' state.
469 Useful in case of error. Any currently waiting threads and
470 threads attempting to 'wait()' will have BrokenBarrierError
471 raised.
472 """
473 with self._cond:
474 self._break()
475
476 def _break(self):
477 # An internal error was detected. The barrier is set to
478 # a broken state all parties awakened.
479 self._state = -2
480 self._cond.notify_all()
481
482 @property
483 def parties(self):
484 """
485 Return the number of threads required to trip the barrier.
486 """
487 return self._parties
488
489 @property
490 def n_waiting(self):
491 """
492 Return the number of threads that are currently waiting at the barrier.
493 """
494 # We don't need synchronization here since this is an ephemeral result
495 # anyway. It returns the correct value in the steady state.
496 if self._state == 0:
497 return self._count
498 return 0
499
500 @property
501 def broken(self):
502 """
503 Return True if the barrier is in a broken state
504 """
505 return self._state == -2
506
507#exception raised by the Barrier class
508class BrokenBarrierError(RuntimeError): pass
509
510
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000511# Helper to generate new thread names
512_counter = 0
513def _newname(template="Thread-%d"):
514 global _counter
Raymond Hettinger720da572013-03-10 15:13:35 -0700515 _counter += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000516 return template % _counter
517
518# Active thread administration
519_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000520_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000521_limbo = {}
522
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200523# For debug and leak testing
524_dangling = WeakSet()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000525
526# Main class for threads
527
Victor Stinner135b6d82012-03-03 01:32:57 +0100528class Thread:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000529
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000530 __initialized = False
Brett Cannoncc4e9352004-07-03 03:52:35 +0000531 # Need to store a reference to sys.exc_info for printing
532 # out exceptions when a thread tries to use a global var. during interp.
533 # shutdown and thus raises an exception about trying to perform some
534 # operation on/with a NoneType
535 __exc_info = _sys.exc_info
Christian Heimesbbe741d2008-03-28 10:53:29 +0000536 # Keep sys.exc_clear too to clear the exception just before
537 # allowing .join() to return.
538 #XXX __exc_clear = _sys.exc_clear
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000539
540 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100541 args=(), kwargs=None, *, daemon=None):
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000542 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000543 if kwargs is None:
544 kwargs = {}
Guido van Rossumd0648992007-08-20 19:25:41 +0000545 self._target = target
546 self._name = str(name or _newname())
547 self._args = args
548 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000549 if daemon is not None:
550 self._daemonic = daemon
551 else:
552 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000553 self._ident = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000554 self._started = Event()
Guido van Rossumd0648992007-08-20 19:25:41 +0000555 self._stopped = False
556 self._block = Condition(Lock())
557 self._initialized = True
Brett Cannoncc4e9352004-07-03 03:52:35 +0000558 # sys.stderr is not stored in the class like
559 # sys.exc_info since it can be changed between instances
Guido van Rossumd0648992007-08-20 19:25:41 +0000560 self._stderr = _sys.stderr
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200561 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000562
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000563 def _reset_internal_locks(self):
564 # private! Called by _after_fork() to reset our internal locks as
565 # they may be in an invalid state leading to a deadlock or crash.
566 if hasattr(self, '_block'): # DummyThread deletes _block
567 self._block.__init__()
568 self._started._reset_internal_locks()
569
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000570 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000571 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000572 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000573 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000574 status = "started"
Guido van Rossumd0648992007-08-20 19:25:41 +0000575 if self._stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000576 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000577 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000578 status += " daemon"
579 if self._ident is not None:
580 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000581 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000582
583 def start(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000584 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000585 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000586
Benjamin Peterson672b8032008-06-11 19:14:14 +0000587 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000588 raise RuntimeError("threads can only be started once")
Benjamin Petersond23f8222009-04-05 19:13:16 +0000589 with _active_limbo_lock:
590 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000591 try:
592 _start_new_thread(self._bootstrap, ())
593 except Exception:
594 with _active_limbo_lock:
595 del _limbo[self]
596 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000597 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000598
599 def run(self):
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000600 try:
601 if self._target:
602 self._target(*self._args, **self._kwargs)
603 finally:
604 # Avoid a refcycle if the thread is running a function with
605 # an argument that has a member that points to the thread.
606 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000607
Guido van Rossumd0648992007-08-20 19:25:41 +0000608 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000609 # Wrapper around the real bootstrap code that ignores
610 # exceptions during interpreter cleanup. Those typically
611 # happen when a daemon thread wakes up at an unfortunate
612 # moment, finds the world around it destroyed, and raises some
613 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000614 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000615 # don't help anybody, and they confuse users, so we suppress
616 # them. We suppress them only when it appears that the world
617 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000618 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000619 # reported. Also, we only suppress them for daemonic threads;
620 # if a non-daemonic encounters this, something else is wrong.
621 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000622 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000623 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000624 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000625 return
626 raise
627
Benjamin Petersond23f8222009-04-05 19:13:16 +0000628 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200629 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000630
Guido van Rossumd0648992007-08-20 19:25:41 +0000631 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000632 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000633 self._set_ident()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000634 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000635 with _active_limbo_lock:
636 _active[self._ident] = self
637 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000638
639 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000640 _sys.settrace(_trace_hook)
641 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000642 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000643
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000644 try:
645 self.run()
646 except SystemExit:
Victor Stinner135b6d82012-03-03 01:32:57 +0100647 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000648 except:
Brett Cannoncc4e9352004-07-03 03:52:35 +0000649 # If sys.stderr is no more (most likely from interpreter
Guido van Rossumd0648992007-08-20 19:25:41 +0000650 # shutdown) use self._stderr. Otherwise still use sys (as in
Brett Cannoncc4e9352004-07-03 03:52:35 +0000651 # _sys) in case sys.stderr was redefined since the creation of
652 # self.
653 if _sys:
654 _sys.stderr.write("Exception in thread %s:\n%s\n" %
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000655 (self.name, _format_exc()))
Brett Cannoncc4e9352004-07-03 03:52:35 +0000656 else:
657 # Do the best job possible w/o a huge amt. of code to
658 # approximate a traceback (code ideas from
659 # Lib/traceback.py)
Guido van Rossumd0648992007-08-20 19:25:41 +0000660 exc_type, exc_value, exc_tb = self._exc_info()
Brett Cannoncc4e9352004-07-03 03:52:35 +0000661 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000662 print((
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000663 "Exception in thread " + self.name +
Guido van Rossumd0648992007-08-20 19:25:41 +0000664 " (most likely raised during interpreter shutdown):"), file=self._stderr)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000665 print((
Guido van Rossumd0648992007-08-20 19:25:41 +0000666 "Traceback (most recent call last):"), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000667 while exc_tb:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000668 print((
Brett Cannoncc4e9352004-07-03 03:52:35 +0000669 ' File "%s", line %s, in %s' %
670 (exc_tb.tb_frame.f_code.co_filename,
671 exc_tb.tb_lineno,
Guido van Rossumd0648992007-08-20 19:25:41 +0000672 exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000673 exc_tb = exc_tb.tb_next
Guido van Rossumd0648992007-08-20 19:25:41 +0000674 print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
Brett Cannoncc4e9352004-07-03 03:52:35 +0000675 # Make sure that exc_tb gets deleted since it is a memory
676 # hog; deleting everything else is just for thoroughness
677 finally:
678 del exc_type, exc_value, exc_tb
Christian Heimesbbe741d2008-03-28 10:53:29 +0000679 finally:
680 # Prevent a race in
681 # test_threading.test_no_refcycle_through_target when
682 # the exception keeps the target alive past when we
683 # assert that it's dead.
684 #XXX self.__exc_clear()
685 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000686 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000687 with _active_limbo_lock:
688 self._stop()
689 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000690 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000691 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200692 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000693 except:
694 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000695
Guido van Rossumd0648992007-08-20 19:25:41 +0000696 def _stop(self):
Christian Heimes969fe572008-01-25 11:23:10 +0000697 self._block.acquire()
698 self._stopped = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000699 self._block.notify_all()
Christian Heimes969fe572008-01-25 11:23:10 +0000700 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000701
Guido van Rossumd0648992007-08-20 19:25:41 +0000702 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +0000703 "Remove current thread from the dict of currently running threads."
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000704
Georg Brandl2067bfd2008-05-25 13:05:15 +0000705 # Notes about running with _dummy_thread:
Tim Peters21429932004-07-21 03:36:52 +0000706 #
Georg Brandl2067bfd2008-05-25 13:05:15 +0000707 # Must take care to not raise an exception if _dummy_thread is being
Tim Peters21429932004-07-21 03:36:52 +0000708 # used (and thus this module is being used as an instance of
Georg Brandl2067bfd2008-05-25 13:05:15 +0000709 # dummy_threading). _dummy_thread.get_ident() always returns -1 since
710 # there is only one thread if _dummy_thread is being used. Thus
Tim Peters21429932004-07-21 03:36:52 +0000711 # len(_active) is always <= 1 here, and any Thread instance created
712 # overwrites the (if any) thread currently registered in _active.
713 #
714 # An instance of _MainThread is always created by 'threading'. This
715 # gets overwritten the instant an instance of Thread is created; both
Georg Brandl2067bfd2008-05-25 13:05:15 +0000716 # threads return -1 from _dummy_thread.get_ident() and thus have the
Tim Peters21429932004-07-21 03:36:52 +0000717 # same key in the dict. So when the _MainThread instance created by
718 # 'threading' tries to clean itself up when atexit calls this method
719 # it gets a KeyError if another Thread instance was created.
720 #
721 # This all means that KeyError from trying to delete something from
722 # _active if dummy_threading is being used is a red herring. But
723 # since it isn't if dummy_threading is *not* being used then don't
724 # hide the exception.
Brett Cannon8b3d92a2004-07-21 02:21:58 +0000725
Christian Heimes969fe572008-01-25 11:23:10 +0000726 try:
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000727 with _active_limbo_lock:
Victor Stinner2a129742011-05-30 23:02:52 +0200728 del _active[get_ident()]
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000729 # There must not be any python code between the previous line
730 # and after the lock is released. Otherwise a tracing function
731 # could try to acquire the lock again in the same thread, (in
Benjamin Peterson672b8032008-06-11 19:14:14 +0000732 # current_thread()), and would block.
Neal Norwitzf5c7c2e2008-04-05 04:47:45 +0000733 except KeyError:
734 if 'dummy_threading' not in _sys.modules:
735 raise
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000736
737 def join(self, timeout=None):
Guido van Rossumd0648992007-08-20 19:25:41 +0000738 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000739 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000740 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000741 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000742 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000743 raise RuntimeError("cannot join current thread")
744
Christian Heimes969fe572008-01-25 11:23:10 +0000745 self._block.acquire()
746 try:
Brett Cannonad07ff22005-11-23 02:15:50 +0000747 if timeout is None:
Guido van Rossumd0648992007-08-20 19:25:41 +0000748 while not self._stopped:
749 self._block.wait()
Brett Cannonad07ff22005-11-23 02:15:50 +0000750 else:
751 deadline = _time() + timeout
Guido van Rossumd0648992007-08-20 19:25:41 +0000752 while not self._stopped:
Brett Cannonad07ff22005-11-23 02:15:50 +0000753 delay = deadline - _time()
754 if delay <= 0:
Brett Cannonad07ff22005-11-23 02:15:50 +0000755 break
Guido van Rossumd0648992007-08-20 19:25:41 +0000756 self._block.wait(delay)
Christian Heimes969fe572008-01-25 11:23:10 +0000757 finally:
758 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000759
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000760 @property
761 def name(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000762 assert self._initialized, "Thread.__init__() not called"
763 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000764
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000765 @name.setter
766 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +0000767 assert self._initialized, "Thread.__init__() not called"
768 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000769
Benjamin Peterson773c17b2008-08-18 16:45:31 +0000770 @property
771 def ident(self):
Georg Brandl0c77a822008-06-10 16:37:50 +0000772 assert self._initialized, "Thread.__init__() not called"
773 return self._ident
774
Benjamin Peterson672b8032008-06-11 19:14:14 +0000775 def is_alive(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000776 assert self._initialized, "Thread.__init__() not called"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000777 return self._started.is_set() and not self._stopped
Tim Petersb90f89a2001-01-15 03:26:36 +0000778
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000779 isAlive = is_alive
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000780
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000781 @property
782 def daemon(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000783 assert self._initialized, "Thread.__init__() not called"
784 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000785
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000786 @daemon.setter
787 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +0000788 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000789 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +0000790 if self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000791 raise RuntimeError("cannot set daemon status of active thread");
Guido van Rossumd0648992007-08-20 19:25:41 +0000792 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000793
Benjamin Peterson6640d722008-08-18 18:16:46 +0000794 def isDaemon(self):
795 return self.daemon
796
797 def setDaemon(self, daemonic):
798 self.daemon = daemonic
799
800 def getName(self):
801 return self.name
802
803 def setName(self, name):
804 self.name = name
805
Martin v. Löwis44f86962001-09-05 13:44:54 +0000806# The timer class was contributed by Itamar Shtull-Trauring
807
Éric Araujo0cdd4452011-07-28 00:28:28 +0200808class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +0000809 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +0000810
Martin v. Löwis44f86962001-09-05 13:44:54 +0000811 t = Timer(30.0, f, args=[], kwargs={})
812 t.start()
813 t.cancel() # stop the timer's action if it's still waiting
814 """
Tim Petersb64bec32001-09-18 02:26:39 +0000815
Martin v. Löwis44f86962001-09-05 13:44:54 +0000816 def __init__(self, interval, function, args=[], kwargs={}):
817 Thread.__init__(self)
818 self.interval = interval
819 self.function = function
820 self.args = args
821 self.kwargs = kwargs
822 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +0000823
Martin v. Löwis44f86962001-09-05 13:44:54 +0000824 def cancel(self):
825 """Stop the timer if it hasn't finished yet"""
826 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +0000827
Martin v. Löwis44f86962001-09-05 13:44:54 +0000828 def run(self):
829 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +0000830 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +0000831 self.function(*self.args, **self.kwargs)
832 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000833
834# Special thread class to represent the main thread
835# This is garbage collected through an exit handler
836
837class _MainThread(Thread):
838
839 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000840 Thread.__init__(self, name="MainThread", daemon=False)
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000841 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000842 self._set_ident()
843 with _active_limbo_lock:
844 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000845
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000846 def _exitfunc(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000847 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000848 t = _pickSomeNonDaemonThread()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000849 while t:
850 t.join()
851 t = _pickSomeNonDaemonThread()
Guido van Rossumd0648992007-08-20 19:25:41 +0000852 self._delete()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000853
854def _pickSomeNonDaemonThread():
855 for t in enumerate():
Benjamin Petersonfdbea962008-08-18 17:33:47 +0000856 if not t.daemon and t.is_alive():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000857 return t
858 return None
859
860
861# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +0000862# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000863# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +0000864# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +0000865# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000866# They are marked as daemon threads so we won't wait for them
867# when we exit (conform previous semantics).
868
869class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +0000870
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000871 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000872 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +0000873
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000874 # Thread._block consumes an OS-level locking primitive, which
Tim Peters711906e2005-01-08 07:30:42 +0000875 # can never be used by a _DummyThread. Since a _DummyThread
876 # instance is immortal, that's bad, so release this resource.
Guido van Rossumd0648992007-08-20 19:25:41 +0000877 del self._block
Tim Peters711906e2005-01-08 07:30:42 +0000878
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000879 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000880 self._set_ident()
881 with _active_limbo_lock:
882 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000883
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +0200884 def _stop(self):
885 pass
886
Neal Norwitz45bec8c2002-02-19 03:01:36 +0000887 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000888 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000889
890
891# Global API functions
892
Benjamin Peterson672b8032008-06-11 19:14:14 +0000893def current_thread():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000894 try:
Victor Stinner2a129742011-05-30 23:02:52 +0200895 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000896 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000897 return _DummyThread()
898
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000899currentThread = current_thread
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000900
Benjamin Peterson672b8032008-06-11 19:14:14 +0000901def active_count():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000902 with _active_limbo_lock:
903 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000904
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000905activeCount = active_count
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000906
Antoine Pitroubdec11f2009-11-05 13:49:14 +0000907def _enumerate():
908 # Same as enumerate(), but without the lock. Internal use only.
909 return list(_active.values()) + list(_limbo.values())
910
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000911def enumerate():
Benjamin Petersond23f8222009-04-05 19:13:16 +0000912 with _active_limbo_lock:
913 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000914
Georg Brandl2067bfd2008-05-25 13:05:15 +0000915from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000916
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000917# Create the main thread object,
918# and make it available for the interpreter
919# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000920
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000921_shutdown = _MainThread()._exitfunc
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000922
Jim Fultond15dc062004-07-14 19:11:50 +0000923# get thread-local implementation, either from the thread
924# module, or from the python fallback
925
926try:
Georg Brandl2067bfd2008-05-25 13:05:15 +0000927 from _thread import _local as local
Jim Fultond15dc062004-07-14 19:11:50 +0000928except ImportError:
929 from _threading_local import local
930
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000931
Jesse Nollera8513972008-07-17 16:49:17 +0000932def _after_fork():
933 # This function is called by Python/ceval.c:PyEval_ReInitThreads which
934 # is called from PyOS_AfterFork. Here we cleanup threading module state
935 # that should not exist after a fork.
936
937 # Reset _active_limbo_lock, in case we forked while the lock was held
938 # by another (non-forked) thread. http://bugs.python.org/issue874900
939 global _active_limbo_lock
940 _active_limbo_lock = _allocate_lock()
941
942 # fork() only copied the current thread; clear references to others.
943 new_active = {}
944 current = current_thread()
945 with _active_limbo_lock:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000946 for thread in _active.values():
Charles-François Natalib055bf62011-12-18 18:45:16 +0100947 # Any lock/condition variable may be currently locked or in an
948 # invalid state, so we reinitialize them.
949 thread._reset_internal_locks()
Jesse Nollera8513972008-07-17 16:49:17 +0000950 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000951 # There is only one active thread. We reset the ident to
952 # its new value since it can have changed.
Victor Stinner2a129742011-05-30 23:02:52 +0200953 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +0000954 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +0000955 new_active[ident] = thread
956 else:
957 # All the others are already stopped.
Charles-François Natalib055bf62011-12-18 18:45:16 +0100958 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +0000959
960 _limbo.clear()
961 _active.clear()
962 _active.update(new_active)
963 assert len(_active) == 1