void python_taskqueue::run( GLFWwindow *Context, rendertask_sequence &Tasks, threading::condition_variable &Condition, std::atomic<bool> &Exit ) { glfwMakeContextCurrent( Context ); // create a state object for this thread PyEval_AcquireLock(); auto *threadstate { PyThreadState_New( m_mainthread->interp ) }; PyEval_ReleaseLock(); render_task *task { nullptr }; while( false == Exit.load() ) { // regardless of the reason we woke up prime the spurious wakeup flag for the next time Condition.spurious( true ); // keep working as long as there's any scheduled tasks do { task = nullptr; // acquire a lock on the task queue and potentially grab a task from it { std::lock_guard<std::mutex> lock( Tasks.mutex ); if( false == Tasks.data.empty() ) { // fifo task = Tasks.data.front(); Tasks.data.pop_front(); } } if( task != nullptr ) { // swap in my thread state PyEval_RestoreThread( threadstate ); { // execute python code task->run(); error(); } // clear the thread state PyEval_SaveThread(); } // TBD, TODO: add some idle time between tasks in case we're on a single thread cpu? } while( task != nullptr ); // if there's nothing left to do wait until there is // but check every now and then on your own to minimize potential deadlock situations Condition.wait_for( std::chrono::seconds( 5 ) ); } // clean up thread state data PyEval_AcquireLock(); PyThreadState_Swap( nullptr ); PyThreadState_Clear( threadstate ); PyThreadState_Delete( threadstate ); PyEval_ReleaseLock(); }
static void TargetCleanup(void *self, void *data) { /* Decrement _PLUGIN_COUNT, protected by _PLUGIN_INIT_MUTEX * call Py_Finalize when _PLUGIN_COUNT drops to zero */ if (pthread_mutex_lock(&_PLUGIN_INIT_MUTEX)) { perror("Can't lock _PLUGIN_INIT_MUTEX"); abort(); } if (--_PLUGIN_COUNT > 0) { pthread_mutex_unlock(&_PLUGIN_INIT_MUTEX); return; } TARGET_THREAD_BEGIN_BLOCK; Py_DecRef(_TARGET_MODULE); TARGET_THREAD_END_BLOCK; PyEval_AcquireLock(); PyThreadState_Swap(pluginMainPyThreadState); if (_TARGET_INIT) // if Python is initialized and _PLUGIN_COUNT == 0, call Py_Finalize { debug("Calling Py_Finalize()"); Py_Finalize(); _TARGET_INIT=0; // false } pthread_mutex_unlock(&_PLUGIN_INIT_MUTEX); }
bool KviPythonInterpreter::execute( const QString &szCode, QStringList &lArgs, //args QString &szRetVal, QString &szError, QStringList &) //lWarnings { if(!m_pThreadState) { szError = __tr2qs_ctx("Internal error: python interpreter not initialized","python"); return false; } int retVal; g_lError.clear(); // grab the global interpreter lock PyEval_AcquireLock(); // swap in my thread state PyThreadState_Swap(m_pThreadState); QString szVarCode = "aArgs = ["; bool bFirst = true; foreach(QString szArg,lArgs) { if(!bFirst) szVarCode += ","; else bFirst = false; szVarCode += QString::fromLatin1("\"%1\"").arg(szArg); } szVarCode += "]"; PyRun_SimpleString(szVarCode.toUtf8().data()); //clean "cr" from the python code (ticket #1028) QString szCleanCode = szCode; szCleanCode.replace(QRegExp("\r\n?"), "\n"); // execute some python code retVal = PyRun_SimpleString(szCleanCode.toUtf8().data()); szRetVal.setNum(retVal); if (PyErr_Occurred() || retVal) { szError = g_lError; } // clear the thread state PyThreadState_Swap(NULL); // release our hold on the global interpreter PyEval_ReleaseLock(); if(retVal) return false; return true; }
void XBPython::Finalize() { if (m_bInitialized) { CLog::Log(LOGINFO, "Python, unloading python shared library because no scripts are running anymore"); PyEval_AcquireLock(); PyThreadState_Swap((PyThreadState*)m_mainThreadState); Py_Finalize(); PyEval_ReleaseLock(); #if !(defined(__APPLE__) || defined(_WIN32)) UnloadExtensionLibs(); #endif // first free all dlls loaded by python, after that python24.dll (this is done by UnloadPythonDlls #if !(defined(__APPLE__) || defined(_WIN32)) DllLoaderContainer::UnloadPythonDlls(); #endif #if defined(_LINUX) && !defined(__APPLE__) // we can't release it on windows, as this is done in UnloadPythonDlls() for win32 (see above). // The implementation for linux needs looking at - UnloadPythonDlls() currently only searches for "python24.dll" // The implementation for osx can never unload the python dylib. DllLoaderContainer::ReleaseModule(m_pDll); #endif m_hModule = NULL; m_mainThreadState = NULL; m_bInitialized = false; } }
void XBPyThread::stop() { CSingleLock lock(m_pExecuter->m_critSection); if(m_stopping) return; m_stopping = true; if (m_threadState) { PyEval_AcquireLock(); PyThreadState* old = PyThreadState_Swap((PyThreadState*)m_threadState); PyObject *m; m = PyImport_AddModule((char*)"xbmc"); if(!m || PyObject_SetAttrString(m, (char*)"abortRequested", PyBool_FromLong(1))) CLog::Log(LOGERROR, "XBPyThread::stop - failed to set abortRequested"); for(PyThreadState* state = ((PyThreadState*)m_threadState)->interp->tstate_head; state; state = state->next) { Py_XDECREF(state->async_exc); state->async_exc = PyExc_SystemExit; Py_XINCREF(state->async_exc); } PyThreadState_Swap(old); PyEval_ReleaseLock(); } }
bool KviPythonInterpreter::init() { // get the global lock PyEval_AcquireLock(); // get a reference to the PyInterpreterState PyInterpreterState * mainInterpreterState = mainThreadState->interp; // create a thread state object for this thread m_pThreadState = PyThreadState_New(mainInterpreterState); // swap in the current thread state PyThreadState_Swap(m_pThreadState); // and hook in the kvirc error handling routines QString szPreCode = QString( \ "import kvirc\n" \ "import sys\n" \ "class kvirc_stderr_grabber:\n" \ "\tdef write(self,s):\n" \ "\t\tkvirc.error(s)\n" \ "sys.stderr=kvirc_stderr_grabber()\n" ); // evaluate that PyRun_SimpleString(szPreCode.toUtf8().data()); // swap out our thread state for now PyThreadState_Swap(NULL); // free the lock PyEval_ReleaseLock(); return true; }
/** * Should be called when a script is finished */ void XBPython::Finalize() { // for linux - we never release the library. its loaded and stays in memory. EnterCriticalSection(&m_critSection); m_iDllScriptCounter--; if (m_iDllScriptCounter == 0 && m_bInitialized) { CLog::Log(LOGINFO, "Python, unloading python24.dll cause no scripts are running anymore"); PyEval_AcquireLock(); PyThreadState_Swap(mainThreadState); Py_Finalize(); UnloadExtensionLibs(); //g_sectionLoader.UnloadDLL(PYTHON_DLL); // first free all dlls loaded by python, after that python24.dll (this is done by UnloadPythonDlls //dllFreeLibrary(m_hModule); DllLoaderContainer::UnloadPythonDlls(); #ifdef _LINUX DllLoaderContainer::ReleaseModule(m_pDll); #endif m_hModule = NULL; mainThreadState = NULL; m_bInitialized = false; } LeaveCriticalSection(&m_critSection); }
void init() { if (etiss::cfg().get<bool>("pyinitialize",true)) { Py_Initialize(); if (etiss::cfg().get<bool>("pyinittheads",true)) { if (PyEval_ThreadsInitialized()==0) { PyEval_InitThreads(); // init gil PyEval_ReleaseLock(); // release gil etiss::log(etiss::VERBOSE,"PyEval_InitThreads() called."); } } etiss::log(etiss::VERBOSE,"Py_Initialize() called."); } run([]() { PyEval_AcquireLock(); // lock gil if (etiss::verbosity() >= etiss::INFO) PyRun_SimpleString("print('ETISS: INFO: ETISS has been build with python support.')\n"); //Py_InitModule3("etiss", ETISSMethods,"ETISS python bindings"); PyEval_ReleaseLock(); // release gil }); }
static int rw_write_th(SDL_RWops* context, const void* ptr, int size, int num) { RWHelper* helper = (RWHelper*)context->hidden.unknown.data1; PyObject* result; PyThreadState* oldstate; if(!helper->write) return -1; PyEval_AcquireLock(); oldstate = PyThreadState_Swap(helper->thread); result = PyObject_CallFunction(helper->write, "s#", ptr, size * num); if(!result) { PyThreadState_Swap(oldstate); PyEval_ReleaseLock(); return -1; } Py_DECREF(result); PyThreadState_Swap(oldstate); PyEval_ReleaseLock(); return num; }
PythonModule* PythonModule_Init( PythonEnv* Environment, char* ModuleName ) { PyObject* pName; PythonModule* Self = (PythonModule*) malloc( sizeof(PythonModule) ); Self->Module=NULL; Self->List = NULL; Self->ListLength = 0; Self->ModuleName = (char*) malloc( sizeof(char) * strlen(ModuleName)+1 ); strcpy( Self->ModuleName, ModuleName ); Self->Environment = Environment; PyEval_AcquireLock(); Self->CurrentThreadState = PyThreadState_New( Self->Environment->MainThreadState->interp ); PyThreadState_Swap( Self->CurrentThreadState ); pName = PyString_FromString(Self->ModuleName); Self->Module = PyImport_Import(pName); if( Self->Module == NULL ) { PyErr_Print(); PythonModule_LeaveThread( Self ); PythonModule_Destroy( Self ); return NULL; } Py_INCREF(Self->Module); Py_DECREF(pName); PythonModule_LeaveThread( Self ); return Self; }
static int rw_close_th(SDL_RWops* context) { RWHelper* helper = (RWHelper*)context->hidden.unknown.data1; PyObject* result; int retval = 0; PyThreadState* oldstate; PyEval_AcquireLock(); oldstate = PyThreadState_Swap(helper->thread); if(helper->close) { result = PyObject_CallFunction(helper->close, NULL); if(result) retval = -1; Py_XDECREF(result); } PyThreadState_Swap(oldstate); PyThreadState_Clear(helper->thread); PyThreadState_Delete(helper->thread); Py_XDECREF(helper->seek); Py_XDECREF(helper->tell); Py_XDECREF(helper->write); Py_XDECREF(helper->read); Py_XDECREF(helper->close); PyMem_Del(helper); PyEval_ReleaseLock(); SDL_FreeRW(context); return retval; }
/* * 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; }
int PythonEnv_Destroy( PythonEnv* Self ) { PyEval_AcquireLock(); PyThreadState_Swap(Self->MainThreadState); Py_Finalize(); free( Self ); return 0; }
void ModuleLoader::finalize() { PyEval_AcquireLock(); PyThreadState_Swap( g_pymaintstate ); Py_Finalize(); }
/** * Execute the current script * We are now in the thread. */ void PyApi::ExecuteInThread() const { assert(Py_IsInitialized() ); // get the lock so we can change things. PyEval_AcquireLock(); // make sure that the main thread is the active one. const auto mainInterpreterState = _mainThreadState->interp; PyThreadState_Swap(_mainThreadState); // create a new thread. const auto myThreadState = PyThreadState_New(mainInterpreterState); // make sure that the new thread has control // https://docs.python.org/3/c-api/init.html PyThreadState_Swap(myThreadState); // execute it... { const auto main_module = PyImport_AddModule("__main__"); const auto main_dict = PyModule_GetDict(main_module); Py_XINCREF(main_module); const auto local_dic = PyDict_New(); Py_XINCREF(local_dic); // we can now run our script const auto s = _script.c_str(); const auto pyRes = PyRun_String(s, Py_file_input, main_dict, local_dic); CheckForPythonErrors(); PyDict_Clear(local_dic); Py_XDECREF(local_dic); // pending calls must be cleared out Py_XDECREF(main_module); } // swap back to this thread. PyThreadState_Swap(myThreadState); // clear anything left behind. PyThreadState_Clear(myThreadState); PyThreadState_Swap(nullptr); // delete my thread. PyThreadState_Delete(myThreadState); // give control back to main thread PyThreadState_Swap(_mainThreadState); // release the lock one last time. PyEval_ReleaseLock(); }
void End() { PyEval_AcquireLock(); PyThreadState_Swap( NULL ); PyThreadState_Clear( mainThreadState ); PyThreadState_Delete( mainThreadState ); //PyEval_Restore( mainThreadState ); Py_Finalize(); started = ScriptEngine::HasBegun(); }
void XBPyThread::stop() { CSingleLock lock(m_pExecuter->m_critSection); if(m_stopping) return; m_stopping = true; if (m_threadState) { PyEval_AcquireLock(); PyThreadState* old = PyThreadState_Swap((PyThreadState*)m_threadState); //tell xbmc.Monitor to call onAbortRequested() g_pythonParser.OnAbortRequested(); PyObject *m; m = PyImport_AddModule((char*)"xbmc"); if(!m || PyObject_SetAttrString(m, (char*)"abortRequested", PyBool_FromLong(1))) CLog::Log(LOGERROR, "XBPyThread::stop - failed to set abortRequested"); PyThreadState_Swap(old); PyEval_ReleaseLock(); if(!stoppedEvent.WaitMSec(5000))//let the script 5 secs for shut stuff down { CLog::Log(LOGERROR, "XBPyThread::stop - script didn't stop in proper time - lets kill it"); } //everything which didn't exit by now gets killed PyEval_AcquireLock(); old = PyThreadState_Swap((PyThreadState*)m_threadState); for(PyThreadState* state = ((PyThreadState*)m_threadState)->interp->tstate_head; state; state = state->next) { Py_XDECREF(state->async_exc); state->async_exc = PyExc_SystemExit; Py_XINCREF(state->async_exc); } PyThreadState_Swap(old); PyEval_ReleaseLock(); } }
/** Cleanup any thread local storage on pthread_exit() */ static void do_python_cleanup(void *arg) { PyThreadState *my_thread_state = arg; PyEval_AcquireLock(); PyThreadState_Swap(NULL); /* Not entirely sure this is needed */ PyThreadState_Clear(my_thread_state); PyThreadState_Delete(my_thread_state); PyEval_ReleaseLock(); }
void gil_real_get() { //uwsgi_log("LOCK %d\n", uwsgi.mywid); #if !defined(PYTHREE) && !defined(UWSGI_PYPY) PyEval_AcquireLock(); PyThreadState_Swap((PyThreadState *) pthread_getspecific(up.upt_gil_key)); #else PyEval_RestoreThread((PyThreadState *) pthread_getspecific(up.upt_gil_key)); #endif //uwsgi_log("LOCKED !!! %d\n", uwsgi.mywid); }
static void on_shutdown() { if (!py_module) return; // not initialized // shut down the interpreter nxweb_log_error("shutting down python"); PyEval_AcquireLock(); PyThreadState_Swap(py_main_thread_state); Py_XDECREF(py_nxweb_on_request_func); Py_XDECREF(py_module); Py_Finalize(); nxweb_log_error("python finalized"); }
void XBPyThread::stop() { if (m_threadState) { PyEval_AcquireLock(); m_threadState->c_tracefunc = xbTrace; m_threadState->use_tracing = 1; PyEval_ReleaseLock(); } stopping = true; }
void spyceClose() { if(spyInitialized) { PyEval_AcquireLock(); PyThreadState_Swap(NULL); PyThreadState_Clear(spyThreadState); PyThreadState_Delete(spyThreadState); PyEval_ReleaseLock(); g_pythonParser.Finalize(); } }
PythonThreadState::PythonThreadState() : m_mainThreadState(NULL), m_thisThreadState(NULL) { m_mainThreadState = g_mainThreadState; assert(m_mainThreadState); if(Poco::Thread::current()) { PyEval_AcquireLock(); PyInterpreterState * mainInterpreterState = m_mainThreadState->interp; m_thisThreadState = PyThreadState_New(mainInterpreterState); PyThreadState_Swap(m_thisThreadState); } }
int PythonModule_Destroy( PythonModule* Self ) { if( Self != NULL ) { PyEval_AcquireLock(); PyThreadState_Swap(NULL); PyThreadState_Clear(Self->CurrentThreadState); PyThreadState_Delete(Self->CurrentThreadState); PyEval_ReleaseLock(); } free( Self ); }
void KviPythonInterpreter::done() { if(!m_pThreadState)return; // grab the lock PyEval_AcquireLock(); // swap my thread state out of the interpreter PyThreadState_Swap(NULL); // clear out any cruft from thread state object PyThreadState_Clear(m_pThreadState); // delete my thread state object PyThreadState_Delete(m_pThreadState); // release the lock PyEval_ReleaseLock(); m_pThreadState = 0; }
gpointer pass_to_motor_v_multi_thread(gpointer args){ PyEval_AcquireLock(); PyInterpreterState * mainInterpreterState = mainThreadState->interp; PyThreadState * myThreadState = PyThreadState_New(mainInterpreterState); PyThreadState_Swap(myThreadState); pass_to_motor(args); PyThreadState_Swap(NULL); PyThreadState_Clear(myThreadState); PyThreadState_Delete(myThreadState); PyEval_ReleaseLock(); return NULL; }
int classicalDecisionQProcessor(pktcore_t *pcore, gpacket_t *pkt) { ftentry_t *entry_res = checkFlowTable(pcore->flowtable, pkt); int (*processor)(gpacket_t *); if (entry_res == NULL) { printf("[decisionProcessor]:: Cannot find action to given packet...Drop!\n"); return EXIT_FAILURE; } else if (entry_res->language == C_FUNCTION) { verbose(2, "[decisionProcessor]:: Entry found protocol: %#06x C Function: Action: (0x%lx)\n", entry_res->ip_protocol_type, (unsigned long) entry_res->action); processor = entry_res->action; int nextlabel = (*processor)(pkt); if (nextlabel == NULL_PROTOCOL) return EXIT_SUCCESS; verbose(2, "[decisionProcessor][Ft]New style round"); labelNext(pkt, entry_res->ip_protocol_type, nextlabel); writeQueue(pcore->decisionQ, pkt, sizeof (gpacket_t)); verbose(2, "[decisionProcessor]:: Wrote back to decision Q..."); } else if (entry_res->language == PYTHON_FUNCTION) { verbose(2, "[decisionProcessor]:: Entry found protocol: %#06x Python Function: Action: (0x%lx)\n", entry_res->ip_protocol_type, (unsigned long) entry_res->action); PyObject * PyActionFun, *PyPkt, *PyFunReturn; PyActionFun = entry_res->action; PyPkt = SWIG_NewPointerObj((void *) pkt, SWIGTYPE_p__gpacket_t, 1); if (PyPkt) { /*TODO: handle PyReturn for further process*/ verbose(2, "[decisionProcessor]:: Ready to call Python function"); printf("[classicalDecisionQProcessor] check lock..\n"); PyEval_AcquireLock(); verbose(2, "got the lock\n"); PyFunReturn = PyObject_CallFunction(PyActionFun, "O", PyPkt); printf("[classicalDecisionQProcessor]ready to release lock\n"); PyEval_ReleaseLock(); CheckPythonError(); } } }
/** * Should be called when a script is finished */ void XBPython::Finalize() { CSingleLock lock(m_critSection); m_iDllScriptCounter--; if (m_iDllScriptCounter == 0 && m_bInitialized) { m_bInitialized = false; CLog::Log(LOGINFO, "Python, unloading python27.dll because no scripts are running anymore"); PyEval_AcquireLock(); PyThreadState_Swap(mainThreadState); Py_Finalize(); PyEval_ReleaseLock(); // first free all dlls loaded by python, after that python27.dll (this is done by UnloadPythonDlls DllLoaderContainer::UnloadPythonDlls(); m_hModule = NULL; mainThreadState = NULL; } }
static void handler(FSEventStreamRef stream, FSEventStreamInfo* info, int numEvents, const char *const eventPaths[], const FSEventStreamEventFlags *eventMasks, const uint64_t *eventIDs) { PyEval_AcquireLock(); PyThreadState *_save; _save = PyThreadState_Swap(info->state); /* convert event data to Python objects */ PyObject *eventPathList = PyList_New(numEvents); PyObject *eventMaskList = PyList_New(numEvents); if ((!eventPathList) || (!eventMaskList)) return; int i; for (i = 0; i < numEvents; i++) { PyObject *str = PyString_FromString(eventPaths[i]); PyObject *num = PyInt_FromLong(eventMasks[i]); if ((!num) || (!str)) { Py_DECREF(eventPathList); Py_DECREF(eventMaskList); return; } PyList_SET_ITEM(eventPathList, i, str); PyList_SET_ITEM(eventMaskList, i, num); } if (PyObject_CallFunction(info->callback, "OO", eventPathList, eventMaskList) == NULL) { /* may can return NULL if an exception is raised */ if (!PyErr_Occurred()) PyErr_SetString(PyExc_ValueError, callback_error_msg); /* stop listening */ CFRunLoopStop(info->loop); } PyThreadState_Swap(_save); PyEval_ReleaseLock(); }
static int rw_seek_th(SDL_RWops* context, int offset, int whence) { RWHelper* helper = (RWHelper*)context->hidden.unknown.data1; PyObject* result; int retval; PyThreadState* oldstate; if(!helper->seek || !helper->tell) return -1; PyEval_AcquireLock(); oldstate = PyThreadState_Swap(helper->thread); if(!(offset == 0 && whence == SEEK_CUR)) /*being called only for 'tell'*/ { result = PyObject_CallFunction(helper->seek, "ii", offset, whence); if(!result) { PyErr_Clear(); PyThreadState_Swap(oldstate); PyEval_ReleaseLock(); return -1; } Py_DECREF(result); } result = PyObject_CallFunction(helper->tell, NULL); if(!result) { PyThreadState_Swap(oldstate); PyEval_ReleaseLock(); return -1; } retval = PyInt_AsLong(result); Py_DECREF(result); PyThreadState_Swap(oldstate); PyEval_ReleaseLock(); return retval; }