1
2
3
4
5
6
7
8 package unix
9
10 import (
11 "sync"
12 )
13
14
15
16
17
18 type EpollEvent struct {
19 Events uint32
20 Fd int32
21 Pad int32
22 }
23
24 const (
25 EPOLLERR = 0x8
26 EPOLLHUP = 0x10
27 EPOLLIN = 0x1
28 EPOLLMSG = 0x400
29 EPOLLOUT = 0x4
30 EPOLLPRI = 0x2
31 EPOLLRDBAND = 0x80
32 EPOLLRDNORM = 0x40
33 EPOLLWRBAND = 0x200
34 EPOLLWRNORM = 0x100
35 EPOLL_CTL_ADD = 0x1
36 EPOLL_CTL_DEL = 0x2
37 EPOLL_CTL_MOD = 0x3
38
39
40
41
42
43
44
45
46 )
47
48
49
50
51
52
53 func epToPollEvt(events uint32) int16 {
54 var ep2p = map[uint32]int16{
55 EPOLLIN: POLLIN,
56 EPOLLOUT: POLLOUT,
57 EPOLLHUP: POLLHUP,
58 EPOLLPRI: POLLPRI,
59 EPOLLERR: POLLERR,
60 }
61
62 var pollEvts int16 = 0
63 for epEvt, pEvt := range ep2p {
64 if (events & epEvt) != 0 {
65 pollEvts |= pEvt
66 }
67 }
68
69 return pollEvts
70 }
71
72
73 func pToEpollEvt(revents int16) uint32 {
74 var p2ep = map[int16]uint32{
75 POLLIN: EPOLLIN,
76 POLLOUT: EPOLLOUT,
77 POLLHUP: EPOLLHUP,
78 POLLPRI: EPOLLPRI,
79 POLLERR: EPOLLERR,
80 }
81
82 var epollEvts uint32 = 0
83 for pEvt, epEvt := range p2ep {
84 if (revents & pEvt) != 0 {
85 epollEvts |= epEvt
86 }
87 }
88
89 return epollEvts
90 }
91
92
93 type epollImpl struct {
94 mu sync.Mutex
95 epfd2ep map[int]*eventPoll
96 nextEpfd int
97 }
98
99
100
101 type eventPoll struct {
102 mu sync.Mutex
103 fds map[int]*EpollEvent
104 }
105
106
107 var impl epollImpl = epollImpl{
108 epfd2ep: make(map[int]*eventPoll),
109 nextEpfd: 0,
110 }
111
112 func (e *epollImpl) epollcreate(size int) (epfd int, err error) {
113 e.mu.Lock()
114 defer e.mu.Unlock()
115 epfd = e.nextEpfd
116 e.nextEpfd++
117
118 e.epfd2ep[epfd] = &eventPoll{
119 fds: make(map[int]*EpollEvent),
120 }
121 return epfd, nil
122 }
123
124 func (e *epollImpl) epollcreate1(flag int) (fd int, err error) {
125 return e.epollcreate(4)
126 }
127
128 func (e *epollImpl) epollctl(epfd int, op int, fd int, event *EpollEvent) (err error) {
129 e.mu.Lock()
130 defer e.mu.Unlock()
131
132 ep, ok := e.epfd2ep[epfd]
133 if !ok {
134
135 return EBADF
136 }
137
138 switch op {
139 case EPOLL_CTL_ADD:
140
141
142 if _, ok := ep.fds[fd]; ok {
143 return EEXIST
144 }
145 ep.fds[fd] = event
146 case EPOLL_CTL_MOD:
147 if _, ok := ep.fds[fd]; !ok {
148 return ENOENT
149 }
150 ep.fds[fd] = event
151 case EPOLL_CTL_DEL:
152 if _, ok := ep.fds[fd]; !ok {
153 return ENOENT
154 }
155 delete(ep.fds, fd)
156
157 }
158 return nil
159 }
160
161
162 func (ep *eventPoll) getFds() []int {
163 fds := make([]int, len(ep.fds))
164 for fd := range ep.fds {
165 fds = append(fds, fd)
166 }
167 return fds
168 }
169
170 func (e *epollImpl) epollwait(epfd int, events []EpollEvent, msec int) (n int, err error) {
171 e.mu.Lock()
172 ep, ok := e.epfd2ep[epfd]
173
174 if !ok {
175 e.mu.Unlock()
176 return 0, EBADF
177 }
178
179 pollfds := make([]PollFd, 4)
180 for fd, epollevt := range ep.fds {
181 pollfds = append(pollfds, PollFd{Fd: int32(fd), Events: epToPollEvt(epollevt.Events)})
182 }
183 e.mu.Unlock()
184
185 n, err = Poll(pollfds, msec)
186 if err != nil {
187 return n, err
188 }
189
190 i := 0
191 for _, pFd := range pollfds {
192 if pFd.Revents != 0 {
193 events[i] = EpollEvent{Fd: pFd.Fd, Events: pToEpollEvt(pFd.Revents)}
194 i++
195 }
196
197 if i == n {
198 break
199 }
200 }
201
202 return n, nil
203 }
204
205 func EpollCreate(size int) (fd int, err error) {
206 return impl.epollcreate(size)
207 }
208
209 func EpollCreate1(flag int) (fd int, err error) {
210 return impl.epollcreate1(flag)
211 }
212
213 func EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error) {
214 return impl.epollctl(epfd, op, fd, event)
215 }
216
217
218
219 func EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error) {
220 return impl.epollwait(epfd, events, msec)
221 }
222
View as plain text