1 //========- unittests/Support/TaskQueue.cpp - TaskQueue.h tests ------========//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Config/llvm-config.h"
11 #if LLVM_ENABLE_THREADS
13 #include "llvm/Support/TaskQueue.h"
15 #include "gtest/gtest.h"
19 class TaskQueueTest
: public testing::Test
{
24 TEST_F(TaskQueueTest
, OrderedFutures
) {
27 std::atomic
<int> X
{ 0 };
28 std::atomic
<int> Y
{ 0 };
29 std::atomic
<int> Z
{ 0 };
31 std::mutex M1
, M2
, M3
;
32 std::unique_lock
<std::mutex
> L1(M1
);
33 std::unique_lock
<std::mutex
> L2(M2
);
34 std::unique_lock
<std::mutex
> L3(M3
);
36 std::future
<void> F1
= TQ
.async([&] {
37 std::unique_lock
<std::mutex
> Lock(M1
);
40 std::future
<void> F2
= TQ
.async([&] {
41 std::unique_lock
<std::mutex
> Lock(M2
);
44 std::future
<void> F3
= TQ
.async([&] {
45 std::unique_lock
<std::mutex
> Lock(M3
);
68 TEST_F(TaskQueueTest
, UnOrderedFutures
) {
71 std::atomic
<int> X
{ 0 };
72 std::atomic
<int> Y
{ 0 };
73 std::atomic
<int> Z
{ 0 };
76 std::unique_lock
<std::mutex
> Lock(M
);
78 std::future
<void> F1
= TQ
.async([&] { ++X
; });
79 std::future
<void> F2
= TQ
.async([&] { ++Y
; });
80 std::future
<void> F3
= TQ
.async([&M
, &Z
] {
81 std::unique_lock
<std::mutex
> Lock(M
);
98 TEST_F(TaskQueueTest
, FutureWithReturnValue
) {
101 std::future
<std::string
> F1
= TQ
.async([&] { return std::string("Hello"); });
102 std::future
<int> F2
= TQ
.async([&] { return 42; });
104 ASSERT_EQ(42, F2
.get());
105 ASSERT_EQ("Hello", F1
.get());