Thread optimization

This commit is contained in:
momo5502 2016-06-02 15:11:31 +02:00
parent 0a0d6ae5e6
commit e8c915374b
9 changed files with 25 additions and 47 deletions

2
deps/protobuf vendored

@ -1 +1 @@
Subproject commit 40ff94ebef494e2e16107ce2965215ae67c031c1 Subproject commit 18a9140f3308272313a9642af58ab0051ac09fd2

View File

@ -80,15 +80,13 @@ namespace Components
Localization::Set("MPUI_SECURITY_INCREASE_MESSAGE", Utils::VA("Increasing security level from %d to %d (est. %s)", Auth::GetSecurityLevel(), Auth::TokenContainer.targetLevel, Utils::FormatTimeSpan(static_cast<int>(mseconds)).data())); Localization::Set("MPUI_SECURITY_INCREASE_MESSAGE", Utils::VA("Increasing security level from %d to %d (est. %s)", Auth::GetSecurityLevel(), Auth::TokenContainer.targetLevel, Utils::FormatTimeSpan(static_cast<int>(mseconds)).data()));
} }
else if(Auth::TokenContainer.thread) else
{ {
if (Auth::TokenContainer.thread->joinable()) if (Auth::TokenContainer.thread.joinable())
{ {
Auth::TokenContainer.thread->join(); Auth::TokenContainer.thread.join();
} }
delete Auth::TokenContainer.thread;
Auth::TokenContainer.thread = nullptr;
Auth::TokenContainer.generating = false; Auth::TokenContainer.generating = false;
Auth::StoreKey(); Auth::StoreKey();
@ -208,7 +206,7 @@ namespace Components
Command::Execute("openmenu security_increase_popmenu", true); Command::Execute("openmenu security_increase_popmenu", true);
// Start thread // Start thread
Auth::TokenContainer.thread = new std::thread([&level] () Auth::TokenContainer.thread = std::thread([&level] ()
{ {
Auth::TokenContainer.generating = true; Auth::TokenContainer.generating = true;
Auth::TokenContainer.hashes = 0; Auth::TokenContainer.hashes = 0;
@ -293,7 +291,6 @@ namespace Components
{ {
Auth::TokenContainer.cancel = false; Auth::TokenContainer.cancel = false;
Auth::TokenContainer.generating = false; Auth::TokenContainer.generating = false;
Auth::TokenContainer.thread = nullptr;
Localization::Set("MPUI_SECURITY_INCREASE_MESSAGE", ""); Localization::Set("MPUI_SECURITY_INCREASE_MESSAGE", "");
@ -428,15 +425,9 @@ namespace Components
Auth::TokenContainer.generating = false; Auth::TokenContainer.generating = false;
// Terminate thread // Terminate thread
if (Auth::TokenContainer.thread) if (Auth::TokenContainer.thread.joinable())
{ {
if (Auth::TokenContainer.thread->joinable()) Auth::TokenContainer.thread.join();
{
Auth::TokenContainer.thread->join();
}
delete Auth::TokenContainer.thread;
Auth::TokenContainer.thread = nullptr;
} }
Auth::StoreKey(); Auth::StoreKey();

View File

@ -40,7 +40,7 @@ namespace Components
{ {
bool cancel; bool cancel;
bool generating; bool generating;
std::thread* thread; std::thread thread;
uint32_t targetLevel; uint32_t targetLevel;
int startTime; int startTime;
std::string command; std::string command;

View File

@ -2,7 +2,7 @@
namespace Components namespace Components
{ {
Discovery::Container Discovery::DiscoveryContainer = { false, false, nullptr }; Discovery::Container Discovery::DiscoveryContainer = { false, false, std::thread() };
void Discovery::Perform() void Discovery::Perform()
{ {
@ -16,7 +16,7 @@ namespace Components
Discovery::DiscoveryContainer.Perform = false; Discovery::DiscoveryContainer.Perform = false;
Discovery::DiscoveryContainer.Terminate = false; Discovery::DiscoveryContainer.Terminate = false;
Discovery::DiscoveryContainer.Thread = new std::thread([] () Discovery::DiscoveryContainer.Thread = std::thread([] ()
{ {
while (!Discovery::DiscoveryContainer.Terminate) while (!Discovery::DiscoveryContainer.Terminate)
{ {
@ -85,11 +85,9 @@ namespace Components
Discovery::DiscoveryContainer.Perform = false; Discovery::DiscoveryContainer.Perform = false;
Discovery::DiscoveryContainer.Terminate = true; Discovery::DiscoveryContainer.Terminate = true;
if (Discovery::DiscoveryContainer.Thread) if (Discovery::DiscoveryContainer.Thread.joinable())
{ {
Discovery::DiscoveryContainer.Thread->join(); Discovery::DiscoveryContainer.Thread.join();
delete Discovery::DiscoveryContainer.Thread;
Discovery::DiscoveryContainer.Thread = nullptr;
} }
} }
} }

View File

@ -14,7 +14,7 @@ namespace Components
{ {
bool Perform; bool Perform;
bool Terminate; bool Terminate;
std::thread* Thread; std::thread Thread;
std::string Challenge; std::string Challenge;
}; };

View File

@ -7,7 +7,7 @@ namespace Components
#pragma region Pipe #pragma region Pipe
Pipe::Pipe() : mType(IPCTYPE_NONE), ReconnectAttempt(0), hPipe(INVALID_HANDLE_VALUE), mThread(0), ConnectCallback(0), mThreadAttached(false) Pipe::Pipe() : mType(IPCTYPE_NONE), ReconnectAttempt(0), hPipe(INVALID_HANDLE_VALUE), ConnectCallback(0), mThreadAttached(false)
{ {
this->Destroy(); this->Destroy();
} }
@ -66,7 +66,7 @@ namespace Components
if (!Loader::PerformingUnitTests()) if (!Loader::PerformingUnitTests())
{ {
this->mThreadAttached = true; this->mThreadAttached = true;
this->mThread = new std::thread(Pipe::ReceiveThread, this); this->mThread = std::thread(Pipe::ReceiveThread, this);
} }
Logger::Print("Pipe successfully created\n"); Logger::Print("Pipe successfully created\n");
@ -117,17 +117,13 @@ namespace Components
this->mThreadAttached = false; this->mThreadAttached = false;
if (this->mThread) if (this->mThread.joinable())
{ {
Logger::Print("Terminating pipe thread...\n"); Logger::Print("Terminating pipe thread...\n");
this->mThread->join(); this->mThread.join();
Logger::Print("Pipe thread terminated.\n"); Logger::Print("Pipe thread terminated.\n");
delete this->mThread;
this->mThread = 0;
} }
} }

View File

@ -41,7 +41,7 @@ namespace Components
std::map<std::string, PacketCallback> PacketCallbacks; std::map<std::string, PacketCallback> PacketCallbacks;
HANDLE hPipe; HANDLE hPipe;
std::thread* mThread; std::thread mThread;
bool mThreadAttached; bool mThreadAttached;
Type mType; Type mType;

View File

@ -4,20 +4,16 @@
namespace Components namespace Components
{ {
std::thread* News::Thread = nullptr; std::thread News::Thread;
bool News::UnitTest() bool News::UnitTest()
{ {
bool result = true; bool result = true;
if (News::Thread) if (News::Thread.joinable())
{ {
Logger::Print("Awaiting thread termination...\n"); Logger::Print("Awaiting thread termination...\n");
News::Thread.join();
News::Thread->join();
delete News::Thread;
News::Thread = nullptr;
if (!strlen(Localization::Get("MPUI_CHANGELOG_TEXT")) || Localization::Get("MPUI_CHANGELOG_TEXT") == std::string("Loading...")) if (!strlen(Localization::Get("MPUI_CHANGELOG_TEXT")) || Localization::Get("MPUI_CHANGELOG_TEXT") == std::string("Loading..."))
{ {
@ -48,7 +44,7 @@ namespace Components
Localization::Set("MPUI_CHANGELOG_TEXT", "Loading..."); Localization::Set("MPUI_CHANGELOG_TEXT", "Loading...");
Localization::Set("MPUI_MOTD_TEXT", NEWS_MOTD_DEFUALT); Localization::Set("MPUI_MOTD_TEXT", NEWS_MOTD_DEFUALT);
News::Thread = new std::thread([] () News::Thread = std::thread([] ()
{ {
Localization::Set("MPUI_CHANGELOG_TEXT", Utils::WebIO("IW4x", "https://iw4xcachep26muba.onion.to/iw4/changelog.txt").SetTimeout(5000)->Get().data()); Localization::Set("MPUI_CHANGELOG_TEXT", Utils::WebIO("IW4x", "https://iw4xcachep26muba.onion.to/iw4/changelog.txt").SetTimeout(5000)->Get().data());
@ -65,12 +61,9 @@ namespace Components
News::~News() News::~News()
{ {
if (News::Thread) if (News::Thread.joinable())
{ {
News::Thread->join(); News::Thread.join();
delete News::Thread;
News::Thread = nullptr;
} }
} }
} }

View File

@ -9,6 +9,6 @@ namespace Components
bool UnitTest(); bool UnitTest();
private: private:
static std::thread* Thread; static std::thread Thread;
}; };
} }