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