Module: check_mk
Branch: master
Commit: 0d19eecc11dd50827edf14446acca699e2b203ea
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0d19eecc11dd50…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Fri Nov 13 17:25:53 2015 +0100
Added lock_guard.
TODO: Nuke (un)lockLogCache.
---
livestatus/src/ClientQueue.cc | 12 +++++++-----
livestatus/src/Mutex.h | 23 ++++++++++++++++++++---
livestatus/src/Store.cc | 5 +++--
livestatus/src/TimeperiodsCache.cc | 20 +++++++-------------
4 files changed, 37 insertions(+), 23 deletions(-)
diff --git a/livestatus/src/ClientQueue.cc b/livestatus/src/ClientQueue.cc
index 38d003b..7e44ec2 100644
--- a/livestatus/src/ClientQueue.cc
+++ b/livestatus/src/ClientQueue.cc
@@ -25,6 +25,8 @@
#include "ClientQueue.h"
#include <unistd.h>
+using mk::lock_guard;
+using mk::mutex;
ClientQueue::ClientQueue()
{
@@ -44,16 +46,17 @@ ClientQueue::~ClientQueue()
void ClientQueue::addConnection(int fd)
{
- _lock.lock();
- _queue.push_back(fd);
- _lock.unlock();
+ {
+ lock_guard<mutex> lg(_lock);
+ _queue.push_back(fd);
+ }
pthread_cond_signal(&_signal);
}
int ClientQueue::popConnection()
{
- _lock.lock();
+ lock_guard<mutex> lg(_lock);
if (_queue.size() == 0) {
pthread_cond_wait(&_signal, _lock.native_handle());
}
@@ -63,7 +66,6 @@ int ClientQueue::popConnection()
fd = _queue.front();
_queue.pop_front();
}
- _lock.unlock();
return fd;
}
diff --git a/livestatus/src/Mutex.h b/livestatus/src/Mutex.h
index c19a8f6..c19852a 100644
--- a/livestatus/src/Mutex.h
+++ b/livestatus/src/Mutex.h
@@ -39,7 +39,6 @@ namespace mk {
class mutex {
public:
typedef pthread_mutex_t *native_handle_type;
-
mutex() : _mutex(PTHREAD_MUTEX_INITIALIZER) {}
~mutex() {}
void lock() { check(pthread_mutex_lock(native_handle())); }
@@ -49,7 +48,6 @@ public:
if (status != EBUSY) check(status);
return status == 0;
}
-
void unlock() { check(pthread_mutex_unlock(native_handle())); }
native_handle_type native_handle() { return &_mutex; }
private:
@@ -65,5 +63,24 @@ private:
pthread_mutex_t _mutex;
};
-}
+
+struct adopt_lock_t { };
+// constexpr adopt_lock_t adopt_lock { };
+
+template <typename Mutex>
+class lock_guard {
+public:
+ typedef Mutex mutex_type;
+ explicit lock_guard(mutex_type &m) : _mutex(m) { _mutex.lock(); }
+ lock_guard(mutex_type &m, adopt_lock_t) : _mutex(m) {}
+ ~lock_guard() { _mutex.unlock(); }
+private:
+ lock_guard(const lock_guard &); // = delete
+ lock_guard &operator=(const lock_guard &); // = delete
+
+ mutex_type &_mutex;
+};
+
+} // namespace mk
+
#endif // Mutex_h
diff --git a/livestatus/src/Store.cc b/livestatus/src/Store.cc
index aae00d1..75dc517 100644
--- a/livestatus/src/Store.cc
+++ b/livestatus/src/Store.cc
@@ -43,6 +43,8 @@
#include "tables.h" // IWYU pragma: keep
#undef EXTERN
+using mk::lock_guard;
+using mk::mutex;
using std::make_pair;
using std::string;
@@ -164,7 +166,7 @@ bool Store::answerRequest(InputBuffer *input, OutputBuffer *output)
void Store::answerCommandRequest(const char *command)
{
- _command_mutex.lock();
+ lock_guard<mutex> lg(_command_mutex);
#ifdef NAGIOS4
process_external_command1((char *)command);
#else
@@ -172,7 +174,6 @@ void Store::answerCommandRequest(const char *command)
/* int ret = */
submit_external_command((char *)command, &buffer_items);
#endif
- _command_mutex.unlock();
}
diff --git a/livestatus/src/TimeperiodsCache.cc b/livestatus/src/TimeperiodsCache.cc
index e72563b..6bb787f 100644
--- a/livestatus/src/TimeperiodsCache.cc
+++ b/livestatus/src/TimeperiodsCache.cc
@@ -30,8 +30,9 @@
#include <utility>
#include "logger.h"
-
using std::make_pair;
+using mk::lock_guard;
+using mk::mutex;
extern timeperiod *timeperiod_list;
@@ -46,7 +47,7 @@ TimeperiodsCache::~TimeperiodsCache()
}
void TimeperiodsCache::logCurrentTimeperiods(){
- _cache_lock.lock();
+ lock_guard<mutex> lg(_cache_lock);
time_t now = time(0);
// Loop over all timeperiods and compute if we are
// currently in. Detect the case where no time periods
@@ -65,21 +66,17 @@ void TimeperiodsCache::logCurrentTimeperiods(){
logTransition(tp->name, it->second ? 1 : 0, is_in ? 1 : 0);
tp = tp->next;
}
- _cache_lock.unlock();
}
void TimeperiodsCache::update(time_t now)
{
- _cache_lock.lock();
+ lock_guard<mutex> lg(_cache_lock);
// update cache only once a minute. The timeperiod
// definitions have 1 minute as granularity, so a
// 1sec resultion is not needed.
int minutes = now / 60;
- if (minutes == _cache_time) {
- _cache_lock.unlock();
- return;
- }
+ if (minutes == _cache_time) return;
// Loop over all timeperiods and compute if we are
// currently in. Detect the case where no time periods
@@ -109,8 +106,6 @@ void TimeperiodsCache::update(time_t now)
_cache_time = minutes;
else
logger(LG_INFO, "Timeperiod cache not updated, there are no timeperiods
(yet)");
-
- _cache_lock.unlock();
}
bool TimeperiodsCache::inTimeperiod(const char *tpname)
@@ -127,9 +122,9 @@ bool TimeperiodsCache::inTimeperiod(const char *tpname)
bool TimeperiodsCache::inTimeperiod(timeperiod *tp)
{
- bool is_in;
- _cache_lock.lock();
+ lock_guard<mutex> lg(_cache_lock);
_cache_t::iterator it = _cache.find(tp);
+ bool is_in;
if (it != _cache.end())
is_in = it->second;
else {
@@ -141,7 +136,6 @@ bool TimeperiodsCache::inTimeperiod(timeperiod *tp)
// time_t now = time(0);
// is_in = 0 == check_time_against_period(now, tp);
}
- _cache_lock.unlock();
return is_in;
}