OLD | NEW |
| (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 MOJO_EDK_SYSTEM_DATA_PIPE_H_ | |
6 #define MOJO_EDK_SYSTEM_DATA_PIPE_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include "base/macros.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/synchronization/lock.h" | |
14 #include "mojo/edk/system/handle_signals_state.h" | |
15 #include "mojo/edk/system/memory.h" | |
16 #include "mojo/edk/system/system_impl_export.h" | |
17 #include "mojo/public/c/system/data_pipe.h" | |
18 #include "mojo/public/c/system/types.h" | |
19 | |
20 namespace mojo { | |
21 namespace system { | |
22 | |
23 class Awakable; | |
24 class AwakableList; | |
25 | |
26 // |DataPipe| is a base class for secondary objects implementing data pipes, | |
27 // similar to |MessagePipe| (see the explanatory comment in core.cc). It is | |
28 // typically owned by the dispatcher(s) corresponding to the local endpoints. | |
29 // Its subclasses implement the three cases: local producer and consumer, local | |
30 // producer and remote consumer, and remote producer and local consumer. This | |
31 // class is thread-safe. | |
32 class MOJO_SYSTEM_IMPL_EXPORT DataPipe | |
33 : public base::RefCountedThreadSafe<DataPipe> { | |
34 public: | |
35 // The default options for |MojoCreateDataPipe()|. (Real uses should obtain | |
36 // this via |ValidateCreateOptions()| with a null |in_options|; this is | |
37 // exposed directly for testing convenience.) | |
38 static MojoCreateDataPipeOptions GetDefaultCreateOptions(); | |
39 | |
40 // Validates and/or sets default options for |MojoCreateDataPipeOptions|. If | |
41 // non-null, |in_options| must point to a struct of at least | |
42 // |in_options->struct_size| bytes. |out_options| must point to a (current) | |
43 // |MojoCreateDataPipeOptions| and will be entirely overwritten on success (it | |
44 // may be partly overwritten on failure). | |
45 static MojoResult ValidateCreateOptions( | |
46 UserPointer<const MojoCreateDataPipeOptions> in_options, | |
47 MojoCreateDataPipeOptions* out_options); | |
48 | |
49 // These are called by the producer dispatcher to implement its methods of | |
50 // corresponding names. | |
51 void ProducerCancelAllAwakables(); | |
52 void ProducerClose(); | |
53 MojoResult ProducerWriteData(UserPointer<const void> elements, | |
54 UserPointer<uint32_t> num_bytes, | |
55 bool all_or_none); | |
56 MojoResult ProducerBeginWriteData(UserPointer<void*> buffer, | |
57 UserPointer<uint32_t> buffer_num_bytes, | |
58 bool all_or_none); | |
59 MojoResult ProducerEndWriteData(uint32_t num_bytes_written); | |
60 HandleSignalsState ProducerGetHandleSignalsState(); | |
61 MojoResult ProducerAddAwakable(Awakable* awakable, | |
62 MojoHandleSignals signals, | |
63 uint32_t context, | |
64 HandleSignalsState* signals_state); | |
65 void ProducerRemoveAwakable(Awakable* awakable, | |
66 HandleSignalsState* signals_state); | |
67 bool ProducerIsBusy() const; | |
68 | |
69 // These are called by the consumer dispatcher to implement its methods of | |
70 // corresponding names. | |
71 void ConsumerCancelAllAwakables(); | |
72 void ConsumerClose(); | |
73 // This does not validate its arguments, except to check that |*num_bytes| is | |
74 // a multiple of |element_num_bytes_|. | |
75 MojoResult ConsumerReadData(UserPointer<void> elements, | |
76 UserPointer<uint32_t> num_bytes, | |
77 bool all_or_none, | |
78 bool peek); | |
79 MojoResult ConsumerDiscardData(UserPointer<uint32_t> num_bytes, | |
80 bool all_or_none); | |
81 MojoResult ConsumerQueryData(UserPointer<uint32_t> num_bytes); | |
82 MojoResult ConsumerBeginReadData(UserPointer<const void*> buffer, | |
83 UserPointer<uint32_t> buffer_num_bytes, | |
84 bool all_or_none); | |
85 MojoResult ConsumerEndReadData(uint32_t num_bytes_read); | |
86 HandleSignalsState ConsumerGetHandleSignalsState(); | |
87 MojoResult ConsumerAddAwakable(Awakable* awakable, | |
88 MojoHandleSignals signals, | |
89 uint32_t context, | |
90 HandleSignalsState* signals_state); | |
91 void ConsumerRemoveAwakable(Awakable* awakable, | |
92 HandleSignalsState* signals_state); | |
93 bool ConsumerIsBusy() const; | |
94 | |
95 protected: | |
96 DataPipe(bool has_local_producer, | |
97 bool has_local_consumer, | |
98 const MojoCreateDataPipeOptions& validated_options); | |
99 | |
100 friend class base::RefCountedThreadSafe<DataPipe>; | |
101 virtual ~DataPipe(); | |
102 | |
103 virtual void ProducerCloseImplNoLock() = 0; | |
104 // |num_bytes.Get()| will be a nonzero multiple of |element_num_bytes_|. | |
105 virtual MojoResult ProducerWriteDataImplNoLock( | |
106 UserPointer<const void> elements, | |
107 UserPointer<uint32_t> num_bytes, | |
108 uint32_t max_num_bytes_to_write, | |
109 uint32_t min_num_bytes_to_write) = 0; | |
110 virtual MojoResult ProducerBeginWriteDataImplNoLock( | |
111 UserPointer<void*> buffer, | |
112 UserPointer<uint32_t> buffer_num_bytes, | |
113 uint32_t min_num_bytes_to_write) = 0; | |
114 virtual MojoResult ProducerEndWriteDataImplNoLock( | |
115 uint32_t num_bytes_written) = 0; | |
116 // Note: A producer should not be writable during a two-phase write. | |
117 virtual HandleSignalsState ProducerGetHandleSignalsStateImplNoLock() | |
118 const = 0; | |
119 | |
120 virtual void ConsumerCloseImplNoLock() = 0; | |
121 // |*num_bytes| will be a nonzero multiple of |element_num_bytes_|. | |
122 virtual MojoResult ConsumerReadDataImplNoLock(UserPointer<void> elements, | |
123 UserPointer<uint32_t> num_bytes, | |
124 uint32_t max_num_bytes_to_read, | |
125 uint32_t min_num_bytes_to_read, | |
126 bool peek) = 0; | |
127 virtual MojoResult ConsumerDiscardDataImplNoLock( | |
128 UserPointer<uint32_t> num_bytes, | |
129 uint32_t max_num_bytes_to_discard, | |
130 uint32_t min_num_bytes_to_discard) = 0; | |
131 // |*num_bytes| will be a nonzero multiple of |element_num_bytes_|. | |
132 virtual MojoResult ConsumerQueryDataImplNoLock( | |
133 UserPointer<uint32_t> num_bytes) = 0; | |
134 virtual MojoResult ConsumerBeginReadDataImplNoLock( | |
135 UserPointer<const void*> buffer, | |
136 UserPointer<uint32_t> buffer_num_bytes, | |
137 uint32_t min_num_bytes_to_read) = 0; | |
138 virtual MojoResult ConsumerEndReadDataImplNoLock(uint32_t num_bytes_read) = 0; | |
139 // Note: A consumer should not be writable during a two-phase read. | |
140 virtual HandleSignalsState ConsumerGetHandleSignalsStateImplNoLock() | |
141 const = 0; | |
142 | |
143 // Thread-safe and fast (they don't take the lock): | |
144 bool may_discard() const { return may_discard_; } | |
145 size_t element_num_bytes() const { return element_num_bytes_; } | |
146 size_t capacity_num_bytes() const { return capacity_num_bytes_; } | |
147 | |
148 // Must be called under lock. | |
149 bool producer_open_no_lock() const { | |
150 lock_.AssertAcquired(); | |
151 return producer_open_; | |
152 } | |
153 bool consumer_open_no_lock() const { | |
154 lock_.AssertAcquired(); | |
155 return consumer_open_; | |
156 } | |
157 uint32_t producer_two_phase_max_num_bytes_written_no_lock() const { | |
158 lock_.AssertAcquired(); | |
159 return producer_two_phase_max_num_bytes_written_; | |
160 } | |
161 uint32_t consumer_two_phase_max_num_bytes_read_no_lock() const { | |
162 lock_.AssertAcquired(); | |
163 return consumer_two_phase_max_num_bytes_read_; | |
164 } | |
165 void set_producer_two_phase_max_num_bytes_written_no_lock( | |
166 uint32_t num_bytes) { | |
167 lock_.AssertAcquired(); | |
168 producer_two_phase_max_num_bytes_written_ = num_bytes; | |
169 } | |
170 void set_consumer_two_phase_max_num_bytes_read_no_lock(uint32_t num_bytes) { | |
171 lock_.AssertAcquired(); | |
172 consumer_two_phase_max_num_bytes_read_ = num_bytes; | |
173 } | |
174 bool producer_in_two_phase_write_no_lock() const { | |
175 lock_.AssertAcquired(); | |
176 return producer_two_phase_max_num_bytes_written_ > 0; | |
177 } | |
178 bool consumer_in_two_phase_read_no_lock() const { | |
179 lock_.AssertAcquired(); | |
180 return consumer_two_phase_max_num_bytes_read_ > 0; | |
181 } | |
182 | |
183 private: | |
184 void AwakeProducerAwakablesForStateChangeNoLock( | |
185 const HandleSignalsState& new_producer_state); | |
186 void AwakeConsumerAwakablesForStateChangeNoLock( | |
187 const HandleSignalsState& new_consumer_state); | |
188 | |
189 bool has_local_producer_no_lock() const { | |
190 lock_.AssertAcquired(); | |
191 return !!producer_awakable_list_; | |
192 } | |
193 bool has_local_consumer_no_lock() const { | |
194 lock_.AssertAcquired(); | |
195 return !!consumer_awakable_list_; | |
196 } | |
197 | |
198 const bool may_discard_; | |
199 const size_t element_num_bytes_; | |
200 const size_t capacity_num_bytes_; | |
201 | |
202 mutable base::Lock lock_; // Protects the following members. | |
203 // *Known* state of producer or consumer. | |
204 bool producer_open_; | |
205 bool consumer_open_; | |
206 // Non-null only if the producer or consumer, respectively, is local. | |
207 scoped_ptr<AwakableList> producer_awakable_list_; | |
208 scoped_ptr<AwakableList> consumer_awakable_list_; | |
209 // These are nonzero if and only if a two-phase write/read is in progress. | |
210 uint32_t producer_two_phase_max_num_bytes_written_; | |
211 uint32_t consumer_two_phase_max_num_bytes_read_; | |
212 | |
213 DISALLOW_COPY_AND_ASSIGN(DataPipe); | |
214 }; | |
215 | |
216 } // namespace system | |
217 } // namespace mojo | |
218 | |
219 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_H_ | |
OLD | NEW |