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