Skip to content

Commit

Permalink
Wrap std::thread in a holder that joins it on destruction.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Marsh committed Nov 28, 2017
1 parent 49b2304 commit 9130707
Showing 1 changed file with 46 additions and 32 deletions.
78 changes: 46 additions & 32 deletions src/discord-rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,50 @@ static int Pid{0};
static int Nonce{1};

#ifndef DISCORD_DISABLE_IO_THREAD
static std::atomic_bool KeepRunning{true};
static std::mutex WaitForIOMutex;
static std::condition_variable WaitForIOActivity;
static std::thread IoThread;
static void Discord_UpdateConnection(void);
class IoThreadHolder {
private:
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
static IoThreadHolder IoThread;

static void UpdateReconnectTime()
{
Expand Down Expand Up @@ -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()
{
#ifndef DISCORD_DISABLE_IO_THREAD
WaitForIOActivity.notify_all();
#endif
IoThread.Notify();
}

static bool RegisterForEvent(const char* evtName)
Expand Down Expand Up @@ -274,10 +297,7 @@ extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
UpdateReconnectTime();
};

#ifndef DISCORD_DISABLE_IO_THREAD
KeepRunning.store(true);
IoThread = std::thread(DiscordRpcIo);
#endif
IoThread.Start();
}

extern "C" DISCORD_EXPORT void Discord_Shutdown()
Expand All @@ -288,13 +308,7 @@ extern "C" DISCORD_EXPORT void Discord_Shutdown()
Connection->onConnect = nullptr;
Connection->onDisconnect = nullptr;
Handlers = {};
#ifndef DISCORD_DISABLE_IO_THREAD
KeepRunning.exchange(false);
SignalIOActivity();
if (IoThread.joinable()) {
IoThread.join();
}
#endif
IoThread.Stop();
RpcConnection::Destroy(Connection);
}

Expand Down

0 comments on commit 9130707

Please sign in to comment.