-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgil_release.h
More file actions
37 lines (31 loc) · 803 Bytes
/
gil_release.h
File metadata and controls
37 lines (31 loc) · 803 Bytes
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
#ifndef _GIL_RELEASE_H
#define _GIL_RELEASE_H
#include <boost/python.hpp>
#include <iostream>
#define GIL_DEBUG 0
using std::cerr;
using std::endl;
//used to release GIL for python when it uses c++ code
namespace gil_release{
class ScopedGILRelease {
public:
inline ScopedGILRelease() {
if(Py_IsInitialized()) {
if(GIL_DEBUG) cerr << "Releasing GIL lock" << endl;
m_thread_state = PyEval_SaveThread();
if(GIL_DEBUG) cerr << "Released GIL lock" << endl;
}
}
inline ~ScopedGILRelease() {
if(Py_IsInitialized()) {
if(GIL_DEBUG) cerr << "Restoring GIL lock" << endl;
PyEval_RestoreThread(m_thread_state);
m_thread_state = NULL;
if(GIL_DEBUG) cerr << "Restored GIL lock" << endl;
}
}
private:
PyThreadState * m_thread_state;
};
}
#endif