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

Side by Side Diff: base/threading/simple_thread.h

Issue 2204333003: Add joinable option to SimpleThread::Options as was just done for Thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@b1_nonjoinable_thread
Patch Set: Created 4 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
« no previous file with comments | « no previous file | base/threading/simple_thread.cc » ('j') | base/threading/simple_thread.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // WARNING: You should probably be using Thread (thread.h) instead. Thread is 5 // WARNING: You should probably be using Thread (thread.h) instead. Thread is
6 // Chrome's message-loop based Thread abstraction, and if you are a 6 // Chrome's message-loop based Thread abstraction, and if you are a
7 // thread running in the browser, there will likely be assumptions 7 // thread running in the browser, there will likely be assumptions
8 // that your thread will have an associated message loop. 8 // that your thread will have an associated message loop.
9 // 9 //
10 // This is a simple thread interface that backs to a native operating system 10 // This is a simple thread interface that backs to a native operating system
(...skipping 30 matching lines...) Expand all
41 #define BASE_THREADING_SIMPLE_THREAD_H_ 41 #define BASE_THREADING_SIMPLE_THREAD_H_
42 42
43 #include <stddef.h> 43 #include <stddef.h>
44 44
45 #include <queue> 45 #include <queue>
46 #include <string> 46 #include <string>
47 #include <vector> 47 #include <vector>
48 48
49 #include "base/base_export.h" 49 #include "base/base_export.h"
50 #include "base/compiler_specific.h" 50 #include "base/compiler_specific.h"
51 #include "base/macros.h"
51 #include "base/synchronization/lock.h" 52 #include "base/synchronization/lock.h"
52 #include "base/synchronization/waitable_event.h" 53 #include "base/synchronization/waitable_event.h"
53 #include "base/threading/platform_thread.h" 54 #include "base/threading/platform_thread.h"
54 55
55 namespace base { 56 namespace base {
56 57
57 // This is the base SimpleThread. You can derive from it and implement the 58 // This is the base SimpleThread. You can derive from it and implement the
58 // virtual Run method, or you can use the DelegateSimpleThread interface. 59 // virtual Run method, or you can use the DelegateSimpleThread interface.
59 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate { 60 class BASE_EXPORT SimpleThread : public PlatformThread::Delegate {
60 public: 61 public:
61 class BASE_EXPORT Options { 62 struct BASE_EXPORT Options {
62 public: 63 public:
63 Options() : stack_size_(0), priority_(ThreadPriority::NORMAL) {} 64 Options() = default;
64 explicit Options(ThreadPriority priority) 65 explicit Options(ThreadPriority priority_in) : priority(priority_in) {}
65 : stack_size_(0), priority_(priority) {} 66 ~Options() = default;
66 ~Options() {}
67 67
68 // We use the standard compiler-supplied copy constructor. 68 // Allow copies.
69 Options(const Options& other) = default;
70 Options& operator=(const Options& other) = default;
69 71
70 // A custom stack size, or 0 for the system default. 72 // A custom stack size, or 0 for the system default.
71 void set_stack_size(size_t size) { stack_size_ = size; } 73 size_t stack_size = 0;
72 size_t stack_size() const { return stack_size_; }
73 74
74 // A custom thread priority. 75 ThreadPriority priority = ThreadPriority::NORMAL;
75 void set_priority(ThreadPriority priority) { priority_ = priority; } 76
76 ThreadPriority priority() const { return priority_; } 77 // If false, the thread's PlatformThreadHandle will not be kept around and
77 private: 78 // the SimpleThread instance will not be required to be Join()'ed before
78 size_t stack_size_; 79 // being destroyed
79 ThreadPriority priority_; 80 bool joinable = true;
80 }; 81 };
81 82
82 // Create a SimpleThread. |options| should be used to manage any specific 83 // Create a SimpleThread. |options| should be used to manage any specific
83 // configuration involving the thread creation and management. 84 // configuration involving the thread creation and management.
84 // Every thread has a name, in the form of |name_prefix|/TID, for example 85 // Every thread has a name, in the form of |name_prefix|/TID, for example
85 // "my_thread/321". The thread will not be created until Start() is called. 86 // "my_thread/321". The thread will not be created until Start() is called.
86 explicit SimpleThread(const std::string& name_prefix); 87 explicit SimpleThread(const std::string& name_prefix);
87 SimpleThread(const std::string& name_prefix, const Options& options); 88 SimpleThread(const std::string& name_prefix, const Options& options);
88 89
89 ~SimpleThread() override; 90 ~SimpleThread() override;
90 91
91 virtual void Start(); 92 virtual void Start();
92 virtual void Join(); 93 virtual void Join();
93 94
94 // Subclasses should override the Run method. 95 // Subclasses should override the Run method.
95 virtual void Run() = 0; 96 virtual void Run() = 0;
96 97
97 // Return the thread name prefix, or "unnamed" if none was supplied. 98 // Return the thread name prefix, or "unnamed" if none was supplied.
98 std::string name_prefix() { return name_prefix_; } 99 std::string name_prefix() { return name_prefix_; }
99 100
100 // Return the completed name including TID, only valid after Start(). 101 // Return the completed name including TID, only valid after Start().
101 std::string name() { return name_; } 102 std::string name() { return name_; }
102 103
103 // Return the thread id, only valid after Start(). 104 // Return the thread id, only valid after Start().
104 PlatformThreadId tid() { return tid_; } 105 PlatformThreadId tid() { return tid_; }
105 106
106 // Return True if Start() has ever been called. 107 // Return True if Start() has ever been called.
107 bool HasBeenStarted(); 108 bool HasBeenStarted();
108 109
109 // Return True if Join() has evern been called. 110 // Return True if Join() has ever been called.
110 bool HasBeenJoined() { return joined_; } 111 bool HasBeenJoined() { return joined_; }
111 112
112 // Overridden from PlatformThread::Delegate: 113 // Overridden from PlatformThread::Delegate:
113 void ThreadMain() override; 114 void ThreadMain() override;
114 115
115 private: 116 private:
116 const std::string name_prefix_; 117 const std::string name_prefix_;
117 std::string name_; 118 std::string name_;
118 const Options options_; 119 const Options options_;
119 PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join! 120 PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join!
120 WaitableEvent event_; // Signaled if Start() was ever called. 121 WaitableEvent event_; // Signaled if Start() was ever called.
121 PlatformThreadId tid_; // The backing thread's id. 122 PlatformThreadId tid_ = 0; // The backing thread's id.
Lei Zhang 2016/08/03 23:24:12 Maybe init to kInvalidThreadId? (which is still 0)
gab 2016/08/04 14:35:01 Good point, done.
122 bool joined_; // True if Join has been called. 123 bool joined_ = false; // True if Join has been called.
124
125 DISALLOW_COPY_AND_ASSIGN(SimpleThread);
123 }; 126 };
124 127
125 class BASE_EXPORT DelegateSimpleThread : public SimpleThread { 128 class BASE_EXPORT DelegateSimpleThread : public SimpleThread {
126 public: 129 public:
127 class BASE_EXPORT Delegate { 130 class BASE_EXPORT Delegate {
128 public: 131 public:
129 Delegate() { } 132 Delegate() { }
130 virtual ~Delegate() { } 133 virtual ~Delegate() { }
131 virtual void Run() = 0; 134 virtual void Run() = 0;
132 }; 135 };
133 136
134 DelegateSimpleThread(Delegate* delegate, 137 DelegateSimpleThread(Delegate* delegate,
135 const std::string& name_prefix); 138 const std::string& name_prefix);
136 DelegateSimpleThread(Delegate* delegate, 139 DelegateSimpleThread(Delegate* delegate,
137 const std::string& name_prefix, 140 const std::string& name_prefix,
138 const Options& options); 141 const Options& options);
139 142
140 ~DelegateSimpleThread() override; 143 ~DelegateSimpleThread() override;
141 void Run() override; 144 void Run() override;
142 145
143 private: 146 private:
144 Delegate* delegate_; 147 Delegate* delegate_;
148
149 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThread);
145 }; 150 };
146 151
147 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, 152 // DelegateSimpleThreadPool allows you to start up a fixed number of threads,
148 // and then add jobs which will be dispatched to the threads. This is 153 // and then add jobs which will be dispatched to the threads. This is
149 // convenient when you have a lot of small work that you want done 154 // convenient when you have a lot of small work that you want done
150 // multi-threaded, but don't want to spawn a thread for each small bit of work. 155 // multi-threaded, but don't want to spawn a thread for each small bit of work.
151 // 156 //
152 // You just call AddWork() to add a delegate to the list of work to be done. 157 // You just call AddWork() to add a delegate to the list of work to be done.
153 // JoinAll() will make sure that all outstanding work is processed, and wait 158 // JoinAll() will make sure that all outstanding work is processed, and wait
154 // for everything to finish. You can reuse a pool, so you can call Start() 159 // for everything to finish. You can reuse a pool, so you can call Start()
(...skipping 24 matching lines...) Expand all
179 // We implement the Delegate interface, for running our internal threads. 184 // We implement the Delegate interface, for running our internal threads.
180 void Run() override; 185 void Run() override;
181 186
182 private: 187 private:
183 const std::string name_prefix_; 188 const std::string name_prefix_;
184 int num_threads_; 189 int num_threads_;
185 std::vector<DelegateSimpleThread*> threads_; 190 std::vector<DelegateSimpleThread*> threads_;
186 std::queue<Delegate*> delegates_; 191 std::queue<Delegate*> delegates_;
187 base::Lock lock_; // Locks delegates_ 192 base::Lock lock_; // Locks delegates_
188 WaitableEvent dry_; // Not signaled when there is no work to do. 193 WaitableEvent dry_; // Not signaled when there is no work to do.
194
195 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThreadPool);
Lei Zhang 2016/08/03 23:24:11 +3 :)
gab 2016/08/04 14:35:01 :-)
189 }; 196 };
190 197
191 } // namespace base 198 } // namespace base
192 199
193 #endif // BASE_THREADING_SIMPLE_THREAD_H_ 200 #endif // BASE_THREADING_SIMPLE_THREAD_H_
OLDNEW
« no previous file with comments | « no previous file | base/threading/simple_thread.cc » ('j') | base/threading/simple_thread.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698