2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2013-12-23 22:48:20 +06:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup pygen
|
2013-12-23 22:48:20 +06:00
|
|
|
*
|
|
|
|
|
* This file contains wrapper functions related to global interpreter lock.
|
|
|
|
|
* these functions are slightly different from the original Python API,
|
|
|
|
|
* don't throw SIGABRT even if the thread state is NULL. */
|
|
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
|
|
#include "../BPY_extern.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2013-12-23 22:48:20 +06:00
|
|
|
|
|
|
|
|
BPy_ThreadStatePtr BPY_thread_save(void)
|
|
|
|
|
{
|
2021-09-10 14:57:56 +02:00
|
|
|
/* Use `_PyThreadState_UncheckedGet()` instead of `PyThreadState_Get()`, to avoid a fatal error
|
|
|
|
|
* issued when a thread state is NULL (the thread state can be NULL when quitting Blender).
|
|
|
|
|
*
|
|
|
|
|
* `PyEval_SaveThread()` will release the GIL, so this thread has to have the GIL to begin with
|
|
|
|
|
* or badness will ensue. */
|
|
|
|
|
if (_PyThreadState_UncheckedGet() && PyGILState_Check()) {
|
2020-10-14 18:36:04 +11:00
|
|
|
return (BPy_ThreadStatePtr)PyEval_SaveThread();
|
2013-12-23 22:48:20 +06:00
|
|
|
}
|
2020-10-14 18:36:04 +11:00
|
|
|
return NULL;
|
2013-12-23 22:48:20 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BPY_thread_restore(BPy_ThreadStatePtr tstate)
|
|
|
|
|
{
|
|
|
|
|
if (tstate) {
|
|
|
|
|
PyEval_RestoreThread((PyThreadState *)tstate);
|
|
|
|
|
}
|
|
|
|
|
}
|