Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
b62c247b
Commit
b62c247b
authored
Oct 25, 2017
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add TaskSet.onEmpty() to wait until all tasks have completed.
Fixes #286.
parent
5cb6cf76
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
0 deletions
+52
-0
async-test.c++
c++/src/kj/async-test.c++
+24
-0
async.c++
c++/src/kj/async.c++
+20
-0
async.h
c++/src/kj/async.h
+8
-0
No files found.
c++/src/kj/async-test.c++
View file @
b62c247b
...
...
@@ -620,6 +620,30 @@ TEST(Async, TaskSet) {
EXPECT_EQ
(
1u
,
errorHandler
.
exceptionCount
);
}
TEST
(
Async
,
TaskSetOnEmpty
)
{
EventLoop
loop
;
WaitScope
waitScope
(
loop
);
ErrorHandlerImpl
errorHandler
;
TaskSet
tasks
(
errorHandler
);
KJ_EXPECT
(
tasks
.
isEmpty
());
auto
paf
=
newPromiseAndFulfiller
<
void
>
();
tasks
.
add
(
kj
::
mv
(
paf
.
promise
));
tasks
.
add
(
evalLater
([]()
{}));
KJ_EXPECT
(
!
tasks
.
isEmpty
());
auto
promise
=
tasks
.
onEmpty
();
KJ_EXPECT
(
!
promise
.
poll
(
waitScope
));
KJ_EXPECT
(
!
tasks
.
isEmpty
());
paf
.
fulfiller
->
fulfill
();
KJ_ASSERT
(
promise
.
poll
(
waitScope
));
KJ_EXPECT
(
tasks
.
isEmpty
());
promise
.
wait
(
waitScope
);
}
class
DestructorDetector
{
public
:
DestructorDetector
(
bool
&
setTrue
)
:
setTrue
(
setTrue
)
{}
...
...
c++/src/kj/async.c++
View file @
b62c247b
...
...
@@ -127,6 +127,14 @@ protected:
*
prev
=
kj
::
mv
(
next
);
next
=
nullptr
;
prev
=
nullptr
;
KJ_IF_MAYBE
(
f
,
taskSet
.
emptyFulfiller
)
{
if
(
taskSet
.
tasks
==
nullptr
)
{
f
->
get
()
->
fulfill
();
taskSet
.
emptyFulfiller
=
nullptr
;
}
}
return
mv
(
self
);
}
...
...
@@ -165,6 +173,18 @@ kj::String TaskSet::trace() {
return
kj
::
strArray
(
traces
,
"
\n
============================================
\n
"
);
}
Promise
<
void
>
TaskSet
::
onEmpty
()
{
KJ_REQUIRE
(
emptyFulfiller
==
nullptr
,
"onEmpty() can only be called once at a time"
);
if
(
tasks
==
nullptr
)
{
return
READY_NOW
;
}
else
{
auto
paf
=
newPromiseAndFulfiller
<
void
>
();
emptyFulfiller
=
kj
::
mv
(
paf
.
fulfiller
);
return
kj
::
mv
(
paf
.
promise
);
}
}
namespace
_
{
// private
class
LoggingErrorHandler
:
public
TaskSet
::
ErrorHandler
{
...
...
c++/src/kj/async.h
View file @
b62c247b
...
...
@@ -532,11 +532,19 @@ public:
kj
::
String
trace
();
// Return debug info about all promises currently in the TaskSet.
bool
isEmpty
()
{
return
tasks
==
nullptr
;
}
// Check if any tasks are running.
Promise
<
void
>
onEmpty
();
// Returns a promise that fulfills the next time the TaskSet is empty. Only one such promise can
// exist at a time.
private
:
class
Task
;
TaskSet
::
ErrorHandler
&
errorHandler
;
Maybe
<
Own
<
Task
>>
tasks
;
Maybe
<
Own
<
PromiseFulfiller
<
void
>>>
emptyFulfiller
;
};
// =======================================================================================
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment