blob: 093ba3535abfa5629c1d6fb8879101b5e6bafb65 [file] [log] [blame]
Mathieu Chartier0e4627e2012-10-23 16:13:36 -07001/*
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 "atomic_integer.h"
22#include "common_test.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/object_array-inl.h"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070024#include "thread_pool.h"
25#include "UniquePtr.h"
26
27namespace art {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070028class CheckWaitTask : public Task {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070029 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -070030 CheckWaitTask(Barrier* barrier, AtomicInteger* count1, AtomicInteger* count2,
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070031 AtomicInteger* count3)
32 : barrier_(barrier),
33 count1_(count1),
34 count2_(count2),
35 count3_(count3) {
36
37 }
38
39 void Run(Thread* self) {
Ian Rogers23055dc2013-04-18 16:29:16 -070040 LOG(INFO) << "Before barrier 1 " << *self;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070041 ++*count1_;
42 barrier_->Wait(self);
43 ++*count2_;
Ian Rogers23055dc2013-04-18 16:29:16 -070044 LOG(INFO) << "Before barrier 2 " << *self;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070045 barrier_->Wait(self);
46 ++*count3_;
Ian Rogers23055dc2013-04-18 16:29:16 -070047 LOG(INFO) << "After barrier 2 " << *self;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070048 }
49
50 virtual void Finalize() {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070051 delete this;
52 }
53 private:
54 Barrier* const barrier_;
55 AtomicInteger* const count1_;
56 AtomicInteger* const count2_;
57 AtomicInteger* const count3_;
58};
59
60class BarrierTest : public CommonTest {
61 public:
62 static int32_t num_threads;
63};
64
65int32_t BarrierTest::num_threads = 4;
66
67// Check that barrier wait and barrier increment work.
68TEST_F(BarrierTest, CheckWait) {
69 Thread* self = Thread::Current();
70 ThreadPool thread_pool(num_threads);
Mathieu Chartier35883cc2012-11-13 14:08:12 -080071 Barrier barrier(0);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070072 AtomicInteger count1 = 0;
73 AtomicInteger count2 = 0;
74 AtomicInteger count3 = 0;
75 for (int32_t i = 0; i < num_threads; ++i) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070076 thread_pool.AddTask(self, new CheckWaitTask(&barrier, &count1, &count2, &count3));
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070077 }
78 thread_pool.StartWorkers(self);
79 barrier.Increment(self, num_threads);
80 // At this point each thread should have passed through the barrier. The first count should be
81 // equal to num_threads.
82 EXPECT_EQ(num_threads, count1);
83 // Count 3 should still be zero since no thread should have gone past the second barrier.
84 EXPECT_EQ(0, count3);
85 // Now lets tell the threads to pass again.
86 barrier.Increment(self, num_threads);
87 // Count 2 should be equal to num_threads since each thread must have passed the second barrier
88 // at this point.
89 EXPECT_EQ(num_threads, count2);
90 // Wait for all the threads to finish.
91 thread_pool.Wait(self);
92 // All three counts should be equal to num_threads now.
93 EXPECT_EQ(count1, count2);
94 EXPECT_EQ(count2, count3);
95 EXPECT_EQ(num_threads, count3);
96}
97
Mathieu Chartier02b6a782012-10-26 13:51:26 -070098class CheckPassTask : public Task {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070099 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700100 CheckPassTask(Barrier* barrier, AtomicInteger* count, size_t subtasks)
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700101 : barrier_(barrier),
102 count_(count),
103 subtasks_(subtasks) {
104
105 }
106
107 void Run(Thread* self) {
108 for (size_t i = 0; i < subtasks_; ++i) {
109 ++*count_;
110 // Pass through to next subtask.
111 barrier_->Pass(self);
112 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700113 }
114
115 void Finalize() {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700116 delete this;
117 }
118 private:
119 Barrier* const barrier_;
120 AtomicInteger* const count_;
121 const size_t subtasks_;
122};
123
124// Check that barrier pass through works.
125TEST_F(BarrierTest, CheckPass) {
126 Thread* self = Thread::Current();
127 ThreadPool thread_pool(num_threads);
Mathieu Chartier35883cc2012-11-13 14:08:12 -0800128 Barrier barrier(0);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700129 AtomicInteger count = 0;
130 const int32_t num_tasks = num_threads * 4;
131 const int32_t num_sub_tasks = 128;
132 for (int32_t i = 0; i < num_tasks; ++i) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700133 thread_pool.AddTask(self, new CheckPassTask(&barrier, &count, num_sub_tasks));
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700134 }
135 thread_pool.StartWorkers(self);
136 const int32_t expected_total_tasks = num_sub_tasks * num_tasks;
137 // Wait for all the tasks to complete using the barrier.
138 barrier.Increment(self, expected_total_tasks);
139 // The total number of completed tasks should be equal to expected_total_tasks.
140 EXPECT_EQ(count, expected_total_tasks);
141}
142
143} // namespace art