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 private: | |
14 class Worker; | |
siva
2012/03/06 05:07:00
Why is this forward declaration needed? I don't se
turnidge
2012/03/06 22:34:01
I used to have a task->worker pointer, but I don't
| |
15 | |
16 public: | |
17 // Subclasses of Task are able to run on a ThreadPool. | |
18 class Task { | |
19 protected: | |
20 Task(); | |
21 | |
22 public: | |
23 virtual ~Task(); | |
24 | |
25 // Override this to provide task-specific behavior. | |
26 virtual void Run() = 0; | |
27 | |
28 private: | |
29 DISALLOW_COPY_AND_ASSIGN(Task); | |
30 }; | |
31 | |
32 ThreadPool(); | |
33 | |
34 // Shuts down this thread pool. Causes workers to terminate | |
35 // themselves when they are active again. | |
36 ~ThreadPool(); | |
37 | |
38 // Runs a task on the thread pool. | |
39 void Run(Task* task); | |
40 | |
41 // Some simple stats. | |
42 uint64_t workers_started() const { return workers_started_; } | |
43 uint64_t workers_stopped() const { return workers_stopped_; } | |
siva
2012/03/06 05:07:00
workers_active() and workers_idle() would also be
turnidge
2012/03/06 22:34:01
Added workers_running() and workers_idle().
| |
44 | |
45 private: | |
46 class Worker { | |
47 public: | |
48 explicit Worker(ThreadPool* pool); | |
49 ~Worker(); | |
50 | |
51 // Runs a task on the worker. | |
52 void Run(Task* task); | |
53 | |
54 // Main loop for a worker. | |
55 void Loop(); | |
56 | |
57 // Causes worker to terminate eventually. | |
58 void Shutdown(); | |
59 | |
60 private: | |
61 friend class ThreadPool; | |
62 | |
63 Monitor* monitor_; | |
siva
2012/03/06 05:07:00
Why not use a value object for the monitor?
turnidge
2012/03/06 22:34:01
Done.
| |
64 ThreadPool* pool_; | |
65 Task* task_; | |
66 bool done_; | |
67 | |
68 // Fields owned by ThreadPool. | |
69 Worker* next_; // Protected by ThreadPool::mutex_ | |
70 Worker* prev_; // Protected by ThreadPool::mutex_ | |
siva
2012/03/06 05:07:00
Do we need a doubly linked list for this? a singly
turnidge
2012/03/06 22:34:01
Changed the code to use two singly-linked lists.
| |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(Worker); | |
73 }; | |
74 | |
75 void Shutdown(); | |
76 | |
77 // List primitives. | |
78 void RemoveWorkerFromList(Worker* worker, Worker** head); | |
79 void AddWorkerToList(Worker* worker, Worker** head); | |
80 | |
81 // Worker operations. | |
82 Worker* GetIdleWorker(); | |
83 void SetIdle(Worker* worker); | |
84 bool RemoveIdleWorker(Worker* worker); | |
85 void ShutdownWorkerList(Worker* head); | |
86 | |
87 // The main entry point for new worker threads. | |
88 static void Main(uword args); | |
89 | |
90 Mutex* mutex_; | |
siva
2012/03/06 05:07:00
why not use a valueobject for the mutex?
turnidge
2012/03/06 22:34:01
Done.
| |
91 bool done_; | |
siva
2012/03/06 05:07:00
would 'bool shutting_down_;' be more readable?
Usi
turnidge
2012/03/06 22:34:01
Done.
| |
92 Worker* idle_workers_; | |
93 Worker* running_workers_; | |
94 uint64_t workers_started_; | |
95 uint64_t workers_stopped_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(ThreadPool); | |
98 }; | |
99 | |
100 } // namespace dart | |
101 | |
102 #endif // VM_THREAD_POOL_H_ | |
OLD | NEW |