aboutsummaryrefslogtreecommitdiffstats
path: root/libshiboken/threadstatesaver.h
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2010-01-18 10:46:38 -0300
committerMarcelo Lira <marcelo.lira@openbossa.org>2010-01-18 14:53:27 -0300
commit4cc55bd5ced408b987f41a0c0cd55fa99a869ba3 (patch)
tree80d8ab7f60a7d18221fdba4b2d561a1ed2fc85db /libshiboken/threadstatesaver.h
parent79774d9827ba3883ac1862aee86c890634fa21a9 (diff)
Adds convenience ThreadStateSaver class to libshiboken.
ThreadStateSaver class just wraps the Python Thread save and restore operations. Its destructor also ensures the restoration of saved thread state if the method ends abruptly, e.g. when the wrapped C++ library throws an exception.
Diffstat (limited to 'libshiboken/threadstatesaver.h')
-rw-r--r--libshiboken/threadstatesaver.h63
1 files changed, 63 insertions, 0 deletions
diff --git a/libshiboken/threadstatesaver.h b/libshiboken/threadstatesaver.h
new file mode 100644
index 000000000..6ff4e90d3
--- /dev/null
+++ b/libshiboken/threadstatesaver.h
@@ -0,0 +1,63 @@
+/*
+ * This file is part of the Shiboken Python Bindings Generator project.
+ *
+ * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+ *
+ * Contact: PySide team <contact@pyside.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation. Please
+ * review the following information to ensure the GNU Lesser General
+ * Public License version 2.1 requirements will be met:
+ * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * As a special exception to the GNU Lesser General Public License
+ * version 2.1, the object code form of a "work that uses the Library"
+ * may incorporate material from a header file that is part of the
+ * Library. You may distribute such object code under terms of your
+ * choice, provided that the incorporated material (i) does not exceed
+ * more than 5% of the total size of the Library; and (ii) is limited to
+ * numerical parameters, data structure layouts, accessors, macros,
+ * inline functions and templates.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#ifndef THREADSTATESAVER_H
+#define THREADSTATESAVER_H
+
+#include <Python.h>
+
+namespace Shiboken
+{
+
+class LIBSHIBOKEN_API ThreadStateSaver
+{
+public:
+ ThreadStateSaver() : m_threadState(0) {}
+ ~ThreadStateSaver() { restore(); }
+ inline void save() { m_threadState = PyEval_SaveThread(); }
+ inline void restore()
+ {
+ if (m_threadState) {
+ PyEval_RestoreThread(m_threadState);
+ m_threadState = 0;
+ }
+ }
+private:
+ PyThreadState* m_threadState;
+};
+
+} // namespace Shiboken
+
+#endif // THREADSTATESAVER_H
+