OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_INTERFACE_PTR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_INTERFACE_PTR_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_INTERFACE_PTR_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_INTERFACE_PTR_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 base::MakeUnique<ForwardToCallingThread>(std::move(responder)); | 110 base::MakeUnique<ForwardToCallingThread>(std::move(responder)); |
111 task_runner_->PostTask( | 111 task_runner_->PostTask( |
112 FROM_HERE, base::Bind(forward_with_responder_, base::Passed(message), | 112 FROM_HERE, base::Bind(forward_with_responder_, base::Passed(message), |
113 base::Passed(&reply_forwarder))); | 113 base::Passed(&reply_forwarder))); |
114 return true; | 114 return true; |
115 } | 115 } |
116 | 116 |
117 SyncCallRestrictions::AssertSyncCallAllowed(); | 117 SyncCallRestrictions::AssertSyncCallAllowed(); |
118 | 118 |
119 // If the InterfacePtr is bound to this thread, dispatch it directly. | 119 // If the InterfacePtr is bound to this thread, dispatch it directly. |
120 if (task_runner_->RunsTasksOnCurrentThread()) { | 120 if (task_runner_->RunsTasksInCurrentSequence()) { |
121 forward_with_responder_.Run(std::move(*message), std::move(responder)); | 121 forward_with_responder_.Run(std::move(*message), std::move(responder)); |
122 return true; | 122 return true; |
123 } | 123 } |
124 | 124 |
125 // If the InterfacePtr is bound on another thread, post the call. | 125 // If the InterfacePtr is bound on another thread, post the call. |
126 // TODO(yzshen, watk): We block both this thread and the InterfacePtr | 126 // TODO(yzshen, watk): We block both this thread and the InterfacePtr |
127 // thread. Ideally only this thread would block. | 127 // thread. Ideally only this thread would block. |
128 auto response = make_scoped_refptr(new SyncResponseInfo()); | 128 auto response = make_scoped_refptr(new SyncResponseInfo()); |
129 auto response_signaler = base::MakeUnique<SyncResponseSignaler>(response); | 129 auto response_signaler = base::MakeUnique<SyncResponseSignaler>(response); |
130 task_runner_->PostTask( | 130 task_runner_->PostTask( |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 base::Bind(&PtrWrapper::AcceptWithResponder, this), | 328 base::Bind(&PtrWrapper::AcceptWithResponder, this), |
329 associated_group_); | 329 associated_group_); |
330 } | 330 } |
331 | 331 |
332 private: | 332 private: |
333 friend struct PtrWrapperDeleter; | 333 friend struct PtrWrapperDeleter; |
334 | 334 |
335 ~PtrWrapper() {} | 335 ~PtrWrapper() {} |
336 | 336 |
337 void Bind(PtrInfoType ptr_info) { | 337 void Bind(PtrInfoType ptr_info) { |
338 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 338 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
339 ptr_.Bind(std::move(ptr_info)); | 339 ptr_.Bind(std::move(ptr_info)); |
340 } | 340 } |
341 | 341 |
342 void Accept(Message message) { | 342 void Accept(Message message) { |
343 ptr_.internal_state()->ForwardMessage(std::move(message)); | 343 ptr_.internal_state()->ForwardMessage(std::move(message)); |
344 } | 344 } |
345 | 345 |
346 void AcceptWithResponder(Message message, | 346 void AcceptWithResponder(Message message, |
347 std::unique_ptr<MessageReceiver> responder) { | 347 std::unique_ptr<MessageReceiver> responder) { |
348 ptr_.internal_state()->ForwardMessageWithResponder(std::move(message), | 348 ptr_.internal_state()->ForwardMessageWithResponder(std::move(message), |
349 std::move(responder)); | 349 std::move(responder)); |
350 } | 350 } |
351 | 351 |
352 void DeleteOnCorrectThread() const { | 352 void DeleteOnCorrectThread() const { |
353 if (!task_runner_->RunsTasksOnCurrentThread()) { | 353 if (!task_runner_->RunsTasksInCurrentSequence()) { |
354 // NOTE: This is only called when there are no more references to | 354 // NOTE: This is only called when there are no more references to |
355 // |this|, so binding it unretained is both safe and necessary. | 355 // |this|, so binding it unretained is both safe and necessary. |
356 task_runner_->PostTask(FROM_HERE, | 356 task_runner_->PostTask(FROM_HERE, |
357 base::Bind(&PtrWrapper::DeleteOnCorrectThread, | 357 base::Bind(&PtrWrapper::DeleteOnCorrectThread, |
358 base::Unretained(this))); | 358 base::Unretained(this))); |
359 } else { | 359 } else { |
360 delete this; | 360 delete this; |
361 } | 361 } |
362 } | 362 } |
363 | 363 |
(...skipping 21 matching lines...) Expand all Loading... |
385 using ThreadSafeAssociatedInterfacePtr = | 385 using ThreadSafeAssociatedInterfacePtr = |
386 ThreadSafeInterfacePtrBase<AssociatedInterfacePtr<Interface>>; | 386 ThreadSafeInterfacePtrBase<AssociatedInterfacePtr<Interface>>; |
387 | 387 |
388 template <typename Interface> | 388 template <typename Interface> |
389 using ThreadSafeInterfacePtr = | 389 using ThreadSafeInterfacePtr = |
390 ThreadSafeInterfacePtrBase<InterfacePtr<Interface>>; | 390 ThreadSafeInterfacePtrBase<InterfacePtr<Interface>>; |
391 | 391 |
392 } // namespace mojo | 392 } // namespace mojo |
393 | 393 |
394 #endif // MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_INTERFACE_PTR_H_ | 394 #endif // MOJO_PUBLIC_CPP_BINDINGS_THREAD_SAFE_INTERFACE_PTR_H_ |
OLD | NEW |