diff options
Diffstat (limited to 'tests/Semaphore_Test.cpp')
-rw-r--r-- | tests/Semaphore_Test.cpp | 71 |
1 files changed, 42 insertions, 29 deletions
diff --git a/tests/Semaphore_Test.cpp b/tests/Semaphore_Test.cpp index 8b38fd352fd..a00f2aebe87 100644 --- a/tests/Semaphore_Test.cpp +++ b/tests/Semaphore_Test.cpp @@ -9,11 +9,11 @@ // Semaphore Test // // = DESCRIPTION -// This test verifies the functionality of the ACE_Thread_Semaphore +// This test verifies the functionality of the <ACE_Thread_Semaphore> // implementation. // // = AUTHOR -// Darrell Brunsch +// Darrell Brunsch <brunsch@cs.wustl.edu> // // ============================================================================ @@ -25,10 +25,10 @@ ACE_RCSID(tests, Semaphore_Test, "$Id$") -#if defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 -USELIB("..\ace\aced.lib"); +#if defined (__BORLANDC__) && __BORLANDC__ >= 0x0530 +USELIB ("..\ace\aced.lib"); //--------------------------------------------------------------------------- -#endif /* defined(__BORLANDC__) && __BORLANDC__ >= 0x0530 */ +#endif /* defined (__BORLANDC__) && __BORLANDC__ >= 0x0530 */ // msec that times are allowed to differ before test fails. #define ACE_ALLOWED_SLACK 100 @@ -52,7 +52,7 @@ static size_t n_workers = 10; static size_t n_release_count = 3; #if !defined (ACE_HAS_STHREADS) -// Number of times to call test_timeout(). +// Number of times to call test_timeout (). static size_t test_timeout_count = 3; // Number of timeouts. @@ -99,40 +99,44 @@ parse_args (int argc, ASYS_TCHAR *argv[]) static int test_timeout (void) { - int status = 0; - long msecs_expected, msecs_waited, msecs_diff; // milliseconds + // milliseconds... + long msecs_expected; + long msecs_waited; + long msecs_diff; // Wait a little longer each time - static long wait_secs = 3; + static long wait_secs = 3; ACE_Time_Value wait = ACE_OS::gettimeofday (); ACE_Time_Value begin = wait; - wait.sec(wait.sec() + wait_secs); + wait.sec (wait.sec () + wait_secs); if (s.acquire (wait) == -1) ACE_ASSERT (errno == ETIME); ACE_Time_Value wait_diff = ACE_OS::gettimeofday () - begin; - msecs_waited = wait_diff.msec(); + msecs_waited = wait_diff.msec (); msecs_expected = wait_secs * 1000; - msecs_diff = labs(msecs_expected - msecs_waited); + msecs_diff = labs (msecs_expected - msecs_waited); + if (msecs_diff > ACE_ALLOWED_SLACK) { - ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("Timed wait fails length test\n"))); - ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("Value: %d ms, actual %d ms\n"), - msecs_expected, msecs_waited)); + ACE_DEBUG ((LM_DEBUG, + ASYS_TEXT ("Timed wait fails length test\n"))); + ACE_DEBUG ((LM_DEBUG, + ASYS_TEXT ("Value: %d ms, actual %d ms\n"), + msecs_expected, + msecs_waited)); status = -1; } ++wait_secs; - return status; - } // Worker tries to acquire the semaphore, hold it for a while, and @@ -145,27 +149,29 @@ worker (void *) iterations <= n_iterations; iterations++) { - ACE_Time_Value wait(0, iterations * 1000 * 100); // Wait 'iter' msec - ACE_Time_Value tv = ACE_OS::gettimeofday() + wait; + ACE_Time_Value wait (0, + iterations * 1000 * 100); // Wait 'iter' msec + ACE_Time_Value tv = ACE_OS::gettimeofday () + wait; if (s.acquire (tv)) ++timeouts; else { - ACE_Time_Value diff = ACE_OS::gettimeofday(); + ACE_Time_Value diff = ACE_OS::gettimeofday (); diff = diff - tv; // tv should have been reset to time acquired - if (diff.msec() > ACE_ALLOWED_SLACK) + if (diff.msec () > ACE_ALLOWED_SLACK) { ACE_DEBUG ((LM_DEBUG, - ASYS_TEXT("Acquire fails time reset test\n"))); + ASYS_TEXT ("Acquire fails time reset test\n"))); ACE_DEBUG ((LM_DEBUG, - ASYS_TEXT("Diff btw now and returned time: %d ms\n"), - diff.msec())); + ASYS_TEXT ("Diff btw now and returned time: %d ms\n"), + diff.msec ())); test_result = 1; } // Hold the lock for a while. - ACE_OS::sleep (ACE_Time_Value (0, (ACE_OS::rand () % 1000) * 1000)); + ACE_OS::sleep (ACE_Time_Value (0, + (ACE_OS::rand () % 1000) * 1000)); s.release (); } @@ -178,7 +184,7 @@ worker (void *) #endif /* !ACE_HAS_STHREADS && !ACE_HAS_POSIX_SEM */ #endif /* ACE_HAS_THREADS */ -// Test semaphore functionality +// Test semaphore functionality. int main (int argc, ASYS_TCHAR *argv[]) { @@ -198,15 +204,22 @@ int main (int argc, ASYS_TCHAR *argv[]) s.release (n_release_count); if (ACE_Thread_Manager::instance ()->spawn_n - (n_workers, ACE_THR_FUNC (worker), 0, THR_NEW_LWP) == -1) - ACE_ERROR_RETURN ((LM_ERROR, ASYS_TEXT ("%p\n"), ASYS_TEXT ("spawn_n")), 1); + (n_workers, + ACE_THR_FUNC (worker), + 0, + THR_NEW_LWP) == -1) + ACE_ERROR_RETURN ((LM_ERROR, + ASYS_TEXT ("%p\n"), + ASYS_TEXT ("spawn_n")), + 1); ACE_Thread_Manager::instance ()->wait (); size_t percent = (timeouts * 100) / (n_workers * n_iterations); ACE_DEBUG ((LM_DEBUG, - ASYS_TEXT ("Worker threads timed out %d percent of the time\n"), percent)); + ASYS_TEXT ("Worker threads timed out %d percent of the time\n"), + percent)); #else ACE_ERROR ((LM_ERROR, ASYS_TEXT ("Timed semaphores are not supported with native Solaris threads or on POSIX semaphores\n"))); |