]> granicus.if.org Git - icinga2/commitdiff
DaemonCommand: make the atomics a bit more atomic
authorAlexander A. Klimov <alexander.klimov@icinga.com>
Tue, 16 Jul 2019 09:28:20 +0000 (11:28 +0200)
committerAlexander A. Klimov <alexander.klimov@icinga.com>
Tue, 16 Jul 2019 09:39:09 +0000 (11:39 +0200)
Just to be sure.

refs #5230

lib/base/CMakeLists.txt
lib/base/atomic.hpp [new file with mode: 0644]
lib/cli/daemoncommand.cpp

index fb3de302947da403056e4c8518560e545f11d606..100ca27a7517f56ecefe832dfc8d468f019477a8 100644 (file)
@@ -15,6 +15,7 @@ set(base_SOURCES
   i2-base.hpp
   application.cpp application.hpp application-ti.hpp application-version.cpp application-environment.cpp
   array.cpp array.hpp array-script.cpp
+  atomic.hpp
   base64.cpp base64.hpp
   boolean.cpp boolean.hpp boolean-script.cpp
   configobject.cpp configobject.hpp configobject-ti.hpp configobject-script.cpp
diff --git a/lib/base/atomic.hpp b/lib/base/atomic.hpp
new file mode 100644 (file)
index 0000000..0ebcdde
--- /dev/null
@@ -0,0 +1,43 @@
+/* Icinga 2 | (c) 2019 Icinga GmbH | GPLv2+ */
+
+#ifndef ATOMIC_H
+#define ATOMIC_H
+
+#include <atomic>
+
+namespace icinga
+{
+
+/**
+ * Extends std::atomic with an atomic constructor.
+ *
+ * @ingroup base
+ */
+template<class T>
+class Atomic : public std::atomic<T> {
+public:
+       /**
+        * Like std::atomic#atomic, but operates atomically
+        *
+        * @param desired Initial value
+        */
+       inline Atomic(T desired)
+       {
+               this->store(desired);
+       }
+
+       /**
+        * Like std::atomic#atomic, but operates atomically
+        *
+        * @param desired Initial value
+        * @param order Initial store operation's memory order
+        */
+       inline Atomic(T desired, std::memory_order order)
+       {
+               this->store(desired, order);
+       }
+};
+
+}
+
+#endif /* ATOMIC_H */
index 33a6343f1001b8cab1a8329c1a812c6dc8139bb6..cdc105dc60eb048ae0972d31ae91645b3703c1fd 100644 (file)
@@ -7,6 +7,7 @@
 #include "config/configcompiler.hpp"
 #include "config/configcompilercontext.hpp"
 #include "config/configitembuilder.hpp"
+#include "base/atomic.hpp"
 #include "base/defer.hpp"
 #include "base/logger.hpp"
 #include "base/application.hpp"
@@ -17,7 +18,6 @@
 #include "base/scriptglobal.hpp"
 #include "base/context.hpp"
 #include "config.h"
-#include <atomic>
 #include <cstdint>
 #include <cstring>
 #include <boost/program_options.hpp>
@@ -186,7 +186,7 @@ std::vector<String> DaemonCommand::GetArgumentSuggestions(const String& argument
 pid_t l_UmbrellaPid = 0;
 
 // Whether the umbrella process allowed us to continue working beyond config validation
-static std::atomic<bool> l_AllowedToWork (false);
+static Atomic<bool> l_AllowedToWork (false);
 #endif /* _WIN32 */
 
 /**
@@ -284,19 +284,19 @@ static const sigset_t l_UnixWorkerSignals = ([]() -> sigset_t {
 })();
 
 // The PID of the seemless worker currently being started by StartUnixWorker()
-static std::atomic<pid_t> l_CurrentlyStartingUnixWorkerPid (-1);
+static Atomic<pid_t> l_CurrentlyStartingUnixWorkerPid (-1);
 
 // The state of the seemless worker currently being started by StartUnixWorker()
-static std::atomic<UnixWorkerState> l_CurrentlyStartingUnixWorkerState (UnixWorkerState::Pending);
+static Atomic<UnixWorkerState> l_CurrentlyStartingUnixWorkerState (UnixWorkerState::Pending);
 
 // The last temination signal we received
-static std::atomic<int> l_TermSignal (-1);
+static Atomic<int> l_TermSignal (-1);
 
 // Whether someone requested to re-load config (and we didn't handle that request, yet)
-static std::atomic<bool> l_RequestedReload (false);
+static Atomic<bool> l_RequestedReload (false);
 
 // Whether someone requested to re-open logs (and we didn't handle that request, yet)
-static std::atomic<bool> l_RequestedReopenLogs (false);
+static Atomic<bool> l_RequestedReopenLogs (false);
 
 /**
  * Umbrella process' signal handlers
@@ -374,7 +374,7 @@ static void WorkerSignalHandler(int num, siginfo_t *info, void*)
 
 #ifdef HAVE_SYSTEMD
 // When we last notified the watchdog.
-static std::atomic<double> l_LastNotifiedWatchdog (0);
+static Atomic<double> l_LastNotifiedWatchdog (0);
 
 /**
  * Notify the watchdog if not notified during the last 2.5s.