PythonQt
PythonQtThreadSupport.h
Go to the documentation of this file.
1 #pragma once
2 /*
3 *
4 * Copyright (C) 2018 MeVis Medical Solutions AG All Rights Reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * Further, this software is distributed without any warranty that it is
17 * free of the rightful claim of any third person regarding infringement
18 * or the like. Any license provided herein, whether implied or
19 * otherwise, applies only to this software file. Patent licenses, if
20 * any, provided herein do not apply to combinations of this program with
21 * other software, or any other product whatsoever.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 * Contact information: MeVis Medical Solutions AG, Universitaetsallee 29,
28 * 28359 Bremen, Germany or:
29 *
30 * http://www.mevis.de
31 *
32 */
33 
34 //----------------------------------------------------------------------------------
40 //----------------------------------------------------------------------------------
41 
42 #include <QtGlobal> //Q_DISABLE_COPY
43 #include "PythonQtPythonInclude.h"
44 #include "PythonQtSystem.h"
45 
46 #define PYTHONQT_FULL_THREAD_SUPPORT
47 
48 #ifdef PYTHONQT_FULL_THREAD_SUPPORT
49 #define PYTHONQT_GIL_SUPPORT
50 #define PYTHONQT_ALLOW_THREADS_SUPPORT
51 #endif
52 
53 #ifdef PYTHONQT_GIL_SUPPORT
54 
55 #define PYTHONQT_GIL_SCOPE PythonQtGILScope internal_pythonqt_gilscope;
56 
61 {
62 public:
64 
66 
67  void release();
68 
72  static void setGILScopeEnabled(bool flag);
74  static bool isGILScopeEnabled();
75 
76 private:
77  PyGILState_STATE _state;
78  bool _ensured;
79 
80  static bool _enableGILScope;
81 };
82 
83 #else
84 
85 #define PYTHONQT_GIL_SCOPE
86 
88 class PythonQtGILScope
89 {
90 public:
91  PythonQtGILScope() {}
92  void release() {}
93 };
94 
95 #endif
96 
97 #ifdef PYTHONQT_ALLOW_THREADS_SUPPORT
98 
99 #define PYTHONQT_ALLOW_THREADS_SCOPE PythonQtThreadStateSaver internal_pythonqt_savethread;
100 
105 {
106  Q_DISABLE_COPY(PythonQtThreadStateSaver)
107 public:
109  save();
110  }
111 
113  restore();
114  }
115 
116  void save() {
117  _state = PyEval_SaveThread();
118  }
119 
120  void restore() {
121  if (_state) {
122  PyEval_RestoreThread(_state);
123  _state = nullptr;
124  }
125  }
126 
127 private:
128  PyThreadState* _state;
129 };
130 
131 #else
132 
133 #define PYTHONQT_ALLOW_THREADS_SCOPE
134 
137 {
138 public:
140  void save() {}
141  void restore() {}
142 };
143 
144 #endif
#define PYTHONQT_EXPORT
static void setGILScopeEnabled(bool flag)
static bool isGILScopeEnabled()
Check if GIL scopes are enabled.