OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_proxy_impl.h" | 5 #include "base/message_loop_proxy_impl.h" |
6 #include "base/threading/thread_restrictions.h" | 6 #include "base/threading/thread_restrictions.h" |
7 | 7 |
8 namespace base { | 8 namespace base { |
9 | 9 |
10 MessageLoopProxyImpl::MessageLoopProxyImpl() | |
11 : target_message_loop_(MessageLoop::current()) { | |
12 target_message_loop_->AddDestructionObserver(this); | |
13 } | |
14 | |
15 MessageLoopProxyImpl::~MessageLoopProxyImpl() { | 10 MessageLoopProxyImpl::~MessageLoopProxyImpl() { |
16 AutoLock lock(message_loop_lock_); | 11 AutoLock lock(message_loop_lock_); |
17 // If the target message loop still exists, the d'tor WILL execute on the | 12 // If the target message loop still exists, the d'tor WILL execute on the |
18 // target loop. | 13 // target loop. |
19 if (target_message_loop_) { | 14 if (target_message_loop_) { |
20 DCHECK(MessageLoop::current() == target_message_loop_); | 15 DCHECK(MessageLoop::current() == target_message_loop_); |
21 MessageLoop::current()->RemoveDestructionObserver(this); | 16 MessageLoop::current()->RemoveDestructionObserver(this); |
22 } | 17 } |
23 } | 18 } |
24 | 19 |
(...skipping 24 matching lines...) Expand all Loading... |
49 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | 44 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
50 // may be deleted by ~AtExitManager when a WorkerPool thread calls this | 45 // may be deleted by ~AtExitManager when a WorkerPool thread calls this |
51 // function. | 46 // function. |
52 // http://crbug.com/63678 | 47 // http://crbug.com/63678 |
53 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; | 48 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; |
54 AutoLock lock(message_loop_lock_); | 49 AutoLock lock(message_loop_lock_); |
55 return (target_message_loop_ && | 50 return (target_message_loop_ && |
56 (MessageLoop::current() == target_message_loop_)); | 51 (MessageLoop::current() == target_message_loop_)); |
57 } | 52 } |
58 | 53 |
59 bool MessageLoopProxyImpl::PostTaskHelper( | 54 // MessageLoop::DestructionObserver implementation |
60 const tracked_objects::Location& from_here, Task* task, int64 delay_ms, | 55 void MessageLoopProxyImpl::WillDestroyCurrentMessageLoop() { |
61 bool nestable) { | 56 AutoLock lock(message_loop_lock_); |
62 bool ret = false; | 57 target_message_loop_ = NULL; |
63 { | |
64 AutoLock lock(message_loop_lock_); | |
65 if (target_message_loop_) { | |
66 if (nestable) { | |
67 target_message_loop_->PostDelayedTask(from_here, task, delay_ms); | |
68 } else { | |
69 target_message_loop_->PostNonNestableDelayedTask(from_here, task, | |
70 delay_ms); | |
71 } | |
72 ret = true; | |
73 } | |
74 } | |
75 if (!ret) | |
76 delete task; | |
77 return ret; | |
78 } | 58 } |
79 | 59 |
80 void MessageLoopProxyImpl::OnDestruct() const { | 60 void MessageLoopProxyImpl::OnDestruct() const { |
81 // We shouldn't use MessageLoop::current() since it uses LazyInstance which | 61 // We shouldn't use MessageLoop::current() since it uses LazyInstance which |
82 // may be deleted by ~AtExitManager when a WorkerPool thread calls this | 62 // may be deleted by ~AtExitManager when a WorkerPool thread calls this |
83 // function. | 63 // function. |
84 // http://crbug.com/63678 | 64 // http://crbug.com/63678 |
85 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; | 65 base::ThreadRestrictions::ScopedAllowSingleton allow_singleton; |
86 bool delete_later = false; | 66 bool delete_later = false; |
87 { | 67 { |
88 AutoLock lock(message_loop_lock_); | 68 AutoLock lock(message_loop_lock_); |
89 if (target_message_loop_ && | 69 if (target_message_loop_ && |
90 (MessageLoop::current() != target_message_loop_)) { | 70 (MessageLoop::current() != target_message_loop_)) { |
91 target_message_loop_->DeleteSoon(FROM_HERE, this); | 71 target_message_loop_->DeleteSoon(FROM_HERE, this); |
92 delete_later = true; | 72 delete_later = true; |
93 } | 73 } |
94 } | 74 } |
95 if (!delete_later) | 75 if (!delete_later) |
96 delete this; | 76 delete this; |
97 } | 77 } |
98 | 78 |
99 // MessageLoop::DestructionObserver implementation | 79 MessageLoopProxyImpl::MessageLoopProxyImpl() |
100 void MessageLoopProxyImpl::WillDestroyCurrentMessageLoop() { | 80 : target_message_loop_(MessageLoop::current()) { |
101 AutoLock lock(message_loop_lock_); | 81 target_message_loop_->AddDestructionObserver(this); |
102 target_message_loop_ = NULL; | 82 } |
| 83 |
| 84 bool MessageLoopProxyImpl::PostTaskHelper( |
| 85 const tracked_objects::Location& from_here, Task* task, int64 delay_ms, |
| 86 bool nestable) { |
| 87 bool ret = false; |
| 88 { |
| 89 AutoLock lock(message_loop_lock_); |
| 90 if (target_message_loop_) { |
| 91 if (nestable) { |
| 92 target_message_loop_->PostDelayedTask(from_here, task, delay_ms); |
| 93 } else { |
| 94 target_message_loop_->PostNonNestableDelayedTask(from_here, task, |
| 95 delay_ms); |
| 96 } |
| 97 ret = true; |
| 98 } |
| 99 } |
| 100 if (!ret) |
| 101 delete task; |
| 102 return ret; |
103 } | 103 } |
104 | 104 |
105 scoped_refptr<MessageLoopProxy> | 105 scoped_refptr<MessageLoopProxy> |
106 MessageLoopProxy::CreateForCurrentThread() { | 106 MessageLoopProxy::CreateForCurrentThread() { |
107 scoped_refptr<MessageLoopProxy> ret(new MessageLoopProxyImpl()); | 107 scoped_refptr<MessageLoopProxy> ret(new MessageLoopProxyImpl()); |
108 return ret; | 108 return ret; |
109 } | 109 } |
110 | 110 |
111 } // namespace base | 111 } // namespace base |
OLD | NEW |