blob: 2cae5a78ed8f2240554040645b2cce2437fbbc48 (
plain)
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
55
56
57
58
59
60
61
62
63
64
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#pragma once
//WARNING: since this will unlock/lock the python global interpreter lock,
// it will not work recursively
//this is basically a scoped version of a Py_BEGIN_ALLOW_THREADS .. Py_END_ALLOW_THREADS block
class CPyThreadState
{
public:
explicit CPyThreadState(bool save = true)
{
m_threadState = NULL;
if (save)
Save();
}
~CPyThreadState()
{
Restore();
}
void Save()
{
if (!m_threadState)
m_threadState = PyEval_SaveThread(); //same as Py_BEGIN_ALLOW_THREADS
}
void Restore()
{
if (m_threadState)
{
PyEval_RestoreThread(m_threadState); //same as Py_END_ALLOW_THREADS
m_threadState = NULL;
}
}
private:
PyThreadState* m_threadState;
};
/**
* A std::unique_lock<CCriticalSection> that will relinquish the GIL during the time
* it takes to obtain the CriticalSection
*/
class GilSafeSingleLock : public CPyThreadState, public std::unique_lock<CCriticalSection>
{
public:
explicit GilSafeSingleLock(CCriticalSection& critSec)
: CPyThreadState(true), std::unique_lock<CCriticalSection>(critSec)
{
CPyThreadState::Restore();
}
};
|