From ff2db46e116803981d4414d6ebd3acda2f94fba7 Mon Sep 17 00:00:00 2001
From: "SND\\kernelnet_cp"
 <SND\kernelnet_cp@9b283d60-5439-405e-af05-b73fd8c4d996>
Date: Thu, 20 Oct 2011 05:35:41 +0000
Subject: [PATCH] [0.0 -> 0.1 ] integrated : pyaux.h

git-svn-id: https://pykd.svn.codeplex.com/svn@70638 9b283d60-5439-405e-af05-b73fd8c4d996
---
 pykd/pyaux.h | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
 create mode 100644 pykd/pyaux.h

diff --git a/pykd/pyaux.h b/pykd/pyaux.h
new file mode 100644
index 0000000..8e85f5c
--- /dev/null
+++ b/pykd/pyaux.h
@@ -0,0 +1,90 @@
+#pragma once
+
+#include <windows.h>
+
+///////////////////////////////////////////////////////////////////////////////
+
+class PyThreadStateSaver {
+
+public:
+
+    PyThreadStateSaver() {
+        m_index = TlsAlloc();
+    }
+
+    ~PyThreadStateSaver() {
+        TlsFree( m_index );
+    }
+
+    void saveState() {
+        if ( !isWindbgExt() )
+            TlsSetValue( m_index, PyEval_SaveThread() );
+        else
+            WindbgGlobalSession::SavePyState();                
+    }
+
+    void restoreState() {
+        if ( !isWindbgExt() )
+        {
+            PyThreadState*      state = (PyThreadState*)TlsGetValue( m_index );
+            if ( state )
+                PyEval_RestoreThread( state );
+        }
+        else
+        {
+            WindbgGlobalSession::RestorePyState();
+        }
+    }
+
+private:
+
+    DWORD   m_index;
+};
+
+extern PyThreadStateSaver       g_pyThreadState;
+
+
+//typedef PyThreadState *PyThreadStatePtr;
+//extern __declspec( thread ) PyThreadStatePtr ptrPyThreadState;
+
+//  --> call back 
+//  { PyThread_StateSave  state( winext->getThreadState() );
+//    do_callback();
+//  }
+//
+//  ���� ������ ��� ������ � ��� ���� � ������� ������ �������� �������� ( ��� ����� setExecutionStatus )
+//  �� ����� ����������� ������������ ���� ����� ������������ ��������, � ����� �������� ����������,
+//  ����� ��������� ���
+
+class PyThread_StateSave {
+
+public:
+
+    PyThread_StateSave() 
+    {
+        g_pyThreadState.restoreState();
+    }
+
+    ~PyThread_StateSave() {
+        g_pyThreadState.saveState();
+    }
+};
+
+// { PyThread_StateRestore   state;
+//   long_or_block_opreration();
+// }
+
+class PyThread_StateRestore
+{
+public:
+
+    PyThread_StateRestore() {
+        g_pyThreadState.saveState();
+    }
+
+    ~PyThread_StateRestore() {
+        g_pyThreadState.restoreState();
+    }
+};
+
+///////////////////////////////////////////////////////////////////////////////
\ No newline at end of file