2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2013-12-23 17:48:20 +01:00
|
|
|
|
2019-02-17 22:08:12 +01:00
|
|
|
/** \file
|
|
|
|
* \ingroup pygen
|
2013-12-23 17:48:20 +01:00
|
|
|
*
|
|
|
|
* This file contains wrapper functions related to global interpreter lock.
|
|
|
|
* these functions are slightly different from the original Python API,
|
2023-07-21 19:41:03 +02:00
|
|
|
* don't throw SIGABRT even if the thread state is nullptr. */
|
2013-12-23 17:48:20 +01:00
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
|
|
#include "../BPY_extern.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2013-12-23 17:48:20 +01:00
|
|
|
|
2023-07-22 03:33:36 +02:00
|
|
|
BPy_ThreadStatePtr BPY_thread_save()
|
2013-12-23 17:48:20 +01:00
|
|
|
{
|
2021-09-10 14:57:56 +02:00
|
|
|
/* Use `_PyThreadState_UncheckedGet()` instead of `PyThreadState_Get()`, to avoid a fatal error
|
2023-07-21 19:41:03 +02:00
|
|
|
* issued when a thread state is nullptr (the thread state can be nullptr when quitting Blender).
|
2021-09-10 14:57:56 +02:00
|
|
|
*
|
|
|
|
* `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 09:36:04 +02:00
|
|
|
return (BPy_ThreadStatePtr)PyEval_SaveThread();
|
2013-12-23 17:48:20 +01:00
|
|
|
}
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2013-12-23 17:48:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BPY_thread_restore(BPy_ThreadStatePtr tstate)
|
|
|
|
{
|
|
|
|
if (tstate) {
|
|
|
|
PyEval_RestoreThread((PyThreadState *)tstate);
|
|
|
|
}
|
|
|
|
}
|