mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-26 22:57:00 +02:00
9bfe000724
Currently closing game while network thread is waiting for something is very bug-prone, since network thread may resume during shutdown and access partially destroyed client state. Now if exit has been requested, the very first step would be semi- graceful shutdown of network thread (via exception throwing). This may in theory skip some cleanup in non-RAII code, but since game is shutting down this does not matters much. This logic applies to: - shutting down while network thread is waiting for dialogs - shuttind down while network thread waiting for animations in combat
78 lines
1.2 KiB
C++
78 lines
1.2 KiB
C++
/*
|
|
* ConditionalWait.h, part of VCMI engine
|
|
*
|
|
* Authors: listed in file AUTHORS in main folder
|
|
*
|
|
* License: GNU General Public License v2.0 or later
|
|
* Full text of license available in license.txt file, in main folder
|
|
*
|
|
*/
|
|
#pragma once
|
|
|
|
#include <condition_variable>
|
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
class TerminationRequestedException : public std::exception
|
|
{
|
|
public:
|
|
using exception::exception;
|
|
|
|
const char* what() const noexcept override
|
|
{
|
|
return "Thread termination requested";
|
|
}
|
|
};
|
|
|
|
class ConditionalWait
|
|
{
|
|
bool isBusyValue = false;
|
|
bool isTerminating = false;
|
|
std::condition_variable cond;
|
|
std::mutex mx;
|
|
|
|
void set(bool value)
|
|
{
|
|
boost::unique_lock<std::mutex> lock(mx);
|
|
isBusyValue = value;
|
|
}
|
|
|
|
public:
|
|
ConditionalWait() = default;
|
|
|
|
void setBusy()
|
|
{
|
|
set(true);
|
|
}
|
|
|
|
void setFree()
|
|
{
|
|
set(false);
|
|
cond.notify_all();
|
|
}
|
|
|
|
void requestTermination()
|
|
{
|
|
isTerminating = true;
|
|
setFree();
|
|
}
|
|
|
|
bool isBusy()
|
|
{
|
|
std::unique_lock<std::mutex> lock(mx);
|
|
return isBusyValue;
|
|
}
|
|
|
|
void waitWhileBusy()
|
|
{
|
|
std::unique_lock<std::mutex> un(mx);
|
|
while(isBusyValue)
|
|
cond.wait(un);
|
|
|
|
if (isTerminating)
|
|
throw TerminationRequestedException();
|
|
}
|
|
};
|
|
|
|
VCMI_LIB_NAMESPACE_END
|