Home | History | Annotate | Download | only in system
      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_SYSTEM_DATA_PIPE_H_
      6 #define MOJO_SYSTEM_DATA_PIPE_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/synchronization/lock.h"
     12 #include "mojo/public/system/core.h"
     13 #include "mojo/system/system_impl_export.h"
     14 
     15 namespace mojo {
     16 namespace system {
     17 
     18 class Waiter;
     19 class WaiterList;
     20 
     21 // |DataPipe| is a base class for secondary objects implementing data pipes,
     22 // similar to |MessagePipe| (see the explanatory comment in core_impl.cc). It is
     23 // typically owned by the dispatcher(s) corresponding to the local endpoints.
     24 // Its subclasses implement the three cases: local producer and consumer, local
     25 // producer and remote consumer, and remote producer and local consumer. This
     26 // class is thread-safe.
     27 class MOJO_SYSTEM_IMPL_EXPORT DataPipe :
     28     public base::RefCountedThreadSafe<DataPipe> {
     29  public:
     30   // These are called by the producer dispatcher to implement its methods of
     31   // corresponding names.
     32   void ProducerCancelAllWaiters();
     33   void ProducerClose();
     34   // This does not validate its arguments.
     35   MojoResult ProducerWriteData(const void* elements,
     36                                uint32_t* num_elements,
     37                                MojoWriteDataFlags flags);
     38   // This does not validate its arguments.
     39   MojoResult ProducerBeginWriteData(void** buffer,
     40                                     uint32_t* buffer_num_elements,
     41                                     MojoWriteDataFlags flags);
     42   MojoResult ProducerEndWriteData(uint32_t num_elements_written);
     43   MojoResult ProducerAddWaiter(Waiter* waiter,
     44                                MojoWaitFlags flags,
     45                                MojoResult wake_result);
     46   void ProducerRemoveWaiter(Waiter* waiter);
     47 
     48   // These are called by the consumer dispatcher to implement its methods of
     49   // corresponding names.
     50   void ConsumerCancelAllWaiters();
     51   void ConsumerClose();
     52   // This does not validate its arguments.
     53   MojoResult ConsumerReadData(void* elements,
     54                               uint32_t* num_elements,
     55                               MojoReadDataFlags flags);
     56   // This does not validate its arguments.
     57   MojoResult ConsumerBeginReadData(const void** buffer,
     58                                    uint32_t* buffer_num_elements,
     59                                    MojoReadDataFlags flags);
     60   MojoResult ConsumerEndReadData(uint32_t num_elements_read);
     61   MojoResult ConsumerAddWaiter(Waiter* waiter,
     62                                MojoWaitFlags flags,
     63                                MojoResult wake_result);
     64   void ConsumerRemoveWaiter(Waiter* waiter);
     65 
     66   // Thread-safe and fast (they don't take the lock):
     67   bool may_discard() const { return may_discard_; }
     68   size_t element_size() const { return element_size_; }
     69   size_t capacity_num_elements() const { return capacity_num_elements_; }
     70 
     71  protected:
     72   DataPipe(bool has_local_producer, bool has_local_consumer);
     73 
     74   friend class base::RefCountedThreadSafe<DataPipe>;
     75   virtual ~DataPipe();
     76 
     77   // Not thread-safe; must be called before any other methods are called. This
     78   // object is only usable on success.
     79   MojoResult Init(bool may_discard,
     80                   size_t element_size,
     81                   size_t capacity_num_elements);
     82 
     83   void AwakeProducerWaitersForStateChangeNoLock();
     84   void AwakeConsumerWaitersForStateChangeNoLock();
     85 
     86   virtual void ProducerCloseImplNoLock() = 0;
     87   virtual MojoResult ProducerBeginWriteDataImplNoLock(
     88       void** buffer,
     89       uint32_t* buffer_num_elements,
     90       MojoWriteDataFlags flags) = 0;
     91   virtual MojoResult ProducerEndWriteDataImplNoLock(
     92       uint32_t num_elements_written) = 0;
     93   virtual MojoWaitFlags ProducerSatisfiedFlagsNoLock() = 0;
     94   virtual MojoWaitFlags ProducerSatisfiableFlagsNoLock() = 0;
     95 
     96   virtual void ConsumerCloseImplNoLock() = 0;
     97   virtual MojoResult ConsumerDiscardDataNoLock(uint32_t* num_elements,
     98                                                bool all_or_none) = 0;
     99   virtual MojoResult ConsumerQueryDataNoLock(uint32_t* num_elements) = 0;
    100   virtual MojoResult ConsumerBeginReadDataImplNoLock(
    101       const void** buffer,
    102       uint32_t* buffer_num_elements,
    103       MojoReadDataFlags flags) = 0;
    104   virtual MojoResult ConsumerEndReadDataImplNoLock(
    105       uint32_t num_elements_read) = 0;
    106   virtual MojoWaitFlags ConsumerSatisfiedFlagsNoLock() = 0;
    107   virtual MojoWaitFlags ConsumerSatisfiableFlagsNoLock() = 0;
    108 
    109  private:
    110   bool has_local_producer_no_lock() const {
    111     return !!producer_waiter_list_.get();
    112   }
    113   bool has_local_consumer_no_lock() const {
    114     return !!consumer_waiter_list_.get();
    115   }
    116 
    117   // Set by |Init()| and never changed afterwards:
    118   bool may_discard_;
    119   size_t element_size_;
    120   size_t capacity_num_elements_;
    121 
    122   base::Lock lock_;  // Protects the following members.
    123   scoped_ptr<WaiterList> producer_waiter_list_;
    124   scoped_ptr<WaiterList> consumer_waiter_list_;
    125   bool producer_in_two_phase_write_;
    126   bool consumer_in_two_phase_read_;
    127 
    128   DISALLOW_COPY_AND_ASSIGN(DataPipe);
    129 };
    130 
    131 }  // namespace system
    132 }  // namespace mojo
    133 
    134 #endif  // MOJO_SYSTEM_DATA_PIPE_H_
    135