summaryrefslogtreecommitdiff
path: root/ACE/ace/Countdown_Time.cpp
blob: dded80fc592f1e333033ffac8391e55a440fbba3 (plain)
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
// $Id$

#include "ace/Countdown_Time.h"

#if !defined (__ACE_INLINE__)
#include "ace/Countdown_Time.inl"
#endif /* __ACE_INLINE__ */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

ACE_Countdown_Time::ACE_Countdown_Time (ACE_Time_Value *max_wait_time,
                                        ACE_Time_Value (*gettimeofday)(void))
: max_wait_time_ (max_wait_time),
  stopped_ (false),
  gettimeofday_ (gettimeofday)
{
  this->start ();
}

ACE_Countdown_Time::~ACE_Countdown_Time (void)
{
  this->stop ();
}

void
ACE_Countdown_Time::start (void)
{
  if (this->max_wait_time_ != 0)
    {
      this->start_time_ = this->gettimeofday ();
      this->stopped_ = false;
    }
}

void
ACE_Countdown_Time::stop (void)
{
  if (this->max_wait_time_ != 0 && !this->stopped_)
    {
      ACE_Time_Value const elapsed_time =
        this->gettimeofday () - this->start_time_;

      if (elapsed_time >= ACE_Time_Value::zero &&
          *this->max_wait_time_ > elapsed_time)
        {
          *this->max_wait_time_ -= elapsed_time;
        }
      else
        {
          // Used all of timeout.
          *this->max_wait_time_ = ACE_Time_Value::zero;
          // errno = ETIME;
        }
      this->stopped_ = true;
    }
}

ACE_END_VERSIONED_NAMESPACE_DECL