LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/base - timer.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 118 129 91.5 %
Date: 2026-11-16 02:06:09 Functions: 16 17 94.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include "base/timer.h"
       7             : #include "base/timer_impl.h"
       8             : 
       9             : class Timer::TimerTask : public Task {
      10             : public:
      11       58340 :     TimerTask(TimerPtr timer, boost::system::error_code ec)
      12       58340 :         : Task(timer->task_id_, timer->task_instance_), timer_(timer), ec_(ec) {
      13       58340 :     }
      14             : 
      15      116680 :     virtual ~TimerTask() {
      16      116680 :     }
      17             : 
      18             :     // Invokes user callback.
      19             :     // Timer could have been cancelled or delete when task was enqueued
      20       58166 :     virtual bool Run() {
      21             :         {
      22       58166 :             std::unique_lock<std::mutex> lock(timer_->mutex_);
      23             : 
      24             :             // cancelled task .. ignore
      25       58163 :             if (task_cancelled()) {
      26             :                 // Cancelled timer's task releases the ownership of the timer
      27           3 :                 lock.unlock();
      28           3 :                 timer_ = NULL;
      29           3 :                 return true;
      30             :             }
      31             : 
      32             :             // Conditions to invoke user callback met. Fire it
      33       58109 :             timer_->SetState(Timer::Fired);
      34       58145 :         }
      35             : 
      36       58163 :         bool restart = false;
      37             : 
      38             :         // TODO: Is this error needed by user?
      39       58163 :         if (ec_ && !timer_->error_handler_.empty()) {
      40           0 :             timer_->error_handler_(timer_->name_,
      41           0 :                                    std::string(ec_.category().name()),
      42           0 :                                    ec_.message());
      43             :         } else {
      44       58163 :             restart = timer_->handler_();
      45             :         }
      46             : 
      47       58213 :         OnTaskCancel();
      48             : 
      49       58145 :         if (restart) {
      50       66189 :             timer_->Start(timer_->time_, timer_->handler_,
      51       33117 :                           timer_->error_handler_);
      52       25008 :         } else if (timer_->delete_on_completion_) {
      53         165 :             TimerManager::DeleteTimer(timer_.get());
      54             :         }
      55       58233 :         return true;
      56             :     }
      57             : 
      58             :     // Task Cancelled/Destroyed when it was Fired.
      59       58203 :     void OnTaskCancel() {
      60       58203 :         if (!timer_) {
      61           3 :             return;
      62             :         }
      63       58178 :         std::scoped_lock lock(timer_->mutex_);
      64             : 
      65       58092 :         if (timer_->timer_task_ != this) {
      66           0 :             assert(!timer_->timer_task_);
      67             :         }
      68             : 
      69       58028 :         timer_->timer_task_ = NULL;
      70       57992 :         timer_->SetState(Timer::Init);
      71       57973 :     }
      72             : 
      73           0 :     virtual std::string Description() const {
      74           0 :         return timer_->Description();
      75             :     }
      76             : 
      77             : private:
      78             :     TimerPtr timer_;
      79             :     boost::system::error_code ec_;
      80             :     DISALLOW_COPY_AND_ASSIGN(TimerTask);
      81             : };
      82             : 
      83      379763 : Timer::Timer(boost::asio::io_context &service, const std::string &name,
      84      379763 :           int task_id, int task_instance, bool delete_on_completion)
      85      379764 :         : impl_(new TimerImpl(service)),
      86      379762 :           name_(name),
      87      379763 :           handler_(NULL),
      88      379762 :           error_handler_(NULL),
      89      379762 :           state_(Init),
      90      379762 :           timer_task_(NULL),
      91      379762 :           time_(0),
      92      379762 :           task_id_(task_id),
      93      379762 :           task_instance_(task_instance),
      94      379762 :           seq_no_(0),
      95      759525 :           delete_on_completion_(delete_on_completion) {
      96      379762 :     refcount_ = 0;
      97      379764 : }
      98             : 
      99      756632 : Timer::~Timer() {
     100      378316 :     assert(state_ != Running && state_ != Fired);
     101      756632 : }
     102             : 
     103             : //
     104             : // Start a timer
     105             : //
     106             : // If the timer is already running, return silently
     107             : //
     108     4766345 : bool Timer::Start(int time, Handler handler, ErrorHandler error_handler) {
     109     4766345 :     std::scoped_lock lock(mutex_);
     110             : 
     111     4766502 :     if (time < 0) {
     112         138 :         return true;
     113             :     }
     114             : 
     115     4766364 :     if (state_ == Running || state_ == Fired) {
     116      175735 :         return true;
     117             :     }
     118             : 
     119             :     // Restart the timer
     120     4590629 :     time_ = time;
     121     4590629 :     handler_ = handler;
     122     4589876 :     seq_no_++;
     123     4589876 :     error_handler_ = error_handler;
     124     4589665 :     boost::system::error_code ec;
     125     4589665 :     impl_->expires_from_now(time, ec);
     126     4590665 :     if (ec) {
     127           0 :         return false;
     128             :     }
     129             : 
     130     4590648 :     SetState(Running);
     131     9181251 :     impl_->async_wait(
     132     9181341 :         boost::bind(&Timer::StartTimerTask, this, TimerPtr(this),
     133             :                     time, seq_no_, boost::asio::placeholders::error));
     134     4590748 :     return true;
     135     4766621 : }
     136             : 
     137       10453 : bool Timer::Reschedule(int time)
     138             : {
     139       10453 :     if (state_ != Fired)
     140         183 :         return false;
     141             : 
     142       10270 :     if (time < 0)
     143           0 :         return false;
     144             : 
     145       10270 :     time_ = time;
     146       10270 :     return true;
     147             : }
     148             : 
     149             : // Cancel a running timer
     150     5452696 : bool Timer::Cancel() {
     151     5452696 :     std::scoped_lock lock(mutex_);
     152             : 
     153             :     // A fired timer cannot be cancelled
     154     5452686 :     if (state_ == Fired) {
     155        4693 :         return false;
     156             :     }
     157             : 
     158             :     // Cancel Task. If Task cancel succeeds, there will be no callback.
     159             :     // Reset TaskRef if call succeeds.
     160     5447993 :     if (timer_task_) {
     161             :         TaskScheduler::CancelReturnCode rc =
     162         105 :             TaskScheduler::GetInstance()->Cancel(timer_task_);
     163         105 :         assert(rc != TaskScheduler::FAILED);
     164         105 :         timer_task_ = NULL;
     165             :     }
     166             : 
     167     5447993 :     SetState(Cancelled);
     168     5447947 :     return true;
     169     5452640 : }
     170             : 
     171             : // ASIO callback on timer expiry. Start a task to serve the timer
     172     4511106 : void Timer::StartTimerTask(TimerPtr reference, int time, uint32_t seq_no,
     173             :                            const boost::system::error_code &ec) {
     174     4511106 :     std::scoped_lock lock(mutex_);
     175             : 
     176     4511107 :     if (state_ == Cancelled) {
     177       25142 :         return;
     178             :     }
     179             : 
     180             :     // If timer was cancelled, no callback is invoked
     181     4485965 :     if (ec && ec.value() == boost::asio::error::operation_aborted) {
     182     4427625 :         return;
     183             :     }
     184             : 
     185             :     // Timer could have fired for previous run. Validate the seq_no_
     186       58340 :     if (seq_no_ != seq_no) {
     187           0 :         return;
     188             :     }
     189             :     // Start a task and add Task reference.
     190       58340 :     assert(timer_task_ == NULL);
     191       58340 :     timer_task_ = new TimerTask(reference, ec);
     192       58340 :     TaskScheduler::GetInstance()->Enqueue(timer_task_);
     193     4511107 : }
     194             : 
     195             : //
     196             : // TimerManager class routines
     197             : //
     198             : TimerManager::TimerSet TimerManager::timer_ref_;
     199             : std::mutex TimerManager::mutex_;
     200             : 
     201      379761 : Timer *TimerManager::CreateTimer(
     202             :             boost::asio::io_context &service, const std::string &name,
     203             :             int task_id, int task_instance, bool delete_on_completion) {
     204             :     Timer *timer = new Timer(service, name, task_id, task_instance,
     205      379761 :                              delete_on_completion);
     206      379764 :     AddTimer(timer);
     207      379764 :     return timer;
     208             : }
     209             : 
     210      379764 : void TimerManager::AddTimer(Timer *timer) {
     211      379764 :     std::scoped_lock lock(mutex_);
     212      379764 :     timer_ref_.insert(TimerPtr(timer));
     213             : 
     214      759528 :     return;
     215      379764 : }
     216             : 
     217             : //
     218             : // Delete a timer object from the data base, by removing the intrusive
     219             : // reference. If any other objects has a reference to this timer such as
     220             : // boost::asio, the timer object deletion is automatically deferred
     221             : //
     222      374163 : bool TimerManager::DeleteTimer(Timer *timer) {
     223      374163 :     if (!timer || timer->fired()) return false;
     224             : 
     225      362395 :     if (!timer->Cancel() && timer->IsDeleteOnCompletion())
     226           0 :         return false;
     227             : 
     228      362395 :     std::scoped_lock lock(mutex_);
     229      362395 :     timer_ref_.erase(TimerPtr(timer));
     230             : 
     231      362395 :     return true;
     232      362395 : }
     233             : 
     234             : // Get timer's already elapsed time in milliseconds.
     235         728 : int64_t Timer::GetElapsedTime() const {
     236         728 :     std::scoped_lock lock(mutex_);
     237             :     int64_t elapsed;
     238             : 
     239             : #if __cplusplus >= 201103L
     240         728 :     elapsed = std::chrono::nanoseconds(impl_->expires_from_now()).count();
     241             : #else
     242             :     elapsed = boost::chrono::nanoseconds(impl_->expires_from_now()).count();
     243             : #endif
     244             : 
     245         728 :     elapsed = time_ - elapsed / 1000000; // Convert nanoseconds to milliseconds.
     246         728 :     if (elapsed < 0)
     247           0 :         elapsed = 0;
     248         728 :     return elapsed;
     249         728 : }

Generated by: LCOV version 1.14