2 * Copyright (C) 2012-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
24 #include "SystemClock.h"
26 CTimer::CTimer(ITimerCallback *callback)
39 bool CTimer::Start(uint32_t timeout, bool interval /* = false */)
41 if (m_callback == NULL || timeout == 0 || IsRunning())
45 m_interval = interval;
51 bool CTimer::Stop(bool wait /* = false */)
63 bool CTimer::Restart()
69 return Start(m_timeout, m_interval);
72 float CTimer::GetElapsedSeconds() const
74 return GetElapsedMilliseconds() / 1000.0f;
77 float CTimer::GetElapsedMilliseconds() const
82 return (float)(XbmcThreads::SystemClockMillis() - (m_endTime - m_timeout));
85 void CTimer::Process()
87 uint32_t currentTime = XbmcThreads::SystemClockMillis();
88 m_endTime = currentTime + m_timeout;
92 // wait the necessary time
93 if (!m_eventTimeout.WaitMSec(m_endTime - currentTime))
95 currentTime = XbmcThreads::SystemClockMillis();
96 if (m_endTime <= currentTime)
98 // execute OnTimeout() callback
99 m_callback->OnTimeout();
101 // stop if this is not an interval timer
105 m_endTime = currentTime + m_timeout;