C/C++ API Reference
Loading...
Searching...
No Matches
dispatcher.h
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/dispatcher.h"
17#include "pw_async/task.h"
18#include "pw_sync/interrupt_spin_lock.h"
19#include "pw_sync/lock_annotations.h"
20#include "pw_sync/timed_thread_notification.h"
21#include "pw_thread/thread_core.h"
22
23namespace pw::async {
24
26
28class BasicDispatcher : public Dispatcher, public thread::ThreadCore {
29 public:
30 explicit BasicDispatcher() = default;
31 ~BasicDispatcher() override;
32
35
38 void RunUntil(chrono::SystemClock::time_point end_time);
39
43
49
50 // ThreadCore overrides:
51
55 void Run() override PW_LOCKS_EXCLUDED(lock_);
56
57 // Dispatcher overrides:
58 void PostAt(Task& task, chrono::SystemClock::time_point time) override;
59 bool Cancel(Task& task) override PW_LOCKS_EXCLUDED(lock_);
60
61 // VirtualSystemClock overrides:
62 chrono::SystemClock::time_point now() override {
64 }
65
66 protected:
71 virtual void ExecuteTask(backend::NativeTask& task, Status status)
72 PW_LOCKS_EXCLUDED(lock_);
73
74 private:
75 // Insert |task| into task_queue_ maintaining its min-heap property, keyed by
76 // |time_due|.
77 void PostTaskInternal(backend::NativeTask& task,
78 chrono::SystemClock::time_point time_due)
79 PW_LOCKS_EXCLUDED(lock_);
80
81 // If no tasks are due, sleep until a notification is received or the next
82 // task comes due; whichever occurs first.
83 void MaybeSleep() PW_EXCLUSIVE_LOCKS_REQUIRED(lock_);
84
85 // If no tasks are due, sleep until a notification is received, the next task
86 // comes due, or a timeout elapses; whichever occurs first.
87 void MaybeSleepUntil(std::optional<chrono::SystemClock::time_point> wake_time)
89
90 // Dequeue and run each task that is due.
91 void ExecuteDueTasks() PW_EXCLUSIVE_LOCKS_REQUIRED(lock_);
92
93 // Dequeue each task and call each TaskFunction with a PW_STATUS_CANCELLED
94 // status.
95 void DrainTaskQueue() PW_EXCLUSIVE_LOCKS_REQUIRED(lock_);
96
97 sync::InterruptSpinLock lock_;
98 sync::TimedThreadNotification timed_notification_;
99 bool stop_requested_ PW_GUARDED_BY(lock_) = false;
100 // A priority queue of scheduled Tasks sorted by earliest due times first.
101 IntrusiveList<backend::NativeTask> task_queue_ PW_GUARDED_BY(lock_);
102};
103
104} // namespace pw::async
Definition: status.h:109
BasicDispatcher is a generic implementation of Dispatcher.
Definition: dispatcher.h:28
Definition: dispatcher.h:48
Definition: task.h:33
Definition: intrusive_list.h:88
void RunUntilIdle()
Execute all runnable tasks and return without waiting.
void RunUntil(chrono::SystemClock::time_point end_time)
virtual void ExecuteTask(backend::NativeTask &task, Status status)
chrono::SystemClock::time_point now() override
Returns the current time.
Definition: dispatcher.h:62
void PostAt(Task &task, chrono::SystemClock::time_point time) override
void RunFor(chrono::SystemClock::duration duration)
bool Cancel(Task &task) override
static time_point now() noexcept
This is thread and IRQ safe. This must be provided by the backend.
Definition: system_clock.h:117
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:90
#define PW_GUARDED_BY(x)
Definition: lock_annotations.h:60
#define PW_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: lock_annotations.h:146
#define PW_LOCKS_EXCLUDED(...)
Definition: lock_annotations.h:176
(Deprecated) Async library
Definition: context.h:19