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

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

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

Powered by Google App Engine
This is Rietveld 408576698