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
15 #include "pw_async_fuchsia/task.h"
16
17 #include <zircon/assert.h>
18
19 #include "pw_async_fuchsia/util.h"
20
21 namespace pw::async::backend {
22
NativeTask(::pw::async::Task & task)23 NativeTask::NativeTask(::pw::async::Task& task)
24 : async_task_t{{ASYNC_STATE_INIT}, &NativeTask::Handler, {}}, task_(task) {}
NativeTask(::pw::async::Task & task,TaskFunction && f)25 NativeTask::NativeTask(::pw::async::Task& task, TaskFunction&& f)
26 : async_task_t{{ASYNC_STATE_INIT}, &NativeTask::Handler, {}},
27 func_(std::move(f)),
28 task_(task) {}
29
operator ()(Context & ctx,Status status)30 void NativeTask::operator()(Context& ctx, Status status) { func_(ctx, status); }
31
set_function(TaskFunction && f)32 void NativeTask::set_function(TaskFunction&& f) { func_ = std::move(f); }
33
due_time() const34 pw::chrono::SystemClock::time_point NativeTask::due_time() const {
35 return pw::async_fuchsia::ZxTimeToTimepoint(zx::time{deadline});
36 }
37
set_due_time(chrono::SystemClock::time_point due_time)38 void NativeTask::set_due_time(chrono::SystemClock::time_point due_time) {
39 deadline = pw::async_fuchsia::TimepointToZxTime(due_time).get();
40 }
41
Handler(async_dispatcher_t *,async_task_t * task,zx_status_t status)42 void NativeTask::Handler(async_dispatcher_t* /*dispatcher*/,
43 async_task_t* task,
44 zx_status_t status) {
45 auto self = static_cast<NativeTask*>(task);
46 Context c{.dispatcher = self->dispatcher_, .task = &self->task_};
47 (*self)(c, status == ZX_OK ? OkStatus() : Status::Cancelled());
48 }
49
50 } // namespace pw::async::backend
51