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 #ifndef VM_THREAD_POOL_H_ | |
6 #define VM_THREAD_POOL_H_ | |
7 | |
8 #include "vm/thread.h" | |
9 | |
10 namespace dart { | |
11 | |
12 class ThreadPool { | |
13 public: | |
14 // Subclasses of Task are able to run on a ThreadPool. | |
15 class Task { | |
16 protected: | |
17 Task(); | |
18 | |
19 public: | |
20 virtual ~Task(); | |
21 | |
22 // Override this to provide task-specific behavior. | |
23 virtual void Run() = 0; | |
24 | |
25 private: | |
26 DISALLOW_COPY_AND_ASSIGN(Task); | |
27 }; | |
28 | |
29 ThreadPool(); | |
30 | |
31 // Shuts down this thread pool. Causes workers to terminate | |
32 // themselves when they are active again. | |
33 ~ThreadPool(); | |
34 | |
35 // Runs a task on the thread pool. | |
36 void Run(Task* task); | |
37 | |
38 // Some simple stats. | |
39 uint64_t workers_running() const { return count_running_; } | |
40 uint64_t workers_idle() const { return count_idle_; } | |
41 uint64_t workers_started() const { return count_started_; } | |
42 uint64_t workers_stopped() const { return count_stopped_; } | |
43 | |
44 private: | |
45 class Worker { | |
46 public: | |
47 explicit Worker(ThreadPool* pool); | |
48 | |
49 // Starts the thread for the worker. Call Run() first. | |
Ivan Posva
2012/03/14 18:51:26
This comment "Call Run() first." is unclear.
turnidge
2012/03/14 21:00:27
Done.
| |
50 void StartThread(); | |
51 | |
52 // Runs a task on the worker. | |
53 void Run(Task* task); | |
Ivan Posva
2012/03/14 18:51:26
Maybe SetTask(task)?
turnidge
2012/03/14 21:00:27
Done.
| |
54 | |
55 // Main loop for a worker. | |
56 void Loop(); | |
57 | |
58 // Causes worker to terminate eventually. | |
59 void Shutdown(); | |
60 | |
61 private: | |
62 friend class ThreadPool; | |
63 | |
64 // The main entry point for new worker threads. | |
65 static void Main(uword args); | |
66 | |
67 // Fields owned by Worker. | |
68 Monitor monitor_; | |
69 ThreadPool* pool_; | |
70 Task* task_; | |
71 bool done_; | |
72 | |
73 // Fields owned by ThreadPool. Workers should not look at these | |
74 // directly. It's like looking at the sun. | |
75 bool owned_; // Protected by ThreadPool::mutex_ | |
76 Worker* idle_next_; // Protected by ThreadPool::mutex_ | |
77 Worker* all_next_; // Protected by ThreadPool::mutex_ | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(Worker); | |
80 }; | |
81 | |
82 void Shutdown(); | |
83 | |
84 // Expensive. Use only in assertions. | |
85 bool IsIdle(Worker* worker); | |
86 | |
87 bool RemoveWorkerFromIdleList(Worker* worker); | |
88 bool RemoveWorkerFromAllList(Worker* worker); | |
89 | |
90 // Worker operations. | |
91 void SetIdle(Worker* worker); | |
92 bool ReleaseIdleWorker(Worker* worker); | |
93 | |
94 Mutex mutex_; | |
95 bool shutting_down_; | |
96 Worker* all_workers_; | |
97 Worker* idle_workers_; | |
98 uint64_t count_started_; | |
99 uint64_t count_stopped_; | |
100 uint64_t count_running_; | |
101 uint64_t count_idle_; | |
102 | |
103 DISALLOW_COPY_AND_ASSIGN(ThreadPool); | |
104 }; | |
105 | |
106 } // namespace dart | |
107 | |
108 #endif // VM_THREAD_POOL_H_ | |
OLD | NEW |