Continue move to CMake
This commit is contained in:
38
include/siadrive_api/autothread.h
Normal file
38
include/siadrive_api/autothread.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#ifndef _AUTOTHREAD_H
|
||||||
|
#define _AUTOTHREAD_H
|
||||||
|
|
||||||
|
#include <siacommon.h>
|
||||||
|
|
||||||
|
NS_BEGIN(Sia)
|
||||||
|
NS_BEGIN(Api)
|
||||||
|
|
||||||
|
class CSiaDriveConfig;
|
||||||
|
class CSiaCurl;
|
||||||
|
class SIADRIVE_EXPORTABLE CAutoThread
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CAutoThread(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
|
||||||
|
CAutoThread(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig, std::function<void(const CSiaCurl&, CSiaDriveConfig*)> autoThreadCallback);
|
||||||
|
|
||||||
|
public:
|
||||||
|
virtual ~CAutoThread();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<CSiaCurl> _siaCurl;
|
||||||
|
CSiaDriveConfig* _siaDriveConfig;
|
||||||
|
HANDLE _stopEvent;
|
||||||
|
std::unique_ptr<std::thread> _thread;
|
||||||
|
std::mutex _startStopMutex;
|
||||||
|
std::function<void(const CSiaCurl&, CSiaDriveConfig*)> _AutoThreadCallback;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
|
||||||
|
|
||||||
|
public:
|
||||||
|
SiaHostConfig GetHostConfig() const;
|
||||||
|
void StartAutoThread();
|
||||||
|
void StopAutoThread();
|
||||||
|
};
|
||||||
|
|
||||||
|
NS_END(2)
|
||||||
|
#endif //_AUTOTHREAD_H
|
@@ -42,6 +42,8 @@
|
|||||||
#include <json.hpp>
|
#include <json.hpp>
|
||||||
#include <ttmath/ttmath.h>
|
#include <ttmath/ttmath.h>
|
||||||
#include <sstring.h>
|
#include <sstring.h>
|
||||||
|
#include <thread>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
66
src/siadrive_api/autothread.cpp
Normal file
66
src/siadrive_api/autothread.cpp
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user