/* When a thread state is created for a thread by some mechanism other than PyGILState_Ensure, it's important that the GILState machinery knows about it so it doesn't try to create another thread state for the thread (this is a better fix for SF bug #1010677 than the first one attempted). */ static void _PyGILState_NoteThreadState(PyThreadState* tstate) { /* If autoTLSkey isn't initialized, this must be the very first threadstate created in Py_Initialize(). Don't do anything for now (we'll be back here when _PyGILState_Init is called). */ if (!autoInterpreterState) return; /* Stick the thread state for this thread in thread local storage. The only situation where you can legitimately have more than one thread state for an OS level thread is when there are multiple interpreters, when: a) You shouldn't really be using the PyGILState_ APIs anyway, and: b) The slightly odd way PyThread_set_key_value works (see comments by its implementation) means that the first thread state created for that given OS level thread will "win", which seems reasonable behaviour. */ if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) Py_FatalError("Couldn't create autoTLSkey mapping"); /* PyGILState_Release must not try to delete this thread state. */ tstate->gilstate_counter = 1; }
/* Reset the TLS key - called by PyOS_AfterFork. * This should not be necessary, but some - buggy - pthread implementations * don't flush TLS on fork, see issue #10517. */ void _PyGILState_Reinit(void) { PyThreadState *tstate = PyGILState_GetThisThreadState(); PyThread_delete_key(autoTLSkey); if ((autoTLSkey = PyThread_create_key()) == -1) Py_FatalError("Could not allocate TLS entry"); /* re-associate the current thread state with the new key */ if (PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) Py_FatalError("Couldn't create autoTLSkey mapping"); }
/* Reset the TLS key - called by PyOS_AfterFork(). * This should not be necessary, but some - buggy - pthread implementations * don't reset TLS upon fork(), see issue #10517. */ void _PyGILState_Reinit(void) { PyThreadState *tstate = PyGILState_GetThisThreadState(); PyThread_delete_key(autoTLSkey); if ((autoTLSkey = PyThread_create_key()) == -1) Py_FatalError("Could not allocate TLS entry"); /* If the thread had an associated auto thread state, reassociate it with * the new key. */ if (tstate && PyThread_set_key_value(autoTLSkey, (void *)tstate) < 0) Py_FatalError("Couldn't create autoTLSkey mapping"); }
static inline void pyg_restore_current_main_loop (GMainLoop *main_loop) { GMainLoop *prev; g_return_if_fail (pyg_current_main_loop_key != -1); prev = PyThread_get_key_value(pyg_current_main_loop_key); if (prev != NULL) g_main_loop_unref(prev); PyThread_delete_key_value(pyg_current_main_loop_key); if (main_loop != NULL) PyThread_set_key_value(pyg_current_main_loop_key, main_loop); }
static inline GMainLoop * pyg_save_current_main_loop (GMainLoop *main_loop) { GMainLoop *retval; g_return_val_if_fail(main_loop != NULL, NULL); if (pyg_current_main_loop_key == -1) pyg_current_main_loop_key = PyThread_create_key(); retval = PyThread_get_key_value(pyg_current_main_loop_key); PyThread_delete_key_value(pyg_current_main_loop_key); PyThread_set_key_value(pyg_current_main_loop_key, g_main_loop_ref(main_loop)); return retval; }