forked from gosexy/redis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfd.go
187 lines (148 loc) · 3.02 KB
/
fd.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
// This file is based on src/pkg/net/fd.go.
//
// I removed most of the networking stuff only to leave a bare bones file
// descriptor watcher that could make use of already existing multi-OS code,
// such as fd_linux.go, fd_darwin.go, etc.
//
// <[email protected]>
//
// Original net/fd.go's notice:
//
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin freebsd linux netbsd openbsd
package redis
import (
"sync"
"syscall"
"time"
)
// File descriptor.
type fileFD struct {
sysfd int
rdeadline int64
wdeadline int64
}
var fds = map[int]*fileFD{}
type pollServer struct {
poll *pollster // low-level OS hooks
sync.Mutex // controls pending and deadline
pending map[int]*fileFD
deadline int64 // next deadline (nsec since 1970)
}
func (s *pollServer) AddFD(fd *fileFD, mode int) error {
intfd := fd.sysfd
if intfd < 0 {
return nil
}
var t int64
key := intfd << 1
if mode == 'r' {
t = fd.rdeadline
} else {
key++
t = fd.wdeadline
}
s.pending[key] = fd
if t > 0 && (s.deadline == 0 || t < s.deadline) {
s.deadline = t
}
_, err := s.poll.AddFD(intfd, mode, false)
if err != nil {
s.EvictWrite(intfd)
s.EvictRead(intfd)
return nil
}
return nil
}
func (s *pollServer) EvictWrite(fd int) {
if ffd, ok := s.pending[fd<<1|1]; ok == true {
s.poll.DelFD(ffd.sysfd, 'w')
delete(s.pending, ffd.sysfd<<1|1)
}
}
func (s *pollServer) EvictRead(fd int) {
if ffd, ok := s.pending[fd<<1]; ok == true {
s.poll.DelFD(ffd.sysfd, 'r')
delete(s.pending, ffd.sysfd<<1)
}
}
func (s *pollServer) Now() int64 {
return time.Now().UnixNano()
}
func (s *pollServer) Run() {
s.Lock()
defer s.Unlock()
for {
var t = s.deadline
if t > 0 {
t = t - s.Now()
if t <= 0 {
continue
}
}
fd, m, err := s.poll.WaitFD(s, t)
if err != nil {
print("pollServer WaitFD: ", err.Error(), "\n")
return
}
if fd < 0 {
continue
}
if fdev[m] != nil {
if fn, ok := fdev[m][fd]; ok == true {
fn()
}
}
}
}
func (s *pollServer) WaitRead(fd int) error {
var err error
/*
var ok bool
if _, ok = s.pending[fd<<1]; ok == true {
s.EvictRead(fd)
}
*/
ffd, err := newFD(fd)
if err != nil {
return err
}
err = s.AddFD(ffd, 'r')
return err
}
func (s *pollServer) WaitWrite(fd int) error {
var err error
/*
var ok bool
if _, ok = s.pending[fd<<1|1]; ok == true {
s.EvictWrite(fd)
}
*/
ffd, err := newFD(fd)
if err != nil {
return err
}
err = s.AddFD(ffd, 'w')
return err
}
// Network FD methods.
// All the network FDs use a single pollServer.
var pollserver *pollServer
func startServer() {
p, err := newPollServer()
if err != nil {
print("Start pollServer: ", err.Error(), "\n")
}
pollserver = p
}
func newFD(fd int) (*fileFD, error) {
if err := syscall.SetNonblock(fd, true); err != nil {
return nil, err
}
ffd := &fileFD{
sysfd: fd,
}
return ffd, nil
}