diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index 44a7f521f2..aad6d024e3 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -178,6 +178,11 @@ class DPP_EXPORT cluster { */ std::unique_ptr pool{nullptr}; + /** + * @brief Used to spawn the socket engine into its own thread if + * the cluster is started with dpp::st_return. It is unused otherwise. + */ + std::unique_ptr engine_thread{nullptr}; public: /** @@ -233,9 +238,13 @@ class DPP_EXPORT cluster { websocket_protocol_t ws_mode; /** - * @brief Condition variable notified when the cluster is terminating. + * @brief Atomic bool to set to true when the cluster is terminating. + * + * D++ itself does not set this value, it is for library users to set if they want + * the cluster to terminate outside of a flow where they may have simple access to + * destruct the cluster object. */ - std::condition_variable terminating; + std::atomic_bool terminating{false}; /** * @brief The time (in seconds) that a request is allowed to take. diff --git a/src/dpp/cluster.cpp b/src/dpp/cluster.cpp index 9c804a9075..a7fdf7d975 100644 --- a/src/dpp/cluster.cpp +++ b/src/dpp/cluster.cpp @@ -202,10 +202,10 @@ dpp::utility::uptime cluster::uptime() void cluster::start(bool return_after) { - auto block_calling_thread = [this]() { - std::mutex thread_mutex; - std::unique_lock thread_lock(thread_mutex); - this->terminating.wait(thread_lock); + auto event_loop = [this]() -> void { + do { + socketengine->process_events(); + } while (!this->terminating); }; if (on_guild_member_add && !(intents & dpp::i_guild_members)) { @@ -300,13 +300,13 @@ void cluster::start(bool return_after) { log(ll_debug, "Shards started."); }); - do { - // TODO: Thread this - socketengine->process_events(); - } while (true); - - if (!return_after) { - block_calling_thread(); + if (return_after) { + engine_thread = std::make_unique(std::jthread([event_loop]() { + dpp::utility::set_thread_name("event_loop"); + event_loop(); + })); + } else { + event_loop(); } } diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index 5c586da5e9..6c89f89358 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -1163,12 +1163,6 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b } }); - set_test(SYNC, false); - if (!offline) { - dpp::message m = dpp::sync(&bot, &dpp::cluster::message_create, dpp::message(TEST_TEXT_CHANNEL_ID, "TEST")); - set_test(SYNC, m.content == "TEST"); - } - bot.on_guild_create([&](const dpp::guild_create_t & event) { if (event.created->id == TEST_GUILD_ID) { set_test(GUILDCREATE, true); diff --git a/src/unittest/test.h b/src/unittest/test.h index 97637b7711..a687287623 100644 --- a/src/unittest/test.h +++ b/src/unittest/test.h @@ -144,7 +144,6 @@ DPP_TEST(MD_ESC_1, "Markdown escaping (ignore code block contents)", tf_offline) DPP_TEST(MD_ESC_2, "Markdown escaping (escape code block contents)", tf_offline); DPP_TEST(URLENC, "URL encoding", tf_offline); DPP_TEST(BASE64ENC, "Base 64 encoding", tf_offline); -DPP_TEST(SYNC, "sync()", tf_online); DPP_TEST(COMPARISON, "manged object comparison", tf_offline); DPP_TEST(CHANNELCACHE, "find_channel()", tf_online); DPP_TEST(CHANNELTYPES, "channel type flags", tf_online);