Wrap std::thread in a holder that joins it on destruction.
This commit is contained in:
parent
49b23040c6
commit
9130707086
1 changed files with 46 additions and 32 deletions
|
@ -72,11 +72,50 @@ static int Pid{0};
|
||||||
static int Nonce{1};
|
static int Nonce{1};
|
||||||
|
|
||||||
#ifndef DISCORD_DISABLE_IO_THREAD
|
#ifndef DISCORD_DISABLE_IO_THREAD
|
||||||
static std::atomic_bool KeepRunning{true};
|
static void Discord_UpdateConnection(void);
|
||||||
static std::mutex WaitForIOMutex;
|
class IoThreadHolder {
|
||||||
static std::condition_variable WaitForIOActivity;
|
private:
|
||||||
static std::thread IoThread;
|
std::atomic_bool keepRunning{true};
|
||||||
|
std::mutex waitForIOMutex;
|
||||||
|
std::condition_variable waitForIOActivity;
|
||||||
|
std::thread ioThread;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
keepRunning.store(true);
|
||||||
|
ioThread = std::thread([&]() {
|
||||||
|
const std::chrono::duration<int64_t, std::milli> maxWait{500LL};
|
||||||
|
while (keepRunning.load()) {
|
||||||
|
Discord_UpdateConnection();
|
||||||
|
std::unique_lock<std::mutex> lock(waitForIOMutex);
|
||||||
|
waitForIOActivity.wait_for(lock, maxWait);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void Notify() { waitForIOActivity.notify_all(); }
|
||||||
|
|
||||||
|
void Stop()
|
||||||
|
{
|
||||||
|
keepRunning.exchange(false);
|
||||||
|
Notify();
|
||||||
|
if (ioThread.joinable()) {
|
||||||
|
ioThread.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~IoThreadHolder() { Stop(); }
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
class IoThreadHolder {
|
||||||
|
public:
|
||||||
|
void Start() {}
|
||||||
|
void Stop() {}
|
||||||
|
void Notify() {}
|
||||||
|
};
|
||||||
#endif // DISCORD_DISABLE_IO_THREAD
|
#endif // DISCORD_DISABLE_IO_THREAD
|
||||||
|
static IoThreadHolder IoThread;
|
||||||
|
|
||||||
static void UpdateReconnectTime()
|
static void UpdateReconnectTime()
|
||||||
{
|
{
|
||||||
|
@ -189,25 +228,9 @@ static void Discord_UpdateConnection(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef DISCORD_DISABLE_IO_THREAD
|
|
||||||
static void DiscordRpcIo(void)
|
|
||||||
{
|
|
||||||
const std::chrono::duration<int64_t, std::milli> maxWait{500LL};
|
|
||||||
|
|
||||||
while (KeepRunning.load()) {
|
|
||||||
Discord_UpdateConnection();
|
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(WaitForIOMutex);
|
|
||||||
WaitForIOActivity.wait_for(lock, maxWait);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static void SignalIOActivity()
|
static void SignalIOActivity()
|
||||||
{
|
{
|
||||||
#ifndef DISCORD_DISABLE_IO_THREAD
|
IoThread.Notify();
|
||||||
WaitForIOActivity.notify_all();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool RegisterForEvent(const char* evtName)
|
static bool RegisterForEvent(const char* evtName)
|
||||||
|
@ -274,10 +297,7 @@ extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
|
||||||
UpdateReconnectTime();
|
UpdateReconnectTime();
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifndef DISCORD_DISABLE_IO_THREAD
|
IoThread.Start();
|
||||||
KeepRunning.store(true);
|
|
||||||
IoThread = std::thread(DiscordRpcIo);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" DISCORD_EXPORT void Discord_Shutdown()
|
extern "C" DISCORD_EXPORT void Discord_Shutdown()
|
||||||
|
@ -288,13 +308,7 @@ extern "C" DISCORD_EXPORT void Discord_Shutdown()
|
||||||
Connection->onConnect = nullptr;
|
Connection->onConnect = nullptr;
|
||||||
Connection->onDisconnect = nullptr;
|
Connection->onDisconnect = nullptr;
|
||||||
Handlers = {};
|
Handlers = {};
|
||||||
#ifndef DISCORD_DISABLE_IO_THREAD
|
IoThread.Stop();
|
||||||
KeepRunning.exchange(false);
|
|
||||||
SignalIOActivity();
|
|
||||||
if (IoThread.joinable()) {
|
|
||||||
IoThread.join();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
RpcConnection::Destroy(Connection);
|
RpcConnection::Destroy(Connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue