1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
//=============================================================================
/**
* @file Recursive_Condition_Test.cpp
*
* $Id: Recursive_Condition_Test.cpp 93638 2011-03-24 13:16:05Z johnnyw $
*
* This test program validates the functionality of the
* ACE_Condition<ACE_Recursive_Thread_Mutex> template
* specialization.
*
*
* @author Stephen Howard <stephen.e.howard@lmco.com> and Douglas C. Schmidt <schmidt@cs.wustl.edu>
*/
//=============================================================================
#include "test_config.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_sys_time.h"
#include "ace/Event_Handler.h"
#include "ace/Log_Msg.h"
#include "ace/Thread_Manager.h"
#include "ace/Timer_Heap.h"
#include "ace/Timer_Queue_Adapters.h"
#if defined (ACE_HAS_THREADS)
typedef ACE_Thread_Timer_Queue_Adapter<ACE_Timer_Heap> Thread_Timer_Queue;
class Test_Handler : public ACE_Event_Handler
{
public:
Test_Handler (void) : nr_expirations_ (0) {}
int nr_expirations (void) { return this->nr_expirations_; }
virtual int handle_timeout (const ACE_Time_Value &,
const void *arg)
{
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%t) Test_Handler::handle_timeout\n")));
++this->nr_expirations_;
void *nc_arg = const_cast<void *> (arg);
Thread_Timer_Queue *timer_queue =
reinterpret_cast<Thread_Timer_Queue *> (nc_arg);
ACE_Time_Value timeout = ACE_OS::gettimeofday () + ACE_Time_Value (1, 0);
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")));
return 0;
}
private:
int nr_expirations_;
};
// These are for the basic functionality tests.
ACE_SYNCH_RECURSIVE_MUTEX mutex_;
ACE_Condition<ACE_SYNCH_RECURSIVE_MUTEX> condition_ (mutex_);
// Test driver sets this to non-zero before spawning and to zero for
// waiter.
int protected_int = 0;
static 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 const 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")));
return 0;
}
static 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_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_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_Thread_Manager::instance ()->wait ();
return 0;
}
static 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_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_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")));
// 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")));
ACE_Thread_Manager::instance ()->wait ();
return 0;
}
static int
test_3 (void)
{
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 ("(%t) %p\n"),
ACE_TEXT ("test 3 mutex acquire")),
1);
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_Thread_Manager::instance ()->wait ();
return 0;
}
static int
test_4 (void)
{
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_OS::sleep (2);
int i;
for (i = 0; i < recurse_count; ++i)
if (mutex_.acquire () == -1)
ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("pass %d, %p\n"),
i + 1,
ACE_TEXT ("recursive acquire")),
1);
// Don't report a failure if the mutex doesn't offer a view of the
// recursion count.
int nesting_level = mutex_.get_nesting_level ();
if (!(nesting_level == -1 && errno == ENOTSUP) && nesting_level != i)
ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT("test 4 nesting level %d;")
ACE_TEXT (" should be %d\n"),
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)
{
// 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 ();
}
// The waiter thread will acquire the mutex as a result of the releases
// above... don't check the nesting level until waiter() has had a chance
// to wake up, acquire, and release the mutex.
ACE_Thread_Manager::instance ()->wait ();
nesting_level = mutex_.get_nesting_level ();
if (!(nesting_level == -1 && errno == ENOTSUP) && nesting_level != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT("(%t) nesting level %d; should be 0\n"),
nesting_level),
1);
return 0;
}
#endif /* ACE_HAS_THREADS */
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Recursive_Condition_Test"));
#if defined (ACE_HAS_THREADS)
int status = 0;
/* Test 1 - Simple test */
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Test 1...\n")));
if (test_1 () != 0)
++status;
/* Test #2 - Sleep 2 seconds before releasing mutex */
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Test 2...\n")));
if (test_2 () != 0)
++status;
/* 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_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_DEBUG ((LM_DEBUG, ACE_TEXT ("Starting to sleep\n")));
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")));
#endif /* ACE_HAS_THREADS */
ACE_END_TEST;
return 0;
}
|