Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Recursive_Condition_Bug_Test.cpp
blobc2e4c9a6f8e8c1dde01ef26dc64e67c9b8f41d5b
2 //=============================================================================
3 /**
4 * @file Recursive_Condition_Bug_Test.cpp
6 * This test program validates the functionality of the
7 * ACE_Condition<ACE_Recursive_Thread_Mutex> template
8 * specialization when combined with the
9 * ACE_Thread_Timer_Queue_Adapter on Win32 and Posix pthreads.
10 * It was added to test for bugs with the ACE_OS recursive
11 * condition implementation.
13 * @author Leonid Kvetnyi <leonidk@nice.com> and Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
15 //=============================================================================
17 #include "test_config.h"
18 #include "ace/OS_NS_sys_time.h"
19 #include "ace/Task_T.h"
20 #include "ace/Activation_Queue.h"
21 #include "ace/Timer_Heap.h"
22 #include "ace/Timer_Queue_Adapters.h"
23 #include "ace/Condition_Recursive_Thread_Mutex.h"
25 #if defined (ACE_HAS_THREADS)
27 // Number of iterations for the performance tests.
28 static int max_iterations = 30;
30 using Thread_Timer_Queue = ACE_Thread_Timer_Queue_Adapter<ACE_Timer_Heap>;
32 class Test_Handler;
34 class Test_Task : public ACE_Task<ACE_MT_SYNCH>
36 public:
37 int svc () override
39 while (--max_iterations > 0)
41 // dequeue the next object
42 ACE_Message_Block * mb = 0;
44 if (this->getq (mb) == -1)
45 ACE_ERROR_RETURN ((LM_ERROR,
46 ACE_TEXT ("(%t) %p\n"),
47 ACE_TEXT ("getq failed")),
48 -1);
50 Test_Handler *handler = reinterpret_cast<Test_Handler *> (mb->base ());
51 mb->release ();
53 ACE_Time_Value timeout = ACE_OS::gettimeofday () + ACE_Time_Value (1, 0);
55 if (timer_queue_.schedule (reinterpret_cast<ACE_Event_Handler *> (handler),
56 this,
57 timeout) == -1)
58 ACE_ERROR_RETURN ((LM_ERROR,
59 ACE_TEXT ("(%t) %p\n"),
60 ACE_TEXT ("schedule failed")),
61 -1);
63 ACE_DEBUG ((LM_INFO,
64 ACE_TEXT ("(%t) %d iteration(s) remain\n"),
65 max_iterations));
68 timer_queue_.deactivate ();
69 ACE_DEBUG ((LM_INFO,
70 ACE_TEXT ("(%t) leaving the Test_task\n")));
71 return 0;
74 //FUZZ: disable check_for_lack_ACE_OS
75 int open (void * = 0) override
77 //FUZZ: enable check_for_lack_ACE_OS
78 if (ACE_Task<ACE_MT_SYNCH>::activate (THR_NEW_LWP, 1) != 0)
79 ACE_ERROR_RETURN ((LM_ERROR,
80 ACE_TEXT ("(%t) %p\n"),
81 ACE_TEXT ("Test_Task::activate")),
82 -1);
83 if (0 != timer_queue_.activate ())
84 ACE_ERROR_RETURN ((LM_ERROR,
85 ACE_TEXT ("(%t) %p\n"),
86 ACE_TEXT ("Test_Task::queue activate")),
87 -1);
88 return 0;
91 private:
92 Thread_Timer_Queue timer_queue_;
95 class Test_Handler : public ACE_Event_Handler
97 public:
98 int handle_timeout (const ACE_Time_Value &,
99 const void *arg) override
101 ACE_DEBUG ((LM_DEBUG,
102 ACE_TEXT ("(%t) Test_Handler::handle_timeout\n")));
104 void *nc_arg = const_cast<void *> (arg);
105 Test_Task *test_task =
106 reinterpret_cast<Test_Task *> (nc_arg);
107 ACE_Message_Block *mb = 0;
108 ACE_NEW_MALLOC_RETURN (mb,
109 static_cast<ACE_Message_Block *> (ACE_Allocator::instance()->malloc (sizeof (ACE_Message_Block))),
110 ACE_Message_Block (sizeof (*this), // size
111 ACE_Message_Block::MB_DATA, // type
112 0, // cont
113 (char *) this, // data
116 ACE_DEFAULT_MESSAGE_BLOCK_PRIORITY,
117 ACE_Time_Value::zero,
118 ACE_Time_Value::max_time,
120 ACE_Allocator::instance()), // data
121 -1);
123 test_task->putq (mb);
124 return 0;
128 #endif /* ACE_HAS_THREADS */
131 run_main (int, ACE_TCHAR *[])
133 ACE_START_TEST (ACE_TEXT ("Recursive_Condition_Bug_Test"));
135 #if defined (ACE_HAS_THREADS)
137 // Timer queue usage.
139 Test_Handler handler;
140 Test_Task task;
142 if (0 != task.open ())
143 ACE_ERROR_RETURN ((LM_ERROR,
144 ACE_TEXT ("(%t) %p\n"),
145 ACE_TEXT ("open")),
146 -1);
148 ACE_Message_Block *mb = 0;
149 ACE_NEW_MALLOC_RETURN (mb,
150 static_cast<ACE_Message_Block *> (ACE_Allocator::instance()->malloc (sizeof (ACE_Message_Block))),
151 ACE_Message_Block (sizeof (handler), // size
152 ACE_Message_Block::MB_DATA, // type
153 0, // cont
154 (char *) &handler,
157 ACE_DEFAULT_MESSAGE_BLOCK_PRIORITY,
158 ACE_Time_Value::zero,
159 ACE_Time_Value::max_time,
161 ACE_Allocator::instance()), // data
162 -1);
164 if (-1 == task.putq (mb))
165 ACE_ERROR_RETURN ((LM_ERROR,
166 ACE_TEXT ("(%t) %p\n"),
167 ACE_TEXT ("putq")),
168 -1);
170 if (ACE_Thread_Manager::instance ()->wait () == -1)
171 ACE_ERROR_RETURN ((LM_ERROR,
172 "wait on Thread_Manager failed"),
173 -1);
174 #else
175 ACE_ERROR ((LM_ERROR,
176 ACE_TEXT ("ACE doesn't support recursive condition variables on this platform\n")));
177 #endif /* ACE_HAS_THREADS */
178 ACE_END_TEST;
179 return 0;