Poco::Util::Timer now uses Poco::Clock instead of Poco::Timestamp

This commit is contained in:
Guenter Obiltschnig 2013-12-12 18:37:08 +01:00
Родитель ac2b26fcb9
Коммит 70e116fda2
2 изменённых файлов: 85 добавлений и 19 удалений

Просмотреть файл

@ -1,7 +1,7 @@
//
// Timer.h
//
// $Id: //poco/1.4/Util/include/Poco/Util/Timer.h#1 $
// $Id: //poco/1.4/Util/include/Poco/Util/Timer.h#2 $
//
// Library: Util
// Package: Timer
@ -93,6 +93,19 @@ public:
///
/// If the time lies in the past, the task is executed
/// immediately.
///
/// Note: the relative time the task will be executed
/// won't change if the system's time changes. If the
/// given time is 10 seconds in the future at the point
/// schedule() is called, the task will be executed 10
/// seconds later, even if the system time changes in
/// between.
void schedule(TimerTask::Ptr pTask, Poco::Clock clock);
/// Schedules a task for execution at the specified time.
///
/// If the time lies in the past, the task is executed
/// immediately.
void schedule(TimerTask::Ptr pTask, long delay, long interval);
/// Schedules a task for periodic execution.
@ -107,6 +120,20 @@ public:
/// The task is first executed at the given time.
/// Subsequently, the task is executed periodically with
/// the given interval in milliseconds between invocations.
///
/// Note: the relative time the task will be executed
/// won't change if the system's time changes. If the
/// given time is 10 seconds in the future at the point
/// schedule() is called, the task will be executed 10
/// seconds later, even if the system time changes in
/// between.
void schedule(TimerTask::Ptr pTask, Poco::Clock clock, long interval);
/// Schedules a task for periodic execution.
///
/// The task is first executed at the given time.
/// Subsequently, the task is executed periodically with
/// the given interval in milliseconds between invocations.
void scheduleAtFixedRate(TimerTask::Ptr pTask, long delay, long interval);
/// Schedules a task for periodic execution at a fixed rate.
@ -127,6 +154,23 @@ public:
///
/// If task execution takes longer than the given interval,
/// further executions are delayed.
///
/// Note: the relative time the task will be executed
/// won't change if the system's time changes. If the
/// given time is 10 seconds in the future at the point
/// scheduleAtFixedRate() is called, the task will be executed 10
/// seconds later, even if the system time changes in
/// between.
void scheduleAtFixedRate(TimerTask::Ptr pTask, Poco::Clock clock, long interval);
/// Schedules a task for periodic execution at a fixed rate.
///
/// The task is first executed at the given time.
/// Subsequently, the task is executed periodically
/// every number of milliseconds specified by interval.
///
/// If task execution takes longer than the given interval,
/// further executions are delayed.
protected:
void run();

Просмотреть файл

@ -1,7 +1,7 @@
//
// Timer.cpp
//
// $Id: //poco/1.4/Util/src/Timer.cpp#1 $
// $Id: //poco/1.4/Util/src/Timer.cpp#2 $
//
// Library: Util
// Package: Timer
@ -187,9 +187,9 @@ public:
if (!task()->isCancelled())
{
Poco::Timestamp now;
Poco::Timestamp nextExecution;
nextExecution += static_cast<Poco::Timestamp::TimeDiff>(_interval)*1000;
Poco::Clock now;
Poco::Clock nextExecution;
nextExecution += static_cast<Poco::Clock::ClockDiff>(_interval)*1000;
if (nextExecution < now) nextExecution = now;
queue().enqueueNotification(this, nextExecution);
duplicate();
@ -205,10 +205,10 @@ private:
class FixedRateTaskNotification: public TaskNotification
{
public:
FixedRateTaskNotification(Poco::TimedNotificationQueue& queue, TimerTask::Ptr pTask, long interval, Poco::Timestamp time):
FixedRateTaskNotification(Poco::TimedNotificationQueue& queue, TimerTask::Ptr pTask, long interval, Poco::Clock clock):
TaskNotification(queue, pTask),
_interval(interval),
_nextExecution(time)
_nextExecution(clock)
{
}
@ -222,8 +222,8 @@ public:
if (!task()->isCancelled())
{
Poco::Timestamp now;
_nextExecution += static_cast<Poco::Timestamp::TimeDiff>(_interval)*1000;
Poco::Clock now;
_nextExecution += static_cast<Poco::Clock::ClockDiff>(_interval)*1000;
if (_nextExecution < now) _nextExecution = now;
queue().enqueueNotification(this, _nextExecution);
duplicate();
@ -233,7 +233,7 @@ public:
private:
long _interval;
Poco::Timestamp _nextExecution;
Poco::Clock _nextExecution;
};
@ -252,7 +252,7 @@ Timer::Timer(Poco::Thread::Priority priority)
Timer::~Timer()
{
_queue.enqueueNotification(new StopNotification(_queue), 0);
_queue.enqueueNotification(new StopNotification(_queue), Poco::Clock(0));
_thread.join();
}
@ -260,7 +260,7 @@ Timer::~Timer()
void Timer::cancel(bool wait)
{
Poco::AutoPtr<CancelNotification> pNf = new CancelNotification(_queue);
_queue.enqueueNotification(pNf, 0);
_queue.enqueueNotification(pNf, Poco::Clock(0));
if (wait)
{
pNf->wait();
@ -273,12 +273,18 @@ void Timer::schedule(TimerTask::Ptr pTask, Poco::Timestamp time)
_queue.enqueueNotification(new TaskNotification(_queue, pTask), time);
}
void Timer::schedule(TimerTask::Ptr pTask, Poco::Clock clock)
{
_queue.enqueueNotification(new TaskNotification(_queue, pTask), clock);
}
void Timer::schedule(TimerTask::Ptr pTask, long delay, long interval)
{
Poco::Timestamp time;
time += static_cast<Poco::Timestamp::TimeDiff>(delay)*1000;
schedule(pTask, time, interval);
Poco::Clock clock;
clock += static_cast<Poco::Clock::ClockDiff>(delay)*1000;
schedule(pTask, clock, interval);
}
@ -287,18 +293,34 @@ void Timer::schedule(TimerTask::Ptr pTask, Poco::Timestamp time, long interval)
_queue.enqueueNotification(new PeriodicTaskNotification(_queue, pTask, interval), time);
}
void Timer::schedule(TimerTask::Ptr pTask, Poco::Clock clock, long interval)
{
_queue.enqueueNotification(new PeriodicTaskNotification(_queue, pTask, interval), clock);
}
void Timer::scheduleAtFixedRate(TimerTask::Ptr pTask, long delay, long interval)
{
Poco::Timestamp time;
time += static_cast<Poco::Timestamp::TimeDiff>(delay)*1000;
scheduleAtFixedRate(pTask, time, interval);
Poco::Clock clock;
clock += static_cast<Poco::Clock::ClockDiff>(delay)*1000;
scheduleAtFixedRate(pTask, clock, interval);
}
void Timer::scheduleAtFixedRate(TimerTask::Ptr pTask, Poco::Timestamp time, long interval)
{
_queue.enqueueNotification(new FixedRateTaskNotification(_queue, pTask, interval, time), time);
Poco::Timestamp tsNow;
Poco::Clock clock;
Poco::Timestamp::TimeDiff diff = time - tsNow;
clock += diff;
_queue.enqueueNotification(new FixedRateTaskNotification(_queue, pTask, interval, clock), clock);
}
void Timer::scheduleAtFixedRate(TimerTask::Ptr pTask, Poco::Clock clock, long interval)
{
_queue.enqueueNotification(new FixedRateTaskNotification(_queue, pTask, interval, clock), clock);
}