Add deferred task implementation

This commit is contained in:
Sergey Yershov 2016-03-03 15:37:38 +03:00
parent 1ffafe2be5
commit 1e3acb49ea
5 changed files with 90 additions and 0 deletions

View file

@ -11,6 +11,7 @@ SOURCES += \
base.cpp \
condition.cpp \
gmtime.cpp \
deferred_task.cpp \
exception.cpp \
internal/message.cpp \
logging.cpp \
@ -42,6 +43,7 @@ HEADERS += \
collection_cast.hpp \
condition.hpp \
const_helper.hpp \
deferred_task.hpp \
exception.hpp \
gmtime.hpp \
internal/messagex.hpp \

44
base/deferred_task.cpp Normal file
View file

@ -0,0 +1,44 @@
#include "deferred_task.hpp"
DeferredTask::DeferredTask(TDuration duration) : m_duration(duration)
{
m_thread = thread([this] {
unique_lock<mutex> l(m_mutex);
while (!m_terminate)
{
if (!m_fn)
{
m_cv.wait(l);
continue;
}
if (m_cv.wait_for(l, m_duration) != cv_status::timeout)
continue;
auto local_fn = move(m_fn);
m_fn = nullptr;
l.unlock();
local_fn();
l.lock();
}
});
}
DeferredTask::~DeferredTask()
{
{
unique_lock<mutex> l(m_mutex);
m_terminate = true;
}
m_cv.notify_one();
m_thread.join();
}
void DeferredTask::Drop()
{
{
unique_lock<mutex> l(m_mutex);
m_fn = nullptr;
}
m_cv.notify_one();
}

35
base/deferred_task.hpp Normal file
View file

@ -0,0 +1,35 @@
#pragma once
#include "std/thread.hpp"
#include "std/chrono.hpp"
#include "std/mutex.hpp"
#include "std/condition_variable.hpp"
#include "std/function.hpp"
class DeferredTask
{
using TDuration = duration<double>;
thread m_thread;
mutex m_mutex;
condition_variable m_cv;
function<void()> m_fn;
TDuration m_duration;
bool m_terminate = false;
public:
DeferredTask(TDuration duration);
~DeferredTask();
void Drop();
template<typename TFn>
void RestartWith(TFn const && fn)
{
{
unique_lock<mutex> l(m_mutex);
m_fn = fn;
}
m_cv.notify_one();
}
};

View file

@ -7,6 +7,7 @@
#include <condition_variable>
using std::condition_variable;
using std::cv_status;
#ifdef DEBUG_NEW
#define new DEBUG_NEW

View file

@ -69,6 +69,8 @@
6753420F1A3F57E400A0A8C3 /* timer.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 675341CA1A3F57E400A0A8C3 /* timer.hpp */; };
6753453D1A3F6F6A00A0A8C3 /* message.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6753453A1A3F6F6A00A0A8C3 /* message.cpp */; };
6753453E1A3F6F6A00A0A8C3 /* message.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 6753453B1A3F6F6A00A0A8C3 /* message.hpp */; };
67A609AE1C88642E001E641A /* deferred_task.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 67A609AC1C88642E001E641A /* deferred_task.cpp */; };
67A609AF1C88642E001E641A /* deferred_task.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 67A609AD1C88642E001E641A /* deferred_task.hpp */; };
67B52B601AD3C84E00664C17 /* thread_checker.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 67B52B5E1AD3C84E00664C17 /* thread_checker.cpp */; };
67B52B611AD3C84E00664C17 /* thread_checker.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 67B52B5F1AD3C84E00664C17 /* thread_checker.hpp */; };
/* End PBXBuildFile section */
@ -140,6 +142,8 @@
675341CA1A3F57E400A0A8C3 /* timer.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = timer.hpp; sourceTree = "<group>"; };
6753453A1A3F6F6A00A0A8C3 /* message.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = message.cpp; sourceTree = "<group>"; };
6753453B1A3F6F6A00A0A8C3 /* message.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = message.hpp; sourceTree = "<group>"; };
67A609AC1C88642E001E641A /* deferred_task.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = deferred_task.cpp; sourceTree = "<group>"; };
67A609AD1C88642E001E641A /* deferred_task.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = deferred_task.hpp; sourceTree = "<group>"; };
67B52B5E1AD3C84E00664C17 /* thread_checker.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = thread_checker.cpp; sourceTree = "<group>"; };
67B52B5F1AD3C84E00664C17 /* thread_checker.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = thread_checker.hpp; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -240,6 +244,8 @@
675341CA1A3F57E400A0A8C3 /* timer.hpp */,
34CB447C1C0C34C50061F6A0 /* timegm.cpp */,
34CB447D1C0C34C50061F6A0 /* timegm.hpp */,
67A609AC1C88642E001E641A /* deferred_task.cpp */,
67A609AD1C88642E001E641A /* deferred_task.hpp */,
);
name = base;
path = ../../base;
@ -269,6 +275,7 @@
6753420D1A3F57E400A0A8C3 /* threaded_priority_queue.hpp in Headers */,
675341EF1A3F57E400A0A8C3 /* rolling_hash.hpp in Headers */,
671182F11C807C0A00CB8177 /* gmtime.hpp in Headers */,
67A609AF1C88642E001E641A /* deferred_task.hpp in Headers */,
675342021A3F57E400A0A8C3 /* string_utils.hpp in Headers */,
675341E41A3F57E400A0A8C3 /* matrix.hpp in Headers */,
675341D81A3F57E400A0A8C3 /* condition.hpp in Headers */,
@ -378,6 +385,7 @@
674A7E2E1C0DB03D003D48E1 /* timegm.cpp in Sources */,
6753420A1A3F57E400A0A8C3 /* threaded_container.cpp in Sources */,
675341FF1A3F57E400A0A8C3 /* string_format.cpp in Sources */,
67A609AE1C88642E001E641A /* deferred_task.cpp in Sources */,
675341DF1A3F57E400A0A8C3 /* logging.cpp in Sources */,
671182F01C807C0A00CB8177 /* gmtime.cpp in Sources */,
675341D71A3F57E400A0A8C3 /* condition.cpp in Sources */,