]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / stgconfig.cpp
index 73f58145f6beb23bcf1986575362b844d602dafb..4666575c1ec4c7191903f86a1e621653173bd122 100644 (file)
@@ -20,7 +20,6 @@
 
 #include "stgconfig.h"
 
 
 #include "stgconfig.h"
 
-#include "stg/plugin_creator.h"
 #include "stg/common.h"
 
 #include <algorithm>
 #include "stg/common.h"
 
 #include <algorithm>
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-static PLUGIN_CREATOR<STG_CONFIG> stgc;
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-bool STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
+bool STG_CONFIG_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
 {
 {
-    PARAM_VALUE pv;
-    std::vector<PARAM_VALUE>::const_iterator pvi;
+    STG::ParamValue pv;
+    std::vector<STG::ParamValue>::const_iterator pvi;
     ///////////////////////////
     pv.param = "Port";
     pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
     ///////////////////////////
     pv.param = "Port";
     pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
@@ -67,9 +62,10 @@ bool STG_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-extern "C" PLUGIN * GetPlugin()
+extern "C" STG::Plugin * GetPlugin()
 {
 {
-return stgc.GetPlugin();
+    static STG_CONFIG plugin;
+    return &plugin;
 }
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 }
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -77,7 +73,7 @@ return stgc.GetPlugin();
 STG_CONFIG::STG_CONFIG()
     : nonstop(false),
       isRunning(false),
 STG_CONFIG::STG_CONFIG()
     : nonstop(false),
       isRunning(false),
-      logger(GetPluginLogger(GetStgLogger(), "conf_sg")),
+      logger(STG::PluginLogger::get("conf_sg")),
       config(logger)
 {
 }
       config(logger)
 {
 }
@@ -106,13 +102,7 @@ int STG_CONFIG::Start()
         return -1;
     }
 
         return -1;
     }
 
-    if (pthread_create(&thread, NULL, Run, this))
-    {
-        errorStr = std::string("Cannot create thread: '") + strerror(errno) + "'.";
-        printfd(__FILE__, "%s\n", errorStr.c_str());
-        logger(errorStr);
-        return -1;
-    }
+    m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
 
     return 0;
 }
 
     return 0;
 }
@@ -123,6 +113,7 @@ int STG_CONFIG::Stop()
         return 0;
 
     config.Stop();
         return 0;
 
     config.Stop();
+    m_thread.request_stop();
 
     //5 seconds to thread stops itself
     for (size_t i = 0; i < 25; ++i)
 
     //5 seconds to thread stops itself
     for (size_t i = 0; i < 25; ++i)
@@ -135,23 +126,22 @@ int STG_CONFIG::Stop()
     }
 
     if (isRunning)
     }
 
     if (isRunning)
-        return -1;
+        m_thread.detach();
+    else
+        m_thread.join();
 
     return 0;
 }
 //-----------------------------------------------------------------------------
 
     return 0;
 }
 //-----------------------------------------------------------------------------
-void * STG_CONFIG::Run(void * d)
+void STG_CONFIG::Run(std::stop_token token)
 {
     sigset_t signalSet;
     sigfillset(&signalSet);
     pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
 
 {
     sigset_t signalSet;
     sigfillset(&signalSet);
     pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
 
-    STG_CONFIG & stgConf = *static_cast<STG_CONFIG *>(d);
-    stgConf.isRunning = true;
-
-    stgConf.config.Run();
+    isRunning = true;
 
 
-    stgConf.isRunning = false;
+    config.Run(token);
 
 
-    return NULL;
+    isRunning = false;
 }
 }