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:155
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:158

◆ 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:159
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:156

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

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
158{};

◆ errorCallback_

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

◆ onEmpty_

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

◆ tasks_

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

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