summaryrefslogtreecommitdiff
path: root/tests/Recursive_Condition_Test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Recursive_Condition_Test.cpp')
-rw-r--r--tests/Recursive_Condition_Test.cpp264
1 files changed, 110 insertions, 154 deletions
diff --git a/tests/Recursive_Condition_Test.cpp b/tests/Recursive_Condition_Test.cpp
index 6fa0eb56245..76dbb8c0abd 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/Log_Msg.h"
+#include "ace/Trace.h"
#include "ace/Thread_Manager.h"
#include "ace/Timer_Heap.h"
#include "ace/Timer_Queue_Adapters.h"
@@ -37,60 +37,39 @@ 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_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Test_Handler::handle_timeout\n")));
- ++this->nr_expirations_;
+ ACE_TRACE ("Test_Handler::handle_timeout");
- void *nc_arg = ACE_const_cast (void *, arg);
- Thread_Timer_Queue *timer_queue =
- ACE_reinterpret_cast (Thread_Timer_Queue *, nc_arg);
+ /*Thread_Timer_Queue *timer_queue =
+ (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 ("(%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")));
+ ACE_TEXT ("(%P|%t) scheduling timer\n")));
+ /*
+ int timer_id =
+ timer_queue->schedule (this, timer_queue, timeout);
+ */
return 0;
}
-
-private:
- int nr_expirations_;
};
-// These are for the basic functionality tests.
+// These are for the second test - simple wait/signal.
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;
}
@@ -98,28 +77,26 @@ 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 ("(%t) %p\n"),
- ACE_TEXT ("test 1 spawn")),
- 1);
+ {
+ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn")), 1);
+ }
ACE_OS::sleep (2);
if (mutex_.acquire () == -1)
- ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%t) %p\n"),
- ACE_TEXT ("test 1 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_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) signaling condition...\n")));
- protected_int = 0;
if (condition_.signal () == -1)
- 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")));
+ {
+ ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("signal")));
+ }
+ mutex_.release ();
ACE_Thread_Manager::instance ()->wait ();
return 0;
}
@@ -127,29 +104,27 @@ 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 ("(%t) %p\n"),
- ACE_TEXT ("test 2 spawn")),
- 1);
+ {
+ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn")), 1);
+ }
ACE_OS::sleep (2);
if (mutex_.acquire () == -1)
- ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%t) %p\n"),
- ACE_TEXT ("test 2 mutex acquire")),
- 1);
+ {
+ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
+ ACE_TEXT ("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 ("test 2 signal")));
+ {
+ ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("signal")));
+ }
- // Wait to clear protected_int to be sure cv properly reacquires the
- // mutex before returning control to caller.
ACE_OS::sleep(2);
- protected_int = 0;
- if (mutex_.release () == -1)
- ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"),
- ACE_TEXT ("test 2 release")));
-
+ mutex_.release ();
ACE_Thread_Manager::instance ()->wait ();
return 0;
}
@@ -157,26 +132,25 @@ test_2(void)
int
test_3()
{
- 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);
+ if (ACE_Thread_Manager::instance()->spawn_n (4,waiter) == -1)
+ {
+ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn")), 1);
+ }
ACE_OS::sleep (2);
if (mutex_.acquire () == -1)
- ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("(%t) %p\n"),
- ACE_TEXT ("test 3 mutex acquire")),
+ {
+ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
+ ACE_TEXT ("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 ("(%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_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("signal")));
+ }
+ mutex_.release ();
ACE_Thread_Manager::instance ()->wait ();
return 0;
@@ -185,52 +159,44 @@ 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 ("(%t) %p\n"),
- ACE_TEXT ("spawn")), 1);
+ {
+ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn")), 1);
+ }
ACE_OS::sleep (2);
- int i;
- for (i = 0; i < recurse_count; ++i)
+ 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)
{
- if (mutex_.acquire () == -1)
- {
- ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("pass %d, %p\n"),
- i + 1,
- ACE_TEXT ("recursive acquire")),
- 1);
- }
+ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT("%d\n"),
+ ACE_TEXT("get_nestling_level")),
+ 1);
}
- 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")));
+ ACE_TRACE (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)
{
- // 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 ();
+ ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("signal")));
}
- if (mutex_.get_nesting_level() != 0)
- ACE_ERROR_RETURN ((LM_ERROR,
- ACE_TEXT("(%t) nesting level %d; should be 0\n"),
- mutex_.get_nesting_level ()),
- 1);
+ 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_Thread_Manager::instance ()->wait ();
return 0;
}
@@ -242,57 +208,47 @@ 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);
- int status = 0;
+ ACE_OS::sleep (10);
+ timer_queue.deactivate ();
+ timer_queue.wait ();
+#endif
/* Test 1 - Simple test */
- ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Test 1...\n")));
if (test_1 () != 0)
- ++status;
+ {
+ ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT("test #1")), 1);
+ }
/* Test #2 - Sleep 2 seconds before releasing mutex */
- ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Test 2...\n")));
if (test_2 () != 0)
- ++status;
+ {
+ ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT("test #2")), 1);
+ }
/* Test #3 - One main thread - 4 subthreads */
- ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Test 3...\n")));
if (test_3 () != 0)
- ++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 ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), ACE_TEXT ("activate")));
- ++status;
+ ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT("test #3")), 1);
}
- ACE_Time_Value timeout =
- ACE_OS::gettimeofday() + ACE_Time_Value (1, 0);
-
- if (-1 == timer_queue.schedule (&handler, &timer_queue, timeout))
+ /* Test #4 - Multiple calls to mutex_.acquire and mutex_.release */
+ if (test_4 () != 0)
{
- ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%t) %p\n"), ACE_TEXT ("schedule")));
- ++status;
+ ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT("test #4")), 1);
}
- 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")));