Debian Codegraph @

pypy/module/select/interp_epoll.py

raw symbols SCIP
1 from __future__ import with_statement
2
3 import errno
4
5 from pypy.interpreter.baseobjspace import W_Root
6 from pypy.interpreter.gateway import interp2app, unwrap_spec
7 from pypy.interpreter.error import OperationError, oefmt
8 from pypy.interpreter.error import exception_from_saved_errno
9 from pypy.interpreter.typedef import TypeDef, GetSetProperty
10 from pypy.module.time import timeutils
11 from rpython.rtyper.lltypesystem import lltype, rffi
12 from rpython.rtyper.tool import rffi_platform
13 from rpython.rlib._rsocket_rffi import socketclose, FD_SETSIZE
14 from rpython.rlib.rposix import get_saved_errno
15 from rpython.rlib.rarithmetic import intmask, widen
16 from rpython.translator.tool.cbuild import ExternalCompilationInfo
17
18
19 eci = ExternalCompilationInfo(
20 includes = ['sys/epoll.h', 'stddef.h', 'stdlib.h', 'stdio.h'],
21 post_include_bits = [
22 "RPY_EXTERN\n"
23 "int pypy_epoll_ctl(int, int, int, uint32_t);"
24 "RPY_EXTERN\n"
25 "int pypy_epoll_wait(int, uint32_t*, int*, int, int);"
26 ],
27 separate_module_sources = ['''
28 int pypy_epoll_ctl(int epfd, int op, int fd, uint32_t events){
29 struct epoll_event evt = {events, (epoll_data_t)fd};
30 return epoll_ctl(epfd, op, fd, &evt);
31 };
32 int pypy_epoll_wait(int epfd, uint32_t *fds, int *evnts, int maxevents, int timeout){
33 struct epoll_event *events = malloc(sizeof(struct epoll_event) * maxevents);
34 if (events == NULL) {
35 return -1;
36 }
37 int ret = epoll_wait(epfd, events, maxevents, timeout);
38 if (ret < 0) {
39 free(events);
40 return ret;
41 }
42 for (int i=0; i<ret; i++) {
43 fds[i] = events[i].data.fd;
44 evnts[i] = events[i].events;
45 }
46 free(events);
47 return ret;
48 };
49 '''],
50 )
51
52 class CConfig:
53 _compilation_info_ = eci
54
55
56 CConfig.epoll_data = rffi_platform.Struct("union epoll_data", [
57 ("fd", rffi.INT),
58 ])
59 CConfig.epoll_event = rffi_platform.Struct("struct epoll_event", [
60 ("events", rffi.UINT),
61 ("data", CConfig.epoll_data)
62 ])
63
64 public_symbols = dict.fromkeys([
65 "EPOLLIN", "EPOLLOUT", "EPOLLPRI", "EPOLLERR", "EPOLLHUP",
66 "EPOLLET", "EPOLLONESHOT", "EPOLLRDNORM", "EPOLLRDBAND",
67 "EPOLLWRNORM", "EPOLLWRBAND", "EPOLLMSG"
68 ])
69 for symbol in public_symbols:
70 setattr(CConfig, symbol, rffi_platform.DefinedConstantInteger(symbol))
71
72 for symbol in ["EPOLL_CTL_ADD", "EPOLL_CTL_MOD", "EPOLL_CTL_DEL",
73 "EPOLL_CLOEXEC"]:
74 setattr(CConfig, symbol, rffi_platform.ConstantInteger(symbol))
75
76 cconfig = rffi_platform.configure(CConfig)
77
78 for symbol in public_symbols:
79 public_symbols[symbol] = intmask(cconfig[symbol])
80
81
82 EPOLL_CTL_ADD = cconfig["EPOLL_CTL_ADD"]
83 EPOLL_CTL_MOD = cconfig["EPOLL_CTL_MOD"]
84 EPOLL_CTL_DEL = cconfig["EPOLL_CTL_DEL"]
85 EPOLL_CLOEXEC = cconfig["EPOLL_CLOEXEC"]
86
87 DEF_REGISTER_EVENTMASK = (public_symbols["EPOLLIN"] |
88 public_symbols["EPOLLOUT"] |
89 public_symbols["EPOLLPRI"])
90
91 epoll_create1 = rffi.llexternal(
92 "epoll_create1", [rffi.INT], rffi.INT, compilation_info=eci,
93 save_err=rffi.RFFI_SAVE_ERRNO
94 )
95 pypy_epoll_ctl = rffi.llexternal(
96 "pypy_epoll_ctl",
97 [rffi.INT, rffi.INT, rffi.INT, rffi.UINT],
98 rffi.INT,
99 compilation_info=eci,
100 save_err=rffi.RFFI_SAVE_ERRNO
101 )
102 pypy_epoll_wait = rffi.llexternal(
103 "pypy_epoll_wait",
104 [rffi.INT_real, rffi.CArrayPtr(rffi.UINT_real), rffi.CArrayPtr(rffi.INT_real),
105 rffi.INT, rffi.INT],
106 rffi.INT,
107 compilation_info=eci,
108 save_err=rffi.RFFI_SAVE_ERRNO
109 )
110
111 class W_Epoll(W_Root):
112 def __init__(self, space, epfd):
113 self.space = space
114 self.epfd = epfd
115 self.register_finalizer(space)
116
117 @unwrap_spec(sizehint=int, flags=int)
118 def descr__new__(space, w_subtype, sizehint=-1, flags=0):
119 if sizehint == -1:
120 sizehint = FD_SETSIZE - 1
121 elif sizehint <= 0: # 'sizehint' is otherwise ignored
122 raise oefmt(space.w_ValueError,
123 "sizehint must be positive or -1")
124 epfd = epoll_create1(flags | EPOLL_CLOEXEC)
125 if epfd < 0:
126 raise exception_from_saved_errno(space, space.w_IOError)
127
128 return W_Epoll(space, epfd)
129
130 @unwrap_spec(fd=int)
131 def descr_fromfd(space, w_cls, fd):
132 return W_Epoll(space, fd)
133
134 def _finalize_(self):
135 self.close()
136
137 def check_closed(self, space):
138 if self.get_closed():
139 raise oefmt(space.w_ValueError, "I/O operation on closed epoll fd")
140
141 def get_closed(self):
142 return self.epfd < 0
143
144 def close(self):
145 if not self.get_closed():
146 socketclose(self.epfd)
147 self.epfd = -1
148 self.may_unregister_rpython_finalizer(self.space)
149
150 def epoll_ctl(self, space, ctl, w_fd, eventmask):
151 fd = space.c_filedescriptor_w(w_fd)
152 result = pypy_epoll_ctl(self.epfd, ctl, fd, rffi.cast(rffi.UINT, eventmask))
153 if result < 0:
154 raise exception_from_saved_errno(space, space.w_IOError)
155
156 def descr_get_closed(self, space):
157 return space.newbool(self.get_closed())
158
159 def descr_fileno(self, space):
160 self.check_closed(space)
161 return space.newint(self.epfd)
162
163 def descr_close(self, space):
164 self.close()
165
166 @unwrap_spec(eventmask=int)
167 def descr_register(self, space, w_fd, eventmask=DEF_REGISTER_EVENTMASK):
168 self.check_closed(space)
169 self.epoll_ctl(space, EPOLL_CTL_ADD, w_fd, eventmask)
170
171 def descr_unregister(self, space, w_fd):
172 self.check_closed(space)
173 self.epoll_ctl(space, EPOLL_CTL_DEL, w_fd, 0)
174
175 @unwrap_spec(eventmask=int)
176 def descr_modify(self, space, w_fd, eventmask):
177 self.check_closed(space)
178 self.epoll_ctl(space, EPOLL_CTL_MOD, w_fd, eventmask)
179
180 @unwrap_spec(timeout=float, maxevents=int)
181 def descr_poll(self, space, timeout=-1.0, maxevents=-1):
182 self.check_closed(space)
183 if timeout < 0:
184 end_time = 0.0
185 itimeout = -1
186 else:
187 end_time = timeutils.monotonic(space) + timeout
188 itimeout = int(timeout * 1000.0 + 0.999)
189
190 if maxevents == -1:
191 maxevents = FD_SETSIZE - 1
192 elif maxevents < 1:
193 raise oefmt(space.w_ValueError,
194 "maxevents must be greater than 0, not %d", maxevents)
195
196 with lltype.scoped_alloc(rffi.CArray(rffi.UINT_real), maxevents) as fids:
197 with lltype.scoped_alloc(rffi.CArray(rffi.INT_real), maxevents) as events:
198 while True:
199 nfds = pypy_epoll_wait(self.epfd, fids, events, maxevents, itimeout)
200 if nfds < 0:
201 if get_saved_errno() == errno.EINTR:
202 space.getexecutioncontext().checksignals()
203 if itimeout >= 0:
204 timeout = end_time - timeutils.monotonic(space)
205 timeout = max(timeout, 0.0)
206 itimeout = int(timeout * 1000.0 + 0.999)
207 continue
208 raise exception_from_saved_errno(space, space.w_IOError)
209 break
210
211 elist_w = [None] * nfds
212 for i in xrange(nfds):
213 elist_w[i] = space.newtuple2(
214 space.newint(intmask(fids[i])), space.newint(intmask(events[i]))
215 )
216 return space.newlist(elist_w)
217
218 def descr_enter(self, space):
219 self.check_closed(space)
220 return self
221
222 def descr_exit(self, space, __args__):
223 self.close()
224
225
226 W_Epoll.typedef = TypeDef("select.epoll",
227 __new__ = interp2app(W_Epoll.descr__new__.im_func),
228 fromfd = interp2app(W_Epoll.descr_fromfd.im_func, as_classmethod=True),
229
230 closed = GetSetProperty(W_Epoll.descr_get_closed),
231 fileno = interp2app(W_Epoll.descr_fileno),
232 close = interp2app(W_Epoll.descr_close),
233 register = interp2app(W_Epoll.descr_register),
234 unregister = interp2app(W_Epoll.descr_unregister),
235 modify = interp2app(W_Epoll.descr_modify),
236 poll = interp2app(W_Epoll.descr_poll),
237 __enter__ = interp2app(W_Epoll.descr_enter),
238 __exit__ = interp2app(W_Epoll.descr_exit),
239 )
240 W_Epoll.typedef.acceptable_as_base_class = False