threading.py 48 KB
Newer Older
1
"""Thread module emulating a subset of Java's threading model."""
2

3
import sys as _sys
4
import _thread
5

6
from time import monotonic as _time
7
from traceback import format_exc as _format_exc
8
from _weakrefset import WeakSet
9
from itertools import islice as _islice, count as _count
10 11
try:
    from _collections import deque as _deque
12
except ImportError:
13
    from collections import deque as _deque
14

15 16 17
# Note regarding PEP 8 compliant names
#  This threading model was originally inspired by Java, and inherited
# the convention of camelCase function and method names from that
18
# language. Those original names are not in any imminent danger of
19 20 21 22 23 24
# being deprecated (even for Py3k),so this module provides them as an
# alias for the PEP 8 compliant names
# Note that using the new PEP 8 compliant names facilitates substitution
# with the multiprocessing module, which doesn't provide the old
# Java inspired names.

25
__all__ = ['active_count', 'Condition', 'current_thread', 'enumerate', 'Event',
26
           'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread', 'Barrier',
27
           'Timer', 'ThreadError', 'setprofile', 'settrace', 'local', 'stack_size']
28

29
# Rename some stuff so "from threading import *" is safe
30 31
_start_new_thread = _thread.start_new_thread
_allocate_lock = _thread.allocate_lock
32
_set_sentinel = _thread._set_sentinel
33
get_ident = _thread.get_ident
34
ThreadError = _thread.error
35 36 37 38
try:
    _CRLock = _thread.RLock
except AttributeError:
    _CRLock = None
39
TIMEOUT_MAX = _thread.TIMEOUT_MAX
40
del _thread
41 42


43 44 45 46 47 48
# Support for profile and trace hooks

_profile_hook = None
_trace_hook = None

def setprofile(func):
49 50 51 52 53 54
    """Set a profile function for all threads started from the threading module.

    The func will be passed to sys.setprofile() for each thread, before its
    run() method is called.

    """
55 56
    global _profile_hook
    _profile_hook = func
Tim Peters's avatar
Tim Peters committed
57

58
def settrace(func):
59 60 61 62 63 64
    """Set a trace function for all threads started from the threading module.

    The func will be passed to sys.settrace() for each thread, before its run()
    method is called.

    """
65 66
    global _trace_hook
    _trace_hook = func
67 68 69 70 71

# Synchronization classes

Lock = _allocate_lock

72
def RLock(*args, **kwargs):
73 74 75 76 77 78 79 80
    """Factory function that returns a new reentrant lock.

    A reentrant lock must be released by the thread that acquired it. Once a
    thread has acquired a reentrant lock, the same thread may acquire it again
    without blocking; the thread must release it once for each time it has
    acquired it.

    """
81 82
    if _CRLock is None:
        return _PyRLock(*args, **kwargs)
83
    return _CRLock(*args, **kwargs)
84

85
class _RLock:
86 87 88 89 90 91 92 93
    """This class implements reentrant lock objects.

    A reentrant lock must be released by the thread that acquired it. Once a
    thread has acquired a reentrant lock, the same thread may acquire it
    again without blocking; the thread must release it once for each time it
    has acquired it.

    """
Tim Peters's avatar
Tim Peters committed
94

95
    def __init__(self):
96 97 98
        self._block = _allocate_lock()
        self._owner = None
        self._count = 0
99 100

    def __repr__(self):
101
        owner = self._owner
102 103 104 105
        try:
            owner = _active[owner].name
        except KeyError:
            pass
106 107 108 109 110 111 112 113
        return "<%s %s.%s object owner=%r count=%d at %s>" % (
            "locked" if self._block.locked() else "unlocked",
            self.__class__.__module__,
            self.__class__.__qualname__,
            owner,
            self._count,
            hex(id(self))
        )
114

115
    def acquire(self, blocking=True, timeout=-1):
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        """Acquire a lock, blocking or non-blocking.

        When invoked without arguments: if this thread already owns the lock,
        increment the recursion level by one, and return immediately. Otherwise,
        if another thread owns the lock, block until the lock is unlocked. Once
        the lock is unlocked (not owned by any thread), then grab ownership, set
        the recursion level to one, and return. If more than one thread is
        blocked waiting until the lock is unlocked, only one at a time will be
        able to grab ownership of the lock. There is no return value in this
        case.

        When invoked with the blocking argument set to true, do the same thing
        as when called without arguments, and return true.

        When invoked with the blocking argument set to false, do not block. If a
        call without an argument would block, return false immediately;
        otherwise, do the same thing as when called without arguments, and
        return true.

        When invoked with the floating-point timeout argument set to a positive
        value, block for at most the number of seconds specified by timeout
        and as long as the lock cannot be acquired.  Return true if the lock has
        been acquired, false if the timeout has elapsed.

        """
141
        me = get_ident()
142
        if self._owner == me:
143
            self._count += 1
144
            return 1
145
        rc = self._block.acquire(blocking, timeout)
146
        if rc:
147 148
            self._owner = me
            self._count = 1
149 150
        return rc

151 152
    __enter__ = acquire

153
    def release(self):
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        """Release a lock, decrementing the recursion level.

        If after the decrement it is zero, reset the lock to unlocked (not owned
        by any thread), and if any other threads are blocked waiting for the
        lock to become unlocked, allow exactly one of them to proceed. If after
        the decrement the recursion level is still nonzero, the lock remains
        locked and owned by the calling thread.

        Only call this method when the calling thread owns the lock. A
        RuntimeError is raised if this method is called when the lock is
        unlocked.

        There is no return value.

        """
169
        if self._owner != get_ident():
Georg Brandl's avatar
Georg Brandl committed
170
            raise RuntimeError("cannot release un-acquired lock")
171
        self._count = count = self._count - 1
172
        if not count:
173 174
            self._owner = None
            self._block.release()
175

176 177 178
    def __exit__(self, t, v, tb):
        self.release()

179 180
    # Internal methods used by condition variables

181
    def _acquire_restore(self, state):
182 183
        self._block.acquire()
        self._count, self._owner = state
184 185

    def _release_save(self):
186 187
        if self._count == 0:
            raise RuntimeError("cannot release un-acquired lock")
188 189 190 191 192
        count = self._count
        self._count = 0
        owner = self._owner
        self._owner = None
        self._block.release()
193 194 195
        return (count, owner)

    def _is_owned(self):
196
        return self._owner == get_ident()
197

198 199
_PyRLock = _RLock

200

201
class Condition:
202 203 204 205 206 207 208 209 210 211
    """Class that implements a condition variable.

    A condition variable allows one or more threads to wait until they are
    notified by another thread.

    If the lock argument is given and not None, it must be a Lock or RLock
    object, and it is used as the underlying lock. Otherwise, a new RLock object
    is created and used as the underlying lock.

    """
212

213
    def __init__(self, lock=None):
214 215
        if lock is None:
            lock = RLock()
216
        self._lock = lock
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
        # Export the lock's acquire() and release() methods
        self.acquire = lock.acquire
        self.release = lock.release
        # If the lock defines _release_save() and/or _acquire_restore(),
        # these override the default implementations (which just call
        # release() and acquire() on the lock).  Ditto for _is_owned().
        try:
            self._release_save = lock._release_save
        except AttributeError:
            pass
        try:
            self._acquire_restore = lock._acquire_restore
        except AttributeError:
            pass
        try:
            self._is_owned = lock._is_owned
        except AttributeError:
            pass
235
        self._waiters = _deque()
236

237
    def __enter__(self):
238
        return self._lock.__enter__()
239

240
    def __exit__(self, *args):
241
        return self._lock.__exit__(*args)
242

243
    def __repr__(self):
244
        return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
245 246

    def _release_save(self):
247
        self._lock.release()           # No state to save
248 249

    def _acquire_restore(self, x):
250
        self._lock.acquire()           # Ignore saved state
251 252

    def _is_owned(self):
253
        # Return True if lock is owned by current_thread.
254
        # This method is called only if _lock doesn't have _is_owned().
255 256
        if self._lock.acquire(0):
            self._lock.release()
257
            return False
258
        else:
259
            return True
260 261

    def wait(self, timeout=None):
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        """Wait until notified or until a timeout occurs.

        If the calling thread has not acquired the lock when this method is
        called, a RuntimeError is raised.

        This method releases the underlying lock, and then blocks until it is
        awakened by a notify() or notify_all() call for the same condition
        variable in another thread, or until the optional timeout occurs. Once
        awakened or timed out, it re-acquires the lock and returns.

        When the timeout argument is present and not None, it should be a
        floating point number specifying a timeout for the operation in seconds
        (or fractions thereof).

        When the underlying lock is an RLock, it is not released using its
        release() method, since this may not actually unlock the lock when it
        was acquired multiple times recursively. Instead, an internal interface
        of the RLock class is used, which really unlocks it even when it has
        been recursively acquired several times. Another internal interface is
        then used to restore the recursion level when the lock is reacquired.

        """
284
        if not self._is_owned():
Georg Brandl's avatar
Georg Brandl committed
285
            raise RuntimeError("cannot wait on un-acquired lock")
286 287
        waiter = _allocate_lock()
        waiter.acquire()
288
        self._waiters.append(waiter)
289
        saved_state = self._release_save()
290
        gotit = False
291 292 293
        try:    # restore state no matter what (e.g., KeyboardInterrupt)
            if timeout is None:
                waiter.acquire()
294
                gotit = True
295
            else:
296 297 298 299
                if timeout > 0:
                    gotit = waiter.acquire(True, timeout)
                else:
                    gotit = waiter.acquire(False)
300
            return gotit
301 302
        finally:
            self._acquire_restore(saved_state)
303 304 305 306 307
            if not gotit:
                try:
                    self._waiters.remove(waiter)
                except ValueError:
                    pass
308

Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
309
    def wait_for(self, predicate, timeout=None):
310 311 312 313 314 315 316
        """Wait until a condition evaluates to True.

        predicate should be a callable which result will be interpreted as a
        boolean value.  A timeout may be provided giving the maximum time to
        wait.

        """
Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
        endtime = None
        waittime = timeout
        result = predicate()
        while not result:
            if waittime is not None:
                if endtime is None:
                    endtime = _time() + waittime
                else:
                    waittime = endtime - _time()
                    if waittime <= 0:
                        break
            self.wait(waittime)
            result = predicate()
        return result

332
    def notify(self, n=1):
333 334 335 336 337 338 339 340 341
        """Wake up one or more threads waiting on this condition, if any.

        If the calling thread has not acquired the lock when this method is
        called, a RuntimeError is raised.

        This method wakes up at most n of the threads waiting for the condition
        variable; it is a no-op if no threads are waiting.

        """
342
        if not self._is_owned():
Georg Brandl's avatar
Georg Brandl committed
343
            raise RuntimeError("cannot notify on un-acquired lock")
344 345 346
        all_waiters = self._waiters
        waiters_to_notify = _deque(_islice(all_waiters, n))
        if not waiters_to_notify:
347
            return
348
        for waiter in waiters_to_notify:
349 350
            waiter.release()
            try:
351
                all_waiters.remove(waiter)
352 353 354
            except ValueError:
                pass

355
    def notify_all(self):
356 357 358 359 360 361
        """Wake up all threads waiting on this condition.

        If the calling thread has not acquired the lock when this method
        is called, a RuntimeError is raised.

        """
362
        self.notify(len(self._waiters))
363

364 365
    notifyAll = notify_all

366

367
class Semaphore:
368 369 370 371 372 373 374 375
    """This class implements semaphore objects.

    Semaphores manage a counter representing the number of release() calls minus
    the number of acquire() calls, plus an initial value. The acquire() method
    blocks if necessary until it can return without making the counter
    negative. If not given, value defaults to 1.

    """
376

377
    # After Tim Peters' semaphore class, but not quite the same (no maximum)
378

379
    def __init__(self, value=1):
380 381
        if value < 0:
            raise ValueError("semaphore initial value must be >= 0")
382 383
        self._cond = Condition(Lock())
        self._value = value
384

385
    def acquire(self, blocking=True, timeout=None):
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
        """Acquire a semaphore, decrementing the internal counter by one.

        When invoked without arguments: if the internal counter is larger than
        zero on entry, decrement it by one and return immediately. If it is zero
        on entry, block, waiting until some other thread has called release() to
        make it larger than zero. This is done with proper interlocking so that
        if multiple acquire() calls are blocked, release() will wake exactly one
        of them up. The implementation may pick one at random, so the order in
        which blocked threads are awakened should not be relied on. There is no
        return value in this case.

        When invoked with blocking set to true, do the same thing as when called
        without arguments, and return true.

        When invoked with blocking set to false, do not block. If a call without
        an argument would block, return false immediately; otherwise, do the
        same thing as when called without arguments, and return true.

        When invoked with a timeout other than None, it will block for at
        most timeout seconds.  If acquire does not complete successfully in
        that interval, return false.  Return true otherwise.

        """
409 410
        if not blocking and timeout is not None:
            raise ValueError("can't specify timeout for non-blocking acquire")
411
        rc = False
412
        endtime = None
413 414 415 416 417 418 419 420 421 422 423 424 425
        with self._cond:
            while self._value == 0:
                if not blocking:
                    break
                if timeout is not None:
                    if endtime is None:
                        endtime = _time() + timeout
                    else:
                        timeout = endtime - _time()
                        if timeout <= 0:
                            break
                self._cond.wait(timeout)
            else:
426
                self._value -= 1
427
                rc = True
428 429
        return rc

430 431
    __enter__ = acquire

432
    def release(self):
433 434 435 436 437 438
        """Release a semaphore, incrementing the internal counter by one.

        When the counter is zero on entry and another thread is waiting for it
        to become larger than zero again, wake up that thread.

        """
439
        with self._cond:
440
            self._value += 1
441
            self._cond.notify()
442

443 444 445
    def __exit__(self, t, v, tb):
        self.release()

446

447
class BoundedSemaphore(Semaphore):
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    """Implements a bounded semaphore.

    A bounded semaphore checks to make sure its current value doesn't exceed its
    initial value. If it does, ValueError is raised. In most situations
    semaphores are used to guard resources with limited capacity.

    If the semaphore is released too many times it's a sign of a bug. If not
    given, value defaults to 1.

    Like regular semaphores, bounded semaphores manage a counter representing
    the number of release() calls minus the number of acquire() calls, plus an
    initial value. The acquire() method blocks if necessary until it can return
    without making the counter negative. If not given, value defaults to 1.

    """

464 465
    def __init__(self, value=1):
        Semaphore.__init__(self, value)
466 467 468
        self._initial_value = value

    def release(self):
469 470 471 472 473 474 475 476 477
        """Release a semaphore, incrementing the internal counter by one.

        When the counter is zero on entry and another thread is waiting for it
        to become larger than zero again, wake up that thread.

        If the number of releases exceeds the number of acquires,
        raise a ValueError.

        """
478 479 480 481 482
        with self._cond:
            if self._value >= self._initial_value:
                raise ValueError("Semaphore released too many times")
            self._value += 1
            self._cond.notify()
483 484


485
class Event:
486 487 488 489 490 491 492
    """Class implementing event objects.

    Events manage a flag that can be set to true with the set() method and reset
    to false with the clear() method. The wait() method blocks until the flag is
    true.  The flag is initially false.

    """
493 494 495

    # After Tim Peters' event class (without is_posted())

496
    def __init__(self):
497 498
        self._cond = Condition(Lock())
        self._flag = False
499

500 501 502 503
    def _reset_internal_locks(self):
        # private!  called by Thread._reset_internal_locks by _after_fork()
        self._cond.__init__()

504
    def is_set(self):
505
        """Return true if and only if the internal flag is true."""
506
        return self._flag
507

508
    isSet = is_set
509

510
    def set(self):
511 512 513 514 515 516
        """Set the internal flag to true.

        All threads waiting for it to become true are awakened. Threads
        that call wait() once the flag is true will not block at all.

        """
517 518
        self._cond.acquire()
        try:
519
            self._flag = True
520
            self._cond.notify_all()
521 522
        finally:
            self._cond.release()
523 524

    def clear(self):
525 526 527 528 529 530
        """Reset the internal flag to false.

        Subsequently, threads calling wait() will block until set() is called to
        set the internal flag to true again.

        """
531 532
        self._cond.acquire()
        try:
533
            self._flag = False
534 535
        finally:
            self._cond.release()
536 537

    def wait(self, timeout=None):
538 539 540 541 542 543 544 545 546 547 548 549 550 551
        """Block until the internal flag is true.

        If the internal flag is true on entry, return immediately. Otherwise,
        block until another thread calls set() to set the flag to true, or until
        the optional timeout occurs.

        When the timeout argument is present and not None, it should be a
        floating point number specifying a timeout for the operation in seconds
        (or fractions thereof).

        This method returns the internal flag on exit, so it will always return
        True except if a timeout is given and the operation times out.

        """
552 553
        self._cond.acquire()
        try:
554 555 556 557
            signaled = self._flag
            if not signaled:
                signaled = self._cond.wait(timeout)
            return signaled
558 559
        finally:
            self._cond.release()
560

Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
561 562 563 564 565 566 567 568 569 570 571

# A barrier class.  Inspired in part by the pthread_barrier_* api and
# the CyclicBarrier class from Java.  See
# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
#        CyclicBarrier.html
# for information.
# We maintain two main states, 'filling' and 'draining' enabling the barrier
# to be cyclic.  Threads are not allowed into it until it has fully drained
# since the previous cycle.  In addition, a 'resetting' state exists which is
# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melotti's avatar
Ezio Melotti committed
572
# and a 'broken' state in which all threads get the exception.
573
class Barrier:
574 575 576 577 578 579
    """Implements a Barrier.

    Useful for synchronizing a fixed number of threads at known synchronization
    points.  Threads block on 'wait()' and are simultaneously once they have all
    made that call.

Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
580
    """
581

582
    def __init__(self, parties, action=None, timeout=None):
583 584 585 586 587 588 589
        """Create a barrier, initialised to 'parties' threads.

        'action' is a callable which, when supplied, will be called by one of
        the threads after they have all entered the barrier and just prior to
        releasing them all. If a 'timeout' is provided, it is uses as the
        default for all subsequent 'wait()' calls.

Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
590 591 592 593 594 595 596 597 598
        """
        self._cond = Condition(Lock())
        self._action = action
        self._timeout = timeout
        self._parties = parties
        self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
        self._count = 0

    def wait(self, timeout=None):
599 600 601 602 603
        """Wait for the barrier.

        When the specified number of threads have started waiting, they are all
        simultaneously awoken. If an 'action' was provided for the barrier, one
        of the threads will have executed that callback prior to returning.
Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
604
        Returns an individual index number from 0 to 'parties-1'.
605

Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
        """
        if timeout is None:
            timeout = self._timeout
        with self._cond:
            self._enter() # Block while the barrier drains.
            index = self._count
            self._count += 1
            try:
                if index + 1 == self._parties:
                    # We release the barrier
                    self._release()
                else:
                    # We wait until someone releases us
                    self._wait(timeout)
                return index
            finally:
                self._count -= 1
                # Wake up any threads waiting for barrier to drain.
                self._exit()

    # Block until the barrier is ready for us, or raise an exception
    # if it is broken.
    def _enter(self):
        while self._state in (-1, 1):
            # It is draining or resetting, wait until done
            self._cond.wait()
        #see if the barrier is in a broken state
        if self._state < 0:
            raise BrokenBarrierError
        assert self._state == 0

    # Optionally run the 'action' and release the threads waiting
    # in the barrier.
    def _release(self):
        try:
            if self._action:
                self._action()
            # enter draining state
            self._state = 1
            self._cond.notify_all()
        except:
            #an exception during the _action handler.  Break and reraise
            self._break()
            raise

    # Wait in the barrier until we are relased.  Raise an exception
    # if the barrier is reset or broken.
    def _wait(self, timeout):
Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
654 655 656 657 658 659
        if not self._cond.wait_for(lambda : self._state != 0, timeout):
            #timed out.  Break the barrier
            self._break()
            raise BrokenBarrierError
        if self._state < 0:
            raise BrokenBarrierError
Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
660 661 662 663 664 665 666 667 668 669 670 671
        assert self._state == 1

    # If we are the last thread to exit the barrier, signal any threads
    # waiting for the barrier to drain.
    def _exit(self):
        if self._count == 0:
            if self._state in (-1, 1):
                #resetting or draining
                self._state = 0
                self._cond.notify_all()

    def reset(self):
672 673
        """Reset the barrier to the initial state.

Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
674 675
        Any threads currently waiting will get the BrokenBarrier exception
        raised.
676

Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
        """
        with self._cond:
            if self._count > 0:
                if self._state == 0:
                    #reset the barrier, waking up threads
                    self._state = -1
                elif self._state == -2:
                    #was broken, set it to reset state
                    #which clears when the last thread exits
                    self._state = -1
            else:
                self._state = 0
            self._cond.notify_all()

    def abort(self):
692 693 694 695 696
        """Place the barrier into a 'broken' state.

        Useful in case of error.  Any currently waiting threads and threads
        attempting to 'wait()' will have BrokenBarrierError raised.

Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
697 698 699 700 701 702 703 704 705 706 707 708
        """
        with self._cond:
            self._break()

    def _break(self):
        # An internal error was detected.  The barrier is set to
        # a broken state all parties awakened.
        self._state = -2
        self._cond.notify_all()

    @property
    def parties(self):
709
        """Return the number of threads required to trip the barrier."""
Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
710 711 712 713
        return self._parties

    @property
    def n_waiting(self):
714
        """Return the number of threads currently waiting at the barrier."""
Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
715 716 717 718 719 720 721 722
        # We don't need synchronization here since this is an ephemeral result
        # anyway.  It returns the correct value in the steady state.
        if self._state == 0:
            return self._count
        return 0

    @property
    def broken(self):
723
        """Return True if the barrier is in a broken state."""
Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
724 725
        return self._state == -2

726 727 728
# exception raised by the Barrier class
class BrokenBarrierError(RuntimeError):
    pass
Kristján Valur Jónsson's avatar
Kristján Valur Jónsson committed
729 730


731
# Helper to generate new thread names
732 733
_counter = _count().__next__
_counter() # Consume 0 so first non-main thread has id 1.
734
def _newname(template="Thread-%d"):
735
    return template % _counter()
736 737 738

# Active thread administration
_active_limbo_lock = _allocate_lock()
739
_active = {}    # maps thread id to Thread object
740
_limbo = {}
741
_dangling = WeakSet()
742 743 744

# Main class for threads

745
class Thread:
746 747 748 749 750 751 752
    """A class that represents a thread of control.

    This class can be safely subclassed in a limited fashion. There are two ways
    to specify the activity: by passing a callable object to the constructor, or
    by overriding the run() method in a subclass.

    """
753

754
    _initialized = False
755 756 757 758
    # Need to store a reference to sys.exc_info for printing
    # out exceptions when a thread tries to use a global var. during interp.
    # shutdown and thus raises an exception about trying to perform some
    # operation on/with a NoneType
759
    _exc_info = _sys.exc_info
760 761 762
    # Keep sys.exc_clear too to clear the exception just before
    # allowing .join() to return.
    #XXX __exc_clear = _sys.exc_clear
763 764

    def __init__(self, group=None, target=None, name=None,
765
                 args=(), kwargs=None, *, daemon=None):
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
        """This constructor should always be called with keyword arguments. Arguments are:

        *group* should be None; reserved for future extension when a ThreadGroup
        class is implemented.

        *target* is the callable object to be invoked by the run()
        method. Defaults to None, meaning nothing is called.

        *name* is the thread name. By default, a unique name is constructed of
        the form "Thread-N" where N is a small decimal number.

        *args* is the argument tuple for the target invocation. Defaults to ().

        *kwargs* is a dictionary of keyword arguments for the target
        invocation. Defaults to {}.

        If a subclass overrides the constructor, it must make sure to invoke
        the base class constructor (Thread.__init__()) before doing anything
        else to the thread.

        """
787
        assert group is None, "group argument must be None for now"
788 789
        if kwargs is None:
            kwargs = {}
790 791 792 793
        self._target = target
        self._name = str(name or _newname())
        self._args = args
        self._kwargs = kwargs
794 795 796 797
        if daemon is not None:
            self._daemonic = daemon
        else:
            self._daemonic = current_thread().daemon
Georg Brandl's avatar
Georg Brandl committed
798
        self._ident = None
799
        self._tstate_lock = None
Christian Heimes's avatar
Christian Heimes committed
800
        self._started = Event()
801
        self._is_stopped = False
802
        self._initialized = True
803 804
        # sys.stderr is not stored in the class like
        # sys.exc_info since it can be changed between instances
805
        self._stderr = _sys.stderr
806
        # For debugging and _after_fork()
807
        _dangling.add(self)
808

809
    def _reset_internal_locks(self, is_alive):
810 811 812
        # private!  Called by _after_fork() to reset our internal locks as
        # they may be in an invalid state leading to a deadlock or crash.
        self._started._reset_internal_locks()
813 814 815 816 817
        if is_alive:
            self._set_tstate_lock()
        else:
            # The thread isn't alive after fork: it doesn't have a tstate
            # anymore.
818
            self._is_stopped = True
819
            self._tstate_lock = None
820

821
    def __repr__(self):
822
        assert self._initialized, "Thread.__init__() was not called"
823
        status = "initial"
824
        if self._started.is_set():
825
            status = "started"
826
        self.is_alive() # easy way to get ._is_stopped set when appropriate
827
        if self._is_stopped:
828
            status = "stopped"
829
        if self._daemonic:
Georg Brandl's avatar
Georg Brandl committed
830 831 832
            status += " daemon"
        if self._ident is not None:
            status += " %s" % self._ident
833
        return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
834 835

    def start(self):
836 837 838 839 840 841 842 843 844
        """Start the thread's activity.

        It must be called at most once per thread object. It arranges for the
        object's run() method to be invoked in a separate thread of control.

        This method will raise a RuntimeError if called more than once on the
        same thread object.

        """
845
        if not self._initialized:
846
            raise RuntimeError("thread.__init__() not called")
Christian Heimes's avatar
Christian Heimes committed
847

848
        if self._started.is_set():
849
            raise RuntimeError("threads can only be started once")
Benjamin Peterson's avatar
Benjamin Peterson committed
850 851
        with _active_limbo_lock:
            _limbo[self] = self
852 853 854 855 856 857
        try:
            _start_new_thread(self._bootstrap, ())
        except Exception:
            with _active_limbo_lock:
                del _limbo[self]
            raise
Christian Heimes's avatar
Christian Heimes committed
858
        self._started.wait()
859 860

    def run(self):
861 862 863 864 865 866 867 868
        """Method representing the thread's activity.

        You may override this method in a subclass. The standard run() method
        invokes the callable object passed to the object's constructor as the
        target argument, if any, with sequential and keyword arguments taken
        from the args and kwargs arguments, respectively.

        """
869 870 871 872 873 874 875
        try:
            if self._target:
                self._target(*self._args, **self._kwargs)
        finally:
            # Avoid a refcycle if the thread is running a function with
            # an argument that has a member that points to the thread.
            del self._target, self._args, self._kwargs
876

877
    def _bootstrap(self):
878 879 880 881 882
        # Wrapper around the real bootstrap code that ignores
        # exceptions during interpreter cleanup.  Those typically
        # happen when a daemon thread wakes up at an unfortunate
        # moment, finds the world around it destroyed, and raises some
        # random exception *** while trying to report the exception in
Christian Heimes's avatar
Christian Heimes committed
883
        # _bootstrap_inner() below ***.  Those random exceptions
884 885 886
        # don't help anybody, and they confuse users, so we suppress
        # them.  We suppress them only when it appears that the world
        # indeed has already been destroyed, so that exceptions in
Christian Heimes's avatar
Christian Heimes committed
887
        # _bootstrap_inner() during normal business hours are properly
888 889 890
        # reported.  Also, we only suppress them for daemonic threads;
        # if a non-daemonic encounters this, something else is wrong.
        try:
891
            self._bootstrap_inner()
892
        except:
893
            if self._daemonic and _sys is None:
894 895 896
                return
            raise

Benjamin Peterson's avatar
Benjamin Peterson committed
897
    def _set_ident(self):
898
        self._ident = get_ident()
Benjamin Peterson's avatar
Benjamin Peterson committed
899

900 901 902 903 904 905 906 907
    def _set_tstate_lock(self):
        """
        Set a lock object which will be released by the interpreter when
        the underlying thread state (see pystate.h) gets deleted.
        """
        self._tstate_lock = _set_sentinel()
        self._tstate_lock.acquire()

908
    def _bootstrap_inner(self):
909
        try:
Benjamin Peterson's avatar
Benjamin Peterson committed
910
            self._set_ident()
911
            self._set_tstate_lock()
Christian Heimes's avatar
Christian Heimes committed
912
            self._started.set()
Benjamin Peterson's avatar
Benjamin Peterson committed
913 914 915
            with _active_limbo_lock:
                _active[self._ident] = self
                del _limbo[self]
916 917 918 919 920

            if _trace_hook:
                _sys.settrace(_trace_hook)
            if _profile_hook:
                _sys.setprofile(_profile_hook)
Tim Peters's avatar
Tim Peters committed
921

922 923 924
            try:
                self.run()
            except SystemExit:
925
                pass
926
            except:
927
                # If sys.stderr is no more (most likely from interpreter
928
                # shutdown) use self._stderr.  Otherwise still use sys (as in
929 930
                # _sys) in case sys.stderr was redefined since the creation of
                # self.
931 932 933 934
                if _sys and _sys.stderr is not None:
                    print("Exception in thread %s:\n%s" %
                          (self.name, _format_exc()), file=self._stderr)
                elif self._stderr is not None:
935 936 937
                    # Do the best job possible w/o a huge amt. of code to
                    # approximate a traceback (code ideas from
                    # Lib/traceback.py)
938
                    exc_type, exc_value, exc_tb = self._exc_info()
939
                    try:
940
                        print((
941
                            "Exception in thread " + self.name +
942
                            " (most likely raised during interpreter shutdown):"), file=self._stderr)
943
                        print((
944
                            "Traceback (most recent call last):"), file=self._stderr)
945
                        while exc_tb:
946
                            print((
947 948 949
                                '  File "%s", line %s, in %s' %
                                (exc_tb.tb_frame.f_code.co_filename,
                                    exc_tb.tb_lineno,
950
                                    exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
951
                            exc_tb = exc_tb.tb_next
952
                        print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
953 954 955 956
                    # Make sure that exc_tb gets deleted since it is a memory
                    # hog; deleting everything else is just for thoroughness
                    finally:
                        del exc_type, exc_value, exc_tb
957 958 959 960 961
            finally:
                # Prevent a race in
                # test_threading.test_no_refcycle_through_target when
                # the exception keeps the target alive past when we
                # assert that it's dead.
962
                #XXX self._exc_clear()
963
                pass
964
        finally:
965 966
            with _active_limbo_lock:
                try:
Georg Brandl's avatar
Georg Brandl committed
967
                    # We don't call self._delete() because it also
968
                    # grabs _active_limbo_lock.
969
                    del _active[get_ident()]
970 971
                except:
                    pass
972

973
    def _stop(self):
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
        # After calling ._stop(), .is_alive() returns False and .join() returns
        # immediately.  ._tstate_lock must be released before calling ._stop().
        #
        # Normal case:  C code at the end of the thread's life
        # (release_sentinel in _threadmodule.c) releases ._tstate_lock, and
        # that's detected by our ._wait_for_tstate_lock(), called by .join()
        # and .is_alive().  Any number of threads _may_ call ._stop()
        # simultaneously (for example, if multiple threads are blocked in
        # .join() calls), and they're not serialized.  That's harmless -
        # they'll just make redundant rebindings of ._is_stopped and
        # ._tstate_lock.  Obscure:  we rebind ._tstate_lock last so that the
        # "assert self._is_stopped" in ._wait_for_tstate_lock() always works
        # (the assert is executed only if ._tstate_lock is None).
        #
        # Special case:  _main_thread releases ._tstate_lock via this
        # module's _shutdown() function.
        lock = self._tstate_lock
        if lock is not None:
            assert not lock.locked()
993 994
        self._is_stopped = True
        self._tstate_lock = None
995

996
    def _delete(self):
997 998
        "Remove current thread from the dict of currently running threads."

999
        # Notes about running with _dummy_thread:
1000
        #
1001
        # Must take care to not raise an exception if _dummy_thread is being
1002
        # used (and thus this module is being used as an instance of
1003 1004
        # dummy_threading).  _dummy_thread.get_ident() always returns -1 since
        # there is only one thread if _dummy_thread is being used.  Thus
1005 1006 1007 1008 1009
        # len(_active) is always <= 1 here, and any Thread instance created
        # overwrites the (if any) thread currently registered in _active.
        #
        # An instance of _MainThread is always created by 'threading'.  This
        # gets overwritten the instant an instance of Thread is created; both
1010
        # threads return -1 from _dummy_thread.get_ident() and thus have the
1011 1012 1013 1014 1015 1016 1017 1018 1019
        # same key in the dict.  So when the _MainThread instance created by
        # 'threading' tries to clean itself up when atexit calls this method
        # it gets a KeyError if another Thread instance was created.
        #
        # This all means that KeyError from trying to delete something from
        # _active if dummy_threading is being used is a red herring.  But
        # since it isn't if dummy_threading is *not* being used then don't
        # hide the exception.

1020
        try:
Neal Norwitz's avatar
Neal Norwitz committed
1021
            with _active_limbo_lock:
1022
                del _active[get_ident()]
Neal Norwitz's avatar
Neal Norwitz committed
1023 1024 1025
                # There must not be any python code between the previous line
                # and after the lock is released.  Otherwise a tracing function
                # could try to acquire the lock again in the same thread, (in
1026
                # current_thread()), and would block.
Neal Norwitz's avatar
Neal Norwitz committed
1027 1028 1029
        except KeyError:
            if 'dummy_threading' not in _sys.modules:
                raise
1030 1031

    def join(self, timeout=None):
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
        """Wait until the thread terminates.

        This blocks the calling thread until the thread whose join() method is
        called terminates -- either normally or through an unhandled exception
        or until the optional timeout occurs.

        When the timeout argument is present and not None, it should be a
        floating point number specifying a timeout for the operation in seconds
        (or fractions thereof). As join() always returns None, you must call
        isAlive() after join() to decide whether a timeout happened -- if the
        thread is still alive, the join() call timed out.

        When the timeout argument is not present or None, the operation will
        block until the thread terminates.

        A thread can be join()ed many times.

        join() raises a RuntimeError if an attempt is made to join the current
        thread as that would cause a deadlock. It is also an error to join() a
        thread before it has been started and attempts to do so raises the same
        exception.

        """
1055
        if not self._initialized:
1056
            raise RuntimeError("Thread.__init__() not called")
1057
        if not self._started.is_set():
1058
            raise RuntimeError("cannot join thread before it is started")
1059
        if self is current_thread():
1060
            raise RuntimeError("cannot join current thread")
1061

1062 1063
        if timeout is None:
            self._wait_for_tstate_lock()
1064 1065
        else:
            # the behavior of a negative timeout isn't documented, but
1066
            # historically .join(timeout=x) for x<0 has acted as if timeout=0
1067
            self._wait_for_tstate_lock(timeout=max(timeout, 0))
1068

1069
    def _wait_for_tstate_lock(self, block=True, timeout=-1):
1070
        # Issue #18808: wait for the thread state to be gone.
1071 1072 1073 1074 1075
        # At the end of the thread's life, after all knowledge of the thread
        # is removed from C data structures, C code releases our _tstate_lock.
        # This method passes its arguments to _tstate_lock.aquire().
        # If the lock is acquired, the C code is done, and self._stop() is
        # called.  That sets ._is_stopped to True, and ._tstate_lock to None.
1076
        lock = self._tstate_lock
1077 1078 1079
        if lock is None:  # already determined that the C code is done
            assert self._is_stopped
        elif lock.acquire(block, timeout):
1080
            lock.release()
1081
            self._stop()
1082

1083 1084
    @property
    def name(self):
1085 1086 1087 1088 1089 1090
        """A string used for identification purposes only.

        It has no semantics. Multiple threads may be given the same name. The
        initial name is set by the constructor.

        """
1091 1092
        assert self._initialized, "Thread.__init__() not called"
        return self._name
1093

1094 1095
    @name.setter
    def name(self, name):
1096 1097
        assert self._initialized, "Thread.__init__() not called"
        self._name = str(name)
1098

1099 1100
    @property
    def ident(self):
1101 1102 1103 1104 1105 1106 1107
        """Thread identifier of this thread or None if it has not been started.

        This is a nonzero integer. See the thread.get_ident() function. Thread
        identifiers may be recycled when a thread exits and another thread is
        created. The identifier is available even after the thread has exited.

        """
Georg Brandl's avatar
Georg Brandl committed
1108 1109 1110
        assert self._initialized, "Thread.__init__() not called"
        return self._ident

1111
    def is_alive(self):
1112 1113 1114 1115 1116 1117 1118
        """Return whether the thread is alive.

        This method returns True just before the run() method starts until just
        after the run() method terminates. The module function enumerate()
        returns a list of all alive threads.

        """
1119
        assert self._initialized, "Thread.__init__() not called"
1120
        if self._is_stopped or not self._started.is_set():
1121 1122
            return False
        self._wait_for_tstate_lock(False)
1123
        return not self._is_stopped
Tim Peters's avatar
Tim Peters committed
1124

1125
    isAlive = is_alive
1126

1127 1128
    @property
    def daemon(self):
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
        """A boolean value indicating whether this thread is a daemon thread.

        This must be set before start() is called, otherwise RuntimeError is
        raised. Its initial value is inherited from the creating thread; the
        main thread is not a daemon thread and therefore all threads created in
        the main thread default to daemon = False.

        The entire Python program exits when no alive non-daemon threads are
        left.

        """
1140 1141
        assert self._initialized, "Thread.__init__() not called"
        return self._daemonic
1142

1143 1144
    @daemon.setter
    def daemon(self, daemonic):
1145
        if not self._initialized:
1146
            raise RuntimeError("Thread.__init__() not called")
1147
        if self._started.is_set():
Antoine Pitrou's avatar
Antoine Pitrou committed
1148
            raise RuntimeError("cannot set daemon status of active thread")
1149
        self._daemonic = daemonic
1150

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
    def isDaemon(self):
        return self.daemon

    def setDaemon(self, daemonic):
        self.daemon = daemonic

    def getName(self):
        return self.name

    def setName(self, name):
        self.name = name

1163 1164
# The timer class was contributed by Itamar Shtull-Trauring

1165
class Timer(Thread):
1166
    """Call a function after a specified number of seconds:
Tim Peters's avatar
Tim Peters committed
1167

1168 1169 1170 1171
            t = Timer(30.0, f, args=None, kwargs=None)
            t.start()
            t.cancel()     # stop the timer's action if it's still waiting

1172
    """
Tim Peters's avatar
Tim Peters committed
1173

1174
    def __init__(self, interval, function, args=None, kwargs=None):
1175 1176 1177
        Thread.__init__(self)
        self.interval = interval
        self.function = function
1178 1179
        self.args = args if args is not None else []
        self.kwargs = kwargs if kwargs is not None else {}
1180
        self.finished = Event()
Tim Peters's avatar
Tim Peters committed
1181

1182
    def cancel(self):
1183
        """Stop the timer if it hasn't finished yet."""
1184
        self.finished.set()
Tim Peters's avatar
Tim Peters committed
1185

1186 1187
    def run(self):
        self.finished.wait(self.interval)
1188
        if not self.finished.is_set():
1189 1190
            self.function(*self.args, **self.kwargs)
        self.finished.set()
1191 1192 1193 1194 1195 1196 1197

# Special thread class to represent the main thread
# This is garbage collected through an exit handler

class _MainThread(Thread):

    def __init__(self):
1198
        Thread.__init__(self, name="MainThread", daemon=False)
1199
        self._set_tstate_lock()
Christian Heimes's avatar
Christian Heimes committed
1200
        self._started.set()
Benjamin Peterson's avatar
Benjamin Peterson committed
1201 1202 1203
        self._set_ident()
        with _active_limbo_lock:
            _active[self._ident] = self
1204 1205 1206


# Dummy thread class to represent threads not started here.
1207
# These aren't garbage collected when they die, nor can they be waited for.
1208
# If they invoke anything in threading.py that calls current_thread(), they
1209
# leave an entry in the _active dict forever after.
1210
# Their purpose is to return *something* from current_thread().
1211 1212 1213 1214
# They are marked as daemon threads so we won't wait for them
# when we exit (conform previous semantics).

class _DummyThread(Thread):
Tim Peters's avatar
Tim Peters committed
1215

1216
    def __init__(self):
1217
        Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
1218

Christian Heimes's avatar
Christian Heimes committed
1219
        self._started.set()
Benjamin Peterson's avatar
Benjamin Peterson committed
1220 1221 1222
        self._set_ident()
        with _active_limbo_lock:
            _active[self._ident] = self
1223

1224 1225 1226
    def _stop(self):
        pass

1227
    def join(self, timeout=None):
1228
        assert False, "cannot join a dummy thread"
1229 1230 1231 1232


# Global API functions

1233
def current_thread():
1234 1235 1236 1237 1238 1239
    """Return the current Thread object, corresponding to the caller's thread of control.

    If the caller's thread of control was not created through the threading
    module, a dummy thread object with limited functionality is returned.

    """
1240
    try:
1241
        return _active[get_ident()]
1242 1243 1244
    except KeyError:
        return _DummyThread()

1245
currentThread = current_thread
1246

1247
def active_count():
1248 1249 1250 1251 1252 1253
    """Return the number of Thread objects currently alive.

    The returned count is equal to the length of the list returned by
    enumerate().

    """
Benjamin Peterson's avatar
Benjamin Peterson committed
1254 1255
    with _active_limbo_lock:
        return len(_active) + len(_limbo)
1256

1257
activeCount = active_count
1258

1259 1260 1261 1262
def _enumerate():
    # Same as enumerate(), but without the lock. Internal use only.
    return list(_active.values()) + list(_limbo.values())

1263
def enumerate():
1264 1265 1266 1267 1268 1269 1270
    """Return a list of all Thread objects currently alive.

    The list includes daemonic threads, dummy thread objects created by
    current_thread(), and the main thread. It excludes terminated threads and
    threads that have not yet been started.

    """
Benjamin Peterson's avatar
Benjamin Peterson committed
1271 1272
    with _active_limbo_lock:
        return list(_active.values()) + list(_limbo.values())
1273

1274
from _thread import stack_size
1275

1276 1277 1278
# Create the main thread object,
# and make it available for the interpreter
# (Py_Main) as threading._shutdown.
1279

1280 1281 1282
_main_thread = _MainThread()

def _shutdown():
1283 1284 1285 1286 1287
    # Obscure:  other threads may be waiting to join _main_thread.  That's
    # dubious, but some code does it.  We can't wait for C code to release
    # the main thread's tstate_lock - that won't happen until the interpreter
    # is nearly dead.  So we release it here.  Note that just calling _stop()
    # isn't enough:  other threads may already be waiting on _tstate_lock.
1288 1289 1290 1291 1292 1293
    tlock = _main_thread._tstate_lock
    # The main thread isn't finished yet, so its thread state lock can't have
    # been released.
    assert tlock is not None
    assert tlock.locked()
    tlock.release()
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
    _main_thread._stop()
    t = _pickSomeNonDaemonThread()
    while t:
        t.join()
        t = _pickSomeNonDaemonThread()
    _main_thread._delete()

def _pickSomeNonDaemonThread():
    for t in enumerate():
        if not t.daemon and t.is_alive():
            return t
    return None

def main_thread():
1308 1309 1310 1311 1312
    """Return the main thread object.

    In normal conditions, the main thread is the thread from which the
    Python interpreter was started.
    """
1313
    return _main_thread
1314

1315 1316 1317 1318
# get thread-local implementation, either from the thread
# module, or from the python fallback

try:
1319
    from _thread import _local as local
1320
except ImportError:
1321 1322
    from _threading_local import local

1323

Jesse Noller's avatar
Jesse Noller committed
1324 1325 1326 1327 1328 1329 1330
def _after_fork():
    # This function is called by Python/ceval.c:PyEval_ReInitThreads which
    # is called from PyOS_AfterFork.  Here we cleanup threading module state
    # that should not exist after a fork.

    # Reset _active_limbo_lock, in case we forked while the lock was held
    # by another (non-forked) thread.  http://bugs.python.org/issue874900
1331
    global _active_limbo_lock, _main_thread
Jesse Noller's avatar
Jesse Noller committed
1332 1333 1334 1335 1336
    _active_limbo_lock = _allocate_lock()

    # fork() only copied the current thread; clear references to others.
    new_active = {}
    current = current_thread()
1337
    _main_thread = current
Jesse Noller's avatar
Jesse Noller committed
1338
    with _active_limbo_lock:
1339 1340 1341 1342 1343
        # Dangling thread instances must still have their locks reset,
        # because someone may join() them.
        threads = set(_enumerate())
        threads.update(_dangling)
        for thread in threads:
1344 1345
            # Any lock/condition variable may be currently locked or in an
            # invalid state, so we reinitialize them.
Jesse Noller's avatar
Jesse Noller committed
1346
            if thread is current:
1347 1348
                # There is only one active thread. We reset the ident to
                # its new value since it can have changed.
1349
                thread._reset_internal_locks(True)
1350
                ident = get_ident()
1351
                thread._ident = ident
Jesse Noller's avatar
Jesse Noller committed
1352 1353 1354
                new_active[ident] = thread
            else:
                # All the others are already stopped.
1355
                thread._reset_internal_locks(False)
1356
                thread._stop()
Jesse Noller's avatar
Jesse Noller committed
1357 1358 1359 1360 1361

        _limbo.clear()
        _active.clear()
        _active.update(new_active)
        assert len(_active) == 1