forked from dshulyak/uring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops.go
141 lines (124 loc) · 3.49 KB
/
ops.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
package uring
import (
"unsafe"
"golang.org/x/sys/unix"
)
// Nop ...
func Nop(sqe *SQEntry) {
sqe.opcode = IORING_OP_NOP
}
// Write ...
func Write(sqe *SQEntry, fd uintptr, buf []byte) {
sqe.opcode = IORING_OP_WRITE
sqe.fd = int32(fd)
sqe.addr = (uint64)(uintptr(unsafe.Pointer(&buf[0])))
sqe.len = uint32(len(buf))
}
// Read ...
func Read(sqe *SQEntry, fd uintptr, buf []byte) {
sqe.opcode = IORING_OP_READ
sqe.fd = int32(fd)
sqe.addr = (uint64)(uintptr(unsafe.Pointer(&buf[0])))
sqe.len = uint32(len(buf))
}
// Writev ...
func Writev(sqe *SQEntry, fd uintptr, iovec []unix.Iovec, offset uint64, flags uint32) {
sqe.opcode = IORING_OP_WRITEV
sqe.fd = int32(fd)
sqe.len = uint32(len(iovec))
sqe.offset = offset
sqe.opcodeFlags = flags
sqe.addr = (uint64)(uintptr(unsafe.Pointer(&iovec[0])))
}
// Readv
func Readv(sqe *SQEntry, fd uintptr, iovec []unix.Iovec, offset uint64, flags uint32) {
sqe.opcode = IORING_OP_READV
sqe.fd = int32(fd)
sqe.len = uint32(len(iovec))
sqe.offset = offset
sqe.opcodeFlags = flags
sqe.addr = (uint64)(uintptr(unsafe.Pointer(&iovec[0])))
}
// WriteFixed ...
func WriteFixed(sqe *SQEntry, fd uintptr, base *byte, len, offset uint64, flags uint32, bufIndex uint16) {
sqe.opcode = IORING_OP_WRITE_FIXED
sqe.fd = int32(fd)
sqe.len = uint32(len)
sqe.offset = offset
sqe.opcodeFlags = flags
sqe.addr = (uint64)(uintptr(unsafe.Pointer(base)))
sqe.SetBufIndex(bufIndex)
}
// ReadFixed ...
func ReadFixed(sqe *SQEntry, fd uintptr, base *byte, len, offset uint64, flags uint32, bufIndex uint16) {
sqe.opcode = IORING_OP_READ_FIXED
sqe.fd = int32(fd)
sqe.len = uint32(len)
sqe.offset = offset
sqe.opcodeFlags = flags
sqe.addr = (uint64)(uintptr(unsafe.Pointer(base)))
sqe.SetBufIndex(bufIndex)
}
// Fsync ...
func Fsync(sqe *SQEntry, fd uintptr) {
sqe.opcode = IORING_OP_FSYNC
sqe.fd = int32(fd)
}
// Fdatasync ...
func Fdatasync(sqe *SQEntry, fd uintptr) {
sqe.opcode = IORING_OP_FSYNC
sqe.fd = int32(fd)
sqe.opcodeFlags = IORING_FSYNC_DATASYNC
}
// Openat
func Openat(sqe *SQEntry, dfd int32, pathptr *byte, flags uint32, mode uint32) {
sqe.opcode = IORING_OP_OPENAT
sqe.fd = dfd
sqe.opcodeFlags = flags
sqe.addr = (uint64)(uintptr(unsafe.Pointer(pathptr)))
sqe.len = mode
}
// Close ...
func Close(sqe *SQEntry, fd uintptr) {
sqe.opcode = IORING_OP_CLOSE
sqe.fd = int32(fd)
}
// Send ...
func Send(sqe *SQEntry, fd uintptr, buf []byte, flags uint32) {
sqe.SetOpcode(IORING_OP_SEND)
sqe.SetFD(int32(fd))
sqe.SetAddr((uint64)(uintptr(unsafe.Pointer(&buf[0]))))
sqe.SetLen(uint32(len(buf)))
sqe.SetOpcodeFlags(flags)
}
// Recv ...
func Recv(sqe *SQEntry, fd uintptr, buf []byte, flags uint32) {
sqe.SetOpcode(IORING_OP_RECV)
sqe.SetFD(int32(fd))
sqe.SetAddr((uint64)(uintptr(unsafe.Pointer(&buf[0]))))
sqe.SetLen(uint32(len(buf)))
sqe.SetOpcodeFlags(flags)
}
// Timeout operation.
// if abs is true then IORING_TIMEOUT_ABS will be added to timeoutFlags.
// count is the number of events to wait.
func Timeout(sqe *SQEntry, ts *unix.Timespec, abs bool, count uint64) {
sqe.SetFD(-1)
sqe.SetOpcode(IORING_OP_TIMEOUT)
sqe.SetAddr((uint64)(uintptr(unsafe.Pointer(ts))))
sqe.SetLen(1)
if abs {
sqe.SetOpcodeFlags(IORING_TIMEOUT_ABS)
}
sqe.SetOffset(count)
}
// LinkTimeout will cancel linked operation if it doesn't complete in time.
func LinkTimeout(sqe *SQEntry, ts *unix.Timespec, abs bool) {
sqe.SetFD(-1)
sqe.SetOpcode(IORING_OP_LINK_TIMEOUT)
sqe.SetAddr((uint64)(uintptr(unsafe.Pointer(ts))))
sqe.SetLen(1)
if abs {
sqe.SetOpcodeFlags(IORING_TIMEOUT_ABS)
}
}