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] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 7 | #include "base/lazy_instance.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 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 47 | namespace { |
| 48 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 49 | // We use this thread-local variable to record whether or not a thread exited |
| 50 | // because its Stop method was called. This allows us to catch cases where |
| 51 | // MessageLoop::Quit() is called directly, which is unexpected when using a |
| 52 | // Thread to setup and run a MessageLoop. |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 53 | base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool( |
| 54 | base::LINKER_INITIALIZED); |
[email protected] | 83a05dd | 2008-09-03 16:47:37 | [diff] [blame] | 55 | |
| 56 | } // namespace |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 57 | |
| 58 | void Thread::SetThreadWasQuitProperly(bool flag) { |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 59 | lazy_tls_bool.Pointer()->Set(flag); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | bool Thread::GetThreadWasQuitProperly() { |
| 63 | bool quit_properly = true; |
| 64 | #ifndef NDEBUG |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 65 | quit_properly = lazy_tls_bool.Pointer()->Get(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 66 | #endif |
| 67 | return quit_properly; |
| 68 | } |
| 69 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 70 | bool Thread::Start() { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 71 | return StartWithOptions(Options()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 74 | bool Thread::StartWithOptions(const Options& options) { |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 75 | DCHECK(!message_loop_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 76 | |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 77 | SetThreadWasQuitProperly(false); |
| 78 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 79 | StartupData startup_data(options); |
| 80 | startup_data_ = &startup_data; |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 81 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 82 | if (!PlatformThread::Create(options.stack_size, this, &thread_)) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 83 | DLOG(ERROR) << "failed to create thread"; |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 84 | startup_data_ = NULL; // Record that we failed to start. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | |
| 88 | // Wait for the thread to start and initialize message_loop_ |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 89 | startup_data.event.Wait(); |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 90 | |
| 91 | DCHECK(message_loop_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
| 95 | void Thread::Stop() { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 96 | if (!thread_was_started()) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 97 | return; |
| 98 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 99 | // We should only be called on the same thread that started us. |
| 100 | DCHECK_NE(thread_id_, PlatformThread::CurrentId()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 101 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 102 | // StopSoon may have already been called. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 103 | if (message_loop_) |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 104 | message_loop_->PostTask(FROM_HERE, new ThreadQuitTask()); |
| 105 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 106 | // Wait for the thread to exit. It should already have terminated but make |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 107 | // sure this assumption is valid. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 108 | // |
| 109 | // TODO(darin): Unfortunately, we need to keep message_loop_ around until |
| 110 | // the thread exits. Some consumers are abusing the API. Make them stop. |
| 111 | // |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 112 | PlatformThread::Join(thread_); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 113 | |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 114 | // The thread can't receive messages anymore. |
| 115 | message_loop_ = NULL; |
| 116 | |
| 117 | // The thread no longer needs to be joined. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 118 | startup_data_ = NULL; |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void Thread::StopSoon() { |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 122 | if (!message_loop_) |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 123 | return; |
| 124 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 125 | // We should only be called on the same thread that started us. |
| 126 | DCHECK_NE(thread_id_, PlatformThread::CurrentId()); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 127 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 128 | // We had better have a message loop at this point! If we do not, then it |
| 129 | // most likely means that the thread terminated unexpectedly, probably due |
| 130 | // to someone calling Quit() on our message loop directly. |
| 131 | DCHECK(message_loop_); |
| 132 | |
| 133 | message_loop_->PostTask(FROM_HERE, new ThreadQuitTask()); |
| 134 | |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 135 | // The thread can't receive messages anymore. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 136 | message_loop_ = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 139 | void Thread::ThreadMain() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 140 | // The message loop for this thread. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 141 | MessageLoop message_loop(startup_data_->options.message_loop_type); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 142 | |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 143 | // Complete the initialization of our Thread object. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 144 | thread_id_ = PlatformThread::CurrentId(); |
[email protected] | 7d0f9445 | 2008-08-25 13:54:18 | [diff] [blame] | 145 | PlatformThread::SetName(name_.c_str()); |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 146 | message_loop.set_thread_name(name_); |
| 147 | message_loop_ = &message_loop; |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 148 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 149 | startup_data_->event.Signal(); |
| 150 | // startup_data_ can't be touched anymore since the starting thread is now |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 151 | // unlocked. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 152 | |
| 153 | // Let the thread do extra initialization. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 154 | Init(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 155 | |
| 156 | message_loop.Run(); |
| 157 | |
| 158 | // Let the thread do extra cleanup. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 159 | CleanUp(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 160 | |
| 161 | // Assert that MessageLoop::Quit was called by ThreadQuitTask. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 162 | DCHECK(GetThreadWasQuitProperly()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 163 | |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 164 | // We can't receive messages anymore. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 165 | message_loop_ = NULL; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 166 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 167 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 168 | } // namespace base |