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

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: fix TSan and ASan 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') | no next file with comments »
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 underlying thread's PlatformThreadHandle will not be kept
77 private: 78 // around and as such the SimpleThread instance will not be Join()able and
78 size_t stack_size_; 79 // must not be deleted before Run() is invoked. After that, it's up to
79 ThreadPriority priority_; 80 // the subclass to determine when it is safe to delete itself.
81 bool joinable = true;
80 }; 82 };
81 83
82 // Create a SimpleThread. |options| should be used to manage any specific 84 // Create a SimpleThread. |options| should be used to manage any specific
83 // configuration involving the thread creation and management. 85 // configuration involving the thread creation and management.
84 // Every thread has a name, in the form of |name_prefix|/TID, for example 86 // 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. 87 // "my_thread/321". The thread will not be created until Start() is called.
86 explicit SimpleThread(const std::string& name_prefix); 88 explicit SimpleThread(const std::string& name_prefix);
87 SimpleThread(const std::string& name_prefix, const Options& options); 89 SimpleThread(const std::string& name_prefix, const Options& options);
88 90
89 ~SimpleThread() override; 91 ~SimpleThread() override;
90 92
91 virtual void Start(); 93 virtual void Start();
92 virtual void Join(); 94 virtual void Join();
93 95
94 // Subclasses should override the Run method. 96 // Subclasses should override the Run method.
95 virtual void Run() = 0; 97 virtual void Run() = 0;
96 98
97 // Return the thread name prefix, or "unnamed" if none was supplied. 99 // Return the thread name prefix, or "unnamed" if none was supplied.
98 std::string name_prefix() { return name_prefix_; } 100 std::string name_prefix() { return name_prefix_; }
99 101
100 // Return the completed name including TID, only valid after Start(). 102 // Return the completed name including TID, only valid after Start().
101 std::string name() { return name_; } 103 std::string name() { return name_; }
102 104
103 // Return the thread id, only valid after Start(). 105 // Return the thread id, only valid after Start().
104 PlatformThreadId tid() { return tid_; } 106 PlatformThreadId tid() { return tid_; }
105 107
106 // Return True if Start() has ever been called. 108 // Return True if Start() has ever been called.
107 bool HasBeenStarted(); 109 bool HasBeenStarted();
108 110
109 // Return True if Join() has evern been called. 111 // Return True if Join() has ever been called.
110 bool HasBeenJoined() { return joined_; } 112 bool HasBeenJoined() { return joined_; }
111 113
112 // Overridden from PlatformThread::Delegate: 114 // Overridden from PlatformThread::Delegate:
113 void ThreadMain() override; 115 void ThreadMain() override;
114 116
115 private: 117 private:
116 const std::string name_prefix_; 118 const std::string name_prefix_;
117 std::string name_; 119 std::string name_;
118 const Options options_; 120 const Options options_;
119 PlatformThreadHandle thread_; // PlatformThread handle, invalid after Join! 121 PlatformThreadHandle thread_; // PlatformThread handle, reset after Join.
120 WaitableEvent event_; // Signaled if Start() was ever called. 122 WaitableEvent event_; // Signaled if Start() was ever called.
121 PlatformThreadId tid_; // The backing thread's id. 123 PlatformThreadId tid_ = kInvalidThreadId; // The backing thread's id.
122 bool joined_; // True if Join has been called. 124 bool joined_ = false; // True if Join has been called.
125
126 DISALLOW_COPY_AND_ASSIGN(SimpleThread);
123 }; 127 };
124 128
129 // A SimpleThread which delegates Run() to its Delegate. Non-joinable
130 // DelegateSimpleThread are safe to delete after Run() was invoked, their
131 // Delegates are also safe to delete after that point from this class' point of
132 // view (although implementations must of course make sure that Run() will not
133 // use their Delegate's member state after its deletion).
125 class BASE_EXPORT DelegateSimpleThread : public SimpleThread { 134 class BASE_EXPORT DelegateSimpleThread : public SimpleThread {
126 public: 135 public:
127 class BASE_EXPORT Delegate { 136 class BASE_EXPORT Delegate {
128 public: 137 public:
129 Delegate() { } 138 virtual ~Delegate() = default;
130 virtual ~Delegate() { }
131 virtual void Run() = 0; 139 virtual void Run() = 0;
132 }; 140 };
133 141
134 DelegateSimpleThread(Delegate* delegate, 142 DelegateSimpleThread(Delegate* delegate,
135 const std::string& name_prefix); 143 const std::string& name_prefix);
136 DelegateSimpleThread(Delegate* delegate, 144 DelegateSimpleThread(Delegate* delegate,
137 const std::string& name_prefix, 145 const std::string& name_prefix,
138 const Options& options); 146 const Options& options);
139 147
140 ~DelegateSimpleThread() override; 148 ~DelegateSimpleThread() override;
141 void Run() override; 149 void Run() override;
142 150
143 private: 151 private:
144 Delegate* delegate_; 152 Delegate* delegate_;
153
154 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThread);
145 }; 155 };
146 156
147 // DelegateSimpleThreadPool allows you to start up a fixed number of threads, 157 // 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 158 // 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 159 // 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. 160 // multi-threaded, but don't want to spawn a thread for each small bit of work.
151 // 161 //
152 // You just call AddWork() to add a delegate to the list of work to be done. 162 // 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 163 // 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() 164 // 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. 189 // We implement the Delegate interface, for running our internal threads.
180 void Run() override; 190 void Run() override;
181 191
182 private: 192 private:
183 const std::string name_prefix_; 193 const std::string name_prefix_;
184 int num_threads_; 194 int num_threads_;
185 std::vector<DelegateSimpleThread*> threads_; 195 std::vector<DelegateSimpleThread*> threads_;
186 std::queue<Delegate*> delegates_; 196 std::queue<Delegate*> delegates_;
187 base::Lock lock_; // Locks delegates_ 197 base::Lock lock_; // Locks delegates_
188 WaitableEvent dry_; // Not signaled when there is no work to do. 198 WaitableEvent dry_; // Not signaled when there is no work to do.
199
200 DISALLOW_COPY_AND_ASSIGN(DelegateSimpleThreadPool);
189 }; 201 };
190 202
191 } // namespace base 203 } // namespace base
192 204
193 #endif // BASE_THREADING_SIMPLE_THREAD_H_ 205 #endif // BASE_THREADING_SIMPLE_THREAD_H_
OLDNEW
« no previous file with comments | « no previous file | base/threading/simple_thread.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698