Files
UnrealEngineUWP/Engine/Source/Developer/TraceAnalysis/Private/Store/AsioContext.cpp
Martin Ridgers 29c4f53269 Cleaner store service shutdown which closes everything and waits for the async IO loops to drain.
#rb none
#rnx

[CL 11126050 by Martin Ridgers in Dev-Core branch]
2020-01-28 04:11:19 -05:00

82 lines
1.4 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "AsioContext.h"
namespace Trace
{
////////////////////////////////////////////////////////////////////////////////
FAsioContext::FAsioContext(int32 ThreadCount)
{
if (ThreadCount < 1)
{
ThreadCount = 1;
}
IoContext = new asio::io_context(ThreadCount);
ThreadPool.SetNum(ThreadCount);
}
////////////////////////////////////////////////////////////////////////////////
FAsioContext::~FAsioContext()
{
Wait();
delete IoContext;
}
////////////////////////////////////////////////////////////////////////////////
void FAsioContext::Start()
{
if (bRunning)
{
return;
}
for (std::thread& Thread : ThreadPool)
{
std::thread TempThread([this] () { IoContext->run(); });
Thread = MoveTemp(TempThread);
}
bRunning = true;
}
////////////////////////////////////////////////////////////////////////////////
void FAsioContext::Stop(bool bWait)
{
if (!bRunning)
{
return;
}
IoContext->stop();
if (bWait)
{
Wait();
}
}
////////////////////////////////////////////////////////////////////////////////
void FAsioContext::Wait()
{
for (std::thread& Thread : ThreadPool)
{
if (Thread.joinable())
{
Thread.join();
}
}
bRunning = false;
}
////////////////////////////////////////////////////////////////////////////////
asio::io_context& FAsioContext::Get()
{
return *IoContext;
}
} // namespace Trace