-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc++] Applied [[nodiscard]] to concurrency (partially)
#169463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Zingam
merged 4 commits into
llvm:main
from
H-G-Hristov:hgh/libcxx/nodiscard-to-concurrency
Nov 26, 2025
+173
−53
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // UNSUPPORTED: c++03 | ||
| // UNSUPPORTED: no-threads | ||
|
|
||
| // Check that functions are marked [[nodiscard]] | ||
|
|
||
| #include <chrono> | ||
| #include <barrier> | ||
| #include <latch> | ||
| #include <mutex> | ||
| #include <semaphore> | ||
| #include <thread> | ||
|
|
||
| #include "test_macros.h" | ||
|
|
||
| using namespace std::chrono_literals; | ||
|
|
||
| const auto timePoint = std::chrono::steady_clock::now(); | ||
|
|
||
| void test() { | ||
| // Threads | ||
| { | ||
| std::thread th; | ||
|
|
||
| th.joinable(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| th.get_id(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| th.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| th.hardware_concurrency(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
| #if TEST_STD_VER >= 20 | ||
| { | ||
| std::jthread jt; | ||
|
|
||
| jt.joinable(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| jt.get_id(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| jt.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| jt.get_stop_source(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| jt.get_stop_token(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| jt.hardware_concurrency(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
| #endif | ||
|
|
||
| // Mutual exclusion | ||
|
|
||
| { // <mutex> | ||
| std::mutex m; | ||
|
|
||
| m.try_lock(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| m.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
| { | ||
| std::recursive_mutex m; | ||
|
|
||
| m.try_lock(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| m.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
| { | ||
| std::timed_mutex m; | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| m.try_lock(); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| m.try_lock_for(82ms); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| m.try_lock_until(timePoint); | ||
| } | ||
| { | ||
| std::recursive_timed_mutex m; | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| m.try_lock(); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| m.try_lock_for(82ms); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| m.try_lock_until(timePoint); | ||
| } | ||
| { | ||
| std::mutex m1; | ||
| std::mutex m2; | ||
| std::mutex m3; | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| std::try_lock(m1, m2); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| std::try_lock(m1, m2, m3); | ||
| } | ||
|
|
||
| // Condition variables | ||
|
|
||
| { // <condition_variable> | ||
| std::condition_variable cv; | ||
|
|
||
| cv.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
|
|
||
| #if TEST_STD_VER >= 20 | ||
|
|
||
| // Semaphores | ||
|
|
||
| { // <semaphore> | ||
| std::counting_semaphore<> cs{0}; | ||
|
|
||
| cs.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| cs.try_acquire_for(92ms); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| cs.try_acquire(); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| cs.try_acquire_until(timePoint); | ||
|
|
||
| std::binary_semaphore bs{0}; | ||
|
|
||
| bs.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
|
|
||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| bs.try_acquire_for(82ms); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| bs.try_acquire(); | ||
| // expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| bs.try_acquire_until(timePoint); | ||
| } | ||
|
|
||
| // Latches and barriers | ||
|
|
||
| { // <barrier> | ||
| std::barrier<> b{94}; | ||
|
|
||
| b.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
| { // <latch> | ||
| std::latch l{94}; | ||
|
|
||
| l.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| l.try_wait(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} | ||
| } | ||
|
|
||
| #endif | ||
| } |
29 changes: 0 additions & 29 deletions
29
libcxx/test/std/thread/thread.jthread/nodiscard.verify.cpp
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, the
try_*functions below.