2 * Copyright (C) 2012 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"
25 #include "utils/log.h"
27 CTimer::CTimer(ITimerCallback *callback)
40 bool CTimer::Start(uint32_t timeout, bool interval /* = false */)
42 if (m_callback == NULL || timeout == 0 || IsRunning())
44 CLog::Log(LOGWARNING, "CTimer: can't start timer");
49 m_interval = interval;
51 CLog::Log(LOGDEBUG, "CTimer: starting for %d ms %s", m_timeout, m_interval ? "(interval)" : "");
56 bool CTimer::Stop(bool wait /* = false */)
61 CLog::Log(LOGDEBUG, "CTimer: stopping %s", wait ? "(wait)" : "");
69 float CTimer::GetElapsedSeconds() const
71 return GetElapsedMilliseconds() / 1000.0f;
74 float CTimer::GetElapsedMilliseconds() const
79 return (float)(XbmcThreads::SystemClockMillis() - (m_endTime - m_timeout));
82 void CTimer::Process()
84 uint32_t currentTime = XbmcThreads::SystemClockMillis();
85 m_endTime = currentTime + m_timeout;
89 // wait the necessary time
90 if (!m_eventTimeout.WaitMSec(m_endTime - currentTime))
92 currentTime = XbmcThreads::SystemClockMillis();
93 if (m_endTime <= currentTime)
95 CLog::Log(LOGDEBUG, "CTimer: timeout");
96 // execute OnTimeout() callback
97 m_callback->OnTimeout();
99 // stop if this is not an interval timer
103 CLog::Log(LOGDEBUG, "CTimer: restart");
104 m_endTime = currentTime + m_timeout;
109 CLog::Log(LOGDEBUG, "CTimer: finished");