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/internal/lock.h"
20#include "pw_async2/poll.h"
21#include "pw_async2/waker.h"
22#include "pw_containers/intrusive_forward_list.h"
23#include "pw_containers/intrusive_list.h"
24#include "pw_containers/intrusive_queue.h"
25#include "pw_log/tokenized_args.h"
26#include "pw_sync/lock_annotations.h"
33#define PW_ASYNC_TASK_NAME(name) PW_LOG_TOKEN_EXPR("pw_async2", name)
139 explicit constexpr Task(log::Token name = kDefaultName) : name_(name) {}
143 Task& operator=(
const Task&) =
delete;
211 static constexpr log::Token kDefaultName =
242 PW_DASSERT(state_ == State::kUnposted);
243 PW_DASSERT(dispatcher_ ==
nullptr);
244 state_ = State::kWoken;
245 dispatcher_ = &dispatcher;
250 allocator::internal::ControlBlock* Unpost()
263 void UnpostAndReleaseRefFromDispatcherDestructor()
267 state_ = State::kRunning;
281 void AddWakerLocked(Waker& waker)
283 wakers_.push_front(waker);
290 void RemoveWakerLocked(Waker& waker)
292 wakers_.remove(waker);
295 void ReleaseSharedRef(allocator::internal::ControlBlock* control_block)
298 SharedPtr<Task> temp(
this, control_block);
303 void SetControlBlockBeforePosted(
304 allocator::internal::ControlBlock& control_block)
306 control_block_ = &control_block;
309 enum class State :
unsigned char {
313 kDeregisteredButRunning,
319 State state_
PW_GUARDED_BY(internal::lock()) = State::kUnposted;
325 } waker_requirement_ = kWakerNeeded;
333 Dispatcher* dispatcher_
PW_GUARDED_BY(internal::lock()) =
nullptr;
336 allocator::internal::ControlBlock* control_block_
340 IntrusiveForwardList<Waker> wakers_
PW_GUARDED_BY(internal::lock());
352 static_cast<Task&
>(*this).waker_requirement_ = Task::kNoWakerNeeded;
357 Waker(
static_cast<Task&
>(*
this), {}).Wake();
Definition: intrusive_queue.h:30
Definition: dispatcher.h:74
virtual Poll DoPend(Context &)=0
Poll Pend(Context &cx)
Definition: task.h:166
bool IsRegistered() const
constexpr Task(log::Token name=kDefaultName)
Definition: task.h:139
PendingType Unschedule()
Definition: task.h:351
void ReEnqueue()
Definition: task.h:356
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:353
RunTaskResult
Definition: task.h:89
@ 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
The Pigweed namespace.
Definition: alignment.h:27