//python2.7.5/Python/ceval.cstatic PyThread_type_lock interpreter_lock =0;/* This is the GIL */static PyThread_type_lock pending_lock =0;/* for pending calls */staticlong main_thread =0;intPyEval_ThreadsInitialized(void){return interpreter_lock !=0;}voidPyEval_InitThreads(void){if(interpreter_lock)return; interpreter_lock =PyThread_allocate_lock();PyThread_acquire_lock(interpreter_lock,1); main_thread =PyThread_get_thread_ident();}//python2.7.5/Python/Thread_pthread.htypedefstruct{/* 0=unlocked, 1=locked */char locked;/* a <cond, mutex> pair to handle an acquire of a locked lock */pthread_cond_t lock_released;pthread_mutex_t mut;} pthread_lock;
if (--_Py_Ticker < 0) {
if (*next_instr == SETUP_FINALLY) {
/* Make the last opcode before
a try: finally: block uninterruptible. */
goto fast_next_opcode;
}
_Py_Ticker = _Py_CheckInterval;
tstate->tick_counter++;
#ifdef WITH_TSC
ticked = 1;
#endif
if (pendingcalls_to_do) {
if (Py_MakePendingCalls() < 0) {
why = WHY_EXCEPTION;
goto on_error;
}
if (pendingcalls_to_do)
/* MakePendingCalls() didn't succeed.
Force early re-execution of this
"periodic" code, possibly after
a thread switch */
_Py_Ticker = 0;
}
if (interpreter_lock) {
/* Give another thread a chance */
//这里是一次GIL锁的释放和获取,子线程有机会获取GIL得以运行
if (PyThreadState_Swap(NULL) != tstate)
Py_FatalError("ceval: tstate mix-up");
PyThread_release_lock(interpreter_lock);
/* Other threads may run now */
PyThread_acquire_lock(interpreter_lock, 1);
if (PyThreadState_Swap(tstate) != NULL)
Py_FatalError("ceval: orphan tstate");