From 67950756978f093d8b6e3d73596d1e7d183d51b4 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Thu, 8 Dec 2011 23:39:42 +0100 Subject: [PATCH] Thread join checks that the thread is started. Test to check the volatile-ness of state member. --- src/Thread.cpp | 5 ++-- test/test_Thread.hpp | 60 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/Thread.cpp b/src/Thread.cpp index 5a62587..c021053 100644 --- a/src/Thread.cpp +++ b/src/Thread.cpp @@ -31,8 +31,9 @@ void Thread::start() void* Thread::join() const { TRACE; -// if ( !m_isRunning ) -// return 0; + + if ( !m_isRunning ) + return 0; void* retVal; pthread_join( m_threadHandler, &retVal ); diff --git a/test/test_Thread.hpp b/test/test_Thread.hpp index 93d8666..9ac14f7 100644 --- a/test/test_Thread.hpp +++ b/test/test_Thread.hpp @@ -68,6 +68,7 @@ private: { TRACE; + // we will got a signal before the sleep finishes sleep(665); void* retVal = malloc(sizeof(int)); @@ -122,7 +123,7 @@ private: public: - void eetestEmpty( void ) + void testEmpty( void ) { TEST_HEADER; @@ -134,16 +135,51 @@ public: TS_ASSERT_EQUALS ( retVal , (void *)0 ); } -// void testJoiningNotStartedThread( void ) -// { -// TEST_HEADER; -// -// EmptyThreadClass e; -// -// e.stop(); -// e.join(); -// void *retVal = e.join(); -// TS_ASSERT_EQUALS ( retVal , (void *)0 ); -// } + void testJoiningNotStartedThread( void ) + { + TEST_HEADER; + + EmptyThreadClass e; + + e.stop(); + e.join(); + void *retVal = e.join(); + TS_ASSERT_EQUALS ( retVal , (void *)0 ); + } + +private: + + class EndlessThread : public Thread + { + + private: + + void* run( void ) + { + TRACE; + + /** @note if m_isRunning is not volatile, it can be optimized out + * to a while(1), since body of the loop does not modifies it + */ + while ( m_isRunning ) + sleep(1); + + return 0; + } + }; + +public: + + void testStatusIsVolatile() + { + TEST_HEADER; + + EndlessThread et; + et.start(); + + sleep(1); + et.stop(); + et.join(); + } };