Pigweed
 
Loading...
Searching...
No Matches
allocate_task.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include "pw_allocator/allocator.h"
17#include "pw_async2/dispatcher.h"
18
19namespace pw::async2 {
20namespace internal {
21
22template <typename Pendable>
23class AllocatedTask final : public Task {
24 public:
25 template <typename... Args>
26 AllocatedTask(pw::allocator::Deallocator& deallocator, Args&&... args)
27 : deallocator_(deallocator), pendable_(std::forward<Args>(args)...) {}
28
29 private:
30 Poll<> DoPend(Context& cx) final { return pendable_.Pend(cx); }
31
32 void DoDestroy() final { deallocator_.Delete(this); }
33
34 pw::allocator::Deallocator& deallocator_;
35 Pendable pendable_;
36};
37
38} // namespace internal
39
46template <typename Pendable>
47Task* AllocateTask(pw::allocator::Allocator& allocator, Pendable&& pendable) {
48 return allocator.New<internal::AllocatedTask<Pendable>>(
49 allocator, std::forward<Pendable>(pendable));
50}
51
58template <typename Pendable, typename... Args>
59Task* AllocateTask(pw::allocator::Allocator& allocator, Args&&... args) {
60 return allocator.New<internal::AllocatedTask<Pendable>>(
61 allocator, std::forward<Args>(args)...);
62}
63
64} // namespace pw::async2
Definition: allocator.h:34
std::enable_if_t<!std::is_array_v< T >, T * > New(Args &&... args)
Definition: allocator.h:57
Definition: dispatcher_base.h:52
Definition: poll.h:54
Definition: dispatcher_base.h:158
Definition: allocate_task.h:23
Poll DoPend(Context &cx) final
Definition: allocate_task.h:30
void DoDestroy() final
Definition: allocate_task.h:32