Skip to content

Commit d126f49

Browse files
committed
Addressed comments
1 parent 059c7f0 commit d126f49

File tree

5 files changed

+67
-15
lines changed

5 files changed

+67
-15
lines changed

libcxx/include/__mutex/mutex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_CAPABILITY("mutex") mutex {
3737
# endif
3838

3939
_LIBCPP_ACQUIRE_CAPABILITY() void lock();
40-
_LIBCPP_TRY_ACQUIRE_CAPABILITY(true) bool try_lock() _NOEXCEPT;
40+
[[__nodiscard__]] _LIBCPP_TRY_ACQUIRE_CAPABILITY(true) bool try_lock() _NOEXCEPT;
4141
_LIBCPP_RELEASE_CAPABILITY void unlock() _NOEXCEPT;
4242

4343
typedef __libcpp_mutex_t* native_handle_type;

libcxx/include/latch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public:
9999
if (__old == __update)
100100
__a_.notify_all();
101101
}
102-
inline _LIBCPP_HIDE_FROM_ABI bool try_wait() const noexcept {
102+
[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI bool try_wait() const noexcept {
103103
auto __value = __a_.load(memory_order_acquire);
104104
return try_wait_impl(__value);
105105
}

libcxx/include/mutex

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public:
229229
recursive_mutex& operator=(const recursive_mutex&) = delete;
230230

231231
void lock();
232-
bool try_lock() _NOEXCEPT;
232+
[[__nodiscard__]] bool try_lock() _NOEXCEPT;
233233
void unlock() _NOEXCEPT;
234234

235235
typedef __libcpp_recursive_mutex_t* native_handle_type;
@@ -251,14 +251,14 @@ public:
251251

252252
public:
253253
void lock();
254-
bool try_lock() _NOEXCEPT;
254+
[[__nodiscard__]] bool try_lock() _NOEXCEPT;
255255
template <class _Rep, class _Period>
256-
_LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
256+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
257257
return try_lock_until(chrono::steady_clock::now() + __d);
258258
}
259259

260260
template <class _Clock, class _Duration>
261-
_LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
261+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
262262
using namespace chrono;
263263
unique_lock<mutex> __lk(__m_);
264264
bool __no_timeout = _Clock::now() < __t;
@@ -288,14 +288,14 @@ public:
288288
recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
289289

290290
void lock();
291-
bool try_lock() _NOEXCEPT;
291+
[[__nodiscard__]] bool try_lock() _NOEXCEPT;
292292
template <class _Rep, class _Period>
293-
_LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
293+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
294294
return try_lock_until(chrono::steady_clock::now() + __d);
295295
}
296296

297297
template <class _Clock, class _Duration>
298-
_LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
298+
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
299299
using namespace chrono;
300300
__thread_id __id = this_thread::get_id();
301301
unique_lock<mutex> __lk(__m_);
@@ -320,7 +320,7 @@ public:
320320
};
321321

322322
template <class _L0, class _L1>
323-
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1) {
323+
[[__nodiscard__]] _LIBCPP_NO_THREAD_SAFETY_ANALYSIS _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1) {
324324
unique_lock<_L0> __u0(__l0, try_to_lock_t());
325325
if (__u0.owns_lock()) {
326326
if (__l1.try_lock()) {
@@ -335,7 +335,7 @@ _LIBCPP_NO_THREAD_SAFETY_ANALYSIS _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0,
335335
# ifndef _LIBCPP_CXX03_LANG
336336

337337
template <class _L0, class _L1, class _L2, class... _L3>
338-
_LIBCPP_NO_THREAD_SAFETY_ANALYSIS _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
338+
[[__nodiscard__]] _LIBCPP_NO_THREAD_SAFETY_ANALYSIS _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
339339
int __r = 0;
340340
unique_lock<_L0> __u0(__l0, try_to_lock);
341341
if (__u0.owns_lock()) {

libcxx/include/semaphore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ public:
156156
}
157157
_LIBCPP_HIDE_FROM_ABI void acquire() { __semaphore_.acquire(); }
158158
template <class _Rep, class _Period>
159-
_LIBCPP_HIDE_FROM_ABI bool try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) {
159+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) {
160160
return __semaphore_.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time));
161161
}
162-
_LIBCPP_HIDE_FROM_ABI bool try_acquire() { return __semaphore_.try_acquire(); }
162+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool try_acquire() { return __semaphore_.try_acquire(); }
163163
template <class _Clock, class _Duration>
164-
_LIBCPP_HIDE_FROM_ABI bool try_acquire_until(chrono::time_point<_Clock, _Duration> const& __abs_time) {
164+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI bool try_acquire_until(chrono::time_point<_Clock, _Duration> const& __abs_time) {
165165
auto const __current = _Clock::now();
166166
if (__current >= __abs_time)
167167
return try_acquire();

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
// Check that functions are marked [[nodiscard]]
1313

14+
#include <chrono>
1415
#include <barrier>
1516
#include <latch>
1617
#include <mutex>
@@ -19,6 +20,10 @@
1920

2021
#include "test_macros.h"
2122

23+
using namespace std::chrono_literals;
24+
25+
const auto timePoint = std::chrono::steady_clock::now();
26+
2227
void test() {
2328
// Threads
2429
{
@@ -47,13 +52,45 @@ void test() {
4752
{ // <mutex>
4853
std::mutex m;
4954

55+
m.try_lock(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
5056
m.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
5157
}
5258
{
5359
std::recursive_mutex m;
5460

61+
m.try_lock(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
5562
m.native_handle(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
5663
}
64+
{
65+
std::timed_mutex m;
66+
67+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
68+
m.try_lock();
69+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
70+
m.try_lock_for(82ms);
71+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
72+
m.try_lock_until(timePoint);
73+
}
74+
{
75+
std::recursive_timed_mutex m;
76+
77+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
78+
m.try_lock();
79+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
80+
m.try_lock_for(82ms);
81+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
82+
m.try_lock_until(timePoint);
83+
}
84+
{
85+
std::mutex m1;
86+
std::mutex m2;
87+
std::mutex m3;
88+
89+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
90+
std::try_lock(m1, m2);
91+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
92+
std::try_lock(m1, m2, m3);
93+
}
5794

5895
// Condition variables
5996

@@ -72,9 +109,23 @@ void test() {
72109

73110
cs.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
74111

112+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
113+
cs.try_acquire_for(92ms);
114+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
115+
cs.try_acquire();
116+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
117+
cs.try_acquire_until(timePoint);
118+
75119
std::binary_semaphore bs{0};
76120

77121
bs.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
122+
123+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
124+
bs.try_acquire_for(82ms);
125+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
126+
bs.try_acquire();
127+
// expected-warning@+1 {{ignoring return value of function declared with 'nodiscard' attribute}}
128+
bs.try_acquire_until(timePoint);
78129
}
79130

80131
// Latches and barriers
@@ -87,7 +138,8 @@ void test() {
87138
{ // <latch>
88139
std::latch l{94};
89140

90-
l.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
141+
l.max(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
142+
l.try_wait(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
91143
}
92144

93145
#endif

0 commit comments

Comments
 (0)