1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
//
// WaitingTaskList_test.cpp
// DispatchProcessingDemo
//
// Created by Chris Jones on 9/27/11.
//
#include <iostream>
#include <catch.hpp>
#include <chrono>
#include <memory>
#include <atomic>
#include <thread>
#include "oneapi/tbb/task.h"
#include "FWCore/Concurrency/interface/WaitingTaskList.h"
#include "FWCore/Concurrency/interface/FinalWaitingTask.h"
namespace {
class TestCalledTask : public edm::WaitingTask {
public:
TestCalledTask(std::atomic<bool>& iCalled, std::exception_ptr& iPtr) : m_called(iCalled), m_ptr(iPtr) {}
void execute() final {
if (exceptionPtr()) {
m_ptr = exceptionPtr();
}
m_called = true;
return;
}
private:
std::atomic<bool>& m_called;
std::exception_ptr& m_ptr;
};
class TestValueSetTask : public edm::WaitingTask {
public:
TestValueSetTask(std::atomic<bool>& iValue) : m_value(iValue) {}
void execute() final {
REQUIRE(m_value);
return;
}
private:
std::atomic<bool>& m_value;
};
void join_thread(std::thread* iThread) {
if (iThread->joinable()) {
iThread->join();
}
}
} // namespace
using namespace std::chrono_literals;
TEST_CASE("WaitingTaskList", "[WaitingTaskList]") {
SECTION("add then done") {
std::atomic<bool> called{false};
edm::WaitingTaskList waitList;
{
std::exception_ptr excPtr;
auto t = new TestCalledTask{called, excPtr};
oneapi::tbb::task_group group;
waitList.add(edm::WaitingTaskHolder(group, t));
std::this_thread::sleep_for(10us);
REQUIRE_FALSE(called);
waitList.doneWaiting(std::exception_ptr{});
group.wait();
REQUIRE(called);
REQUIRE_FALSE(bool(excPtr));
}
waitList.reset();
called = false;
{
std::exception_ptr excPtr;
auto t = new TestCalledTask{called, excPtr};
oneapi::tbb::task_group group;
waitList.add(edm::WaitingTaskHolder(group, t));
std::this_thread::sleep_for(10us);
REQUIRE_FALSE(called);
waitList.doneWaiting(std::exception_ptr{});
group.wait();
REQUIRE(called);
REQUIRE_FALSE(bool(excPtr));
}
}
SECTION("done then add") {
std::atomic<bool> called{false};
std::exception_ptr excPtr;
edm::WaitingTaskList waitList;
{
oneapi::tbb::task_group group;
auto t = new TestCalledTask{called, excPtr};
waitList.doneWaiting(std::exception_ptr{});
waitList.add(edm::WaitingTaskHolder(group, t));
group.wait();
REQUIRE(called);
REQUIRE_FALSE(bool(excPtr));
}
}
SECTION("add then done failed") {
std::atomic<bool> called{false};
edm::WaitingTaskList waitList;
{
std::exception_ptr excPtr;
auto t = new TestCalledTask{called, excPtr};
oneapi::tbb::task_group group;
waitList.add(edm::WaitingTaskHolder(group, t));
std::this_thread::sleep_for(10us);
REQUIRE_FALSE(called);
waitList.doneWaiting(std::make_exception_ptr(std::string("failed")));
group.wait();
REQUIRE(called);
REQUIRE(bool(excPtr));
}
}
SECTION("done then add failed") {
std::atomic<bool> called{false};
std::exception_ptr excPtr;
edm::WaitingTaskList waitList;
{
auto t = new TestCalledTask{called, excPtr};
waitList.doneWaiting(std::make_exception_ptr(std::string("failed")));
oneapi::tbb::task_group group;
waitList.add(edm::WaitingTaskHolder(group, t));
group.wait();
REQUIRE(called);
REQUIRE(bool(excPtr));
}
}
SECTION("stress Test") {
edm::WaitingTaskList waitList;
oneapi::tbb::task_group group;
unsigned int index = 1000;
const unsigned int nTasks = 10000;
while (0 != --index) {
edm::FinalWaitingTask waitTask{group};
auto* pWaitTask = &waitTask;
{
edm::WaitingTaskHolder waitTaskH(group, pWaitTask);
std::thread makeTasksThread([&waitList, waitTaskH] {
for (unsigned int i = 0; i < nTasks; ++i) {
waitList.add(waitTaskH);
}
});
std::shared_ptr<std::thread>(&makeTasksThread, join_thread);
std::thread doneWaitThread([&waitList, waitTaskH] { waitList.doneWaiting(std::exception_ptr{}); });
std::shared_ptr<std::thread>(&doneWaitThread, join_thread);
}
waitTask.wait();
}
}
}
|