int
ACE_Event_Handler_Handle_Timeout_Upcall::
timeout (ACE_Timer_Queue &timer_queue,
        ACE_Event_Handler *event_handler,
        const void *act,
        int recurring_timer,
        const ACE_Time_Value &cur_time)
{
  int requires_reference_counting = 0;

  if (!recurring_timer)
    {
      requires_reference_counting =
        event_handler->reference_counting_policy ().value () ==
        ACE_Event_Handler::Reference_Counting_Policy::ENABLED;
    }

  // Upcall to the <handler>s handle_timeout method.
  if (event_handler->handle_timeout (cur_time, act) == -1)
    {
      if (event_handler->reactor_timer_interface ())
        event_handler->reactor_timer_interface ()->cancel_timer (event_handler, 0);
      else
        timer_queue.cancel (event_handler, 0); // 0 means "call handle_close()".
    }

  if (!recurring_timer &&
      requires_reference_counting)
    {
      event_handler->remove_reference ();
    }

  return 0;
}
void
expire (ACE_Timer_Queue &timer_queue,
        Expire_Function expire_function)
{
  int events = 0;
  int result = 0;

  Reference_Counted_Event_Handler *handler =
    new Reference_Counted_Event_Handler (1);

  ACE_Event_Handler_var safe_handler (handler);

  long timer_id =
    timer_queue.schedule (handler,
                          one_second_timeout,
                          ACE_Time_Value (1) + timer_queue.gettimeofday (),
                          ACE_Time_Value (1));
  ACE_ASSERT (timer_id != -1);

  result =
    timer_queue.schedule (handler,
                          two_second_timeout,
                          ACE_Time_Value (2) + timer_queue.gettimeofday ());
  ACE_ASSERT (result != -1);

  events += 4;

  for (int i = 0; i < events;)
    {
      WAIT_FOR_NEXT_EVENT (timer_queue);

      result =
        expire_function (timer_queue);

      ACE_ASSERT (result >= 0);

      i += result;
    }

  timer_queue.cancel (timer_id, 0, 0);
}
void
simple (ACE_Timer_Queue &timer_queue)
{
  int events = 0;
  int result = 0;
  long timer_id = -1;

  {
    Simple_Event_Handler *handler =
      new Simple_Event_Handler;

    timer_id =
      timer_queue.schedule (handler,
                            one_second_timeout,
                            ACE_Time_Value (1) + timer_queue.gettimeofday (),
                            ACE_Time_Value (1));
    ACE_ASSERT (timer_id != -1);

    result =
      timer_queue.cancel (timer_id,
                          0,
                          0);
    ACE_ASSERT (result == 1);
  }

  {
    Simple_Event_Handler *handler =
      new Simple_Event_Handler;

    timer_id =
      timer_queue.schedule (handler,
                            one_second_timeout,
                            ACE_Time_Value (1) + timer_queue.gettimeofday (),
                            ACE_Time_Value (1));
    ACE_ASSERT (timer_id != -1);

    events += 3;
  }

  for (int i = 0; i < events;)
    {
      WAIT_FOR_NEXT_EVENT (timer_queue);

      result =
        timer_queue.expire ();

      ACE_ASSERT (result >= 0);

      i += result;
    }

  timer_queue.cancel (timer_id, 0, 0);
}
int
invoke_one_upcall (ACE_Timer_Queue &timer_queue)
{
  // Get the current time
  ACE_Time_Value current_time (timer_queue.gettimeofday () +
                               timer_queue.timer_skew ());

  // Look for a node in the timer queue whose timer <= the present
  // time.
  ACE_Timer_Node_Dispatch_Info dispatch_info;

  if (timer_queue.dispatch_info (current_time,
                                 dispatch_info))
    {
      const void *upcall_act = 0;

      // Preinvoke.
      timer_queue.preinvoke (dispatch_info,
                             current_time,
                             upcall_act);

      // Call the functor
      timer_queue.upcall (dispatch_info,
                          current_time);

      // Postinvoke
      timer_queue.postinvoke (dispatch_info,
                              current_time,
                              upcall_act);

      // We have dispatched a timer
      return 1;
    }

  return 0;
}
示例#5
0
bool
LiveCheck::schedule_ping (LiveEntry *entry)
{
  if (!this->running_)
    return false;

  LiveStatus status = entry->status();
  if (status == LS_PING_AWAY || status == LS_DEAD)
    {
      return status != LS_DEAD;
    }

  ACE_Time_Value now (ACE_High_Res_Timer::gettimeofday_hr());
  ACE_Time_Value next = entry->next_check ();

  if (this->handle_timeout_busy_ > 0)
    {
      ACE_Time_Value delay = ACE_Time_Value::zero;
      if (next > now)
        {
          delay = next - now;
        }

      ACE_Timer_Queue *tq = this->reactor ()->timer_queue ();
      if (!tq->is_empty ())
        {
          for (ACE_Timer_Queue_Iterator_T<ACE_Event_Handler*> &i = tq->iter ();
               !i.isdone (); i.next())
            {
              if (i.item ()->get_type () == this)
                {
                  if (next >= tq->earliest_time ())
                    {
                      if (ImR_Locator_i::debug () > 2)
                        {
                          ORBSVCS_DEBUG ((LM_DEBUG,
                                          ACE_TEXT ("(%P|%t) LiveCheck::schedule_ping ")
                                          ACE_TEXT ("already scheduled\n")));
                        }
                      return true;
                    }
                  break;
                }
            }
        }
      ++this->token_;
      if (ImR_Locator_i::debug () > 2)
        {
          ORBSVCS_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t) LiveCheck::schedule_ping (%d),")
                          ACE_TEXT (" delay = %d,%d\n"),
                          this->token_, delay.sec(), delay.usec()));
        }
      this->reactor()->schedule_timer (this,
                                       reinterpret_cast<void *>(this->token_),
                                       delay);
    }
  else
    {
      if (ImR_Locator_i::debug () > 2)
        {
          ORBSVCS_DEBUG ((LM_DEBUG,
                          ACE_TEXT ("(%P|%t) LiveCheck::schedule_ping deferred")));
        }
      if (!this->want_timeout_ || next < this->deferred_timeout_)
        {
          this->want_timeout_ = true;
          this->deferred_timeout_ = next;
        }
    }
  return true;
}
int
invoke_expire (ACE_Timer_Queue &timer_queue)
{
  return timer_queue.expire ();
}
void
cancellation (ACE_Timer_Queue &timer_queue,
              int repeat_timer,
              int cancel_handler,
              int second_timer,
              int dont_call_handle_close)
{
  int result = 0;

  int expected_number_of_handle_close_calls = -1;

  if (!dont_call_handle_close)
    {
      if (cancel_handler)
        expected_number_of_handle_close_calls = 1;
      else if (second_timer)
        expected_number_of_handle_close_calls = 2;
      else
        expected_number_of_handle_close_calls = 1;
    }

  Reference_Counted_Event_Handler *handler =
    new Reference_Counted_Event_Handler (expected_number_of_handle_close_calls);

  ACE_Event_Handler_var safe_handler (handler);

  long first_timer_id = -1;
  long second_timer_id = -1;

  if (repeat_timer)
    {
      first_timer_id =
        timer_queue.schedule (handler,
                              one_second_timeout,
                              ACE_Time_Value (1) + timer_queue.gettimeofday (),
                              ACE_Time_Value (1));
      ACE_ASSERT (first_timer_id != -1);
    }
  else
    {
      first_timer_id =
        timer_queue.schedule (handler,
                              one_second_timeout,
                              ACE_Time_Value (1) + timer_queue.gettimeofday ());
      ACE_ASSERT (first_timer_id != -1);
    }

  if (second_timer)
    {
      second_timer_id =
        timer_queue.schedule (handler,
                              two_second_timeout,
                              ACE_Time_Value (2) + timer_queue.gettimeofday (),
                              ACE_Time_Value (2));
      ACE_ASSERT (second_timer_id != -1);
    }

  if (cancel_handler)
    {
      result =
        timer_queue.cancel (handler,
                            dont_call_handle_close);

      if (second_timer)
        ACE_ASSERT (result == 2);
      else
        ACE_ASSERT (result == 1);
    }
  else
    {
      result =
        timer_queue.cancel (first_timer_id,
                            0,
                            dont_call_handle_close);
      ACE_ASSERT (result == 1);

      if (second_timer)
        {
          result =
            timer_queue.cancel (second_timer_id,
                                0,
                                dont_call_handle_close);
          ACE_ASSERT (result == 1);
        }
    }
}