本文整理汇总了C++中PyEval_AcquireLock函数的典型用法代码示例。如果您正苦于以下问题:C++ PyEval_AcquireLock函数的具体用法?C++ PyEval_AcquireLock怎么用?C++ PyEval_AcquireLock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PyEval_AcquireLock函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: glfwMakeContextCurrent
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();
}
开发者ID:firleju,项目名称:maszyna,代码行数:49,代码来源:PyInt.cpp
示例2: TargetCleanup
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);
}
开发者ID:Dinesh-Ramakrishnan,项目名称:openwsman,代码行数:32,代码来源:target_python.c
示例3: __tr2qs_ctx
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;
}
开发者ID:kartagis,项目名称:KVIrc,代码行数:60,代码来源:libkvipythoncore.cpp
示例4: PyEval_AcquireLock
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;
}
}
开发者ID:Saddamisalami,项目名称:xbmc,代码行数:31,代码来源:XBPython.cpp
示例5: lock
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();
}
}
开发者ID:marlboroman,项目名称:xbmc,代码行数:29,代码来源:XBPyThread.cpp
示例6: PyEval_AcquireLock
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;
}
开发者ID:kartagis,项目名称:KVIrc,代码行数:28,代码来源:libkvipythoncore.cpp
示例7: init
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
});
}
开发者ID:julienwuw,项目名称:ETISS,代码行数:28,代码来源:ETISSPython.cpp
示例8: rw_write_th
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;
}
开发者ID:FractalBobz,项目名称:renpy,代码行数:27,代码来源:rwobject.c
示例9: PythonModule_Init
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;
}
开发者ID:BackupTheBerlios,项目名称:easyconnect-svn,代码行数:35,代码来源:pythonmodule.c
示例10: rw_close_th
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;
}
开发者ID:FractalBobz,项目名称:renpy,代码行数:34,代码来源:rwobject.c
示例11: newPlugin
/*
* 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;
}
开发者ID:Akheon23,项目名称:bareos,代码行数:31,代码来源:python-sd.c
示例12: PythonEnv_Destroy
int PythonEnv_Destroy( PythonEnv* Self )
{
PyEval_AcquireLock();
PyThreadState_Swap(Self->MainThreadState);
Py_Finalize();
free( Self );
return 0;
}
开发者ID:BackupTheBerlios,项目名称:easyconnect-svn,代码行数:8,代码来源:pythonmodule.c
示例13: PyEval_AcquireLock
void ModuleLoader::finalize()
{
PyEval_AcquireLock();
PyThreadState_Swap( g_pymaintstate );
Py_Finalize();
}
开发者ID:BackupTheBerlios,项目名称:bluetool-svn,代码行数:8,代码来源:btool_module_loader.cpp
示例14: assert
/**
* 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();
}
开发者ID:FFMG,项目名称:myoddweb.piger,代码行数:61,代码来源:pyapi.cpp
示例15: End
void End()
{
PyEval_AcquireLock();
PyThreadState_Swap( NULL );
PyThreadState_Clear( mainThreadState );
PyThreadState_Delete( mainThreadState );
//PyEval_Restore( mainThreadState );
Py_Finalize();
started = ScriptEngine::HasBegun();
}
开发者ID:nszhsl,项目名称:pylon,代码行数:10,代码来源:scriptengine.cpp
示例16: do_python_cleanup
/** 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();
}
开发者ID:padam,项目名称:freeradius-server,代码行数:12,代码来源:rlm_python.c
示例17: gil_real_get
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);
}
开发者ID:20tab,项目名称:uwsgi,代码行数:11,代码来源:gil.c
示例18: on_shutdown
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");
}
开发者ID:liexusong,项目名称:NXWEB,代码行数:11,代码来源:python.c
示例19: PyEval_AcquireLock
void XBPyThread::stop()
{
if (m_threadState)
{
PyEval_AcquireLock();
m_threadState->c_tracefunc = xbTrace;
m_threadState->use_tracing = 1;
PyEval_ReleaseLock();
}
stopping = true;
}
开发者ID:Avoidnf8,项目名称:xbmc-fork,代码行数:12,代码来源:XBPyThread.cpp
示例20: m_mainThreadState
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);
}
}
开发者ID:AlistairMills,项目名称:mantid,代码行数:13,代码来源:Threading.cpp
注:本文中的PyEval_AcquireLock函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论