1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // UNSUPPORTED: libcpp-has-no-threads
10 
11 // <mutex>
12 
13 // struct once_flag;
14 
15 // template<class Callable, class ...Args>
16 //   void call_once(once_flag& flag, Callable&& func, Args&&... args);
17 
18 // This test is supposed to be run with ThreadSanitizer and verifies that
19 // call_once properly synchronizes user state, a data race that was fixed
20 // in r280621.
21 
22 #include <mutex>
23 #include <thread>
24 #include <cassert>
25 
26 #include "test_macros.h"
27 
28 std::once_flag flg0;
29 long global = 0;
30 
31 void init0()
32 {
33     ++global;
34 }
35 
36 void f0()
37 {
38     std::call_once(flg0, init0);
39     assert(global == 1);
40 }
41 
42 int main(int, char**)
43 {
44     std::thread t0(f0);
45     std::thread t1(f0);
46     t0.join();
47     t1.join();
48     assert(global == 1);
49 
50   return 0;
51 }
52