Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: jingle/glue/thread_wrapper.cc

Issue 6747017: Add JingleThreadWrapper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "jingle/glue/thread_wrapper.h"
6
7 namespace jingle_glue {
8
9 JingleThreadWrapper::JingleThreadWrapper(MessageLoop* message_loop)
10 : message_loop_(message_loop) {
11 DCHECK_EQ(message_loop_, MessageLoop::current());
12
13 talk_base::ThreadManager::SetCurrent(this);
14 message_loop_->AddDestructionObserver(this);
15 }
16
17 JingleThreadWrapper::~JingleThreadWrapper() {
18 }
19
20 void JingleThreadWrapper::WillDestroyCurrentMessageLoop() {
21 talk_base::ThreadManager::SetCurrent(NULL);
22 message_loop_->RemoveDestructionObserver(this);
23 delete this;
24 }
25
26 void JingleThreadWrapper::Post(
27 talk_base::MessageHandler* handler, uint32 id,
28 talk_base::MessageData* data, bool time_sensitive) {
29 bool queue_was_empty;
30 talk_base::Message message;
31 message.phandler = handler;
32 message.message_id = id;
33 message.pdata = data;
34 {
35 base::AutoLock auto_lock(lock_);
36 queue_was_empty = messages_.empty();
37 messages_.push_back(message);
38 }
39
40 // Post new task only if there is the queue was empty.
41 if (!queue_was_empty) {
42 message_loop_->PostTask(FROM_HERE, NewRunnableMethod(
43 this, &JingleThreadWrapper::RunPendingTasks));
44 }
45 }
46
47 void JingleThreadWrapper::PostDelayed(
48 int delay_ms, talk_base::MessageHandler* handler,
49 uint32 id, talk_base::MessageData* data) {
50 talk_base::Message message;
51 message.phandler = handler;
52 message.message_id = id;
53 message.pdata = data;
54 talk_base::DelayedMessage delayed_message(
55 delay_ms, talk_base::TimeAfter(delay_ms), dmsgq_next_num_++, message);
56 {
57 base::AutoLock auto_lock(lock_);
58 delayed_messages_.push_back(delayed_message);
59 std::push_heap(delayed_messages_.begin(), delayed_messages_.end());
60 }
61
62 message_loop_->PostDelayedTask(
63 FROM_HERE, NewRunnableMethod(this, &JingleThreadWrapper::RunDelayedTask,
64 delayed_message.msTrigger_),
65 delay_ms);
66 }
67
68 void JingleThreadWrapper::Clear(talk_base::MessageHandler* handler, uint32 id,
69 talk_base::MessageList* removed) {
70 base::AutoLock auto_lock(lock_);
71
72 for (std::list<talk_base::Message>::iterator it = messages_.begin();
Alpha Left Google 2011/03/26 01:06:01 These two loops look really similar, please add co
Sergey Ulanov 2011/03/26 01:44:24 Done.
73 it != messages_.end();) {
74 if (it->Match(handler, id)) {
75 if (removed) {
76 removed->push_back(*it);
77 } else {
78 delete it->pdata;
79 }
80 it = messages_.erase(it);
81 } else {
82 ++it;
83 }
84 }
85
86 for (DelayedMessagesHeap::iterator it = delayed_messages_.begin();
87 it != delayed_messages_.end();) {
88 if (it->msg_.Match(handler, id)) {
89 if (removed) {
90 removed->push_back(it->msg_);
91 } else {
92 delete it->msg_.pdata;
93 }
94 // Move last element to the current position.
95 *it = delayed_messages_.back();
96 delayed_messages_.erase(delayed_messages_.end() - 1);
Alpha Left Google 2011/03/26 01:06:01 why not just use delayed_messages_.back()?
Sergey Ulanov 2011/03/26 01:44:24 erase() needs iterator. back() returns reference t
97 } else {
98 ++it;
99 }
100 }
101
102 // Sort the heap again after we've removed some messages.
103 std::make_heap(delayed_messages_.begin(), delayed_messages_.end());
Alpha Left Google 2011/03/26 01:06:01 where is the comparator for messages, add comment
Sergey Ulanov 2011/03/26 01:44:24 It's defined in talk_base::DelayedMessage. Anyway
104 }
105
106 void JingleThreadWrapper::RunPendingTasks() {
107 bool have_message;
108
109 do {
110 have_message = false;
111 talk_base::Message message;
112 {
113 base::AutoLock auto_lock(lock_);
114 if (!messages_.empty()) {
115 have_message = true;
116 message = messages_.front();
117 messages_.pop_front();
118 }
119 }
120
121 if (have_message) {
122 message.phandler->OnMessage(&message);
123 delete message.pdata;
124 }
125 } while (have_message);
126 }
127
128 void JingleThreadWrapper::RunDelayedTask(uint32 task_time) {
129 bool have_message = false;
130 talk_base::Message message;
131
132 {
133 base::AutoLock auto_lock(lock_);
134 if (!delayed_messages_.empty() &&
135 delayed_messages_.front().msTrigger_ == task_time) {
136 have_message = true;
137 message = delayed_messages_.front().msg_;
138 std::pop_heap(delayed_messages_.begin(), delayed_messages_.end());
139 delayed_messages_.erase(delayed_messages_.end() - 1);
Alpha Left Google 2011/03/26 01:06:01 why not use delayed_messages_.back()?
Sergey Ulanov 2011/03/26 01:44:24 back() returns reference to last element. erase()
140 }
141 }
142
143 if (have_message) {
144 message.phandler->OnMessage(&message);
145 delete message.pdata;
146 }
147 }
148
149 void JingleThreadWrapper::Quit() {
150 NOTREACHED();
Alpha Left Google 2011/03/26 01:06:01 These methods are NOTREACHED(), why? Explain pleas
Sergey Ulanov 2011/03/26 01:44:24 This is mentioned in the header. Added more commen
151 }
152
153 bool JingleThreadWrapper::IsQuitting() {
154 NOTREACHED();
155 return false;
156 }
157
158 void JingleThreadWrapper::Restart() {
159 NOTREACHED();
160 }
161
162 bool JingleThreadWrapper::Get(talk_base::Message*, int, bool) {
163 NOTREACHED();
164 return false;
165 }
166
167 bool JingleThreadWrapper::Peek(talk_base::Message*, int) {
168 NOTREACHED();
169 return false;
170 }
171
172 void JingleThreadWrapper::PostAt(uint32, talk_base::MessageHandler*,
173 uint32, talk_base::MessageData*) {
174 NOTREACHED();
175 }
176
177 void JingleThreadWrapper::Dispatch(talk_base::Message* msg) {
178 NOTREACHED();
179 }
180
181 void JingleThreadWrapper::ReceiveSends() {
182 NOTREACHED();
183 }
184
185 int JingleThreadWrapper::GetDelay() {
186 NOTREACHED();
187 return 0;
188 }
189
190 void JingleThreadWrapper::Stop() {
191 NOTREACHED();
192 }
193
194 void JingleThreadWrapper::Run() {
195 NOTREACHED();
196 }
197
198 } // namespace jingle_glue
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698