Chromium Code Reviews| Index: runtime/vm/thread_pool.h |
| diff --git a/runtime/vm/thread_pool.h b/runtime/vm/thread_pool.h |
| index 792aef7b23898d4cda77cf6345f11b6dfe0ff770..3bd555ee9c95ba9f6f7ddc58e9ca7d31aea16c6d 100644 |
| --- a/runtime/vm/thread_pool.h |
| +++ b/runtime/vm/thread_pool.h |
| @@ -5,11 +5,14 @@ |
| #ifndef VM_THREAD_POOL_H_ |
| #define VM_THREAD_POOL_H_ |
| +#include "vm/allocation.h" |
| #include "vm/globals.h" |
| #include "vm/os_thread.h" |
| namespace dart { |
| +class ReaperTask; |
| + |
| class ThreadPool { |
| public: |
| // Subclasses of Task are able to run on a ThreadPool. |
| @@ -29,7 +32,7 @@ class ThreadPool { |
| ThreadPool(); |
| - // Shuts down this thread pool. Causes workers to terminate |
| + // Shuts down this thread pool. Causes workers to terminate |
| // themselves when they are active again. |
| ~ThreadPool(); |
| @@ -43,7 +46,7 @@ class ThreadPool { |
| uint64_t workers_stopped() const { return count_stopped_; } |
| private: |
| - friend class ThreadPoolTestPeer; |
| + friend class ReaperTask; |
| class Worker { |
| public: |
| @@ -56,24 +59,31 @@ class ThreadPool { |
| // after a task has been set by the initial call to SetTask(). |
| void StartThread(); |
| - // Main loop for a worker. |
| - void Loop(); |
| + // Main loop for a worker. Returns true if worker is removed from thread |
| + // lists, false otherwise. |
| + bool Loop(); |
| // Causes worker to terminate eventually. |
| void Shutdown(); |
| + // Get the Worker's thread id. |
| + ThreadId id() { return id_; } |
| + |
| private: |
| friend class ThreadPool; |
| // The main entry point for new worker threads. |
| static void Main(uword args); |
| - bool IsDone() const { return pool_ == NULL; } |
| + bool IsDone() const { return done_; } |
| // Fields owned by Worker. |
| Monitor monitor_; |
| ThreadPool* pool_; |
| + bool done_; |
|
Ivan Posva
2015/08/17 13:35:52
Please move bool fields together.
zra
2015/08/18 06:23:14
Done.
|
| Task* task_; |
| + ThreadId id_; |
| + bool started_; |
| // Fields owned by ThreadPool. Workers should not look at these |
| // directly. It's like looking at the sun. |
| @@ -81,9 +91,30 @@ class ThreadPool { |
| Worker* all_next_; // Protected by ThreadPool::mutex_ |
| Worker* idle_next_; // Protected by ThreadPool::mutex_ |
| + Worker* shutdown_next_; // Protected by ThreadPool::exit_monitor |
| + |
| DISALLOW_COPY_AND_ASSIGN(Worker); |
| }; |
| + class JoinList { |
| + public: |
| + explicit JoinList(ThreadId id, JoinList* next) : id_(id), next_(next) { |
| + } |
| + |
| + static void Add(ThreadId id, JoinList** list); |
| + |
| + static void Join(JoinList** list); |
| + |
| + ThreadId id() const { return id_; } |
| + JoinList* next() const { return next_; } |
| + |
| + private: |
| + ThreadId id_; |
| + JoinList* next_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(JoinList); |
| + }; |
| + |
| void Shutdown(); |
| // Expensive. Use only in assertions. |
| @@ -92,6 +123,11 @@ class ThreadPool { |
| bool RemoveWorkerFromIdleList(Worker* worker); |
| bool RemoveWorkerFromAllList(Worker* worker); |
| + void AddWorkerToShutdownList(Worker* worker); |
| + bool RemoveWorkerFromShutdownList(Worker* worker); |
| + |
| + void ReapExitedIdleThreads(); |
| + |
| // Worker operations. |
| void SetIdle(Worker* worker); |
| bool ReleaseIdleWorker(Worker* worker); |
| @@ -104,9 +140,11 @@ class ThreadPool { |
| uint64_t count_stopped_; |
| uint64_t count_running_; |
| uint64_t count_idle_; |
| + JoinList* idle_join_list_; |
| - static Monitor* exit_monitor_; // Used only in testing. |
| - static int* exit_count_; // Used only in testing. |
| + Monitor exit_monitor_; |
| + Worker* shutting_down_workers_; |
| + JoinList* join_list_; |
| DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
| }; |