license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 5 | #include "base/thread.h" |
| 6 | |
[email protected] | 83a05dd | 2008-09-03 16:47:37 | [diff] [blame^] | 7 | #include "base/singleton.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | #include "base/string_util.h" |
[email protected] | 83a05dd | 2008-09-03 16:47:37 | [diff] [blame^] | 9 | #include "base/thread_local.h" |
[email protected] | ffd8308 | 2008-08-11 14:35:15 | [diff] [blame] | 10 | #include "base/waitable_event.h" |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 11 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 12 | namespace base { |
| 13 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | // This task is used to trigger the message loop to exit. |
| 15 | class ThreadQuitTask : public Task { |
| 16 | public: |
| 17 | virtual void Run() { |
| 18 | MessageLoop::current()->Quit(); |
| 19 | Thread::SetThreadWasQuitProperly(true); |
| 20 | } |
| 21 | }; |
| 22 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 23 | // Used to pass data to ThreadMain. This structure is allocated on the stack |
| 24 | // from within StartWithOptions. |
| 25 | struct Thread::StartupData { |
| 26 | // We get away with a const reference here because of how we are allocated. |
| 27 | const Thread::Options& options; |
| 28 | |
| 29 | // Used to synchronize thread startup. |
| 30 | WaitableEvent event; |
| 31 | |
| 32 | StartupData(const Options& opt) : options(opt), event(false, false) {} |
| 33 | }; |
| 34 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 35 | Thread::Thread(const char *name) |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 36 | : startup_data_(NULL), |
[email protected] | 6548726 | 2008-08-26 06:06:01 | [diff] [blame] | 37 | thread_(0), |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 38 | message_loop_(NULL), |
| 39 | thread_id_(0), |
| 40 | name_(name) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | Thread::~Thread() { |
| 44 | Stop(); |
| 45 | } |
| 46 | |
| 47 | // We use this thread-local variable to record whether or not a thread exited |
| 48 | // because its Stop method was called. This allows us to catch cases where |
| 49 | // MessageLoop::Quit() is called directly, which is unexpected when using a |
| 50 | // Thread to setup and run a MessageLoop. |
[email protected] | 83a05dd | 2008-09-03 16:47:37 | [diff] [blame^] | 51 | namespace { |
| 52 | |
| 53 | // Use a differentiating type to make sure we don't share our boolean we any |
| 54 | // other Singleton<ThreadLocalBoolean>'s. |
| 55 | struct ThreadExitedDummyDiffType { }; |
| 56 | typedef Singleton<ThreadLocalBoolean, |
| 57 | DefaultSingletonTraits<ThreadLocalBoolean>, |
| 58 | ThreadExitedDummyDiffType> ThreadExitedSingleton; |
| 59 | |
| 60 | } // namespace |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 61 | |
| 62 | void Thread::SetThreadWasQuitProperly(bool flag) { |
[email protected] | 83a05dd | 2008-09-03 16:47:37 | [diff] [blame^] | 63 | ThreadExitedSingleton::get()->Set(flag); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | bool Thread::GetThreadWasQuitProperly() { |
| 67 | bool quit_properly = true; |
| 68 | #ifndef NDEBUG |
[email protected] | 83a05dd | 2008-09-03 16:47:37 | [diff] [blame^] | 69 | quit_properly = ThreadExitedSingleton::get()->Get(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 70 | #endif |
| 71 | return quit_properly; |
| 72 | } |
| 73 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 74 | bool Thread::Start() { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 75 | return StartWithOptions(Options()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 78 | bool Thread::StartWithOptions(const Options& options) { |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 79 | DCHECK(!message_loop_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 80 | |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 81 | SetThreadWasQuitProperly(false); |
| 82 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 83 | StartupData startup_data(options); |
| 84 | startup_data_ = &startup_data; |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 85 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 86 | if (!PlatformThread::Create(options.stack_size, this, &thread_)) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 87 | DLOG(ERROR) << "failed to create thread"; |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 88 | startup_data_ = NULL; // Record that we failed to start. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | |
| 92 | // Wait for the thread to start and initialize message_loop_ |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 93 | startup_data.event.Wait(); |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 94 | |
| 95 | DCHECK(message_loop_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
| 99 | void Thread::Stop() { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 100 | if (!thread_was_started()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 101 | return; |
| 102 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 103 | // We should only be called on the same thread that started us. |
| 104 | DCHECK_NE(thread_id_, PlatformThread::CurrentId()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 105 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 106 | // StopSoon may have already been called. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 107 | if (message_loop_) |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 108 | message_loop_->PostTask(FROM_HERE, new ThreadQuitTask()); |
| 109 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 110 | // Wait for the thread to exit. It should already have terminated but make |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 111 | // sure this assumption is valid. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 112 | // |
| 113 | // TODO(darin): Unfortunately, we need to keep message_loop_ around until |
| 114 | // the thread exits. Some consumers are abusing the API. Make them stop. |
| 115 | // |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 116 | PlatformThread::Join(thread_); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 117 | |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 118 | // The thread can't receive messages anymore. |
| 119 | message_loop_ = NULL; |
| 120 | |
| 121 | // The thread no longer needs to be joined. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 122 | startup_data_ = NULL; |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void Thread::StopSoon() { |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 126 | if (!message_loop_) |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 127 | return; |
| 128 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 129 | // We should only be called on the same thread that started us. |
| 130 | DCHECK_NE(thread_id_, PlatformThread::CurrentId()); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 131 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 132 | // We had better have a message loop at this point! If we do not, then it |
| 133 | // most likely means that the thread terminated unexpectedly, probably due |
| 134 | // to someone calling Quit() on our message loop directly. |
| 135 | DCHECK(message_loop_); |
| 136 | |
| 137 | message_loop_->PostTask(FROM_HERE, new ThreadQuitTask()); |
| 138 | |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 139 | // The thread can't receive messages anymore. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 140 | message_loop_ = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 143 | void Thread::ThreadMain() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 144 | // The message loop for this thread. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 145 | MessageLoop message_loop(startup_data_->options.message_loop_type); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 146 | |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 147 | // Complete the initialization of our Thread object. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 148 | thread_id_ = PlatformThread::CurrentId(); |
[email protected] | 7d0f9445 | 2008-08-25 13:54:18 | [diff] [blame] | 149 | PlatformThread::SetName(name_.c_str()); |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 150 | message_loop.set_thread_name(name_); |
| 151 | message_loop_ = &message_loop; |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 152 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 153 | startup_data_->event.Signal(); |
| 154 | // startup_data_ can't be touched anymore since the starting thread is now |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 155 | // unlocked. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 156 | |
| 157 | // Let the thread do extra initialization. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 158 | Init(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 159 | |
| 160 | message_loop.Run(); |
| 161 | |
| 162 | // Let the thread do extra cleanup. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 163 | CleanUp(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 164 | |
| 165 | // Assert that MessageLoop::Quit was called by ThreadQuitTask. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 166 | DCHECK(GetThreadWasQuitProperly()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 167 | |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 168 | // We can't receive messages anymore. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 169 | message_loop_ = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 171 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 172 | } // namespace base |