#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/c/system/core.h"
#include "mojo/system/system_impl_export.h"
namespace mojo {
namespace system {
class Waiter;
class WaiterList;
class MOJO_SYSTEM_IMPL_EXPORT DataPipe :
public base::RefCountedThreadSafe<DataPipe> {
public:
static MojoResult ValidateOptions(const MojoCreateDataPipeOptions* in_options,
MojoCreateDataPipeOptions* out_options);
void ProducerCancelAllWaiters();
void ProducerClose();
MojoResult ProducerWriteData(const void* elements,
uint32_t* num_bytes,
bool all_or_none);
MojoResult ProducerBeginWriteData(void** buffer,
uint32_t* buffer_num_bytes,
bool all_or_none);
MojoResult ProducerEndWriteData(uint32_t num_bytes_written);
MojoResult ProducerAddWaiter(Waiter* waiter,
MojoWaitFlags flags,
MojoResult wake_result);
void ProducerRemoveWaiter(Waiter* waiter);
bool ProducerIsBusy() const;
void ConsumerCancelAllWaiters();
void ConsumerClose();
MojoResult ConsumerReadData(void* elements,
uint32_t* num_bytes,
bool all_or_none);
MojoResult ConsumerDiscardData(uint32_t* num_bytes,
bool all_or_none);
MojoResult ConsumerQueryData(uint32_t* num_bytes);
MojoResult ConsumerBeginReadData(const void** buffer,
uint32_t* buffer_num_bytes,
bool all_or_none);
MojoResult ConsumerEndReadData(uint32_t num_bytes_read);
MojoResult ConsumerAddWaiter(Waiter* waiter,
MojoWaitFlags flags,
MojoResult wake_result);
void ConsumerRemoveWaiter(Waiter* waiter);
bool ConsumerIsBusy() const;
protected:
DataPipe(bool has_local_producer,
bool has_local_consumer,
const MojoCreateDataPipeOptions& validated_options);
friend class base::RefCountedThreadSafe<DataPipe>;
virtual ~DataPipe();
virtual void ProducerCloseImplNoLock() = 0;
virtual MojoResult ProducerWriteDataImplNoLock(const void* elements,
uint32_t* num_bytes,
bool all_or_none) = 0;
virtual MojoResult ProducerBeginWriteDataImplNoLock(
void** buffer,
uint32_t* buffer_num_bytes,
bool all_or_none) = 0;
virtual MojoResult ProducerEndWriteDataImplNoLock(
uint32_t num_bytes_written) = 0;
virtual MojoWaitFlags ProducerSatisfiedFlagsNoLock() = 0;
virtual MojoWaitFlags ProducerSatisfiableFlagsNoLock() = 0;
virtual void ConsumerCloseImplNoLock() = 0;
virtual MojoResult ConsumerReadDataImplNoLock(void* elements,
uint32_t* num_bytes,
bool all_or_none) = 0;
virtual MojoResult ConsumerDiscardDataImplNoLock(uint32_t* num_bytes,
bool all_or_none) = 0;
virtual MojoResult ConsumerQueryDataImplNoLock(uint32_t* num_bytes) = 0;
virtual MojoResult ConsumerBeginReadDataImplNoLock(const void** buffer,
uint32_t* buffer_num_bytes,
bool all_or_none) = 0;
virtual MojoResult ConsumerEndReadDataImplNoLock(uint32_t num_bytes_read) = 0;
virtual MojoWaitFlags ConsumerSatisfiedFlagsNoLock() = 0;
virtual MojoWaitFlags ConsumerSatisfiableFlagsNoLock() = 0;
bool may_discard() const { return may_discard_; }
size_t element_num_bytes() const { return element_num_bytes_; }
size_t capacity_num_bytes() const { return capacity_num_bytes_; }
bool producer_open_no_lock() const {
lock_.AssertAcquired();
return producer_open_;
}
bool consumer_open_no_lock() const {
lock_.AssertAcquired();
return consumer_open_;
}
uint32_t producer_two_phase_max_num_bytes_written_no_lock() const {
lock_.AssertAcquired();
return producer_two_phase_max_num_bytes_written_;
}
uint32_t consumer_two_phase_max_num_bytes_read_no_lock() const {
lock_.AssertAcquired();
return consumer_two_phase_max_num_bytes_read_;
}
void set_producer_two_phase_max_num_bytes_written_no_lock(
uint32_t num_bytes) {
lock_.AssertAcquired();
producer_two_phase_max_num_bytes_written_ = num_bytes;
}
void set_consumer_two_phase_max_num_bytes_read_no_lock(uint32_t num_bytes) {
lock_.AssertAcquired();
consumer_two_phase_max_num_bytes_read_ = num_bytes;
}
bool producer_in_two_phase_write_no_lock() const {
lock_.AssertAcquired();
return producer_two_phase_max_num_bytes_written_ > 0;
}
bool consumer_in_two_phase_read_no_lock() const {
lock_.AssertAcquired();
return consumer_two_phase_max_num_bytes_read_ > 0;
}
private:
void AwakeProducerWaitersForStateChangeNoLock();
void AwakeConsumerWaitersForStateChangeNoLock();
bool has_local_producer_no_lock() const {
lock_.AssertAcquired();
return !!producer_waiter_list_.get();
}
bool has_local_consumer_no_lock() const {
lock_.AssertAcquired();
return !!consumer_waiter_list_.get();
}
const bool may_discard_;
const size_t element_num_bytes_;
const size_t capacity_num_bytes_;
mutable base::Lock lock_;
bool producer_open_;
bool consumer_open_;
scoped_ptr<WaiterList> producer_waiter_list_;
scoped_ptr<WaiterList> consumer_waiter_list_;
uint32_t producer_two_phase_max_num_bytes_written_;
uint32_t consumer_two_phase_max_num_bytes_read_;
DISALLOW_COPY_AND_ASSIGN(DataPipe);
};
}
}
#endif