Change ObjCNetworkMonitor::OnPathUpdate to use PostTask

Removes use of AsyncInvoker, replaced with PendingTaskSafetyFlag. The
latter is extended to support creation on a different thread than
where it will be used, and to support stop and restart.

Bug: webrtc:12339
Change-Id: I28b6e09b1542f50037e842ef5fe3a47d15704b46
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/211002
Commit-Queue: Niels Moller <nisse@webrtc.org>
Reviewed-by: Tommi <tommi@webrtc.org>
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33432}
This commit is contained in:
Niels Möller
2021-03-11 10:24:06 +01:00
committed by Commit Bot
parent aa6adffba3
commit be140b4187
5 changed files with 21 additions and 5 deletions

View File

@ -14,9 +14,9 @@
#include <vector>
#include "api/sequence_checker.h"
#include "rtc_base/async_invoker.h"
#include "rtc_base/network_monitor.h"
#include "rtc_base/network_monitor_factory.h"
#include "rtc_base/task_utils/pending_task_safety_flag.h"
#include "rtc_base/thread.h"
#include "rtc_base/thread_annotations.h"
#include "sdk/objc/components/network/RTCNetworkMonitor+Private.h"
@ -35,7 +35,7 @@ class ObjCNetworkMonitorFactory : public rtc::NetworkMonitorFactory {
class ObjCNetworkMonitor : public rtc::NetworkMonitorInterface,
public NetworkMonitorObserver {
public:
ObjCNetworkMonitor() = default;
ObjCNetworkMonitor();
~ObjCNetworkMonitor() override;
void Start() override;
@ -58,7 +58,7 @@ class ObjCNetworkMonitor : public rtc::NetworkMonitorInterface,
bool started_ = false;
std::map<std::string, rtc::AdapterType> adapter_type_by_name_
RTC_GUARDED_BY(thread_);
rtc::AsyncInvoker invoker_;
rtc::scoped_refptr<PendingTaskSafetyFlag> safety_flag_;
RTCNetworkMonitor* network_monitor_ = nil;
};

View File

@ -10,6 +10,8 @@
#include "sdk/objc/native/src/objc_network_monitor.h"
#include "rtc_base/task_utils/to_queued_task.h"
#include <algorithm>
#include "rtc_base/logging.h"
@ -20,6 +22,10 @@ rtc::NetworkMonitorInterface* ObjCNetworkMonitorFactory::CreateNetworkMonitor()
return new ObjCNetworkMonitor();
}
ObjCNetworkMonitor::ObjCNetworkMonitor() {
safety_flag_ = PendingTaskSafetyFlag::Create();
}
ObjCNetworkMonitor::~ObjCNetworkMonitor() {
network_monitor_ = nil;
}
@ -30,6 +36,7 @@ void ObjCNetworkMonitor::Start() {
}
thread_ = rtc::Thread::Current();
RTC_DCHECK_RUN_ON(thread_);
safety_flag_->SetAlive();
network_monitor_ = [[RTCNetworkMonitor alloc] initWithObserver:this];
if (network_monitor_ == nil) {
RTC_LOG(LS_WARNING) << "Failed to create RTCNetworkMonitor; not available on this OS?";
@ -42,6 +49,7 @@ void ObjCNetworkMonitor::Stop() {
if (!started_) {
return;
}
safety_flag_->SetNotAlive();
network_monitor_ = nil;
started_ = false;
}
@ -76,11 +84,11 @@ bool ObjCNetworkMonitor::IsAdapterAvailable(const std::string& interface_name) {
void ObjCNetworkMonitor::OnPathUpdate(
std::map<std::string, rtc::AdapterType> adapter_type_by_name) {
RTC_DCHECK(network_monitor_ != nil);
invoker_.AsyncInvoke<void>(RTC_FROM_HERE, thread_, [this, adapter_type_by_name] {
thread_->PostTask(ToQueuedTask(safety_flag_, [this, adapter_type_by_name] {
RTC_DCHECK_RUN_ON(thread_);
adapter_type_by_name_ = adapter_type_by_name;
SignalNetworksChanged();
});
}));
}
} // namespace webrtc