Page MenuHomePhabricator

Please use GitHub pull requests for new patches. Phabricator shutdown timeline

[libc++] Implement `std::condition_variable_any::wait[_for/until]` overloads that take `stop_token`
AcceptedPublic

Authored by huixie90 on Jun 21 2023, 9:40 AM.

Details

Reviewers
ldionne
Group Reviewers
Restricted Project
Summary

Diff Detail

Unit TestsFailed

TimeTest
1,240 mslibcxx-ci - Apple back-deployment macosx10.13 > apple-libc++-backdeployment-cfg-in.std/input_output/iostream_objects/narrow_stream_objects::cerr.sh.cpp
Script: -- : 'SETUP'; cd /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/libcxx/test/std/input.output/iostream.objects/narrow.stream.objects && cp ../check-stderr.sh /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/build/apple-system-backdeployment-10.13/test/std/input.output/iostream.objects/narrow.stream.objects/Output/cerr.sh.cpp.dir
1,220 mslibcxx-ci - Apple back-deployment macosx10.13 > apple-libc++-backdeployment-cfg-in.std/input_output/iostream_objects/narrow_stream_objects::cin.sh.cpp
Script: -- : 'SETUP'; cd /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/libcxx/test/std/input.output/iostream.objects/narrow.stream.objects && cp ../send-stdin.sh /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/build/apple-system-backdeployment-10.13/test/std/input.output/iostream.objects/narrow.stream.objects/Output/cin.sh.cpp.dir
1,260 mslibcxx-ci - Apple back-deployment macosx10.13 > apple-libc++-backdeployment-cfg-in.std/input_output/iostream_objects/narrow_stream_objects::clog.sh.cpp
Script: -- : 'SETUP'; cd /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/libcxx/test/std/input.output/iostream.objects/narrow.stream.objects && cp ../check-stderr.sh /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/build/apple-system-backdeployment-10.13/test/std/input.output/iostream.objects/narrow.stream.objects/Output/clog.sh.cpp.dir
1,350 mslibcxx-ci - Apple back-deployment macosx10.13 > apple-libc++-backdeployment-cfg-in.std/input_output/iostream_objects/narrow_stream_objects::cout.sh.cpp
Script: -- : 'SETUP'; cd /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/libcxx/test/std/input.output/iostream.objects/narrow.stream.objects && cp ../check-stdout.sh /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/build/apple-system-backdeployment-10.13/test/std/input.output/iostream.objects/narrow.stream.objects/Output/cout.sh.cpp.dir
1,280 mslibcxx-ci - Apple back-deployment macosx10.13 > apple-libc++-backdeployment-cfg-in.std/input_output/iostream_objects/wide_stream_objects::wcerr-imbue.sh.cpp
Script: -- : 'SETUP'; cd /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/libcxx/test/std/input.output/iostream.objects/wide.stream.objects && cp ../check-stderr.sh /private/tmp/buildkite-builds/ldionne-imac-local/llvm-project/libcxx-ci/build/apple-system-backdeployment-10.13/test/std/input.output/iostream.objects/wide.stream.objects/Output/wcerr-imbue.sh.cpp.dir
View Full Test Results (213 Failed)

Event Timeline

huixie90 created this revision.Jun 21 2023, 9:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2023, 9:40 AM
huixie90 requested review of this revision.Jun 21 2023, 9:40 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2023, 9:40 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript

In general looks good. I have some minor comments. I think it would be good to keep track how often predicates are called and whether that matches the (minimal) expectation. For example when a stop_requested() == true the predicate is called exactly one time.

libcxx/include/condition_variable
106

Can you align the // since C++20 ?

319
328
libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_for_token_pred.pass.cpp
96

This is our typical code style.

135

I wonder here and at other places whether 2 ms is long enough.
Are we sure this is changed after the wait_for is really in it's loop? Or can the flag be set before entering the loop.

Would it make sense to validate how often the predicate is executed? When it's less than 2 the loop was executed 0 times.

libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_token_pred.pass.cpp
43

Please include <concepts>.

ldionne commandeered this revision.Jul 7 2023, 6:17 AM
ldionne edited reviewers, added: huixie90; removed: ldionne.

Commandeering per discussion w/ Hui.

libcxx/include/condition_variable
95

Let's mark this as experimental.

huixie90 commandeered this revision.Fri, Sep 15, 1:24 AM
huixie90 edited reviewers, added: ldionne; removed: huixie90.
huixie90 updated this revision to Diff 556846.Fri, Sep 15, 4:19 AM
huixie90 marked 3 inline comments as done.

address feedback rebase

huixie90 updated this revision to Diff 556876.Fri, Sep 15, 12:21 PM
huixie90 marked an inline comment as done.

CI

huixie90 added inline comments.Sat, Sep 16, 2:41 AM
libcxx/include/condition_variable
319

this is a member function call . not std::wait_until

328

this is a member function call, not std::wait_until

huixie90 updated this revision to Diff 556913.Sun, Sep 17, 7:27 AM

guard stop_token headers with no thread macro

ldionne added inline comments.Tue, Sep 19, 8:11 AM
libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_for_token_pred.pass.cpp
135

If the thread creation is really slow, this test could be vacuous (it would test the same as the test that does request_stop() before we wait_for). Would it make sense to instead do something like:

auto thread = support::make_test_thread([&]() {
    // wait until we're ready
    some_atomic.wait(...);
    ss.request_stop();

    while (!done) {
        cv.notify_all();
        std::this_thread::sleep_for(2ms);
      }
});

std::same_as<bool> auto r = cv.wait_for(lock, ss.get_token(), 1h, [&]() {
  some_atomic.notify_all(); // here signal that we're ready for the other thread to request a stop
  return false;
});
assert(!r);
done = true;
thread.join();

Does this make sense at all? That way the pred will be the one to trigger the fact that we request_stop() on the other thread. Would that be more robust?

libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_token_pred.pass.cpp
80

Whatever we do for the other test we should do here too.

libcxx/test/std/thread/thread.condition/thread.condition.condvarany/wait_until_token_pred.pass.cpp
130

And here.

huixie90 updated this revision to Diff 557207.Thu, Sep 21, 2:58 PM
huixie90 marked 6 inline comments as done.

address feedback

ldionne accepted this revision.Wed, Sep 27, 8:44 AM

LGTM with minor nitpick. You can ignore the CI failures since they are unrelated and have been fixed since then. Thanks!

libcxx/docs/Status/Cxx20.rst
51
This revision is now accepted and ready to land.Wed, Sep 27, 8:44 AM