Module: check_mk
Branch: master
Commit: f29eed5787f3f4e4cc51fc2ac8e7cf9636dd01a2
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=f29eed5787f3f4…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Fri Nov 17 09:01:35 2017 +0100
Improved typing of wait triggers, simplifying things quite a bit.
Change-Id: Ia3906ad8a5b36fb1c7a254469e95fc799e56ff77
---
livestatus/src/Query.cc | 14 +++------
livestatus/src/Query.h | 2 +-
livestatus/src/waittriggers.cc | 65 +++++++++++++-----------------------------
livestatus/src/waittriggers.h | 29 ++++++++-----------
4 files changed, 37 insertions(+), 73 deletions(-)
diff --git a/livestatus/src/Query.cc b/livestatus/src/Query.cc
index b711dd9..1c75c4f 100644
--- a/livestatus/src/Query.cc
+++ b/livestatus/src/Query.cc
@@ -497,13 +497,7 @@ void Query::parseWaitTimeoutLine(char *line) {
}
void Query::parseWaitTriggerLine(char *line) {
- auto value = nextStringArgument(&line);
- struct trigger *t = trigger_find(value);
- if (t == nullptr) {
- throw std::runtime_error("invalid trigger '" + value +
- "', allowed: " + trigger_all_names());
- }
- _wait_trigger = t;
+ _wait_trigger = &trigger_find(nextStringArgument(&line));
}
void Query::parseWaitObjectLine(char *line) {
@@ -717,15 +711,15 @@ void Query::doWait() {
}
std::cv_status Query::waitForTrigger() const {
- auto trigger = _wait_trigger == nullptr ? trigger_all() : _wait_trigger;
+ auto trigger = _wait_trigger == nullptr ? &trigger_all() : _wait_trigger;
if (_wait_timeout == std::chrono::milliseconds(0)) {
Debug(_logger) << "waiting until condition becomes true";
- trigger_wait(trigger);
+ trigger_wait(*trigger);
return std::cv_status::no_timeout;
}
Debug(_logger) << "waiting " << _wait_timeout.count()
<< "ms or until condition becomes true";
- auto status = trigger_wait_for(trigger, _wait_timeout);
+ auto status = trigger_wait_for(*trigger, _wait_timeout);
if (status == std::cv_status::timeout) {
Debug(_logger) << "wait timeout after " <<
_wait_timeout.count()
<< "ms";
diff --git a/livestatus/src/Query.h b/livestatus/src/Query.h
index e227b17..6d080f1 100644
--- a/livestatus/src/Query.h
+++ b/livestatus/src/Query.h
@@ -93,7 +93,7 @@ private:
bool _wait_conditions_empty; // TODO(sp): HACK, remove me...
std::unique_ptr<AndingFilter> _wait_condition;
std::chrono::milliseconds _wait_timeout;
- struct trigger *_wait_trigger;
+ std::condition_variable *_wait_trigger;
Row _wait_object;
CSVSeparators _separators;
bool _show_column_headers;
diff --git a/livestatus/src/waittriggers.cc b/livestatus/src/waittriggers.cc
index a294d9f..915626a 100644
--- a/livestatus/src/waittriggers.cc
+++ b/livestatus/src/waittriggers.cc
@@ -25,16 +25,9 @@
#include "waittriggers.h"
#include <mutex>
#include <ratio>
+#include <stdexcept>
namespace {
-struct trigger *to_trigger(std::condition_variable *c) {
- return reinterpret_cast<struct trigger *>(c);
-}
-
-std::condition_variable *from_trigger(struct trigger *c) {
- return reinterpret_cast<std::condition_variable *>(c);
-}
-
std::mutex wait_mutex;
std::condition_variable cond_all;
@@ -47,39 +40,23 @@ std::condition_variable cond_command;
std::condition_variable cond_program;
} // namespace
-struct trigger *trigger_all() {
- return to_trigger(&cond_all);
-}
+std::condition_variable &trigger_all() { return cond_all; }
-struct trigger *trigger_check() {
- return to_trigger(&cond_check);
-}
+std::condition_variable &trigger_check() { return cond_check; }
-struct trigger *trigger_state() {
- return to_trigger(&cond_state);
-}
+std::condition_variable &trigger_state() { return cond_state; }
-struct trigger *trigger_log() {
- return to_trigger(&cond_log);
-}
+std::condition_variable &trigger_log() { return cond_log; }
-struct trigger *trigger_downtime() {
- return to_trigger(&cond_downtime);
-}
+std::condition_variable &trigger_downtime() { return cond_downtime; }
-struct trigger *trigger_comment() {
- return to_trigger(&cond_comment);
-}
+std::condition_variable &trigger_comment() { return cond_comment; }
-struct trigger *trigger_command() {
- return to_trigger(&cond_command);
-}
+std::condition_variable &trigger_command() { return cond_command; }
-struct trigger *trigger_program() {
- return to_trigger(&cond_program);
-}
+std::condition_variable &trigger_program() { return cond_program; }
-struct trigger *trigger_find(const std::string &name) {
+std::condition_variable &trigger_find(const std::string &name) {
if (name == "all") {
return trigger_all();
}
@@ -104,25 +81,23 @@ struct trigger *trigger_find(const std::string &name) {
if (name == "program") {
return trigger_program();
}
- return nullptr;
-}
-
-const char *trigger_all_names() {
- return "all, check, state, log, downtime, comment, command and program";
+ throw std::runtime_error(
+ "invalid trigger '" + name +
+ "', allowed: all, check, state, log, downtime, comment, command and
program");
}
-void trigger_notify_all(struct trigger *which) {
- from_trigger(trigger_all())->notify_all();
- from_trigger(which)->notify_all();
+void trigger_notify_all(std::condition_variable &cond) {
+ trigger_all().notify_all();
+ cond.notify_all();
}
-void trigger_wait(struct trigger *which) {
+void trigger_wait(std::condition_variable &cond) {
std::unique_lock<std::mutex> ul(wait_mutex);
- from_trigger(which)->wait(ul);
+ cond.wait(ul);
}
-std::cv_status trigger_wait_for(struct trigger *which,
+std::cv_status trigger_wait_for(std::condition_variable &cond,
std::chrono::milliseconds ms) {
std::unique_lock<std::mutex> ul(wait_mutex);
- return from_trigger(which)->wait_for(ul, ms);
+ return cond.wait_for(ul, ms);
}
diff --git a/livestatus/src/waittriggers.h b/livestatus/src/waittriggers.h
index 0f78a73..8e4a951 100644
--- a/livestatus/src/waittriggers.h
+++ b/livestatus/src/waittriggers.h
@@ -30,26 +30,21 @@
#include <condition_variable>
#include <string>
-// This is basically a C++ class for triggers done the "C way" via an opaque
-// struct, explicit passing of 'this' and using a prefix for names.
-struct trigger;
+std::condition_variable &trigger_all();
+std::condition_variable &trigger_check();
+std::condition_variable &trigger_state();
+std::condition_variable &trigger_log();
+std::condition_variable &trigger_downtime();
+std::condition_variable &trigger_comment();
+std::condition_variable &trigger_command();
+std::condition_variable &trigger_program();
-trigger *trigger_all();
-trigger *trigger_check();
-trigger *trigger_state();
-trigger *trigger_log();
-trigger *trigger_downtime();
-trigger *trigger_comment();
-trigger *trigger_command();
-trigger *trigger_program();
+std::condition_variable &trigger_find(const std::string &name);
-trigger *trigger_find(const std::string &name);
-const char *trigger_all_names();
+void trigger_notify_all(std::condition_variable &cond);
-void trigger_notify_all(struct trigger *which);
-
-void trigger_wait(struct trigger *which);
-std::cv_status trigger_wait_for(struct trigger *which,
+void trigger_wait(std::condition_variable &cond);
+std::cv_status trigger_wait_for(std::condition_variable &cond,
std::chrono::milliseconds ms);
#endif // waittriggers_h