poco/Foundation/src/Timer.cpp

249 lines
4.0 KiB
C++
Raw Normal View History

2012-04-29 20:52:25 +02:00
//
// Timer.cpp
//
// $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.
//
// 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()
{
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)
{
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()
{
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;
}
_nextInvocation += static_cast<Clock::ClockVal>(interval)*1000;
2012-04-29 20:52:25 +02:00
++_skipped;
}
}
while (sleep < 0);
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;
}
_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