Skip to content

Commit

Permalink
pw_async: Fill in bazel build rules
Browse files Browse the repository at this point in the history
Change-Id: I9005136e027a07016c9c950c75cfe22e8c8e2db1
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/156911
Commit-Queue: Auto-Submit <[email protected]>
Reviewed-by: Ted Pudlik <[email protected]>
Pigweed-Auto-Submit: Austin Foxley <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
  • Loading branch information
afoxley authored and CQ Bot Account committed Jul 19, 2023
1 parent 60012df commit 11f9435
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 28 deletions.
117 changes: 107 additions & 10 deletions pw_async/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,115 @@
# License for the specific language governing permissions and limitations under
# the License.

filegroup(
name = "pw_async_files",
srcs = [
"fake_dispatcher_test.cc",
"heap_dispatcher.cc",
"public/pw_async/context.h",
load(
"//pw_build:pigweed.bzl",
"pw_cc_facade",
"pw_cc_library",
"pw_cc_test",
)

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

pw_cc_library(
name = "dispatcher",
hdrs = [
"public/pw_async/dispatcher.h",
"public/pw_async/fake_dispatcher.h",
"public/pw_async/fake_dispatcher_fixture.h",
"public/pw_async/function_dispatcher.h",
"public/pw_async/heap_dispatcher.h",
"public/pw_async/task.h",
],
includes = ["public"],
deps = [
":types",
"//pw_chrono:system_clock",
"//pw_function",
"//pw_status",
],
)

pw_cc_facade(
name = "task_facade",
hdrs = ["public/pw_async/task.h"],
includes = ["public"],
deps = [
":types",
"//pw_chrono:system_clock",
"//pw_function",
"//pw_status",
],
)

pw_cc_library(
name = "task",
hdrs = ["public/pw_async/task.h"],
includes = ["public"],
deps = [
":types",
"//pw_chrono:system_clock",
"//pw_function",
"//pw_status",
"@pigweed_config//:pw_async_task_backend",
],
)

pw_cc_library(
name = "types",
hdrs = [
"public/pw_async/context.h",
"public/pw_async/task_function.h",
],
includes = ["public"],
deps = [
"//pw_function",
"//pw_status",
],
)

pw_cc_facade(
name = "fake_dispatcher_facade",
hdrs = ["public/pw_async/fake_dispatcher.h"],
includes = ["public"],
deps = [":dispatcher"],
)

pw_cc_library(
name = "fake_dispatcher",
hdrs = ["public/pw_async/fake_dispatcher.h"],
includes = ["public"],
deps = [
":dispatcher",
"@pigweed_config//:pw_async_fake_dispatcher_backend",
],
)

pw_cc_test(
name = "fake_dispatcher_test",
srcs = ["fake_dispatcher_test.cc"],
deps = [
":fake_dispatcher",
"//pw_containers:vector",
"//pw_log",
"//pw_string:to_string",
"//pw_sync:timed_thread_notification",
"//pw_thread:thread",
],
)

pw_cc_library(
name = "fake_dispatcher_fixture",
hdrs = ["public/pw_async/fake_dispatcher_fixture.h"],
includes = ["public"],
deps = [":fake_dispatcher"],
)

pw_cc_library(
name = "heap_dispatcher",
srcs = ["heap_dispatcher.cc"],
hdrs = ["public/pw_async/heap_dispatcher.h"],
includes = ["public"],
deps = [
":dispatcher",
":task",
":types",
],
)
9 changes: 7 additions & 2 deletions pw_async/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ The active Task backend is configured with the GN variable
that meets the interface requirements in ``public/pw_async/task.h``. Task will
then trivially wrap ``NativeTask``.

The bazel build provides the ``pw_async_task_backend`` label flag to configure
the active Task backend.

FakeDispatcher
--------------
The FakeDispatcher facade is a utility for simulating a real Dispatcher
Expand All @@ -72,13 +75,16 @@ code that uses Dispatcher. FakeDispatcher is a facade instead of a concrete
implementation because it depends on Task state for processing tasks, which
varies across Task backends.

The active Task backend is configured with the GN variable
The active FakeDispatcher backend is configured with the GN variable
``pw_async_FAKE_DISPATCHER_BACKEND``. The specified target must define a class
``pw::async::test::backend::NativeFakeDispatcher`` in the header
``pw_async_backend/fake_dispatcher.h`` that meets the interface requirements in
``public/pw_async/task.h``. FakeDispatcher will then trivially wrap
``NativeFakeDispatcher``.

The bazel build provides the ``pw_async_fake_dispatcher_backend`` label flag to
configure the FakeDispatcher backend.

Testing FakeDispatcher
^^^^^^^^^^^^^^^^^^^^^^
The GN template ``fake_dispatcher_tests`` in ``fake_dispatcher_tests.gni``
Expand Down Expand Up @@ -193,6 +199,5 @@ Roadmap
-------
- Stabilize Task cancellation API
- Utility for dynamically allocated Tasks
- Bazel support
- CMake support
- Support for C++20 coroutines
95 changes: 82 additions & 13 deletions pw_async_basic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,89 @@
# License for the specific language governing permissions and limitations under
# the License.

filegroup(
name = "pw_async_files",
srcs = [
"dispatcher.cc",
"dispatcher_test.cc",
"fake_dispatcher.cc",
"fake_dispatcher_fixture_test.cc",
"heap_dispatcher_test.cc",
"public/pw_async_basic/dispatcher.h",
"public/pw_async_basic/fake_dispatcher.h",
load(
"//pw_build:pigweed.bzl",
"pw_cc_library",
"pw_cc_test",
)

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

# Backend for //pw_async:task
pw_cc_library(
name = "task",
hdrs = [
"public/pw_async_basic/task.h",
"public_overrides/pw_async_backend/fake_dispatcher.h",
"public_overrides/pw_async_backend/task.h",
"size_report/post_1_task.cc",
"size_report/task.cc",
],
includes = [
"public",
"public_overrides",
],
deps = [
"//pw_async:task_facade",
"//pw_containers:intrusive_list",
],
)

# Backend for //pw_async:fake_dispatcher
pw_cc_library(
name = "fake_dispatcher",
srcs = ["fake_dispatcher.cc"],
hdrs = [
"public/pw_async_basic/fake_dispatcher.h",
"public_overrides/pw_async_backend/fake_dispatcher.h",
],
includes = [
"public",
"public_override",
],
deps = [
"//pw_async:fake_dispatcher_facade",
"//pw_async:task",
"//pw_log",
],
)

pw_cc_test(
name = "fake_dispatcher_fixture_test",
srcs = ["fake_dispatcher_fixture_test.cc"],
deps = ["//pw_async:fake_dispatcher_fixture"],
)

pw_cc_library(
name = "dispatcher",
srcs = ["dispatcher.cc"],
hdrs = ["public/pw_async_basic/dispatcher.h"],
includes = ["public"],
deps = [
"//pw_async:dispatcher",
"//pw_async:task",
"//pw_containers:intrusive_list",
"//pw_log",
"//pw_sync:interrupt_spin_lock",
"//pw_sync:timed_thread_notification",
"//pw_thread:thread_core",
],
)

pw_cc_test(
name = "dispatcher_test",
srcs = ["dispatcher_test.cc"],
deps = [
":dispatcher",
"//pw_log",
"//pw_thread:thread",
],
)

pw_cc_test(
name = "heap_dispatcher_test",
srcs = ["heap_dispatcher_test.cc"],
deps = [
"//pw_async:fake_dispatcher_fixture",
"//pw_async:heap_dispatcher",
],
)
38 changes: 38 additions & 0 deletions pw_async_basic/size_report/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2023 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load(
"//pw_build:pigweed.bzl",
"pw_cc_binary",
)

licenses(["notice"])

pw_cc_binary(
name = "post_1_task",
srcs = ["post_1_task.cc"],
deps = [
"//pw_async_basic:dispatcher",
"//pw_bloat:bloat_this_binary",
],
)

pw_cc_binary(
name = "task",
srcs = ["task.cc"],
deps = [
"//pw_async:task",
"//pw_bloat:bloat_this_binary",
],
)
5 changes: 3 additions & 2 deletions pw_async_basic/size_report/post_1_task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ int main() {
pw::bloat::BloatThisBinary();

pw::async::BasicDispatcher dispatcher;
pw::async::Task task(
[](pw::async::Context& /*ctx*/) { printf("hello world\n"); });
pw::async::Task task([](pw::async::Context& /*ctx*/, pw::Status /*status*/) {
printf("hello world\n");
});
dispatcher.Post(task);
dispatcher.Run();
return 0;
Expand Down
3 changes: 2 additions & 1 deletion pw_async_basic/size_report/task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

int main() {
pw::bloat::BloatThisBinary();
pw::async::Task task([](pw::async::Context& /*ctx*/) {});
pw::async::Task task(
[](pw::async::Context& /*ctx*/, pw::Status /*status*/) {});
return 0;
}
10 changes: 10 additions & 0 deletions targets/default_config.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ label_flag(
build_setting_default = "@pigweed//pw_assert:backend_multiplexer",
)

label_flag(
name = "pw_async_task_backend",
build_setting_default = "@pigweed//pw_async_basic:task",
)

label_flag(
name = "pw_async_fake_dispatcher_backend",
build_setting_default = "@pigweed//pw_async_basic:fake_dispatcher",
)

label_flag(
name = "pw_boot_backend",
build_setting_default = "@pigweed//pw_boot:backend_multiplexer",
Expand Down

0 comments on commit 11f9435

Please sign in to comment.