blob: 086ef440a39d2ba4a26548520788d957f2961f51 [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
Ian Rogersef7d42f2014-01-06 12:55:46 -080021#include "atomic.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022#include "common_runtime_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"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070025
26namespace art {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070027class CheckWaitTask : public Task {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070028 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -070029 CheckWaitTask(Barrier* barrier, AtomicInteger* count1, AtomicInteger* count2,
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070030 AtomicInteger* count3)
31 : barrier_(barrier),
32 count1_(count1),
33 count2_(count2),
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070034 count3_(count3) {}
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070035
36 void Run(Thread* self) {
Ian Rogers23055dc2013-04-18 16:29:16 -070037 LOG(INFO) << "Before barrier 1 " << *self;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070038 ++*count1_;
39 barrier_->Wait(self);
40 ++*count2_;
Ian Rogers23055dc2013-04-18 16:29:16 -070041 LOG(INFO) << "Before barrier 2 " << *self;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070042 barrier_->Wait(self);
43 ++*count3_;
Ian Rogers23055dc2013-04-18 16:29:16 -070044 LOG(INFO) << "After barrier 2 " << *self;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070045 }
46
47 virtual void Finalize() {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070048 delete this;
49 }
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -070050
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070051 private:
52 Barrier* const barrier_;
53 AtomicInteger* const count1_;
54 AtomicInteger* const count2_;
55 AtomicInteger* const count3_;
56};
57
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080058class BarrierTest : public CommonRuntimeTest {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070059 public:
60 static int32_t num_threads;
61};
62
63int32_t BarrierTest::num_threads = 4;
64
65// Check that barrier wait and barrier increment work.
66TEST_F(BarrierTest, CheckWait) {
67 Thread* self = Thread::Current();
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080068 ThreadPool thread_pool("Barrier test thread pool", num_threads);
Mathieu Chartier35883cc2012-11-13 14:08:12 -080069 Barrier barrier(0);
Brian Carlstrom93ba8932013-07-17 21:31:49 -070070 AtomicInteger count1(0);
71 AtomicInteger count2(0);
72 AtomicInteger count3(0);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070073 for (int32_t i = 0; i < num_threads; ++i) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070074 thread_pool.AddTask(self, new CheckWaitTask(&barrier, &count1, &count2, &count3));
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070075 }
76 thread_pool.StartWorkers(self);
77 barrier.Increment(self, num_threads);
78 // At this point each thread should have passed through the barrier. The first count should be
79 // equal to num_threads.
Ian Rogers3e5cf302014-05-20 16:40:37 -070080 EXPECT_EQ(num_threads, count1.LoadRelaxed());
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070081 // Count 3 should still be zero since no thread should have gone past the second barrier.
Ian Rogers3e5cf302014-05-20 16:40:37 -070082 EXPECT_EQ(0, count3.LoadRelaxed());
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070083 // Now lets tell the threads to pass again.
84 barrier.Increment(self, num_threads);
85 // Count 2 should be equal to num_threads since each thread must have passed the second barrier
86 // at this point.
Ian Rogers3e5cf302014-05-20 16:40:37 -070087 EXPECT_EQ(num_threads, count2.LoadRelaxed());
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070088 // Wait for all the threads to finish.
Ian Rogers1d54e732013-05-02 21:10:01 -070089 thread_pool.Wait(self, true, false);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070090 // All three counts should be equal to num_threads now.
Ian Rogers3e5cf302014-05-20 16:40:37 -070091 EXPECT_EQ(count1.LoadRelaxed(), count2.LoadRelaxed());
92 EXPECT_EQ(count2.LoadRelaxed(), count3.LoadRelaxed());
93 EXPECT_EQ(num_threads, count3.LoadRelaxed());
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070094}
95
Mathieu Chartier02b6a782012-10-26 13:51:26 -070096class CheckPassTask : public Task {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070097 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -070098 CheckPassTask(Barrier* barrier, AtomicInteger* count, size_t subtasks)
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070099 : barrier_(barrier),
100 count_(count),
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700101 subtasks_(subtasks) {}
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700102
103 void Run(Thread* self) {
104 for (size_t i = 0; i < subtasks_; ++i) {
105 ++*count_;
106 // Pass through to next subtask.
107 barrier_->Pass(self);
108 }
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700109 }
110
111 void Finalize() {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700112 delete this;
113 }
114 private:
115 Barrier* const barrier_;
116 AtomicInteger* const count_;
117 const size_t subtasks_;
118};
119
120// Check that barrier pass through works.
121TEST_F(BarrierTest, CheckPass) {
122 Thread* self = Thread::Current();
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800123 ThreadPool thread_pool("Barrier test thread pool", num_threads);
Mathieu Chartier35883cc2012-11-13 14:08:12 -0800124 Barrier barrier(0);
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700125 AtomicInteger count(0);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700126 const int32_t num_tasks = num_threads * 4;
127 const int32_t num_sub_tasks = 128;
128 for (int32_t i = 0; i < num_tasks; ++i) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700129 thread_pool.AddTask(self, new CheckPassTask(&barrier, &count, num_sub_tasks));
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700130 }
131 thread_pool.StartWorkers(self);
132 const int32_t expected_total_tasks = num_sub_tasks * num_tasks;
133 // Wait for all the tasks to complete using the barrier.
134 barrier.Increment(self, expected_total_tasks);
135 // The total number of completed tasks should be equal to expected_total_tasks.
Ian Rogers3e5cf302014-05-20 16:40:37 -0700136 EXPECT_EQ(count.LoadRelaxed(), expected_total_tasks);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700137}
138
139} // namespace art