-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This function is designed to allow for easy transition from heap-allocated callback-based APIs or uses of the deprecated pw::async HeapDispatcher. Change-Id: I067c938b4f8ea931bad4e7c71e6869e4a707a150 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/242035 Commit-Queue: Taylor Cramer <[email protected]> Reviewed-by: Aaron Green <[email protected]> Lint: Lint 🤖 <[email protected]>
- Loading branch information
Showing
7 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2024 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. | ||
|
||
#include "pw_async2/enqueue_heap_func.h" | ||
|
||
#include "pw_async2/dispatcher_base.h" | ||
#include "pw_status/status.h" | ||
#include "pw_unit_test/framework.h" | ||
|
||
namespace { | ||
|
||
using ::pw::async2::Dispatcher; | ||
using ::pw::async2::EnqueueHeapFunc; | ||
|
||
TEST(Dispatcher, DispatcherRunsEnqueuedTasksOnce) { | ||
Dispatcher dispatcher; | ||
int ran = 0; | ||
EnqueueHeapFunc(dispatcher, [&ran]() { ++ran; }); | ||
EXPECT_EQ(ran, 0); | ||
EXPECT_TRUE(dispatcher.RunUntilStalled().IsReady()); | ||
EXPECT_EQ(ran, 1); | ||
EXPECT_TRUE(dispatcher.RunUntilStalled().IsReady()); | ||
EXPECT_EQ(ran, 1); | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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. | ||
#pragma once | ||
|
||
#include "pw_async2/dispatcher.h" | ||
|
||
namespace pw::async2 { | ||
namespace internal { | ||
|
||
template <typename Func> | ||
class RunHeapFuncTask : public Task { | ||
public: | ||
static Task& New(Func&& func) { | ||
return *(new RunHeapFuncTask(std::forward<Func>(func))); | ||
} | ||
|
||
private: | ||
RunHeapFuncTask(Func&& func) : func_(std::forward<Func>(func)) {} | ||
Poll<> DoPend(Context&) final { | ||
func_(); | ||
return Ready(); | ||
} | ||
void DoDestroy() final { delete this; } | ||
Func func_; | ||
}; | ||
|
||
} // namespace internal | ||
|
||
/// Heap-allocates space for ``func`` and enqueues it to run on ``dispatcher``. | ||
/// | ||
/// ``func`` must be a no-argument callable that returns ``void``. | ||
/// | ||
/// This function requires heap allocation using ``new`` be available. | ||
template <typename Func> | ||
void EnqueueHeapFunc(Dispatcher& dispatcher, Func&& func) { | ||
return dispatcher.Post( | ||
internal::RunHeapFuncTask<Func>::New(std::forward<Func>(func))); | ||
} | ||
|
||
} // namespace pw::async2 |