static void * worker (void *) { for (int iterations = 1; iterations <= n_iterations; iterations++) { //FUZZ: disable check_for_lack_ACE_OS ACE_Time_Value wait (0, iterations * 1000 * 100); // Wait 'iter' msec //FUZZ: enable check_for_lack_ACE_OS ACE_Time_Value tv = ACE_OS::gettimeofday () + wait; if (s.acquire (tv) == -1) { // verify that we have ETIME if (ACE_OS::last_error() != ETIME) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("Worker should be ETIME but is"))); } else ++timeouts; ACE_Time_Value diff = ACE_OS::gettimeofday (); diff = diff - tv; // tv should have been reset to time acquired long diff_msec = diff.msec (); if (diff_msec > ACE_ALLOWED_SLACK) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Acquire fails time reset test\n"))); ACE_ERROR ((LM_ERROR, ACE_TEXT ("Diff btw now and returned time: %d ms; ") ACE_TEXT ("%d allowed\n"), (int)diff_msec, (int)ACE_ALLOWED_SLACK)); test_result = 1; } // Hold the lock for a while. ACE_OS::sleep (ACE_Time_Value (0, (ACE_OS::rand () % 1000) * 1000)); s.release (); } ACE_Thread::yield (); } return 0; }
static void * worker (void *) { for (int iterations = 1; iterations <= n_iterations; iterations++) { #if !defined (ACE_HAS_STHREADS) && !defined (ACE_HAS_POSIX_SEM) 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 (); diff = diff - tv; // tv should have been reset to time acquired if (diff.msec () > ACE_ALLOWED_SLACK) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Acquire fails time reset test\n"))); ACE_DEBUG ((LM_DEBUG, ACE_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)); s.release (); } #else s.acquire (); // Hold the lock for a while. ACE_OS::sleep (ACE_Time_Value (0, (ACE_OS::rand () % 1000) * 1000)); s.release (); #endif /* ACE_HAS_STHREADS && ACE_HAS_POSIX_SEM */ ACE_Thread::yield (); } return 0; }
// Tests the amount of time spent in a timed wait. static int test_timeout (void) { int status = 0; // milliseconds... long msecs_expected; long msecs_waited; long msecs_diff; // Wait a little longer each time static long wait_secs = 3; ACE_Time_Value wait = ACE_OS::gettimeofday (); ACE_Time_Value begin = wait; wait.sec (wait.sec () + wait_secs); if (s.acquire (wait) == -1) { if (errno != ETIME) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("test_timeout should be ETIME but is"))); status = -1; } } ACE_Time_Value wait_diff = ACE_OS::gettimeofday () - begin; msecs_waited = wait_diff.msec (); msecs_expected = wait_secs * 1000; msecs_diff = labs (msecs_expected - msecs_waited); if (msecs_diff > ACE_ALLOWED_SLACK) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Timed wait fails length test\n"))); ACE_ERROR ((LM_ERROR, ACE_TEXT ("Expected %d ms, actual %d ms; %d allowed\n"), (int)msecs_expected, (int)msecs_waited, (int)ACE_ALLOWED_SLACK)); status = -1; } ++wait_secs; return status; }
int MyTask::svc (void) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" (%t) MyTask started\n"))); // signal that we are ready sem_.release (1); while (ACE_Reactor::instance()->reactor_event_loop_done () == 0) ACE_Reactor::instance()->run_reactor_event_loop (); ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" (%t) MyTask finished\n"))); return 0; }
int run_main (int argc, ACE_TCHAR *argv[]) { ACE_START_TEST (ACE_TEXT ("Semaphore_Test")); #if defined (ACE_HAS_THREADS) parse_args (argc, argv); ACE_OS::srand (ACE_OS::time (0L)); # if !defined (ACE_HAS_STHREADS) && !defined (ACE_HAS_POSIX_SEM) //Test timed waits. for (size_t i = 0; i < test_timeout_count; i++) if (test_timeout () != 0) test_result = 1; # endif /* ACE_HAS_STHREADS && ACE_HAS_POSIX_SEM */ // Release the semaphore a certain number of times. s.release (n_release_count); if (ACE_Thread_Manager::instance ()->spawn_n (ACE_static_cast (size_t, n_workers), ACE_THR_FUNC (worker), 0, THR_NEW_LWP) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn_n")), 1); ACE_Thread_Manager::instance ()->wait (); # if !defined (ACE_HAS_STHREADS) && !defined (ACE_HAS_POSIX_SEM) size_t percent = (timeouts * 100) / (n_workers * n_iterations); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Worker threads timed out %d percent of the time\n"), percent)); # endif /* ACE_HAS_STHREADS && ACE_HAS_POSIX_SEM */ ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Semaphore Test successful\n"))); #else ACE_UNUSED_ARG (argc); ACE_UNUSED_ARG (argv); ACE_ERROR ((LM_INFO, ACE_TEXT ("Threads not supported on this platform\n"))); #endif /* ACE_HAS_THREADS */ ACE_END_TEST; return test_result; }
// Tests the amount of time spent in a timed wait. static int test_timeout (void) { int status = 0; // milliseconds... long msecs_expected; long msecs_waited; long msecs_diff; // Wait a little longer each time static long wait_secs = 3; ACE_Time_Value wait = ACE_OS::gettimeofday (); ACE_Time_Value begin = wait; 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_expected = wait_secs * 1000; msecs_diff = labs (msecs_expected - msecs_waited); if (msecs_diff > ACE_ALLOWED_SLACK) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Timed wait fails length test\n"))); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Value: %d ms, actual %d ms\n"), msecs_expected, msecs_waited)); status = -1; } ++wait_secs; return status; }
int MyTask::start (int num_threads) { if (this->create_reactor () == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p.\n"), ACE_TEXT ("unable to create reactor")), -1); if (this->activate (THR_NEW_LWP, num_threads) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p.\n"), ACE_TEXT ("unable to activate thread pool")), -1); for (; num_threads > 0 ; num_threads--) sem_.acquire (); return 0; }
int Synchronized_Suspend_Resume_Test::svc () { #if ACE_DEBUG_CST > 0 ACE_DEBUG ((LM_DEBUG, "Synchronized_Suspend_Resume_Test::svc (), entering")); ACE_hthread_t thread_id; ACE_Thread_Manager::instance ()->thr_self (thread_id); ACE_DEBUG ((LM_DEBUG, "; thread ID is %u\n", thread_id)); #endif /* ACE_DEBUG_CST */ { Mutex_Acquire_Release_Test mutex_acquire_release_test (num_iterations); mutex_acquire_release_test.svc (); mutex_acquire_release_time_ = static_cast<ACE_UINT32> (mutex_acquire_release_test.elapsed_time () / num_iterations); #if ACE_DEBUG_CST > 0 ACE_DEBUG ((LM_DEBUG, "mutex_acquire_release: %u nsec\n", mutex_acquire_release_time_)); #endif /* ACE_DEBUG_CST */ } high_.ready (); #if ACE_DEBUG_CST > 0 int priority, high_priority; ACE_OS::thr_getprio (thread_id, priority); ACE_OS::thr_getprio (high_.thread_id (), high_priority); ACE_DEBUG ((LM_DEBUG, "Synchronized_Suspend_Resume_Test::svc (), priority is %d, " ", high thread priority is %d\n", priority, high_priority)); #endif /* ACE_DEBUG_CST */ // For information: the cost of the just the loop itself below, // without the suspend and resume calls, on a 166 MHz Ultrasparc // is about 12.3 nanoseconds per iteration. ACE_UINT32 i; for (i = 0; i < iterations_; ++i) { #if ACE_DEBUG_CST > 0 if (i % (iterations_ >= 10 ? iterations_ / 10 : 1) == 0) { ACE_DEBUG ((LM_DEBUG, "Synchronized_Suspend_Resume_Test::svc (), iteration " "%d, continue high-priority thread %u\n", i, high_.thread_id ())); } #endif /* ACE_DEBUG_CST */ { // Acquire the mutex so that the high priority thread will // block after we signal it via the condition variable. ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, mutex_, -1); // Release the semaphore so that the high priority thread can // proceed. if (sem_.release () != 0) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "sem_.release"), -1); timer_.start (); // Release the mutex so that the high priority thread can // proceed. The ACE_GUARD_RETURN macro implicity releases // the mutex. } } high_.done (); // The high priority thread will be block on the semaphore, so // release it. if (sem_.release () != 0) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "sem_.release"), -1); #if ACE_DEBUG_CST > 0 ACE_DEBUG ((LM_DEBUG, "Synchronized_Suspend_Resume_Test::svc: told high priority " "task to terminate\n")); #endif /* ACE_DEBUG_CST */ // Resume the thread until thr_continue fails, indicating that it has // finished. for (i = 0; i < 10000 && ! ACE_OS::thr_continue (high_.thread_id ()); ++i) /* null */; #if ACE_DEBUG_CST > 0 ACE_DEBUG ((LM_DEBUG, "Synchronized_Suspend_Resume_Test::svc, finishing\n")); #endif /* ACE_DEBUG_CST */ return 0; }