2015-04-18 16:50:19 +02:00
|
|
|
#include "PeriodicTask.h"
|
2015-10-17 03:30:42 +02:00
|
|
|
#include <messmer/cpp-utils/logging/logging.h>
|
2015-04-18 16:50:19 +02:00
|
|
|
|
|
|
|
using std::function;
|
|
|
|
using std::endl;
|
2015-10-17 03:30:42 +02:00
|
|
|
using namespace cpputils::logging;
|
2015-04-18 16:50:19 +02:00
|
|
|
|
|
|
|
namespace blockstore {
|
|
|
|
namespace caching {
|
|
|
|
|
2015-10-28 15:00:49 +01:00
|
|
|
PeriodicTask::PeriodicTask(function<void ()> task, double intervalSec) :
|
|
|
|
_task(task),
|
|
|
|
_interval((uint64_t)(UINT64_C(1000000000) * intervalSec)),
|
|
|
|
_thread(std::bind(&PeriodicTask::_loopIteration, this)) {
|
|
|
|
_thread.start();
|
2015-04-18 16:50:19 +02:00
|
|
|
}
|
|
|
|
|
2015-11-12 22:07:59 +01:00
|
|
|
bool PeriodicTask::_loopIteration() {
|
2015-10-28 15:30:59 +01:00
|
|
|
//Has to be boost::this_thread::sleep_for and not std::this_thread::sleep_for, because it has to be interruptible.
|
|
|
|
//LoopThread will interrupt this method if it has to be restarted.
|
|
|
|
boost::this_thread::sleep_for(_interval);
|
|
|
|
_task();
|
2015-11-12 22:07:59 +01:00
|
|
|
return true; // Run another iteration (don't terminate thread)
|
2015-04-18 16:50:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|