[libcxx] r281477 - [libcxx] Add a TSan regression test for a data race in call_once

Kuba Brecka via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 14 07:15:42 PDT 2016


Author: kuba.brecka
Date: Wed Sep 14 09:15:42 2016
New Revision: 281477

URL: http://llvm.org/viewvc/llvm-project?rev=281477&view=rev
Log:
[libcxx] Add a TSan regression test for a data race in call_once

Differential Revision: https://reviews.llvm.org/D24297


Added:
    libcxx/trunk/test/std/thread/thread.mutex/thread.once/thread.once.callonce/race.pass.cpp

Added: libcxx/trunk/test/std/thread/thread.mutex/thread.once/thread.once.callonce/race.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/thread/thread.mutex/thread.once/thread.once.callonce/race.pass.cpp?rev=281477&view=auto
==============================================================================
--- libcxx/trunk/test/std/thread/thread.mutex/thread.once/thread.once.callonce/race.pass.cpp (added)
+++ libcxx/trunk/test/std/thread/thread.mutex/thread.once/thread.once.callonce/race.pass.cpp Wed Sep 14 09:15:42 2016
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// UNSUPPORTED: libcpp-has-no-threads
+
+// <mutex>
+
+// struct once_flag;
+
+// template<class Callable, class ...Args>
+//   void call_once(once_flag& flag, Callable&& func, Args&&... args);
+
+// This test is supposed to be run with ThreadSanitizer and verifies that
+// call_once properly synchronizes user state, a data race that was fixed
+// in r280621.
+
+#include <mutex>
+#include <thread>
+#include <cassert>
+
+std::once_flag flg0;
+long global = 0;
+
+void init0()
+{
+    ++global;
+}
+
+void f0()
+{
+    std::call_once(flg0, init0);
+    assert(global == 1);
+}
+
+int main()
+{
+    std::thread t0(f0);
+    std::thread t1(f0);
+    t0.join();
+    t1.join();
+    assert(global == 1);
+}




More information about the cfe-commits mailing list