Make io thread optional

This commit is contained in:
Chris Marsh 2017-07-18 09:47:33 -07:00
parent 6168a36201
commit 0d0485444d
4 changed files with 31 additions and 9 deletions

View file

@ -70,7 +70,11 @@ static void gameLoop() {
++FrustrationLevel; ++FrustrationLevel;
updateDiscordPresence(); updateDiscordPresence();
Discord_Update();
#ifdef DISCORD_DISABLE_IO_THREAD
Discord_UpdateConnection();
#endif
Discord_RunCallbacks();
} }
} }

View file

@ -1,6 +1,8 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
//#define DISCORD_DISABLE_IO_THREAD
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -36,7 +38,12 @@ void Discord_Shutdown();
void Discord_UpdatePresence(const DiscordRichPresence* presence); void Discord_UpdatePresence(const DiscordRichPresence* presence);
/* checks for incoming messages, dispatches callbacks */ /* checks for incoming messages, dispatches callbacks */
void Discord_Update(); void Discord_RunCallbacks();
/* If you disable the lib starting its own io thread, you'll need to call this from your own */
#ifdef DISCORD_DISABLE_IO_THREAD
void Discord_UpdateConnection();
#endif
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */

View file

@ -1,6 +1,6 @@
include_directories(${PROJECT_SOURCE_DIR}/include) include_directories(${PROJECT_SOURCE_DIR}/include)
set(BASE_RPC_SRC ${PROJECT_SOURCE_DIR}/include/discord-rpc.h discord-rpc.cpp rpc_connection.h rpc_connection.cpp yolojson.h connection.h) set(BASE_RPC_SRC ${PROJECT_SOURCE_DIR}/include/discord-rpc.h discord-rpc.cpp rpc_connection.h rpc_connection.cpp yolojson.h connection.h backoff.h)
if(WIN32) if(WIN32)
add_library(discord-rpc-simple STATIC ${PROJECT_SOURCE_DIR}/include/discord-rpc.h discord-rpc-simple.cpp) add_library(discord-rpc-simple STATIC ${PROJECT_SOURCE_DIR}/include/discord-rpc.h discord-rpc-simple.cpp)

View file

@ -25,15 +25,18 @@ static std::atomic_bool WasJustConnected{false};
static std::atomic_bool WasJustDisconnected{false}; static std::atomic_bool WasJustDisconnected{false};
static int LastErrorCode{0}; static int LastErrorCode{0};
static char LastErrorMessage[256]; static char LastErrorMessage[256];
static std::atomic_bool KeepRunning{true};
static std::mutex WaitForIOMutex;
static std::condition_variable WaitForIOActivity;
static std::thread IoThread;
static QueuedMessage SendQueue[MessageQueueSize]{}; static QueuedMessage SendQueue[MessageQueueSize]{};
static std::atomic_uint SendQueueNextAdd{0}; static std::atomic_uint SendQueueNextAdd{0};
static std::atomic_uint SendQueueNextSend{0}; static std::atomic_uint SendQueueNextSend{0};
static std::atomic_uint SendQueuePendingSends{0}; static std::atomic_uint SendQueuePendingSends{0};
#ifndef DISCORD_DISABLE_IO_THREAD
static std::atomic_bool KeepRunning{ true };
static std::mutex WaitForIOMutex;
static std::condition_variable WaitForIOActivity;
static std::thread IoThread;
#endif // DISCORD_DISABLE_IO_THREAD
static QueuedMessage* SendQueueGetNextAddMessage() { static QueuedMessage* SendQueueGetNextAddMessage() {
// if we are falling behind, bail // if we are falling behind, bail
if (SendQueuePendingSends.load() >= MessageQueueSize) { if (SendQueuePendingSends.load() >= MessageQueueSize) {
@ -50,7 +53,7 @@ static void SendQueueCommitMessage() {
SendQueuePendingSends++; SendQueuePendingSends++;
} }
void Discord_UpdateConnection() extern "C" void Discord_UpdateConnection()
{ {
if (!Connection->IsOpen()) { if (!Connection->IsOpen()) {
Connection->Open(); Connection->Open();
@ -72,6 +75,7 @@ void Discord_UpdateConnection()
} }
} }
#ifndef DISCORD_DISABLE_IO_THREAD
void DiscordRpcIo() void DiscordRpcIo()
{ {
const std::chrono::duration<int64_t, std::milli> maxWait{500LL}; const std::chrono::duration<int64_t, std::milli> maxWait{500LL};
@ -83,10 +87,13 @@ void DiscordRpcIo()
WaitForIOActivity.wait_for(lock, maxWait); WaitForIOActivity.wait_for(lock, maxWait);
} }
} }
#endif
void SignalIOActivity() void SignalIOActivity()
{ {
#ifndef DISCORD_DISABLE_IO_THREAD
WaitForIOActivity.notify_all(); WaitForIOActivity.notify_all();
#endif
} }
extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers) extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers)
@ -108,7 +115,9 @@ extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandle
WasJustDisconnected.exchange(true); WasJustDisconnected.exchange(true);
}; };
#ifndef DISCORD_DISABLE_IO_THREAD
IoThread = std::thread(DiscordRpcIo); IoThread = std::thread(DiscordRpcIo);
#endif
} }
extern "C" void Discord_Shutdown() extern "C" void Discord_Shutdown()
@ -116,11 +125,13 @@ extern "C" void Discord_Shutdown()
Connection->onConnect = nullptr; Connection->onConnect = nullptr;
Connection->onDisconnect = nullptr; Connection->onDisconnect = nullptr;
Handlers = {}; Handlers = {};
#ifndef DISCORD_DISABLE_IO_THREAD
KeepRunning.exchange(false); KeepRunning.exchange(false);
SignalIOActivity(); SignalIOActivity();
if (IoThread.joinable()) { if (IoThread.joinable()) {
IoThread.join(); IoThread.join();
} }
#endif
RpcConnection::Destroy(Connection); RpcConnection::Destroy(Connection);
} }
@ -136,7 +147,7 @@ extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence)
} }
} }
extern "C" void Discord_Update() extern "C" void Discord_RunCallbacks()
{ {
if (WasJustDisconnected.exchange(false) && Handlers.disconnected) { if (WasJustDisconnected.exchange(false) && Handlers.disconnected) {
Handlers.disconnected(LastErrorCode, LastErrorMessage); Handlers.disconnected(LastErrorCode, LastErrorMessage);