LCOV - code coverage report
Current view: top level - source - Timer.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 94.7 % 38 36
Test Date: 2026-06-10 12:08:30 Functions: 100.0 % 9 9

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2023 Sky UK
       3              :  *
       4              :  * This library is free software; you can redistribute it and/or
       5              :  * modify it under the terms of the GNU Lesser General Public
       6              :  * License as published by the Free Software Foundation;
       7              :  * version 2.1 of the License.
       8              :  *
       9              :  * This library is distributed in the hope that it will be useful,
      10              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12              :  * Lesser General Public License for more details.
      13              :  *
      14              :  * You should have received a copy of the GNU Lesser General Public
      15              :  * License along with this library; if not, write to the Free Software
      16              :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
      17              :  */
      18              : 
      19              : #include "Timer.h"
      20              : #include <gst/gst.h>
      21              : 
      22              : std::weak_ptr<ITimerFactory> TimerFactory::m_factory;
      23              : 
      24           50 : std::shared_ptr<ITimerFactory> ITimerFactory::getFactory()
      25              : {
      26           50 :     std::shared_ptr<ITimerFactory> factory = TimerFactory::m_factory.lock();
      27              : 
      28           50 :     if (!factory)
      29              :     {
      30              :         try
      31              :         {
      32           50 :             factory = std::make_shared<TimerFactory>();
      33              :         }
      34            0 :         catch (const std::exception &e)
      35              :         {
      36            0 :             GST_ERROR("Failed to create the timer factory, reason: %s", e.what());
      37              :         }
      38              : 
      39           50 :         TimerFactory::m_factory = factory;
      40              :     }
      41              : 
      42           50 :     return factory;
      43              : }
      44              : 
      45            3 : std::unique_ptr<ITimer> TimerFactory::createTimer(const std::chrono::milliseconds &timeout,
      46              :                                                   const std::function<void()> &callback, TimerType timerType) const
      47              : {
      48            3 :     return std::make_unique<Timer>(timeout, callback, timerType);
      49              : }
      50              : 
      51            3 : Timer::Timer(const std::chrono::milliseconds &timeout, const std::function<void()> &callback, TimerType timerType)
      52            3 :     : m_active{true}, m_timeout{timeout}, m_callback{callback}
      53              : {
      54            3 :     m_thread = std::thread(
      55            6 :         [this, timerType]()
      56              :         {
      57              :             do
      58              :             {
      59            6 :                 bool shouldCallback = false;
      60              :                 {
      61            6 :                     std::unique_lock<std::mutex> lock{m_mutex};
      62           17 :                     if (!m_cv.wait_for(lock, m_timeout, [this]() { return !m_active; }))
      63              :                     {
      64            4 :                         shouldCallback = m_active && m_callback;
      65              :                     }
      66            6 :                 } // lock released here by destructor
      67              : 
      68            6 :                 if (shouldCallback)
      69              :                 {
      70            4 :                     m_callback();
      71              :                 }
      72            6 :             } while (timerType == TimerType::PERIODIC && m_active);
      73            3 :             m_active = false;
      74            6 :         });
      75            3 : }
      76              : 
      77            6 : Timer::~Timer()
      78              : {
      79            3 :     doCancel();
      80            6 : }
      81              : 
      82            1 : void Timer::cancel()
      83              : {
      84            1 :     doCancel();
      85              : }
      86              : 
      87            4 : bool Timer::isActive() const
      88              : {
      89            4 :     return m_active;
      90              : }
      91              : 
      92            4 : void Timer::doCancel()
      93              : {
      94            4 :     m_active = false;
      95              : 
      96            4 :     if (std::this_thread::get_id() != m_thread.get_id() && m_thread.joinable())
      97              :     {
      98            3 :         m_cv.notify_one();
      99            3 :         m_thread.join();
     100              :     }
     101            4 : }
        

Generated by: LCOV version 2.0-1