Exemple #1
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;
}
Exemple #2
0
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);
}
Exemple #3
0
static void
faulthandler_thread(void *unused)
{
    PyLockStatus st;
    const char* errmsg;
    PyThreadState *current;
    int ok;

    do {
        st = PyThread_acquire_lock_timed(thread.cancel_event,
                                         thread.timeout_ms, 0);
        if (st == PY_LOCK_ACQUIRED) {
            /* Cancelled by user */
            break;
        }
        /* Timeout => dump traceback */
        assert(st == PY_LOCK_FAILURE);

        /* get the thread holding the GIL, NULL if no thread hold the GIL */
        current = _Py_atomic_load_relaxed(&_PyThreadState_Current);

        errmsg = _Py_DumpTracebackThreads(thread.fd, thread.interp, current);
        ok = (errmsg == NULL);

        if (thread.exit)
            _exit(1);
    } while (ok && thread.repeat);

    /* The only way out */
    PyThread_release_lock(thread.cancel_event);
    PyThread_release_lock(thread.join_event);
}
Exemple #4
0
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);
  }
}
Exemple #5
0
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);
}
Exemple #6
0
    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 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;
}
Exemple #8
0
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]);
	}
}
Exemple #9
0
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 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;
}
Exemple #11
0
static void
destruct_lock(PyThread_type_lock lock)
{
	PyThread_acquire_lock(lock, 0);
	PyThread_release_lock(lock);
	PyThread_free_lock(lock);
}
/*
 * 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
}
Exemple #13
0
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;
}
Exemple #14
0
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);
}
Exemple #16
0
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);
}
Exemple #17
0
    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;
    }
Exemple #18
0
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;
    }
}
Exemple #19
0
static inline void CReader_Buffer_release(CReader_Buffer *buffer)
 {
# ifdef DEBUG_THREAD
   fprintf(stderr, "Buffer lock %p ]", buffer); fflush(stderr);
# endif
  assert(!buffer->state);
  PyThread_release_lock(buffer->lock);
# ifdef DEBUG_THREAD
   fprintf(stderr, " --> %i\n", --buffer->bufferlockings); fflush(stderr);
# endif
 }
Exemple #20
0
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);
}
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);
}
Exemple #22
0
    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);
        }
    }
Exemple #23
0
static void
faulthandler_thread(void *unused)
{
    PyLockStatus st;
    const char* errmsg;
    PyThreadState *current;
    int ok;
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
    sigset_t set;

    /* we don't want to receive any signal */
    sigfillset(&set);
    pthread_sigmask(SIG_SETMASK, &set, NULL);
#endif

    do {
        st = PyThread_acquire_lock_timed(thread.cancel_event,
                                         thread.timeout_us, 0);
        if (st == PY_LOCK_ACQUIRED) {
            PyThread_release_lock(thread.cancel_event);
            break;
        }
        /* Timeout => dump traceback */
        assert(st == PY_LOCK_FAILURE);

        /* get the thread holding the GIL, NULL if no thread hold the GIL */
        current = _Py_atomic_load_relaxed(&_PyThreadState_Current);

        write(thread.fd, thread.header, thread.header_len);

        errmsg = _Py_DumpTracebackThreads(thread.fd, thread.interp, current);
        ok = (errmsg == NULL);

        if (thread.exit)
            _exit(1);
    } while (ok && thread.repeat);

    /* The only way out */
    PyThread_release_lock(thread.running);
}
Exemple #24
0
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;
}
Exemple #25
0
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);
}
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 *
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;
}
Exemple #29
0
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);
}
Exemple #30
0
static void bpo20891_thread(void *lockp)
{
    PyThread_type_lock lock = *((PyThread_type_lock*)lockp);

    PyGILState_STATE state = PyGILState_Ensure();
    if (!PyGILState_Check()) {
        fprintf(stderr, "PyGILState_Check failed!");
        abort();
    }

    PyGILState_Release(state);

    PyThread_release_lock(lock);

    PyThread_exit_thread();
}