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.
5 #ifndef MOJO_EDK_SYSTEM_LOCAL_DATA_PIPE_H_
6 #define MOJO_EDK_SYSTEM_LOCAL_DATA_PIPE_H_
8 #include "base/macros.h"
9 #include "base/memory/aligned_memory.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "mojo/edk/system/data_pipe.h"
13 #include "mojo/edk/system/system_impl_export.h"
18 // |LocalDataPipe| is a subclass that "implements" |DataPipe| for data pipes
19 // whose producer and consumer are both local. This class is thread-safe (with
20 // protection provided by |DataPipe|'s |lock_|.
21 class MOJO_SYSTEM_IMPL_EXPORT LocalDataPipe
: public DataPipe
{
23 // |validated_options| should be the output of |DataPipe::ValidateOptions()|.
24 // In particular: |struct_size| is ignored (so |validated_options| must be the
25 // current version of the struct) and |capacity_num_bytes| must be nonzero.
26 explicit LocalDataPipe(const MojoCreateDataPipeOptions
& validated_options
);
29 friend class base::RefCountedThreadSafe
<LocalDataPipe
>;
30 ~LocalDataPipe() override
;
32 // |DataPipe| implementation:
33 void ProducerCloseImplNoLock() override
;
34 MojoResult
ProducerWriteDataImplNoLock(
35 UserPointer
<const void> elements
,
36 UserPointer
<uint32_t> num_bytes
,
37 uint32_t max_num_bytes_to_write
,
38 uint32_t min_num_bytes_to_write
) override
;
39 MojoResult
ProducerBeginWriteDataImplNoLock(
40 UserPointer
<void*> buffer
,
41 UserPointer
<uint32_t> buffer_num_bytes
,
42 uint32_t min_num_bytes_to_write
) override
;
43 MojoResult
ProducerEndWriteDataImplNoLock(
44 uint32_t num_bytes_written
) override
;
45 HandleSignalsState
ProducerGetHandleSignalsStateImplNoLock() const override
;
46 void ConsumerCloseImplNoLock() override
;
47 MojoResult
ConsumerReadDataImplNoLock(UserPointer
<void> elements
,
48 UserPointer
<uint32_t> num_bytes
,
49 uint32_t max_num_bytes_to_read
,
50 uint32_t min_num_bytes_to_read
,
52 MojoResult
ConsumerDiscardDataImplNoLock(
53 UserPointer
<uint32_t> num_bytes
,
54 uint32_t max_num_bytes_to_discard
,
55 uint32_t min_num_bytes_to_discard
) override
;
56 MojoResult
ConsumerQueryDataImplNoLock(
57 UserPointer
<uint32_t> num_bytes
) override
;
58 MojoResult
ConsumerBeginReadDataImplNoLock(
59 UserPointer
<const void*> buffer
,
60 UserPointer
<uint32_t> buffer_num_bytes
,
61 uint32_t min_num_bytes_to_read
) override
;
62 MojoResult
ConsumerEndReadDataImplNoLock(uint32_t num_bytes_read
) override
;
63 HandleSignalsState
ConsumerGetHandleSignalsStateImplNoLock() const override
;
65 void EnsureBufferNoLock();
66 void DestroyBufferNoLock();
68 // Get the maximum (single) write/read size right now (in number of elements);
69 // result fits in a |uint32_t|.
70 size_t GetMaxNumBytesToWriteNoLock();
71 size_t GetMaxNumBytesToReadNoLock();
73 // Marks the given number of bytes as consumed/discarded. |num_bytes| must be
74 // greater than |current_num_bytes_|.
75 void MarkDataAsConsumedNoLock(size_t num_bytes
);
77 // The members below are protected by |DataPipe|'s |lock_|:
78 scoped_ptr
<char, base::AlignedFreeDeleter
> buffer_
;
81 size_t current_num_bytes_
;
83 DISALLOW_COPY_AND_ASSIGN(LocalDataPipe
);
89 #endif // MOJO_EDK_SYSTEM_LOCAL_DATA_PIPE_H_