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_; } |
| 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 // Fields owned by ThreadPool. |
| 46 enum WorkerState { |
| 47 kInvalid, |
| 48 kRunning, |
| 49 kIdle, |
| 50 kDone, |
| 51 }; |
| 52 |
| 53 class Worker { |
| 54 public: |
| 55 explicit Worker(ThreadPool* pool); |
| 56 |
| 57 // Runs a task on the worker. |
| 58 void Run(Task* task); |
| 59 |
| 60 // Main loop for a worker. |
| 61 void Loop(); |
| 62 |
| 63 // Causes worker to terminate eventually. |
| 64 void Shutdown(); |
| 65 |
| 66 private: |
| 67 friend class ThreadPool; |
| 68 |
| 69 // The main entry point for new worker threads. |
| 70 static void Main(uword args); |
| 71 |
| 72 // Fields owned by Worker. |
| 73 Monitor monitor_; |
| 74 ThreadPool* pool_; |
| 75 Task* task_; |
| 76 bool started_; |
| 77 bool done_; |
| 78 |
| 79 // Fields owned by ThreadPool. |
| 80 WorkerState state_; // Protected by ThreadPool::mutex_ |
| 81 Worker* idle_next_; // Protected by ThreadPool::mutex_ |
| 82 Worker* all_next_; // Protected by ThreadPool::mutex_ |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(Worker); |
| 85 }; |
| 86 |
| 87 Worker* GetIdleWorker(); |
| 88 void Shutdown(); |
| 89 |
| 90 bool RemoveWorkerFromAllList(Worker* worker); |
| 91 bool RemoveWorkerFromIdleList(Worker* worker); |
| 92 |
| 93 // Worker operations. |
| 94 void SetIdle(Worker* worker); |
| 95 bool ReleaseIdleWorker(Worker* worker); |
| 96 |
| 97 Mutex mutex_; |
| 98 bool shutting_down_; |
| 99 Worker* all_workers_; |
| 100 Worker* idle_workers_; |
| 101 uint64_t count_started_; |
| 102 uint64_t count_stopped_; |
| 103 uint64_t count_running_; |
| 104 uint64_t count_idle_; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(ThreadPool); |
| 107 }; |
| 108 |
| 109 } // namespace dart |
| 110 |
| 111 #endif // VM_THREAD_POOL_H_ |
OLD | NEW |