OLD | NEW |
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 #ifndef VM_THREAD_POOL_H_ | 5 #ifndef VM_THREAD_POOL_H_ |
6 #define VM_THREAD_POOL_H_ | 6 #define VM_THREAD_POOL_H_ |
7 | 7 |
8 #include "vm/globals.h" | 8 #include "vm/globals.h" |
9 #include "vm/os_thread.h" | 9 #include "vm/os_thread.h" |
10 | 10 |
(...skipping 25 matching lines...) Expand all Loading... |
36 // Runs a task on the thread pool. | 36 // Runs a task on the thread pool. |
37 void Run(Task* task); | 37 void Run(Task* task); |
38 | 38 |
39 // Some simple stats. | 39 // Some simple stats. |
40 uint64_t workers_running() const { return count_running_; } | 40 uint64_t workers_running() const { return count_running_; } |
41 uint64_t workers_idle() const { return count_idle_; } | 41 uint64_t workers_idle() const { return count_idle_; } |
42 uint64_t workers_started() const { return count_started_; } | 42 uint64_t workers_started() const { return count_started_; } |
43 uint64_t workers_stopped() const { return count_stopped_; } | 43 uint64_t workers_stopped() const { return count_stopped_; } |
44 | 44 |
45 private: | 45 private: |
46 friend class ThreadPoolTestPeer; | |
47 | |
48 class Worker { | 46 class Worker { |
49 public: | 47 public: |
50 explicit Worker(ThreadPool* pool); | 48 explicit Worker(ThreadPool* pool); |
51 | 49 |
52 // Sets a task on the worker. | 50 // Sets a task on the worker. |
53 void SetTask(Task* task); | 51 void SetTask(Task* task); |
54 | 52 |
55 // Starts the thread for the worker. This should only be called | 53 // Starts the thread for the worker. This should only be called |
56 // after a task has been set by the initial call to SetTask(). | 54 // after a task has been set by the initial call to SetTask(). |
57 void StartThread(); | 55 void StartThread(); |
58 | 56 |
59 // Main loop for a worker. | 57 // Main loop for a worker. Returns true if worker is removed from thread |
60 void Loop(); | 58 // lists, false otherwise. |
| 59 bool Loop(); |
61 | 60 |
62 // Causes worker to terminate eventually. | 61 // Causes worker to terminate eventually. |
63 void Shutdown(); | 62 bool Shutdown(); |
| 63 |
| 64 // Get the Worker's thread id. |
| 65 ThreadId id() { return id_; } |
64 | 66 |
65 private: | 67 private: |
66 friend class ThreadPool; | 68 friend class ThreadPool; |
67 | 69 |
68 // The main entry point for new worker threads. | 70 // The main entry point for new worker threads. |
69 static void Main(uword args); | 71 static void Main(uword args); |
70 | 72 |
71 bool IsDone() const { return pool_ == NULL; } | 73 bool IsDone() const { return pool_ == NULL; } |
72 | 74 |
73 // Fields owned by Worker. | 75 // Fields owned by Worker. |
74 Monitor monitor_; | 76 Monitor monitor_; |
75 ThreadPool* pool_; | 77 ThreadPool* pool_; |
76 Task* task_; | 78 Task* task_; |
| 79 ThreadId id_; |
| 80 bool started_; |
77 | 81 |
78 // Fields owned by ThreadPool. Workers should not look at these | 82 // Fields owned by ThreadPool. Workers should not look at these |
79 // directly. It's like looking at the sun. | 83 // directly. It's like looking at the sun. |
80 bool owned_; // Protected by ThreadPool::mutex_ | 84 bool owned_; // Protected by ThreadPool::mutex_ |
81 Worker* all_next_; // Protected by ThreadPool::mutex_ | 85 Worker* all_next_; // Protected by ThreadPool::mutex_ |
82 Worker* idle_next_; // Protected by ThreadPool::mutex_ | 86 Worker* idle_next_; // Protected by ThreadPool::mutex_ |
| 87 Worker* shutdown_next_; // Protected by ThreadPool::exit_monitor |
83 | 88 |
84 DISALLOW_COPY_AND_ASSIGN(Worker); | 89 DISALLOW_COPY_AND_ASSIGN(Worker); |
85 }; | 90 }; |
86 | 91 |
87 void Shutdown(); | 92 void Shutdown(); |
88 | 93 |
89 // Expensive. Use only in assertions. | 94 // Expensive. Use only in assertions. |
90 bool IsIdle(Worker* worker); | 95 bool IsIdle(Worker* worker); |
91 | 96 |
92 bool RemoveWorkerFromIdleList(Worker* worker); | 97 bool RemoveWorkerFromIdleList(Worker* worker); |
93 bool RemoveWorkerFromAllList(Worker* worker); | 98 bool RemoveWorkerFromAllList(Worker* worker); |
94 | 99 |
| 100 static void AddWorkerToShutdownList(Worker* worker); |
| 101 static bool RemoveWorkerFromShutdownList(Worker* worker); |
| 102 |
95 // Worker operations. | 103 // Worker operations. |
96 void SetIdle(Worker* worker); | 104 void SetIdle(Worker* worker); |
97 bool ReleaseIdleWorker(Worker* worker); | 105 bool ReleaseIdleWorker(Worker* worker); |
98 | 106 |
99 Mutex mutex_; | 107 Mutex mutex_; |
100 bool shutting_down_; | 108 bool shutting_down_; |
101 Worker* all_workers_; | 109 Worker* all_workers_; |
102 Worker* idle_workers_; | 110 Worker* idle_workers_; |
103 uint64_t count_started_; | 111 uint64_t count_started_; |
104 uint64_t count_stopped_; | 112 uint64_t count_stopped_; |
105 uint64_t count_running_; | 113 uint64_t count_running_; |
106 uint64_t count_idle_; | 114 uint64_t count_idle_; |
107 | 115 |
108 static Monitor* exit_monitor_; // Used only in testing. | 116 static Monitor exit_monitor_; |
109 static int* exit_count_; // Used only in testing. | 117 static Worker* shutting_down_workers_; |
110 | 118 |
111 DISALLOW_COPY_AND_ASSIGN(ThreadPool); | 119 DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
112 }; | 120 }; |
113 | 121 |
114 } // namespace dart | 122 } // namespace dart |
115 | 123 |
116 #endif // VM_THREAD_POOL_H_ | 124 #endif // VM_THREAD_POOL_H_ |
OLD | NEW |