JamiAccount: use shared_mutex for connManager

Change-Id: I73eeb7a3c8f09e147a8ca2713028c94de6a72aa7
This commit is contained in:
Adrien Béraud
2025-03-04 16:15:06 -05:00
parent 8647b933c4
commit 5e8c060a7f
2 changed files with 30 additions and 22 deletions

View File

@ -2007,13 +2007,17 @@ JamiAccount::doRegister_()
if (accountManager_->getInfo()->deviceId == deviceId) if (accountManager_->getInfo()->deviceId == deviceId)
return; return;
std::unique_lock lk(connManagerMtx_); dht::ThreadPool::io().run([w = weak(), deviceId, crt] {
initConnectionManager(); auto shared = w.lock();
lk.unlock(); if (!shared) return;
requestSIPConnection( std::unique_lock lk(shared->connManagerMtx_);
getUsername(), shared->initConnectionManager();
crt->getLongId(), lk.unlock();
"sync"); // For git notifications, will use the same socket as sync shared->requestSIPConnection(
shared->getUsername(),
crt->getLongId(),
"sync"); // For git notifications, will use the same socket as sync
});
} }
}, },
[this] { [this] {
@ -2080,7 +2084,7 @@ JamiAccount::doRegister_()
} }
auto uri = Uri(name); auto uri = Uri(name);
std::lock_guard lk(connManagerMtx_); std::shared_lock lk(connManagerMtx_);
auto itHandler = channelHandlers_.find(uri.scheme()); auto itHandler = channelHandlers_.find(uri.scheme());
if (itHandler != channelHandlers_.end() && itHandler->second) if (itHandler != channelHandlers_.end() && itHandler->second)
return itHandler->second->onRequest(cert, name); return itHandler->second->onRequest(cert, name);
@ -2171,7 +2175,7 @@ JamiAccount::doRegister_()
}); });
} else { } else {
// TODO move git:// // TODO move git://
std::lock_guard lk(connManagerMtx_); std::shared_lock lk(connManagerMtx_);
auto uri = Uri(name); auto uri = Uri(name);
auto itHandler = channelHandlers_.find(uri.scheme()); auto itHandler = channelHandlers_.find(uri.scheme());
if (itHandler != channelHandlers_.end() && itHandler->second) if (itHandler != channelHandlers_.end() && itHandler->second)
@ -2258,7 +2262,7 @@ JamiAccount::convModule(bool noCreation)
cb({}); cb({});
return; return;
} }
std::unique_lock lkCM(shared->connManagerMtx_); std::shared_lock lkCM(shared->connManagerMtx_);
if (!shared->connectionManager_) { if (!shared->connectionManager_) {
lkCM.unlock(); lkCM.unlock();
cb({}); cb({});
@ -2304,7 +2308,7 @@ JamiAccount::convModule(bool noCreation)
if (!shared) if (!shared)
return; return;
auto cm = shared->convModule(); auto cm = shared->convModule();
std::lock_guard lkCM(shared->connManagerMtx_); std::shared_lock lkCM(shared->connManagerMtx_);
if (!shared->connectionManager_ || !cm || cm->isBanned(convId, deviceId)) { if (!shared->connectionManager_ || !cm || cm->isBanned(convId, deviceId)) {
Manager::instance().ioContext()->post([cb] { cb({}); }); Manager::instance().ioContext()->post([cb] { cb({}); });
return; return;
@ -2494,7 +2498,7 @@ JamiAccount::connectivityChanged()
cm->connectivityChanged(); cm->connectivityChanged();
dht_->connectivityChanged(); dht_->connectivityChanged();
{ {
std::lock_guard lkCM(connManagerMtx_); std::shared_lock lkCM(connManagerMtx_);
if (connectionManager_) { if (connectionManager_) {
connectionManager_->connectivityChanged(); connectionManager_->connectivityChanged();
// reset cache // reset cache
@ -3091,7 +3095,7 @@ JamiAccount::sendMessage(const std::string& to,
auto devices = std::make_shared<std::set<DeviceId>>(); auto devices = std::make_shared<std::set<DeviceId>>();
// Use the Message channel if available // Use the Message channel if available
std::unique_lock clk(connManagerMtx_); std::shared_lock clk(connManagerMtx_);
auto* handler = static_cast<MessageChannelHandler*>( auto* handler = static_cast<MessageChannelHandler*>(
channelHandlers_[Uri::Scheme::MESSAGE].get()); channelHandlers_[Uri::Scheme::MESSAGE].get());
if (!handler) { if (!handler) {
@ -3776,7 +3780,7 @@ JamiAccount::requestSIPConnection(const std::string& peerId,
return; return;
} }
// If not present, create it // If not present, create it
std::lock_guard lkCM(connManagerMtx_); std::shared_lock lkCM(connManagerMtx_);
if (!connectionManager_) if (!connectionManager_)
return; return;
// Note, Even if we send 50 "sip" request, the connectionManager_ will only use one socket. // Note, Even if we send 50 "sip" request, the connectionManager_ will only use one socket.
@ -3813,7 +3817,7 @@ JamiAccount::requestSIPConnection(const std::string& peerId,
bool bool
JamiAccount::isConnectedWith(const DeviceId& deviceId) const JamiAccount::isConnectedWith(const DeviceId& deviceId) const
{ {
std::lock_guard lkCM(connManagerMtx_); std::shared_lock lkCM(connManagerMtx_);
if (connectionManager_) if (connectionManager_)
return connectionManager_->isConnected(deviceId); return connectionManager_->isConnected(deviceId);
return false; return false;
@ -4168,7 +4172,7 @@ JamiAccount::monitor()
if (auto cm = convModule()) if (auto cm = convModule())
cm->monitor(); cm->monitor();
std::lock_guard lkCM(connManagerMtx_); std::shared_lock lkCM(connManagerMtx_);
if (connectionManager_) if (connectionManager_)
connectionManager_->monitor(); connectionManager_->monitor();
} }
@ -4176,7 +4180,7 @@ JamiAccount::monitor()
std::vector<std::map<std::string, std::string>> std::vector<std::map<std::string, std::string>>
JamiAccount::getConnectionList(const std::string& conversationId) JamiAccount::getConnectionList(const std::string& conversationId)
{ {
std::lock_guard lkCM(connManagerMtx_); std::shared_lock lkCM(connManagerMtx_);
if (connectionManager_ && conversationId.empty()) { if (connectionManager_ && conversationId.empty()) {
return connectionManager_->getConnectionList(); return connectionManager_->getConnectionList();
} else if (connectionManager_ && convModule_) { } else if (connectionManager_ && convModule_) {
@ -4199,7 +4203,7 @@ JamiAccount::getConnectionList(const std::string& conversationId)
std::vector<std::map<std::string, std::string>> std::vector<std::map<std::string, std::string>>
JamiAccount::getChannelList(const std::string& connectionId) JamiAccount::getChannelList(const std::string& connectionId)
{ {
std::lock_guard lkCM(connManagerMtx_); std::shared_lock lkCM(connManagerMtx_);
if (!connectionManager_) if (!connectionManager_)
return {}; return {};
return connectionManager_->getChannelList(connectionId); return connectionManager_->getChannelList(connectionId);
@ -4310,7 +4314,7 @@ JamiAccount::transferFile(const std::string& conversationId,
conversationId, conversationId,
currentDeviceId(), currentDeviceId(),
fid); fid);
std::lock_guard lkCM(connManagerMtx_); std::shared_lock lkCM(connManagerMtx_);
if (!connectionManager_) if (!connectionManager_)
return; return;
connectionManager_ connectionManager_
@ -4359,7 +4363,7 @@ JamiAccount::askForFileChannel(const std::string& conversationId,
size_t end) size_t end)
{ {
auto tryDevice = [=](const auto& did) { auto tryDevice = [=](const auto& did) {
std::lock_guard lkCM(connManagerMtx_); std::shared_lock lkCM(connManagerMtx_);
if (!connectionManager_) if (!connectionManager_)
return; return;
@ -4419,7 +4423,7 @@ JamiAccount::askForProfile(const std::string& conversationId,
const std::string& deviceId, const std::string& deviceId,
const std::string& memberUri) const std::string& memberUri)
{ {
std::lock_guard lkCM(connManagerMtx_); std::shared_lock lkCM(connManagerMtx_);
if (!connectionManager_) if (!connectionManager_)
return; return;

View File

@ -60,6 +60,7 @@
#include <optional> #include <optional>
#include <vector> #include <vector>
#include <filesystem> #include <filesystem>
#include <shared_mutex>
#if HAVE_RINGNS #if HAVE_RINGNS
#include "namedirectory.h" #include "namedirectory.h"
@ -789,7 +790,10 @@ private:
pjsip_transport* via_tp_ {nullptr}; pjsip_transport* via_tp_ {nullptr};
mutable std::mutex connManagerMtx_ {}; /** ConnectionManager is thread-safe.
* The shared mutex protects the pointer while allowing
* multiple threads to access the ConnectionManager concurrently */
mutable std::shared_mutex connManagerMtx_ {};
std::unique_ptr<dhtnet::ConnectionManager> connectionManager_; std::unique_ptr<dhtnet::ConnectionManager> connectionManager_;
virtual void updateUpnpController() override; virtual void updateUpnpController() override;