Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Side by Side Diff: runtime/vm/thread_pool_test.cc

Issue 2974233002: VM: Re-format to use at most one newline between functions (Closed)
Patch Set: Rebase and merge Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/thread_pool.cc ('k') | runtime/vm/thread_registry.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/thread_pool.h"
6 #include "vm/lockers.h"
5 #include "vm/os.h" 7 #include "vm/os.h"
6 #include "vm/lockers.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
15 VM_UNIT_TEST_CASE(ThreadPool_Create) { 14 VM_UNIT_TEST_CASE(ThreadPool_Create) {
16 ThreadPool thread_pool; 15 ThreadPool thread_pool;
17 } 16 }
18 17
19
20 class TestTask : public ThreadPool::Task { 18 class TestTask : public ThreadPool::Task {
21 public: 19 public:
22 TestTask(Monitor* sync, bool* done) : sync_(sync), done_(done) {} 20 TestTask(Monitor* sync, bool* done) : sync_(sync), done_(done) {}
23 21
24 // Before running the task, *done_ should be true. This lets the caller 22 // 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 23 // ASSERT things knowing that the thread is still around. To unblock the
26 // thread, the caller should take the lock, set *done_ to false, and Notify() 24 // thread, the caller should take the lock, set *done_ to false, and Notify()
27 // the monitor. 25 // the monitor.
28 virtual void Run() { 26 virtual void Run() {
29 { 27 {
30 MonitorLocker ml(sync_); 28 MonitorLocker ml(sync_);
31 while (*done_) { 29 while (*done_) {
32 ml.Wait(); 30 ml.Wait();
33 } 31 }
34 } 32 }
35 MonitorLocker ml(sync_); 33 MonitorLocker ml(sync_);
36 *done_ = true; 34 *done_ = true;
37 ml.Notify(); 35 ml.Notify();
38 } 36 }
39 37
40 private: 38 private:
41 Monitor* sync_; 39 Monitor* sync_;
42 bool* done_; 40 bool* done_;
43 }; 41 };
44 42
45
46 VM_UNIT_TEST_CASE(ThreadPool_RunOne) { 43 VM_UNIT_TEST_CASE(ThreadPool_RunOne) {
47 ThreadPool thread_pool; 44 ThreadPool thread_pool;
48 Monitor sync; 45 Monitor sync;
49 bool done = true; 46 bool done = true;
50 thread_pool.Run(new TestTask(&sync, &done)); 47 thread_pool.Run(new TestTask(&sync, &done));
51 { 48 {
52 MonitorLocker ml(&sync); 49 MonitorLocker ml(&sync);
53 done = false; 50 done = false;
54 ml.Notify(); 51 ml.Notify();
55 while (!done) { 52 while (!done) {
56 ml.Wait(); 53 ml.Wait();
57 } 54 }
58 } 55 }
59 EXPECT(done); 56 EXPECT(done);
60 57
61 // Do a sanity test on the worker stats. 58 // Do a sanity test on the worker stats.
62 EXPECT_EQ(1U, thread_pool.workers_started()); 59 EXPECT_EQ(1U, thread_pool.workers_started());
63 EXPECT_EQ(0U, thread_pool.workers_stopped()); 60 EXPECT_EQ(0U, thread_pool.workers_stopped());
64 } 61 }
65 62
66
67 VM_UNIT_TEST_CASE(ThreadPool_RunMany) { 63 VM_UNIT_TEST_CASE(ThreadPool_RunMany) {
68 const int kTaskCount = 100; 64 const int kTaskCount = 100;
69 ThreadPool thread_pool; 65 ThreadPool thread_pool;
70 Monitor sync[kTaskCount]; 66 Monitor sync[kTaskCount];
71 bool done[kTaskCount]; 67 bool done[kTaskCount];
72 68
73 for (int i = 0; i < kTaskCount; i++) { 69 for (int i = 0; i < kTaskCount; i++) {
74 done[i] = true; 70 done[i] = true;
75 thread_pool.Run(new TestTask(&sync[i], &done[i])); 71 thread_pool.Run(new TestTask(&sync[i], &done[i]));
76 } 72 }
77 for (int i = 0; i < kTaskCount; i++) { 73 for (int i = 0; i < kTaskCount; i++) {
78 MonitorLocker ml(&sync[i]); 74 MonitorLocker ml(&sync[i]);
79 done[i] = false; 75 done[i] = false;
80 ml.Notify(); 76 ml.Notify();
81 while (!done[i]) { 77 while (!done[i]) {
82 ml.Wait(); 78 ml.Wait();
83 } 79 }
84 EXPECT(done[i]); 80 EXPECT(done[i]);
85 } 81 }
86 } 82 }
87 83
88
89 class SleepTask : public ThreadPool::Task { 84 class SleepTask : public ThreadPool::Task {
90 public: 85 public:
91 SleepTask(Monitor* sync, int* started_count, int* slept_count, int millis) 86 SleepTask(Monitor* sync, int* started_count, int* slept_count, int millis)
92 : sync_(sync), 87 : sync_(sync),
93 started_count_(started_count), 88 started_count_(started_count),
94 slept_count_(slept_count), 89 slept_count_(slept_count),
95 millis_(millis) {} 90 millis_(millis) {}
96 91
97 virtual void Run() { 92 virtual void Run() {
98 { 93 {
(...skipping 12 matching lines...) Expand all
111 } 106 }
112 } 107 }
113 108
114 private: 109 private:
115 Monitor* sync_; 110 Monitor* sync_;
116 int* started_count_; 111 int* started_count_;
117 int* slept_count_; 112 int* slept_count_;
118 int millis_; 113 int millis_;
119 }; 114 };
120 115
121
122 VM_UNIT_TEST_CASE(ThreadPool_WorkerShutdown) { 116 VM_UNIT_TEST_CASE(ThreadPool_WorkerShutdown) {
123 const int kTaskCount = 10; 117 const int kTaskCount = 10;
124 Monitor sync; 118 Monitor sync;
125 int slept_count = 0; 119 int slept_count = 0;
126 int started_count = 0; 120 int started_count = 0;
127 121
128 // Set up the ThreadPool so that workers notify before they exit. 122 // Set up the ThreadPool so that workers notify before they exit.
129 ThreadPool* thread_pool = new ThreadPool(); 123 ThreadPool* thread_pool = new ThreadPool();
130 124
131 // Run a single task. 125 // Run a single task.
(...skipping 17 matching lines...) Expand all
149 { 143 {
150 MonitorLocker ml(&sync); 144 MonitorLocker ml(&sync);
151 final_count = slept_count; 145 final_count = slept_count;
152 } 146 }
153 147
154 // We should have waited for all the workers to finish, so they all should 148 // We should have waited for all the workers to finish, so they all should
155 // have had a chance to increment slept_count. 149 // have had a chance to increment slept_count.
156 EXPECT_EQ(kTaskCount, final_count); 150 EXPECT_EQ(kTaskCount, final_count);
157 } 151 }
158 152
159
160 VM_UNIT_TEST_CASE(ThreadPool_WorkerTimeout) { 153 VM_UNIT_TEST_CASE(ThreadPool_WorkerTimeout) {
161 // Adjust the worker timeout so that we timeout quickly. 154 // Adjust the worker timeout so that we timeout quickly.
162 int saved_timeout = FLAG_worker_timeout_millis; 155 int saved_timeout = FLAG_worker_timeout_millis;
163 FLAG_worker_timeout_millis = 1; 156 FLAG_worker_timeout_millis = 1;
164 157
165 ThreadPool thread_pool; 158 ThreadPool thread_pool;
166 EXPECT_EQ(0U, thread_pool.workers_started()); 159 EXPECT_EQ(0U, thread_pool.workers_started());
167 EXPECT_EQ(0U, thread_pool.workers_stopped()); 160 EXPECT_EQ(0U, thread_pool.workers_stopped());
168 161
169 // Run a worker. 162 // Run a worker.
(...skipping 16 matching lines...) Expand all
186 const int kMaxWait = 5000; 179 const int kMaxWait = 5000;
187 int waited = 0; 180 int waited = 0;
188 while (thread_pool.workers_stopped() == 0 && waited < kMaxWait) { 181 while (thread_pool.workers_stopped() == 0 && waited < kMaxWait) {
189 OS::Sleep(1); 182 OS::Sleep(1);
190 waited += 1; 183 waited += 1;
191 } 184 }
192 EXPECT_EQ(1U, thread_pool.workers_stopped()); 185 EXPECT_EQ(1U, thread_pool.workers_stopped());
193 FLAG_worker_timeout_millis = saved_timeout; 186 FLAG_worker_timeout_millis = saved_timeout;
194 } 187 }
195 188
196
197 class SpawnTask : public ThreadPool::Task { 189 class SpawnTask : public ThreadPool::Task {
198 public: 190 public:
199 SpawnTask(ThreadPool* pool, Monitor* sync, int todo, int total, int* done) 191 SpawnTask(ThreadPool* pool, Monitor* sync, int todo, int total, int* done)
200 : pool_(pool), sync_(sync), todo_(todo), total_(total), done_(done) {} 192 : pool_(pool), sync_(sync), todo_(todo), total_(total), done_(done) {}
201 193
202 virtual void Run() { 194 virtual void Run() {
203 todo_--; // Subtract one for current task. 195 todo_--; // Subtract one for current task.
204 int child_todo = todo_ / 2; 196 int child_todo = todo_ / 2;
205 197
206 // Spawn 0-2 children. 198 // Spawn 0-2 children.
(...skipping 15 matching lines...) Expand all
222 } 214 }
223 215
224 private: 216 private:
225 ThreadPool* pool_; 217 ThreadPool* pool_;
226 Monitor* sync_; 218 Monitor* sync_;
227 int todo_; 219 int todo_;
228 int total_; 220 int total_;
229 int* done_; 221 int* done_;
230 }; 222 };
231 223
232
233 VM_UNIT_TEST_CASE(ThreadPool_RecursiveSpawn) { 224 VM_UNIT_TEST_CASE(ThreadPool_RecursiveSpawn) {
234 ThreadPool thread_pool; 225 ThreadPool thread_pool;
235 Monitor sync; 226 Monitor sync;
236 const int kTotalTasks = 500; 227 const int kTotalTasks = 500;
237 int done = 0; 228 int done = 0;
238 thread_pool.Run( 229 thread_pool.Run(
239 new SpawnTask(&thread_pool, &sync, kTotalTasks, kTotalTasks, &done)); 230 new SpawnTask(&thread_pool, &sync, kTotalTasks, kTotalTasks, &done));
240 { 231 {
241 MonitorLocker ml(&sync); 232 MonitorLocker ml(&sync);
242 while (done < kTotalTasks) { 233 while (done < kTotalTasks) {
243 ml.Wait(); 234 ml.Wait();
244 } 235 }
245 } 236 }
246 EXPECT_EQ(kTotalTasks, done); 237 EXPECT_EQ(kTotalTasks, done);
247 } 238 }
248 239
249 } // namespace dart 240 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/thread_pool.cc ('k') | runtime/vm/thread_registry.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698