aboutsummaryrefslogtreecommitdiff
path: root/pw_async_basic/public/pw_async_basic/task.h
blob: e2be508c6b0a9069c40c2b2be7013862c417bbfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 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_async/context.h"
#include "pw_async/task_function.h"
#include "pw_chrono/system_clock.h"
#include "pw_containers/intrusive_list.h"

namespace pw::async {
class BasicDispatcher;
namespace test::backend {
class NativeFakeDispatcher;
}
}  // namespace pw::async

namespace pw::async::backend {

// Task backend for BasicDispatcher.
class NativeTask final : public IntrusiveList<NativeTask>::Item {
 private:
  friend class ::pw::async::Task;
  friend class ::pw::async::BasicDispatcher;
  friend class ::pw::async::test::backend::NativeFakeDispatcher;

  NativeTask(::pw::async::Task& task) : task_(task) {}
  explicit NativeTask(::pw::async::Task& task, TaskFunction&& f)
      : func_(std::move(f)), task_(task) {}
  void operator()(Context& ctx, Status status) { func_(ctx, status); }
  void set_function(TaskFunction&& f) { func_ = std::move(f); }

  pw::chrono::SystemClock::time_point due_time() const { return due_time_; }
  void set_due_time(chrono::SystemClock::time_point due_time) {
    due_time_ = due_time;
  }

  TaskFunction func_ = nullptr;
  // task_ is placed after func_ to take advantage of the padding that would
  // otherwise be added here. On 32-bit systems, func_ and due_time_ have an
  // alignment of 8, but func_ has a size of 12 by default. Thus, 4 bytes of
  // padding would be added here, which is just enough for a pointer.
  Task& task_;
  pw::chrono::SystemClock::time_point due_time_;
};

using NativeTaskHandle = NativeTask&;

}  // namespace pw::async::backend