-
Notifications
You must be signed in to change notification settings - Fork 10
/
read_writer.go
236 lines (210 loc) · 5.04 KB
/
read_writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// +build linux
package iouring
import (
"io"
"os"
"runtime"
"sync/atomic"
"syscall"
"unsafe"
"github.com/pkg/errors"
)
// ReadWriteAtCloser supports reading, writing, and closing.
type ReadWriteAtCloser interface {
io.WriterAt
io.ReadWriteCloser
}
// ringFIO is used for handling file IO.
type ringFIO struct {
r *Ring
f *os.File
fd int32
fOffset *int64
c *completer
}
// getCqe is used for getting a CQE result and will retry up to one time.
func (i *ringFIO) getCqe(reqID uint64, count, min int) (int, error) {
// TODO: consider adding the submitter interface here, or move out the
// submit function from this method all together.
if count > 0 || min > 0 {
_, err := i.r.Enter(uint(count), uint(min), EnterGetEvents, nil)
if err != nil {
return 0, err
}
}
cq := i.r.cq
foundIdx := 0
findCqe:
head := atomic.LoadUint32(cq.Head)
tail := atomic.LoadUint32(cq.Tail)
mask := atomic.LoadUint32(cq.Mask)
end := int(tail & mask)
for x := int(head & mask); x < len(cq.Entries); x++ {
cqe := cq.Entries[x]
if cqe.UserData == reqID {
if cqe.Res < 0 {
return 0, syscall.Errno(-cqe.Res)
}
atomic.StoreInt64(i.fOffset, atomic.LoadInt64(i.fOffset)+int64(cqe.Res))
foundIdx = x
i.c.complete(foundIdx)
return int(cqe.Res), nil
}
if x == end {
goto findCqe
}
}
tail = atomic.LoadUint32(cq.Tail)
mask = atomic.LoadUint32(cq.Mask)
end = int(tail & mask)
for x := 0; x < end; x++ {
cqe := cq.Entries[x]
if cqe.UserData == reqID {
if cqe.Res < 0 {
return 0, syscall.Errno(-cqe.Res)
}
atomic.StoreInt64(i.fOffset, atomic.LoadInt64(i.fOffset)+int64(cqe.Res))
foundIdx = x
i.c.complete(foundIdx)
return int(cqe.Res), nil
}
if x == end {
goto findCqe
}
}
goto findCqe
}
// Write implements the io.Writer interface.
func (i *ringFIO) Write(b []byte) (int, error) {
id, ready, err := i.PrepareWrite(b, 0)
if err != nil {
return 0, err
}
ready()
n, err := i.getCqe(id, 1, 1)
runtime.KeepAlive(b)
return n, err
}
// PrepareWrite is used to prepare a Write SQE. The ring is able to be entered
// after the returned callback is called.
func (i *ringFIO) PrepareWrite(b []byte, flags uint8) (uint64, func(), error) {
sqe, ready := i.r.SubmitEntry()
if sqe == nil {
return 0, nil, errRingUnavailable
}
sqe.Opcode = Write
sqe.UserData = i.r.ID()
sqe.Fd = i.fd
sqe.Len = uint32(len(b))
sqe.Flags = flags
sqe.Offset = uint64(atomic.LoadInt64(i.fOffset))
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&b[0])))
return sqe.UserData, ready, nil
}
// PrepareRead is used to prepare a Read SQE. The ring is able to be entered
// after the returned callback is called.
func (i *ringFIO) PrepareRead(b []byte, flags uint8) (uint64, func(), error) {
sqe, ready := i.r.SubmitEntry()
if sqe == nil {
return 0, nil, errRingUnavailable
}
sqe.Opcode = Read
sqe.UserData = i.r.ID()
sqe.Fd = i.fd
sqe.Len = uint32(len(b))
sqe.Flags = flags
sqe.Offset = uint64(atomic.LoadInt64(i.fOffset))
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&b[0])))
return sqe.UserData, ready, nil
}
// Read implements the io.Reader interface.
func (i *ringFIO) Read(b []byte) (int, error) {
id, ready, err := i.PrepareRead(b, 0)
if err != nil {
return 0, err
}
ready()
n, err := i.getCqe(id, 1, 1)
runtime.KeepAlive(b)
if err != nil {
return 0, err
}
if n == 0 {
return n, io.EOF
}
return n, nil
}
// WriteAt implements the io.WriterAt interface.
func (i *ringFIO) WriteAt(b []byte, o int64) (int, error) {
sqe, ready := i.r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Write
sqe.UserData = i.r.ID()
sqe.Fd = i.fd
sqe.Len = uint32(len(b))
sqe.Flags = 0
sqe.Offset = uint64(o)
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&b[0])))
ready()
n, err := i.getCqe(sqe.UserData, 1, 1)
runtime.KeepAlive(b)
return n, err
}
// ReadAt implements the io.ReaderAt interface.
func (i *ringFIO) ReadAt(b []byte, o int64) (int, error) {
sqe, ready := i.r.SubmitEntry()
if sqe == nil {
return 0, errRingUnavailable
}
sqe.Opcode = Read
sqe.UserData = i.r.ID()
sqe.Fd = i.fd
sqe.Len = uint32(len(b))
sqe.Flags = 0
sqe.Offset = uint64(o)
sqe.Addr = (uint64)(uintptr(unsafe.Pointer(&b[0])))
ready()
n, err := i.getCqe(sqe.UserData, 1, 1)
runtime.KeepAlive(b)
if err != nil {
return 0, err
}
if n == 0 {
return n, io.EOF
}
return n, nil
}
// Close implements the io.Closer interface.
func (i *ringFIO) Close() error {
id, err := i.r.PrepareClose(int(i.fd))
if err != nil {
return err
}
_, err = i.getCqe(id, 1, 1)
if err != nil {
return err
}
return nil
}
// Seek implements the io.Seeker interface.
func (i *ringFIO) Seek(offset int64, whence int) (int64, error) {
switch whence {
case io.SeekStart:
atomic.StoreInt64(i.fOffset, offset)
return 0, nil
case io.SeekCurrent:
atomic.StoreInt64(i.fOffset, atomic.LoadInt64(i.fOffset)+offset)
return 0, nil
case io.SeekEnd:
stat, err := i.f.Stat()
if err != nil {
return 0, err
}
atomic.StoreInt64(i.fOffset, stat.Size()-offset)
return 0, nil
default:
return 0, errors.New("unknown whence")
}
}