xref: /aosp_15_r20/external/pigweed/pw_async_fuchsia/public/pw_async_fuchsia/task.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 <lib/async/task.h>
17 
18 #include "pw_async/task_function.h"
19 #include "pw_chrono/system_clock.h"
20 
21 namespace pw::async_fuchsia {
22 
23 // NativeTask friend forward declaration.
24 class FuchsiaDispatcher;
25 
26 }  // namespace pw::async_fuchsia
27 
28 namespace pw::async::test::backend {
29 
30 // NativeTask friend forward declaration.
31 class NativeFakeDispatcher;
32 
33 }  // namespace pw::async::test::backend
34 
35 namespace pw::async::backend {
36 
37 class NativeTask final : public async_task_t {
38  private:
39   friend class ::pw::async_fuchsia::FuchsiaDispatcher;
40   friend class ::pw::async::test::backend::NativeFakeDispatcher;
41   friend class ::pw::async::Task;
42 
43   explicit NativeTask(::pw::async::Task& task);
44   explicit NativeTask(::pw::async::Task& task, TaskFunction&& f);
45 
46   void operator()(Context& ctx, Status status);
47 
48   void set_function(TaskFunction&& f);
49 
50   chrono::SystemClock::time_point due_time() const;
51 
52   void set_due_time(chrono::SystemClock::time_point due_time);
53 
54   static void Handler(async_dispatcher_t* /*dispatcher*/,
55                       async_task_t* task,
56                       zx_status_t status);
57 
58   TaskFunction func_;
59   Task& task_;
60   // `dispatcher_` is set by a Dispatcher to its own address before forwarding a
61   // task to the underlying Zircon async-loop, so that the Dispatcher pointer in
62   // the Context may be set in a type-safe manner inside `Handler` when invoked
63   // by the async-loop.
64   Dispatcher* dispatcher_ = nullptr;
65 };
66 
67 using NativeTaskHandle = NativeTask&;
68 
69 }  // namespace pw::async::backend
70