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 PyObject* faulthandler_dump_traceback_later(PyObject *self, PyObject *args, PyObject *kwargs) { static char *kwlist[] = {"timeout", "repeat", "file", "exit", NULL}; double timeout; PY_TIMEOUT_T timeout_ms; int repeat = 0; PyObject *file = NULL; int fd; int exit = 0; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "d|iOi:dump_tracebacks_later", kwlist, &timeout, &repeat, &file, &exit)) return NULL; timeout *= 1e6; if (timeout >= (double) PY_TIMEOUT_MAX) { PyErr_SetString(PyExc_OverflowError, "timeout value is too large"); return NULL; } timeout_ms = (PY_TIMEOUT_T)timeout; if (timeout_ms <= 0) { PyErr_SetString(PyExc_ValueError, "timeout must be greater than 0"); return NULL; } file = faulthandler_get_fileno(file, &fd); if (file == NULL) return NULL; /* Cancel previous thread, if running */ faulthandler_cancel_dump_tracebacks_later(); Py_XDECREF(thread.file); Py_INCREF(file); thread.file = file; thread.fd = fd; thread.timeout_ms = timeout_ms; thread.repeat = repeat; thread.interp = PyThreadState_Get()->interp; thread.exit = exit; /* Arm these locks to serve as events when released */ PyThread_acquire_lock(thread.join_event, 1); PyThread_acquire_lock(thread.cancel_event, 1); thread.running = 1; if (PyThread_start_new_thread(faulthandler_thread, NULL) == -1) { thread.running = 0; PyThread_release_lock(thread.join_event); PyThread_release_lock(thread.cancel_event); Py_CLEAR(thread.file); PyErr_SetString(PyExc_RuntimeError, "unable to start watchdog thread"); return NULL; } Py_RETURN_NONE; }
void MPyEmbed_CBPostFrame(void) { int i; if (!PythonAvailable) { return; } PyThread_acquire_lock(threaddatalock, WAIT_LOCK); for (i = 0; i < THREADS; i++) { if (VALID(threaddata[i])) { PyObject *cb = threaddata[i].postframe; PyThreadState *ts = threaddata[i].mainstate; PyObject *rv; PyThread_release_lock(threaddatalock); ts->thread_id = PyThread_get_thread_ident(); PyEval_AcquireThread(ts); if (cb != NULL && PyCallable_Check(cb)) { rv = PyObject_CallObject(cb, NULL); Py_XDECREF(rv); if (rv == NULL) { PyErr_Print(); } } PyEval_ReleaseThread(ts); PyThread_acquire_lock(threaddatalock, WAIT_LOCK); } } PyThread_release_lock(threaddatalock); }
static void evaluator_thread(void *arg) { threadinfo *info = (threadinfo *)arg; PyFFEnergyTermObject *term; int i; while (1) { #if THREAD_DEBUG printf("Thread %d waiting for lock...\n", info->input.thread_id); #endif PyThread_acquire_lock(info->lock, 1); #if THREAD_DEBUG printf("Thread %d running\n", info->input.thread_id); #endif if (info->exit) { info->stop = 1; break; } for (i = 0; i < info->evaluator->nterms+1; i++) info->energy.energy_terms[i] = 0.; info->energy.energy = 0.; info->energy.virial_available = 1; info->energy.error = 0; if (info->with_gradients && info->energy.gradients != NULL) { double *data = (double *)((PyArrayObject *)info->energy.gradients)->data; for (i = 0; i < 3*info->input.natoms; i++) data[i] = 0.; } PyThread_acquire_lock(info->evaluator->global_lock, 1); info->done = 0; PyThread_release_lock(info->evaluator->global_lock); for (i = 0; i < info->evaluator->ntermobjects; i++) { term = ((PyFFEnergyTermObject **)info->evaluator->terms->data)[i]; if (term->threaded) { (*term->eval_func)(term, info->evaluator, &info->input, &info->energy); #if THREAD_DEBUG { int j; printf("Thread %d: %s: ", info->input.thread_id, term->evaluator_name); for (j = term->index; j < term->index+term->nterms; j++) printf("%lf ", info->energy.energy_terms[j]); printf("\n"); } #endif } } PyThread_acquire_lock(info->evaluator->global_lock, 1); info->done = 1; PyThread_release_lock(info->evaluator->global_lock); } }
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; }
static PyObject * rlock_acquire_restore(rlockobject *self, PyObject *arg) { long owner; unsigned long count; int r = 1; if (!PyArg_ParseTuple(arg, "kl:_acquire_restore", &count, &owner)) return NULL; if (!PyThread_acquire_lock(self->rlock_lock, 0)) { Py_BEGIN_ALLOW_THREADS r = PyThread_acquire_lock(self->rlock_lock, 1); Py_END_ALLOW_THREADS }
static PyObject * Connection__thr_lockop(pycbc_Connection *self, PyObject *arg) { int rv; int is_unlock = 0; rv = PyArg_ParseTuple(arg, "i:is_unlock", &is_unlock); if (!rv) { return NULL; } if (!self->lockmode) { PYCBC_EXC_WRAP(PYCBC_EXC_THREADING, 0, "lockmode is LOCKMODE_NONE"); return NULL; } if (is_unlock) { PyThread_release_lock(self->lock); } else { if (!PyThread_acquire_lock(self->lock, WAIT_LOCK)) { PYCBC_EXC_WRAP(PYCBC_EXC_THREADING, 0, "Couldn't lock"); return NULL; } } Py_RETURN_NONE; }
static void _ssl_thread_locking_function (int mode, int n, const char *file, int line) { /* this function is needed to perform locking on shared data structures. (Note that OpenSSL uses a number of global data structures that will be implicitly shared whenever multiple threads use OpenSSL.) Multi-threaded applications will crash at random if it is not set. locking_function() must be able to handle up to CRYPTO_num_locks() different mutex locks. It sets the n-th lock if mode & CRYPTO_LOCK, and releases it otherwise. file and line are the file number of the function setting the lock. They can be useful for debugging. */ if ((_ssl_locks == NULL) || (n < 0) || ((unsigned)n >= _ssl_locks_count)) return; if (mode & CRYPTO_LOCK) { PyThread_acquire_lock(_ssl_locks[n], 1); } else { PyThread_release_lock(_ssl_locks[n]); } }
static void destruct_lock(PyThread_type_lock lock) { PyThread_acquire_lock(lock, 0); PyThread_release_lock(lock); PyThread_free_lock(lock); }
/* 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 } }
/* * Add an image_id to the dictionary; the module name of the loaded image * is the key. Note that if the key is already in the dict, we unload * that image; this should allow reload() to work on dynamically loaded * modules (super-keen!). */ static void beos_add_dyn( char *name, image_id id ) { int retval; PyObject *py_id; if( beos_dyn_images == NULL ) { beos_init_dyn(); } #ifdef WITH_THREAD retval = PyThread_acquire_lock( beos_dyn_lock, 1 ); #endif /* If there's already an object with this key in the dictionary, * we're doing a reload(), so let's nuke it. */ py_id = PyDict_GetItemString( beos_dyn_images, name ); if( py_id ) { beos_nuke_dyn( py_id ); retval = PyDict_DelItemString( beos_dyn_images, name ); } py_id = PyInt_FromLong( (long)id ); if( py_id ) { retval = PyDict_SetItemString( beos_dyn_images, name, py_id ); } #ifdef WITH_THREAD PyThread_release_lock( beos_dyn_lock ); #endif }
void barrier(barrierinfo *binfo, int thread_id, int nthreads) { int done = 0; if (nthreads > 1) { PyThread_acquire_lock(binfo->lock, 1); if (binfo->n == nthreads) binfo->n = 1; else binfo->n++; PyThread_release_lock(binfo->lock); while (!done) { PyThread_acquire_lock(binfo->lock, 1); done = (binfo->n == nthreads); PyThread_release_lock(binfo->lock); } } }
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); }
void _PyInterpreterState_IDIncref(PyInterpreterState *interp) { if (interp->id_mutex == NULL) { return; } PyThread_acquire_lock(interp->id_mutex, WAIT_LOCK); interp->id_refcount += 1; PyThread_release_lock(interp->id_mutex); }
static void cancel_dump_tracebacks_later(void) { /* Notify cancellation */ PyThread_release_lock(thread.cancel_event); /* Wait for thread to join */ PyThread_acquire_lock(thread.running, 1); PyThread_release_lock(thread.running); /* The main thread should always hold the cancel_event lock */ PyThread_acquire_lock(thread.cancel_event, 1); Py_CLEAR(thread.file); if (thread.header) { free(thread.header); thread.header = NULL; } }
static Box* locked(Box* _self) { RELEASE_ASSERT(_self->cls == thread_lock_cls, ""); BoxedThreadLock* self = static_cast<BoxedThreadLock*>(_self); if (PyThread_acquire_lock(self->lock_lock, 0)) { PyThread_release_lock(self->lock_lock); return False; } return True; }
static inline void CReader_Buffer_lock(CReader_Buffer *buffer) { # ifdef DEBUG_THREAD fprintf(stderr, "Buffer lock %p [", buffer); fflush(stderr); # endif assert(!buffer->state); PyThread_acquire_lock(buffer->lock, 1); # ifdef DEBUG_THREAD fprintf(stderr, " --> %i\n", ++buffer->bufferlockings); fflush(stderr); # endif }
but it needn't be locked by the same thread that unlocks it."); static PyObject * lock_locked_lock(lockobject *self) { if (PyThread_acquire_lock(self->lock_lock, 0)) { PyThread_release_lock(self->lock_lock); return PyBool_FromLong(0L); } return PyBool_FromLong(1L); }
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); } }
char * PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) { char *rv; if (_PyOS_ReadlineTState == PyThreadState_GET()) { PyErr_SetString(PyExc_RuntimeError, "can't re-enter readline"); return NULL; } if (PyOS_ReadlineFunctionPointer == NULL) { #ifdef __VMS PyOS_ReadlineFunctionPointer = vms__StdioReadline; #else PyOS_ReadlineFunctionPointer = PyOS_StdioReadline; #endif } #ifdef WITH_THREAD if (_PyOS_ReadlineLock == NULL) { _PyOS_ReadlineLock = PyThread_allocate_lock(); } #endif _PyOS_ReadlineTState = PyThreadState_GET(); Py_BEGIN_ALLOW_THREADS #ifdef WITH_THREAD PyThread_acquire_lock(_PyOS_ReadlineLock, 1); #endif /* This is needed to handle the unlikely case that the * interpreter is in interactive mode *and* stdin/out are not * a tty. This can happen, for example if python is run like * this: python -i < test1.py */ if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout))) rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt); else rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout, prompt); Py_END_ALLOW_THREADS #ifdef WITH_THREAD PyThread_release_lock(_PyOS_ReadlineLock); #endif _PyOS_ReadlineTState = NULL; return rv; }
static Box* release(Box* _self) { RELEASE_ASSERT(_self->cls == thread_lock_cls, ""); BoxedThreadLock* self = static_cast<BoxedThreadLock*>(_self); if (PyThread_acquire_lock(self->lock_lock, 0)) { PyThread_release_lock(self->lock_lock); raiseExcHelper(ThreadError, "release unlocked lock"); return None; } PyThread_release_lock(self->lock_lock); return None; }
static void faulthandler_cancel_dump_tracebacks_later(void) { if (thread.running) { /* Notify cancellation */ PyThread_release_lock(thread.cancel_event); } /* Wait for thread to join */ PyThread_acquire_lock(thread.join_event, 1); PyThread_release_lock(thread.join_event); thread.running = 0; Py_CLEAR(thread.file); }
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 */ PyThread_acquire_lock(self->lock_lock, 0); PyThread_release_lock(self->lock_lock); PyThread_free_lock(self->lock_lock); } PyObject_Del(self); }
but it needn't be locked by the same thread that unlocks it."; static PyObject * lock_locked_lock(lockobject *self, PyObject *args) { if (!PyArg_NoArgs(args)) return NULL; if (PyThread_acquire_lock(self->lock_lock, 0)) { PyThread_release_lock(self->lock_lock); return PyInt_FromLong(0L); } return PyInt_FromLong(1L); }
static PyObject * lock_PyThread_release_lock(lockobject *self) { /* Sanity check: the lock must be locked */ if (PyThread_acquire_lock(self->lock_lock, 0)) { PyThread_release_lock(self->lock_lock); PyErr_SetString(ThreadError, "release unlocked lock"); return NULL; } PyThread_release_lock(self->lock_lock); Py_INCREF(Py_None); return Py_None; }
static PyObject * lock_PyThread_acquire_lock(lockobject *self, PyObject *args) { int i = 1; if (!PyArg_ParseTuple(args, "|i:acquire", &i)) return NULL; Py_BEGIN_ALLOW_THREADS i = PyThread_acquire_lock(self->lock_lock, i); Py_END_ALLOW_THREADS return PyBool_FromLong((long)i); }
PyObject *MPyEmbed_GetPostFrame(void) { struct MPyEmbed_ThreadData *td; PyObject *postframe; PyThread_acquire_lock(threaddatalock, WAIT_LOCK); td = get_current_data(); if (!td) { PyThread_release_lock(threaddatalock); return 0; } postframe = td->postframe; PyThread_release_lock(threaddatalock); Py_XINCREF(postframe); return postframe; }
static Box* acquire(Box* _self, Box* _waitflag) { RELEASE_ASSERT(_self->cls == thread_lock_cls, ""); BoxedThreadLock* self = static_cast<BoxedThreadLock*>(_self); RELEASE_ASSERT(isSubclass(_waitflag->cls, int_cls), ""); int waitflag = static_cast<BoxedInt*>(_waitflag)->n; int rtn; { threading::GLAllowThreadsReadRegion _allow_threads; rtn = PyThread_acquire_lock(self->lock_lock, waitflag); } return boxBool(rtn); }
int MPyEmbed_SetPostFrame(PyObject *postframe) { struct MPyEmbed_ThreadData *td; PyObject *old_postframe; PyThread_acquire_lock(threaddatalock, WAIT_LOCK); td = get_current_data(); if (!td) { PyThread_release_lock(threaddatalock); return 0; } old_postframe = td->postframe; td->postframe = postframe; Py_XINCREF(postframe); PyThread_release_lock(threaddatalock); Py_XDECREF(old_postframe); return 1; }
/* Forget the associations for key across *all* threads. */ void PyThread_delete_key(int key) { struct key *p, **q; PyThread_acquire_lock(keymutex, 1); q = &keyhead; while ((p = *q) != NULL) { if (p->key == key) { *q = p->next; free((void *)p); /* NB This does *not* free p->value! */ } else q = &p->next; } PyThread_release_lock(keymutex); }