@@ -687,15 +687,14 @@ This is so common that a pair of macros exists to simplify it::
687
687
688
688
The :c:macro:`Py_BEGIN_ALLOW_THREADS` macro opens a new block and declares a
689
689
hidden local variable; the :c:macro:`Py_END_ALLOW_THREADS` macro closes the
690
- block. These two macros are still available when Python is compiled without
691
- thread support (they simply have an empty expansion).
690
+ block.
692
691
693
- When thread support is enabled, the block above expands to the following code::
692
+ The block above expands to the following code::
694
693
695
694
PyThreadState *_save;
696
695
697
696
_save = PyEval_SaveThread();
698
- ...Do some blocking I/O operation...
697
+ ... Do some blocking I/O operation ...
699
698
PyEval_RestoreThread(_save);
700
699
701
700
.. index ::
@@ -818,54 +817,40 @@ code, or when embedding the Python interpreter:
818
817
819
818
This is a no-op when called for a second time.
820
819
820
+ .. versionchanged:: 3.7
821
+ This function is now called by :c:func:`Py_Initialize()`, so you don't
822
+ have to call it yourself anymore.
823
+
821
824
.. versionchanged:: 3.2
822
825
This function cannot be called before :c:func:`Py_Initialize()` anymore.
823
826
824
827
.. index:: module: _thread
825
828
826
- .. note::
827
-
828
- When only the main thread exists, no GIL operations are needed. This is a
829
- common situation (most Python programs do not use threads), and the lock
830
- operations slow the interpreter down a bit. Therefore, the lock is not
831
- created initially. This situation is equivalent to having acquired the lock:
832
- when there is only a single thread, all object accesses are safe. Therefore,
833
- when this function initializes the global interpreter lock, it also acquires
834
- it. Before the Python :mod:`_thread` module creates a new thread, knowing
835
- that either it has the lock or the lock hasn't been created yet, it calls
836
- :c:func:`PyEval_InitThreads`. When this call returns, it is guaranteed that
837
- the lock has been created and that the calling thread has acquired it.
838
-
839
- It is **not** safe to call this function when it is unknown which thread (if
840
- any) currently has the global interpreter lock.
841
-
842
- This function is not available when thread support is disabled at compile time.
843
-
844
829
845
830
.. c:function:: int PyEval_ThreadsInitialized()
846
831
847
832
Returns a non-zero value if :c:func:`PyEval_InitThreads` has been called. This
848
833
function can be called without holding the GIL, and therefore can be used to
849
- avoid calls to the locking API when running single-threaded. This function is
850
- not available when thread support is disabled at compile time.
834
+ avoid calls to the locking API when running single-threaded.
835
+
836
+ .. versionchanged:: 3.7
837
+ The :term:`GIL` is now initialized by :c:func:`Py_Initialize()`.
851
838
852
839
853
840
.. c:function:: PyThreadState* PyEval_SaveThread()
854
841
855
842
Release the global interpreter lock (if it has been created and thread
856
843
support is enabled) and reset the thread state to *NULL*, returning the
857
844
previous thread state (which is not *NULL *). If the lock has been created,
858
- the current thread must have acquired it. (This function is available even
859
- when thread support is disabled at compile time.)
845
+ the current thread must have acquired it.
860
846
861
847
862
848
.. c:function:: void PyEval_RestoreThread(PyThreadState *tstate)
863
849
864
850
Acquire the global interpreter lock (if it has been created and thread
865
851
support is enabled) and set the thread state to *tstate*, which must not be
866
852
*NULL*. If the lock has been created, the current thread must not have
867
- acquired it, otherwise deadlock ensues. (This function is available even
868
- when thread support is disabled at compile time.)
853
+ acquired it, otherwise deadlock ensues.
869
854
870
855
871
856
.. c:function:: PyThreadState* PyThreadState_Get()
@@ -957,37 +942,37 @@ example usage in the Python source distribution.
957
942
This macro expands to ``{ PyThreadState *_save; _save = PyEval_SaveThread(); ``.
958
943
Note that it contains an opening brace; it must be matched with a following
959
944
:c:macro: `Py_END_ALLOW_THREADS ` macro. See above for further discussion of this
960
- macro. It is a no-op when thread support is disabled at compile time.
945
+ macro.
961
946
962
947
963
948
.. c :macro :: Py_END_ALLOW_THREADS
964
949
965
950
This macro expands to ``PyEval_RestoreThread(_save); } ``. Note that it contains
966
951
a closing brace; it must be matched with an earlier
967
952
:c:macro: `Py_BEGIN_ALLOW_THREADS ` macro. See above for further discussion of
968
- this macro. It is a no-op when thread support is disabled at compile time.
953
+ this macro.
969
954
970
955
971
956
.. c :macro :: Py_BLOCK_THREADS
972
957
973
958
This macro expands to ``PyEval_RestoreThread(_save); ``: it is equivalent to
974
- :c:macro: `Py_END_ALLOW_THREADS ` without the closing brace. It is a no-op when
975
- thread support is disabled at compile time.
959
+ :c:macro: `Py_END_ALLOW_THREADS ` without the closing brace.
976
960
977
961
978
962
.. c :macro :: Py_UNBLOCK_THREADS
979
963
980
964
This macro expands to ``_save = PyEval_SaveThread(); ``: it is equivalent to
981
965
:c:macro: `Py_BEGIN_ALLOW_THREADS ` without the opening brace and variable
982
- declaration. It is a no-op when thread support is disabled at compile time.
966
+ declaration.
983
967
984
968
985
969
Low-level API
986
970
-------------
987
971
988
- All of the following functions are only available when thread support is enabled
989
- at compile time, and must be called only when the global interpreter lock has
990
- been created.
972
+ All of the following functions must be called after :c:func: `Py_Initialize `.
973
+
974
+ .. versionchanged :: 3.7
975
+ :c:func: `Py_Initialize() ` now initializes the :term: `GIL `.
991
976
992
977
993
978
.. c :function :: PyInterpreterState* PyInterpreterState_New ()
@@ -1068,8 +1053,7 @@ been created.
1068
1053
If this thread already has the lock, deadlock ensues.
1069
1054
1070
1055
:c:func: `PyEval_RestoreThread ` is a higher-level function which is always
1071
- available (even when thread support isn't enabled or when threads have
1072
- not been initialized).
1056
+ available (even when threads have not been initialized).
1073
1057
1074
1058
1075
1059
.. c:function:: void PyEval_ReleaseThread(PyThreadState *tstate)
@@ -1081,8 +1065,7 @@ been created.
1081
1065
reported.
1082
1066
1083
1067
:c:func: `PyEval_SaveThread ` is a higher-level function which is always
1084
- available (even when thread support isn't enabled or when threads have
1085
- not been initialized).
1068
+ available (even when threads have not been initialized).
1086
1069
1087
1070
1088
1071
.. c:function:: void PyEval_AcquireLock()
0 commit comments