void PyInterpreterState_Delete(PyInterpreterState *interp) { PyInterpreterState **p; zapthreads(interp); HEAD_LOCK(); for (p = &_PyRuntime.interpreters.head; ; p = &(*p)->next) { if (*p == NULL) Py_FatalError( "PyInterpreterState_Delete: invalid interp"); if (*p == interp) break; } if (interp->tstate_head != NULL) Py_FatalError("PyInterpreterState_Delete: remaining threads"); *p = interp->next; if (_PyRuntime.interpreters.main == interp) { _PyRuntime.interpreters.main = NULL; if (_PyRuntime.interpreters.head != NULL) Py_FatalError("PyInterpreterState_Delete: remaining subinterpreters"); } HEAD_UNLOCK(); if (interp->id_mutex != NULL) { PyThread_free_lock(interp->id_mutex); } if (interp->ceval.pending.lock != NULL) { PyThread_free_lock(interp->ceval.pending.lock); } PyMem_RawFree(interp); }
static void evaluator_dealloc(PyFFEvaluatorObject *self) { int i; #ifdef WITH_THREAD if (self->eval_func == evaluator) { threadinfo *tinfo = (threadinfo *)self->scratch; if (self->global_lock != NULL) PyThread_free_lock(self->global_lock); if (self->binfo != NULL) deallocate_barrier(self->binfo); for (i = 1; i < self->nthreads; i++) { int j = 50; tinfo->exit = 1; #if THREAD_DEBUG printf("Releasing thread %d\n", tinfo->input.thread_id); #endif PyThread_release_lock(tinfo->lock); while (!tinfo->stop && j--) { #if THREAD_DEBUG printf("evaluator_dealloc: Waiting for thread %d to stop (tinfo->stop is %d)\n", tinfo->input.thread_id, tinfo->stop); #endif #ifdef MS_WINDOWS Sleep(10); /* 10 ms */ #else { struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 10000; /* 10 ms */ select(0, NULL, NULL, NULL, &tv); } #endif } Py_XDECREF(tinfo->energy.gradients); free(tinfo->energy.energy_terms); PyThread_free_lock(tinfo->lock); tinfo++; } } #endif #ifdef WITH_MPI if (self->energy_parts) free(self->energy_parts); if (self->gradient_parts) free(self->gradient_parts); #endif Py_XDECREF(self->universe_spec); Py_XDECREF(self->terms); Py_XDECREF(self->energy_terms_array); if (self->scratch != NULL) free(self->scratch); PyObject_Del(self); }
/* * Delete all interpreter states except the main interpreter. If there * is a current interpreter state, it *must* be the main interpreter. */ void _PyInterpreterState_DeleteExceptMain() { PyThreadState *tstate = PyThreadState_Swap(NULL); if (tstate != NULL && tstate->interp != _PyRuntime.interpreters.main) { Py_FatalError("PyInterpreterState_DeleteExceptMain: not main interpreter"); } HEAD_LOCK(); PyInterpreterState *interp = _PyRuntime.interpreters.head; _PyRuntime.interpreters.head = NULL; while (interp != NULL) { if (interp == _PyRuntime.interpreters.main) { _PyRuntime.interpreters.main->next = NULL; _PyRuntime.interpreters.head = interp; interp = interp->next; continue; } PyInterpreterState_Clear(interp); // XXX must activate? zapthreads(interp); if (interp->id_mutex != NULL) { PyThread_free_lock(interp->id_mutex); } PyInterpreterState *prev_interp = interp; interp = interp->next; PyMem_RawFree(prev_interp); } HEAD_UNLOCK(); if (_PyRuntime.interpreters.head == NULL) { Py_FatalError("PyInterpreterState_DeleteExceptMain: missing main"); } PyThreadState_Swap(tstate); }
/* atexit() handler that'll call unload_add_on() for every item in the * dictionary. */ static void beos_cleanup_dyn( void ) { if( beos_dyn_images ) { int idx; int list_size; PyObject *id_list; #ifdef WITH_THREAD PyThread_acquire_lock( beos_dyn_lock, 1 ); #endif id_list = PyDict_Values( beos_dyn_images ); list_size = PyList_Size( id_list ); for( idx = 0; idx < list_size; idx++ ) { PyObject *the_item; the_item = PyList_GetItem( id_list, idx ); beos_nuke_dyn( the_item ); } PyDict_Clear( beos_dyn_images ); #ifdef WITH_THREAD PyThread_free_lock( beos_dyn_lock ); #endif } }
static void _free_init_once_lock(PyObject *capsule) { PyThread_type_lock lock; lock = PyCapsule_GetPointer(capsule, "cffi_init_once_lock"); if (lock != NULL) PyThread_free_lock(lock); }
static int test_bpo20891(void) { /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must call PyEval_InitThreads() for us in this case. */ PyThread_type_lock lock = PyThread_allocate_lock(); if (!lock) { fprintf(stderr, "PyThread_allocate_lock failed!"); return 1; } _testembed_Py_Initialize(); unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock); if (thrd == PYTHREAD_INVALID_THREAD_ID) { fprintf(stderr, "PyThread_start_new_thread failed!"); return 1; } PyThread_acquire_lock(lock, WAIT_LOCK); Py_BEGIN_ALLOW_THREADS /* wait until the thread exit */ PyThread_acquire_lock(lock, WAIT_LOCK); Py_END_ALLOW_THREADS PyThread_free_lock(lock); return 0; }
static void Connection_dtor(pycbc_Connection *self) { if (self->instance) { lcb_destroy(self->instance); self->instance = NULL; } Py_XDECREF(self->dfl_fmt); Py_XDECREF(self->errors); Py_XDECREF(self->tc); Py_XDECREF(self->bucket); if (self->iops) { pycbc_iops_free(self->iops); self->iops = NULL; } #ifdef WITH_THREAD if (self->lock) { PyThread_free_lock(self->lock); self->lock = NULL; } #endif Py_TYPE(self)->tp_free((PyObject*)self); }
void PyInterpreterState_Delete(PyInterpreterState *interp) { PyInterpreterState **p; zapthreads(interp); HEAD_LOCK(); for (p = &interp_head; ; p = &(*p)->next) { if (*p == NULL) Py_FatalError( "PyInterpreterState_Delete: invalid interp"); if (*p == interp) break; } if (interp->tstate_head != NULL) Py_FatalError("PyInterpreterState_Delete: remaining threads"); *p = interp->next; HEAD_UNLOCK(); PyMem_RawFree(interp); #ifdef WITH_THREAD if (interp_head == NULL && head_mutex != NULL) { PyThread_free_lock(head_mutex); head_mutex = NULL; } #endif }
static int _setup_ssl_threads(void) { unsigned int i; if (_ssl_locks == NULL) { _ssl_locks_count = CRYPTO_num_locks(); _ssl_locks = (PyThread_type_lock *) malloc(sizeof(PyThread_type_lock) * _ssl_locks_count); if (_ssl_locks == NULL) return 0; memset(_ssl_locks, 0, sizeof(PyThread_type_lock) * _ssl_locks_count); for (i = 0; i < _ssl_locks_count; i++) { _ssl_locks[i] = PyThread_allocate_lock(); if (_ssl_locks[i] == NULL) { unsigned int j; for (j = 0; j < i; j++) { PyThread_free_lock(_ssl_locks[j]); } free(_ssl_locks); return 0; } } CRYPTO_set_locking_callback(_ssl_thread_locking_function); CRYPTO_set_id_callback(_ssl_thread_id_function); } return 1; }
static void destruct_lock(PyThread_type_lock lock) { PyThread_acquire_lock(lock, 0); PyThread_release_lock(lock); PyThread_free_lock(lock); }
static void Bucket_dtor(pycbc_Bucket *self) { if (self->flags & PYCBC_CONN_F_CLOSED) { lcb_destroy(self->instance); self->instance = NULL; } if (self->instance) { lcb_set_cookie(self->instance, NULL); pycbc_schedule_dtor_event(self); } Py_XDECREF(self->dtorcb); Py_XDECREF(self->dfl_fmt); Py_XDECREF(self->tc); Py_XDECREF(self->bucket); Py_XDECREF(self->conncb); Py_XDECREF(self->dur_testhook); Py_XDECREF(self->iopswrap); if (self->instance) { lcb_destroy(self->instance); } #ifdef WITH_THREAD if (self->lock) { PyThread_free_lock(self->lock); self->lock = NULL; } #endif Py_TYPE(self)->tp_free((PyObject*)self); }
static void Dealloc(compobject *self) { #ifdef WITH_THREAD PyThread_free_lock(self->lock); #endif Py_XDECREF(self->unused_data); Py_XDECREF(self->unconsumed_tail); PyObject_Del(self); }
static void Compressor_dealloc(Compressor *self) { lzma_end(&self->lzs); #ifdef WITH_THREAD if (self->lock != NULL) PyThread_free_lock(self->lock); #endif Py_TYPE(self)->tp_free((PyObject *)self); }
static void lock_dealloc(lockobject *self) { /* Unlock the lock so it's safe to free it */ PyThread_acquire_lock(self->lock_lock, 0); PyThread_release_lock(self->lock_lock); PyThread_free_lock(self->lock_lock); PyObject_Del(self); }
static void EVP_dealloc(EVPobject *self) { #ifdef WITH_THREAD if (self->lock != NULL) PyThread_free_lock(self->lock); #endif EVP_MD_CTX_free(self->ctx); Py_XDECREF(self->name); PyObject_Del(self); }
static void threadLockDestructor(Box* _self) { RELEASE_ASSERT(_self->cls == thread_lock_cls, ""); BoxedThreadLock* self = static_cast<BoxedThreadLock*>(_self); if (self->lock_lock != NULL) { /* Unlock the lock so it's safe to free it */ PyThread_acquire_lock(self->lock_lock, 0); PyThread_release_lock(self->lock_lock); PyThread_free_lock(self->lock_lock); } }
void MPyEmbed_Fini(void) { int i; if (!PythonAvailable) { return; } MPyEmbed_Exiting = 1; for (i = 0; i < THREADS; i++) { if (VALID(threaddata[i])) { PyThread_acquire_lock(threaddata[i].exitlock, WAIT_LOCK); PyThread_free_lock(threaddata[i].exitlock); } else if (threaddata[i].exitlock != 0) { PyThread_free_lock(threaddata[i].exitlock); } } memset(threaddata, 0, sizeof(threaddata)); PyEval_RestoreThread(mainstate); Py_Finalize(); MPyEmbed_Exiting = 0; }
static void mailbox_dealloc(MailboxObject *self) { if (self->disconnected == 0) SP_disconnect(self->mbox); Py_DECREF(self->private_group); #ifdef SPREAD_DISCONNECT_RACE_BUG if (self->spread_lock) PyThread_free_lock(self->spread_lock); #endif PyObject_Del(self); }
static void lock_dealloc(lockobject *self) { if (self->in_weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *) self); if (self->lock_lock != NULL) { /* Unlock the lock so it's safe to free it */ if (self->locked) PyThread_release_lock(self->lock_lock); PyThread_free_lock(self->lock_lock); } PyObject_Del(self); }
static void rlock_dealloc(rlockobject *self) { assert(self->rlock_lock); if (self->in_weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *) self); /* Unlock the lock so it's safe to free it */ if (self->rlock_count > 0) PyThread_release_lock(self->rlock_lock); PyThread_free_lock(self->rlock_lock); Py_TYPE(self)->tp_free(self); }
static PyObject * test_thread_state(PyObject *self, PyObject *args) { PyObject *fn; int success = 1; if (!PyArg_ParseTuple(args, "O:test_thread_state", &fn)) return NULL; if (!PyCallable_Check(fn)) { PyErr_Format(PyExc_TypeError, "'%s' object is not callable", fn->ob_type->tp_name); return NULL; } /* Ensure Python is set up for threading */ PyEval_InitThreads(); thread_done = PyThread_allocate_lock(); if (thread_done == NULL) return PyErr_NoMemory(); PyThread_acquire_lock(thread_done, 1); /* Start a new thread with our callback. */ PyThread_start_new_thread(_make_call_from_thread, fn); /* Make the callback with the thread lock held by this thread */ success &= _make_call(fn); /* Do it all again, but this time with the thread-lock released */ Py_BEGIN_ALLOW_THREADS success &= _make_call(fn); PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ Py_END_ALLOW_THREADS /* And once more with and without a thread XXX - should use a lock and work out exactly what we are trying to test <wink> */ Py_BEGIN_ALLOW_THREADS PyThread_start_new_thread(_make_call_from_thread, fn); success &= _make_call(fn); PyThread_acquire_lock(thread_done, 1); /* wait for thread to finish */ Py_END_ALLOW_THREADS /* Release lock we acquired above. This is required on HP-UX. */ PyThread_release_lock(thread_done); PyThread_free_lock(thread_done); if (!success) return NULL; Py_RETURN_NONE; }
void _PyRuntimeState_Fini(_PyRuntimeState *runtime) { /* Force the allocator used by _PyRuntimeState_Init(). */ PyMemAllocatorEx old_alloc; _PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc); if (runtime->interpreters.mutex != NULL) { PyThread_free_lock(runtime->interpreters.mutex); runtime->interpreters.mutex = NULL; } PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); }
static void simplequeue_dealloc(simplequeueobject *self) { PyObject_GC_UnTrack(self); if (self->lock != NULL) { /* Unlock the lock so it's safe to free it */ if (self->locked > 0) PyThread_release_lock(self->lock); PyThread_free_lock(self->lock); } Py_XDECREF(self->lst); if (self->weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *) self); Py_TYPE(self)->tp_free(self); }
static void CReader_Buffer_dealloc(CReader_Buffer* self) { # ifdef DEBUG_ALLOC fprintf(stderr, "Dealloc: %p\n", (void *) self); # endif PyObject_GC_UnTrack((PyObject*) self); CReader_Buffer_clear(self); if (self->type->clear) self->type->clear(self); # ifdef WITH_THREAD PyThread_free_lock(self->lock); self->lock = NULL; # endif self->ob_type->tp_free((PyObject*)self); # ifdef DEBUG_ALLOC fprintf(stderr, "Dealloc done: %p\n", (void *) self); # endif }
BOOL Ps_CloseLog() { static int init = 0; //只能关闭一次 if(init) return TRUE; init = 1; //将文件指针置空 GET_LOG_LOCK(); log_normal = NULL; log_waring = NULL; log_error = NULL; RELEASE_LOG_LOCK(); //释放锁 #if LOG_LOCK_ENABLE PyThread_free_lock(log_lock); #endif return TRUE; }
static void bsddb_dealloc(bsddbobject *dp) { #ifdef WITH_THREAD if (dp->di_lock) { PyThread_acquire_lock(dp->di_lock, 0); PyThread_release_lock(dp->di_lock); PyThread_free_lock(dp->di_lock); dp->di_lock = NULL; } #endif if (dp->di_bsddb != NULL) { int status; Py_BEGIN_ALLOW_THREADS status = (dp->di_bsddb->close)(dp->di_bsddb); Py_END_ALLOW_THREADS if (status != 0) fprintf(stderr, "Python bsddb: close errno %d in dealloc\n", errno); }
int MPyEmbed_RunThread(char *file) { int i; FILE *fp; if (!PythonAvailable) { return 0; } fp = fopen(file, "r"); if (!fp) { perror("fopen"); return 0; } PyThread_acquire_lock(threaddatalock, WAIT_LOCK); for (i = 0; i < THREADS; i++) { if (!VALID(threaddata[i])) { if (threaddata[i].exitlock != 0) PyThread_free_lock(threaddata[i].exitlock); threaddata[i].thread_id = PyThread_get_thread_ident(); PyEval_AcquireLock(); threaddata[i].threadstate = Py_NewInterpreter(); initpydega(); PySys_SetArgv(argc, argv); PyEval_ReleaseThread(threaddata[i].threadstate); threaddata[i].mainstate = PyThreadState_New(threaddata[i].threadstate->interp); threaddata[i].mainstate->thread_id = mainstate->thread_id; threaddata[i].exitlock = PyThread_allocate_lock(); PyThread_acquire_lock(threaddata[i].exitlock, WAIT_LOCK); break; } } PyThread_release_lock(threaddatalock); if (i == THREADS) { fclose(fp); return 0; } PyEval_AcquireThread(threaddata[i].threadstate); PyRun_SimpleFile(fp, file); PyEval_ReleaseThread(threaddata[i].threadstate); fclose(fp); PyThread_acquire_lock(threaddatalock, WAIT_LOCK); PyEval_AcquireThread(threaddata[i].threadstate); PyThread_release_lock(threaddatalock); Py_XDECREF(threaddata[i].postframe); PyThread_acquire_lock(threaddatalock, WAIT_LOCK); PyThreadState_Clear(threaddata[i].mainstate); PyThreadState_Delete(threaddata[i].mainstate); Py_EndInterpreter(threaddata[i].threadstate); PyEval_ReleaseLock(); threaddata[i].mainstate = threaddata[i].threadstate = 0; threaddata[i].postframe = 0; PyThread_release_lock(threaddatalock); PyThread_release_lock(threaddata[i].exitlock); return 1; }
/* PyCapsule_New is redefined to be PyCObject_FromVoidPtr in _cffi_backend, which gives 2.6 compatibility; but the destructor signature is different */ static void _free_init_once_lock(void *lock) { PyThread_free_lock((PyThread_type_lock)lock); }
static void deallocate_barrier(barrierinfo *binfo) { PyThread_free_lock(binfo->lock); free(binfo); }
static PyObject *ffi_init_once(FFIObject *self, PyObject *args, PyObject *kwds) { static char *keywords[] = {"func", "tag", NULL}; PyObject *cache, *func, *tag, *tup, *res, *x, *lockobj; PyThread_type_lock lock; if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", keywords, &func, &tag)) return NULL; /* a lot of fun with reference counting and error checking in this function */ /* atomically get or create a new dict (no GIL release) */ cache = self->init_once_cache; if (cache == NULL) { cache = PyDict_New(); if (cache == NULL) return NULL; self->init_once_cache = cache; } /* get the tuple from cache[tag], or make a new one: (False, lock) */ tup = PyDict_GetItem(cache, tag); if (tup == NULL) { lock = PyThread_allocate_lock(); if (lock == NULL) return NULL; x = PyCapsule_New(lock, "cffi_init_once_lock", _free_init_once_lock); if (x == NULL) { PyThread_free_lock(lock); return NULL; } tup = PyTuple_Pack(2, Py_False, x); Py_DECREF(x); if (tup == NULL) return NULL; x = tup; /* Possible corner case if 'tag' is an object overriding __eq__ in pure Python: the GIL may be released when we are running it. We really need to call dict.setdefault(). */ tup = PyObject_CallMethod(cache, "setdefault", "OO", tag, x); Py_DECREF(x); if (tup == NULL) return NULL; Py_DECREF(tup); /* there is still a ref inside the dict */ } res = PyTuple_GET_ITEM(tup, 1); Py_INCREF(res); if (PyTuple_GET_ITEM(tup, 0) == Py_True) { /* tup == (True, result): return the result. */ return res; } /* tup == (False, lock) */ lockobj = res; lock = (PyThread_type_lock)PyCapsule_GetPointer(lockobj, "cffi_init_once_lock"); if (lock == NULL) { Py_DECREF(lockobj); return NULL; } Py_BEGIN_ALLOW_THREADS PyThread_acquire_lock(lock, WAIT_LOCK); Py_END_ALLOW_THREADS x = PyDict_GetItem(cache, tag); if (x != NULL && PyTuple_GET_ITEM(x, 0) == Py_True) { /* the real result was put in the dict while we were waiting for PyThread_acquire_lock() above */ res = PyTuple_GET_ITEM(x, 1); Py_INCREF(res); } else { res = PyObject_CallFunction(func, ""); if (res != NULL) { tup = PyTuple_Pack(2, Py_True, res); if (tup == NULL || PyDict_SetItem(cache, tag, tup) < 0) { Py_XDECREF(tup); Py_DECREF(res); res = NULL; } } } PyThread_release_lock(lock); Py_DECREF(lockobj); return res; }