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

Side by Side Diff: mojo/edk/system/channel_manager.cc

Issue 899993002: Add a thread-safe CreateChannel method to ChannelManager. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/edk/system/channel_manager.h" 5 #include "mojo/edk/system/channel_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
8 #include "base/location.h" 9 #include "base/location.h"
9 #include "base/message_loop/message_loop_proxy.h" 10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/task_runner.h"
10 12
11 namespace mojo { 13 namespace mojo {
12 namespace system { 14 namespace system {
13 15
14 namespace { 16 namespace {
15 17
16 void ShutdownChannelHelper(const ChannelInfo& channel_info) { 18 void ShutdownChannelHelper(const ChannelInfo& channel_info) {
17 if (base::MessageLoopProxy::current() == 19 if (base::MessageLoopProxy::current() ==
18 channel_info.channel_thread_task_runner) { 20 channel_info.channel_thread_task_runner) {
19 channel_info.channel->Shutdown(); 21 channel_info.channel->Shutdown();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 54
53 { 55 {
54 base::AutoLock locker(lock_); 56 base::AutoLock locker(lock_);
55 CHECK(channel_infos_.find(channel_id) == channel_infos_.end()); 57 CHECK(channel_infos_.find(channel_id) == channel_infos_.end());
56 channel_infos_[channel_id] = 58 channel_infos_[channel_id] =
57 ChannelInfo(channel, base::MessageLoopProxy::current()); 59 ChannelInfo(channel, base::MessageLoopProxy::current());
58 } 60 }
59 channel->SetChannelManager(this); 61 channel->SetChannelManager(this);
60 } 62 }
61 63
64 void ChannelManager::CreateChannel(
65 ChannelId channel_id,
66 embedder::ScopedPlatformHandle platform_handle,
67 scoped_refptr<system::ChannelEndpoint> bootstrap_channel_endpoint,
68 scoped_refptr<base::TaskRunner> io_thread_task_runner,
69 base::Closure callback,
70 scoped_refptr<base::TaskRunner> callback_thread_task_runner) {
71 DCHECK(io_thread_task_runner);
72 DCHECK(!callback.is_null());
73 // (|callback_thread_task_runner| may be null.)
74
75 io_thread_task_runner->PostTask(
76 FROM_HERE,
77 base::Bind(&ChannelManager::CreateChannelHelper, base::Unretained(this),
78 channel_id, base::Passed(&platform_handle),
79 bootstrap_channel_endpoint, callback,
80 callback_thread_task_runner));
81 }
82
62 scoped_refptr<Channel> ChannelManager::GetChannel(ChannelId channel_id) const { 83 scoped_refptr<Channel> ChannelManager::GetChannel(ChannelId channel_id) const {
63 base::AutoLock locker(lock_); 84 base::AutoLock locker(lock_);
64 auto it = channel_infos_.find(channel_id); 85 auto it = channel_infos_.find(channel_id);
65 DCHECK(it != channel_infos_.end()); 86 DCHECK(it != channel_infos_.end());
66 return it->second.channel; 87 return it->second.channel;
67 } 88 }
68 89
69 void ChannelManager::WillShutdownChannel(ChannelId channel_id) { 90 void ChannelManager::WillShutdownChannel(ChannelId channel_id) {
70 GetChannel(channel_id)->WillShutdownSoon(); 91 GetChannel(channel_id)->WillShutdownSoon();
71 } 92 }
72 93
73 void ChannelManager::ShutdownChannel(ChannelId channel_id) { 94 void ChannelManager::ShutdownChannel(ChannelId channel_id) {
74 ChannelInfo channel_info; 95 ChannelInfo channel_info;
75 { 96 {
76 base::AutoLock locker(lock_); 97 base::AutoLock locker(lock_);
77 auto it = channel_infos_.find(channel_id); 98 auto it = channel_infos_.find(channel_id);
78 DCHECK(it != channel_infos_.end()); 99 DCHECK(it != channel_infos_.end());
79 channel_info.Swap(&it->second); 100 channel_info.Swap(&it->second);
80 channel_infos_.erase(it); 101 channel_infos_.erase(it);
81 } 102 }
82 ShutdownChannelHelper(channel_info); 103 ShutdownChannelHelper(channel_info);
83 } 104 }
84 105
106 void ChannelManager::CreateChannelHelper(
107 ChannelId channel_id,
108 embedder::ScopedPlatformHandle platform_handle,
109 scoped_refptr<system::ChannelEndpoint> bootstrap_channel_endpoint,
110 base::Closure callback,
111 scoped_refptr<base::TaskRunner> callback_thread_task_runner) {
112 CreateChannelOnIOThread(channel_id, platform_handle.Pass(),
113 bootstrap_channel_endpoint);
114 if (callback_thread_task_runner)
115 callback_thread_task_runner->PostTask(FROM_HERE, callback);
116 else
117 callback.Run();
118 }
119
85 } // namespace system 120 } // namespace system
86 } // namespace mojo 121 } // namespace mojo
OLDNEW
« mojo/edk/system/channel_manager.h ('K') | « mojo/edk/system/channel_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698