From baaf911c80e15b51ed7cfa52b873ba8c0049a883 Mon Sep 17 00:00:00 2001 From: Danil Chapovalov Date: Fri, 18 Jan 2019 10:09:40 +0100 Subject: [PATCH] Introduce global task queue factory. Bug: webrtc:10191 Change-Id: I7bdc97fd626da955b9194a9a0d8ed4f5aebddf66 Reviewed-on: https://webrtc-review.googlesource.com/c/118120 Reviewed-by: Niels Moller Reviewed-by: Karl Wiberg Commit-Queue: Danil Chapovalov Cr-Commit-Position: refs/heads/master@{#26315} --- api/task_queue/BUILD.gn | 29 ++++++++++++++ api/task_queue/default_task_queue_factory.cc | 22 +++++++++++ api/task_queue/default_task_queue_factory.h | 23 +++++++++++ api/task_queue/global_task_queue_factory.cc | 40 ++++++++++++++++++++ api/task_queue/global_task_queue_factory.h | 27 +++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 api/task_queue/default_task_queue_factory.cc create mode 100644 api/task_queue/default_task_queue_factory.h create mode 100644 api/task_queue/global_task_queue_factory.cc create mode 100644 api/task_queue/global_task_queue_factory.h diff --git a/api/task_queue/BUILD.gn b/api/task_queue/BUILD.gn index d058291944..ee35dab5ad 100644 --- a/api/task_queue/BUILD.gn +++ b/api/task_queue/BUILD.gn @@ -34,3 +34,32 @@ rtc_source_set("task_queue_factory") { "//third_party/abseil-cpp/absl/strings", ] } + +rtc_source_set("default_task_queue_factory") { + # TODO(bugs.webrtc.org/10191): Make public when implemented for all + # supported platforms. + visibility = [ ":global_task_queue_factory" ] + sources = [ + "default_task_queue_factory.cc", + "default_task_queue_factory.h", + ] + deps = [ + ":task_queue_factory", + "../../rtc_base:checks", + ] +} + +rtc_source_set("global_task_queue_factory") { + # TODO(danilchap): Remove this target when task queue factory propagated to + # all components that create TaskQueues. + visibility = [ "*" ] + sources = [ + "global_task_queue_factory.cc", + "global_task_queue_factory.h", + ] + deps = [ + ":default_task_queue_factory", + ":task_queue_factory", + "../../rtc_base:checks", + ] +} diff --git a/api/task_queue/default_task_queue_factory.cc b/api/task_queue/default_task_queue_factory.cc new file mode 100644 index 0000000000..b3e86bb4df --- /dev/null +++ b/api/task_queue/default_task_queue_factory.cc @@ -0,0 +1,22 @@ +/* + * Copyright 2019 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#include "api/task_queue/default_task_queue_factory.h" + +#include "rtc_base/checks.h" + +namespace webrtc { + +std::unique_ptr CreateDefaultTaskQueueFactory() { + RTC_CHECK(false) + << "Default task queue is not implemented for current platform, " + "overwrite the task queue implementation by setting global factory."; +} + +} // namespace webrtc diff --git a/api/task_queue/default_task_queue_factory.h b/api/task_queue/default_task_queue_factory.h new file mode 100644 index 0000000000..ccdd1ebec0 --- /dev/null +++ b/api/task_queue/default_task_queue_factory.h @@ -0,0 +1,23 @@ +/* + * Copyright 2019 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#ifndef API_TASK_QUEUE_DEFAULT_TASK_QUEUE_FACTORY_H_ +#define API_TASK_QUEUE_DEFAULT_TASK_QUEUE_FACTORY_H_ + +#include + +#include "api/task_queue/task_queue_factory.h" + +namespace webrtc { + +std::unique_ptr CreateDefaultTaskQueueFactory(); + +} // namespace webrtc + +#endif // API_TASK_QUEUE_DEFAULT_TASK_QUEUE_FACTORY_H_ diff --git a/api/task_queue/global_task_queue_factory.cc b/api/task_queue/global_task_queue_factory.cc new file mode 100644 index 0000000000..528d14ddb1 --- /dev/null +++ b/api/task_queue/global_task_queue_factory.cc @@ -0,0 +1,40 @@ +/* + * Copyright 2019 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "api/task_queue/global_task_queue_factory.h" + +#include "api/task_queue/default_task_queue_factory.h" +#include "rtc_base/checks.h" + +namespace webrtc { +namespace { + +TaskQueueFactory* GlobalOrDefault(TaskQueueFactory* global) { + static TaskQueueFactory* const factory = + global ? global : CreateDefaultTaskQueueFactory().release(); + return factory; +} + +} // namespace + +void SetGlobalTaskQueueFactory(std::unique_ptr factory) { + RTC_CHECK(factory) << "Can't set nullptr TaskQueueFactory"; + // Own, but never delete the global factory. + TaskQueueFactory* global = factory.release(); + RTC_CHECK(GlobalOrDefault(global) == global) + << "Task queue factory set after another SetFactory or after a task " + "queue was created"; +} + +TaskQueueFactory& GlobalTaskQueueFactory() { + return *GlobalOrDefault(nullptr); +} + +} // namespace webrtc diff --git a/api/task_queue/global_task_queue_factory.h b/api/task_queue/global_task_queue_factory.h new file mode 100644 index 0000000000..a58083301d --- /dev/null +++ b/api/task_queue/global_task_queue_factory.h @@ -0,0 +1,27 @@ +/* + * Copyright 2019 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ +#ifndef API_TASK_QUEUE_GLOBAL_TASK_QUEUE_FACTORY_H_ +#define API_TASK_QUEUE_GLOBAL_TASK_QUEUE_FACTORY_H_ + +#include + +#include "api/task_queue/task_queue_factory.h" + +namespace webrtc { + +// May be called at most once, and before any TaskQueue is created. +void SetGlobalTaskQueueFactory(std::unique_ptr factory); + +// Returns TaskQueue factory. Always returns the same factory. +TaskQueueFactory& GlobalTaskQueueFactory(); + +} // namespace webrtc + +#endif // API_TASK_QUEUE_GLOBAL_TASK_QUEUE_FACTORY_H_