Skip to content
Snippets Groups Projects
Commit 91cda3cb authored by Sébastien Blin's avatar Sébastien Blin
Browse files

upnp_context: check shutdown in dispatch

If we're joining and already shutdown, no need to perform complex
code.

GitLab: #19
Change-Id: Ie8bc24361c9e1ab71b26f19711cce928a2b64077
parent 8b6e99fd
No related branches found
No related tags found
No related merge requests found
...@@ -114,10 +114,11 @@ UPnPContext::shutdown() ...@@ -114,10 +114,11 @@ UPnPContext::shutdown()
} }
if (ioContextRunner_) { if (ioContextRunner_) {
if (logger_) logger_->debug("UPnPContext: stopping io_context thread"); if (logger_) logger_->debug("UPnPContext: stopping io_context thread {}", fmt::ptr(this));
ctx->stop(); ctx->stop();
ioContextRunner_->join(); ioContextRunner_->join();
ioContextRunner_.reset(); ioContextRunner_.reset();
if (logger_) logger_->debug("UPnPContext: stopping io_context thread - finished {}", fmt::ptr(this));
} }
} }
...@@ -160,11 +161,6 @@ UPnPContext::startUpnp() ...@@ -160,11 +161,6 @@ UPnPContext::startUpnp()
void void
UPnPContext::stopUpnp(bool forceRelease) UPnPContext::stopUpnp(bool forceRelease)
{ {
/*if (not isValidThread()) {
ctx->post([this, forceRelease] { stopUpnp(forceRelease); });
return;
}*/
if (logger_) logger_->debug("Stopping UPNP context"); if (logger_) logger_->debug("Stopping UPNP context");
// Clear all current mappings if any. // Clear all current mappings if any.
...@@ -392,6 +388,8 @@ void ...@@ -392,6 +388,8 @@ void
UPnPContext::releaseMapping(const Mapping& map) UPnPContext::releaseMapping(const Mapping& map)
{ {
ctx->dispatch([this, map] { ctx->dispatch([this, map] {
if (shutdownComplete_)
return;
auto mapPtr = getMappingWithKey(map.getMapKey()); auto mapPtr = getMappingWithKey(map.getMapKey());
if (not mapPtr) { if (not mapPtr) {
...@@ -435,6 +433,8 @@ UPnPContext::registerController(void* controller) ...@@ -435,6 +433,8 @@ UPnPContext::registerController(void* controller)
void void
UPnPContext::unregisterController(void* controller) UPnPContext::unregisterController(void* controller)
{ {
if (shutdownComplete_)
return;
std::unique_lock<std::mutex> lock(mappingMutex_); std::unique_lock<std::mutex> lock(mappingMutex_);
if (controllerList_.erase(controller) == 1) { if (controllerList_.erase(controller) == 1) {
if (logger_) logger_->debug("Successfully unregistered controller {}", fmt::ptr(controller)); if (logger_) logger_->debug("Successfully unregistered controller {}", fmt::ptr(controller));
...@@ -473,12 +473,6 @@ void ...@@ -473,12 +473,6 @@ void
UPnPContext::requestMapping(const Mapping::sharedPtr_t& map) UPnPContext::requestMapping(const Mapping::sharedPtr_t& map)
{ {
assert(map); assert(map);
/*if (not isValidThread()) {
ctx->post([this, map] { requestMapping(map); });
return;
}*/
auto const& igd = getPreferredIgd(); auto const& igd = getPreferredIgd();
// We must have at least a valid IGD pointer if we get here. // We must have at least a valid IGD pointer if we get here.
// Not this method is called only if there were a valid IGD, however, // Not this method is called only if there were a valid IGD, however,
...@@ -928,11 +922,6 @@ UPnPContext::onIgdUpdated(const std::shared_ptr<IGD>& igd, UpnpIgdEvent event) ...@@ -928,11 +922,6 @@ UPnPContext::onIgdUpdated(const std::shared_ptr<IGD>& igd, UpnpIgdEvent event)
{ {
assert(igd); assert(igd);
/*if (not isValidThread()) {
ctx->post([this, igd, event] { onIgdUpdated(igd, event); });
return;
}*/
// Reset to start search for a new best IGD. // Reset to start search for a new best IGD.
preferredIgd_.reset(); preferredIgd_.reset();
...@@ -1079,11 +1068,6 @@ UPnPContext::requestRemoveMapping(const Mapping::sharedPtr_t& map) ...@@ -1079,11 +1068,6 @@ UPnPContext::requestRemoveMapping(const Mapping::sharedPtr_t& map)
void void
UPnPContext::deleteAllMappings(PortType type) UPnPContext::deleteAllMappings(PortType type)
{ {
/*if (not isValidThread()) {
ctx->post([this, type] { deleteAllMappings(type); });
return;
}*/
std::lock_guard<std::mutex> lock(mappingMutex_); std::lock_guard<std::mutex> lock(mappingMutex_);
auto& mappingList = getMappingList(type); auto& mappingList = getMappingList(type);
...@@ -1098,11 +1082,6 @@ UPnPContext::onMappingRemoved(const std::shared_ptr<IGD>& igd, const Mapping& ma ...@@ -1098,11 +1082,6 @@ UPnPContext::onMappingRemoved(const std::shared_ptr<IGD>& igd, const Mapping& ma
if (not mapRes.isValid()) if (not mapRes.isValid())
return; return;
/*if (not isValidThread()) {
ctx->post([this, igd, mapRes] { onMappingRemoved(igd, mapRes); });
return;
}*/
auto map = getMappingWithKey(mapRes.getMapKey()); auto map = getMappingWithKey(mapRes.getMapKey());
// Notify the listener. // Notify the listener.
if (map and map->getNotifyCallback()) if (map and map->getNotifyCallback())
......
...@@ -26,13 +26,10 @@ Controller::Controller(const std::shared_ptr<UPnPContext>& ctx) ...@@ -26,13 +26,10 @@ Controller::Controller(const std::shared_ptr<UPnPContext>& ctx)
upnpContext_->dispatch([c=upnpContext_, this]{ upnpContext_->dispatch([c=upnpContext_, this]{
c->registerController(this); c->registerController(this);
}); });
//if (upnpContext_->logger_) upnpContext_->logger_->debug("Controller@{}: Created UPnP Controller session", fmt::ptr(this));
} }
Controller::~Controller() Controller::~Controller()
{ {
//if (logger_) logger_->debug("Controller@{}: Destroying UPnP Controller session", fmt::ptr(this));
releaseAllMappings(); releaseAllMappings();
upnpContext_->dispatch([c=upnpContext_, this]{ upnpContext_->dispatch([c=upnpContext_, this]{
c->unregisterController(this); c->unregisterController(this);
...@@ -112,9 +109,6 @@ Controller::addLocalMap(const Mapping& map) ...@@ -112,9 +109,6 @@ Controller::addLocalMap(const Mapping& map)
if (map.getMapKey()) { if (map.getMapKey()) {
std::lock_guard<std::mutex> lock(mapListMutex_); std::lock_guard<std::mutex> lock(mapListMutex_);
auto ret = mappingList_.emplace(map.getMapKey(), map); auto ret = mappingList_.emplace(map.getMapKey(), map);
if (not ret.second) {
// JAMI_WARN("Mapping request for %s already in the list!", map.toString().c_str());
}
} }
} }
...@@ -124,12 +118,7 @@ Controller::removeLocalMap(const Mapping& map) ...@@ -124,12 +118,7 @@ Controller::removeLocalMap(const Mapping& map)
assert(upnpContext_); assert(upnpContext_);
std::lock_guard<std::mutex> lk(mapListMutex_); std::lock_guard<std::mutex> lk(mapListMutex_);
if (mappingList_.erase(map.getMapKey()) != 1) { return mappingList_.erase(map.getMapKey()) == 1;
// JAMI_ERR("Failed to remove mapping %s from local list", map.getTypeStr());
return false;
}
return true;
} }
uint16_t uint16_t
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment