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

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

Issue 8427007: Thread::Stop() must be called before any subclass's destructor completes. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Latest version as trybot-ted Created 9 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « base/memory/weak_ptr_unittest.cc ('k') | base/threading/thread_unittest.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 #ifndef BASE_THREAD_H_ 5 #ifndef BASE_THREAD_H_
6 #define BASE_THREAD_H_ 6 #define BASE_THREAD_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 10
11 #include "base/base_export.h" 11 #include "base/base_export.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/message_loop_proxy.h" 13 #include "base/message_loop_proxy.h"
14 #include "base/threading/platform_thread.h" 14 #include "base/threading/platform_thread.h"
15 15
16 namespace base { 16 namespace base {
17 17
18 // A simple thread abstraction that establishes a MessageLoop on a new thread. 18 // A simple thread abstraction that establishes a MessageLoop on a new thread.
19 // The consumer uses the MessageLoop of the thread to cause code to execute on 19 // The consumer uses the MessageLoop of the thread to cause code to execute on
20 // the thread. When this object is destroyed the thread is terminated. All 20 // the thread. When this object is destroyed the thread is terminated. All
21 // pending tasks queued on the thread's message loop will run to completion 21 // pending tasks queued on the thread's message loop will run to completion
22 // before the thread is terminated. 22 // before the thread is terminated.
23 // 23 //
24 // NOTE: Subclasses must call Stop() in their destructor. See ~Thread below.
25 //
24 // After the thread is stopped, the destruction sequence is: 26 // After the thread is stopped, the destruction sequence is:
25 // 27 //
26 // (1) Thread::CleanUp() 28 // (1) Thread::CleanUp()
27 // (2) MessageLoop::~MessageLoop 29 // (2) MessageLoop::~MessageLoop
28 // (3.b) MessageLoop::DestructionObserver::WillDestroyCurrentMessageLoop 30 // (3.b) MessageLoop::DestructionObserver::WillDestroyCurrentMessageLoop
29 class BASE_EXPORT Thread : PlatformThread::Delegate { 31 class BASE_EXPORT Thread : PlatformThread::Delegate {
30 public: 32 public:
31 struct Options { 33 struct Options {
32 Options() : message_loop_type(MessageLoop::TYPE_DEFAULT), stack_size(0) {} 34 Options() : message_loop_type(MessageLoop::TYPE_DEFAULT), stack_size(0) {}
33 Options(MessageLoop::Type type, size_t size) 35 Options(MessageLoop::Type type, size_t size)
34 : message_loop_type(type), stack_size(size) {} 36 : message_loop_type(type), stack_size(size) {}
35 37
36 // Specifies the type of message loop that will be allocated on the thread. 38 // Specifies the type of message loop that will be allocated on the thread.
37 MessageLoop::Type message_loop_type; 39 MessageLoop::Type message_loop_type;
38 40
39 // Specifies the maximum stack size that the thread is allowed to use. 41 // Specifies the maximum stack size that the thread is allowed to use.
40 // This does not necessarily correspond to the thread's initial stack size. 42 // This does not necessarily correspond to the thread's initial stack size.
41 // A value of 0 indicates that the default maximum should be used. 43 // A value of 0 indicates that the default maximum should be used.
42 size_t stack_size; 44 size_t stack_size;
43 }; 45 };
44 46
45 // Constructor. 47 // Constructor.
46 // name is a display string to identify the thread. 48 // name is a display string to identify the thread.
47 explicit Thread(const char* name); 49 explicit Thread(const char* name);
48 50
49 // Destroys the thread, stopping it if necessary. 51 // Destroys the thread, stopping it if necessary.
50 // 52 //
51 // NOTE: If you are subclassing from Thread, and you wish for your CleanUp 53 // NOTE: All subclasses of Thread must call Stop() in their
52 // method to be called, then you need to call Stop() from your destructor. 54 // destructor, or otherwise ensure Stop() is called before the
53 // 55 // subclass is destructed. This is required to avoid a data race
56 // between the destructor modifying the vtable, and the thread's
57 // ThreadMain calling the virtual method Run. It also ensures that
58 // the CleanUp() virtual method is called on the subclass before it
59 // is destructed.
54 virtual ~Thread(); 60 virtual ~Thread();
55 61
56 // Starts the thread. Returns true if the thread was successfully started; 62 // Starts the thread. Returns true if the thread was successfully started;
57 // otherwise, returns false. Upon successful return, the message_loop() 63 // otherwise, returns false. Upon successful return, the message_loop()
58 // getter will return non-null. 64 // getter will return non-null.
59 // 65 //
60 // Note: This function can't be called on Windows with the loader lock held; 66 // Note: This function can't be called on Windows with the loader lock held;
61 // i.e. during a DllMain, global object construction or destruction, atexit() 67 // i.e. during a DllMain, global object construction or destruction, atexit()
62 // callback. 68 // callback.
63 bool Start(); 69 bool Start();
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 std::string name_; 188 std::string name_;
183 189
184 friend class ThreadQuitTask; 190 friend class ThreadQuitTask;
185 191
186 DISALLOW_COPY_AND_ASSIGN(Thread); 192 DISALLOW_COPY_AND_ASSIGN(Thread);
187 }; 193 };
188 194
189 } // namespace base 195 } // namespace base
190 196
191 #endif // BASE_THREAD_H_ 197 #endif // BASE_THREAD_H_
OLDNEW
« no previous file with comments | « base/memory/weak_ptr_unittest.cc ('k') | base/threading/thread_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698