uvco 0.1
Loading...
Searching...
No Matches
uvco::anonymous_namespace{combinators.cc}::TaskSetImpl Class Reference
Inheritance diagram for uvco::anonymous_namespace{combinators.cc}::TaskSetImpl:
Collaboration diagram for uvco::anonymous_namespace{combinators.cc}::TaskSetImpl:

Public Member Functions

 TaskSetImpl ()=default
 TaskSetImpl (const TaskSetImpl &)=delete
 TaskSetImpl (TaskSetImpl &&)=delete
TaskSetImploperator= (const TaskSetImpl &)=delete
TaskSetImploperator= (TaskSetImpl &&)=delete
 ~TaskSetImpl () override=default
Public Member Functions inherited from uvco::TaskSet
 TaskSet ()=default
 TaskSet (const TaskSet &)=delete
 TaskSet (TaskSet &&) noexcept=default
TaskSetoperator= (const TaskSet &)=delete
TaskSetoperator= (TaskSet &&) noexcept=default
virtual ~TaskSet ()=default

Private Member Functions

Id add (Promise< void > task) override
bool empty () override
 Check if there are any active tasks on the TaskSet.
Promise< void > onEmpty () override
void setOnError (ErrorCallback ecb) override
Promise< void > wrap (Id taskId, Promise< void > task)

Private Attributes

Id counter_ = 0
std::unordered_map< Id, Promise< void > > tasks_ {}
ErrorCallback errorCallback_
std::deque< IddoneTasks_ {}
WaitPoint onEmpty_ {}

Additional Inherited Members

Public Types inherited from uvco::TaskSet
using Id = size_t
using ErrorCallback = std::function<void(Id, std::exception_ptr)>
Static Public Member Functions inherited from uvco::TaskSet
static std::unique_ptr< TaskSetcreate ()

Constructor & Destructor Documentation

◆ TaskSetImpl() [1/3]

uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::TaskSetImpl ( )
default

◆ TaskSetImpl() [2/3]

uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::TaskSetImpl ( const TaskSetImpl & )
delete

◆ TaskSetImpl() [3/3]

uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::TaskSetImpl ( TaskSetImpl && )
delete

◆ ~TaskSetImpl()

uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::~TaskSetImpl ( )
overridedefault

Member Function Documentation

◆ add()

Id uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::add ( Promise< void > task)
inlineoverrideprivatevirtual

Add a task to the TaskSet. It will be run to completion. If an exception is thrown, it is printed to stderr, or an ErrorCallback will be called if set before using setOnError().

Implements uvco::TaskSet.

90 {
91 const Id taskId = counter_++;
92 tasks_.insert({taskId, wrap(taskId, std::move(task))});
93 return taskId;
94 }
size_t Id
Definition combinators.h:145
std::unordered_map< Id, Promise< void > > tasks_
Definition combinators.cc:154
Promise< void > wrap(Id taskId, Promise< void > task)
Definition combinators.cc:109

◆ empty()

bool uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::empty ( )
inlineoverrideprivatevirtual

Check if there are any active tasks on the TaskSet.

Implements uvco::TaskSet.

96{ return 0 == tasks_.size() - doneTasks_.size(); }
std::deque< Id > doneTasks_
Definition combinators.cc:157

◆ onEmpty()

Promise< void > uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::onEmpty ( )
inlineoverrideprivatevirtual

Return a Promise which will be fulfilled when no tasks are left on the TaskSet. Multiple calls to this will return Promise instances which will become ready simultaneously; this means that depending on the scheduling order, by the time this Promise resolves, the TaskSet instance may not be empty anymore.

Implements uvco::TaskSet.

98 {
99 if (empty()) {
100 co_return;
101 }
102 co_await onEmpty_.wait();
103 }
WaitPoint onEmpty_
Definition combinators.cc:158
bool empty() override
Check if there are any active tasks on the TaskSet.
Definition combinators.cc:96

◆ operator=() [1/2]

TaskSetImpl & uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::operator= ( const TaskSetImpl & )
delete

◆ operator=() [2/2]

TaskSetImpl & uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::operator= ( TaskSetImpl && )
delete

◆ setOnError()

void uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::setOnError ( ErrorCallback callback)
inlineoverrideprivatevirtual

Register a callback which handles errors thrown by tasks. By default, errors are logged to stderr. The callback is called when a task finishes by throwing an exception.

Implements uvco::TaskSet.

105 {
106 errorCallback_ = std::move(ecb);
107 }
ErrorCallback errorCallback_
Definition combinators.cc:155

◆ wrap()

Promise< void > uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::wrap ( Id taskId,
Promise< void > task )
inlineprivate
109 {
110 // Wait for task to finish; do regular housekeeping; then mark current task
111 // as ready for cleanup.
112 try {
113 co_await task;
114 } catch (const std::exception &e) {
115 if (errorCallback_) {
116 errorCallback_(taskId, std::current_exception());
117 } else {
118 fmt::print(stderr,
119 "TaskSet task {} failed with exception: {} (setOnError() to "
120 "suppress this message)\n",
121 taskId, e.what());
122 }
123 } catch (...) {
124 if (errorCallback_) {
125 errorCallback_(taskId, std::current_exception());
126 } else {
127 fmt::print(stderr,
128 "TaskSet task {} failed with unknown exception "
129 "(setOnError() to suppress this message)\n",
130 taskId);
131 }
132 }
133
134 // Already trigger onEmpty if no more tasks are left. Otherwise this leads
135 // to a deadlock-like problem, as no more tasks will run to trigger the
136 // cleanup, and thus notify onEmpty.
137 if (tasks_.size() - doneTasks_.size() - 1 == 0) {
138 onEmpty_.releaseAll();
139 }
140
141 // Drop finished tasks.
142 while (!doneTasks_.empty()) {
143 tasks_.erase(doneTasks_.front());
144 doneTasks_.pop_front();
145 }
146
147 // We cannot erase the task directly here, as the task in tasks_ refers to
148 // this very coroutine. Therefore, mark it for cleanup and deal with it
149 // later.
150 doneTasks_.push_back(taskId);
151 }

Member Data Documentation

◆ counter_

Id uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::counter_ = 0
private

◆ doneTasks_

std::deque<Id> uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::doneTasks_ {}
private
157{};

◆ errorCallback_

ErrorCallback uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::errorCallback_
private

◆ onEmpty_

WaitPoint uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::onEmpty_ {}
private
158{};

◆ tasks_

std::unordered_map<Id, Promise<void> > uvco::anonymous_namespace{combinators.cc}::TaskSetImpl::tasks_ {}
private
154{};

The documentation for this class was generated from the following file: