OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 package system | |
6 | |
7 // ConsumerHandle is a handle for the consumer part of a data pipe. | |
8 type ConsumerHandle interface { | |
9 Handle | |
10 | |
11 // ReadData reads data from the data pipe consumer handle with the | |
12 // given flags. On success, returns the data that was read. | |
13 ReadData(flags MojoReadDataFlags) (MojoResult, []byte) | |
14 | |
15 // BeginReadData begins a two-phase read from the data pipe consumer. | |
16 // On success, returns a slice from which the caller can read up to its | |
17 // length bytes of data. The slice length will always be a multiple of | |
18 // the element size. | |
19 // | |
20 // During a two-phase read, this handle is *not* readable. E.g., read | |
21 // from this handle will return |MOJO_RESULT_BUSY|. | |
22 // | |
23 // Once the caller has finished reading data from the slice, it should | |
24 // call |EndReadData()| to specify the amount read and to complete the | |
25 // two-phase read. | |
26 BeginReadData(flags MojoReadDataFlags) (MojoResult, []byte) | |
27 | |
28 // EndReadData ends a two-phase read from the data pipe consumer that | |
29 // was begun by a call to |BeginReadData()| on the same handle. | |
30 // |numBytesRead| should indicate the amount of data actually read; it | |
31 // must be less than or equal to the length of the slice returned by | |
32 // |BeginReadData()| and must be a multiple of the element size. | |
33 // | |
34 // On failure, the two-phase read (if any) is ended (so the handle may | |
35 // become readable again) but no data is "removed" from the data pipe. | |
36 EndReadData(numBytesRead int) MojoResult | |
37 } | |
38 | |
39 // ProducerHandle is a handle for the producer part of a data pipe. | |
40 type ProducerHandle interface { | |
41 Handle | |
42 | |
43 // WriteData writes data to the data pipe producer handle with the | |
44 // given flags. On success, returns the number of bytes that were | |
45 // actually written. | |
46 WriteData(data []byte, flags MojoWriteDataFlags) (MojoResult, int) | |
47 | |
48 // BeginWriteData begins a two-phase write to the data pipe producer. | |
49 // On success, returns a slice to which the caller can write. The slice | |
50 // length will always be a multiple of the element size. | |
51 // | |
52 // During a two-phase write, this handle is *not* writable. E.g., write | |
53 // to this handle will return |MOJO_RESULT_BUSY|. | |
54 // | |
55 // Once the caller has finished writing data to the buffer, it should | |
56 // call |EndWriteData()| to specify the amount written and to complete | |
57 // the two-phase write. | |
58 BeginWriteData(flags MojoWriteDataFlags) (MojoResult, []byte) | |
59 | |
60 // EndWriteData ends a two-phase write to the data pipe producer that | |
61 // was begun by a call to |BeginWriteData()| on the same handle. | |
62 // |numBytesWritten| should indicate the amount of data actually | |
63 // written; it must be less than or equal to the length of the slice | |
64 // returned by |BeginWriteData()| and must be a multiple of the element | |
65 // size. The slice returned from |BeginWriteData()| must have been | |
66 // filled with exactly |numBytesWritten| bytes of data. | |
67 // | |
68 // On failure, the two-phase write (if any) is ended (so the handle may | |
69 // become writable again, if there's space available) but no data | |
70 // written to the slice is "put into" the data pipe. | |
71 EndWriteData(numBytesWritten int) MojoResult | |
72 } | |
73 | |
74 type dataPipeConsumer struct { | |
75 // baseHandle should always be the first component of this struct, | |
76 // see |finalizeHandle()| for more details. | |
77 baseHandle | |
78 } | |
79 | |
80 func (h *dataPipeConsumer) ReadData(flags MojoReadDataFlags) (MojoResult, []byte
) { | |
81 h.core.mu.Lock() | |
82 r, buf := sysImpl.ReadData(uint32(h.mojoHandle), uint32(flags)) | |
83 h.core.mu.Unlock() | |
84 return MojoResult(r), buf | |
85 } | |
86 | |
87 func (h *dataPipeConsumer) BeginReadData(flags MojoReadDataFlags) (MojoResult, [
]byte) { | |
88 h.core.mu.Lock() | |
89 r, buf := sysImpl.BeginReadData(uint32(h.mojoHandle), uint32(flags)) | |
90 h.core.mu.Unlock() | |
91 return MojoResult(r), buf | |
92 } | |
93 | |
94 func (h *dataPipeConsumer) EndReadData(numBytesRead int) MojoResult { | |
95 h.core.mu.Lock() | |
96 r := sysImpl.EndReadData(uint32(h.mojoHandle), uint32(numBytesRead)) | |
97 h.core.mu.Unlock() | |
98 return MojoResult(r) | |
99 } | |
100 | |
101 type dataPipeProducer struct { | |
102 // baseHandle should always be the first component of this struct, | |
103 // see |finalizeHandle()| for more details. | |
104 baseHandle | |
105 } | |
106 | |
107 func (h *dataPipeProducer) WriteData(data []byte, flags MojoWriteDataFlags) (Moj
oResult, int) { | |
108 h.core.mu.Lock() | |
109 r, bytesWritten := sysImpl.WriteData(uint32(h.mojoHandle), data, uint32(
flags)) | |
110 h.core.mu.Unlock() | |
111 return MojoResult(r), int(bytesWritten) | |
112 } | |
113 | |
114 func (h *dataPipeProducer) BeginWriteData(flags MojoWriteDataFlags) (MojoResult,
[]byte) { | |
115 h.core.mu.Lock() | |
116 r, buf := sysImpl.BeginWriteData(uint32(h.mojoHandle), uint32(flags)) | |
117 h.core.mu.Unlock() | |
118 return MojoResult(r), buf | |
119 } | |
120 | |
121 func (h *dataPipeProducer) EndWriteData(numBytesWritten int) MojoResult { | |
122 h.core.mu.Lock() | |
123 r := sysImpl.EndWriteData(uint32(h.mojoHandle), uint32(numBytesWritten)) | |
124 h.core.mu.Unlock() | |
125 return MojoResult(r) | |
126 } | |
OLD | NEW |