From d6efbcb0b515eff5228ccd06c7fb137fd4e0a1be Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Wed, 15 Jan 2020 19:23:02 -0800 Subject: [PATCH] drd/tests/std_mutex: Add a unit test for std::mutex --- .gitignore | 1 + drd/tests/Makefile.am | 6 ++++++ drd/tests/std_mutex.cpp | 39 +++++++++++++++++++++++++++++++++++++++ drd/tests/std_mutex.stderr.exp | 4 ++++ drd/tests/std_mutex.vgtest | 4 ++++ 5 files changed, 54 insertions(+) create mode 100644 drd/tests/std_mutex.cpp create mode 100644 drd/tests/std_mutex.stderr.exp create mode 100644 drd/tests/std_mutex.vgtest diff --git a/.gitignore b/.gitignore index 58dfee04f..8e5f0a666 100644 --- a/.gitignore +++ b/.gitignore @@ -405,6 +405,7 @@ /drd/tests/sigalrm /drd/tests/std_atomic /drd/tests/std_list +/drd/tests/std_mutex /drd/tests/std_string /drd/tests/std_thread /drd/tests/std_thread2 diff --git a/drd/tests/Makefile.am b/drd/tests/Makefile.am index a0da60d0e..20c72c243 100644 --- a/drd/tests/Makefile.am +++ b/drd/tests/Makefile.am @@ -274,6 +274,8 @@ EXTRA_DIST = \ std_atomic.vgtest \ std_list.stderr.exp \ std_list.vgtest \ + std_mutex.stderr.exp \ + std_mutex.vgtest \ std_string.stderr.exp \ std_string.vgtest \ std_thread.stderr.exp \ @@ -434,6 +436,7 @@ if HAVE_SHARED_POINTER_ANNOTATION check_PROGRAMS += \ std_atomic \ std_list \ + std_mutex \ std_string \ std_thread \ std_thread2 @@ -559,6 +562,9 @@ std_atomic_CXXFLAGS = $(AM_CXXFLAGS) -std=c++0x -Wno-sign-compare std_list_SOURCES = std_list.cpp std_list_CXXFLAGS = $(AM_CXXFLAGS) -std=c++0x -Wno-sign-compare +std_mutex_SOURCES = std_mutex.cpp +std_mutex_CXXFLAGS = $(AM_CXXFLAGS) -std=c++0x -Wno-sign-compare + std_string_SOURCES = std_string.cpp std_string_CXXFLAGS = $(AM_CXXFLAGS) -std=c++0x -Wno-sign-compare diff --git a/drd/tests/std_mutex.cpp b/drd/tests/std_mutex.cpp new file mode 100644 index 000000000..7c3333eb1 --- /dev/null +++ b/drd/tests/std_mutex.cpp @@ -0,0 +1,39 @@ +// See also https://bugs.kde.org/show_bug.cgi?id=416286 + +#include +#include +#include +#include + +class counter { +public: + counter(): mutex() {} + void get() { std::unique_lock lock(mutex); } + +private: + std::mutex mutex; +}; + +static counter& get_counter() +{ + static counter manager; + return manager; +} + +static void do_work() +{ + get_counter().get(); +} + +int main() +{ + std::vector v; + + for (int i = 0; i < 16; i++) + v.emplace_back([]{ do_work(); }); + + for (auto& t : v) + t.join(); + + std::cerr << "Done.\n"; +} diff --git a/drd/tests/std_mutex.stderr.exp b/drd/tests/std_mutex.stderr.exp new file mode 100644 index 000000000..d16127fb5 --- /dev/null +++ b/drd/tests/std_mutex.stderr.exp @@ -0,0 +1,4 @@ + +Done. + +ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) diff --git a/drd/tests/std_mutex.vgtest b/drd/tests/std_mutex.vgtest new file mode 100644 index 000000000..c592eeadc --- /dev/null +++ b/drd/tests/std_mutex.vgtest @@ -0,0 +1,4 @@ +prereq: test -e std_mutex && ./supported_libpthread +vgopts: --check-stack-var=yes --show-confl-seg=no +prog: std_mutex +stderr_filter: filter_stderr -- 2.11.4.GIT