1
0

Continue move to CMake

This commit is contained in:
Scott E. Graves
2017-03-15 12:34:03 -05:00
parent 19f3b0c625
commit 2b690f83c2
3 changed files with 106 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
#include "autothread.h"
using namespace Sia::Api;
CAutoThread::CAutoThread(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) :
CAutoThread(siaCurl, siaDriveConfig, nullptr)
{
}
CAutoThread::CAutoThread(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig, std::function<void(const CSiaCurl&, CSiaDriveConfig*)> autoThreadCallback) :
_siaCurl(new CSiaCurl(siaCurl)),
_siaDriveConfig(siaDriveConfig),
#ifdef _WIN32
_stopEvent(::CreateEvent(nullptr, FALSE, FALSE, nullptr)),
#endif
_AutoThreadCallback(autoThreadCallback)
{
}
CAutoThread::~CAutoThread()
{
StopAutoThread();
::CloseHandle(_stopEvent);
}
SiaHostConfig CAutoThread::GetHostConfig() const
{
return _siaCurl->GetHostConfig();
}
void CAutoThread::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig)
{
if (_AutoThreadCallback)
{
_AutoThreadCallback(siaCurl, siaDriveConfig);
}
}
void CAutoThread::StartAutoThread()
{
std::lock_guard<std::mutex> l(_startStopMutex);
if (!_thread)
{
_thread.reset(new std::thread([this]() {
#ifdef _WIN32
do
{
AutoThreadCallback(*_siaCurl, _siaDriveConfig);
} while (::WaitForSingleObject(_stopEvent, 2000) == WAIT_TIMEOUT);
#endif
}));
}
}
void CAutoThread::StopAutoThread()
{
std::lock_guard<std::mutex> l(_startStopMutex);
if (_thread)
{
#ifdef _WIN32
::SetEvent(_stopEvent);
#endif
_thread->join();
_thread.reset(nullptr);
}
}