| 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: 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 "make_test_thread.h" |
| 27 | #include "test_macros.h" |
| 28 | |
| 29 | std::once_flag flg0; |
| 30 | long global = 0; |
| 31 | |
| 32 | void init0() |
| 33 | { |
| 34 | ++global; |
| 35 | } |
| 36 | |
| 37 | void f0() |
| 38 | { |
| 39 | std::call_once(once&: flg0, f&: init0); |
| 40 | assert(global == 1); |
| 41 | } |
| 42 | |
| 43 | int main(int, char**) |
| 44 | { |
| 45 | std::thread t0 = support::make_test_thread(f0); |
| 46 | std::thread t1 = support::make_test_thread(f0); |
| 47 | t0.join(); |
| 48 | t1.join(); |
| 49 | assert(global == 1); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |