]> git.saurik.com Git - wxWidgets.git/commitdiff
make POSIX and Windows implementation of wxThread::Run() coherently assert when tryin...
authorFrancesco Montorsi <f18m_cpp217828@yahoo.it>
Sun, 25 Jul 2010 13:55:36 +0000 (13:55 +0000)
committerFrancesco Montorsi <f18m_cpp217828@yahoo.it>
Sun, 25 Jul 2010 13:55:36 +0000 (13:55 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65106 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

interface/wx/thread.h
src/msw/thread.cpp
tests/thread/misc.cpp

index 996673001020c39717d0387f180bf65ec612c803..34d98462fdbe1b16bf89f26aa0031a3ac2ce1a95 100644 (file)
@@ -788,7 +788,7 @@ enum
 
             if (m_pThread)         // does the thread still exist?
             {
-                m_out.Printf("MYFRAME: deleting thread");
+                wxMessageOutputDebug().Printf("MYFRAME: deleting thread");
 
                 if (m_pThread->Delete() != wxTHREAD_NO_ERROR )
                     wxLogError("Can't delete the thread!");
@@ -1125,6 +1125,10 @@ public:
         of detached threads.
 
         This function can only be called from another thread context.
+        
+        Finally, note that once a thread has completed and its Entry() function
+        returns, you cannot call Run() on it again (an assert will fail in debug
+        builds or @c wxTHREAD_RUNNING will be returned in release builds).
     */
     wxThreadError Run();
 
index 6fe0d3bca07f01e477d139da15e84f97ce4b51d8..58715647cef2a14c9fe959c43bc98ac20b01cb25 100644 (file)
@@ -1077,11 +1077,8 @@ wxThreadError wxThread::Run()
 {
     wxCriticalSectionLocker lock(m_critsect);
 
-    if ( m_internal->GetState() != STATE_NEW )
-    {
-        // actually, it may be almost any state at all, not only STATE_RUNNING
-        return wxTHREAD_RUNNING;
-    }
+    wxCHECK_MSG( m_internal->GetState() == STATE_NEW, wxTHREAD_RUNNING,
+             wxT("thread may only be started once after Create()") );
 
     // the thread has just been created and is still suspended - let it run
     return Resume();
index 38473a76f6a7251eb105eb3e3072ff96b7f24ad5..5dcab1520aa2dfda226a3996703fcf6d1b654fc4 100644 (file)
@@ -208,6 +208,7 @@ private:
         CPPUNIT_TEST( TestDetached );
         CPPUNIT_TEST( TestThreadSuspend );
         CPPUNIT_TEST( TestThreadDelete );
+        CPPUNIT_TEST( TestThreadRun );
         CPPUNIT_TEST( TestThreadConditions );
         CPPUNIT_TEST( TestSemaphore );
     CPPUNIT_TEST_SUITE_END();
@@ -218,6 +219,7 @@ private:
 
     void TestThreadSuspend();
     void TestThreadDelete();
+    void TestThreadRun();
     void TestThreadConditions();
 
     DECLARE_NO_COPY_CLASS(MiscThreadTestCase)
@@ -320,6 +322,7 @@ void MiscThreadTestCase::TestThreadSuspend()
 
 void MiscThreadTestCase::TestThreadDelete()
 {
+    // FIXME:
     // As above, using Sleep() is only for testing here - we must use some
     // synchronisation object instead to ensure that the thread is still
     // running when we delete it - deleting a detached thread which already
@@ -345,7 +348,7 @@ void MiscThreadTestCase::TestThreadDelete()
     MyJoinableThread thread3(20);
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread3.Run() );
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread3.Delete() );       
-        // delete a joinable thread
+        // delete a joinable running thread
 
     MyJoinableThread thread4(2);
     CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread4.Run() );
@@ -354,6 +357,16 @@ void MiscThreadTestCase::TestThreadDelete()
         // delete a joinable thread which already terminated
 }
 
+void MiscThreadTestCase::TestThreadRun()
+{
+    MyJoinableThread thread1(2);
+    CPPUNIT_ASSERT_EQUAL( wxTHREAD_NO_ERROR, thread1.Run() );
+    thread1.Wait();     // wait until the thread ends
+
+    // verify that running twice the same thread fails
+    WX_ASSERT_FAILS_WITH_ASSERT( thread1.Run() );
+}
+
 void MiscThreadTestCase::TestThreadConditions()
 {
     wxMutex mutex;