1 // Copyright 2023 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_async/context.h" 17 #include "pw_async/task_function.h" 18 #include "pw_chrono/system_clock.h" 19 #include "pw_containers/intrusive_list.h" 20 21 namespace pw::async { 22 class BasicDispatcher; 23 namespace test::backend { 24 class NativeFakeDispatcher; 25 } 26 } // namespace pw::async 27 28 namespace pw::async::backend { 29 30 // Task backend for BasicDispatcher. 31 class NativeTask final : public IntrusiveList<NativeTask>::Item { 32 private: 33 friend class ::pw::async::Task; 34 friend class ::pw::async::BasicDispatcher; 35 friend class ::pw::async::test::backend::NativeFakeDispatcher; 36 NativeTask(::pw::async::Task & task)37 NativeTask(::pw::async::Task& task) : task_(task) {} NativeTask(::pw::async::Task & task,TaskFunction && f)38 explicit NativeTask(::pw::async::Task& task, TaskFunction&& f) 39 : func_(std::move(f)), task_(task) {} operator()40 void operator()(Context& ctx, Status status) { func_(ctx, status); } set_function(TaskFunction && f)41 void set_function(TaskFunction&& f) { func_ = std::move(f); } 42 due_time()43 pw::chrono::SystemClock::time_point due_time() const { return due_time_; } set_due_time(chrono::SystemClock::time_point due_time)44 void set_due_time(chrono::SystemClock::time_point due_time) { 45 due_time_ = due_time; 46 } 47 48 TaskFunction func_ = nullptr; 49 // task_ is placed after func_ to take advantage of the padding that would 50 // otherwise be added here. On 32-bit systems, func_ and due_time_ have an 51 // alignment of 8, but func_ has a size of 12 by default. Thus, 4 bytes of 52 // padding would be added here, which is just enough for a pointer. 53 Task& task_; 54 pw::chrono::SystemClock::time_point due_time_; 55 }; 56 57 using NativeTaskHandle = NativeTask&; 58 59 } // namespace pw::async::backend 60