Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "core/platform/Barrier.h"
#include "core/platform/threadpool.h"
 
#include "gtest/gtest.h"
 
#include <atomic>
#include <thread>
 
namespace {
 
static void TestBarrier(int num_threads, uint64_t per_thread_count, bool spin) {
  std::atomic<uint64_t> counter{0};
  onnxruntime::Barrier barrier(num_threads, spin);
 
  std::vector<std::thread> threads;
  for (auto i = 0; i < num_threads + 1; i++) {
    threads.push_back(std::thread([&, i] {
      if (i > 0) {
        // Worker thread; increment the shared counter then
        // notify the barrier.
        for (uint64_t j = 0; j < per_thread_count; j++) {
          counter++;
        }
        barrier.Notify();
      } else {
        // Main thread; wait on the barrier, and then check the count seen.
        barrier.Wait();
        ASSERT_EQ(counter, per_thread_count * num_threads);
      }
    }));
  }
 
  // Wait for the threads to finish
  for (auto& t : threads) {
    t.join();
  }
}
 
}  // namespace
 
namespace onnxruntime {
 
constexpr uint64_t count = 1000000ull;
 
TEST(BarrierTest, TestBarrier_0Workers_Spin) {
  TestBarrier(0, count, true);
}
 
TEST(BarrierTest, TestBarrier_0Workers_Block) {
  TestBarrier(0, count, false);
}
 
TEST(BarrierTest, TestBarrier_1Worker_Spin) {
  TestBarrier(1, count, true);
}
 
TEST(BarrierTest, TestBarrier_1Worker_Block) {
  TestBarrier(1, count, false);
}
 
TEST(BarrierTest, TestBarrier_4Workers_Spin) {
  TestBarrier(4, count, true);
}
 
TEST(BarrierTest, TestBarrier_4Workers_Block) {
  TestBarrier(4, count, false);
}
 
}  // namespace onnxruntime