Ejemplo n.º 1
0
static int
CPyStreamWrapper_Close_Threaded (CPyStreamWrapper *wrapper)
{
    PyObject *result;
    int retval = 1;
#ifdef WITH_THREAD
    PyThreadState* oldstate;
#endif

    if (!wrapper->close)
        return -1;

#ifdef WITH_THREAD
    PyEval_AcquireThread (wrapper->thread);
    oldstate = PyThreadState_Swap (wrapper->thread);
#endif

    if (wrapper->close)
    {
        result = PyObject_CallFunction (wrapper->close, NULL);
        if (!result)
        {
            PyErr_Print ();
            retval = 0;
        }
        Py_XDECREF (result);
    }

#ifdef WITH_THREAD
    PyThreadState_Swap (oldstate);
    PyEval_ReleaseThread (wrapper->thread);
#endif
    return retval;
}
Ejemplo n.º 2
0
static pgint32
CPyStreamWrapper_Tell_Threaded (CPyStreamWrapper *wrapper)
{
    PyObject* result;
    pgint32 retval = 0;
#ifdef WITH_THREAD
    PyThreadState* oldstate;
#endif

    if (!wrapper->tell)
        return -1;

#ifdef WITH_THREAD
    PyEval_AcquireThread (wrapper->thread);
    oldstate = PyThreadState_Swap (wrapper->thread);
#endif

    result = PyObject_CallFunction (wrapper->tell, NULL);
    if (!result)
    {
        PyErr_Print ();
        retval = -1;
        goto end;
    }
    retval = PyInt_AsLong (result);
    Py_DECREF (result);

end:
#ifdef WITH_THREAD
    PyThreadState_Swap (oldstate);
    PyEval_ReleaseThread (wrapper->thread);
#endif
    return retval;
}
// Asuming we have a valid thread state, release the Python lock.
void PyXPCOM_InterpreterLock_Release()
{
	ThreadData *pData = (ThreadData *)PR_GetThreadPrivate(tlsIndex);
	NS_ABORT_IF_FALSE(pData, "Have no thread data for this thread!");
	PyThreadState *thisThreadState = pData->ts;
	PyEval_ReleaseThread(thisThreadState);
}
Ejemplo n.º 4
0
/*
 * Create a new instance of the plugin i.e. allocate our private storage
 */
static bRC newPlugin(bpContext *ctx)
{
   struct plugin_ctx *p_ctx;

   p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx));
   if (!p_ctx) {
      return bRC_Error;
   }
   memset(p_ctx, 0, sizeof(struct plugin_ctx));
   ctx->pContext = (void *)p_ctx;        /* set our context pointer */

   /*
    * For each plugin instance we instantiate a new Python interpreter.
    */
   PyEval_AcquireLock();
   p_ctx->interpreter = Py_NewInterpreter();
   PyEval_ReleaseThread(p_ctx->interpreter);

   /*
    * Always register some events the python plugin itself can register
    * any other events it is interested in.
    */
   bfuncs->registerBareosEvents(ctx,
                                1,
                                bsdEventNewPluginOptions);

   return bRC_OK;
}
Ejemplo n.º 5
0
static int
CPyStreamWrapper_Seek_Threaded (CPyStreamWrapper *wrapper, pgint32 offset,
    int whence)
{
    PyObject* result;
    int retval = 1;
#ifdef WITH_THREAD
    PyThreadState* oldstate;
#endif

    if (!wrapper->seek)
        return 0;

#ifdef WITH_THREAD
    PyEval_AcquireThread (wrapper->thread);
    oldstate = PyThreadState_Swap (wrapper->thread);
#endif

    result = PyObject_CallFunction (wrapper->seek, "li", offset, whence);
    if (!result)
    {
        PyErr_Print();
        retval = 0;
        goto end;
    }
    Py_DECREF (result);
end:
#ifdef WITH_THREAD
    PyThreadState_Swap (oldstate);
    PyEval_ReleaseThread (wrapper->thread);
#endif
    return retval;
}
Ejemplo n.º 6
0
static int test_repeated_init_and_subinterpreters(void)
{
    PyThreadState *mainstate, *substate;
    PyGILState_STATE gilstate;
    int i, j;

    for (i=0; i<15; i++) {
        printf("--- Pass %d ---\n", i);
        _testembed_Py_Initialize();
        mainstate = PyThreadState_Get();

        PyEval_InitThreads();
        PyEval_ReleaseThread(mainstate);

        gilstate = PyGILState_Ensure();
        print_subinterp();
        PyThreadState_Swap(NULL);

        for (j=0; j<3; j++) {
            substate = Py_NewInterpreter();
            print_subinterp();
            Py_EndInterpreter(substate);
        }

        PyThreadState_Swap(mainstate);
        print_subinterp();
        PyGILState_Release(gilstate);

        PyEval_RestoreThread(mainstate);
        Py_Finalize();
    }
    return 0;
}
Ejemplo n.º 7
0
/**
 * z_policy_thread_release:
 * @self:  this
 *
 * Releases reference information acquired by z_policy_thread_acquire.
 */
void
z_policy_thread_release(ZPolicyThread *self)
{
  self->used = FALSE;
  PyEval_ReleaseThread(self->thread);
  g_static_private_set(&policy_thread, NULL, NULL);
}
Ejemplo n.º 8
0
Archivo: embed.c Proyecto: Plombo/dega
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 clRunPython(char* cmd)
{ 
  #define BSZ 2048
  char pyBuf[BSZ];
  //clprintf (CL_LOG_SEV_INFO, "clRunPython called with [%s]", cmd);
  //clOsalMutexLock(&pyMutex);
  PyThreadState *tstate;
  //PyObject *result;
  
  snprintf(pyBuf,BSZ,"safplus.Callback(\"\"\"%s\"\"\")\n",cmd);
  //clprintf (CL_LOG_SEV_INFO, "clRunPython requesting python lock [%s]", cmd);

  /* interp is your reference to an interpreter object. */
  tstate = PyThreadState_New(thePythonInterpreter);
  PyEval_AcquireThread(tstate);
  
  //clprintf(CL_LOG_SEV_INFO,"Stage 1.  Passing to Python layer: %s",pyBuf);
  int ret = PyRun_SimpleString(pyBuf);
  //clprintf (CL_LOG_SEV_INFO, "clRunPython requesting release of python lock [%s]", cmd);

  PyEval_ReleaseThread(tstate);

  if (ret != 0)  clprintf(CL_LOG_SEV_ERROR,"Ran: %s.  There was an error.",pyBuf);

  PyThreadState_Delete(tstate);
}
Ejemplo n.º 10
0
static int
_pyobj_write_threaded (SDL_RWops *ops, const void* ptr, int size, int num)
{
    _RWWrapper *wrapper = (_RWWrapper *) ops->hidden.unknown.data1;
    PyObject *result;
    int retval;
    PyThreadState* oldstate;

    if (!wrapper->write)
        return -1;

    PyEval_AcquireThread (wrapper->thread);
    oldstate = PyThreadState_Swap (wrapper->thread);

#ifdef IS_PYTHON_3
    result = PyObject_CallFunction (wrapper->write, "y#", ptr, size * num);
#else
    result = PyObject_CallFunction (wrapper->write, "s#", ptr, size * num);
#endif
    if (!result)
    {
        PyErr_Print ();
        retval = -1;
        goto end;
    }
    Py_DECREF (result);
    retval = num;
    
end:
    PyThreadState_Swap (oldstate);
    PyEval_ReleaseThread (wrapper->thread);
    return retval;
}
Ejemplo n.º 11
0
/*
 * Based on
 *
 *   https://wiki.blender.org/index.php/Dev:2.4/Source/Python/API/Threads
 */
void __declspec(dllexport) python_init(void)
{
    PyThreadState * py_tstate = NULL;
    Py_Initialize();
    PyEval_InitThreads();
    py_tstate = PyGILState_GetThisThreadState();
    PyEval_ReleaseThread(py_tstate);
}
Ejemplo n.º 12
0
STDAPI DllUnregisterServer()
{
	int rc, pyrc;
	if (gPythoncom == 0)
		startUp();
	PyEval_AcquireThread(thisthread);
	rc = callSimpleEntryPoint("DllUnregisterServer", &pyrc);
	PyEval_ReleaseThread(thisthread);
	return rc==0 ? pyrc : SELFREG_E_CLASS;
}
Ejemplo n.º 13
0
Archivo: embed.c Proyecto: Plombo/dega
int MPyEmbed_Repl(void) {
	if (!PythonAvailable) {
		return 0;
	}
	mainstate->thread_id = PyThread_get_thread_ident();
	PyEval_AcquireThread(mainstate);
	PyRun_AnyFile(stdin, "<stdin>");
	PyEval_ReleaseThread(mainstate);
	return 1;
}
Ejemplo n.º 14
0
static bRC handlePluginEvent(bpContext *ctx, bDirEvent *event, void *value)
{
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
   bRC retval = bRC_Error;

   PyEval_AcquireThread(p_ctx->interpreter);
   retval = PyHandlePluginEvent(ctx, event, value);
   PyEval_ReleaseThread(p_ctx->interpreter);

   return retval;
}
Ejemplo n.º 15
0
static bRC setPluginValue(bpContext *ctx, pDirVariable var, void *value)
{
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
   bRC retval = bRC_Error;

   PyEval_AcquireThread(p_ctx->interpreter);
   retval = PySetPluginValue(ctx, var, value);
   PyEval_ReleaseThread(p_ctx->interpreter);

   return retval;
}
Ejemplo n.º 16
0
void IvrPython::onDTMFEvent(int detectedKey) {
  dtmfKey.set(detectedKey); // wake up waiting functions...

   if (onDTMFCallback == NULL) {
    DBG("IvrPython::onDTMFEvent, but script did not set onDTMF callback!\n");
    return;
  }
  DBG("IvrPython::onDTMFEvent(): calling onDTMFCallback key is %d...\n", detectedKey);

#ifndef IVR_PERL
  PyThreadState *tstate;

  /* interp is your reference to an interpreter object. */
  tstate = PyThreadState_New(mainInterpreterThreadState->interp);
  PyEval_AcquireThread(tstate);

  /* Perform Python actions here.  */
  PyObject *arglist = Py_BuildValue("(i)", detectedKey);
  PyObject *result = PyEval_CallObject(onDTMFCallback, arglist);
  Py_DECREF(arglist);

  if (result == NULL) {
      DBG("Calling IVR" SCRIPT_TYPE "onDTMF failed.\n");
       PyErr_Print();
      //return ;
  } else {
      Py_DECREF(result);
  }

  /* Release the thread. No Python API allowed beyond this point. */
  PyEval_ReleaseThread(tstate);

  /* You can either delete the thread state, or save it
     until you need it the next time. */
  PyThreadState_Delete(tstate);
#else   //IVR_PERL
  DBG("IvrPython::onDTMFEvent(): calling onDTMFCallback func is %s...\n", onDTMFCallback);

	PERL_SET_CONTEXT(my_perl_interp);
	DBG("context is %ld\n", (long) Perl_get_context());
	dSP ;
	ENTER ;
	SAVETMPS ;
	PUSHMARK(SP) ;
	XPUSHs(sv_2mortal(newSViv(detectedKey)));
	PUTBACK ;
	call_pv(onDTMFCallback, G_DISCARD);
	FREETMPS ;
	LEAVE ;
#endif	//IVR_PERL
  DBG("IvrPython::onDTMFEvent done...\n");
}
Ejemplo n.º 17
0
void start_python()
{
    PyThreadState *py_tstate = NULL;
    //...
    Py_Initialize();
    PyEval_InitThreads();
    //...
    py_tstate = PyGILState_GetThisThreadState();
    PyEval_ReleaseThread(py_tstate);
    //gil_lock glock;
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("print sys.path");
}
Ejemplo n.º 18
0
/* When using Incremental dump, all previous dumps are necessary */
static bRC checkFile(bpContext *ctx, char *fname)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Ejemplo n.º 19
0
/*
 * Do actual I/O.  Bareos calls this after startBackupFile
 *   or after startRestoreFile to do the actual file
 *   input or output.
 */
static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Ejemplo n.º 20
0
/*
 * Called when starting to backup a file.  Here the plugin must
 *  return the "stat" packet for the directory/file and provide
 *  certain information so that Bareos knows what the file is.
 *  The plugin can create "Virtual" files by giving them a
 *  name that is not normally found on the file system.
 */
static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Ejemplo n.º 21
0
/*
 * Called after the file has been restored. This can be used to
 *  set directory permissions, ...
 */
static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Ejemplo n.º 22
0
/*
 * Handle an event that was generated in Bareos
 */
static bRC handlePluginEvent(bpContext *ctx, bsdEvent *event, void *value)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Ejemplo n.º 23
0
/*
 * Set a plugin value (none defined)
 */
static bRC setPluginValue(bpContext *ctx, psdVariable var, void *value)
{
#ifdef HAVE_PYTHON
   struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

   PyEval_AcquireThread(p_ctx->interpreter);

   /*
    * Do python calls.
    */

   PyEval_ReleaseThread(p_ctx->interpreter);
#endif

   return bRC_OK;
}
Ejemplo n.º 24
0
Archivo: embed.c Proyecto: Plombo/dega
int MPyEmbed_Run(char *file) {
	FILE *fp;
	if (!PythonAvailable) {
		return 0;
	}
	fp = fopen(file, "r");
	if (!fp) {
		perror("fopen");
		return 0;
	}
	mainstate->thread_id = PyThread_get_thread_ident();
	PyEval_AcquireThread(mainstate);
	PyRun_SimpleFile(fp, file);
	PyEval_ReleaseThread(mainstate);
	fclose(fp);
	return 1;
}
Ejemplo n.º 25
0
/*
 * Class:     jep_InvocationHandler
 * Method:    invoke
 * Signature: (Ljava/lang/String;JJ[Ljava/lang/Object;[Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;
 */
JNIEXPORT jobject JNICALL Java_jep_InvocationHandler_invoke
(JNIEnv *env,
 jclass clazz,
 jstring jname,
 jlong _jepThread,
 jlong _target,
 jobjectArray args,
 jintArray types,
 jint returnType)
{

    JepThread     *jepThread;
    jobject        ret;
    const char    *cname;
    PyObject      *target;
    PyObject      *callable;

    target   = (PyObject *) (intptr_t) _target;
    ret      = NULL;
    callable = NULL;

    jepThread = (JepThread *) (intptr_t) _jepThread;
    if (!jepThread) {
        THROW_JEP(env, "Couldn't get thread objects.");
        return NULL;
    }

    PyEval_AcquireThread(jepThread->tstate);

    // now get the callable object
    cname = jstring2char(env, jname);
    // python docs say this returns a new ref. they lie like dogs.
    callable = PyObject_GetAttrString(target, (char *) cname);
    release_utf_char(env, jname, cname);

    if (process_py_exception(env, 0) || !callable) {
        goto EXIT;
    }

    ret = pyembed_invoke(env, callable, args, types);

EXIT:
    PyEval_ReleaseThread(jepThread->tstate);

    return ret;
}
Ejemplo n.º 26
0
void IvrPython::onMediaQueueEmpty() {
  isMediaQueueEmpty.set(true);

    DBG("executiong MQE callback...\n");
    if (onMediaQueueEmptyCallback == NULL) {
	DBG("IvrPython::onMediaQueueEmpty, but script did not set onMediaQueueEmpty callback.\n");
	return;
    }

#ifndef IVR_PERL
    PyThreadState *tstate;

    /* interp is your reference to an interpreter object. */
    tstate = PyThreadState_New(mainInterpreterThreadState->interp);
    PyEval_AcquireThread(tstate);

    /* Perform Python actions here.  */
    PyObject *arglist = Py_BuildValue("()");
    PyObject *result = PyEval_CallObject(onMediaQueueEmptyCallback, arglist);
    Py_DECREF(arglist);

    if (result == NULL) {
	DBG("Calling IVR" SCRIPT_TYPE "onMediaQueueEmpty failed.\n");
	    // PyErr_Print();
	//return ;
    } else {
	Py_DECREF(result);
    }

    /* Release the thread. No Python API allowed beyond this point. */
    PyEval_ReleaseThread(tstate);

    /* You can either delete the thread state, or save it
       until you need it the next time. */
    PyThreadState_Delete(tstate);
#else   //IVR_PERL
	PERL_SET_CONTEXT(my_perl_interp);
	DBG("context is %ld\n", (long) Perl_get_context());

	dSP ;
	PUSHMARK(SP) ;
	call_pv(onMediaQueueEmptyCallback, G_DISCARD|G_NOARGS) ;
#endif	//IVR_PERL
    DBG("IvrPython::onMediaQueueEmpty done.\n");
}
Ejemplo n.º 27
0
Archivo: pypointer.c Proyecto: mrj0/jep
/*
 * Class:     jep_python_PyPointer
 * Method:    decref
 * Signature: (JJ)V
 */
JNIEXPORT void JNICALL Java_jep_python_PyPointer_decref
(JNIEnv *env, jobject obj, jlong tstate, jlong pyobj)
{
    JepThread  *jepThread;
    PyObject   *pyObject;

    jepThread = (JepThread *) tstate;
    if (!jepThread) {
        THROW_JEP(env, "Couldn't get thread objects.");
        return;
    }

    pyObject = (PyObject*) pyobj;

    PyEval_AcquireThread(jepThread->tstate);
    Py_DECREF(pyObject);
    PyEval_ReleaseThread(jepThread->tstate);
}
Ejemplo n.º 28
0
static int
CPyStreamWrapper_Write_Threaded (CPyStreamWrapper *wrapper, const void *buf,
    pguint32 num, pguint32 size, pguint32 *written)
{
    PyObject *result;
    int retval = 1;
    const char *ptr = buf;

#ifdef WITH_THREAD
    PyThreadState* oldstate;
#endif

    if (!wrapper->write || !buf || !written)
        return 0;

#ifdef WITH_THREAD
    PyEval_AcquireThread (wrapper->thread);
    oldstate = PyThreadState_Swap (wrapper->thread);
#endif

#ifdef IS_PYTHON_3
    result = PyObject_CallFunction (wrapper->write, "y#", ptr, size * num);
#else
    result = PyObject_CallFunction (wrapper->write, "s#", ptr, size * num);
#endif

    if (!result)
    {
        PyErr_Print ();
        retval = 0;
        goto end;
    }
    Py_DECREF (result);
    *written = num;
    
end:
#ifdef WITH_THREAD
    PyThreadState_Swap (oldstate);
    PyEval_ReleaseThread (wrapper->thread);
#endif
    return retval;
}
static void clRunPython(char* cmd)
{ 
  #define BSZ 2048
  char pyBuf[BSZ];
  clprintf (CL_LOG_SEV_INFO, "clRunPython called with [%s]", cmd);
  //clOsalMutexLock(&pyMutex);
  thrdState = PyThreadState_Get();
  
  #if 0
  if (quit)
    {
      clprintf(CL_LOG_SEV_INFO,"Python has quit, so not running: %s",cmd);
      //clOsalMutexUnlock(&pyMutex);
      return;
    }
  #endif

#if 0
  PyEval_AcquireLock();
  PyThreadState_Swap(thrdState);
#endif

  snprintf(pyBuf,BSZ,"clCmdFromAsp(\"\"\"%s\"\"\")\n",cmd);

  clprintf (CL_LOG_SEV_INFO, "clRunPython requesting python lock [%s]", cmd);
  PyEval_AcquireThread(thrdState);
  clprintf(CL_LOG_SEV_INFO,"Stage 1.  Passing to Python layer: %s",pyBuf);
  int ret = PyRun_SimpleString(pyBuf);
  clprintf (CL_LOG_SEV_INFO, "clRunPython requesting release of python lock [%s]", cmd);
  PyEval_ReleaseThread(thrdState);

  if (ret != 0)  clprintf(CL_LOG_SEV_ERROR,"Ran: %s.  There was an error.",pyBuf);

#if 0
  PyThreadState_Swap(NULL);
  PyEval_ReleaseLock();
#endif

  //clprintf (CL_LOG_SEV_INFO, "clRunPython unlocking mutex [%s]", cmd);
  //clOsalMutexUnlock(&pyMutex);
  //clprintf (CL_LOG_SEV_INFO, "clRunPython unlocked mutex [%s]", cmd);
}
Ejemplo n.º 30
0
static int
_pyobj_seek_threaded (SDL_RWops *ops, int offset, int whence)
{
    _RWWrapper *wrapper = (_RWWrapper *) ops->hidden.unknown.data1;
    PyObject* result;
    int retval;
    PyThreadState* oldstate;

    if (!wrapper->seek || !wrapper->tell)
        return -1;

    PyEval_AcquireThread (wrapper->thread);
    oldstate = PyThreadState_Swap (wrapper->thread);

    if (!(offset == 0 && whence == SEEK_CUR)) /*being called only for 'tell'*/
    {
        result = PyObject_CallFunction (wrapper->seek, "ii", offset, whence);
        if (!result)
        {
            PyErr_Print();
            retval = -1;
            goto end;
        }
        Py_DECREF (result);
    }

    result = PyObject_CallFunction (wrapper->tell, NULL);
    if (!result)
    {
        PyErr_Print ();
        retval = -1;
        goto end;
    }
    retval = PyInt_AsLong (result);
    Py_DECREF (result);

end:
    PyThreadState_Swap (oldstate);
    PyEval_ReleaseThread (wrapper->thread);
    return retval;
}