| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 14 #include "mojo/edk/system/handle_signals_state.h" | 14 #include "mojo/edk/system/handle_signals_state.h" |
| 15 #include "mojo/edk/system/memory.h" | 15 #include "mojo/edk/system/memory.h" |
| 16 #include "mojo/edk/system/system_impl_export.h" | 16 #include "mojo/edk/system/system_impl_export.h" |
| 17 #include "mojo/public/c/system/data_pipe.h" | 17 #include "mojo/public/c/system/data_pipe.h" |
| 18 #include "mojo/public/c/system/types.h" | 18 #include "mojo/public/c/system/types.h" |
| 19 | 19 |
| 20 namespace mojo { | 20 namespace mojo { |
| 21 namespace system { | 21 namespace system { |
| 22 | 22 |
| 23 class Waiter; | 23 class Awakable; |
| 24 class WaiterList; | 24 class AwakableList; |
| 25 | 25 |
| 26 // |DataPipe| is a base class for secondary objects implementing data pipes, | 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 | 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. | 28 // typically owned by the dispatcher(s) corresponding to the local endpoints. |
| 29 // Its subclasses implement the three cases: local producer and consumer, local | 29 // Its subclasses implement the three cases: local producer and consumer, local |
| 30 // producer and remote consumer, and remote producer and local consumer. This | 30 // producer and remote consumer, and remote producer and local consumer. This |
| 31 // class is thread-safe. | 31 // class is thread-safe. |
| 32 class MOJO_SYSTEM_IMPL_EXPORT DataPipe | 32 class MOJO_SYSTEM_IMPL_EXPORT DataPipe |
| 33 : public base::RefCountedThreadSafe<DataPipe> { | 33 : public base::RefCountedThreadSafe<DataPipe> { |
| 34 public: | 34 public: |
| 35 // The default options for |MojoCreateDataPipe()|. (Real uses should obtain | 35 // The default options for |MojoCreateDataPipe()|. (Real uses should obtain |
| 36 // this via |ValidateCreateOptions()| with a null |in_options|; this is | 36 // this via |ValidateCreateOptions()| with a null |in_options|; this is |
| 37 // exposed directly for testing convenience.) | 37 // exposed directly for testing convenience.) |
| 38 static MojoCreateDataPipeOptions GetDefaultCreateOptions(); | 38 static MojoCreateDataPipeOptions GetDefaultCreateOptions(); |
| 39 | 39 |
| 40 // Validates and/or sets default options for |MojoCreateDataPipeOptions|. If | 40 // Validates and/or sets default options for |MojoCreateDataPipeOptions|. If |
| 41 // non-null, |in_options| must point to a struct of at least | 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) | 42 // |in_options->struct_size| bytes. |out_options| must point to a (current) |
| 43 // |MojoCreateDataPipeOptions| and will be entirely overwritten on success (it | 43 // |MojoCreateDataPipeOptions| and will be entirely overwritten on success (it |
| 44 // may be partly overwritten on failure). | 44 // may be partly overwritten on failure). |
| 45 static MojoResult ValidateCreateOptions( | 45 static MojoResult ValidateCreateOptions( |
| 46 UserPointer<const MojoCreateDataPipeOptions> in_options, | 46 UserPointer<const MojoCreateDataPipeOptions> in_options, |
| 47 MojoCreateDataPipeOptions* out_options); | 47 MojoCreateDataPipeOptions* out_options); |
| 48 | 48 |
| 49 // These are called by the producer dispatcher to implement its methods of | 49 // These are called by the producer dispatcher to implement its methods of |
| 50 // corresponding names. | 50 // corresponding names. |
| 51 void ProducerCancelAllWaiters(); | 51 void ProducerCancelAllAwakables(); |
| 52 void ProducerClose(); | 52 void ProducerClose(); |
| 53 MojoResult ProducerWriteData(UserPointer<const void> elements, | 53 MojoResult ProducerWriteData(UserPointer<const void> elements, |
| 54 UserPointer<uint32_t> num_bytes, | 54 UserPointer<uint32_t> num_bytes, |
| 55 bool all_or_none); | 55 bool all_or_none); |
| 56 MojoResult ProducerBeginWriteData(UserPointer<void*> buffer, | 56 MojoResult ProducerBeginWriteData(UserPointer<void*> buffer, |
| 57 UserPointer<uint32_t> buffer_num_bytes, | 57 UserPointer<uint32_t> buffer_num_bytes, |
| 58 bool all_or_none); | 58 bool all_or_none); |
| 59 MojoResult ProducerEndWriteData(uint32_t num_bytes_written); | 59 MojoResult ProducerEndWriteData(uint32_t num_bytes_written); |
| 60 HandleSignalsState ProducerGetHandleSignalsState(); | 60 HandleSignalsState ProducerGetHandleSignalsState(); |
| 61 MojoResult ProducerAddWaiter(Waiter* waiter, | 61 MojoResult ProducerAddAwakable(Awakable* awakable, |
| 62 MojoHandleSignals signals, | 62 MojoHandleSignals signals, |
| 63 uint32_t context, | 63 uint32_t context, |
| 64 HandleSignalsState* signals_state); | 64 HandleSignalsState* signals_state); |
| 65 void ProducerRemoveWaiter(Waiter* waiter, HandleSignalsState* signals_state); | 65 void ProducerRemoveAwakable(Awakable* awakable, |
| 66 HandleSignalsState* signals_state); |
| 66 bool ProducerIsBusy() const; | 67 bool ProducerIsBusy() const; |
| 67 | 68 |
| 68 // These are called by the consumer dispatcher to implement its methods of | 69 // These are called by the consumer dispatcher to implement its methods of |
| 69 // corresponding names. | 70 // corresponding names. |
| 70 void ConsumerCancelAllWaiters(); | 71 void ConsumerCancelAllAwakables(); |
| 71 void ConsumerClose(); | 72 void ConsumerClose(); |
| 72 // This does not validate its arguments, except to check that |*num_bytes| is | 73 // This does not validate its arguments, except to check that |*num_bytes| is |
| 73 // a multiple of |element_num_bytes_|. | 74 // a multiple of |element_num_bytes_|. |
| 74 MojoResult ConsumerReadData(UserPointer<void> elements, | 75 MojoResult ConsumerReadData(UserPointer<void> elements, |
| 75 UserPointer<uint32_t> num_bytes, | 76 UserPointer<uint32_t> num_bytes, |
| 76 bool all_or_none, | 77 bool all_or_none, |
| 77 bool peek); | 78 bool peek); |
| 78 MojoResult ConsumerDiscardData(UserPointer<uint32_t> num_bytes, | 79 MojoResult ConsumerDiscardData(UserPointer<uint32_t> num_bytes, |
| 79 bool all_or_none); | 80 bool all_or_none); |
| 80 MojoResult ConsumerQueryData(UserPointer<uint32_t> num_bytes); | 81 MojoResult ConsumerQueryData(UserPointer<uint32_t> num_bytes); |
| 81 MojoResult ConsumerBeginReadData(UserPointer<const void*> buffer, | 82 MojoResult ConsumerBeginReadData(UserPointer<const void*> buffer, |
| 82 UserPointer<uint32_t> buffer_num_bytes, | 83 UserPointer<uint32_t> buffer_num_bytes, |
| 83 bool all_or_none); | 84 bool all_or_none); |
| 84 MojoResult ConsumerEndReadData(uint32_t num_bytes_read); | 85 MojoResult ConsumerEndReadData(uint32_t num_bytes_read); |
| 85 HandleSignalsState ConsumerGetHandleSignalsState(); | 86 HandleSignalsState ConsumerGetHandleSignalsState(); |
| 86 MojoResult ConsumerAddWaiter(Waiter* waiter, | 87 MojoResult ConsumerAddAwakable(Awakable* awakable, |
| 87 MojoHandleSignals signals, | 88 MojoHandleSignals signals, |
| 88 uint32_t context, | 89 uint32_t context, |
| 89 HandleSignalsState* signals_state); | 90 HandleSignalsState* signals_state); |
| 90 void ConsumerRemoveWaiter(Waiter* waiter, HandleSignalsState* signals_state); | 91 void ConsumerRemoveAwakable(Awakable* awakable, |
| 92 HandleSignalsState* signals_state); |
| 91 bool ConsumerIsBusy() const; | 93 bool ConsumerIsBusy() const; |
| 92 | 94 |
| 93 protected: | 95 protected: |
| 94 DataPipe(bool has_local_producer, | 96 DataPipe(bool has_local_producer, |
| 95 bool has_local_consumer, | 97 bool has_local_consumer, |
| 96 const MojoCreateDataPipeOptions& validated_options); | 98 const MojoCreateDataPipeOptions& validated_options); |
| 97 | 99 |
| 98 friend class base::RefCountedThreadSafe<DataPipe>; | 100 friend class base::RefCountedThreadSafe<DataPipe>; |
| 99 virtual ~DataPipe(); | 101 virtual ~DataPipe(); |
| 100 | 102 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 bool producer_in_two_phase_write_no_lock() const { | 174 bool producer_in_two_phase_write_no_lock() const { |
| 173 lock_.AssertAcquired(); | 175 lock_.AssertAcquired(); |
| 174 return producer_two_phase_max_num_bytes_written_ > 0; | 176 return producer_two_phase_max_num_bytes_written_ > 0; |
| 175 } | 177 } |
| 176 bool consumer_in_two_phase_read_no_lock() const { | 178 bool consumer_in_two_phase_read_no_lock() const { |
| 177 lock_.AssertAcquired(); | 179 lock_.AssertAcquired(); |
| 178 return consumer_two_phase_max_num_bytes_read_ > 0; | 180 return consumer_two_phase_max_num_bytes_read_ > 0; |
| 179 } | 181 } |
| 180 | 182 |
| 181 private: | 183 private: |
| 182 void AwakeProducerWaitersForStateChangeNoLock( | 184 void AwakeProducerAwakablesForStateChangeNoLock( |
| 183 const HandleSignalsState& new_producer_state); | 185 const HandleSignalsState& new_producer_state); |
| 184 void AwakeConsumerWaitersForStateChangeNoLock( | 186 void AwakeConsumerAwakablesForStateChangeNoLock( |
| 185 const HandleSignalsState& new_consumer_state); | 187 const HandleSignalsState& new_consumer_state); |
| 186 | 188 |
| 187 bool has_local_producer_no_lock() const { | 189 bool has_local_producer_no_lock() const { |
| 188 lock_.AssertAcquired(); | 190 lock_.AssertAcquired(); |
| 189 return !!producer_waiter_list_; | 191 return !!producer_awakable_list_; |
| 190 } | 192 } |
| 191 bool has_local_consumer_no_lock() const { | 193 bool has_local_consumer_no_lock() const { |
| 192 lock_.AssertAcquired(); | 194 lock_.AssertAcquired(); |
| 193 return !!consumer_waiter_list_; | 195 return !!consumer_awakable_list_; |
| 194 } | 196 } |
| 195 | 197 |
| 196 const bool may_discard_; | 198 const bool may_discard_; |
| 197 const size_t element_num_bytes_; | 199 const size_t element_num_bytes_; |
| 198 const size_t capacity_num_bytes_; | 200 const size_t capacity_num_bytes_; |
| 199 | 201 |
| 200 mutable base::Lock lock_; // Protects the following members. | 202 mutable base::Lock lock_; // Protects the following members. |
| 201 // *Known* state of producer or consumer. | 203 // *Known* state of producer or consumer. |
| 202 bool producer_open_; | 204 bool producer_open_; |
| 203 bool consumer_open_; | 205 bool consumer_open_; |
| 204 // Non-null only if the producer or consumer, respectively, is local. | 206 // Non-null only if the producer or consumer, respectively, is local. |
| 205 scoped_ptr<WaiterList> producer_waiter_list_; | 207 scoped_ptr<AwakableList> producer_awakable_list_; |
| 206 scoped_ptr<WaiterList> consumer_waiter_list_; | 208 scoped_ptr<AwakableList> consumer_awakable_list_; |
| 207 // These are nonzero if and only if a two-phase write/read is in progress. | 209 // These are nonzero if and only if a two-phase write/read is in progress. |
| 208 uint32_t producer_two_phase_max_num_bytes_written_; | 210 uint32_t producer_two_phase_max_num_bytes_written_; |
| 209 uint32_t consumer_two_phase_max_num_bytes_read_; | 211 uint32_t consumer_two_phase_max_num_bytes_read_; |
| 210 | 212 |
| 211 DISALLOW_COPY_AND_ASSIGN(DataPipe); | 213 DISALLOW_COPY_AND_ASSIGN(DataPipe); |
| 212 }; | 214 }; |
| 213 | 215 |
| 214 } // namespace system | 216 } // namespace system |
| 215 } // namespace mojo | 217 } // namespace mojo |
| 216 | 218 |
| 217 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_H_ | 219 #endif // MOJO_EDK_SYSTEM_DATA_PIPE_H_ |
| OLD | NEW |