| 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_micros); |
| 13 | 13 |
| 14 | 14 |
| 15 UNIT_TEST_CASE(ThreadPool_Create) { | 15 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) {} |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 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_micros; |
| 163 FLAG_worker_timeout_millis = 1; | 163 FLAG_worker_timeout_micros = 1000; |
| 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; |
| 171 bool done = true; | 171 bool done = true; |
| 172 thread_pool.Run(new TestTask(&sync, &done)); | 172 thread_pool.Run(new TestTask(&sync, &done)); |
| 173 EXPECT_EQ(1U, thread_pool.workers_started()); | 173 EXPECT_EQ(1U, thread_pool.workers_started()); |
| 174 EXPECT_EQ(0U, thread_pool.workers_stopped()); | 174 EXPECT_EQ(0U, thread_pool.workers_stopped()); |
| 175 { | 175 { |
| 176 MonitorLocker ml(&sync); | 176 MonitorLocker ml(&sync); |
| 177 done = false; | 177 done = false; |
| 178 ml.Notify(); | 178 ml.Notify(); |
| 179 while (!done) { | 179 while (!done) { |
| 180 ml.Wait(); | 180 ml.Wait(); |
| 181 } | 181 } |
| 182 } | 182 } |
| 183 EXPECT(done); | 183 EXPECT(done); |
| 184 | 184 |
| 185 // Wait up to 5 seconds to see if a worker times out. | 185 // Wait up to 5 seconds to see if a worker times out. |
| 186 const int kMaxWait = 5000; | 186 const int kMaxWait = 5000; |
| 187 int waited = 0; | 187 int waited = 0; |
| 188 while (thread_pool.workers_stopped() == 0 && waited < kMaxWait) { | 188 while (thread_pool.workers_stopped() == 0 && waited < kMaxWait) { |
| 189 OS::Sleep(1); | 189 OS::Sleep(1); |
| 190 waited += 1; | 190 waited += 1; |
| 191 } | 191 } |
| 192 EXPECT_EQ(1U, thread_pool.workers_stopped()); | 192 EXPECT_EQ(1U, thread_pool.workers_stopped()); |
| 193 FLAG_worker_timeout_millis = saved_timeout; | 193 FLAG_worker_timeout_micros = saved_timeout; |
| 194 } | 194 } |
| 195 | 195 |
| 196 | 196 |
| 197 class SpawnTask : public ThreadPool::Task { | 197 class SpawnTask : public ThreadPool::Task { |
| 198 public: | 198 public: |
| 199 SpawnTask(ThreadPool* pool, Monitor* sync, int todo, int total, int* done) | 199 SpawnTask(ThreadPool* pool, Monitor* sync, int todo, int total, int* done) |
| 200 : pool_(pool), sync_(sync), todo_(todo), total_(total), done_(done) {} | 200 : pool_(pool), sync_(sync), todo_(todo), total_(total), done_(done) {} |
| 201 | 201 |
| 202 virtual void Run() { | 202 virtual void Run() { |
| 203 todo_--; // Subtract one for current task. | 203 todo_--; // Subtract one for current task. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |