From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2062) id 0C2553858023; Tue, 22 Jun 2021 18:08:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0C2553858023 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Thomas Rodgers To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-8638] libstdc++: Fix for deadlock in std::counting_semaphore [PR100806] X-Act-Checkin: gcc X-Git-Author: Thomas Rodgers X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: d4175a5d9bb8fe0a62baeff748a18efe884c97c6 X-Git-Newrev: c24384efbbaeeca3dbc53e7d1226a57bc16e9ad5 Message-Id: <20210622180814.0C2553858023@sourceware.org> Date: Tue, 22 Jun 2021 18:08:14 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Jun 2021 18:08:14 -0000 https://gcc.gnu.org/g:c24384efbbaeeca3dbc53e7d1226a57bc16e9ad5 commit r11-8638-gc24384efbbaeeca3dbc53e7d1226a57bc16e9ad5 Author: Thomas Rodgers Date: Tue Jun 22 10:59:07 2021 -0700 libstdc++: Fix for deadlock in std::counting_semaphore [PR100806] libstdc++-v3/ChangeLog: PR libstdc++/100806 * include/bits/semaphore_base.h (__atomic_semaphore::_M_release): Force _M_release() to wake all waiting threads. * testsuite/30_threads/semaphore/100806.cc: New test. (cherry picked from commit e02840c1a92abecd211ffaf05b28329bcb534583) Diff: --- libstdc++-v3/include/bits/semaphore_base.h | 4 +- .../testsuite/30_threads/semaphore/100806.cc | 57 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/bits/semaphore_base.h b/libstdc++-v3/include/bits/semaphore_base.h index 4948f0fd0bc..c970fd66a36 100644 --- a/libstdc++-v3/include/bits/semaphore_base.h +++ b/libstdc++-v3/include/bits/semaphore_base.h @@ -254,7 +254,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__update > 1) __atomic_notify_address_bare(&_M_counter, true); else - __atomic_notify_address_bare(&_M_counter, false); + __atomic_notify_address_bare(&_M_counter, true); +// FIXME - Figure out why this does not wake a waiting thread +// __atomic_notify_address_bare(&_M_counter, false); } private: diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc new file mode 100644 index 00000000000..2fa2628a278 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc @@ -0,0 +1,57 @@ +// { dg-options "-std=gnu++2a -pthread" } +// { dg-do run { target c++2a } } +// { dg-require-effective-target pthread } +// { dg-require-gthreads "" } +// { dg-add-options libatomic } + +#include +#include + +#include +#include +#include +#include +#include + +std::counting_semaphore<4> semaphore{6}; + +std::mutex mtx; +std::vector results; + +void thread_main(size_t x) +{ + semaphore.acquire(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + semaphore.release(); + { + std::ostringstream stm; + stm << "Thread " << x << " finished."; + std::lock_guard g{ mtx }; + results.push_back(stm.str()); + } +} + +int main() +{ + constexpr auto nthreads = 10; + + std::vector threads(nthreads); + + size_t counter{0}; + for(auto& t : threads) + { + t = std::thread(thread_main, counter++); + } + + for(auto& t : threads) + { + t.join(); + { + std::lock_guard g{ mtx }; + for (auto&& r : results) + std::cout << r << '\n'; + std::cout.flush(); + results.clear(); + } + } +}