Use GCD instead of Detached Thread in Async Resolver when on MacOS/iOS

The advantage is that GCD maintains the internal thread pool and
spawns threads when needed. I would expect the behavior to be
almost identical to creating a thread using PlatformThread.

Bug: webrtc:13237
Change-Id: Ie4406b5d128c244f66a73830d5a27f2d8fd88549
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/234300
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Commit-Queue: Byoungchan Lee <daniel.l@hpcnt.com>
Cr-Commit-Position: refs/heads/main@{#35165}
This commit is contained in:
Byoungchan Lee 2021-10-06 20:25:39 +09:00 committed by WebRTC LUCI CQ
parent 40abb7d8ff
commit 7284bd4dab
4 changed files with 39 additions and 5 deletions

View file

@ -542,7 +542,10 @@ if (rtc_enable_libevent) {
if (is_mac || is_ios) {
rtc_library("rtc_task_queue_gcd") {
visibility = [ "../api/task_queue:default_task_queue_factory" ]
visibility = [
":threading",
"../api/task_queue:default_task_queue_factory",
]
sources = [
"task_queue_gcd.cc",
"task_queue_gcd.h",
@ -832,7 +835,10 @@ rtc_library("threading") {
deps += [ ":win32" ]
}
if (is_mac || is_ios) {
deps += [ "system:cocoa_threading" ]
deps += [
":rtc_task_queue_gcd",
"system:cocoa_threading",
]
}
}

View file

@ -40,6 +40,10 @@
#include "rtc_base/task_utils/to_queued_task.h"
#include "rtc_base/third_party/sigslot/sigslot.h" // for signal_with_thread...
#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
#include "rtc_base/task_queue_gcd.h"
#endif
namespace rtc {
int ResolveHostname(const std::string& hostname,
@ -123,7 +127,7 @@ void AsyncResolver::Start(const SocketAddress& addr) {
RTC_DCHECK_RUN_ON(&sequence_checker_);
RTC_DCHECK(!destroy_called_);
addr_ = addr;
PlatformThread::SpawnDetached(
auto thread_function =
[this, addr, caller_task_queue = webrtc::TaskQueueBase::Current(),
state = state_] {
std::vector<IPAddress> addresses;
@ -146,8 +150,13 @@ void AsyncResolver::Start(const SocketAddress& addr) {
}
}));
}
},
"AsyncResolver");
};
#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
webrtc::PostTaskToGlobalQueue(
webrtc::ToQueuedTask(std::move(thread_function)));
#else
PlatformThread::SpawnDetached(std::move(thread_function), "AsyncResolver");
#endif
}
bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const {

View file

@ -145,10 +145,24 @@ class TaskQueueGcdFactory final : public TaskQueueFactory {
}
};
// static
void GlobalGcdRunTask(void* context) {
std::unique_ptr<QueuedTask> task(static_cast<QueuedTask*>(context));
task->Run();
}
} // namespace
std::unique_ptr<TaskQueueFactory> CreateTaskQueueGcdFactory() {
return std::make_unique<TaskQueueGcdFactory>();
}
void PostTaskToGlobalQueue(std::unique_ptr<QueuedTask> task,
TaskQueueFactory::Priority priority) {
dispatch_queue_global_t global_queue =
dispatch_get_global_queue(TaskQueuePriorityToGCD(priority), 0);
QueuedTask* context = task.release();
dispatch_async_f(global_queue, context, &GlobalGcdRunTask);
}
} // namespace webrtc

View file

@ -19,6 +19,11 @@ namespace webrtc {
std::unique_ptr<TaskQueueFactory> CreateTaskQueueGcdFactory();
// Post a task into the system-defined global concurrent queue.
void PostTaskToGlobalQueue(
std::unique_ptr<QueuedTask> task,
TaskQueueFactory::Priority priority = TaskQueueFactory::Priority::NORMAL);
} // namespace webrtc
#endif // RTC_BASE_TASK_QUEUE_GCD_H_