Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(31065)

Side by Side Diff: Python/pystate.c

Issue 20891: PyGILState_Ensure on non-Python thread causes fatal error
Patch Set: Created 3 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* Thread and interpreter state structures and their interfaces */ 2 /* Thread and interpreter state structures and their interfaces */
3 3
4 #include "Python.h" 4 #include "Python.h"
5 5
6 #define GET_TSTATE() \ 6 #define GET_TSTATE() \
7 ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current)) 7 ((PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current))
8 #define SET_TSTATE(value) \ 8 #define SET_TSTATE(value) \
9 _Py_atomic_store_relaxed(&_PyThreadState_Current, (Py_uintptr_t)(value)) 9 _Py_atomic_store_relaxed(&_PyThreadState_Current, (Py_uintptr_t)(value))
10 #define GET_INTERP_STATE() \ 10 #define GET_INTERP_STATE() \
(...skipping 789 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 return 0; 800 return 0;
801 801
802 return (tstate == PyGILState_GetThisThreadState()); 802 return (tstate == PyGILState_GetThisThreadState());
803 } 803 }
804 804
805 PyGILState_STATE 805 PyGILState_STATE
806 PyGILState_Ensure(void) 806 PyGILState_Ensure(void)
807 { 807 {
808 int current; 808 int current;
809 PyThreadState *tcur; 809 PyThreadState *tcur;
810 int need_init_threads = 0;
811
810 /* Note that we do not auto-init Python here - apart from 812 /* Note that we do not auto-init Python here - apart from
811 potential races with 2 threads auto-initializing, pep-311 813 potential races with 2 threads auto-initializing, pep-311
812 spells out other issues. Embedders are expected to have 814 spells out other issues. Embedders are expected to have
813 called Py_Initialize() and usually PyEval_InitThreads(). 815 called Py_Initialize() and usually PyEval_InitThreads().
814 */ 816 */
815 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */ 817 assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */
816 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey); 818 tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);
817 if (tcur == NULL) { 819 if (tcur == NULL) {
818 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is 820 need_init_threads = 1;
819 called from a new thread for the first time, we need the create the
820 GIL. */
821 PyEval_InitThreads();
822 821
823 /* Create a new thread state for this thread */ 822 /* Create a new thread state for this thread */
824 tcur = PyThreadState_New(autoInterpreterState); 823 tcur = PyThreadState_New(autoInterpreterState);
825 if (tcur == NULL) 824 if (tcur == NULL)
826 Py_FatalError("Couldn't create thread-state for new thread"); 825 Py_FatalError("Couldn't create thread-state for new thread");
827 /* This is our thread state! We'll need to delete it in the 826 /* This is our thread state! We'll need to delete it in the
828 matching call to PyGILState_Release(). */ 827 matching call to PyGILState_Release(). */
829 tcur->gilstate_counter = 0; 828 tcur->gilstate_counter = 0;
830 current = 0; /* new thread state is never current */ 829 current = 0; /* new thread state is never current */
831 } 830 }
832 else 831 else
833 current = PyThreadState_IsCurrent(tcur); 832 current = PyThreadState_IsCurrent(tcur);
833
834 if (current == 0) 834 if (current == 0)
835 PyEval_RestoreThread(tcur); 835 PyEval_RestoreThread(tcur);
836 /* Update our counter in the thread-state - no need for locks: 836 /* Update our counter in the thread-state - no need for locks:
837 - tcur will remain valid as we hold the GIL. 837 - tcur will remain valid as we hold the GIL.
838 - the counter is safe as we are the only thread "allowed" 838 - the counter is safe as we are the only thread "allowed"
839 to modify this value 839 to modify this value
840 */ 840 */
841 ++tcur->gilstate_counter; 841 ++tcur->gilstate_counter;
842
843 if (need_init_threads) {
844 /* At startup, Python has no concrete GIL. If PyGILState_Ensure() is
845 called from a new thread for the first time, we need the create the
846 GIL. */
847 PyEval_InitThreads();
848 }
849
842 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED; 850 return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;
843 } 851 }
844 852
845 void 853 void
846 PyGILState_Release(PyGILState_STATE oldstate) 854 PyGILState_Release(PyGILState_STATE oldstate)
847 { 855 {
848 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value( 856 PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(
849 autoTLSkey); 857 autoTLSkey);
850 if (tcur == NULL) 858 if (tcur == NULL)
851 Py_FatalError("auto-releasing thread-state, " 859 Py_FatalError("auto-releasing thread-state, "
(...skipping 28 matching lines...) Expand all
880 PyEval_SaveThread(); 888 PyEval_SaveThread();
881 } 889 }
882 890
883 #endif /* WITH_THREAD */ 891 #endif /* WITH_THREAD */
884 892
885 #ifdef __cplusplus 893 #ifdef __cplusplus
886 } 894 }
887 #endif 895 #endif
888 896
889 897
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+