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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
// Copyright (c) 2012 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 "remoting/jingle_glue/jingle_thread.h"
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/message_loop_proxy.h"
#include "base/message_pump.h"
#include "base/time.h"
#include "third_party/libjingle/source/talk/base/ssladapter.h"
namespace remoting {
const uint32 kRunTasksMessageId = 1;
const uint32 kStopMessageId = 2;
namespace {
class JingleMessagePump : public base::MessagePump,
public talk_base::MessageHandler {
public:
JingleMessagePump(talk_base::Thread* thread)
: thread_(thread), delegate_(NULL), stopping_(false) {
}
virtual void Run(Delegate* delegate) {
delegate_ = delegate;
thread_->Thread::Run();
// Call Restart() so that we can run again.
thread_->Restart();
delegate_ = NULL;
}
virtual void Quit() {
if (!stopping_) {
stopping_ = true;
// Shutdown gracefully: make sure that we excute all messages
// left in the queue before exiting. Thread::Quit() would not do
// that.
thread_->Post(this, kStopMessageId);
}
}
virtual void ScheduleWork() {
thread_->Post(this, kRunTasksMessageId);
}
virtual void ScheduleDelayedWork(const base::TimeTicks& time) {
delayed_work_time_ = time;
ScheduleNextDelayedTask();
}
void OnMessage(talk_base::Message* msg) {
if (msg->message_id == kRunTasksMessageId) {
DCHECK(delegate_);
// Clear currently pending messages in case there were delayed tasks.
// Will schedule it again from ScheduleNextDelayedTask() if neccessary.
thread_->Clear(this, kRunTasksMessageId);
// Process all pending tasks.
while (true) {
if (delegate_->DoWork())
continue;
if (delegate_->DoDelayedWork(&delayed_work_time_))
continue;
if (delegate_->DoIdleWork())
continue;
break;
}
ScheduleNextDelayedTask();
} else if (msg->message_id == kStopMessageId) {
DCHECK(stopping_);
// Stop the thread only if there are no more non-delayed
// messages left in the queue, otherwise post another task to
// try again later.
int delay = thread_->GetDelay();
if (delay > 0 || delay == talk_base::kForever) {
stopping_ = false;
thread_->Quit();
} else {
thread_->Post(this, kStopMessageId);
}
} else {
NOTREACHED();
}
}
protected:
virtual ~JingleMessagePump() {}
private:
void ScheduleNextDelayedTask() {
if (!delayed_work_time_.is_null()) {
base::TimeTicks now = base::TimeTicks::Now();
int delay = static_cast<int>((delayed_work_time_ - now).InMilliseconds());
if (delay > 0) {
thread_->PostDelayed(delay, this, kRunTasksMessageId);
} else {
thread_->Post(this, kRunTasksMessageId);
}
}
}
talk_base::Thread* thread_;
Delegate* delegate_;
base::TimeTicks delayed_work_time_;
bool stopping_;
};
} // namespace
JingleThreadMessageLoop::JingleThreadMessageLoop(talk_base::Thread* thread)
: MessageLoop(MessageLoop::TYPE_IO) {
pump_ = new JingleMessagePump(thread);
}
JingleThreadMessageLoop::~JingleThreadMessageLoop() {
}
TaskPump::TaskPump() {
}
void TaskPump::WakeTasks() {
talk_base::Thread::Current()->Post(this);
}
int64 TaskPump::CurrentTime() {
return static_cast<int64>(talk_base::Time());
}
void TaskPump::OnMessage(talk_base::Message* pmsg) {
RunTasks();
}
JingleThread::JingleThread()
: task_pump_(NULL),
started_event_(true, false),
stopped_event_(true, false),
message_loop_(NULL) {
}
JingleThread::~JingleThread() {
// It is important to call Stop here. If we wait for the base class to
// call Stop in its d'tor, then JingleThread::Run() will access member
// variables that are already gone. See similar comments in
// base/threading/thread.h.
if (message_loop_)
Stop();
}
bool JingleThread::Start() {
if (!Thread::Start())
return false;
started_event_.Wait();
return true;
}
void JingleThread::Run() {
JingleThreadMessageLoop message_loop(this);
message_loop_ = &message_loop;
message_loop_proxy_ = base::MessageLoopProxy::current();
TaskPump task_pump;
task_pump_ = &task_pump;
// Signal after we've initialized |message_loop_| and |task_pump_|.
started_event_.Signal();
message_loop.Run();
stopped_event_.Signal();
task_pump_ = NULL;
message_loop_ = NULL;
}
void JingleThread::Stop() {
message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
stopped_event_.Wait();
// This will wait until the thread is actually finished.
Thread::Stop();
}
MessageLoop* JingleThread::message_loop() {
return message_loop_;
}
base::MessageLoopProxy* JingleThread::message_loop_proxy() {
return message_loop_proxy_;
}
TaskPump* JingleThread::task_pump() {
return task_pump_;
}
} // namespace remoting
|