NRP Core  1.4.1
python_interpreter_state.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef PYTHON_INTERPRETER_STATE_H
23 #define PYTHON_INTERPRETER_STATE_H
24 
26 
27 #include <boost/python.hpp>
28 #include <stack>
29 
34 {
35  public:
41  PythonInterpreterState(int argc, const char *const *argv, bool allowThreads = false);
42 
48  PythonInterpreterState(int argc, const std::vector<const char*> &argv, bool allowThreads = false);
49 
50 
56  explicit PythonInterpreterState(bool allowThreads = false);
57 
61  void allowThreads();
62 
67  bool threadsAllowed() const;
68 
72  void endAllowThreads();
73 
78 
79  private:
83  WCharTConverter _wcharArgs;
84 
88  PyThreadState *_state = nullptr;
89 };
90 
95 {
96  public:
102  PythonGILLock(PyGILState_STATE &state, const bool acquire = true);
103 
107  ~PythonGILLock();
108 
109  // Disable copy mechanism
110  PythonGILLock(const PythonGILLock&) = delete;
111  PythonGILLock &operator=(const PythonGILLock&) = delete;
112 
116  void acquire();
117 
121  static bool hasGIL();
122 
126  void release();
127 
128  private:
132  PyGILState_STATE *_state;
133 };
134 
135 #endif // PYTHON_INTERPRETER_STATE_H
PythonGILLock
Manages the Pyton GIL. Useful for threads.
Definition: python_interpreter_state.h:94
PythonInterpreterState
Initializes the python interpreter as well as python threading.
Definition: python_interpreter_state.h:33
PythonInterpreterState::allowThreads
void allowThreads()
Allow execution of other threads. If this is set, main thread may not execute python code.
Definition: python_interpreter_state.cpp:85
WCharTConverter
Converts and stores an char*[] to wchar*[]. Used mainly to convert argv from char*[] to wchar*[] for ...
Definition: wchar_t_converter.h:32
PythonGILLock::hasGIL
static bool hasGIL()
Does this thread have the GIL?
PythonInterpreterState::~PythonInterpreterState
~PythonInterpreterState()
Destructor. Reestablishes thread state.
Definition: python_interpreter_state.cpp:105
PythonGILLock::PythonGILLock
PythonGILLock(PyGILState_STATE &state, const bool acquire=true)
Constructor. Acquires GIL if requested.
Definition: python_interpreter_state.cpp:110
wchar_t_converter.h
PythonGILLock::~PythonGILLock
~PythonGILLock()
Releases GIL if previously acquired.
Definition: python_interpreter_state.cpp:117
PythonGILLock::acquire
void acquire()
Acquire GIL.
Definition: python_interpreter_state.cpp:123
PythonInterpreterState::PythonInterpreterState
PythonInterpreterState(int argc, const char *const *argv, bool allowThreads=false)
Constructor. Initializes Python with the given start parameters, enables threading,...
Definition: python_interpreter_state.cpp:48
PythonInterpreterState::threadsAllowed
bool threadsAllowed() const
Are threads currently allowed?
Definition: python_interpreter_state.cpp:91
PythonGILLock::operator=
PythonGILLock & operator=(const PythonGILLock &)=delete
PythonGILLock::release
void release()
Release GIL.
Definition: python_interpreter_state.cpp:128
PythonInterpreterState::endAllowThreads
void endAllowThreads()
Halt other threads from executin. This is required if python code should be executed in the main thre...
Definition: python_interpreter_state.cpp:96