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;
223 void AddWakerLocked(Waker& waker)
225 wakers_.push_front(waker);
232 void RemoveWakerLocked(Waker& waker)
234 wakers_.remove(waker);
241 void ReleaseSharedRef(allocator::internal::ControlBlock* control_block)
244 SharedPtr<Task> temp(
this, control_block);
249 void SetControlBlockBeforePosted(
250 allocator::internal::ControlBlock& control_block)
252 control_block_ = &control_block;
255 enum class State :
unsigned char {
259 kDeregisteredButRunning,
265 State state_
PW_GUARDED_BY(internal::lock()) = State::kUnposted;
273 Dispatcher* dispatcher_
PW_GUARDED_BY(internal::lock()) =
nullptr;
276 allocator::internal::ControlBlock* control_block_
280 IntrusiveForwardList<Waker> wakers_
PW_GUARDED_BY(internal::lock());
Definition: dispatcher.h:75
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:296
#define PW_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: lock_annotations.h:147
#define PW_UNLOCK_FUNCTION(...)
Definition: lock_annotations.h:249
#define PW_LOCKS_EXCLUDED(...)
Definition: lock_annotations.h:178