From 24bace15635507c866769ca936935b7dffbb2efc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Sat, 21 Feb 2015 21:46:01 -0500 Subject: [PATCH] replace unique_lock by lock_guard where possible --- include/opendht/dhtrunner.h | 22 +++++++++++----------- src/dhtrunner.cpp | 32 ++++++++++++++++---------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h index b01c7b3d..bbe13b8c 100644 --- a/include/opendht/dhtrunner.h +++ b/include/opendht/dhtrunner.h @@ -84,7 +84,7 @@ public: void dumpTables() const { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); dht_->dumpTables(); } @@ -101,31 +101,31 @@ public: } std::vector exportNodes() const { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); if (!dht_) return {}; return dht_->exportNodes(); } std::vector exportValues() const { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); if (!dht_) return {}; return dht_->exportValues(); } void setLoggers(LogMethod&& error = NOLOG, LogMethod&& warn = NOLOG, LogMethod&& debug = NOLOG) { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); dht_->setLoggers(std::forward(error), std::forward(warn), std::forward(debug)); } void registerType(const ValueType& type) { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); dht_->registerType(type); } void importValues(const std::vector& values) { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); dht_->importValues(values); } @@ -135,23 +135,23 @@ public: int getNodesStats(sa_family_t af, unsigned *good_return, unsigned *dubious_return, unsigned *cached_return, unsigned *incoming_return) const { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); return dht_->getNodesStats(af, good_return, dubious_return, cached_return, incoming_return); } std::string getStorageLog() const { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); return dht_->getStorageLog(); } std::string getRoutingTablesLog(sa_family_t af) const { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); return dht_->getRoutingTablesLog(af); } std::string getSearchesLog(sa_family_t af) const { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); return dht_->getSearchesLog(af); } @@ -161,7 +161,7 @@ public: void run(in_port_t port, const crypto::Identity identity, bool threaded = false, StatusCallback cb = nullptr); time_point loop() { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); return loop_(); } diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp index cfc21f33..41dfd2f2 100644 --- a/src/dhtrunner.cpp +++ b/src/dhtrunner.cpp @@ -52,12 +52,12 @@ DhtRunner::run(in_port_t port, const crypto::Identity identity, bool threaded, S if (not running) return true; { - std::unique_lock lck(sock_mtx); + std::lock_guard lck(sock_mtx); if (not rcv.empty()) return true; } { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); if (not pending_ops.empty()) return true; } @@ -77,7 +77,7 @@ DhtRunner::join() if (rcv_thread.joinable()) rcv_thread.join(); { - std::unique_lock lck(dht_mtx); + std::lock_guard lck(dht_mtx); dht_.reset(); status4 = Dht::Status::Disconnected; status6 = Dht::Status::Disconnected; @@ -92,7 +92,7 @@ DhtRunner::loop_() decltype(pending_ops) ops {}; { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); ops = std::move(pending_ops); } while (not ops.empty()) { @@ -102,7 +102,7 @@ DhtRunner::loop_() time_point wakeup {}; { - std::unique_lock lck(sock_mtx); + std::lock_guard lck(sock_mtx); if (not rcv.empty()) { for (const auto& pck : rcv) { auto& buf = pck.first; @@ -200,7 +200,7 @@ DhtRunner::doRun(in_port_t port, const crypto::Identity identity) if (rc > 0) { buf[rc] = 0; { - std::unique_lock lck(sock_mtx); + std::lock_guard lck(sock_mtx); rcv.emplace_back(Blob {buf, buf+rc+1}, from); } cv.notify_all(); @@ -220,7 +220,7 @@ DhtRunner::doRun(in_port_t port, const crypto::Identity identity) void DhtRunner::get(InfoHash hash, Dht::GetCallback vcb, Dht::DoneCallback dcb, Value::Filter f) { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); pending_ops.emplace([=](SecureDht& dht) { std::cout << "Processing get (" << hash << ")" << std::endl; dht.get(hash, vcb, dcb, f); @@ -237,7 +237,7 @@ DhtRunner::get(const std::string& key, Dht::GetCallback vcb, Dht::DoneCallback d std::future DhtRunner::listen(InfoHash hash, Dht::GetCallback vcb, Value::Filter f) { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); auto ret_token = std::make_shared>(); pending_ops.emplace([=](SecureDht& dht) { std::cout << "Processing listen (" << hash << ")" << std::endl; @@ -256,7 +256,7 @@ DhtRunner::listen(const std::string& key, Dht::GetCallback vcb, Value::Filter f) void DhtRunner::cancelListen(InfoHash h, size_t token) { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); pending_ops.emplace([=](SecureDht& dht) { std::cout << "Processing cancelListen " << h << std::endl; dht.cancelListen(h, token); @@ -266,7 +266,7 @@ DhtRunner::cancelListen(InfoHash h, size_t token) void DhtRunner::cancelListen(InfoHash h, std::shared_future token) { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); pending_ops.emplace([=](SecureDht& dht) { std::cout << "Processing cancelListen (shared_future) " << h << std::endl; auto tk = token.get(); @@ -278,7 +278,7 @@ DhtRunner::cancelListen(InfoHash h, std::shared_future token) void DhtRunner::put(InfoHash hash, Value&& value, Dht::DoneCallback cb) { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); auto sv = std::make_shared(std::move(value)); pending_ops.emplace([=](SecureDht& dht) { std::cout << "Processing put " << hash << " -> " << *sv << std::endl; @@ -296,7 +296,7 @@ DhtRunner::put(const std::string& key, Value&& value, Dht::DoneCallback cb) void DhtRunner::cancelPut(const InfoHash& h , const Value::Id& id) { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); pending_ops.emplace([=](SecureDht& dht) { std::cout << "Processing cancelPut " << h << " / " << id << std::endl; dht.cancelPut(h, id); @@ -306,7 +306,7 @@ DhtRunner::cancelPut(const InfoHash& h , const Value::Id& id) void DhtRunner::putSigned(InfoHash hash, Value&& value, Dht::DoneCallback cb) { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); auto sv = std::make_shared(std::move(value)); pending_ops.emplace([=](SecureDht& dht) { std::cout << "Processing signed put " << hash << " -> " << *sv << std::endl; @@ -324,7 +324,7 @@ DhtRunner::putSigned(const std::string& key, Value&& value, Dht::DoneCallback cb void DhtRunner::putEncrypted(InfoHash hash, InfoHash to, Value&& value, Dht::DoneCallback cb) { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); auto sv = std::make_shared(std::move(value)); pending_ops.emplace([=](SecureDht& dht) { std::cout << "Processing encrypted put at " << hash << " for " << to << " -> " << *sv << std::endl; @@ -342,7 +342,7 @@ DhtRunner::putEncrypted(const std::string& key, InfoHash to, Value&& value, Dht: void DhtRunner::bootstrap(const std::vector& nodes) { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); pending_ops.emplace([=](SecureDht& dht) { for (auto& node : nodes) dht.pingNode((sockaddr*)&node, sizeof(node)); @@ -353,7 +353,7 @@ DhtRunner::bootstrap(const std::vector& nodes) void DhtRunner::bootstrap(const std::vector& nodes) { - std::unique_lock lck(storage_mtx); + std::lock_guard lck(storage_mtx); pending_ops.emplace([=](SecureDht& dht) { for (auto& node : nodes) dht.insertNode(node);