00001 #include "ace/Countdown_Time.h" 00002 #include "ace/OS_NS_sys_time.h" 00003 00004 ACE_RCSID (ace, 00005 Countdown_Time, 00006 "Countdown_Time.cpp,v 4.3 2005/10/28 16:14:52 ossama Exp") 00007 00008 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00009 00010 ACE_Countdown_Time::ACE_Countdown_Time (ACE_Time_Value *max_wait_time) 00011 : max_wait_time_ (max_wait_time), 00012 stopped_ (0) 00013 { 00014 this->start (); 00015 } 00016 00017 ACE_Countdown_Time::~ACE_Countdown_Time (void) 00018 { 00019 this->stop (); 00020 } 00021 00022 int 00023 ACE_Countdown_Time::start (void) 00024 { 00025 if (this->max_wait_time_ != 0) 00026 { 00027 this->start_time_ = ACE_OS::gettimeofday (); 00028 this->stopped_ = 0; 00029 } 00030 return 0; 00031 } 00032 00033 int 00034 ACE_Countdown_Time::stopped (void) const 00035 { 00036 return stopped_; 00037 } 00038 00039 int 00040 ACE_Countdown_Time::stop (void) 00041 { 00042 if (this->max_wait_time_ != 0 && this->stopped_ == 0) 00043 { 00044 ACE_Time_Value elapsed_time = 00045 ACE_OS::gettimeofday () - this->start_time_; 00046 00047 if (*this->max_wait_time_ > elapsed_time) 00048 *this->max_wait_time_ -= elapsed_time; 00049 else 00050 { 00051 // Used all of timeout. 00052 *this->max_wait_time_ = ACE_Time_Value::zero; 00053 // errno = ETIME; 00054 } 00055 this->stopped_ = 1; 00056 } 00057 return 0; 00058 } 00059 00060 int 00061 ACE_Countdown_Time::update (void) 00062 { 00063 return this->stop () == 0 && this->start (); 00064 } 00065 00066 ACE_END_VERSIONED_NAMESPACE_DECL