| 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 { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 ThreadPool thread_pool; | 28 ThreadPool thread_pool; |
| 29 } | 29 } |
| 30 | 30 |
| 31 | 31 |
| 32 class TestTask : public ThreadPool::Task { | 32 class TestTask : public ThreadPool::Task { |
| 33 public: | 33 public: |
| 34 TestTask(Monitor* sync, bool* done) | 34 TestTask(Monitor* sync, bool* done) |
| 35 : sync_(sync), done_(done) { | 35 : sync_(sync), done_(done) { |
| 36 } | 36 } |
| 37 | 37 |
| 38 void Run() { | 38 virtual void Run() { |
| 39 MonitorLocker ml(sync_); | 39 MonitorLocker ml(sync_); |
| 40 *done_ = true; | 40 *done_ = true; |
| 41 ml.Notify(); | 41 ml.Notify(); |
| 42 } | 42 } |
| 43 | 43 |
| 44 private: | 44 private: |
| 45 Monitor* sync_; | 45 Monitor* sync_; |
| 46 bool* done_; | 46 bool* done_; |
| 47 }; | 47 }; |
| 48 | 48 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 | 88 |
| 89 class SleepTask : public ThreadPool::Task { | 89 class SleepTask : public ThreadPool::Task { |
| 90 public: | 90 public: |
| 91 explicit SleepTask(int millis) | 91 explicit SleepTask(int millis) |
| 92 : millis_(millis) { | 92 : millis_(millis) { |
| 93 } | 93 } |
| 94 | 94 |
| 95 void Run() { | 95 virtual void Run() { |
| 96 OS::Sleep(millis_); | 96 OS::Sleep(millis_); |
| 97 } | 97 } |
| 98 | 98 |
| 99 private: | 99 private: |
| 100 int millis_; | 100 int millis_; |
| 101 }; | 101 }; |
| 102 | 102 |
| 103 | 103 |
| 104 UNIT_TEST_CASE(ThreadPool_WorkerShutdown) { | 104 UNIT_TEST_CASE(ThreadPool_WorkerShutdown) { |
| 105 Monitor exit_sync; | 105 Monitor exit_sync; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 FLAG_worker_timeout_millis = saved_timeout; | 159 FLAG_worker_timeout_millis = saved_timeout; |
| 160 } | 160 } |
| 161 | 161 |
| 162 | 162 |
| 163 class SpawnTask : public ThreadPool::Task { | 163 class SpawnTask : public ThreadPool::Task { |
| 164 public: | 164 public: |
| 165 SpawnTask(ThreadPool* pool, Monitor* sync, int todo, int total, int* done) | 165 SpawnTask(ThreadPool* pool, Monitor* sync, int todo, int total, int* done) |
| 166 : pool_(pool), sync_(sync), todo_(todo), total_(total), done_(done) { | 166 : pool_(pool), sync_(sync), todo_(todo), total_(total), done_(done) { |
| 167 } | 167 } |
| 168 | 168 |
| 169 void Run() { | 169 virtual void Run() { |
| 170 todo_--; // Subtract one for current task. | 170 todo_--; // Subtract one for current task. |
| 171 int child_todo = todo_ / 2; | 171 int child_todo = todo_ / 2; |
| 172 | 172 |
| 173 // Spawn 0-2 children. | 173 // Spawn 0-2 children. |
| 174 if (todo_ > 0) { | 174 if (todo_ > 0) { |
| 175 pool_->Run( | 175 pool_->Run( |
| 176 new SpawnTask(pool_, sync_, todo_ - child_todo, total_, done_)); | 176 new SpawnTask(pool_, sync_, todo_ - child_todo, total_, done_)); |
| 177 } | 177 } |
| 178 if (todo_ > 1) { | 178 if (todo_ > 1) { |
| 179 pool_->Run( | 179 pool_->Run( |
| (...skipping 29 matching lines...) Expand all Loading... |
| 209 MonitorLocker ml(&sync); | 209 MonitorLocker ml(&sync); |
| 210 while (done < kTotalTasks) { | 210 while (done < kTotalTasks) { |
| 211 ml.Wait(); | 211 ml.Wait(); |
| 212 } | 212 } |
| 213 } | 213 } |
| 214 EXPECT_EQ(kTotalTasks, done); | 214 EXPECT_EQ(kTotalTasks, done); |
| 215 } | 215 } |
| 216 | 216 |
| 217 | 217 |
| 218 } // namespace dart | 218 } // namespace dart |
| OLD | NEW |