dedup/src/hasherthread.cpp

57 lines
952 B
C++

#include "hasherthread.h"
#include <cassert>
#include <Log.h>
HasherThread::HasherThread(std::mutex& mutex, getNext_f next, doneCallback_f done) : mutex(mutex), getNext(next), doneCallback(done) {
assert(getNext);
assert(doneCallback);
}
HasherThread::~HasherThread() {
stop();
waitfor();
}
void HasherThread::start() {
shouldrun = true;
thread = std::thread(&HasherThread::threadLoop, this);
}
void HasherThread::stop() {
shouldrun = false;
}
void HasherThread::waitfor() {
if(thread.joinable()) {
thread.join();
}
}
void HasherThread::threadLoop() {
while(shouldrun) {
// get task
std::shared_ptr<File> task;
{
std::lock_guard lock(mutex);
task = getNext();
}
// no more tasks
if(!task) {
break;
}
// do the task
Hash hash = task->createHash();
// submit
{
std::lock_guard lock(mutex);
doneCallback(std::move(hash), task);
}
}
Log::info << "no more tasks, terminateing Hasherthread";
}