16#include "pw_allocator/internal/control_block.h"
17#include "pw_allocator/shared_ptr.h"
18#include "pw_assert/assert.h"
19#include "pw_async2/context.h"
20#include "pw_async2/internal/lock.h"
21#include "pw_async2/poll.h"
22#include "pw_containers/intrusive_forward_list.h"
23#include "pw_containers/intrusive_list.h"
24#include "pw_log/tokenized_args.h"
25#include "pw_sync/lock_annotations.h"
32#define PW_ASYNC_TASK_NAME(name) PW_LOG_TOKEN_EXPR("pw_async2", name)
88 explicit constexpr Task(log::Token name = kDefaultName) : name_(name) {}
92 Task& operator=(
const Task&) =
delete;
153 static constexpr log::Token kDefaultName =
184 PW_DASSERT(state_ == State::kUnposted);
185 PW_DASSERT(dispatcher_ ==
nullptr);
186 state_ = State::kWoken;
187 dispatcher_ = &dispatcher;
192 allocator::internal::ControlBlock* Unpost()
205 void UnpostAndReleaseRefFromDispatcherDestructor()
209 state_ = State::kRunning;
225 void AddWakerLocked(Waker& waker)
227 wakers_.push_front(waker);
234 void RemoveWakerLocked(Waker& waker)
236 wakers_.remove(waker);
243 void ReleaseSharedRef(allocator::internal::ControlBlock* control_block)
246 SharedPtr<Task> temp(
this, control_block);
249 void set_control_block(allocator::internal::ControlBlock& control_block)
251 control_block_ = &control_block;
254 enum class State :
unsigned char {
258 kDeregisteredButRunning,
263 State state_
PW_GUARDED_BY(internal::lock()) = State::kUnposted;
271 Dispatcher* dispatcher_
PW_GUARDED_BY(internal::lock()) =
nullptr;
274 allocator::internal::ControlBlock* control_block_
278 IntrusiveForwardList<Waker> wakers_
PW_GUARDED_BY(internal::lock());
Definition: dispatcher.h:67
virtual Poll DoPend(Context &)=0
Poll Pend(Context &cx)
Definition: task.h:115
bool IsRegistered() const
constexpr Task(log::Token name=kDefaultName)
Definition: task.h:88
Definition: intrusive_list.h:88
RunTaskResult
Definition: task.h:38
@ kDeregistered
The task was removed from the dispatcher by another thread.
@ kCompleted
The task finished running.
@ kActive
The task is still posted to the dispatcher.
#define PW_LOG_TOKEN(...)
Definition: tokenized_args.h:63
#define PW_GUARDED_BY(x)
Definition: lock_annotations.h:60
#define PW_NO_LOCK_SAFETY_ANALYSIS
Definition: lock_annotations.h:292
#define PW_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: lock_annotations.h:146
#define PW_UNLOCK_FUNCTION(...)
Definition: lock_annotations.h:247
#define PW_LOCKS_EXCLUDED(...)
Definition: lock_annotations.h:176