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/logging.h" | 8 #include "base/threading/thread_restrictions.h" |
9 #include "base/message_loop/incoming_task_queue.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 | 9 |
12 namespace base { | 10 namespace base { |
13 namespace internal { | |
14 | 11 |
15 MessageLoopProxyImpl::MessageLoopProxyImpl( | 12 MessageLoopProxyImpl::~MessageLoopProxyImpl() { |
16 scoped_refptr<IncomingTaskQueue> incoming_queue) | |
17 : incoming_queue_(incoming_queue), | |
18 valid_thread_id_(PlatformThread::CurrentId()) { | |
19 } | 13 } |
20 | 14 |
21 bool MessageLoopProxyImpl::PostDelayedTask( | 15 bool MessageLoopProxyImpl::PostDelayedTask( |
22 const tracked_objects::Location& from_here, | 16 const tracked_objects::Location& from_here, |
23 const base::Closure& task, | 17 const base::Closure& task, |
24 base::TimeDelta delay) { | 18 base::TimeDelta delay) { |
25 DCHECK(!task.is_null()) << from_here.ToString(); | 19 return PostTaskHelper(from_here, task, delay, true); |
26 return incoming_queue_->AddToIncomingQueue(from_here, task, delay, true); | |
27 } | 20 } |
28 | 21 |
29 bool MessageLoopProxyImpl::PostNonNestableDelayedTask( | 22 bool MessageLoopProxyImpl::PostNonNestableDelayedTask( |
30 const tracked_objects::Location& from_here, | 23 const tracked_objects::Location& from_here, |
31 const base::Closure& task, | 24 const base::Closure& task, |
32 base::TimeDelta delay) { | 25 base::TimeDelta delay) { |
33 DCHECK(!task.is_null()) << from_here.ToString(); | 26 return PostTaskHelper(from_here, task, delay, false); |
34 return incoming_queue_->AddToIncomingQueue(from_here, task, delay, false); | |
35 } | 27 } |
36 | 28 |
37 bool MessageLoopProxyImpl::RunsTasksOnCurrentThread() const { | 29 bool MessageLoopProxyImpl::RunsTasksOnCurrentThread() const { |
38 return valid_thread_id_ == PlatformThread::CurrentId(); | 30 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
| 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_)); |
39 } | 38 } |
40 | 39 |
41 MessageLoopProxyImpl::~MessageLoopProxyImpl() { | 40 // MessageLoop::DestructionObserver implementation |
| 41 void MessageLoopProxyImpl::WillDestroyCurrentMessageLoop() { |
| 42 AutoLock lock(message_loop_lock_); |
| 43 target_message_loop_ = NULL; |
42 } | 44 } |
43 | 45 |
44 } // namespace internal | 46 void MessageLoopProxyImpl::OnDestruct() const { |
| 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 } |
45 | 83 |
46 scoped_refptr<MessageLoopProxy> | 84 scoped_refptr<MessageLoopProxy> |
47 MessageLoopProxy::current() { | 85 MessageLoopProxy::current() { |
48 MessageLoop* cur_loop = MessageLoop::current(); | 86 MessageLoop* cur_loop = MessageLoop::current(); |
49 if (!cur_loop) | 87 if (!cur_loop) |
50 return NULL; | 88 return NULL; |
51 return cur_loop->message_loop_proxy(); | 89 return cur_loop->message_loop_proxy(); |
52 } | 90 } |
53 | 91 |
54 } // namespace base | 92 } // namespace base |
OLD | NEW |