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

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

Issue 1526923006: [mojo] Implement data pipe using a shared buffer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_EDK_SYSTEM_DATA_PIPE_H_ 5 #ifndef MOJO_EDK_SYSTEM_DATA_PIPE_H_
6 #define MOJO_EDK_SYSTEM_DATA_PIPE_H_ 6 #define MOJO_EDK_SYSTEM_DATA_PIPE_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <vector>
9 10
11 #include "base/callback_forward.h"
10 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
11 #include "mojo/edk/embedder/platform_handle_vector.h" 14 #include "mojo/edk/embedder/platform_handle_vector.h"
15 #include "mojo/edk/embedder/platform_shared_buffer.h"
12 #include "mojo/edk/embedder/scoped_platform_handle.h" 16 #include "mojo/edk/embedder/scoped_platform_handle.h"
13 #include "mojo/edk/system/system_impl_export.h" 17 #include "mojo/edk/system/system_impl_export.h"
14 #include "mojo/public/c/system/data_pipe.h" 18 #include "mojo/public/c/system/data_pipe.h"
15 #include "mojo/public/c/system/types.h" 19 #include "mojo/public/c/system/types.h"
16 #include "mojo/public/cpp/system/macros.h" 20 #include "mojo/public/cpp/system/macros.h"
17 21
18 namespace mojo { 22 namespace mojo {
19 namespace edk { 23 namespace edk {
20 class RawChannel; 24 class RawChannel;
21 25
26 enum DataPipeCommand : uint32_t {
27 NOTIFY_SHARED_BUFFER,
Anand Mistry (off Chromium) 2016/01/11 06:19:34 I'm curious why you need this functionality. Since
Eliot Courtney 2016/01/13 00:00:10 Done.
28 DATA_WRITTEN,
29 DATA_READ
30 };
31
32 struct MOJO_ALIGNAS(8) DataPipeCommandHeader {
33 DataPipeCommand command;
34 uint32_t num_bytes;
35 };
36
22 // Shared code between DataPipeConsumerDispatcher and 37 // Shared code between DataPipeConsumerDispatcher and
23 // DataPipeProducerDispatcher. 38 // DataPipeProducerDispatcher. This class is not thread safe -- all locking must
24 class MOJO_SYSTEM_IMPL_EXPORT DataPipe { 39 // be performed by the caller. This class is only intended to be used as a
40 // writer or a reader, not both.
41 class MOJO_SYSTEM_IMPL_EXPORT DataPipe final
42 : public base::RefCountedThreadSafe<DataPipe> {
25 public: 43 public:
26 // The default options for |MojoCreateDataPipe()|. (Real uses should obtain 44 // The default options for |MojoCreateDataPipe()|. (Real uses should obtain
27 // this via |ValidateCreateOptions()| with a null |in_options|; this is 45 // this via |ValidateCreateOptions()| with a null |in_options|; this is
28 // exposed directly for testing convenience.) 46 // exposed directly for testing convenience.)
29 static MojoCreateDataPipeOptions GetDefaultCreateOptions(); 47 static MojoCreateDataPipeOptions GetDefaultCreateOptions();
30 48
31 // Validates and/or sets default options for |MojoCreateDataPipeOptions|. If 49 // Validates and/or sets default options for |MojoCreateDataPipeOptions|. If
32 // non-null, |in_options| must point to a struct of at least 50 // non-null, |in_options| must point to a struct of at least
33 // |in_options->struct_size| bytes. |out_options| must point to a (current) 51 // |in_options->struct_size| bytes. |out_options| must point to a (current)
34 // |MojoCreateDataPipeOptions| and will be entirely overwritten on success (it 52 // |MojoCreateDataPipeOptions| and will be entirely overwritten on success (it
35 // may be partly overwritten on failure). 53 // may be partly overwritten on failure).
36 static MojoResult ValidateCreateOptions( 54 static MojoResult ValidateCreateOptions(
37 const MojoCreateDataPipeOptions* in_options, 55 const MojoCreateDataPipeOptions* in_options,
38 MojoCreateDataPipeOptions* out_options); 56 MojoCreateDataPipeOptions* out_options);
39 57
58 explicit DataPipe(const MojoCreateDataPipeOptions& options);
59
60 void Init(ScopedPlatformHandle message_pipe,
Anand Mistry (off Chromium) 2016/01/11 06:19:34 Most of these arguments are details about the seri
Eliot Courtney 2016/01/13 00:00:10 Done.
61 char* serialized_write_buffer,
62 uint32_t serialized_write_buffer_size,
63 char* serialized_read_buffer,
64 uint32_t serialized_read_buffer_size,
65 ScopedPlatformHandle shared_buffer_handle,
66 uint32_t ring_buffer_start,
67 uint32_t ring_buffer_size,
68 bool is_producer,
69 const base::Closure& init_callback);
70
40 // Helper methods used by DataPipeConsumerDispatcher and 71 // Helper methods used by DataPipeConsumerDispatcher and
41 // DataPipeProducerDispatcher for serialization and deserialization. 72 // DataPipeProducerDispatcher for serialization and deserialization.
42 static void StartSerialize(bool have_channel_handle, 73 void StartSerialize(size_t* max_size, size_t* max_platform_handles);
43 bool have_shared_memory, 74 void EndSerialize(void* destination,
44 size_t* max_size, 75 size_t* actual_size,
45 size_t* max_platform_handles); 76 PlatformHandleVector* platform_handles);
46 static void EndSerialize(const MojoCreateDataPipeOptions& options,
47 ScopedPlatformHandle channel_handle,
48 ScopedPlatformHandle shared_memory_handle,
49 size_t shared_memory_size,
50 void* destination,
51 size_t* actual_size,
52 PlatformHandleVector* platform_handles);
53 static ScopedPlatformHandle Deserialize( 77 static ScopedPlatformHandle Deserialize(
54 const void* source, 78 const void* source,
55 size_t size, 79 size_t size,
56 PlatformHandleVector* platform_handles, 80 PlatformHandleVector* platform_handles,
57 MojoCreateDataPipeOptions* options, 81 MojoCreateDataPipeOptions* options,
58 ScopedPlatformHandle* shared_memory_handle, 82 ScopedPlatformHandle* channel_shared_handle,
59 size_t* shared_memory_size); 83 uint32_t* serialized_read_buffer_size,
84 uint32_t* serialized_write_buffer_size,
85 ScopedPlatformHandle* shared_buffer_handle,
86 uint32_t* ring_buffer_start,
87 uint32_t* ring_buffer_size);
88
89 // Returns the number of readable or writable bytes. If there is no valid
90 // shared buffer, returns 0.
91 uint32_t GetReadableBytes() const;
92 uint32_t GetWritableBytes() const;
93
94 // Two phase reads and writes. Note that since returned memory has to be
95 // contiguous, these may return less than the values returned by
96 // |GetReadableBytes| / |GetWritableBytes|.
97 void* GetWriteBuffer(uint32_t* num_bytes);
98 const void* GetReadBuffer(uint32_t* num_bytes);
99
100 // Normal reads and writes. Will return false if they can't read or write
101 // all the data given.
102 bool WriteDataIntoSharedBuffer(const void* data, uint32_t num_bytes);
103 bool ReadDataFromSharedBuffer(void* buf, uint32_t num_bytes);
104
105 // Notify other end of a read or write. Returns false if the other side was
106 // closed (on a RawChannel error).
107 bool NotifyWrite(uint32_t num_bytes);
108 bool NotifyRead(uint32_t num_bytes);
109
110 // Reading and writing are split into three parts: 1. reading / writing the
111 // data, telling the other side what we did, and updating the ring buffer to
112 // reflect what we did. The functions |UpdateFromRead| and |UpdateFromWrite|
113 // update the ring buffer state.
114 void UpdateFromRead(uint32_t num_bytes);
115 void UpdateFromWrite(uint32_t num_bytes);
116
117 RawChannel* GetChannel() { return channel_; }
118 const MojoCreateDataPipeOptions& GetOptions() { return options_; }
119
120 // Returns whether readable or writable state changed.
121 bool ProcessCommand(const DataPipeCommandHeader& command,
122 ScopedPlatformHandleVectorPtr platform_handles);
123
124 // Shuts down the channel, and releases the shared buffer.
125 void Shutdown();
126
127 // Named after |CreateEquivalentDispatcherAndCloseImplNoLock|. This will
128 // Release() the RawChannel and save its returned read and write buffer, then
129 // swap ourselves into |out|.
130 void CreateEquivalentAndClose(DataPipe* out);
131
132 private:
133 friend class base::RefCountedThreadSafe<DataPipe>;
134 ~DataPipe();
135
136 uint8_t* GetSharedBufferBase();
137 // Sends the other side our shared buffer.
138 void NotifySharedBuffer();
139 // Set our shared buffer.
140 void UpdateSharedBuffer(scoped_refptr<PlatformSharedBuffer> shared_buffer);
141 // Releases the raw channel.
142 void Serialize();
143
144 RawChannel* channel_;
145
146 MojoCreateDataPipeOptions options_;
147
148 // True if we've released the channel because we're about to get serialised
Anand Mistry (off Chromium) 2016/01/11 06:19:34 Unfortunately, we use american spelling. So 'seria
Eliot Courtney 2016/01/13 00:00:10 Done.
149 // and transported. We also store the channel's read and write buffer, and
150 // handle.
151 bool channel_released_;
152 std::vector<char> serialized_read_buffer_;
153 std::vector<char> serialized_write_buffer_;
154 ScopedPlatformHandle serialized_channel_handle_;
155
156 scoped_refptr<PlatformSharedBuffer> shared_buffer_;
157 // Keep the full mapping of the shared buffer around so we don't have to
158 // recreate it.
159 scoped_ptr<PlatformSharedBufferMapping> mapping_;
160 uint32_t ring_buffer_start_;
161 uint32_t ring_buffer_size_;
162
163 // By default, the producer will attempt to create the shared buffer.
164 bool is_producer_;
60 }; 165 };
61 166
62 } // namespace edk 167 } // namespace edk
63 } // namespace mojo 168 } // namespace mojo
64 169
65 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_H_ 170 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698