OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/os.h" | 5 #include "vm/os.h" |
6 #include "vm/lockers.h" | 6 #include "vm/lockers.h" |
7 #include "vm/thread_pool.h" | 7 #include "vm/thread_pool.h" |
8 #include "vm/unit_test.h" | 8 #include "vm/unit_test.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
11 | 11 |
12 DECLARE_FLAG(int, worker_timeout_millis); | 12 DECLARE_FLAG(int, worker_timeout_millis); |
13 | 13 |
14 | 14 |
15 UNIT_TEST_CASE(ThreadPool_Create) { | 15 VM_UNIT_TEST_CASE(ThreadPool_Create) { |
16 ThreadPool thread_pool; | 16 ThreadPool thread_pool; |
17 } | 17 } |
18 | 18 |
19 | 19 |
20 class TestTask : public ThreadPool::Task { | 20 class TestTask : public ThreadPool::Task { |
21 public: | 21 public: |
22 TestTask(Monitor* sync, bool* done) : sync_(sync), done_(done) {} | 22 TestTask(Monitor* sync, bool* done) : sync_(sync), done_(done) {} |
23 | 23 |
24 // Before running the task, *done_ should be true. This lets the caller | 24 // Before running the task, *done_ should be true. This lets the caller |
25 // ASSERT things knowing that the thread is still around. To unblock the | 25 // ASSERT things knowing that the thread is still around. To unblock the |
(...skipping 10 matching lines...) Expand all Loading... |
36 *done_ = true; | 36 *done_ = true; |
37 ml.Notify(); | 37 ml.Notify(); |
38 } | 38 } |
39 | 39 |
40 private: | 40 private: |
41 Monitor* sync_; | 41 Monitor* sync_; |
42 bool* done_; | 42 bool* done_; |
43 }; | 43 }; |
44 | 44 |
45 | 45 |
46 UNIT_TEST_CASE(ThreadPool_RunOne) { | 46 VM_UNIT_TEST_CASE(ThreadPool_RunOne) { |
47 ThreadPool thread_pool; | 47 ThreadPool thread_pool; |
48 Monitor sync; | 48 Monitor sync; |
49 bool done = true; | 49 bool done = true; |
50 thread_pool.Run(new TestTask(&sync, &done)); | 50 thread_pool.Run(new TestTask(&sync, &done)); |
51 { | 51 { |
52 MonitorLocker ml(&sync); | 52 MonitorLocker ml(&sync); |
53 done = false; | 53 done = false; |
54 ml.Notify(); | 54 ml.Notify(); |
55 while (!done) { | 55 while (!done) { |
56 ml.Wait(); | 56 ml.Wait(); |
57 } | 57 } |
58 } | 58 } |
59 EXPECT(done); | 59 EXPECT(done); |
60 | 60 |
61 // Do a sanity test on the worker stats. | 61 // Do a sanity test on the worker stats. |
62 EXPECT_EQ(1U, thread_pool.workers_started()); | 62 EXPECT_EQ(1U, thread_pool.workers_started()); |
63 EXPECT_EQ(0U, thread_pool.workers_stopped()); | 63 EXPECT_EQ(0U, thread_pool.workers_stopped()); |
64 } | 64 } |
65 | 65 |
66 | 66 |
67 UNIT_TEST_CASE(ThreadPool_RunMany) { | 67 VM_UNIT_TEST_CASE(ThreadPool_RunMany) { |
68 const int kTaskCount = 100; | 68 const int kTaskCount = 100; |
69 ThreadPool thread_pool; | 69 ThreadPool thread_pool; |
70 Monitor sync[kTaskCount]; | 70 Monitor sync[kTaskCount]; |
71 bool done[kTaskCount]; | 71 bool done[kTaskCount]; |
72 | 72 |
73 for (int i = 0; i < kTaskCount; i++) { | 73 for (int i = 0; i < kTaskCount; i++) { |
74 done[i] = true; | 74 done[i] = true; |
75 thread_pool.Run(new TestTask(&sync[i], &done[i])); | 75 thread_pool.Run(new TestTask(&sync[i], &done[i])); |
76 } | 76 } |
77 for (int i = 0; i < kTaskCount; i++) { | 77 for (int i = 0; i < kTaskCount; i++) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 112 } |
113 | 113 |
114 private: | 114 private: |
115 Monitor* sync_; | 115 Monitor* sync_; |
116 int* started_count_; | 116 int* started_count_; |
117 int* slept_count_; | 117 int* slept_count_; |
118 int millis_; | 118 int millis_; |
119 }; | 119 }; |
120 | 120 |
121 | 121 |
122 UNIT_TEST_CASE(ThreadPool_WorkerShutdown) { | 122 VM_UNIT_TEST_CASE(ThreadPool_WorkerShutdown) { |
123 const int kTaskCount = 10; | 123 const int kTaskCount = 10; |
124 Monitor sync; | 124 Monitor sync; |
125 int slept_count = 0; | 125 int slept_count = 0; |
126 int started_count = 0; | 126 int started_count = 0; |
127 | 127 |
128 // Set up the ThreadPool so that workers notify before they exit. | 128 // Set up the ThreadPool so that workers notify before they exit. |
129 ThreadPool* thread_pool = new ThreadPool(); | 129 ThreadPool* thread_pool = new ThreadPool(); |
130 | 130 |
131 // Run a single task. | 131 // Run a single task. |
132 for (int i = 0; i < kTaskCount; i++) { | 132 for (int i = 0; i < kTaskCount; i++) { |
(...skipping 17 matching lines...) Expand all Loading... |
150 MonitorLocker ml(&sync); | 150 MonitorLocker ml(&sync); |
151 final_count = slept_count; | 151 final_count = slept_count; |
152 } | 152 } |
153 | 153 |
154 // We should have waited for all the workers to finish, so they all should | 154 // We should have waited for all the workers to finish, so they all should |
155 // have had a chance to increment slept_count. | 155 // have had a chance to increment slept_count. |
156 EXPECT_EQ(kTaskCount, final_count); | 156 EXPECT_EQ(kTaskCount, final_count); |
157 } | 157 } |
158 | 158 |
159 | 159 |
160 UNIT_TEST_CASE(ThreadPool_WorkerTimeout) { | 160 VM_UNIT_TEST_CASE(ThreadPool_WorkerTimeout) { |
161 // Adjust the worker timeout so that we timeout quickly. | 161 // Adjust the worker timeout so that we timeout quickly. |
162 int saved_timeout = FLAG_worker_timeout_millis; | 162 int saved_timeout = FLAG_worker_timeout_millis; |
163 FLAG_worker_timeout_millis = 1; | 163 FLAG_worker_timeout_millis = 1; |
164 | 164 |
165 ThreadPool thread_pool; | 165 ThreadPool thread_pool; |
166 EXPECT_EQ(0U, thread_pool.workers_started()); | 166 EXPECT_EQ(0U, thread_pool.workers_started()); |
167 EXPECT_EQ(0U, thread_pool.workers_stopped()); | 167 EXPECT_EQ(0U, thread_pool.workers_stopped()); |
168 | 168 |
169 // Run a worker. | 169 // Run a worker. |
170 Monitor sync; | 170 Monitor sync; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 | 223 |
224 private: | 224 private: |
225 ThreadPool* pool_; | 225 ThreadPool* pool_; |
226 Monitor* sync_; | 226 Monitor* sync_; |
227 int todo_; | 227 int todo_; |
228 int total_; | 228 int total_; |
229 int* done_; | 229 int* done_; |
230 }; | 230 }; |
231 | 231 |
232 | 232 |
233 UNIT_TEST_CASE(ThreadPool_RecursiveSpawn) { | 233 VM_UNIT_TEST_CASE(ThreadPool_RecursiveSpawn) { |
234 ThreadPool thread_pool; | 234 ThreadPool thread_pool; |
235 Monitor sync; | 235 Monitor sync; |
236 const int kTotalTasks = 500; | 236 const int kTotalTasks = 500; |
237 int done = 0; | 237 int done = 0; |
238 thread_pool.Run( | 238 thread_pool.Run( |
239 new SpawnTask(&thread_pool, &sync, kTotalTasks, kTotalTasks, &done)); | 239 new SpawnTask(&thread_pool, &sync, kTotalTasks, kTotalTasks, &done)); |
240 { | 240 { |
241 MonitorLocker ml(&sync); | 241 MonitorLocker ml(&sync); |
242 while (done < kTotalTasks) { | 242 while (done < kTotalTasks) { |
243 ml.Wait(); | 243 ml.Wait(); |
244 } | 244 } |
245 } | 245 } |
246 EXPECT_EQ(kTotalTasks, done); | 246 EXPECT_EQ(kTotalTasks, done); |
247 } | 247 } |
248 | 248 |
249 } // namespace dart | 249 } // namespace dart |
OLD | NEW |