cinap_lenrekadd hg and python4bb9e2215 years agoHistory
632 lines · 16.7 KB · C
RawDownload
void_PyGILState_Fini(void){	PyThread_delete_key(autoTLSkey);	autoTLSkey = 0;	autoInterpreterState = NULL;} /* 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 is 0, 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 (!autoTLSkey)		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;} /* The public functions */PyThreadState *PyGILState_GetThisThreadState(void){	if (autoInterpreterState == NULL || autoTLSkey == 0)		return NULL;	return (PyThreadState *)PyThread_get_key_value(autoTLSkey);} PyGILState_STATEPyGILState_Ensure(void){	int current;	PyThreadState *tcur;	/* Note that we do not auto-init Python here - apart from	   potential races with 2 threads auto-initializing, pep-311	   spells out other issues.  Embedders are expected to have	   called Py_Initialize() and usually PyEval_InitThreads().	*/	assert(autoInterpreterState); /* Py_Initialize() hasn't been called! */	tcur = (PyThreadState *)PyThread_get_key_value(autoTLSkey);	if (tcur == NULL) {		/* Create a new thread state for this thread */		tcur = PyThreadState_New(autoInterpreterState);		if (tcur == NULL)			Py_FatalError("Couldn't create thread-state for new thread");		/* This is our thread state!  We'll need to delete it in the		   matching call to PyGILState_Release(). */		tcur->gilstate_counter = 0;		current = 0; /* new thread state is never current */	}	else		current = PyThreadState_IsCurrent(tcur);	if (current == 0)		PyEval_RestoreThread(tcur);	/* Update our counter in the thread-state - no need for locks:	   - tcur will remain valid as we hold the GIL.	   - the counter is safe as we are the only thread "allowed"	     to modify this value	*/	++tcur->gilstate_counter;	return current ? PyGILState_LOCKED : PyGILState_UNLOCKED;} voidPyGILState_Release(PyGILState_STATE oldstate){	PyThreadState *tcur = (PyThreadState *)PyThread_get_key_value(                                                                autoTLSkey);	if (tcur == NULL)		Py_FatalError("auto-releasing thread-state, "		              "but no thread-state for this thread");	/* We must hold the GIL and have our thread state current */	/* XXX - remove the check - the assert should be fine,	   but while this is very new (April 2003), the extra check	   by release-only users can't hurt.	*/	if (! PyThreadState_IsCurrent(tcur))		Py_FatalError("This thread state must be current when releasing");	assert(PyThreadState_IsCurrent(tcur));	--tcur->gilstate_counter;	assert(tcur->gilstate_counter >= 0); /* illegal counter value */ 	/* If we're going to destroy this thread-state, we must	 * clear it while the GIL is held, as destructors may run.	 */	if (tcur->gilstate_counter == 0) {		/* can't have been locked when we created it */		assert(oldstate == PyGILState_UNLOCKED);		PyThreadState_Clear(tcur);		/* Delete the thread-state.  Note this releases the GIL too!		 * It's vital that the GIL be held here, to avoid shutdown		 * races; see bugs 225673 and 1061968 (that nasty bug has a		 * habit of coming back).		 */		PyThreadState_DeleteCurrent();	}	/* Release the lock if necessary */	else if (oldstate == PyGILState_UNLOCKED)		PyEval_SaveThread();} #ifdef __cplusplus}#endif #endif /* WITH_THREAD */  

Lines 501–632 of 632. Previous lines