forked from Cryolite/kanachan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gil.cpp
54 lines (44 loc) · 944 Bytes
/
gil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#define PY_SSIZE_T_CLEAN
#include "simulation/gil.hpp"
#include "common/throw.hpp"
#include <Python.h>
#include <stdexcept>
namespace Kanachan::GIL{
RecursiveLock::RecursiveLock()
: state_(PyGILState_Ensure())
, owns_(true)
{}
RecursiveLock::RecursiveLock(RecursiveLock &&rhs) noexcept
: state_(rhs.state_)
, owns_(rhs.owns_)
{
rhs.owns_ = false;
}
RecursiveLock::~RecursiveLock()
{
if (owns_) {
PyGILState_Release(state_);
}
}
RecursiveLock &RecursiveLock::operator=(RecursiveLock &&rhs) noexcept
{
state_ = rhs.state_;
owns_ = rhs.owns_;
rhs.owns_ = false;
return *this;
}
RecursiveRelease::RecursiveRelease()
: save_(nullptr)
{
if (PyGILState_Check() != 0) {
save_ = PyEval_SaveThread();
}
}
RecursiveRelease::~RecursiveRelease()
{
if (save_ != nullptr) {
PyEval_RestoreThread(save_);
save_ = nullptr;
}
}
} // namespace Kanachan::GIL