diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2023-08-17 18:58:24 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2023-11-11 00:41:08 +0000 |
commit | 64bcf3f07a211f2ea136d99b3700c172efc08d93 (patch) | |
tree | cb061d1af41e4353cc82f2a075eec6eae08ac8d9 /libstdc++-v3/include/std/mutex | |
parent | a92a434024c59f57dc24328d946f97a5e71cee94 (diff) | |
download | gcc-64bcf3f07a211f2ea136d99b3700c172efc08d93.zip gcc-64bcf3f07a211f2ea136d99b3700c172efc08d93.tar.gz gcc-64bcf3f07a211f2ea136d99b3700c172efc08d93.tar.bz2 |
libstdc++: Add [[nodiscard]] to lock types
Adding this attribute means users get a warning when they accidentally
create a temporary lock instead of creating an automatic variable with
block scope.
For std::lock_guard both constructors have side effects (they both take
a mutex and so both cause it to be unlocked at the end of the full
expression when a temporary is constructed). Ideally we would just put
the attribute on the class instead of the constructors, but that doesn't
work with GCC (PR c++/85973).
For std::unique_lock the default constructor and std::defer_lock_t
constructor do not cause any locking or unlocking, so do not need to
give a warning. It might still be a mistake to create a temporary using
those constructors, but it's harmless and seems unlikely anyway. For a
lock object created with one of those constructors you would expect the
lock object to be referred to later in the function, and that would not
even compile if it was constructed as an unnamed temporary.
std::scoped_lock gets the same treatment as std::lock_guard, except that
the explicit specialization for zero lockables has no side effects so
doesn't need to warn.
libstdc++-v3/ChangeLog:
* include/bits/std_mutex.h (lock_guard): Add [[nodiscard]]
attribute to constructors.
* include/bits/unique_lock.h (unique_lock): Likewise.
* include/std/mutex (scoped_lock, scoped_lock<Mutex>): Likewise.
* testsuite/30_threads/lock_guard/cons/nodiscard.cc: New test.
* testsuite/30_threads/scoped_lock/cons/nodiscard.cc: New test.
* testsuite/30_threads/unique_lock/cons/nodiscard.cc: New test.
Diffstat (limited to 'libstdc++-v3/include/std/mutex')
-rw-r--r-- | libstdc++-v3/include/std/mutex | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/libstdc++-v3/include/std/mutex b/libstdc++-v3/include/std/mutex index bd3a1cb..9d22ce8 100644 --- a/libstdc++-v3/include/std/mutex +++ b/libstdc++-v3/include/std/mutex @@ -744,9 +744,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION class scoped_lock { public: + + [[nodiscard]] explicit scoped_lock(_MutexTypes&... __m) : _M_devices(std::tie(__m...)) { std::lock(__m...); } + [[nodiscard]] explicit scoped_lock(adopt_lock_t, _MutexTypes&... __m) noexcept : _M_devices(std::tie(__m...)) { } // calling thread owns mutex @@ -779,9 +782,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION public: using mutex_type = _Mutex; + [[nodiscard]] explicit scoped_lock(mutex_type& __m) : _M_device(__m) { _M_device.lock(); } + [[nodiscard]] explicit scoped_lock(adopt_lock_t, mutex_type& __m) noexcept : _M_device(__m) { } // calling thread owns mutex |