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 HIDDEN {
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   std::unique_ptr<ThreadPool> thread_pool(
68       ThreadPool::Create("Barrier test thread pool", num_threads));
69   Barrier barrier(num_threads + 1);  // One extra Wait() in main thread.
70   Barrier timeout_barrier(0);  // Only used for sleeping on timeout.
71   AtomicInteger count1(0);
72   AtomicInteger count2(0);
73   for (int32_t i = 0; i < num_threads; ++i) {
74     thread_pool->AddTask(self, new CheckWaitTask(&barrier, &count1, &count2));
75   }
76   thread_pool->StartWorkers(self);
77   while (count1.load(std::memory_order_relaxed) != num_threads) {
78     timeout_barrier.Increment(self, 1, 100);  // sleep 100 msecs
79   }
80   // Count 2 should still be zero since no thread should have gone past the barrier.
81   EXPECT_EQ(0, count2.load(std::memory_order_relaxed));
82   // Perform one additional Wait(), allowing pool threads to proceed.
83   barrier.Wait(self);
84   // Wait for all the threads to finish.
85   thread_pool->Wait(self, true, false);
86   // Both counts should be equal to num_threads now.
87   EXPECT_EQ(count1.load(std::memory_order_relaxed), num_threads);
88   EXPECT_EQ(count2.load(std::memory_order_relaxed), num_threads);
89   timeout_barrier.Init(self, 0);  // Reset to zero for destruction.
90 }
91 
92 class CheckPassTask : public Task {
93  public:
CheckPassTask(Barrier * barrier,AtomicInteger * count,size_t subtasks)94   CheckPassTask(Barrier* barrier, AtomicInteger* count, size_t subtasks)
95       : barrier_(barrier),
96         count_(count),
97         subtasks_(subtasks) {}
98 
Run(Thread * self)99   void Run(Thread* self) override {
100     for (size_t i = 0; i < subtasks_; ++i) {
101       ++*count_;
102       // Pass through to next subtask.
103       barrier_->Pass(self);
104     }
105   }
106 
Finalize()107   void Finalize() override {
108     delete this;
109   }
110  private:
111   Barrier* const barrier_;
112   AtomicInteger* const count_;
113   const size_t subtasks_;
114 };
115 
116 // Check that barrier pass through works.
TEST_F(BarrierTest,CheckPass)117 TEST_F(BarrierTest, CheckPass) {
118   Thread* self = Thread::Current();
119   std::unique_ptr<ThreadPool> thread_pool(
120       ThreadPool::Create("Barrier test thread pool", num_threads));
121   Barrier barrier(0);
122   AtomicInteger count(0);
123   const int32_t num_tasks = num_threads * 4;
124   const int32_t num_sub_tasks = 128;
125   for (int32_t i = 0; i < num_tasks; ++i) {
126     thread_pool->AddTask(self, new CheckPassTask(&barrier, &count, num_sub_tasks));
127   }
128   thread_pool->StartWorkers(self);
129   const int32_t expected_total_tasks = num_sub_tasks * num_tasks;
130   // Wait for all the tasks to complete using the barrier.
131   barrier.Increment(self, expected_total_tasks);
132   // The total number of completed tasks should be equal to expected_total_tasks.
133   EXPECT_EQ(count.load(std::memory_order_relaxed), expected_total_tasks);
134 }
135 
136 }  // namespace art
137