1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/message_loop_proxy_impl.h"
#include "base/location.h"
#include "base/threading/thread_restrictions.h"
namespace base {
MessageLoopProxyImpl::~MessageLoopProxyImpl() {
}
// MessageLoopProxy implementation
bool MessageLoopProxyImpl::PostTask(const tracked_objects::Location& from_here,
Task* task) {
return PostTaskHelper(from_here, task, 0, true);
}
bool MessageLoopProxyImpl::PostDelayedTask(
const tracked_objects::Location& from_here, Task* task, int64 delay_ms) {
return PostTaskHelper(from_here, task, delay_ms, true);
}
bool MessageLoopProxyImpl::PostNonNestableTask(
const tracked_objects::Location& from_here, Task* task) {
return PostTaskHelper(from_here, task, 0, false);
}
bool MessageLoopProxyImpl::PostNonNestableDelayedTask(
const tracked_objects::Location& from_here,
Task* task,
int64 delay_ms) {
return PostTaskHelper(from_here, task, delay_ms, false);
}
bool MessageLoopProxyImpl::PostTask(const tracked_objects::Location& from_here,
const base::Closure& task) {
return PostTaskHelper(from_here, task, 0, true);
}
bool MessageLoopProxyImpl::PostDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
int64 delay_ms) {
return PostTaskHelper(from_here, task, delay_ms, true);
}
bool MessageLoopProxyImpl::PostNonNestableTask(
const tracked_objects::Location& from_here, const base::Closure& task) {
return PostTaskHelper(from_here, task, 0, false);
}
bool MessageLoopProxyImpl::PostNonNestableDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
int64 delay_ms) {
return PostTaskHelper(from_here, task, delay_ms, false);
}
bool MessageLoopProxyImpl::BelongsToCurrentThread() {
// We shouldn't use MessageLoop::current() since it uses LazyInstance which
// may be deleted by ~AtExitManager when a WorkerPool thread calls this
// function.
// http://crbug.com/63678
base::ThreadRestrictions::ScopedAllowSingleton allow_singleton;
AutoLock lock(message_loop_lock_);
return (target_message_loop_ &&
(MessageLoop::current() == target_message_loop_));
}
// MessageLoop::DestructionObserver implementation
void MessageLoopProxyImpl::WillDestroyCurrentMessageLoop() {
AutoLock lock(message_loop_lock_);
target_message_loop_ = NULL;
}
void MessageLoopProxyImpl::OnDestruct() const {
// We shouldn't use MessageLoop::current() since it uses LazyInstance which
// may be deleted by ~AtExitManager when a WorkerPool thread calls this
// function.
// http://crbug.com/63678
base::ThreadRestrictions::ScopedAllowSingleton allow_singleton;
bool delete_later = false;
{
AutoLock lock(message_loop_lock_);
if (target_message_loop_ &&
(MessageLoop::current() != target_message_loop_)) {
target_message_loop_->DeleteSoon(FROM_HERE, this);
delete_later = true;
}
}
if (!delete_later)
delete this;
}
MessageLoopProxyImpl::MessageLoopProxyImpl()
: target_message_loop_(MessageLoop::current()) {
}
bool MessageLoopProxyImpl::PostTaskHelper(
const tracked_objects::Location& from_here, Task* task, int64 delay_ms,
bool nestable) {
bool ret = false;
{
AutoLock lock(message_loop_lock_);
if (target_message_loop_) {
if (nestable) {
target_message_loop_->PostDelayedTask(from_here, task, delay_ms);
} else {
target_message_loop_->PostNonNestableDelayedTask(from_here, task,
delay_ms);
}
ret = true;
}
}
if (!ret)
delete task;
return ret;
}
bool MessageLoopProxyImpl::PostTaskHelper(
const tracked_objects::Location& from_here, const base::Closure& task,
int64 delay_ms, bool nestable) {
AutoLock lock(message_loop_lock_);
if (target_message_loop_) {
if (nestable) {
target_message_loop_->PostDelayedTask(from_here, task, delay_ms);
} else {
target_message_loop_->PostNonNestableDelayedTask(from_here, task,
delay_ms);
}
return true;
}
return false;
}
scoped_refptr<MessageLoopProxy>
MessageLoopProxy::current() {
MessageLoop* cur_loop = MessageLoop::current();
if (!cur_loop)
return NULL;
return cur_loop->message_loop_proxy();
}
} // namespace base
|