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)
133 explicit constexpr Task(log::Token name = kDefaultName) : name_(name) {}
137 Task& operator=(
const Task&) =
delete;
205 static constexpr log::Token kDefaultName =
236 PW_DASSERT(state_ == State::kUnposted);
237 PW_DASSERT(dispatcher_ ==
nullptr);
238 state_ = State::kWoken;
239 dispatcher_ = &dispatcher;
244 allocator::internal::ControlBlock* Unpost()
257 void UnpostAndReleaseRefFromDispatcherDestructor()
261 state_ = State::kRunning;
275 void AddWakerLocked(Waker& waker)
277 wakers_.push_front(waker);
284 void RemoveWakerLocked(Waker& waker)
286 wakers_.remove(waker);
289 void ReleaseSharedRef(allocator::internal::ControlBlock* control_block)
292 SharedPtr<Task> temp(
this, control_block);
297 void SetControlBlockBeforePosted(
298 allocator::internal::ControlBlock& control_block)
300 control_block_ = &control_block;
303 enum class State :
unsigned char {
307 kDeregisteredButRunning,
313 State state_
PW_GUARDED_BY(internal::lock()) = State::kUnposted;
319 } waker_requirement_ = kWakerNeeded;
327 Dispatcher* dispatcher_
PW_GUARDED_BY(internal::lock()) =
nullptr;
330 allocator::internal::ControlBlock* control_block_
334 IntrusiveForwardList<Waker> wakers_
PW_GUARDED_BY(internal::lock());
342 static_cast<Task&
>(*this).waker_requirement_ = Task::kNoWakerNeeded;
347 Waker(
static_cast<Task&
>(*
this), {}).Wake();
Definition: intrusive_queue.h:30
Definition: dispatcher.h:74
RunTaskResult
Definition: task.h:83
@ 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.
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:353
PendingType Unschedule()
Definition: task.h:341
void ReEnqueue()
Definition: task.h:346
virtual Poll DoPend(Context &)=0
Poll Pend(Context &cx)
Definition: task.h:160
bool IsRegistered() const
constexpr Task(log::Token name=kDefaultName)
Definition: task.h:133
#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