2016-03-27 20:41:29 +00:00
|
|
|
#include "hecl/ClientProcess.hpp"
|
2016-03-28 21:38:31 +00:00
|
|
|
#include "hecl/Database.hpp"
|
2016-03-27 20:41:29 +00:00
|
|
|
#include "athena/FileReader.hpp"
|
2016-03-28 22:39:18 +00:00
|
|
|
#include "BlenderConnection.hpp"
|
2016-03-27 20:41:29 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
#else
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace hecl
|
|
|
|
{
|
|
|
|
static logvisor::Module Log("hecl::ClientProcess");
|
|
|
|
|
|
|
|
static int GetCPUCount()
|
|
|
|
{
|
|
|
|
#if _WIN32
|
|
|
|
SYSTEM_INFO sysinfo;
|
|
|
|
GetSystemInfo(&sysinfo);
|
|
|
|
return sysinfo.dwNumberOfProcessors;
|
|
|
|
#else
|
|
|
|
return sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-03-28 22:39:18 +00:00
|
|
|
void ClientProcess::BufferTransaction::run(BlenderToken& btok)
|
2016-03-27 20:41:29 +00:00
|
|
|
{
|
|
|
|
athena::io::FileReader r(m_path.getAbsolutePath(), 32 * 1024, false);
|
|
|
|
if (r.hasError())
|
|
|
|
{
|
|
|
|
Log.report(logvisor::Fatal, _S("unable to background-buffer '%s'"),
|
|
|
|
m_path.getAbsolutePath().c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (m_offset)
|
|
|
|
r.seek(m_offset, athena::Begin);
|
|
|
|
r.readBytesToBuf(m_targetBuf, m_maxLen);
|
2016-03-28 04:36:32 +00:00
|
|
|
m_complete = true;
|
2016-03-27 20:41:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 22:39:18 +00:00
|
|
|
void ClientProcess::CookTransaction::run(BlenderToken& btok)
|
2016-03-27 20:41:29 +00:00
|
|
|
{
|
2016-03-28 22:39:18 +00:00
|
|
|
m_returnResult = m_parent.syncCook(m_path, m_dataSpec, btok);
|
2016-03-28 04:36:32 +00:00
|
|
|
m_complete = true;
|
2016-03-27 20:41:29 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 04:24:28 +00:00
|
|
|
void ClientProcess::LambdaTransaction::run(BlenderToken& btok)
|
|
|
|
{
|
|
|
|
m_func(btok);
|
|
|
|
m_complete = true;
|
|
|
|
}
|
|
|
|
|
2016-03-27 20:41:29 +00:00
|
|
|
ClientProcess::Worker::Worker(ClientProcess& proc)
|
|
|
|
: m_proc(proc)
|
|
|
|
{
|
|
|
|
m_thr = std::thread(std::bind(&Worker::proc, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientProcess::Worker::proc()
|
|
|
|
{
|
|
|
|
while (m_proc.m_running)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_proc.m_mutex);
|
2016-03-30 21:27:21 +00:00
|
|
|
while (m_proc.m_pendingQueue.size())
|
2016-03-27 20:41:29 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<Transaction> trans = std::move(m_proc.m_pendingQueue.front());
|
|
|
|
m_proc.m_pendingQueue.pop_front();
|
|
|
|
lk.unlock();
|
2016-03-28 22:39:18 +00:00
|
|
|
trans->run(m_blendTok);
|
2016-03-27 20:41:29 +00:00
|
|
|
lk.lock();
|
|
|
|
m_proc.m_completedQueue.push_back(std::move(trans));
|
|
|
|
}
|
2016-04-01 04:24:28 +00:00
|
|
|
if (!m_proc.m_running)
|
|
|
|
break;
|
2016-03-27 20:41:29 +00:00
|
|
|
m_proc.m_cv.wait(lk);
|
|
|
|
}
|
2016-04-01 04:24:28 +00:00
|
|
|
m_blendTok.shutdown();
|
2016-03-27 20:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ClientProcess::ClientProcess(int verbosityLevel)
|
|
|
|
: m_verbosity(verbosityLevel)
|
|
|
|
{
|
|
|
|
int cpuCount = GetCPUCount();
|
|
|
|
m_workers.reserve(cpuCount);
|
|
|
|
for (int i=0 ; i<cpuCount ; ++i)
|
|
|
|
m_workers.emplace_back(*this);
|
|
|
|
}
|
|
|
|
|
2016-03-28 04:36:32 +00:00
|
|
|
const ClientProcess::BufferTransaction*
|
|
|
|
ClientProcess::addBufferTransaction(const ProjectPath& path, void* target,
|
|
|
|
size_t maxLen, size_t offset)
|
2016-03-27 20:41:29 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_mutex);
|
2016-03-28 04:36:32 +00:00
|
|
|
BufferTransaction* ret = new BufferTransaction(*this, path, target, maxLen, offset);
|
|
|
|
m_pendingQueue.emplace_back(ret);
|
2016-03-27 20:41:29 +00:00
|
|
|
m_cv.notify_one();
|
2016-03-28 04:36:32 +00:00
|
|
|
return ret;
|
2016-03-27 20:41:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 04:36:32 +00:00
|
|
|
const ClientProcess::CookTransaction*
|
2016-03-28 22:39:18 +00:00
|
|
|
ClientProcess::addCookTransaction(const hecl::ProjectPath& path, Database::IDataSpec* spec)
|
2016-03-27 20:41:29 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_mutex);
|
2016-03-28 22:39:18 +00:00
|
|
|
CookTransaction* ret = new CookTransaction(*this, path, spec);
|
2016-04-01 04:24:28 +00:00
|
|
|
m_pendingQueue.emplace_back(ret);
|
|
|
|
m_cv.notify_one();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ClientProcess::LambdaTransaction*
|
|
|
|
ClientProcess::addLambdaTransaction(std::function<void(BlenderToken&)>&& func)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_mutex);
|
|
|
|
LambdaTransaction* ret = new LambdaTransaction(*this, std::move(func));
|
2016-03-28 04:36:32 +00:00
|
|
|
m_pendingQueue.emplace_back(ret);
|
2016-03-27 20:41:29 +00:00
|
|
|
m_cv.notify_one();
|
2016-03-28 04:36:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-28 22:39:18 +00:00
|
|
|
bool ClientProcess::syncCook(const hecl::ProjectPath& path, Database::IDataSpec* spec, BlenderToken& btok)
|
2016-03-28 04:36:32 +00:00
|
|
|
{
|
2016-03-28 22:39:18 +00:00
|
|
|
if (spec->canCook(path, btok))
|
2016-03-28 21:38:31 +00:00
|
|
|
{
|
2016-03-28 22:39:18 +00:00
|
|
|
const Database::DataSpecEntry* specEnt = spec->overrideDataSpec(path, spec->getDataSpecEntry(), btok);
|
|
|
|
if (specEnt)
|
|
|
|
{
|
|
|
|
hecl::ProjectPath cooked = path.getCookedPath(*specEnt);
|
2016-03-31 18:56:19 +00:00
|
|
|
cooked.makeDirChain(false);
|
2016-03-28 22:39:18 +00:00
|
|
|
spec->doCook(path, cooked, false, btok, [](const SystemChar*) {});
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-28 21:38:31 +00:00
|
|
|
}
|
2016-03-28 22:39:18 +00:00
|
|
|
return false;
|
2016-03-27 20:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientProcess::swapCompletedQueue(std::list<std::unique_ptr<Transaction>>& queue)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_mutex);
|
|
|
|
queue.swap(m_completedQueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClientProcess::shutdown()
|
|
|
|
{
|
|
|
|
if (!m_running)
|
|
|
|
return;
|
|
|
|
m_running = false;
|
|
|
|
m_cv.notify_all();
|
|
|
|
for (Worker& worker : m_workers)
|
|
|
|
if (worker.m_thr.joinable())
|
|
|
|
worker.m_thr.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|