2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
// Timer.cpp
|
|
|
|
//
|
2013-12-12 18:33:29 +01:00
|
|
|
// $Id: //poco/1.4/Foundation/src/Timer.cpp#3 $
|
2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Threading
|
|
|
|
// Module: Timer
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
2014-05-04 21:02:42 +02:00
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/Timer.h"
|
|
|
|
#include "Poco/ThreadPool.h"
|
|
|
|
#include "Poco/Exception.h"
|
|
|
|
#include "Poco/ErrorHandler.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
|
|
|
Timer::Timer(long startInterval, long periodicInterval):
|
|
|
|
_startInterval(startInterval),
|
|
|
|
_periodicInterval(periodicInterval),
|
|
|
|
_skipped(0),
|
|
|
|
_pCallback(0)
|
|
|
|
{
|
|
|
|
poco_assert (startInterval >= 0 && periodicInterval >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timer::~Timer()
|
|
|
|
{
|
2014-09-19 09:46:49 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
poco_unexpected();
|
|
|
|
}
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer::start(const AbstractTimerCallback& method)
|
|
|
|
{
|
|
|
|
start(method, Thread::PRIO_NORMAL, ThreadPool::defaultPool());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer::start(const AbstractTimerCallback& method, Thread::Priority priority)
|
|
|
|
{
|
|
|
|
start(method, priority, ThreadPool::defaultPool());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer::start(const AbstractTimerCallback& method, ThreadPool& threadPool)
|
|
|
|
{
|
|
|
|
start(method, Thread::PRIO_NORMAL, threadPool);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer::start(const AbstractTimerCallback& method, Thread::Priority priority, ThreadPool& threadPool)
|
|
|
|
{
|
2013-12-12 18:33:29 +01:00
|
|
|
Clock nextInvocation;
|
|
|
|
nextInvocation += static_cast<Clock::ClockVal>(_startInterval)*1000;
|
2014-06-05 15:02:46 +02:00
|
|
|
|
|
|
|
FastMutex::ScopedLock lock(_mutex);
|
2012-04-29 20:52:25 +02:00
|
|
|
|
2014-06-05 15:02:46 +02:00
|
|
|
if (_pCallback)
|
|
|
|
{
|
|
|
|
throw Poco::IllegalStateException("Timer already running");
|
|
|
|
}
|
2012-04-29 20:52:25 +02:00
|
|
|
|
|
|
|
_nextInvocation = nextInvocation;
|
|
|
|
_pCallback = method.clone();
|
|
|
|
_wakeUp.reset();
|
2014-06-05 15:02:46 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
threadPool.startWithPriority(priority, *this);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
delete _pCallback;
|
|
|
|
_pCallback = 0;
|
|
|
|
throw;
|
|
|
|
}
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer::stop()
|
|
|
|
{
|
|
|
|
FastMutex::ScopedLock lock(_mutex);
|
|
|
|
if (_pCallback)
|
|
|
|
{
|
|
|
|
_periodicInterval = 0;
|
|
|
|
_mutex.unlock();
|
|
|
|
_wakeUp.set();
|
|
|
|
_done.wait(); // warning: deadlock if called from timer callback
|
|
|
|
_mutex.lock();
|
|
|
|
delete _pCallback;
|
|
|
|
_pCallback = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer::restart()
|
|
|
|
{
|
|
|
|
FastMutex::ScopedLock lock(_mutex);
|
|
|
|
if (_pCallback)
|
|
|
|
{
|
|
|
|
_wakeUp.set();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer::restart(long milliseconds)
|
|
|
|
{
|
|
|
|
poco_assert (milliseconds >= 0);
|
|
|
|
FastMutex::ScopedLock lock(_mutex);
|
|
|
|
if (_pCallback)
|
|
|
|
{
|
|
|
|
_periodicInterval = milliseconds;
|
|
|
|
_wakeUp.set();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long Timer::getStartInterval() const
|
|
|
|
{
|
|
|
|
FastMutex::ScopedLock lock(_mutex);
|
|
|
|
return _startInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer::setStartInterval(long milliseconds)
|
|
|
|
{
|
|
|
|
poco_assert (milliseconds >= 0);
|
|
|
|
FastMutex::ScopedLock lock(_mutex);
|
|
|
|
_startInterval = milliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long Timer::getPeriodicInterval() const
|
|
|
|
{
|
|
|
|
FastMutex::ScopedLock lock(_mutex);
|
|
|
|
return _periodicInterval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer::setPeriodicInterval(long milliseconds)
|
|
|
|
{
|
|
|
|
poco_assert (milliseconds >= 0);
|
|
|
|
FastMutex::ScopedLock lock(_mutex);
|
|
|
|
_periodicInterval = milliseconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timer::run()
|
|
|
|
{
|
2013-12-12 18:33:29 +01:00
|
|
|
Poco::Clock now;
|
2012-04-29 20:52:25 +02:00
|
|
|
long interval(0);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
long sleep(0);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
now.update();
|
|
|
|
sleep = static_cast<long>((_nextInvocation - now)/1000);
|
|
|
|
if (sleep < 0)
|
|
|
|
{
|
|
|
|
if (interval == 0)
|
|
|
|
{
|
|
|
|
sleep = 0;
|
|
|
|
break;
|
|
|
|
}
|
2013-12-12 18:33:29 +01:00
|
|
|
_nextInvocation += static_cast<Clock::ClockVal>(interval)*1000;
|
2012-04-29 20:52:25 +02:00
|
|
|
++_skipped;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (sleep < 0);
|
|
|
|
|
2014-05-23 11:06:18 +02:00
|
|
|
if (_wakeUp.tryWait(sleep > _periodicInterval ? _periodicInterval : sleep))
|
2012-04-29 20:52:25 +02:00
|
|
|
{
|
|
|
|
Poco::FastMutex::ScopedLock lock(_mutex);
|
|
|
|
_nextInvocation.update();
|
|
|
|
interval = _periodicInterval;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_pCallback->invoke(*this);
|
|
|
|
}
|
|
|
|
catch (Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
Poco::ErrorHandler::handle(exc);
|
|
|
|
}
|
|
|
|
catch (std::exception& exc)
|
|
|
|
{
|
|
|
|
Poco::ErrorHandler::handle(exc);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
Poco::ErrorHandler::handle();
|
|
|
|
}
|
|
|
|
interval = _periodicInterval;
|
|
|
|
}
|
2013-12-12 18:33:29 +01:00
|
|
|
_nextInvocation += static_cast<Clock::ClockVal>(interval)*1000;
|
2012-04-29 20:52:25 +02:00
|
|
|
_skipped = 0;
|
|
|
|
}
|
|
|
|
while (interval > 0);
|
|
|
|
_done.set();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long Timer::skipped() const
|
|
|
|
{
|
|
|
|
return _skipped;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AbstractTimerCallback::AbstractTimerCallback()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AbstractTimerCallback::AbstractTimerCallback(const AbstractTimerCallback& callback)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AbstractTimerCallback::~AbstractTimerCallback()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AbstractTimerCallback& AbstractTimerCallback::operator = (const AbstractTimerCallback& callback)
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|