OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #include "vm/thread.h" | |
6 #include "vm/thread_pool.h" | |
7 #include "vm/unit_test.h" | |
8 | |
9 namespace dart { | |
10 | |
11 DECLARE_FLAG(int, worker_timeout_millis); | |
12 | |
13 UNIT_TEST_CASE(ThreadPool_Create) { | |
14 ThreadPool thread_pool; | |
15 } | |
16 | |
17 | |
18 class TestTask : public ThreadPool::Task { | |
19 public: | |
20 TestTask(Monitor* sync, bool* done) | |
21 : sync_(sync), done_(done) { | |
22 } | |
23 | |
24 void Run() { | |
25 MonitorLocker ml(sync_); | |
26 *done_ = true; | |
27 ml.Notify(); | |
28 } | |
29 | |
30 private: | |
31 Monitor* sync_; | |
32 bool* done_; | |
33 }; | |
34 | |
35 | |
36 UNIT_TEST_CASE(ThreadPool_RunOne) { | |
37 ThreadPool thread_pool; | |
38 Monitor sync; | |
39 bool done = false; | |
40 thread_pool.Run(new TestTask(&sync, &done)); | |
41 { | |
42 MonitorLocker ml(&sync); | |
43 while (!done) { | |
44 ml.Wait(); | |
45 } | |
46 } | |
47 EXPECT(done); | |
48 } | |
49 | |
50 | |
51 UNIT_TEST_CASE(ThreadPool_RunMany) { | |
52 const int kTaskCount = 100; | |
53 ThreadPool thread_pool; | |
54 Monitor sync[kTaskCount]; | |
55 bool done[kTaskCount]; | |
56 | |
57 for (int i = 0; i < kTaskCount; i++) { | |
58 done[i] = false; | |
59 thread_pool.Run(new TestTask(&sync[i], &done[i])); | |
60 } | |
61 for (int i = 0; i < kTaskCount; i++) { | |
62 MonitorLocker ml(&sync[i]); | |
63 while (!done[i]) { | |
64 ml.Wait(); | |
65 } | |
66 EXPECT(done[i]); | |
67 } | |
siva
2012/03/06 05:07:00
100 threads are going to hang around for default t
turnidge
2012/03/06 22:34:01
The destructor stops the threadpool.
| |
68 } | |
69 | |
70 | |
71 UNIT_TEST_CASE(ThreadPool_WorkerTimeout) { | |
72 // Adjust the worker timeout so that we timeout quickly. | |
73 int saved_timeout = FLAG_worker_timeout_millis; | |
74 FLAG_worker_timeout_millis = 1; | |
75 | |
76 ThreadPool thread_pool; | |
77 EXPECT_EQ(0, thread_pool.workers_started()); | |
78 EXPECT_EQ(0, thread_pool.workers_stopped()); | |
79 | |
80 // Run a worker. | |
81 Monitor sync; | |
82 bool done = false; | |
83 thread_pool.Run(new TestTask(&sync, &done)); | |
84 EXPECT_EQ(1, thread_pool.workers_started()); | |
85 EXPECT_EQ(0, thread_pool.workers_stopped()); | |
86 { | |
87 MonitorLocker ml(&sync); | |
88 while (!done) { | |
89 ml.Wait(); | |
90 } | |
91 } | |
92 EXPECT(done); | |
93 | |
94 // Wait up to 5 seconds to see if a worker times out. | |
95 const int kMaxWait = 5000; | |
96 int waited = 0; | |
97 while (thread_pool.workers_stopped() == 0 && waited < kMaxWait) { | |
98 OS::Sleep(1); | |
99 waited += 1; | |
100 } | |
101 EXPECT_EQ(1, thread_pool.workers_stopped()); | |
102 FLAG_worker_timeout_millis = saved_timeout; | |
103 } | |
104 | |
105 | |
106 class SpawnTask : public ThreadPool::Task { | |
107 public: | |
108 SpawnTask(ThreadPool* pool, Monitor* sync, int todo, int total, int* done) | |
109 : pool_(pool), sync_(sync), todo_(todo), total_(total), done_(done) { | |
110 } | |
111 | |
112 void Run() { | |
113 todo_--; // Subtract one for current task. | |
114 int child_todo = todo_ / 2; | |
115 | |
116 // Spawn 0-2 children. | |
117 if (todo_ > 0) { | |
118 pool_->Run( | |
119 new SpawnTask(pool_, sync_, todo_ - child_todo, total_, done_)); | |
120 } | |
121 if (todo_ > 1) { | |
122 pool_->Run( | |
123 new SpawnTask(pool_, sync_, child_todo, total_, done_)); | |
124 } | |
125 | |
126 { | |
127 MonitorLocker ml(sync_); | |
128 (*done_)++; | |
129 if (*done_ >= total_) { | |
130 ml.Notify(); | |
131 } | |
132 } | |
133 } | |
134 | |
135 private: | |
136 ThreadPool* pool_; | |
137 Monitor* sync_; | |
138 int todo_; | |
139 int total_; | |
140 int* done_; | |
141 }; | |
142 | |
143 | |
144 UNIT_TEST_CASE(ThreadPool_RecursiveSpawn) { | |
145 ThreadPool thread_pool; | |
146 Monitor sync; | |
147 const int kTotalTasks = 500; | |
148 int done = 0; | |
149 thread_pool.Run( | |
150 new SpawnTask(&thread_pool, &sync, kTotalTasks, kTotalTasks, &done)); | |
151 { | |
152 MonitorLocker ml(&sync); | |
153 while (done < kTotalTasks) { | |
154 ml.Wait(); | |
155 } | |
156 } | |
157 EXPECT_EQ(kTotalTasks, done); | |
158 } | |
159 | |
160 | |
161 } // namespace dart | |
OLD | NEW |