• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "barrier.h"
18 
19 #include <string>
20 
21 #include "base/atomic.h"
22 #include "common_runtime_test.h"
23 #include "mirror/object_array-inl.h"
24 #include "thread-current-inl.h"
25 #include "thread_pool.h"
26 
27 namespace art {
28 class CheckWaitTask : public Task {
29  public:
CheckWaitTask(Barrier * barrier,AtomicInteger * count1,AtomicInteger * count2)30   CheckWaitTask(Barrier* barrier, AtomicInteger* count1, AtomicInteger* count2)
31       : barrier_(barrier),
32         count1_(count1),
33         count2_(count2) {}
34 
Run(Thread * self)35   void Run(Thread* self) override {
36     LOG(INFO) << "Before barrier" << *self;
37     ++*count1_;
38     barrier_->Wait(self);
39     ++*count2_;
40     LOG(INFO) << "After barrier" << *self;
41   }
42 
Finalize()43   void Finalize() override {
44     delete this;
45   }
46 
47  private:
48   Barrier* const barrier_;
49   AtomicInteger* const count1_;
50   AtomicInteger* const count2_;
51 };
52 
53 class BarrierTest : public CommonRuntimeTest {
54  public:
BarrierTest()55   BarrierTest() {
56     use_boot_image_ = true;  // Make the Runtime creation cheaper.
57   }
58 
59   static int32_t num_threads;
60 };
61 
62 int32_t BarrierTest::num_threads = 4;
63 
64 // Check that barrier wait and barrier increment work.
TEST_F(BarrierTest,CheckWait)65 TEST_F(BarrierTest, CheckWait) {
66   Thread* self = Thread::Current();
67   ThreadPool thread_pool("Barrier test thread pool", num_threads);
68   Barrier barrier(num_threads + 1);  // One extra Wait() in main thread.
69   Barrier timeout_barrier(0);  // Only used for sleeping on timeout.
70   AtomicInteger count1(0);
71   AtomicInteger count2(0);
72   for (int32_t i = 0; i < num_threads; ++i) {
73     thread_pool.AddTask(self, new CheckWaitTask(&barrier, &count1, &count2));
74   }
75   thread_pool.StartWorkers(self);
76   while (count1.load(std::memory_order_relaxed) != num_threads) {
77     timeout_barrier.Increment(self, 1, 100);  // sleep 100 msecs
78   }
79   // Count 2 should still be zero since no thread should have gone past the barrier.
80   EXPECT_EQ(0, count2.load(std::memory_order_relaxed));
81   // Perform one additional Wait(), allowing pool threads to proceed.
82   barrier.Wait(self);
83   // Wait for all the threads to finish.
84   thread_pool.Wait(self, true, false);
85   // Both counts should be equal to num_threads now.
86   EXPECT_EQ(count1.load(std::memory_order_relaxed), num_threads);
87   EXPECT_EQ(count2.load(std::memory_order_relaxed), num_threads);
88   timeout_barrier.Init(self, 0);  // Reset to zero for destruction.
89 }
90 
91 class CheckPassTask : public Task {
92  public:
CheckPassTask(Barrier * barrier,AtomicInteger * count,size_t subtasks)93   CheckPassTask(Barrier* barrier, AtomicInteger* count, size_t subtasks)
94       : barrier_(barrier),
95         count_(count),
96         subtasks_(subtasks) {}
97 
Run(Thread * self)98   void Run(Thread* self) override {
99     for (size_t i = 0; i < subtasks_; ++i) {
100       ++*count_;
101       // Pass through to next subtask.
102       barrier_->Pass(self);
103     }
104   }
105 
Finalize()106   void Finalize() override {
107     delete this;
108   }
109  private:
110   Barrier* const barrier_;
111   AtomicInteger* const count_;
112   const size_t subtasks_;
113 };
114 
115 // Check that barrier pass through works.
TEST_F(BarrierTest,CheckPass)116 TEST_F(BarrierTest, CheckPass) {
117   Thread* self = Thread::Current();
118   ThreadPool thread_pool("Barrier test thread pool", num_threads);
119   Barrier barrier(0);
120   AtomicInteger count(0);
121   const int32_t num_tasks = num_threads * 4;
122   const int32_t num_sub_tasks = 128;
123   for (int32_t i = 0; i < num_tasks; ++i) {
124     thread_pool.AddTask(self, new CheckPassTask(&barrier, &count, num_sub_tasks));
125   }
126   thread_pool.StartWorkers(self);
127   const int32_t expected_total_tasks = num_sub_tasks * num_tasks;
128   // Wait for all the tasks to complete using the barrier.
129   barrier.Increment(self, expected_total_tasks);
130   // The total number of completed tasks should be equal to expected_total_tasks.
131   EXPECT_EQ(count.load(std::memory_order_relaxed), expected_total_tasks);
132 }
133 
134 }  // namespace art
135