From: Maksym Mamontov Date: Fri, 22 Jul 2022 19:39:12 +0000 (+0300) Subject: More jthreads. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/c02633d77cb05a5deb05440d77b12ccc5bc19b85?ds=sidebyside More jthreads. --- diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp index 2d45f109..df5ae9e4 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp @@ -86,7 +86,6 @@ RPC_CONFIG::RPC_CONFIG() store(NULL), fd(-1), rpcServer(NULL), - running(false), stopped(true), dayFee(0), logger(STG::PluginLogger::get("conf_rpc")) @@ -121,7 +120,6 @@ void RPC_CONFIG::SetStgSettings(const STG::Settings * s) int RPC_CONFIG::Start() { InitiateRegistry(); -running = true; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) @@ -147,7 +145,7 @@ addr.sin_family = AF_INET; addr.sin_port = htons(rpcConfigSettings.GetPort()); addr.sin_addr.s_addr = inet_addr("0.0.0.0"); -if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) +if (bind(fd, reinterpret_cast(&addr), sizeof(addr))) { logger("Cannot bind the socket: %s", strerror(errno)); errorStr = "Failed to bind socket"; @@ -170,20 +168,14 @@ rpcServer = new xmlrpc_c::serverAbyss( .socketFd(fd) ); -if (pthread_create(&tid, NULL, Run, this)) - { - errorStr = "Failed to create RPC thread"; - logger("Cannot create RPC thread."); - printfd(__FILE__, "Failed to crate RPC thread\n"); - return -1; - } +m_thread = std::jthread([this](auto token){ Run(std::move(token)); }); return 0; } int RPC_CONFIG::Stop() { -running = false; +m_thread.request_stop(); for (int i = 0; i < 5 && !stopped; ++i) { struct timespec ts = {0, 200000000}; @@ -192,39 +184,33 @@ for (int i = 0; i < 5 && !stopped; ++i) if (!stopped) { - running = true; + m_thread.detach(); logger("Cannot stop RPC thread."); printfd(__FILE__, "Failed to stop RPC thread\n"); errorStr = "Failed to stop RPC thread"; return -1; } else - { - pthread_join(tid, NULL); - } + m_thread.join(); close(fd); return 0; } -void * RPC_CONFIG::Run(void * rc) +void RPC_CONFIG::Run(std::stop_token token) { sigset_t signalSet; sigfillset(&signalSet); pthread_sigmask(SIG_BLOCK, &signalSet, NULL); -RPC_CONFIG * config = static_cast(rc); - -config->stopped = false; -while (config->running) +stopped = false; +while (!token.stop_requested()) { - if (WaitPackets(config->fd)) - config->rpcServer->runOnce(); + if (WaitPackets(fd)) + rpcServer->runOnce(); } -config->stopped = true; - -return NULL; +stopped = true; } bool RPC_CONFIG::GetAdminInfo(const std::string & cookie, diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h index e7c800d5..707c30d5 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h @@ -15,7 +15,10 @@ #include #include -#include +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wshadow" +#include +#pragma GCC diagnostic pop #define RPC_CONFIG_VERSION "Stargazer RPC v. 0.2" @@ -75,7 +78,7 @@ public: int Start() override; int Stop() override; int Reload(const STG::ModuleSettings & /*ms*/) override { return 0; } - bool IsRunning() override { return running && !stopped; } + bool IsRunning() override { return m_thread.joinable() && !stopped; } const std::string & GetStrError() const override { return errorStr; } std::string GetVersion() const override { return RPC_CONFIG_VERSION; } @@ -93,7 +96,7 @@ private: RPC_CONFIG(const RPC_CONFIG & rvalue); RPC_CONFIG & operator=(const RPC_CONFIG & rvalue); - static void * Run(void *); + void Run(std::stop_token token); std::string GetCookie() const; void InitiateRegistry(); @@ -107,9 +110,8 @@ private: int fd; xmlrpc_c::registry rpcRegistry; xmlrpc_c::serverAbyss * rpcServer; - bool running; bool stopped; - pthread_t tid; + std::jthread m_thread; std::map cookies; size_t dayFee; diff --git a/projects/stargazer/stg_timer.cpp b/projects/stargazer/stg_timer.cpp index 572b069a..7a460ddf 100644 --- a/projects/stargazer/stg_timer.cpp +++ b/projects/stargazer/stg_timer.cpp @@ -2,16 +2,26 @@ #include "stg/common.h" +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wshadow" +#include +#pragma GCC diagnostic pop + #include #include #include #include +namespace +{ + +std::jthread thread; + +} + void * StgTimer(void *); -static int nonstop; -static pthread_t thrStgTimer; static bool isTimerRunning = false; volatile time_t stgTime; @@ -33,7 +43,7 @@ const int START_TIME = 2; #endif //----------------------------------------------------------------------------- -void * StgTimer(void *) +void timer(std::stop_token token) { #ifdef STG_TIMER_DEBUG struct tm lt; @@ -70,9 +80,8 @@ sigset_t signalSet; sigfillset(&signalSet); pthread_sigmask(SIG_BLOCK, &signalSet, NULL); -nonstop = 1; isTimerRunning = true; -while (nonstop) +while (!token.stop_requested()) { #ifdef STG_TIMER_DEBUG struct timespec ts; @@ -95,30 +104,22 @@ while (nonstop) #endif } isTimerRunning = false; - -return NULL; } //----------------------------------------------------------------------------- int RunStgTimer() { -static int a = 0; isTimerRunning = false; -if (a == 0) - if (pthread_create(&thrStgTimer, NULL, &StgTimer, NULL)) - { - isTimerRunning = false; - return -1; - } +if (!thread.joinable()) + thread = std::jthread(timer); -a = 1; return 0; } //----------------------------------------------------------------------------- void StopStgTimer() { -nonstop = 0; -pthread_join(thrStgTimer, NULL); // Cleanup thread resources +thread.request_stop(); +thread.join(); printfd(__FILE__, "STG_TIMER stopped\n"); } //-----------------------------------------------------------------------------