From e0e254548b6200d16bc3f4be3bd255b041a76532 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 21 May 2009 22:38:30 +0200 Subject: InformationManager Thread-safe gemacht --- src/Client/InformationManager.cpp | 20 +++++++++++++++----- src/Client/InformationManager.h | 21 +++++++++++++++++++-- src/madc.cpp | 4 +--- 3 files changed, 35 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/Client/InformationManager.cpp b/src/Client/InformationManager.cpp index f1627f4..8fde6f7 100644 --- a/src/Client/InformationManager.cpp +++ b/src/Client/InformationManager.cpp @@ -45,11 +45,15 @@ void InformationManager::DaemonStateUpdateRequestHandler::handlePacket(const Com // TODO Require authentication - std::map::iterator host = informationManager.get()->daemons.find(packet["name"]); - if(host != informationManager.get()->daemons.end()) - host->second.setState(packet["state"]); - else - Common::Logger::log(Common::Logger::WARNING, "Received a state update for an unknown host."); + { + boost::lock_guard lock(informationManager.mutex); + + std::map::iterator host = informationManager.daemons.find(packet["name"]); + if(host != informationManager.daemons.end()) + host->second.setState(packet["state"]); + else + Common::Logger::log(Common::Logger::WARNING, "Received a state update for an unknown host."); + } Common::XmlPacket ret; ret.setType("OK"); @@ -68,6 +72,8 @@ void InformationManager::doDeinit() { } void InformationManager::updateDaemonList(Common::Connection *con) { + boost::lock_guard lock(mutex); + if(updating) return; @@ -78,6 +84,8 @@ void InformationManager::updateDaemonList(Common::Connection *con) { } void InformationManager::daemonListRequestFinished(const Common::Request &request) { + boost::lock_guard lock(mutex); + try { const Common::XmlPacket &packet = request.getResult(); @@ -99,6 +107,8 @@ void InformationManager::daemonListRequestFinished(const Common::Request &reques } updating = false; + + updateCond.notify_all(); } } diff --git a/src/Client/InformationManager.h b/src/Client/InformationManager.h index fff26b3..2c39601 100644 --- a/src/Client/InformationManager.h +++ b/src/Client/InformationManager.h @@ -27,6 +27,10 @@ #include #include +#include +#include +#include + namespace Mad { namespace Client { @@ -43,6 +47,9 @@ class InformationManager : public Common::Initializable { static InformationManager informationManager; + boost::mutex mutex; + boost::condition_variable updateCond; + std::map daemons; bool updating; @@ -66,11 +73,21 @@ class InformationManager : public Common::Initializable { void updateDaemonList(Common::Connection *con); - bool isUpdating() const { + bool isUpdating() { + boost::lock_guard lock(mutex); return updating; } - const std::map& getDaemons() const { + void waitWhileUpdating() { + boost::unique_lock lock(mutex); + + while(updating) + updateCond.wait(lock); + } + + + std::map getDaemons() { + boost::lock_guard lock(mutex); return daemons; } }; diff --git a/src/madc.cpp b/src/madc.cpp index 8499232..fd6b547 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -87,9 +87,7 @@ int main(int argc, char *argv[]) { std::cerr << "Receiving host list..." << std::flush; Client::InformationManager::get()->updateDaemonList(connection); - - while(Client::InformationManager::get()->isUpdating()) - usleep(100000); + Client::InformationManager::get()->waitWhileUpdating(); std::cerr << " done." << std::endl << std::endl; -- cgit v1.2.3