From df191bfe39c1a40649173212029e4967d425a48c Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sat, 23 Oct 2010 14:10:12 +0000
Subject: [PATCH] Added wxThread::OnKill() and OnDelete() callbacks.

Call OnXXX() from wxThread::Kill() and Delete() respectively to allow the
thread being terminated perform some cleanup.

Closes #9046.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65882 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/changes.txt       |  1 +
 include/wx/thread.h    | 13 +++++++++++++
 interface/wx/thread.h  | 31 +++++++++++++++++++++++++++++++
 src/msw/thread.cpp     |  3 +++
 src/unix/threadpsx.cpp |  4 ++++
 5 files changed, 52 insertions(+)

diff --git a/docs/changes.txt b/docs/changes.txt
index 4cb400ad3e..bc7eaae2a2 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -406,6 +406,7 @@ Major new features in this release
 All:
 
 - Added "rest" argument to wxString::Before{First,Last}().
+- Added wxThread::OnKill() and OnDelete() callbacks.
 
 All (GUI):
 
diff --git a/include/wx/thread.h b/include/wx/thread.h
index ed9efa05bf..fbb3cfb042 100644
--- a/include/wx/thread.h
+++ b/include/wx/thread.h
@@ -585,6 +585,19 @@ protected:
     // of this thread.
     virtual void *Entry() = 0;
 
+
+    // Callbacks which may be overridden by the derived class to perform some
+    // specific actions when the thread is deleted or killed. By default they
+    // do nothing.
+
+    // This one is called by Delete() before actually deleting the thread and
+    // is executed in the context of the thread that called Delete().
+    virtual void OnDelete() {}
+
+    // This one is called by Kill() before killing the thread and is executed
+    // in the context of the thread that called Kill().
+    virtual void OnKill() {}
+
 private:
     // no copy ctor/assignment operator
     wxThread(const wxThread&);
diff --git a/interface/wx/thread.h b/interface/wx/thread.h
index 34d98462fd..52b94f3128 100644
--- a/interface/wx/thread.h
+++ b/interface/wx/thread.h
@@ -454,6 +454,37 @@ public:
     */
     virtual ExitCode Entry() = 0;
 
+    /**
+        Callback called by Delete() before actually deleting the thread.
+
+        This function can be overridden by the derived class to perform some
+        specific task when the thread is gracefully destroyed. Notice that it
+        will be executed in the context of the thread that called Delete() and
+        <b>not</b> in this thread's context.
+
+        TestDestroy() will be true for the thread before OnDelete() gets
+        executed.
+
+        @since 2.9.2
+
+        @see OnKill()
+    */
+    virtual void OnDelete();
+
+    /**
+        Callback called by Kill() before actually killing the thread.
+
+        This function can be overridden by the derived class to perform some
+        specific task when the thread is terminated. Notice that it will be
+        executed in the context of the thread that called Kill() and <b>not</b>
+        in this thread's context.
+
+        @since 2.9.2
+
+        @see OnDelete()
+    */
+    virtual void OnKill();
+
     /**
         @deprecated
         Use CreateThread() instead.
diff --git a/src/msw/thread.cpp b/src/msw/thread.cpp
index 53c192cc50..1a668976f3 100644
--- a/src/msw/thread.cpp
+++ b/src/msw/thread.cpp
@@ -686,6 +686,8 @@ bool wxThreadInternal::Create(wxThread *thread, unsigned int stackSize)
 
 wxThreadError wxThreadInternal::Kill()
 {
+    m_thread->OnKill();
+
     if ( !::TerminateThread(m_hThread, THREAD_ERROR_EXIT) )
     {
         wxLogSysError(_("Couldn't terminate thread"));
@@ -759,6 +761,7 @@ wxThreadInternal::WaitForTerminate(wxCriticalSection& cs,
         Cancel();
     }
 
+    threadToDelete->OnDelete();
 
     // now wait for thread to finish
     if ( wxThread::IsMain() )
diff --git a/src/unix/threadpsx.cpp b/src/unix/threadpsx.cpp
index e818385c66..ebb365b0d0 100644
--- a/src/unix/threadpsx.cpp
+++ b/src/unix/threadpsx.cpp
@@ -1453,6 +1453,8 @@ wxThreadError wxThread::Delete(ExitCode *rc)
 
     m_critsect.Leave();
 
+    OnDelete();
+
     switch ( state )
     {
         case STATE_NEW:
@@ -1501,6 +1503,8 @@ wxThreadError wxThread::Kill()
     wxCHECK_MSG( This() != this, wxTHREAD_MISC_ERROR,
                  wxT("a thread can't kill itself") );
 
+    OnKill();
+
     switch ( m_internal->GetState() )
     {
         case STATE_NEW:
-- 
2.47.2