diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-05-21 22:38:30 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-05-21 22:38:30 +0200 |
commit | e0e254548b6200d16bc3f4be3bd255b041a76532 (patch) | |
tree | 5665e3e5f3a54f63495f8f671fe911013a854ba5 | |
parent | 02f8f9fe5ff81d5723bcbffef5a8dcc4e2adb156 (diff) | |
download | mad-e0e254548b6200d16bc3f4be3bd255b041a76532.tar mad-e0e254548b6200d16bc3f4be3bd255b041a76532.zip |
InformationManager Thread-safe gemacht
-rw-r--r-- | src/Client/InformationManager.cpp | 20 | ||||
-rw-r--r-- | src/Client/InformationManager.h | 21 | ||||
-rw-r--r-- | src/madc.cpp | 4 |
3 files changed, 35 insertions, 10 deletions
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<std::string, Common::HostInfo>::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<boost::mutex> lock(informationManager.mutex); + + std::map<std::string, Common::HostInfo>::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<boost::mutex> 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<boost::mutex> 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 <Common/Initializable.h> #include <Common/Request.h> +#include <boost/thread/condition_variable.hpp> +#include <boost/thread/locks.hpp> +#include <boost/thread/mutex.hpp> + 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<std::string, Common::HostInfo> 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<boost::mutex> lock(mutex); return updating; } - const std::map<std::string, Common::HostInfo>& getDaemons() const { + void waitWhileUpdating() { + boost::unique_lock<boost::mutex> lock(mutex); + + while(updating) + updateCond.wait(lock); + } + + + std::map<std::string, Common::HostInfo> getDaemons() { + boost::lock_guard<boost::mutex> 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; |