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

Side by Side Diff: third_party/mojo/src/mojo/edk/system/data_pipe.h

Issue 1676913002: [mojo] Delete third_party/mojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: let's try that again Created 4 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_DATA_PIPE_H_
6 #define THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_DATA_PIPE_H_
7
8 #include <stdint.h>
9
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/synchronization/lock.h"
13 #include "mojo/public/c/system/data_pipe.h"
14 #include "mojo/public/c/system/types.h"
15 #include "mojo/public/cpp/system/macros.h"
16 #include "third_party/mojo/src/mojo/edk/embedder/platform_handle_vector.h"
17 #include "third_party/mojo/src/mojo/edk/system/channel_endpoint_client.h"
18 #include "third_party/mojo/src/mojo/edk/system/handle_signals_state.h"
19 #include "third_party/mojo/src/mojo/edk/system/memory.h"
20 #include "third_party/mojo/src/mojo/edk/system/system_impl_export.h"
21
22 namespace mojo {
23 namespace system {
24
25 class Awakable;
26 class AwakableList;
27 class Channel;
28 class ChannelEndpoint;
29 class DataPipeImpl;
30 class MessageInTransitQueue;
31
32 // |DataPipe| is a base class for secondary objects implementing data pipes,
33 // similar to |MessagePipe| (see the explanatory comment in core.cc). It is
34 // typically owned by the dispatcher(s) corresponding to the local endpoints.
35 // Its subclasses implement the three cases: local producer and consumer, local
36 // producer and remote consumer, and remote producer and local consumer. This
37 // class is thread-safe.
38 class MOJO_SYSTEM_IMPL_EXPORT DataPipe final : public ChannelEndpointClient {
39 public:
40 // The default options for |MojoCreateDataPipe()|. (Real uses should obtain
41 // this via |ValidateCreateOptions()| with a null |in_options|; this is
42 // exposed directly for testing convenience.)
43 static MojoCreateDataPipeOptions GetDefaultCreateOptions();
44
45 // Validates and/or sets default options for |MojoCreateDataPipeOptions|. If
46 // non-null, |in_options| must point to a struct of at least
47 // |in_options->struct_size| bytes. |out_options| must point to a (current)
48 // |MojoCreateDataPipeOptions| and will be entirely overwritten on success (it
49 // may be partly overwritten on failure).
50 static MojoResult ValidateCreateOptions(
51 UserPointer<const MojoCreateDataPipeOptions> in_options,
52 MojoCreateDataPipeOptions* out_options);
53
54 // Creates a local (both producer and consumer) data pipe (using
55 // |LocalDataPipeImpl|. |validated_options| should be the output of
56 // |ValidateOptions()|. In particular: |struct_size| is ignored (so
57 // |validated_options| must be the current version of the struct) and
58 // |capacity_num_bytes| must be nonzero.
59 static DataPipe* CreateLocal(
60 const MojoCreateDataPipeOptions& validated_options);
61
62 // Creates a data pipe with a remote producer and a local consumer, using an
63 // existing |ChannelEndpoint| (whose |ReplaceClient()| it'll call) and taking
64 // |message_queue|'s contents as already-received incoming messages. If
65 // |channel_endpoint| is null, this will create a "half-open" data pipe (with
66 // only the consumer open). Note that this may fail, in which case it returns
67 // null.
68 static DataPipe* CreateRemoteProducerFromExisting(
69 const MojoCreateDataPipeOptions& validated_options,
70 MessageInTransitQueue* message_queue,
71 ChannelEndpoint* channel_endpoint);
72
73 // Creates a data pipe with a local producer and a remote consumer, using an
74 // existing |ChannelEndpoint| (whose |ReplaceClient()| it'll call) and taking
75 // |message_queue|'s contents as already-received incoming messages
76 // (|message_queue| may be null). If |channel_endpoint| is null, this will
77 // create a "half-open" data pipe (with only the producer open). Note that
78 // this may fail, in which case it returns null.
79 static DataPipe* CreateRemoteConsumerFromExisting(
80 const MojoCreateDataPipeOptions& validated_options,
81 size_t consumer_num_bytes,
82 MessageInTransitQueue* message_queue,
83 ChannelEndpoint* channel_endpoint);
84
85 // Used by |DataPipeProducerDispatcher::Deserialize()|. Returns true on
86 // success (in which case, |*data_pipe| is set appropriately) and false on
87 // failure (in which case |*data_pipe| may or may not be set to null).
88 static bool ProducerDeserialize(Channel* channel,
89 const void* source,
90 size_t size,
91 scoped_refptr<DataPipe>* data_pipe);
92
93 // Used by |DataPipeConsumerDispatcher::Deserialize()|. Returns true on
94 // success (in which case, |*data_pipe| is set appropriately) and false on
95 // failure (in which case |*data_pipe| may or may not be set to null).
96 static bool ConsumerDeserialize(Channel* channel,
97 const void* source,
98 size_t size,
99 scoped_refptr<DataPipe>* data_pipe);
100
101 // These are called by the producer dispatcher to implement its methods of
102 // corresponding names.
103 void ProducerCancelAllAwakables();
104 void ProducerClose();
105 MojoResult ProducerWriteData(UserPointer<const void> elements,
106 UserPointer<uint32_t> num_bytes,
107 bool all_or_none);
108 MojoResult ProducerBeginWriteData(UserPointer<void*> buffer,
109 UserPointer<uint32_t> buffer_num_bytes);
110 MojoResult ProducerEndWriteData(uint32_t num_bytes_written);
111 HandleSignalsState ProducerGetHandleSignalsState();
112 MojoResult ProducerAddAwakable(Awakable* awakable,
113 MojoHandleSignals signals,
114 uintptr_t context,
115 HandleSignalsState* signals_state);
116 void ProducerRemoveAwakable(Awakable* awakable,
117 HandleSignalsState* signals_state);
118 void ProducerStartSerialize(Channel* channel,
119 size_t* max_size,
120 size_t* max_platform_handles);
121 bool ProducerEndSerialize(Channel* channel,
122 void* destination,
123 size_t* actual_size,
124 embedder::PlatformHandleVector* platform_handles);
125 bool ProducerIsBusy() const;
126
127 // These are called by the consumer dispatcher to implement its methods of
128 // corresponding names.
129 void ConsumerCancelAllAwakables();
130 void ConsumerClose();
131 // This does not validate its arguments, except to check that |*num_bytes| is
132 // a multiple of |element_num_bytes_|.
133 MojoResult ConsumerReadData(UserPointer<void> elements,
134 UserPointer<uint32_t> num_bytes,
135 bool all_or_none,
136 bool peek);
137 MojoResult ConsumerDiscardData(UserPointer<uint32_t> num_bytes,
138 bool all_or_none);
139 MojoResult ConsumerQueryData(UserPointer<uint32_t> num_bytes);
140 MojoResult ConsumerBeginReadData(UserPointer<const void*> buffer,
141 UserPointer<uint32_t> buffer_num_bytes);
142 MojoResult ConsumerEndReadData(uint32_t num_bytes_read);
143 HandleSignalsState ConsumerGetHandleSignalsState();
144 MojoResult ConsumerAddAwakable(Awakable* awakable,
145 MojoHandleSignals signals,
146 uintptr_t context,
147 HandleSignalsState* signals_state);
148 void ConsumerRemoveAwakable(Awakable* awakable,
149 HandleSignalsState* signals_state);
150 void ConsumerStartSerialize(Channel* channel,
151 size_t* max_size,
152 size_t* max_platform_handles);
153 bool ConsumerEndSerialize(Channel* channel,
154 void* destination,
155 size_t* actual_size,
156 embedder::PlatformHandleVector* platform_handles);
157 bool ConsumerIsBusy() const;
158
159 // The following are only to be used by |DataPipeImpl| (and its subclasses):
160
161 // Replaces |impl_| with |new_impl| (which must not be null). For use when
162 // serializing data pipe dispatchers (i.e., in |ProducerEndSerialize()| and
163 // |ConsumerEndSerialize()|). Returns the old value of |impl_| (in case the
164 // caller needs to manage its lifetime).
165 scoped_ptr<DataPipeImpl> ReplaceImplNoLock(scoped_ptr<DataPipeImpl> new_impl);
166 void SetProducerClosedNoLock();
167 void SetConsumerClosedNoLock();
168
169 void ProducerCloseNoLock();
170 void ConsumerCloseNoLock();
171
172 // Thread-safe and fast (they don't take the lock):
173 const MojoCreateDataPipeOptions& validated_options() const {
174 return validated_options_;
175 }
176 size_t element_num_bytes() const {
177 return validated_options_.element_num_bytes;
178 }
179 size_t capacity_num_bytes() const {
180 return validated_options_.capacity_num_bytes;
181 }
182
183 // Must be called under lock.
184 bool producer_open_no_lock() const {
185 lock_.AssertAcquired();
186 return producer_open_;
187 }
188 bool consumer_open_no_lock() const {
189 lock_.AssertAcquired();
190 return consumer_open_;
191 }
192 uint32_t producer_two_phase_max_num_bytes_written_no_lock() const {
193 lock_.AssertAcquired();
194 return producer_two_phase_max_num_bytes_written_;
195 }
196 uint32_t consumer_two_phase_max_num_bytes_read_no_lock() const {
197 lock_.AssertAcquired();
198 return consumer_two_phase_max_num_bytes_read_;
199 }
200 void set_producer_two_phase_max_num_bytes_written_no_lock(
201 uint32_t num_bytes) {
202 lock_.AssertAcquired();
203 producer_two_phase_max_num_bytes_written_ = num_bytes;
204 }
205 void set_consumer_two_phase_max_num_bytes_read_no_lock(uint32_t num_bytes) {
206 lock_.AssertAcquired();
207 consumer_two_phase_max_num_bytes_read_ = num_bytes;
208 }
209 bool producer_in_two_phase_write_no_lock() const {
210 lock_.AssertAcquired();
211 return producer_two_phase_max_num_bytes_written_ > 0;
212 }
213 bool consumer_in_two_phase_read_no_lock() const {
214 lock_.AssertAcquired();
215 return consumer_two_phase_max_num_bytes_read_ > 0;
216 }
217
218 private:
219 // |validated_options| should be the output of |ValidateOptions()|. In
220 // particular: |struct_size| is ignored (so |validated_options| must be the
221 // current version of the struct) and |capacity_num_bytes| must be nonzero.
222 // TODO(vtl): |has_local_producer|/|has_local_consumer| shouldn't really be
223 // arguments here. Instead, they should be determined from the |impl| ... but
224 // the |impl|'s typically figures these out by examining the owner, i.e., the
225 // |DataPipe| object. Probably, this indicates that more stuff should be moved
226 // to |DataPipeImpl|, but for now we'll live with this.
227 DataPipe(bool has_local_producer,
228 bool has_local_consumer,
229 const MojoCreateDataPipeOptions& validated_options,
230 scoped_ptr<DataPipeImpl> impl);
231 ~DataPipe() override;
232
233 // |ChannelEndpointClient| implementation:
234 bool OnReadMessage(unsigned port, MessageInTransit* message) override;
235 void OnDetachFromChannel(unsigned port) override;
236
237 void AwakeProducerAwakablesForStateChangeNoLock(
238 const HandleSignalsState& new_producer_state);
239 void AwakeConsumerAwakablesForStateChangeNoLock(
240 const HandleSignalsState& new_consumer_state);
241
242 void SetProducerClosed();
243 void SetConsumerClosed();
244
245 bool has_local_producer_no_lock() const {
246 lock_.AssertAcquired();
247 return !!producer_awakable_list_;
248 }
249 bool has_local_consumer_no_lock() const {
250 lock_.AssertAcquired();
251 return !!consumer_awakable_list_;
252 }
253
254 MSVC_SUPPRESS_WARNING(4324)
255 const MojoCreateDataPipeOptions validated_options_;
256
257 mutable base::Lock lock_; // Protects the following members.
258 // *Known* state of producer or consumer.
259 bool producer_open_;
260 bool consumer_open_;
261 // Non-null only if the producer or consumer, respectively, is local.
262 scoped_ptr<AwakableList> producer_awakable_list_;
263 scoped_ptr<AwakableList> consumer_awakable_list_;
264 // These are nonzero if and only if a two-phase write/read is in progress.
265 uint32_t producer_two_phase_max_num_bytes_written_;
266 uint32_t consumer_two_phase_max_num_bytes_read_;
267 scoped_ptr<DataPipeImpl> impl_;
268
269 MOJO_DISALLOW_COPY_AND_ASSIGN(DataPipe);
270 };
271
272 } // namespace system
273 } // namespace mojo
274
275 #endif // THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_DATA_PIPE_H_
OLDNEW
« no previous file with comments | « third_party/mojo/src/mojo/edk/system/core_unittest.cc ('k') | third_party/mojo/src/mojo/edk/system/data_pipe.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698