diff options
Diffstat (limited to 'mojo/system/data_pipe.h')
-rw-r--r-- | mojo/system/data_pipe.h | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/mojo/system/data_pipe.h b/mojo/system/data_pipe.h new file mode 100644 index 0000000..44dc592 --- /dev/null +++ b/mojo/system/data_pipe.h @@ -0,0 +1,100 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_SYSTEM_DATA_PIPE_H_ +#define MOJO_SYSTEM_DATA_PIPE_H_ + +#include "base/basictypes.h" +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#include "base/synchronization/lock.h" +#include "mojo/public/system/core.h" +#include "mojo/system/system_impl_export.h" + +namespace mojo { +namespace system { + +class Waiter; +class WaiterList; + +// |DataPipe| is a base class for secondary objects implementing data pipes, +// similar to |MessagePipe| (see the explanatory comment in core_impl.cc). It is +// typically owned by the dispatcher(s) corresponding to the local endpoints. +// Its subclasses implement the three cases: local producer and consumer, local +// producer and remote consumer, and remote producer and local consumer. This +// class is thread-safe. +class MOJO_SYSTEM_IMPL_EXPORT DataPipe : + public base::RefCountedThreadSafe<DataPipe> { + public: + // These are called by the producer dispatcher to implement its methods of + // corresponding names. + void ProducerCancelAllWaiters(); + void ProducerClose(); + // This does not validate |elements| or |num_elements| (or |*num_elements|). + MojoResult ProducerWriteData(const void* elements, + uint32_t* num_elements, + MojoWriteDataFlags flags); + // This does not validate |buffer| or |buffer_num_elements|. + MojoResult ProducerBeginWriteData(void** buffer, + uint32_t* buffer_num_elements, + MojoWriteDataFlags flags); + MojoResult ProducerEndWriteData(uint32_t num_elements_written); + MojoResult ProducerAddWaiter(Waiter* waiter, + MojoWaitFlags flags, + MojoResult wake_result); + void ProducerRemoveWaiter(Waiter* waiter); + +/* TODO(vtl) + void ConsumerCancelAllWaiters(); + void ConsumerClose(); +... + MojoResult ConsumerAddWaiter(Waiter* waiter, + MojoWaitFlags flags, + MojoResult wake_result); + void ConsumerRemoveWaiter(Waiter* waiter); +*/ + + // Thread-safe and fast (doesn't take the lock). + size_t element_size() const { return element_size_; } + + protected: + DataPipe(bool has_local_producer, bool has_local_consumer); + + void Init(size_t element_size); + + virtual void ProducerCloseImplNoLock() = 0; + virtual MojoResult ProducerBeginWriteDataImplNoLock( + void** buffer, + uint32_t* buffer_num_elements, + MojoWriteDataFlags flags) = 0; + virtual MojoResult ProducerEndWriteDataImplNoLock( + uint32_t num_elements_written) = 0; + virtual MojoWaitFlags ProducerSatisfiedFlagsNoLock() = 0; + virtual MojoWaitFlags ProducerSatisfiableFlagsNoLock() = 0; + + private: + friend class base::RefCountedThreadSafe<DataPipe>; + virtual ~DataPipe(); + + bool has_local_producer_no_lock() const { + return !!producer_waiter_list_.get(); + } + bool has_local_consumer_no_lock() const { + return !!consumer_waiter_list_.get(); + } + + // Set by |Init()| and never changed afterwards. + size_t element_size_; + + base::Lock lock_; // Protects the following members. + scoped_ptr<WaiterList> producer_waiter_list_; + scoped_ptr<WaiterList> consumer_waiter_list_; + + DISALLOW_COPY_AND_ASSIGN(DataPipe); +}; + +} // namespace system +} // namespace mojo + +#endif // MOJO_SYSTEM_DATA_PIPE_H_ |