[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 1 | // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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] | 94d0d3e | 2009-11-23 19:17:33 | [diff] [blame] | 7 | #include "base/dynamic_annotations.h" |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 8 | #include "base/lazy_instance.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | #include "base/string_util.h" |
[email protected] | 83a05dd | 2008-09-03 16:47:37 | [diff] [blame] | 10 | #include "base/thread_local.h" |
[email protected] | ffd8308 | 2008-08-11 14:35:15 | [diff] [blame] | 11 | #include "base/waitable_event.h" |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 12 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 13 | namespace base { |
| 14 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | // This task is used to trigger the message loop to exit. |
| 16 | class ThreadQuitTask : public Task { |
| 17 | public: |
| 18 | virtual void Run() { |
| 19 | MessageLoop::current()->Quit(); |
| 20 | Thread::SetThreadWasQuitProperly(true); |
| 21 | } |
| 22 | }; |
| 23 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 24 | // Used to pass data to ThreadMain. This structure is allocated on the stack |
| 25 | // from within StartWithOptions. |
| 26 | struct Thread::StartupData { |
| 27 | // We get away with a const reference here because of how we are allocated. |
| 28 | const Thread::Options& options; |
| 29 | |
| 30 | // Used to synchronize thread startup. |
| 31 | WaitableEvent event; |
| 32 | |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 33 | explicit StartupData(const Options& opt) |
| 34 | : options(opt), |
| 35 | event(false, false) {} |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 36 | }; |
| 37 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 38 | Thread::Thread(const char *name) |
[email protected] | 88f33312 | 2009-09-14 23:47:38 | [diff] [blame] | 39 | : stopping_(false), |
| 40 | startup_data_(NULL), |
[email protected] | 6548726 | 2008-08-26 06:06:01 | [diff] [blame] | 41 | thread_(0), |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 42 | message_loop_(NULL), |
| 43 | thread_id_(0), |
| 44 | name_(name) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | Thread::~Thread() { |
| 48 | Stop(); |
| 49 | } |
| 50 | |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 51 | namespace { |
| 52 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 53 | // We use this thread-local variable to record whether or not a thread exited |
| 54 | // because its Stop method was called. This allows us to catch cases where |
| 55 | // MessageLoop::Quit() is called directly, which is unexpected when using a |
| 56 | // Thread to setup and run a MessageLoop. |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 57 | base::LazyInstance<base::ThreadLocalBoolean> lazy_tls_bool( |
| 58 | base::LINKER_INITIALIZED); |
[email protected] | 83a05dd | 2008-09-03 16:47:37 | [diff] [blame] | 59 | |
| 60 | } // namespace |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 61 | |
| 62 | void Thread::SetThreadWasQuitProperly(bool flag) { |
[email protected] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 63 | lazy_tls_bool.Pointer()->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] | f886b7bf | 2008-09-10 10:54:06 | [diff] [blame] | 69 | quit_properly = lazy_tls_bool.Pointer()->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] | 88f33312 | 2009-09-14 23:47:38 | [diff] [blame] | 103 | StopSoon(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 104 | |
[email protected] | 88f33312 | 2009-09-14 23:47:38 | [diff] [blame] | 105 | // Wait for the thread to exit. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 106 | // |
| 107 | // TODO(darin): Unfortunately, we need to keep message_loop_ around until |
| 108 | // the thread exits. Some consumers are abusing the API. Make them stop. |
| 109 | // |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 110 | PlatformThread::Join(thread_); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 111 | |
[email protected] | 88f33312 | 2009-09-14 23:47:38 | [diff] [blame] | 112 | // The thread should NULL message_loop_ on exit. |
| 113 | DCHECK(!message_loop_); |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 114 | |
| 115 | // The thread no longer needs to be joined. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 116 | startup_data_ = NULL; |
[email protected] | 88f33312 | 2009-09-14 23:47:38 | [diff] [blame] | 117 | |
| 118 | stopping_ = false; |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void Thread::StopSoon() { |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 122 | // We should only be called on the same thread that started us. |
| 123 | DCHECK_NE(thread_id_, PlatformThread::CurrentId()); |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 124 | |
[email protected] | 3c92e24 | 2009-09-21 17:03:46 | [diff] [blame] | 125 | if (stopping_ || !message_loop_) |
[email protected] | 88f33312 | 2009-09-14 23:47:38 | [diff] [blame] | 126 | return; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 127 | |
[email protected] | 88f33312 | 2009-09-14 23:47:38 | [diff] [blame] | 128 | stopping_ = true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 129 | message_loop_->PostTask(FROM_HERE, new ThreadQuitTask()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 828bd79 | 2009-09-10 05:00:20 | [diff] [blame] | 132 | void Thread::Run(MessageLoop* message_loop) { |
| 133 | message_loop->Run(); |
| 134 | } |
| 135 | |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 136 | void Thread::ThreadMain() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 137 | // The message loop for this thread. |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 138 | MessageLoop message_loop(startup_data_->options.message_loop_type); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 139 | |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 140 | // Complete the initialization of our Thread object. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 141 | thread_id_ = PlatformThread::CurrentId(); |
[email protected] | 7d0f9445 | 2008-08-25 13:54:18 | [diff] [blame] | 142 | PlatformThread::SetName(name_.c_str()); |
[email protected] | 94d0d3e | 2009-11-23 19:17:33 | [diff] [blame] | 143 | ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 144 | message_loop.set_thread_name(name_); |
| 145 | message_loop_ = &message_loop; |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 146 | |
[email protected] | cd636d8 | 2009-01-27 01:26:16 | [diff] [blame] | 147 | // Let the thread do extra initialization. |
| 148 | // Let's do this before signaling we are started. |
| 149 | Init(); |
| 150 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 151 | startup_data_->event.Signal(); |
| 152 | // startup_data_ can't be touched anymore since the starting thread is now |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 153 | // unlocked. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 154 | |
[email protected] | 828bd79 | 2009-09-10 05:00:20 | [diff] [blame] | 155 | Run(message_loop_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 156 | |
| 157 | // Let the thread do extra cleanup. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 158 | CleanUp(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 159 | |
| 160 | // Assert that MessageLoop::Quit was called by ThreadQuitTask. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 161 | DCHECK(GetThreadWasQuitProperly()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 162 | |
[email protected] | eff4aecb | 2008-08-12 18:37:35 | [diff] [blame] | 163 | // We can't receive messages anymore. |
[email protected] | e9ba26d | 2008-08-21 09:46:32 | [diff] [blame] | 164 | message_loop_ = NULL; |
[email protected] | b026f4a | 2009-01-28 17:21:23 | [diff] [blame] | 165 | thread_id_ = 0; |
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 |