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 // UNSUPPORTED: c++03
11
12 // This test requires the fix for http://llvm.org/PR38682 (616ef1863fae). We mark the
13 // test as UNSUPPORTED instead of XFAIL because the test doesn't fail consistently.
14 // UNSUPPORTED: using-built-library-before-llvm-10
15
16 // This test is designed to cause and allow TSAN to detect a race condition
17 // in std::async, as reported in https://llvm.org/PR38682.
18
19 #include <cassert>
20 #include <functional>
21 #include <future>
22 #include <numeric>
23 #include <vector>
24
25 #include "test_macros.h"
26
27
worker(std::vector<int> const & data)28 static int worker(std::vector<int> const& data) {
29 return std::accumulate(data.begin(), data.end(), 0);
30 }
31
worker_ref(int & i)32 static int& worker_ref(int& i) { return i; }
33
worker_void()34 static void worker_void() { }
35
main(int,char **)36 int main(int, char**) {
37 // future<T>
38 {
39 std::vector<int> const v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
40 for (int i = 0; i != 20; ++i) {
41 std::future<int> fut = std::async(std::launch::async, worker, v);
42 int answer = fut.get();
43 assert(answer == 55);
44 }
45 }
46
47 // future<T&>
48 {
49 for (int i = 0; i != 20; ++i) {
50 std::future<int&> fut = std::async(std::launch::async, worker_ref, std::ref(i));
51 int& answer = fut.get();
52 assert(answer == i);
53 }
54 }
55
56 // future<void>
57 {
58 for (int i = 0; i != 20; ++i) {
59 std::future<void> fut = std::async(std::launch::async, worker_void);
60 fut.get();
61 }
62 }
63
64 return 0;
65 }
66