Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Side by Side Diff: runtime/vm/thread_pool.h

Issue 1275353005: VM thread shutdown. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
8 #include "vm/globals.h" 9 #include "vm/globals.h"
9 #include "vm/os_thread.h" 10 #include "vm/os_thread.h"
10 11
11 namespace dart { 12 namespace dart {
12 13
14 class ReaperTask;
15
13 class ThreadPool { 16 class ThreadPool {
14 public: 17 public:
15 // Subclasses of Task are able to run on a ThreadPool. 18 // Subclasses of Task are able to run on a ThreadPool.
16 class Task { 19 class Task {
17 protected: 20 protected:
18 Task(); 21 Task();
19 22
20 public: 23 public:
21 virtual ~Task(); 24 virtual ~Task();
22 25
23 // Override this to provide task-specific behavior. 26 // Override this to provide task-specific behavior.
24 virtual void Run() = 0; 27 virtual void Run() = 0;
25 28
26 private: 29 private:
27 DISALLOW_COPY_AND_ASSIGN(Task); 30 DISALLOW_COPY_AND_ASSIGN(Task);
28 }; 31 };
29 32
30 ThreadPool(); 33 ThreadPool();
31 34
32 // Shuts down this thread pool. Causes workers to terminate 35 // Shuts down this thread pool. Causes workers to terminate
33 // themselves when they are active again. 36 // themselves when they are active again.
34 ~ThreadPool(); 37 ~ThreadPool();
35 38
36 // Runs a task on the thread pool. 39 // Runs a task on the thread pool.
37 void Run(Task* task); 40 void Run(Task* task);
38 41
39 // Some simple stats. 42 // Some simple stats.
40 uint64_t workers_running() const { return count_running_; } 43 uint64_t workers_running() const { return count_running_; }
41 uint64_t workers_idle() const { return count_idle_; } 44 uint64_t workers_idle() const { return count_idle_; }
42 uint64_t workers_started() const { return count_started_; } 45 uint64_t workers_started() const { return count_started_; }
43 uint64_t workers_stopped() const { return count_stopped_; } 46 uint64_t workers_stopped() const { return count_stopped_; }
44 47
45 private: 48 private:
46 friend class ThreadPoolTestPeer; 49 friend class ReaperTask;
47 50
48 class Worker { 51 class Worker {
49 public: 52 public:
50 explicit Worker(ThreadPool* pool); 53 explicit Worker(ThreadPool* pool);
51 54
52 // Sets a task on the worker. 55 // Sets a task on the worker.
53 void SetTask(Task* task); 56 void SetTask(Task* task);
54 57
55 // Starts the thread for the worker. This should only be called 58 // Starts the thread for the worker. This should only be called
56 // after a task has been set by the initial call to SetTask(). 59 // after a task has been set by the initial call to SetTask().
57 void StartThread(); 60 void StartThread();
58 61
59 // Main loop for a worker. 62 // Main loop for a worker. Returns true if worker is removed from thread
60 void Loop(); 63 // lists, false otherwise.
64 bool Loop();
61 65
62 // Causes worker to terminate eventually. 66 // Causes worker to terminate eventually.
63 void Shutdown(); 67 void Shutdown();
64 68
69 // Get the Worker's thread id.
70 ThreadId id() { return id_; }
71
65 private: 72 private:
66 friend class ThreadPool; 73 friend class ThreadPool;
67 74
68 // The main entry point for new worker threads. 75 // The main entry point for new worker threads.
69 static void Main(uword args); 76 static void Main(uword args);
70 77
71 bool IsDone() const { return pool_ == NULL; } 78 bool IsDone() const { return done_; }
72 79
73 // Fields owned by Worker. 80 // Fields owned by Worker.
74 Monitor monitor_; 81 Monitor monitor_;
75 ThreadPool* pool_; 82 ThreadPool* pool_;
83 bool done_;
Ivan Posva 2015/08/17 13:35:52 Please move bool fields together.
zra 2015/08/18 06:23:14 Done.
76 Task* task_; 84 Task* task_;
85 ThreadId id_;
86 bool started_;
77 87
78 // Fields owned by ThreadPool. Workers should not look at these 88 // Fields owned by ThreadPool. Workers should not look at these
79 // directly. It's like looking at the sun. 89 // directly. It's like looking at the sun.
80 bool owned_; // Protected by ThreadPool::mutex_ 90 bool owned_; // Protected by ThreadPool::mutex_
81 Worker* all_next_; // Protected by ThreadPool::mutex_ 91 Worker* all_next_; // Protected by ThreadPool::mutex_
82 Worker* idle_next_; // Protected by ThreadPool::mutex_ 92 Worker* idle_next_; // Protected by ThreadPool::mutex_
83 93
94 Worker* shutdown_next_; // Protected by ThreadPool::exit_monitor
95
84 DISALLOW_COPY_AND_ASSIGN(Worker); 96 DISALLOW_COPY_AND_ASSIGN(Worker);
85 }; 97 };
86 98
99 class JoinList {
100 public:
101 explicit JoinList(ThreadId id, JoinList* next) : id_(id), next_(next) {
102 }
103
104 static void Add(ThreadId id, JoinList** list);
105
106 static void Join(JoinList** list);
107
108 ThreadId id() const { return id_; }
109 JoinList* next() const { return next_; }
110
111 private:
112 ThreadId id_;
113 JoinList* next_;
114
115 DISALLOW_COPY_AND_ASSIGN(JoinList);
116 };
117
87 void Shutdown(); 118 void Shutdown();
88 119
89 // Expensive. Use only in assertions. 120 // Expensive. Use only in assertions.
90 bool IsIdle(Worker* worker); 121 bool IsIdle(Worker* worker);
91 122
92 bool RemoveWorkerFromIdleList(Worker* worker); 123 bool RemoveWorkerFromIdleList(Worker* worker);
93 bool RemoveWorkerFromAllList(Worker* worker); 124 bool RemoveWorkerFromAllList(Worker* worker);
94 125
126 void AddWorkerToShutdownList(Worker* worker);
127 bool RemoveWorkerFromShutdownList(Worker* worker);
128
129 void ReapExitedIdleThreads();
130
95 // Worker operations. 131 // Worker operations.
96 void SetIdle(Worker* worker); 132 void SetIdle(Worker* worker);
97 bool ReleaseIdleWorker(Worker* worker); 133 bool ReleaseIdleWorker(Worker* worker);
98 134
99 Mutex mutex_; 135 Mutex mutex_;
100 bool shutting_down_; 136 bool shutting_down_;
101 Worker* all_workers_; 137 Worker* all_workers_;
102 Worker* idle_workers_; 138 Worker* idle_workers_;
103 uint64_t count_started_; 139 uint64_t count_started_;
104 uint64_t count_stopped_; 140 uint64_t count_stopped_;
105 uint64_t count_running_; 141 uint64_t count_running_;
106 uint64_t count_idle_; 142 uint64_t count_idle_;
143 JoinList* idle_join_list_;
107 144
108 static Monitor* exit_monitor_; // Used only in testing. 145 Monitor exit_monitor_;
109 static int* exit_count_; // Used only in testing. 146 Worker* shutting_down_workers_;
147 JoinList* join_list_;
110 148
111 DISALLOW_COPY_AND_ASSIGN(ThreadPool); 149 DISALLOW_COPY_AND_ASSIGN(ThreadPool);
112 }; 150 };
113 151
114 } // namespace dart 152 } // namespace dart
115 153
116 #endif // VM_THREAD_POOL_H_ 154 #endif // VM_THREAD_POOL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698