Module: check_mk
Branch: master
Commit: ff3d5b68a202b49f6558081ad0c78f81af4f5ca6
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=ff3d5b68a202b4…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Wed Nov 18 11:07:07 2015 +0100
ClientQueue uses condition_variable now, fixing a bug with spurious wakeups.
Made all fields private on the way.
---
livestatus/src/ClientQueue.cc | 33 ++++++++++++---------------------
livestatus/src/ClientQueue.h | 12 ++++++------
2 files changed, 18 insertions(+), 27 deletions(-)
diff --git a/livestatus/src/ClientQueue.cc b/livestatus/src/ClientQueue.cc
index 7e44ec2..ab81bac 100644
--- a/livestatus/src/ClientQueue.cc
+++ b/livestatus/src/ClientQueue.cc
@@ -24,52 +24,43 @@
#include "ClientQueue.h"
#include <unistd.h>
+#include <algorithm>
using mk::lock_guard;
using mk::mutex;
+using mk::unique_lock;
+using std::for_each;
ClientQueue::ClientQueue()
{
- pthread_cond_init(&_signal, 0);
}
ClientQueue::~ClientQueue()
{
- for (_queue_t::iterator it = _queue.begin();
- it != _queue.end();
- ++it)
- {
- close(*it);
- }
- pthread_cond_destroy(&_signal);
+ for_each(_queue.begin(), _queue.end(), close);
}
void ClientQueue::addConnection(int fd)
{
{
- lock_guard<mutex> lg(_lock);
+ lock_guard<mutex> lg(_mutex);
_queue.push_back(fd);
}
- pthread_cond_signal(&_signal);
+ _cond.notify_one();
}
int ClientQueue::popConnection()
{
- lock_guard<mutex> lg(_lock);
- if (_queue.size() == 0) {
- pthread_cond_wait(&_signal, _lock.native_handle());
- }
-
- int fd = -1;
- if (_queue.size() > 0) {
- fd = _queue.front();
- _queue.pop_front();
- }
+ unique_lock<mutex> ul(_mutex);
+ while (_queue.empty()) _cond.wait(ul);
+ int fd = _queue.front();
+ _queue.pop_front();
return fd;
}
+// Note: What we *really* want here is the functionality of notify_all_at_thread_exit.
void ClientQueue::wakeupAll()
{
- pthread_cond_broadcast(&_signal);
+ _cond.notify_all();
}
diff --git a/livestatus/src/ClientQueue.h b/livestatus/src/ClientQueue.h
index 0a9bea0..b8a0584 100644
--- a/livestatus/src/ClientQueue.h
+++ b/livestatus/src/ClientQueue.h
@@ -26,24 +26,24 @@
#define ClientQueue_h
#include "config.h" // IWYU pragma: keep
-#include <pthread.h>
#include <deque>
#include <memory>
+#include "ConditionVariable.h"
#include "Mutex.h"
class ClientQueue
{
- typedef std::deque<int> _queue_t;
- _queue_t _queue;
public:
ClientQueue();
~ClientQueue();
void addConnection(int);
int popConnection();
void wakeupAll();
-
- mk::mutex _lock;
- pthread_cond_t _signal;
+private:
+ typedef std::deque<int> _queue_t;
+ _queue_t _queue;
+ mk::mutex _mutex;
+ mk::condition_variable _cond;
};
#endif // ClientQueue_h