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

Side by Side Diff: mojo/public/bindings/lib/connector.cc

Issue 65043004: Add better handle tracking, and optimize Connector::Accept. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month 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
« no previous file with comments | « mojo/public/bindings/lib/connector.h ('k') | mojo/public/bindings/lib/message.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "mojo/public/bindings/lib/connector.h" 5 #include "mojo/public/bindings/lib/connector.h"
6 6
7 #include <assert.h> 7 #include <assert.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 19 matching lines...) Expand all
30 assert(!incoming_receiver_); 30 assert(!incoming_receiver_);
31 incoming_receiver_ = receiver; 31 incoming_receiver_ = receiver;
32 if (incoming_receiver_) 32 if (incoming_receiver_)
33 WaitToReadMore(); 33 WaitToReadMore();
34 } 34 }
35 35
36 bool Connector::Accept(Message* message) { 36 bool Connector::Accept(Message* message) {
37 if (error_) 37 if (error_)
38 return false; 38 return false;
39 39
40 write_queue_.Push(message); 40 bool wait_to_write;
41 WriteMore(); 41 WriteOne(message, &wait_to_write);
42
43 if (wait_to_write) {
viettrungluu 2013/11/08 20:18:38 I assume that wait_to_write should never be set on
44 WaitToWriteMore();
45 if (!error_)
46 write_queue_.Push(message);
47 }
48
42 return !error_; 49 return !error_;
43 } 50 }
44 51
45 void Connector::OnHandleReady(Callback* callback, MojoResult result) { 52 void Connector::OnHandleReady(Callback* callback, MojoResult result) {
46 if (callback == &read_callback_) 53 if (callback == &read_callback_)
47 ReadMore(); 54 ReadMore();
48 if (callback == &write_callback_) 55 if (callback == &write_callback_)
49 WriteMore(); 56 WriteMore();
50 } 57 }
51 58
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 if (rv != MOJO_RESULT_OK) { 111 if (rv != MOJO_RESULT_OK) {
105 error_ = true; 112 error_ = true;
106 break; 113 break;
107 } 114 }
108 115
109 incoming_receiver_->Accept(&message); 116 incoming_receiver_->Accept(&message);
110 } 117 }
111 } 118 }
112 119
113 void Connector::WriteMore() { 120 void Connector::WriteMore() {
114 while (!write_queue_.IsEmpty()) { 121 while (!error_ && !write_queue_.IsEmpty()) {
115 const Message* message = write_queue_.Peek(); 122 Message* message = write_queue_.Peek();
116 123
117 MojoResult rv = WriteMessage(message_pipe_, 124 bool wait_to_write;
118 message->data, 125 WriteOne(message, &wait_to_write);
119 message->data->header.num_bytes, 126 if (wait_to_write)
120 message->handles.data(), 127 break;
121 message->handles.size(),
122 MOJO_WRITE_MESSAGE_FLAG_NONE);
123 if (rv == MOJO_RESULT_OK) {
124 // TODO(darin): Handles were successfully transferred, and so we need
125 // to take care not to Close them here.
126 write_queue_.Pop();
127 continue; // Write another message.
128 }
129 128
130 error_ = true; 129 write_queue_.Pop();
131 break;
132 } 130 }
133 } 131 }
134 132
133 void Connector::WriteOne(Message* message, bool* wait_to_write) {
134 // TODO(darin): WriteMessage will eventually start generating an error that
135 // it cannot accept more data. In that case, we'll need to wait on the pipe
136 // to determine when we can try writing again. This flag will be set to true
137 // in that case.
138 *wait_to_write = false;
139
140 MojoResult rv = WriteMessage(message_pipe_,
141 message->data,
142 message->data->header.num_bytes,
143 message->handles.data(),
144 message->handles.size(),
viettrungluu 2013/11/08 20:18:38 You need to static_cast this to uint32_t, or else
145 MOJO_WRITE_MESSAGE_FLAG_NONE);
146 if (rv == MOJO_RESULT_OK) {
147 // The handles were successfully transferred, so we don't need the message
148 // to track their lifetime any longer.
149 message->handles.clear();
150 } else {
151 error_ = true;
152 }
153 }
154
135 // ---------------------------------------------------------------------------- 155 // ----------------------------------------------------------------------------
136 156
137 Connector::Callback::Callback() 157 Connector::Callback::Callback()
138 : owner_(NULL) { 158 : owner_(NULL) {
139 } 159 }
140 160
141 void Connector::Callback::Cancel() { 161 void Connector::Callback::Cancel() {
142 owner_ = NULL; 162 owner_ = NULL;
143 BindingsSupport::Get()->CancelWait(this); 163 BindingsSupport::Get()->CancelWait(this);
144 } 164 }
145 165
146 void Connector::Callback::SetOwnerToNotify(Connector* owner) { 166 void Connector::Callback::SetOwnerToNotify(Connector* owner) {
147 assert(!owner_); 167 assert(!owner_);
148 owner_ = owner; 168 owner_ = owner;
149 } 169 }
150 170
151 bool Connector::Callback::IsPending() const { 171 bool Connector::Callback::IsPending() const {
152 return owner_ != NULL; 172 return owner_ != NULL;
153 } 173 }
154 174
155 void Connector::Callback::OnHandleReady(MojoResult result) { 175 void Connector::Callback::OnHandleReady(MojoResult result) {
156 assert(owner_); 176 assert(owner_);
157 Connector* owner = NULL; 177 Connector* owner = NULL;
158 std::swap(owner, owner_); 178 std::swap(owner, owner_);
159 owner->OnHandleReady(this, result); 179 owner->OnHandleReady(this, result);
160 } 180 }
161 181
162 } // namespace mojo 182 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/bindings/lib/connector.h ('k') | mojo/public/bindings/lib/message.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698