@@ -391,12 +391,12 @@ Thread State and the Global Interpreter Lock
391391 single: lock, interpreter
392392
393393The Python interpreter is not fully thread safe. In order to support
394- multi-threaded Python programs, there's a global lock that must be held by the
395- current thread before it can safely access Python objects. Without the lock,
396- even the simplest operations could cause problems in a multi-threaded program:
397- for example, when two threads simultaneously increment the reference count of
398- the same object, the reference count could end up being incremented only once
399- instead of twice.
394+ multi-threaded Python programs, there's a global lock, called the :dfn: ` global
395+ interpreter lock ` or :dfn: ` GIL `, that must be held by the current thread before
396+ it can safely access Python objects. Without the lock, even the simplest
397+ operations could cause problems in a multi-threaded program: for example, when
398+ two threads simultaneously increment the reference count of the same object, the
399+ reference count could end up being incremented only once instead of twice.
400400
401401.. index :: single: setcheckinterval() (in module sys)
402402
@@ -425,9 +425,9 @@ This is easy enough in most cases. Most code manipulating the global
425425interpreter lock has the following simple structure::
426426
427427 Save the thread state in a local variable.
428- Release the interpreter lock.
428+ Release the global interpreter lock.
429429 ...Do some blocking I/O operation...
430- Reacquire the interpreter lock.
430+ Reacquire the global interpreter lock.
431431 Restore the thread state from the local variable.
432432
433433This is so common that a pair of macros exists to simplify it::
@@ -444,7 +444,7 @@ The :cmacro:`Py_BEGIN_ALLOW_THREADS` macro opens a new block and declares a
444444hidden local variable; the :cmacro: `Py_END_ALLOW_THREADS ` macro closes the
445445block. Another advantage of using these two macros is that when Python is
446446compiled without thread support, they are defined empty, thus saving the thread
447- state and lock manipulations.
447+ state and GIL manipulations.
448448
449449When thread support is enabled, the block above expands to the following code::
450450
@@ -476,7 +476,7 @@ There are some subtle differences; in particular, :cfunc:`PyEval_RestoreThread`
476476saves and restores the value of the global variable :cdata: `errno `, since the
477477lock manipulation does not guarantee that :cdata: `errno ` is left alone. Also,
478478when thread support is disabled, :cfunc: `PyEval_SaveThread ` and
479- :cfunc: `PyEval_RestoreThread ` don't manipulate the lock ; in this case,
479+ :cfunc: `PyEval_RestoreThread ` don't manipulate the GIL ; in this case,
480480:cfunc: `PyEval_ReleaseLock ` and :cfunc: `PyEval_AcquireLock ` are not available.
481481This is done so that dynamically loaded extensions compiled with thread support
482482enabled can be loaded by an interpreter that was compiled with disabled thread
@@ -559,16 +559,16 @@ supports the creation of additional interpreters (using
559559
560560 .. index :: module: _thread
561561
562- When only the main thread exists, no lock operations are needed. This is a
562+ When only the main thread exists, no GIL operations are needed. This is a
563563 common situation (most Python programs do not use threads), and the lock
564- operations slow the interpreter down a bit. Therefore, the lock is not created
565- initially. This situation is equivalent to having acquired the lock: when
566- there is only a single thread, all object accesses are safe. Therefore, when
567- this function initializes the lock, it also acquires it. Before the Python
568- :mod: `_thread ` module creates a new thread, knowing that either it has the lock
569- or the lock hasn't been created yet, it calls :cfunc: ` PyEval_InitThreads `. When
570- this call returns, it is guaranteed that the lock has been created and that the
571- calling thread has acquired it.
564+ operations slow the interpreter down a bit. Therefore, the lock is not
565+ created initially. This situation is equivalent to having acquired the lock:
566+ when there is only a single thread, all object accesses are safe. Therefore,
567+ when this function initializes the global interpreter lock, it also acquires
568+ it. Before the Python :mod: `_thread ` module creates a new thread, knowing
569+ that either it has the lock or the lock hasn't been created yet, it calls
570+ :cfunc: ` PyEval_InitThreads `. When this call returns, it is guaranteed that
571+ the lock has been created and that the calling thread has acquired it.
572572
573573 It is **not ** safe to call this function when it is unknown which thread (if
574574 any) currently has the global interpreter lock.
@@ -579,7 +579,7 @@ supports the creation of additional interpreters (using
579579.. cfunction :: int PyEval_ThreadsInitialized()
580580
581581 Returns a non-zero value if :cfunc: `PyEval_InitThreads ` has been called. This
582- function can be called without holding the lock , and therefore can be used to
582+ function can be called without holding the GIL , and therefore can be used to
583583 avoid calls to the locking API when running single-threaded. This function is
584584 not available when thread support is disabled at compile time.
585585
@@ -617,20 +617,20 @@ supports the creation of additional interpreters (using
617617
618618.. cfunction :: PyThreadState* PyEval_SaveThread()
619619
620- Release the interpreter lock (if it has been created and thread support is
621- enabled) and reset the thread state to *NULL *, returning the previous thread
622- state (which is not *NULL *). If the lock has been created, the current thread
623- must have acquired it. (This function is available even when thread support is
624- disabled at compile time.)
620+ Release the global interpreter lock (if it has been created and thread
621+ support is enabled) and reset the thread state to *NULL *, returning the
622+ previous thread state (which is not *NULL *). If the lock has been created,
623+ the current thread must have acquired it. (This function is available even
624+ when thread support is disabled at compile time.)
625625
626626
627627.. cfunction :: void PyEval_RestoreThread(PyThreadState *tstate)
628628
629- Acquire the interpreter lock (if it has been created and thread support is
630- enabled) and set the thread state to *tstate *, which must not be * NULL *. If the
631- lock has been created, the current thread must not have acquired it, otherwise
632- deadlock ensues. (This function is available even when thread support is
633- disabled at compile time.)
629+ Acquire the global interpreter lock (if it has been created and thread
630+ support is enabled) and set the thread state to *tstate *, which must not be
631+ * NULL *. If the lock has been created, the current thread must not have
632+ acquired it, otherwise deadlock ensues. (This function is available even
633+ when thread support is disabled at compile time.)
634634
635635
636636.. cfunction :: void PyEval_ReInitThreads()
@@ -674,60 +674,61 @@ example usage in the Python source distribution.
674674 declaration. It is a no-op when thread support is disabled at compile time.
675675
676676All of the following functions are only available when thread support is enabled
677- at compile time, and must be called only when the interpreter lock has been
678- created.
677+ at compile time, and must be called only when the global interpreter lock has
678+ been created.
679679
680680
681681.. cfunction :: PyInterpreterState* PyInterpreterState_New()
682682
683- Create a new interpreter state object. The interpreter lock need not be held,
684- but may be held if it is necessary to serialize calls to this function.
683+ Create a new interpreter state object. The global interpreter lock need not
684+ be held, but may be held if it is necessary to serialize calls to this
685+ function.
685686
686687
687688.. cfunction :: void PyInterpreterState_Clear(PyInterpreterState *interp)
688689
689- Reset all information in an interpreter state object. The interpreter lock must
690- be held.
690+ Reset all information in an interpreter state object. The global interpreter
691+ lock must be held.
691692
692693
693694.. cfunction :: void PyInterpreterState_Delete(PyInterpreterState *interp)
694695
695- Destroy an interpreter state object. The interpreter lock need not be held.
696- The interpreter state must have been reset with a previous call to
696+ Destroy an interpreter state object. The global interpreter lock need not be
697+ held. The interpreter state must have been reset with a previous call to
697698 :cfunc: `PyInterpreterState_Clear `.
698699
699700
700701.. cfunction :: PyThreadState* PyThreadState_New(PyInterpreterState *interp)
701702
702- Create a new thread state object belonging to the given interpreter object. The
703- interpreter lock need not be held, but may be held if it is necessary to
704- serialize calls to this function.
703+ Create a new thread state object belonging to the given interpreter object.
704+ The global interpreter lock need not be held, but may be held if it is
705+ necessary to serialize calls to this function.
705706
706707
707708.. cfunction :: void PyThreadState_Clear(PyThreadState *tstate)
708709
709- Reset all information in a thread state object. The interpreter lock must be
710- held.
710+ Reset all information in a thread state object. The global interpreter lock
711+ must be held.
711712
712713
713714.. cfunction :: void PyThreadState_Delete(PyThreadState *tstate)
714715
715- Destroy a thread state object. The interpreter lock need not be held. The
716- thread state must have been reset with a previous call to
716+ Destroy a thread state object. The global interpreter lock need not be held.
717+ The thread state must have been reset with a previous call to
717718 :cfunc: `PyThreadState_Clear `.
718719
719720
720721.. cfunction :: PyThreadState* PyThreadState_Get()
721722
722- Return the current thread state. The interpreter lock must be held. When the
723- current thread state is *NULL *, this issues a fatal error (so that the caller
724- needn't check for *NULL *).
723+ Return the current thread state. The global interpreter lock must be held.
724+ When the current thread state is *NULL *, this issues a fatal error (so that
725+ the caller needn't check for *NULL *).
725726
726727
727728.. cfunction :: PyThreadState* PyThreadState_Swap(PyThreadState *tstate)
728729
729730 Swap the current thread state with the thread state given by the argument
730- *tstate *, which may be *NULL *. The interpreter lock must be held.
731+ *tstate *, which may be *NULL *. The global interpreter lock must be held.
731732
732733
733734.. cfunction :: PyObject* PyThreadState_GetDict()
@@ -752,14 +753,15 @@ created.
752753
753754.. cfunction :: PyGILState_STATE PyGILState_Ensure()
754755
755- Ensure that the current thread is ready to call the Python C API regardless of
756- the current state of Python, or of its thread lock. This may be called as many
757- times as desired by a thread as long as each call is matched with a call to
758- :cfunc: `PyGILState_Release `. In general, other thread-related APIs may be used
759- between :cfunc: `PyGILState_Ensure ` and :cfunc: `PyGILState_Release ` calls as long
760- as the thread state is restored to its previous state before the Release(). For
761- example, normal usage of the :cmacro: `Py_BEGIN_ALLOW_THREADS ` and
762- :cmacro: `Py_END_ALLOW_THREADS ` macros is acceptable.
756+ Ensure that the current thread is ready to call the Python C API regardless
757+ of the current state of Python, or of the global interpreter lock. This may
758+ be called as many times as desired by a thread as long as each call is
759+ matched with a call to :cfunc: `PyGILState_Release `. In general, other
760+ thread-related APIs may be used between :cfunc: `PyGILState_Ensure ` and
761+ :cfunc: `PyGILState_Release ` calls as long as the thread state is restored to
762+ its previous state before the Release(). For example, normal usage of the
763+ :cmacro: `Py_BEGIN_ALLOW_THREADS ` and :cmacro: `Py_END_ALLOW_THREADS ` macros is
764+ acceptable.
763765
764766 The return value is an opaque "handle" to the thread state when
765767 :cfunc: `PyGILState_Ensure ` was called, and must be passed to
@@ -793,35 +795,34 @@ pointer and a void argument.
793795
794796.. index :: single: setcheckinterval() (in module sys)
795797
796- Every check interval, when the interpreter lock is released and reacquired,
797- python will also call any such provided functions. This can be used for
798- example by asynchronous IO handlers. The notification can be scheduled
799- from a worker thread and the actual call than made at the earliest
800- convenience by the main thread where it has possession of the global
801- interpreter lock and can perform any Python API calls.
798+ Every check interval, when the global interpreter lock is released and
799+ reacquired, python will also call any such provided functions. This can be used
800+ for example by asynchronous IO handlers. The notification can be scheduled from
801+ a worker thread and the actual call than made at the earliest convenience by the
802+ main thread where it has possession of the global interpreter lock and can
803+ perform any Python API calls.
802804
803805.. cfunction :: void Py_AddPendingCall( int (*func)(void *, void *arg) )
804806
805807 .. index :: single: Py_AddPendingCall()
806808
807- Post a notification to the Python main thread. If successful,
808- *func * will be called with the argument *arg * at the earliest
809- convenience. *func * will be called having the global interpreter
810- lock held and can thus use the full Python API and can take any
811- action such as setting object attributes to signal IO completion.
812- It must return 0 on success, or -1 signalling an exception.
813- The notification function won't be interrupted to perform another
814- asynchronous notification recursively,
815- but it can still be interrupted to switch threads if the interpreter
816- lock is released, for example, if it calls back into python code.
809+ Post a notification to the Python main thread. If successful, *func * will be
810+ called with the argument *arg * at the earliest convenience. *func * will be
811+ called having the global interpreter lock held and can thus use the full
812+ Python API and can take any action such as setting object attributes to
813+ signal IO completion. It must return 0 on success, or -1 signalling an
814+ exception. The notification function won't be interrupted to perform another
815+ asynchronous notification recursively, but it can still be interrupted to
816+ switch threads if the global interpreter lock is released, for example, if it
817+ calls back into python code.
817818
818819 This function returns 0 on success in which case the notification has been
819- scheduled. Otherwise, for example if the notification buffer is full,
820- it returns -1 without setting any exception.
820+ scheduled. Otherwise, for example if the notification buffer is full, it
821+ returns -1 without setting any exception.
821822
822- This function can be called on any thread, be it a Python thread or
823- some other system thread. If it is a Python thread, it doesn't matter if
824- it holds the global interpreter lock or not.
823+ This function can be called on any thread, be it a Python thread or some
824+ other system thread. If it is a Python thread, it doesn't matter if it holds
825+ the global interpreter lock or not.
825826
826827 .. versionadded :: 2.7
827828
0 commit comments