int run_main (int argc, ACE_TCHAR *argv[])
{
  ACE_START_TEST (ACE_TEXT ("Monotonic_Manual_Event_Test"));

#if defined (ACE_HAS_THREADS)
  ACE_Manual_Event_T<ACE_Monotonic_Time_Policy> monotonic_event (0);
  ACE_Manual_Event systime_event (0);
  ACE_Time_Value_T<ACE_Monotonic_Time_Policy> tm_mono;
  ACE_Time_Value tm_sys;

  parse_args (argc, argv);

  //FUZZ: disable check_for_lack_ACE_OS
  ACE_Time_Value wait (7, 0);   // Wait 7 sec
  //FUZZ: enable check_for_lack_ACE_OS

  if (use_monotonic)
    {
      evt = &monotonic_event;
      tm_mono = tm_mono.now () + wait;
      initial_timeout = &tm_mono;
    }
  else
    {
      evt = &systime_event;
      tm_sys = ACE_OS::gettimeofday () + wait;
      initial_timeout = &tm_sys;
    }

  // shift forward in time 3 sec; this will mess up timeouts if
  // monotonic timer is not used
  ACE_Time_Value tv_shift (3, 0);
  set_system_time (ACE_OS::gettimeofday () + tv_shift);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("spawning workers - initial timeout till %#T\n"), initial_timeout));

  if (ACE_Thread_Manager::instance ()->spawn_n
      (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);

  // gives all workers chance to start
  ACE_OS::sleep (5);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("finished waiting for workers to start\n")));

  // restore time (workers will already have failed without monotonic timer)
  set_system_time (ACE_OS::gettimeofday () - tv_shift);

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("sending pulse ()\n")));

  // Release the all workers.
  if (evt->pulse () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("pulse")),
                      1);

  // Wait 2 sec
  ACE_OS::sleep (2);

  //FUZZ: disable check_for_lack_ACE_OS
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("sending signal ()\n")));
  //FUZZ: enable check_for_lack_ACE_OS

  // Signal
  if (evt->signal () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       ACE_TEXT ("%p\n"),
                       ACE_TEXT ("signal")),
                      1);

  ACE_Thread_Manager::instance ()->wait ();
#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;
}
Esempio n. 2
0
int
run_main (int , ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Monotonic_Task_Test"));

  int status = 0;

# if defined (ACE_HAS_THREADS)
  MyTask my_task;

  if (my_task.start () == 0)
    {
      // shift back in time 4 sec; this would mess up timeouts if
      // monotonic timer was not used
      ACE_Time_Value tv_shift (4, 0);
      set_system_time (ACE_OS::gettimeofday () - tv_shift);

      if (my_task.put_message () == 0)
        {
          // task should now have finished dequeueing and started waiting for stop signal
          // wait (2sec) on thread manager should timeout

                                                 // use the time policy aware gettimeofday()
                                                 // method of the task to get current time
          ACE_Time_Value_T<ACE_Monotonic_Time_Policy> tv (my_task.gettimeofday ());
          tv += ACE_Time_Value (2, 0);

          // shift another 3 sec back in time; without monotonic timer support in
          // thread manager this would mess up the timed wait
          tv_shift += ACE_Time_Value (3, 0);
          set_system_time (ACE_OS::gettimeofday () - ACE_Time_Value (3,0));

          if (my_task.thr_mgr ()->wait (&tv) == 0)
            {
              ACE_ERROR ((LM_ERROR, ACE_TEXT ("Thread manager did not time out\n")));
              status = 1;
            }
          else
            {
              ACE_Time_Value_T<ACE_Monotonic_Time_Policy> tv_now (my_task.gettimeofday ());

              ACE_DEBUG ((LM_INFO, ACE_TEXT ("Thread manager timed out at %#T\n"), &tv_now));
            }
        }
      else
        status = 1;

      // ok, now stop task
      if (my_task.stop () != 0)
        {
          ACE_ERROR ((LM_ERROR, ACE_TEXT ("Failed to stop task\n")));
          status = 1;
        }

      // restore time
      set_system_time (ACE_OS::gettimeofday () + tv_shift);
    }
  else
    status = 1;

# endif /* ACE_HAS_THREADS */

  ACE_END_TEST;
  return status;
}