| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_proxy_impl.h" | 5 #include "base/message_loop/message_loop_proxy_impl.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/threading/thread_restrictions.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/incoming_task_queue.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 9 | 11 |
| 10 namespace base { | 12 namespace base { |
| 13 namespace internal { |
| 11 | 14 |
| 12 MessageLoopProxyImpl::~MessageLoopProxyImpl() { | 15 MessageLoopProxyImpl::MessageLoopProxyImpl( |
| 16 scoped_refptr<IncomingTaskQueue> incoming_queue) |
| 17 : incoming_queue_(incoming_queue), |
| 18 valid_thread_id_(PlatformThread::CurrentId()) { |
| 13 } | 19 } |
| 14 | 20 |
| 15 bool MessageLoopProxyImpl::PostDelayedTask( | 21 bool MessageLoopProxyImpl::PostDelayedTask( |
| 16 const tracked_objects::Location& from_here, | 22 const tracked_objects::Location& from_here, |
| 17 const base::Closure& task, | 23 const base::Closure& task, |
| 18 base::TimeDelta delay) { | 24 base::TimeDelta delay) { |
| 19 return PostTaskHelper(from_here, task, delay, true); | 25 DCHECK(!task.is_null()) << from_here.ToString(); |
| 26 return incoming_queue_->AddToIncomingQueue(from_here, task, delay, true); |
| 20 } | 27 } |
| 21 | 28 |
| 22 bool MessageLoopProxyImpl::PostNonNestableDelayedTask( | 29 bool MessageLoopProxyImpl::PostNonNestableDelayedTask( |
| 23 const tracked_objects::Location& from_here, | 30 const tracked_objects::Location& from_here, |
| 24 const base::Closure& task, | 31 const base::Closure& task, |
| 25 base::TimeDelta delay) { | 32 base::TimeDelta delay) { |
| 26 return PostTaskHelper(from_here, task, delay, false); | 33 DCHECK(!task.is_null()) << from_here.ToString(); |
| 34 return incoming_queue_->AddToIncomingQueue(from_here, task, delay, false); |
| 27 } | 35 } |
| 28 | 36 |
| 29 bool MessageLoopProxyImpl::RunsTasksOnCurrentThread() const { | 37 bool MessageLoopProxyImpl::RunsTasksOnCurrentThread() const { |
| 30 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | 38 return valid_thread_id_ == PlatformThread::CurrentId(); |
| 31 // may be deleted by ~AtExitManager when a WorkerPool thread calls this | |
| 32 // function. | |
| 33 // http://crbug.com/63678 | |
| 34 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; | |
| 35 AutoLock lock(message_loop_lock_); | |
| 36 return (target_message_loop_ && | |
| 37 (MessageLoop::current() == target_message_loop_)); | |
| 38 } | 39 } |
| 39 | 40 |
| 40 // MessageLoop::DestructionObserver implementation | 41 MessageLoopProxyImpl::~MessageLoopProxyImpl() { |
| 41 void MessageLoopProxyImpl::WillDestroyCurrentMessageLoop() { | |
| 42 AutoLock lock(message_loop_lock_); | |
| 43 target_message_loop_ = NULL; | |
| 44 } | 42 } |
| 45 | 43 |
| 46 void MessageLoopProxyImpl::OnDestruct() const { | 44 } // namespace internal |
| 47 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | |
| 48 // may be deleted by ~AtExitManager when a WorkerPool thread calls this | |
| 49 // function. | |
| 50 // http://crbug.com/63678 | |
| 51 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; | |
| 52 bool delete_later = false; | |
| 53 { | |
| 54 AutoLock lock(message_loop_lock_); | |
| 55 if (target_message_loop_ && | |
| 56 (MessageLoop::current() != target_message_loop_)) { | |
| 57 target_message_loop_->DeleteSoon(FROM_HERE, this); | |
| 58 delete_later = true; | |
| 59 } | |
| 60 } | |
| 61 if (!delete_later) | |
| 62 delete this; | |
| 63 } | |
| 64 | |
| 65 MessageLoopProxyImpl::MessageLoopProxyImpl() | |
| 66 : target_message_loop_(MessageLoop::current()) { | |
| 67 } | |
| 68 | |
| 69 bool MessageLoopProxyImpl::PostTaskHelper( | |
| 70 const tracked_objects::Location& from_here, const base::Closure& task, | |
| 71 base::TimeDelta delay, bool nestable) { | |
| 72 AutoLock lock(message_loop_lock_); | |
| 73 if (target_message_loop_) { | |
| 74 if (nestable) { | |
| 75 target_message_loop_->PostDelayedTask(from_here, task, delay); | |
| 76 } else { | |
| 77 target_message_loop_->PostNonNestableDelayedTask(from_here, task, delay); | |
| 78 } | |
| 79 return true; | |
| 80 } | |
| 81 return false; | |
| 82 } | |
| 83 | 45 |
| 84 scoped_refptr<MessageLoopProxy> | 46 scoped_refptr<MessageLoopProxy> |
| 85 MessageLoopProxy::current() { | 47 MessageLoopProxy::current() { |
| 86 MessageLoop* cur_loop = MessageLoop::current(); | 48 MessageLoop* cur_loop = MessageLoop::current(); |
| 87 if (!cur_loop) | 49 if (!cur_loop) |
| 88 return NULL; | 50 return NULL; |
| 89 return cur_loop->message_loop_proxy(); | 51 return cur_loop->message_loop_proxy(); |
| 90 } | 52 } |
| 91 | 53 |
| 92 } // namespace base | 54 } // namespace base |
| OLD | NEW |