blob: d5f17d17b15b62907bf903e0e940ff33c880dbf4 [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
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080017#include "thread_pool.h"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070018
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 Rogerse63db272014-07-15 15:36:11 -070023#include "thread-inl.h"
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070024
25namespace art {
26
Mathieu Chartier02b6a782012-10-26 13:51:26 -070027class CountTask : public Task {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070028 public:
Brian Carlstrom93ba8932013-07-17 21:31:49 -070029 explicit CountTask(AtomicInteger* count) : count_(count), verbose_(false) {}
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070030
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
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080053class ThreadPoolTest : public CommonRuntimeTest {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070054 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();
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080063 ThreadPool thread_pool("Thread pool test thread pool", num_threads);
Brian Carlstrom93ba8932013-07-17 21:31:49 -070064 AtomicInteger count(0);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070065 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.
Ian Rogers1d54e732013-05-02 21:10:01 -070071 thread_pool.Wait(self, true, false);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070072 // Make sure that we finished all the work.
Ian Rogers3e5cf302014-05-20 16:40:37 -070073 EXPECT_EQ(num_tasks, count.LoadSequentiallyConsistent());
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070074}
75
76TEST_F(ThreadPoolTest, StopStart) {
77 Thread* self = Thread::Current();
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -080078 ThreadPool thread_pool("Thread pool test thread pool", num_threads);
Brian Carlstrom93ba8932013-07-17 21:31:49 -070079 AtomicInteger count(0);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070080 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.
Ian Rogers3e5cf302014-05-20 16:40:37 -070086 EXPECT_EQ(0, count.LoadSequentiallyConsistent());
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070087 // Signal the threads to start processing tasks.
88 thread_pool.StartWorkers(self);
89 usleep(200);
90 thread_pool.StopWorkers(self);
Brian Carlstrom93ba8932013-07-17 21:31:49 -070091 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.
Ian Rogers3e5cf302014-05-20 16:40:37 -070095 EXPECT_EQ(0, bad_count.LoadSequentiallyConsistent());
Ian Rogersd914eb22013-04-18 16:11:15 -070096 // Allow tasks to finish up and delete themselves.
97 thread_pool.StartWorkers(self);
Mathieu Chartier7947b542014-11-09 12:30:50 -080098 thread_pool.Wait(self, false, false);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -070099}
100
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700101class TreeTask : public Task {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700102 public:
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700103 TreeTask(ThreadPool* const thread_pool, AtomicInteger* count, int depth)
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700104 : thread_pool_(thread_pool),
105 count_(count),
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700106 depth_(depth) {}
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700107
108 void Run(Thread* self) {
109 if (depth_ > 1) {
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700110 thread_pool_->AddTask(self, new TreeTask(thread_pool_, count_, depth_ - 1));
111 thread_pool_->AddTask(self, new TreeTask(thread_pool_, count_, depth_ - 1));
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700112 }
113 // Increment the counter which keeps track of work completed.
114 ++*count_;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700115 }
116
117 void Finalize() {
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700118 delete this;
119 }
120
121 private:
122 ThreadPool* const thread_pool_;
123 AtomicInteger* const count_;
124 const int depth_;
125};
126
127// Test that adding new tasks from within a task works.
128TEST_F(ThreadPoolTest, RecursiveTest) {
129 Thread* self = Thread::Current();
Mathieu Chartierbcd5e9d2013-11-13 14:33:28 -0800130 ThreadPool thread_pool("Thread pool test thread pool", num_threads);
Brian Carlstrom93ba8932013-07-17 21:31:49 -0700131 AtomicInteger count(0);
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700132 static const int depth = 8;
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700133 thread_pool.AddTask(self, new TreeTask(&thread_pool, &count, depth));
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700134 thread_pool.StartWorkers(self);
Ian Rogers1d54e732013-05-02 21:10:01 -0700135 thread_pool.Wait(self, true, false);
Ian Rogers3e5cf302014-05-20 16:40:37 -0700136 EXPECT_EQ((1 << depth) - 1, count.LoadSequentiallyConsistent());
Mathieu Chartier0e4627e2012-10-23 16:13:36 -0700137}
138
139} // namespace art