libstdc++: Add nodiscard attribute to mutex try_lock functions
authorJonathan Wakely <jwakely@redhat.com>
Mon, 5 Dec 2022 12:39:23 +0000 (12:39 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 6 Dec 2022 21:33:29 +0000 (21:33 +0000)
libstdc++-v3/ChangeLog:

* include/bits/std_mutex.h (mutex): Add nodiscard attribute to
try_lock member function.
* include/bits/unique_lock.h (unique_lock): Likewise for
try_lock, try_lock_until, try_lock_for member functions, and
owns_lock and mutex member functions.
* include/std/mutex (recursive_mutex): Likewise for try_lock
member function.
(timed_mutex, recursive_timed_mutex, try_lock): Likewise for
try_lock, try_lock_until, try_lock_for member functions.
(try_lock): Likewise for non-member function.
* include/std/shared_mutex (shared_mutex): Likewise for try_lock
and try_lock_shared member functions.
(shared_timed_mutex): Likewise for try_lock, try_lock_for,
try_lock_shared, try_lock_shared_for, try_lock_until, and
try_lock_shared_until member functions.
(shared_lock): Likewise for try_lock, try_lock, try_lock_for,
try_lock_until, owns_lock, and mutex member functions.
* testsuite/30_threads/recursive_timed_mutex/try_lock_until/clock_neg.cc:
Cast discarded value expression to void.
* testsuite/30_threads/shared_lock/locking/3.cc: Likewise.
* testsuite/30_threads/shared_lock/locking/4.cc: Likewise.
* testsuite/30_threads/shared_lock/locking/clock_neg.cc:
Likewise.
* testsuite/30_threads/shared_timed_mutex/try_lock_until/clock_neg.cc:
Likewise.
* testsuite/30_threads/timed_mutex/try_lock_until/clock_neg.cc:
Likewise.
* testsuite/30_threads/try_lock/4.cc: Likewise.
* testsuite/30_threads/unique_lock/cons/60497.cc: Likewise.
* testsuite/30_threads/unique_lock/locking/3.cc: Likewise.
* testsuite/30_threads/unique_lock/locking/clock_neg.cc:
Likewise.

14 files changed:
libstdc++-v3/include/bits/std_mutex.h
libstdc++-v3/include/bits/unique_lock.h
libstdc++-v3/include/std/mutex
libstdc++-v3/include/std/shared_mutex
libstdc++-v3/testsuite/30_threads/recursive_timed_mutex/try_lock_until/clock_neg.cc
libstdc++-v3/testsuite/30_threads/shared_lock/locking/3.cc
libstdc++-v3/testsuite/30_threads/shared_lock/locking/4.cc
libstdc++-v3/testsuite/30_threads/shared_lock/locking/clock_neg.cc
libstdc++-v3/testsuite/30_threads/shared_timed_mutex/try_lock_until/clock_neg.cc
libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/clock_neg.cc
libstdc++-v3/testsuite/30_threads/try_lock/4.cc
libstdc++-v3/testsuite/30_threads/unique_lock/cons/60497.cc
libstdc++-v3/testsuite/30_threads/unique_lock/locking/3.cc
libstdc++-v3/testsuite/30_threads/unique_lock/locking/clock_neg.cc

index b22e0e1..68f5fb9 100644 (file)
@@ -117,6 +117,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        __throw_system_error(__e);
     }
 
+    _GLIBCXX_NODISCARD
     bool
     try_lock() noexcept
     {
index 9ed7ba2..c6402d9 100644 (file)
@@ -143,6 +143,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
          }
       }
 
+      _GLIBCXX_NODISCARD
       bool
       try_lock()
       {
@@ -158,6 +159,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       }
 
       template<typename _Clock, typename _Duration>
+       _GLIBCXX_NODISCARD
        bool
        try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
        {
@@ -173,6 +175,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        }
 
       template<typename _Rep, typename _Period>
+       _GLIBCXX_NODISCARD
        bool
        try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
        {
@@ -215,6 +218,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        return __ret;
       }
 
+      _GLIBCXX_NODISCARD
       bool
       owns_lock() const noexcept
       { return _M_owns; }
@@ -222,6 +226,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       explicit operator bool() const noexcept
       { return owns_lock(); }
 
+      _GLIBCXX_NODISCARD
       mutex_type*
       mutex() const noexcept
       { return _M_device; }
index b310c15..aca5f91 100644 (file)
@@ -124,6 +124,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        __throw_system_error(__e);
     }
 
+    _GLIBCXX_NODISCARD
     bool
     try_lock() noexcept
     {
@@ -253,6 +254,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        __throw_system_error(__e);
     }
 
+    _GLIBCXX_NODISCARD
     bool
     try_lock() noexcept
     {
@@ -261,11 +263,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     }
 
     template <class _Rep, class _Period>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
       { return _M_try_lock_for(__rtime); }
 
     template <class _Clock, class _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
       { return _M_try_lock_until(__atime); }
@@ -328,6 +332,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        __throw_system_error(__e);
     }
 
+    _GLIBCXX_NODISCARD
     bool
     try_lock() noexcept
     {
@@ -336,11 +341,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     }
 
     template <class _Rep, class _Period>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
       { return _M_try_lock_for(__rtime); }
 
     template <class _Clock, class _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
       { return _M_try_lock_until(__atime); }
@@ -395,6 +402,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       _M_locked = true;
     }
 
+    _GLIBCXX_NODISCARD
     bool
     try_lock()
     {
@@ -406,6 +414,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     }
 
     template<typename _Rep, typename _Period>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
       {
@@ -417,6 +426,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       }
 
     template<typename _Clock, typename _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
       {
@@ -478,6 +488,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       ++_M_count;
     }
 
+    _GLIBCXX_NODISCARD
     bool
     try_lock()
     {
@@ -494,6 +505,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     }
 
     template<typename _Rep, typename _Period>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
       {
@@ -510,6 +522,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       }
 
     template<typename _Clock, typename _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
       {
@@ -616,6 +629,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
    *  Sequentially calls try_lock() on each argument.
    */
   template<typename _L1, typename _L2, typename... _L3>
+    _GLIBCXX_NODISCARD
     inline int
     try_lock(_L1& __l1, _L2& __l2, _L3&... __l3)
     {
index cf74b32..7b70697 100644 (file)
@@ -420,13 +420,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     // Exclusive ownership
 
     void lock() { _M_impl.lock(); }
-    bool try_lock() { return _M_impl.try_lock(); }
+    [[nodiscard]] bool try_lock() { return _M_impl.try_lock(); }
     void unlock() { _M_impl.unlock(); }
 
     // Shared ownership
 
     void lock_shared() { _M_impl.lock_shared(); }
-    bool try_lock_shared() { return _M_impl.try_lock_shared(); }
+    [[nodiscard]] bool try_lock_shared() { return _M_impl.try_lock_shared(); }
     void unlock_shared() { _M_impl.unlock_shared(); }
 
 #if _GLIBCXX_USE_PTHREAD_RWLOCK_T
@@ -473,10 +473,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     // Exclusive ownership
 
     void lock() { _Base::lock(); }
-    bool try_lock() { return _Base::try_lock(); }
+    _GLIBCXX_NODISCARD bool try_lock() { return _Base::try_lock(); }
     void unlock() { _Base::unlock(); }
 
     template<typename _Rep, typename _Period>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
       {
@@ -489,10 +490,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     // Shared ownership
 
     void lock_shared() { _Base::lock_shared(); }
+    _GLIBCXX_NODISCARD
     bool try_lock_shared() { return _Base::try_lock_shared(); }
     void unlock_shared() { _Base::unlock_shared(); }
 
     template<typename _Rep, typename _Period>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_shared_for(const chrono::duration<_Rep, _Period>& __rtime)
       {
@@ -507,6 +510,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     // Exclusive ownership
 
     template<typename _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_until(const chrono::time_point<chrono::system_clock,
                     _Duration>& __atime)
@@ -532,6 +536,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 #ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
     template<typename _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_until(const chrono::time_point<chrono::steady_clock,
                   _Duration>& __atime)
@@ -558,6 +563,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #endif
 
     template<typename _Clock, typename _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
       {
@@ -580,6 +586,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     // Shared ownership
 
     template<typename _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_shared_until(const chrono::time_point<chrono::system_clock,
                            _Duration>& __atime)
@@ -619,6 +626,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 #ifdef _GLIBCXX_USE_PTHREAD_RWLOCK_CLOCKLOCK
     template<typename _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_shared_until(const chrono::time_point<chrono::steady_clock,
                            _Duration>& __atime)
@@ -645,6 +653,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #endif
 
     template<typename _Clock, typename _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_shared_until(const chrono::time_point<_Clock,
                                                     _Duration>& __atime)
@@ -670,6 +679,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     // Exclusive ownership
 
     template<typename _Clock, typename _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
       {
@@ -694,6 +704,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     // Shared ownership
 
     template <typename _Clock, typename _Duration>
+      _GLIBCXX_NODISCARD
       bool
       try_lock_shared_until(const chrono::time_point<_Clock,
                                                     _Duration>& __abs_time)
@@ -776,6 +787,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        _M_owns = true;
       }
 
+      _GLIBCXX_NODISCARD
       bool
       try_lock()
       {
@@ -784,6 +796,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       }
 
       template<typename _Rep, typename _Period>
+       _GLIBCXX_NODISCARD
        bool
        try_lock_for(const chrono::duration<_Rep, _Period>& __rel_time)
        {
@@ -792,6 +805,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        }
 
       template<typename _Clock, typename _Duration>
+       _GLIBCXX_NODISCARD
        bool
        try_lock_until(const chrono::time_point<_Clock, _Duration>& __abs_time)
        {
@@ -826,10 +840,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       // Getters
 
+      _GLIBCXX_NODISCARD
       bool owns_lock() const noexcept { return _M_owns; }
 
       explicit operator bool() const noexcept { return _M_owns; }
 
+      _GLIBCXX_NODISCARD
       mutex_type* mutex() const noexcept { return _M_pm; }
 
     private:
index 541bd19..91f2c83 100644 (file)
@@ -33,7 +33,7 @@ void
 test01()
 {
   std::recursive_timed_mutex m;
-  m.try_lock_until(clok::now()); // { dg-error "here" }
+  (void) m.try_lock_until(clok::now()); // { dg-error "here" }
 }
 
 struct cloc
@@ -51,7 +51,7 @@ void
 test02()
 {
   std::recursive_timed_mutex m;
-  m.try_lock_until(cloc::now()); // { dg-error "here" }
+  (void) m.try_lock_until(cloc::now()); // { dg-error "here" }
 }
 
 // { dg-error "static assertion failed" "" { target *-*-* } 0 }
index c7637d2..8ad962b 100644 (file)
@@ -38,7 +38,7 @@ int main()
 
       try
        {
-         l.try_lock_for(std::chrono::milliseconds(100));
+         (void) l.try_lock_for(std::chrono::milliseconds(100));
        }
       catch(const std::system_error&)
        {
index 126098d..96388a1 100644 (file)
@@ -40,7 +40,7 @@ int main()
 
       try
        {
-         l.try_lock_until(t);
+         (void) l.try_lock_until(t);
        }
       catch(const std::system_error&)
        {
index 3a6373b..d6df66e 100644 (file)
@@ -34,7 +34,7 @@ test01()
 {
   std::shared_timed_mutex m;
   std::shared_lock<std::shared_timed_mutex> l(m, std::defer_lock);
-  l.try_lock_until(clok::now()); // { dg-error "here" }
+  (void) l.try_lock_until(clok::now()); // { dg-error "here" }
 }
 
 struct cloc
@@ -53,7 +53,7 @@ test02()
 {
   std::shared_timed_mutex m;
   std::shared_lock<std::shared_timed_mutex> l(m, std::defer_lock);
-  l.try_lock_until(cloc::now()); // { dg-error "here" }
+  (void) l.try_lock_until(cloc::now()); // { dg-error "here" }
 }
 
 // { dg-error "static assertion failed" "" { target *-*-* } 0 }
index ac40123..f948101 100644 (file)
@@ -33,7 +33,7 @@ void
 test01()
 {
   std::shared_timed_mutex m;
-  m.try_lock_until(clok::now()); // { dg-error "here" }
+  (void) m.try_lock_until(clok::now()); // { dg-error "here" }
 }
 
 struct cloc
@@ -51,7 +51,7 @@ void
 test02()
 {
   std::shared_timed_mutex m;
-  m.try_lock_shared_until(cloc::now()); // { dg-error "here" }
+  (void) m.try_lock_shared_until(cloc::now()); // { dg-error "here" }
 }
 
 // { dg-error "static assertion failed" "" { target *-*-* } 0 }
index 3642474..71c83c5 100644 (file)
@@ -33,7 +33,7 @@ void
 test01()
 {
   std::timed_mutex m;
-  m.try_lock_until(clok::now()); // { dg-error "here" }
+  (void) m.try_lock_until(clok::now()); // { dg-error "here" }
 }
 
 struct cloc
@@ -51,7 +51,7 @@ void
 test02()
 {
   std::timed_mutex m;
-  m.try_lock_until(cloc::now()); // { dg-error "here" }
+  (void) m.try_lock_until(cloc::now()); // { dg-error "here" }
 }
 
 // { dg-error "static assertion failed" "" { target *-*-* } 0 }
index cfb5a74..96afa55 100644 (file)
@@ -125,7 +125,7 @@ void test03()
         unreliable_lock::count = 0;
         try
           {
-            std::try_lock(l1, l2, l3);
+            (void) std::try_lock(l1, l2, l3);
             VERIFY( false );
           }
         catch (int e)
index c3cb621..a8a8db4 100644 (file)
@@ -50,5 +50,5 @@ void test02()
 void test03()
 {
   test_type l1, l2, l3;
-  std::try_lock(l1, l2, l3);
+  (void) std::try_lock(l1, l2, l3);
 }
index 3cb787b..ecc5b82 100644 (file)
@@ -38,7 +38,7 @@ int main()
 
       try
        {
-         l.try_lock_for(std::chrono::milliseconds(100));
+         (void) l.try_lock_for(std::chrono::milliseconds(100));
        }
       catch(const std::system_error&)
        {
index 68157f6..b633e5f 100644 (file)
@@ -34,7 +34,7 @@ test01()
 {
   std::timed_mutex m;
   std::unique_lock<std::timed_mutex> l(m, std::defer_lock);
-  l.try_lock_until(clok::now()); // { dg-error "here" }
+  (void) l.try_lock_until(clok::now()); // { dg-error "here" }
 }
 
 struct cloc
@@ -53,7 +53,7 @@ test02()
 {
   std::recursive_timed_mutex m;
   std::unique_lock<std::recursive_timed_mutex> l(m, std::defer_lock);
-  l.try_lock_until(cloc::now()); // { dg-error "here" }
+  (void) l.try_lock_until(cloc::now()); // { dg-error "here" }
 }
 
 // { dg-error "static assertion failed" "" { target *-*-* } 0 }