Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 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 | |
| 18 | #include <string> |
| 19 | |
| 20 | #include "atomic_integer.h" |
| 21 | #include "common_test.h" |
| 22 | #include "thread_pool.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 26 | class CountTask : public Task { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 27 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 28 | explicit CountTask(AtomicInteger* count) : count_(count), verbose_(false) {} |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 29 | |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 30 | void Run(Thread* self) { |
| 31 | if (verbose_) { |
| 32 | LOG(INFO) << "Running: " << *self; |
| 33 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 34 | // Simulate doing some work. |
| 35 | usleep(100); |
| 36 | // Increment the counter which keeps track of work completed. |
| 37 | ++*count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void Finalize() { |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 41 | if (verbose_) { |
| 42 | LOG(INFO) << "Finalizing: " << *Thread::Current(); |
| 43 | } |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 44 | delete this; |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | AtomicInteger* const count_; |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 49 | const bool verbose_; |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | class ThreadPoolTest : public CommonTest { |
| 53 | public: |
| 54 | static int32_t num_threads; |
| 55 | }; |
| 56 | |
| 57 | int32_t ThreadPoolTest::num_threads = 4; |
| 58 | |
| 59 | // Check that the thread pool actually runs tasks that you assign it. |
| 60 | TEST_F(ThreadPoolTest, CheckRun) { |
| 61 | Thread* self = Thread::Current(); |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 62 | ThreadPool thread_pool("Thread pool test thread pool", num_threads); |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 63 | AtomicInteger count(0); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 64 | static const int32_t num_tasks = num_threads * 4; |
| 65 | for (int32_t i = 0; i < num_tasks; ++i) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 66 | thread_pool.AddTask(self, new CountTask(&count)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 67 | } |
| 68 | thread_pool.StartWorkers(self); |
| 69 | // Wait for tasks to complete. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 70 | thread_pool.Wait(self, true, false); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 71 | // Make sure that we finished all the work. |
| 72 | EXPECT_EQ(num_tasks, count); |
| 73 | } |
| 74 | |
| 75 | TEST_F(ThreadPoolTest, StopStart) { |
| 76 | Thread* self = Thread::Current(); |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 77 | ThreadPool thread_pool("Thread pool test thread pool", num_threads); |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 78 | AtomicInteger count(0); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 79 | static const int32_t num_tasks = num_threads * 4; |
| 80 | for (int32_t i = 0; i < num_tasks; ++i) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 81 | thread_pool.AddTask(self, new CountTask(&count)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 82 | } |
| 83 | usleep(200); |
| 84 | // Check that no threads started prematurely. |
| 85 | EXPECT_EQ(0, count); |
| 86 | // Signal the threads to start processing tasks. |
| 87 | thread_pool.StartWorkers(self); |
| 88 | usleep(200); |
| 89 | thread_pool.StopWorkers(self); |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 90 | AtomicInteger bad_count(0); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 91 | thread_pool.AddTask(self, new CountTask(&bad_count)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 92 | usleep(200); |
| 93 | // Ensure that the task added after the workers were stopped doesn't get run. |
| 94 | EXPECT_EQ(0, bad_count); |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 95 | // Allow tasks to finish up and delete themselves. |
| 96 | thread_pool.StartWorkers(self); |
Mathieu Chartier | 4b95e8f | 2013-07-15 16:32:50 -0700 | [diff] [blame] | 97 | while (count.load() != num_tasks && bad_count.load() != 1) { |
Ian Rogers | d914eb2 | 2013-04-18 16:11:15 -0700 | [diff] [blame] | 98 | usleep(200); |
| 99 | } |
| 100 | thread_pool.StopWorkers(self); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 103 | class TreeTask : public Task { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 104 | public: |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 105 | TreeTask(ThreadPool* const thread_pool, AtomicInteger* count, int depth) |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 106 | : thread_pool_(thread_pool), |
| 107 | count_(count), |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 108 | depth_(depth) {} |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 109 | |
| 110 | void Run(Thread* self) { |
| 111 | if (depth_ > 1) { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 112 | thread_pool_->AddTask(self, new TreeTask(thread_pool_, count_, depth_ - 1)); |
| 113 | thread_pool_->AddTask(self, new TreeTask(thread_pool_, count_, depth_ - 1)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 114 | } |
| 115 | // Increment the counter which keeps track of work completed. |
| 116 | ++*count_; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void Finalize() { |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 120 | delete this; |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | ThreadPool* const thread_pool_; |
| 125 | AtomicInteger* const count_; |
| 126 | const int depth_; |
| 127 | }; |
| 128 | |
| 129 | // Test that adding new tasks from within a task works. |
| 130 | TEST_F(ThreadPoolTest, RecursiveTest) { |
| 131 | Thread* self = Thread::Current(); |
Mathieu Chartier | bcd5e9d | 2013-11-13 14:33:28 -0800 | [diff] [blame] | 132 | ThreadPool thread_pool("Thread pool test thread pool", num_threads); |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 133 | AtomicInteger count(0); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 134 | static const int depth = 8; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 135 | thread_pool.AddTask(self, new TreeTask(&thread_pool, &count, depth)); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 136 | thread_pool.StartWorkers(self); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 137 | thread_pool.Wait(self, true, false); |
Mathieu Chartier | 0e4627e | 2012-10-23 16:13:36 -0700 | [diff] [blame] | 138 | EXPECT_EQ((1 << depth) - 1, count); |
| 139 | } |
| 140 | |
| 141 | } // namespace art |