File: ttaskqueue.cpp

package info (click to toggle)
wsclean 3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,968 kB
  • sloc: cpp: 85,742; python: 3,526; sh: 245; makefile: 21
file content (293 lines) | stat: -rw-r--r-- 9,477 bytes parent folder | download | duplicates (2)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <aocommon/taskqueue.h>

#include <atomic>
#include <numeric>
#include <random>
#include <thread>
#include <vector>

#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>

using aocommon::TaskQueue;

BOOST_AUTO_TEST_SUITE(task_queue)

BOOST_DATA_TEST_CASE(single_thread, boost::unit_test::data::make({false, true}),
                     use_wait) {
  const std::vector<int> kValues{42, 43, 44, 45};
  constexpr int kDummyValue = 142;

  // Using a unique pointer ensures that TaskQueue cannot copy tasks.
  TaskQueue<std::unique_ptr<int>> queue;
  for (const int& value : kValues) {
    queue.Emplace(std::make_unique<int>(value));
  }

  for (const int& value : kValues) {
    std::unique_ptr<int> popped;
    BOOST_TEST(queue.Pop(popped));
    BOOST_REQUIRE(popped);
    BOOST_TEST(*popped == value);
  }

  if (use_wait) queue.WaitForIdle(0);

  queue.Finish();
  auto dummy = std::make_unique<int>(kDummyValue);
  BOOST_TEST(!queue.Pop(dummy));
  BOOST_REQUIRE(dummy);
  BOOST_TEST(*dummy == kDummyValue);
}

BOOST_DATA_TEST_CASE(multiple_threads_pop,
                     boost::unit_test::data::make({false, true}), use_wait) {
  const std::vector<int> kValues{42, 43, 44, 45};
  const size_t kLimit = 2;
  TaskQueue<int> queue{kLimit};
  std::mutex mutex;
  std::condition_variable notify;
  int popped_in_thread = 0;

  std::vector<std::thread> pop_threads;
  for (size_t i = 0; i < kValues.size(); ++i) {
    pop_threads.emplace_back([&] {
      int popped = 0;
      const bool result = queue.Pop(popped);
      std::lock_guard<std::mutex> lock(mutex);
      BOOST_TEST_REQUIRE(result);
      popped_in_thread = popped;
      notify.notify_one();
    });
  }

  if (use_wait) queue.WaitForIdle(pop_threads.size());

  for (const int& value : kValues) {
    popped_in_thread = 0;
    queue.Emplace(value);
    std::unique_lock<std::mutex> lock(mutex);
    while (popped_in_thread == 0) notify.wait(lock);
    BOOST_TEST(popped_in_thread == value);
  }

  for (std::thread& thread : pop_threads) thread.join();

  if (use_wait) queue.WaitForIdle(0);
}

// This test is not 100% deterministic, it is possible (though unlikely) that
// the threads of the 'Pop' for loop execute and finish before the threads of
// the 'TryPop' for loop ever get a chance to run. If this occurs the test may
// fail despite there being no actual code error. To avoid this each worker
// thread calls sleep with a small random interval, to allow the OS an
// opportunity to context switch to other threads. This should ensure that all
// threads in this test get a chance to execute and should avoid the failure
// described above. A random instead of fixed interval is used to better stress
// test the concurrency of the data structure by having different threads wake
// up/operate in different order.
//
// If you are reading this the test may have failed intermittently despite these
// precautions. If so it might become necessary to simplify or rework this test
// in other ways to try and avoid intermittent failures.
BOOST_AUTO_TEST_CASE(multiple_threads_try_pop) {
  std::random_device random_seed;
  std::mt19937 random_generator(random_seed());
  std::uniform_int_distribution<> random_number(1, 5);

  std::vector<int> values(10240);
  std::iota(values.begin(), values.end(), 0);

  TaskQueue<int> queue;
  for (const int& value : values) {
    queue.Emplace(value);
  }

  // Test that we can get tasks via 'Pop' and 'TryPop' concurrently without
  // issue.
  std::vector<std::thread> pop_threads;
  std::atomic<size_t> n_popped = 0;
  for (size_t i = 0; i < 3; ++i) {
    pop_threads.emplace_back([&] {
      int popped_value = 0;
      while (queue.Pop(popped_value)) {
        n_popped++;
        // Prevent thread starvation.
        // See comment at top of test for further explanation.
        std::this_thread::sleep_for(
            std::chrono::milliseconds(random_number(random_seed)));
      }
    });
  }
  std::atomic<size_t> n_try_popped = 0;
  for (size_t i = 0; i < 3; ++i) {
    pop_threads.emplace_back([&] {
      int popped_value;
      while (queue.TryPop(popped_value)) {
        n_try_popped++;
        // Prevent thread starvation.
        // See comment at top of test for further explanation.
        std::this_thread::sleep_for(
            std::chrono::milliseconds(random_number(random_seed)));
      }
      BOOST_CHECK(!queue.Pop(popped_value));
    });
  }
  queue.WaitForIdle(pop_threads.size());

  BOOST_CHECK_GT(n_popped, 0);
  BOOST_CHECK_GT(n_try_popped, 0);
  BOOST_CHECK_EQUAL(n_popped + n_try_popped, values.size());

  queue.Finish();
  for (std::thread& thread : pop_threads) thread.join();
}

// This test is not 100% deterministic, it is possible (though unlikely) that
// the threads of the 'Pop' for loop execute and finish before the threads of
// the 'TryPopN' for loop ever get a chance to run. See comment at top of
// 'multiple_threads_try_pop' test for further explanation.
BOOST_AUTO_TEST_CASE(multiple_threads_try_pop_n) {
  std::random_device random_seed;
  std::mt19937 random_generator(random_seed());
  std::uniform_int_distribution<> random_number(1, 5);

  std::vector<int> values(10240);
  std::iota(values.begin(), values.end(), 0);

  TaskQueue<int> queue;
  for (const int& value : values) {
    queue.Emplace(value);
  }

  // Test that we can get tasks via 'Pop' and 'TryPop' concurrently without
  // issue.
  // Also that TryPopN pops the correct amount of tasks and that they are
  // ordered.
  std::vector<std::thread> pop_threads;
  std::atomic<size_t> n_popped = 0;
  for (size_t i = 0; i < 4; ++i) {
    pop_threads.emplace_back([&] {
      int popped_value;
      while (queue.Pop(popped_value)) {
        n_popped++;
        // Prevent thread starvation.
        // See comment at top of test for further explanation.
        std::this_thread::sleep_for(
            std::chrono::milliseconds(random_number(random_seed)));
      }
    });
  }
  std::atomic<size_t> n_try_popped = 0;
  std::atomic<size_t> n_additional_popped = 0;
  for (size_t i = 0; i < 3; ++i) {
    pop_threads.emplace_back([&] {
      std::vector<int> popped_values;
      size_t read_size = random_number(random_generator);
      while (queue.TryPopN(popped_values, read_size)) {
        BOOST_CHECK_EQUAL(popped_values.size(), read_size);
        std::vector<int> expected_values(read_size);
        std::iota(expected_values.begin(), expected_values.end(),
                  popped_values[0]);
        BOOST_CHECK(popped_values == expected_values);
        n_try_popped += read_size;
        read_size = random_number(random_generator);
        // Prevent thread starvation.
        // See comment at top of test for further explanation.
        std::this_thread::sleep_for(
            std::chrono::milliseconds(random_number(random_seed)));
      }
      int popped_value = 0;
      // Note that while it might seem like this call can be removed to simplify
      // the test, it is mandatory that all worker threads end up blocking
      // inside a call to `Pop` otherwise `WaitForIdle` will not function
      // correctly.
      while (queue.Pop(popped_value)) {
        n_additional_popped++;
      }
    });
  }
  queue.WaitForIdle(pop_threads.size());

  BOOST_CHECK_GT(n_popped, 0);
  BOOST_CHECK_GT(n_try_popped, 0);
  BOOST_CHECK_EQUAL(n_popped + n_additional_popped + n_try_popped,
                    values.size());

  // Test that read past end of available tasks returns false.
  {
    int popped_value = 0;
    BOOST_CHECK(!queue.TryPop(popped_value));
  }
  {
    std::vector<int> popped_values;
    BOOST_CHECK(!queue.TryPopN(popped_values, 1));
    BOOST_CHECK(!queue.TryPopN(popped_values, 2));
    BOOST_CHECK(!queue.TryPopN(popped_values, 10));
  }

  queue.Finish();
  for (std::thread& thread : pop_threads) thread.join();
}

BOOST_AUTO_TEST_CASE(multiple_threads_done) {
  constexpr size_t kNThreads = 42;
  constexpr int kDummyValue = 142;
  TaskQueue<int> queue;

  std::mutex mutex;
  std::vector<std::thread> threads;
  for (size_t i = 0; i < kNThreads; ++i) {
    threads.emplace_back([&] {
      int dummy = kDummyValue;
      std::lock_guard<std::mutex> lock(mutex);
      BOOST_TEST(!queue.Pop(dummy));
      BOOST_TEST(dummy == kDummyValue);
    });
  }

  queue.Finish();

  // Joining the threads also tests that all threads are done.
  for (std::thread& thread : threads) thread.join();
}

BOOST_AUTO_TEST_CASE(wait_for_idle) {
  // Test that WaitForIdle really waits until kNThreads call Pop().
  const size_t kNThreads = 42;

  TaskQueue<int> queue;

  std::atomic<bool> waiting = false;
  std::atomic<bool> done_waiting = false;
  std::thread wait_thread([&] {
    waiting = true;
    queue.WaitForIdle(kNThreads);
    done_waiting = true;
  });
  // Wait until wait_thread starts waiting.
  while (!waiting) std::this_thread::yield();

  std::vector<std::thread> pop_threads;
  for (size_t i = 0; i < kNThreads; ++i) {
    BOOST_TEST(waiting);
    BOOST_TEST(!done_waiting);
    std::atomic<bool> popping = false;
    pop_threads.emplace_back([&] {
      popping = true;
      int dummy;
      queue.Pop(dummy);
    });
    // Wait until the thread starts popping.
    while (!popping) std::this_thread::yield();
  }

  // Wait until wait_thread stops waiting.
  while (!done_waiting) std::this_thread::yield();

  wait_thread.join();
  queue.Finish();
  for (std::thread& pop_thread : pop_threads) pop_thread.join();
}

BOOST_AUTO_TEST_SUITE_END()