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 
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 public:
108  save();
109  }
110 
112  restore();
113  }
114 
115  void save() {
116  _state = PyEval_SaveThread();
117  }
118 
119  void restore() {
120  if (_state) {
121  PyEval_RestoreThread(_state);
122  _state = NULL;
123  }
124  }
125 
126 private:
127  PyThreadState* _state;
128 };
129 
130 #else
131 
132 #define PYTHONQT_ALLOW_THREADS_SCOPE
133 
136 {
137 public:
139  void save() {}
140  void restore() {}
141 };
142 
143 #endif
#define PYTHONQT_EXPORT
static void setGILScopeEnabled(bool flag)
static bool isGILScopeEnabled()
Check if GIL scopes are enabled.