OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 137 |
138 MessageLoop::MessageLoop(Type type) | 138 MessageLoop::MessageLoop(Type type) |
139 : type_(type), | 139 : type_(type), |
140 exception_restoration_(false), | 140 exception_restoration_(false), |
141 nestable_tasks_allowed_(true), | 141 nestable_tasks_allowed_(true), |
142 #if defined(OS_WIN) | 142 #if defined(OS_WIN) |
143 os_modal_loop_(false), | 143 os_modal_loop_(false), |
144 #endif // OS_WIN | 144 #endif // OS_WIN |
145 message_histogram_(NULL), | 145 message_histogram_(NULL), |
146 run_loop_(NULL) { | 146 run_loop_(NULL) { |
147 DCHECK(!current()) << "should only have one message loop per thread"; | 147 Init(); |
148 lazy_tls_ptr.Pointer()->Set(this); | |
149 | |
150 incoming_task_queue_ = new internal::IncomingTaskQueue(this); | |
151 message_loop_proxy_ = | |
152 new internal::MessageLoopProxyImpl(incoming_task_queue_); | |
153 thread_task_runner_handle_.reset( | |
154 new ThreadTaskRunnerHandle(message_loop_proxy_)); | |
155 | 148 |
156 // TODO(rvargas): Get rid of the OS guards. | 149 // TODO(rvargas): Get rid of the OS guards. |
157 #if defined(OS_WIN) | 150 #if defined(OS_WIN) |
158 #define MESSAGE_PUMP_UI new MessagePumpForUI() | 151 #define MESSAGE_PUMP_UI new MessagePumpForUI() |
159 #define MESSAGE_PUMP_IO new MessagePumpForIO() | 152 #define MESSAGE_PUMP_IO new MessagePumpForIO() |
160 #elif defined(OS_IOS) | 153 #elif defined(OS_IOS) |
161 #define MESSAGE_PUMP_UI MessagePumpMac::Create() | 154 #define MESSAGE_PUMP_UI MessagePumpMac::Create() |
162 #define MESSAGE_PUMP_IO new MessagePumpIOSForIO() | 155 #define MESSAGE_PUMP_IO new MessagePumpIOSForIO() |
163 #elif defined(OS_MACOSX) | 156 #elif defined(OS_MACOSX) |
164 #define MESSAGE_PUMP_UI MessagePumpMac::Create() | 157 #define MESSAGE_PUMP_UI MessagePumpMac::Create() |
(...skipping 26 matching lines...) Expand all Loading... |
191 #if defined(OS_ANDROID) | 184 #if defined(OS_ANDROID) |
192 } else if (type_ == TYPE_JAVA) { | 185 } else if (type_ == TYPE_JAVA) { |
193 pump_.reset(MESSAGE_PUMP_UI); | 186 pump_.reset(MESSAGE_PUMP_UI); |
194 #endif | 187 #endif |
195 } else { | 188 } else { |
196 DCHECK_EQ(TYPE_DEFAULT, type_); | 189 DCHECK_EQ(TYPE_DEFAULT, type_); |
197 pump_.reset(new MessagePumpDefault()); | 190 pump_.reset(new MessagePumpDefault()); |
198 } | 191 } |
199 } | 192 } |
200 | 193 |
| 194 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump) |
| 195 : pump_(pump.Pass()), |
| 196 type_(TYPE_CUSTOM), |
| 197 exception_restoration_(false), |
| 198 nestable_tasks_allowed_(true), |
| 199 #if defined(OS_WIN) |
| 200 os_modal_loop_(false), |
| 201 #endif // OS_WIN |
| 202 message_histogram_(NULL), |
| 203 run_loop_(NULL) { |
| 204 DCHECK(pump_.get()); |
| 205 Init(); |
| 206 } |
| 207 |
201 MessageLoop::~MessageLoop() { | 208 MessageLoop::~MessageLoop() { |
202 DCHECK_EQ(this, current()); | 209 DCHECK_EQ(this, current()); |
203 | 210 |
204 DCHECK(!run_loop_); | 211 DCHECK(!run_loop_); |
205 | 212 |
206 // Clean up any unprocessed tasks, but take care: deleting a task could | 213 // Clean up any unprocessed tasks, but take care: deleting a task could |
207 // result in the addition of more tasks (e.g., via DeleteSoon). We set a | 214 // result in the addition of more tasks (e.g., via DeleteSoon). We set a |
208 // limit on the number of times we will allow a deleted task to generate more | 215 // limit on the number of times we will allow a deleted task to generate more |
209 // tasks. Normally, we should only pass through this loop once or twice. If | 216 // tasks. Normally, we should only pass through this loop once or twice. If |
210 // we end up hitting the loop limit, then it is probably due to one task that | 217 // we end up hitting the loop limit, then it is probably due to one task that |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 return incoming_task_queue_->IsIdleForTesting(); | 397 return incoming_task_queue_->IsIdleForTesting(); |
391 } | 398 } |
392 | 399 |
393 void MessageLoop::LockWaitUnLockForTesting(WaitableEvent* caller_wait, | 400 void MessageLoop::LockWaitUnLockForTesting(WaitableEvent* caller_wait, |
394 WaitableEvent* caller_signal) { | 401 WaitableEvent* caller_signal) { |
395 incoming_task_queue_->LockWaitUnLockForTesting(caller_wait, caller_signal); | 402 incoming_task_queue_->LockWaitUnLockForTesting(caller_wait, caller_signal); |
396 } | 403 } |
397 | 404 |
398 //------------------------------------------------------------------------------ | 405 //------------------------------------------------------------------------------ |
399 | 406 |
| 407 void MessageLoop::Init() { |
| 408 DCHECK(!current()) << "should only have one message loop per thread"; |
| 409 lazy_tls_ptr.Pointer()->Set(this); |
| 410 |
| 411 incoming_task_queue_ = new internal::IncomingTaskQueue(this); |
| 412 message_loop_proxy_ = |
| 413 new internal::MessageLoopProxyImpl(incoming_task_queue_); |
| 414 thread_task_runner_handle_.reset( |
| 415 new ThreadTaskRunnerHandle(message_loop_proxy_)); |
| 416 } |
| 417 |
400 // Runs the loop in two different SEH modes: | 418 // Runs the loop in two different SEH modes: |
401 // enable_SEH_restoration_ = false : any unhandled exception goes to the last | 419 // enable_SEH_restoration_ = false : any unhandled exception goes to the last |
402 // one that calls SetUnhandledExceptionFilter(). | 420 // one that calls SetUnhandledExceptionFilter(). |
403 // enable_SEH_restoration_ = true : any unhandled exception goes to the filter | 421 // enable_SEH_restoration_ = true : any unhandled exception goes to the filter |
404 // that was existed before the loop was run. | 422 // that was existed before the loop was run. |
405 void MessageLoop::RunHandler() { | 423 void MessageLoop::RunHandler() { |
406 #if defined(OS_WIN) | 424 #if defined(OS_WIN) |
407 if (exception_restoration_) { | 425 if (exception_restoration_) { |
408 RunInternalInSEHFrame(); | 426 RunInternalInSEHFrame(); |
409 return; | 427 return; |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
762 fd, | 780 fd, |
763 persistent, | 781 persistent, |
764 mode, | 782 mode, |
765 controller, | 783 controller, |
766 delegate); | 784 delegate); |
767 } | 785 } |
768 | 786 |
769 #endif | 787 #endif |
770 | 788 |
771 } // namespace base | 789 } // namespace base |
OLD | NEW |