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

Side by Side Diff: mojo/edk/embedder/embedder.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/embedder/embedder.h" 5 #include "mojo/edk/embedder/embedder.h"
6 6
7 #include "base/atomicops.h" 7 #include "base/atomicops.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 29 matching lines...) Expand all
40 // Don't allow the counter to wrap. Note that any (strictly) positive value is 40 // Don't allow the counter to wrap. Note that any (strictly) positive value is
41 // a valid |ChannelId| (and |NoBarrier_AtomicIncrement()| returns the value 41 // a valid |ChannelId| (and |NoBarrier_AtomicIncrement()| returns the value
42 // post-increment). 42 // post-increment).
43 CHECK_GT(new_counter_value, 0); 43 CHECK_GT(new_counter_value, 0);
44 // Use "negative" values for these IDs, so that we'll also be able to use 44 // Use "negative" values for these IDs, so that we'll also be able to use
45 // "positive" "process identifiers" (see connection_manager.h) as IDs (and 45 // "positive" "process identifiers" (see connection_manager.h) as IDs (and
46 // they won't conflict). 46 // they won't conflict).
47 return static_cast<system::ChannelId>(-new_counter_value); 47 return static_cast<system::ChannelId>(-new_counter_value);
48 } 48 }
49 49
50 // Helper for |CreateChannel()|. Called on the channel creation thread.
51 void CreateChannelHelper(
52 ScopedPlatformHandle platform_handle,
53 scoped_ptr<ChannelInfo> channel_info,
54 scoped_refptr<system::ChannelEndpoint> channel_endpoint,
55 DidCreateChannelCallback callback,
56 scoped_refptr<base::TaskRunner> callback_thread_task_runner) {
57 channel_info->channel_id = MakeChannelId();
58 internal::g_channel_manager->CreateChannelOnIOThread(
59 channel_info->channel_id, platform_handle.Pass(), channel_endpoint);
60
61 // Hand the channel back to the embedder.
62 if (callback_thread_task_runner) {
63 callback_thread_task_runner->PostTask(
64 FROM_HERE, base::Bind(callback, channel_info.release()));
65 } else {
66 callback.Run(channel_info.release());
67 }
68 }
69
70 } // namespace 50 } // namespace
71 51
72 namespace internal { 52 namespace internal {
73 53
74 // Declared in embedder_internal.h. 54 // Declared in embedder_internal.h.
75 PlatformSupport* g_platform_support = nullptr; 55 PlatformSupport* g_platform_support = nullptr;
76 system::Core* g_core = nullptr; 56 system::Core* g_core = nullptr;
77 system::ChannelManager* g_channel_manager = nullptr; 57 system::ChannelManager* g_channel_manager = nullptr;
78 58
79 } // namespace internal 59 } // namespace internal
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint); 112 system::MessagePipeDispatcher::CreateRemoteMessagePipe(&channel_endpoint);
133 113
134 DCHECK(internal::g_core); 114 DCHECK(internal::g_core);
135 ScopedMessagePipeHandle rv( 115 ScopedMessagePipeHandle rv(
136 MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher))); 116 MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher)));
137 117
138 // We'll have to set |channel_info->channel_id| on the I/O thread. 118 // We'll have to set |channel_info->channel_id| on the I/O thread.
139 scoped_ptr<ChannelInfo> channel_info(new ChannelInfo()); 119 scoped_ptr<ChannelInfo> channel_info(new ChannelInfo());
140 120
141 if (rv.is_valid()) { 121 if (rv.is_valid()) {
142 io_thread_task_runner->PostTask( 122 channel_info->channel_id = MakeChannelId();
143 FROM_HERE, 123 internal::g_channel_manager->CreateChannel(
144 base::Bind(&CreateChannelHelper, base::Passed(&platform_handle), 124 channel_info->channel_id, platform_handle.Pass(), channel_endpoint,
145 base::Passed(&channel_info), channel_endpoint, callback, 125 io_thread_task_runner,
146 callback_thread_task_runner)); 126 base::Bind(callback, base::Unretained(channel_info.release())),
127 callback_thread_task_runner);
147 } else { 128 } else {
148 (callback_thread_task_runner ? callback_thread_task_runner 129 (callback_thread_task_runner ? callback_thread_task_runner
149 : io_thread_task_runner) 130 : io_thread_task_runner)
150 ->PostTask(FROM_HERE, base::Bind(callback, channel_info.release())); 131 ->PostTask(FROM_HERE, base::Bind(callback, channel_info.release()));
151 } 132 }
152 133
153 return rv.Pass(); 134 return rv.Pass();
154 } 135 }
155 136
156 // TODO(vtl): Write tests for this. 137 // TODO(vtl): Write tests for this.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 } 192 }
212 193
213 MojoResult AsyncWait(MojoHandle handle, 194 MojoResult AsyncWait(MojoHandle handle,
214 MojoHandleSignals signals, 195 MojoHandleSignals signals,
215 base::Callback<void(MojoResult)> callback) { 196 base::Callback<void(MojoResult)> callback) {
216 return internal::g_core->AsyncWait(handle, signals, callback); 197 return internal::g_core->AsyncWait(handle, signals, callback);
217 } 198 }
218 199
219 } // namespace embedder 200 } // namespace embedder
220 } // namespace mojo 201 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | mojo/edk/embedder/embedder_unittest.cc » ('j') | mojo/edk/system/channel_manager.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698