diff options
author | Steve Huston <shuston@riverace.com> | 2002-11-06 16:15:20 +0000 |
---|---|---|
committer | Steve Huston <shuston@riverace.com> | 2002-11-06 16:15:20 +0000 |
commit | 4223e80901ffa6a721608e04d6d0016834b689f7 (patch) | |
tree | 181253aa1b128e34a87dfb351824b1ebbb8f1fc3 | |
parent | 54dc977359ae1d9a3c1070c0bf1221c10a03e58e (diff) | |
download | ATCD-4223e80901ffa6a721608e04d6d0016834b689f7.tar.gz |
ChangeLogTag:Wed Nov 6 11:12:28 2002 Steve Huston <shuston@riverace.com>
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | ChangeLogs/ChangeLog-03a | 8 | ||||
-rw-r--r-- | tests/Recursive_Condition_Test.cpp | 263 |
3 files changed, 169 insertions, 110 deletions
diff --git a/ChangeLog b/ChangeLog index c198c19a925..a9450ba806e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Wed Nov 6 11:12:28 2002 Steve Huston <shuston@riverace.com> + + * tests/Recursive_Condition_Test.cpp: Replaced ACE_TRACE with + ACE_DEBUG so the log gets all the info. Re-enabled the timer queue + part of the test and added a check to be sure "enough" expirations + are recorded. Added a piece of data protected by the mutex/condition + to be sure threads aren't released too early. + Tue Nov 5 21:54:40 2002 Balachandran Natarajan <bala@isis-server.isis.vanderbilt.edu> * tests/run_tests.lst: diff --git a/ChangeLogs/ChangeLog-03a b/ChangeLogs/ChangeLog-03a index c198c19a925..a9450ba806e 100644 --- a/ChangeLogs/ChangeLog-03a +++ b/ChangeLogs/ChangeLog-03a @@ -1,3 +1,11 @@ +Wed Nov 6 11:12:28 2002 Steve Huston <shuston@riverace.com> + + * tests/Recursive_Condition_Test.cpp: Replaced ACE_TRACE with + ACE_DEBUG so the log gets all the info. Re-enabled the timer queue + part of the test and added a check to be sure "enough" expirations + are recorded. Added a piece of data protected by the mutex/condition + to be sure threads aren't released too early. + Tue Nov 5 21:54:40 2002 Balachandran Natarajan <bala@isis-server.isis.vanderbilt.edu> * tests/run_tests.lst: diff --git a/tests/Recursive_Condition_Test.cpp b/tests/Recursive_Condition_Test.cpp index 76dbb8c0abd..39639fca0a7 100644 --- a/tests/Recursive_Condition_Test.cpp +++ b/tests/Recursive_Condition_Test.cpp @@ -23,7 +23,7 @@ #include "test_config.h" #include "ace/Event_Handler.h" #include "ace/Synch.h" -#include "ace/Trace.h" +#include "ace/Log_Msg.h" #include "ace/Thread_Manager.h" #include "ace/Timer_Heap.h" #include "ace/Timer_Queue_Adapters.h" @@ -37,39 +37,59 @@ ACE_RCSID(tests, Recursive_Condition_Test, "$Id$") class Test_Handler : public ACE_Event_Handler { public: + Test_Handler () : nr_expirations_ (0) {} + int nr_expirations (void) { return this->nr_expirations_; } + virtual int handle_timeout (const ACE_Time_Value &, - const void * /*arg*/) + const void *arg) { - ACE_TRACE ("Test_Handler::handle_timeout"); + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Test_Handler::handle_timeout\n"))); + ++this->nr_expirations_; - /*Thread_Timer_Queue *timer_queue = - (Thread_Timer_Queue *) arg;*/ + Thread_Timer_Queue *timer_queue = + ACE_reinterpret_cast (Thread_Timer_Queue *, arg); ACE_Time_Value timeout = ACE_OS::gettimeofday () + ACE_Time_Value (1, 0); - ACE_UNUSED_ARG (timeout); - ACE_DEBUG ((LM_DEBUG, - ACE_TEXT ("(%P|%t) scheduling timer\n"))); + ACE_TEXT ("(%t) scheduling new timer 1 sec from now\n"))); + if (timer_queue->schedule (this, timer_queue, timeout) == -1) + ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("schedule failed"))); - /* - int timer_id = - timer_queue->schedule (this, timer_queue, timeout); - */ return 0; } + +private: + int nr_expirations_; }; -// These are for the second test - simple wait/signal. +// These are for the basic functionality tests. ACE_SYNCH_RECURSIVE_MUTEX mutex_; ACE_SYNCH_RECURSIVE_CONDITION condition_(mutex_); +// Test driver sets this to non-zero before spawning and to zero for waiter. +int protected_int = 0; + +ACE_THR_FUNC_RETURN waiter (void *) +{ + if (mutex_.acquire () != 0) + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("acquire")), 0); + + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) waiting for cv signal...\n"))); + if (condition_.wait () == 0) + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) woken up!!!\n"))); + else + ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), ACE_TEXT ("wait"))); + + int copy_int = protected_int; // Copy it in case it's erroneously changing + if (copy_int != 0) + ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) waiter found protected_int %d\n"), + copy_int)); + + if (mutex_.release () != 0) + ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), ACE_TEXT ("release"))); -ACE_THR_FUNC_RETURN waiter (void *) { - ACE_ASSERT (mutex_.acquire () == 0); - ACE_TRACE (ACE_TEXT ("(%t) waiting for cv signal...\n")); - condition_.wait(); - ACE_TRACE (ACE_TEXT ("(%t) woken up!!!\n")); - mutex_.release (); return 0; } @@ -77,26 +97,28 @@ ACE_THR_FUNC_RETURN waiter (void *) { int test_1(void) { + protected_int = 1; if (ACE_Thread_Manager::instance()->spawn (waiter) == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn")), 1); - } + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("test 1 spawn")), + 1); ACE_OS::sleep (2); if (mutex_.acquire () == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), - ACE_TEXT ("mutex acquire")), - 1); - } - ACE_TRACE (ACE_TEXT ("(%t) signaling condition...\n")); + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("test 1 mutex acquire")), + 1); + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) signaling condition...\n"))); + protected_int = 0; if (condition_.signal () == -1) - { - ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("signal"))); - } + ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("test 1 signal"))); + + if (mutex_.release () == -1) + ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("test 1 release"))); - mutex_.release (); ACE_Thread_Manager::instance ()->wait (); return 0; } @@ -104,27 +126,29 @@ test_1(void) int test_2(void) { + protected_int = 1; if (ACE_Thread_Manager::instance()->spawn (waiter) == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn")), 1); - } + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("test 2 spawn")), + 1); ACE_OS::sleep (2); if (mutex_.acquire () == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), - ACE_TEXT ("mutex acquire")), - 1); - } + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("test 2 mutex acquire")), + 1); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) signaling condition...\n"))); - if (condition_.signal () == -1) - { - ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("signal"))); - } + ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("test 2 signal"))); + // Wait to clear protected_int to be sure cv properly reacquires the + // mutex before returning control to caller. ACE_OS::sleep(2); - mutex_.release (); + protected_int = 0; + if (mutex_.release () == -1) + ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("test 2 release"))); + ACE_Thread_Manager::instance ()->wait (); return 0; } @@ -132,25 +156,26 @@ test_2(void) int test_3() { - if (ACE_Thread_Manager::instance()->spawn_n (4,waiter) == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn")), 1); - } + protected_int = 1; + if (ACE_Thread_Manager::instance()->spawn_n (4, waiter) == -1) + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), + ACE_TEXT ("test 3 spawn")), 1); ACE_OS::sleep (2); if (mutex_.acquire () == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), - ACE_TEXT ("mutex acquire")), + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("test 3 mutex acquire")), 1); - } - ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) signaling condition...\n"))); + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) broadcasting condition...\n"))); if (condition_.broadcast () == -1) - { - ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("signal"))); - } - mutex_.release (); + ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("test 3 broadcast"))); + protected_int = 0; + if (mutex_.release () == -1) + ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("test 3 release"))); + ACE_Thread_Manager::instance ()->wait (); return 0; @@ -159,44 +184,52 @@ test_3() int test_4() { + const int recurse_count = 3; + + protected_int = recurse_count; if (ACE_Thread_Manager::instance()->spawn (waiter) == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn")), 1); - } + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("spawn")), 1); ACE_OS::sleep (2); - for(int i = 0; i < 3; ++i){ - if (mutex_.acquire () == -1) - { - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), - ACE_TEXT ("mutex acquire")), - 1); - } - } - - if (mutex_.get_nesting_level() != 3) + int i; + for (i = 0; i < recurse_count; ++i) { - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT("%d\n"), - ACE_TEXT("get_nestling_level")), - 1); + if (mutex_.acquire () == -1) + { + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("pass %d, %p\n"), + i + 1, + ACE_TEXT ("recursive acquire")), + 1); + } } - ACE_TRACE (ACE_TEXT ("(%t) signaling condition...\n")); + if (mutex_.get_nesting_level () != i) + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT("test 4 nesting level %d;"), + ACE_TEXT (" should be %d\n"), + mutex_.get_nesting_level (), i), + 1); + + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) signaling condition...\n"))); if (condition_.signal () == -1) + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), + ACE_TEXT ("test 4 signal")), + 1); + + for (i = 0; i < recurse_count; ++i) { - ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("signal"))); + // Only decrement - be sure all the waiting threads are not released + // before we release the mutex the correct number of times. + --protected_int; + mutex_.release (); } - for(int k = 0; k < 3; ++k){ - mutex_.release (); - } - if (mutex_.get_nesting_level() != 0) - { - ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT("%d\n"), - ACE_TEXT("get_nestling_level")), - 1); - } + ACE_ERROR_RETURN ((LM_ERROR, + ACE_TEXT("(%t) nesting level %d; should be 0\n"), + mutex_.get_nesting_level ()), + 1); + ACE_Thread_Manager::instance ()->wait (); return 0; } @@ -208,47 +241,57 @@ ACE_TMAIN (int, ACE_TCHAR *[]) ACE_START_TEST (ACE_TEXT ("Recursive_Condition_Test")); #if defined (ACE_HAS_THREADS) -#if 0 - Thread_Timer_Queue timer_queue; - Test_Handler handler; - int status = timer_queue.activate (); - - ACE_ASSERT (status == 0); - - ACE_Time_Value timeout = - ACE_OS::gettimeofday() + ACE_Time_Value (1, 0); - - int timer_id = timer_queue.schedule (&handler, &timer_queue, timeout); - ACE_OS::sleep (10); - timer_queue.deactivate (); - timer_queue.wait (); -#endif + int status = 0; /* Test 1 - Simple test */ + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Test 1...\n"))); if (test_1 () != 0) - { - ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT("test #1")), 1); - } + ++status; /* Test #2 - Sleep 2 seconds before releasing mutex */ + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Test 2...\n"))); if (test_2 () != 0) - { - ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT("test #2")), 1); - } + ++status; /* Test #3 - One main thread - 4 subthreads */ + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Test 3...\n"))); if (test_3 () != 0) - { - ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT("test #3")), 1); - } + ++status; /* Test #4 - Multiple calls to mutex_.acquire and mutex_.release */ + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Test 4...\n"))); if (test_4 () != 0) + ++status; + + // Timer queue usage. + Thread_Timer_Queue timer_queue; + Test_Handler handler; + if (0 != timer_queue.activate ()) { - ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT("test #4")), 1); + ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), ACE_TEXT ("activate"))); + ++status; } + ACE_Time_Value timeout = + ACE_OS::gettimeofday() + ACE_Time_Value (1, 0); + + if (-1 == timer_queue.schedule (&handler, &timer_queue, timeout)) + { + ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), ACE_TEXT ("schedule"))); + ++status; + } + + ACE_OS::sleep (10); + timer_queue.deactivate (); + timer_queue.wait (); + // Scheduling every second, waiting 10 seconds, should get at least 9 + int expirations = handler.nr_expirations (); + ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Caught %d timer expirations\n"), + expirations)); + if (expirations < 9) + ACE_ERROR ((LM_ERROR, ACE_TEXT ("Should have caught at least 9\n"))); + #else ACE_ERROR ((LM_ERROR, ACE_TEXT ("ACE doesn't support recursive condition variables on this platform\n"))); |