Skip to content

Commit 059c7f0

Browse files
committed
[libc++] [[nodiscard]] to concurrency
`[[nodiscard]]` should be applied to functions where discarding the return value is most likely a correctness issue. - https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant The following utilities have been anotated in this patch: - [x] `<barrier>` - [x] `<condition_variable>` - [x] `<latch>` - [x] `<mutex>` - [x] `<semaphore>` - [x] `<thread>`
1 parent 6ec6867 commit 059c7f0

File tree

9 files changed

+108
-39
lines changed

9 files changed

+108
-39
lines changed

libcxx/include/__condition_variable/condition_variable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class _LIBCPP_EXPORTED_FROM_ABI condition_variable {
170170
wait_for(unique_lock<mutex>& __lk, const chrono::duration<_Rep, _Period>& __d, _Predicate __pred);
171171

172172
typedef __libcpp_condvar_t* native_handle_type;
173-
_LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__cv_; }
173+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__cv_; }
174174

175175
private:
176176
void

libcxx/include/__mutex/mutex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_CAPABILITY("mutex") mutex {
4141
_LIBCPP_RELEASE_CAPABILITY void unlock() _NOEXCEPT;
4242

4343
typedef __libcpp_mutex_t* native_handle_type;
44-
_LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__m_; }
44+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__m_; }
4545
};
4646

4747
static_assert(is_nothrow_default_constructible<mutex>::value, "the default constructor for std::mutex must be nothrow");

libcxx/include/__thread/thread.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,13 @@ class _LIBCPP_EXPORTED_FROM_ABI thread {
242242

243243
_LIBCPP_HIDE_FROM_ABI void swap(thread& __t) _NOEXCEPT { std::swap(__t_, __t.__t_); }
244244

245-
_LIBCPP_HIDE_FROM_ABI bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); }
245+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool joinable() const _NOEXCEPT { return !__libcpp_thread_isnull(&__t_); }
246246
void join();
247247
void detach();
248-
_LIBCPP_HIDE_FROM_ABI id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); }
249-
_LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() _NOEXCEPT { return __t_; }
248+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI id get_id() const _NOEXCEPT { return __libcpp_thread_get_id(&__t_); }
249+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() _NOEXCEPT { return __t_; }
250250

251-
static unsigned hardware_concurrency() _NOEXCEPT;
251+
[[__nodiscard__]] static unsigned hardware_concurrency() _NOEXCEPT;
252252
};
253253

254254
inline _LIBCPP_HIDE_FROM_ABI void swap(thread& __x, thread& __y) _NOEXCEPT { __x.swap(__y); }

libcxx/include/barrier

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ class barrier {
158158
public:
159159
using arrival_token = typename __barrier_base<_CompletionF>::arrival_token;
160160

161-
static _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t max() noexcept { return __barrier_base<_CompletionF>::max(); }
161+
[[nodiscard]] static _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t max() noexcept {
162+
return __barrier_base<_CompletionF>::max();
163+
}
162164

163165
_LIBCPP_HIDE_FROM_ABI explicit barrier(ptrdiff_t __count, _CompletionF __completion = _CompletionF())
164166
: __b_(__count, std::move(__completion)) {

libcxx/include/latch

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ class latch {
7070
atomic<ptrdiff_t> __a_;
7171

7272
public:
73-
static _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t max() noexcept { return numeric_limits<ptrdiff_t>::max(); }
73+
[[nodiscard]] static _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t max() noexcept {
74+
return numeric_limits<ptrdiff_t>::max();
75+
}
7476

7577
inline _LIBCPP_HIDE_FROM_ABI constexpr explicit latch(ptrdiff_t __expected) : __a_(__expected) {
7678
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(

libcxx/include/mutex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ public:
234234

235235
typedef __libcpp_recursive_mutex_t* native_handle_type;
236236

237-
_LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__m_; }
237+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__m_; }
238238
};
239239

240240
class _LIBCPP_EXPORTED_FROM_ABI timed_mutex {

libcxx/include/semaphore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class counting_semaphore {
133133
public:
134134
static_assert(__least_max_value >= 0, "The least maximum value must be a positive number");
135135

136-
static constexpr ptrdiff_t max() noexcept { return __least_max_value; }
136+
[[nodiscard]] static constexpr ptrdiff_t max() noexcept { return __least_max_value; }
137137

138138
_LIBCPP_HIDE_FROM_ABI constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore_(__count) {
139139
_LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03
10+
// UNSUPPORTED: no-threads
11+
12+
// Check that functions are marked [[nodiscard]]
13+
14+
#include <barrier>
15+
#include <latch>
16+
#include <mutex>
17+
#include <semaphore>
18+
#include <thread>
19+
20+
#include "test_macros.h"
21+
22+
void test() {
23+
// Threads
24+
{
25+
std::thread th;
26+
27+
th.joinable(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
28+
th.get_id(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
29+
th.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
30+
th.hardware_concurrency(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
31+
}
32+
#if TEST_STD_VER >= 20
33+
{
34+
std::jthread jt;
35+
36+
jt.joinable(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
37+
jt.get_id(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
38+
jt.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
39+
jt.get_stop_source(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
40+
jt.get_stop_token(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
41+
jt.hardware_concurrency(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
42+
}
43+
#endif
44+
45+
// Mutual exclusion
46+
47+
{ // <mutex>
48+
std::mutex m;
49+
50+
m.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
51+
}
52+
{
53+
std::recursive_mutex m;
54+
55+
m.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
56+
}
57+
58+
// Condition variables
59+
60+
{ // <condition_variable>
61+
std::condition_variable cv;
62+
63+
cv.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
64+
}
65+
66+
#if TEST_STD_VER >= 20
67+
68+
// Semaphores
69+
70+
{ // <semaphor>
71+
std::counting_semaphore<> cs{0};
72+
73+
cs.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
74+
75+
std::binary_semaphore bs{0};
76+
77+
bs.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
78+
}
79+
80+
// Latches and barriers
81+
82+
{ // <barrier>
83+
std::barrier<> b{94};
84+
85+
b.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
86+
}
87+
{ // <latch>
88+
std::latch l{94};
89+
90+
l.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
91+
}
92+
93+
#endif
94+
}

libcxx/test/std/thread/thread.jthread/nodiscard.verify.cpp

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)