blob: e056935319c4e16ba90d71d00a7a5c7f5f4c059d [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
18#include <string>
19
20#include "atomic_integer.h"
21#include "common_test.h"
22#include "thread_pool.h"
23
24namespace art {
25
Mathieu Chartier02b6a782012-10-26 13:51:26 -070026class CountTask : public Task {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070027 public:
Ian Rogersd914eb22013-04-18 16:11:15 -070028 CountTask(AtomicInteger* count) : count_(count), verbose_(false) {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070029 }
30
Ian Rogersd914eb22013-04-18 16:11:15 -070031 void Run(Thread* self) {
32 if (verbose_) {
33 LOG(INFO) << "Running: " << *self;
34 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070035 // Simulate doing some work.
36 usleep(100);
37 // Increment the counter which keeps track of work completed.
38 ++*count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070039 }
40
41 void Finalize() {
Ian Rogersd914eb22013-04-18 16:11:15 -070042 if (verbose_) {
43 LOG(INFO) << "Finalizing: " << *Thread::Current();
44 }
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070045 delete this;
46 }
47
48 private:
49 AtomicInteger* const count_;
Ian Rogersd914eb22013-04-18 16:11:15 -070050 const bool verbose_;
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070051};
52
53class ThreadPoolTest : public CommonTest {
54 public:
55 static int32_t num_threads;
56};
57
58int32_t ThreadPoolTest::num_threads = 4;
59
60// Check that the thread pool actually runs tasks that you assign it.
61TEST_F(ThreadPoolTest, CheckRun) {
62 Thread* self = Thread::Current();
63 ThreadPool thread_pool(num_threads);
64 AtomicInteger count = 0;
65 static const int32_t num_tasks = num_threads * 4;
66 for (int32_t i = 0; i < num_tasks; ++i) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070067 thread_pool.AddTask(self, new CountTask(&count));
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070068 }
69 thread_pool.StartWorkers(self);
70 // Wait for tasks to complete.
71 thread_pool.Wait(self);
72 // Make sure that we finished all the work.
73 EXPECT_EQ(num_tasks, count);
74}
75
76TEST_F(ThreadPoolTest, StopStart) {
77 Thread* self = Thread::Current();
78 ThreadPool thread_pool(num_threads);
79 AtomicInteger count = 0;
80 static const int32_t num_tasks = num_threads * 4;
81 for (int32_t i = 0; i < num_tasks; ++i) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -070082 thread_pool.AddTask(self, new CountTask(&count));
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070083 }
84 usleep(200);
85 // Check that no threads started prematurely.
86 EXPECT_EQ(0, count);
87 // Signal the threads to start processing tasks.
88 thread_pool.StartWorkers(self);
89 usleep(200);
90 thread_pool.StopWorkers(self);
91 AtomicInteger bad_count = 0;
Mathieu Chartier02b6a782012-10-26 13:51:26 -070092 thread_pool.AddTask(self, new CountTask(&bad_count));
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070093 usleep(200);
94 // Ensure that the task added after the workers were stopped doesn't get run.
95 EXPECT_EQ(0, bad_count);
Ian Rogersd914eb22013-04-18 16:11:15 -070096 // Allow tasks to finish up and delete themselves.
97 thread_pool.StartWorkers(self);
98 while (count.get() != num_tasks && bad_count.get() != 1) {
99 usleep(200);
100 }
101 thread_pool.StopWorkers(self);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700102}
103
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700104class TreeTask : public Task {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700105 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700106 TreeTask(ThreadPool* const thread_pool, AtomicInteger* count, int depth)
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700107 : thread_pool_(thread_pool),
108 count_(count),
109 depth_(depth) {
110
111 }
112
113 void Run(Thread* self) {
114 if (depth_ > 1) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700115 thread_pool_->AddTask(self, new TreeTask(thread_pool_, count_, depth_ - 1));
116 thread_pool_->AddTask(self, new TreeTask(thread_pool_, count_, depth_ - 1));
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700117 }
118 // Increment the counter which keeps track of work completed.
119 ++*count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700120 }
121
122 void Finalize() {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700123 delete this;
124 }
125
126 private:
127 ThreadPool* const thread_pool_;
128 AtomicInteger* const count_;
129 const int depth_;
130};
131
132// Test that adding new tasks from within a task works.
133TEST_F(ThreadPoolTest, RecursiveTest) {
134 Thread* self = Thread::Current();
135 ThreadPool thread_pool(num_threads);
136 AtomicInteger count = 0;
137 static const int depth = 8;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700138 thread_pool.AddTask(self, new TreeTask(&thread_pool, &count, depth));
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700139 thread_pool.StartWorkers(self);
140 thread_pool.Wait(self);
141 EXPECT_EQ((1 << depth) - 1, count);
142}
143
144} // namespace art