forked from Iceber/iouring-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeout.go
62 lines (47 loc) · 1.73 KB
/
timeout.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
// +build linux
package iouring
import (
"time"
"unsafe"
"golang.org/x/sys/unix"
iouring_syscall "github.com/iceber/iouring-go/syscall"
)
func (prepReq PrepRequest) WithTimeout(timeout time.Duration) []PrepRequest {
linkRequest := func(sqe *iouring_syscall.SubmissionQueueEntry, userData *UserData) {
prepReq(sqe, userData)
sqe.SetFlags(iouring_syscall.IOSQE_FLAGS_IO_LINK)
}
return []PrepRequest{linkRequest, linkTimeout(timeout)}
}
func Timeout(t time.Duration) PrepRequest {
timespec := unix.NsecToTimespec(t.Nanoseconds())
return func(sqe *iouring_syscall.SubmissionQueueEntry, userData *UserData) {
userData.hold(×pec)
userData.request.resolver = timeoutResolver
sqe.PrepOperation(iouring_syscall.IORING_OP_TIMEOUT, -1, uint64(uintptr(unsafe.Pointer(×pec))), 1, 0)
}
}
func TimeoutWithTime(t time.Time) (PrepRequest, error) {
timespec, err := unix.TimeToTimespec(t)
if err != nil {
return nil, err
}
return func(sqe *iouring_syscall.SubmissionQueueEntry, userData *UserData) {
userData.hold(×pec)
userData.request.resolver = timeoutResolver
sqe.PrepOperation(iouring_syscall.IORING_OP_TIMEOUT, -1, uint64(uintptr(unsafe.Pointer(×pec))), 1, 0)
sqe.SetOpFlags(iouring_syscall.IORING_TIMEOUT_ABS)
}, nil
}
func CountCompletionEvent(n uint64) PrepRequest {
return func(sqe *iouring_syscall.SubmissionQueueEntry, userData *UserData) {
userData.request.resolver = timeoutResolver
sqe.PrepOperation(iouring_syscall.IORING_OP_TIMEOUT, -1, 0, 0, n)
}
}
func RemoveTimeout(id uint64) PrepRequest {
return func(sqe *iouring_syscall.SubmissionQueueEntry, userData *UserData) {
userData.request.resolver = removeTimeoutResolver
sqe.PrepOperation(iouring_syscall.IORING_OP_TIMEOUT, -1, id, 0, 0)
}
}