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_; } | |
Ivan Posva
2012/03/15 00:27:34
I missed this in the first go around. Please do no
| |
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 friend class ThreadPoolTestPeer; | |
46 | |
47 class Worker { | |
48 public: | |
49 explicit Worker(ThreadPool* pool); | |
50 | |
51 // Sets a task on the worker. | |
52 void SetTask(Task* task); | |
53 | |
54 // Starts the thread for the worker. This should only be called | |
55 // after a task has been set by the initial call to SetTask(). | |
56 void StartThread(); | |
57 | |
58 // Main loop for a worker. | |
59 void Loop(); | |
60 | |
61 // Causes worker to terminate eventually. | |
62 void Shutdown(); | |
63 | |
64 private: | |
65 friend class ThreadPool; | |
66 | |
67 // The main entry point for new worker threads. | |
68 static void Main(uword args); | |
69 | |
70 bool IsDone() const { return pool_ == NULL; } | |
71 | |
72 // Fields owned by Worker. | |
73 Monitor monitor_; | |
74 ThreadPool* pool_; | |
75 Task* task_; | |
76 | |
77 // Fields owned by ThreadPool. Workers should not look at these | |
78 // directly. It's like looking at the sun. | |
79 bool owned_; // Protected by ThreadPool::mutex_ | |
80 Worker* idle_next_; // Protected by ThreadPool::mutex_ | |
81 Worker* all_next_; // Protected by ThreadPool::mutex_ | |
82 | |
83 DISALLOW_COPY_AND_ASSIGN(Worker); | |
84 }; | |
85 | |
86 void Shutdown(); | |
87 | |
88 // Expensive. Use only in assertions. | |
89 bool IsIdle(Worker* worker); | |
90 | |
91 bool RemoveWorkerFromIdleList(Worker* worker); | |
92 bool RemoveWorkerFromAllList(Worker* worker); | |
93 | |
94 // Worker operations. | |
95 void SetIdle(Worker* worker); | |
96 bool ReleaseIdleWorker(Worker* worker); | |
97 | |
98 Mutex mutex_; | |
99 bool shutting_down_; | |
100 Worker* all_workers_; | |
101 Worker* idle_workers_; | |
102 uint64_t count_started_; | |
103 uint64_t count_stopped_; | |
104 uint64_t count_running_; | |
105 uint64_t count_idle_; | |
106 | |
107 static Monitor* exit_monitor_; // Used only in testing. | |
108 static int* exit_count_; // Used only in testing. | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(ThreadPool); | |
111 }; | |
112 | |
113 } // namespace dart | |
114 | |
115 #endif // VM_THREAD_POOL_H_ | |
OLD | NEW |