Poll.xs

filter: Statementstmt 84.3% 124 / 147 Branchbran 71.6% 53 / 74 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.0% 177 / 221 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« poexs.c ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include <string.h> /* for memmove() mostly */
5 #include <errno.h> /* errno values */
6 #include <poll.h>
7 #include <sys/time.h>
8 #include <time.h>
9 #include "alloc.h"
10 #include "poexs.h"
11  
12 /*#define XS_LOOP_DEBUG*/
13  
14 #if defined(MEM_DEBUG) || defined(XS_LOOP_DEBUG)
15 /* sizes that should require re-allocation of the arrays */
16 #define START_FD_ALLOC 5
17 #define START_LOOKUP_ALLOC 10
18 #else
19 /* more than we need on average */
20 #define START_FD_ALLOC 50
21 #define START_LOOKUP_ALLOC 100
22 #endif
23  
24 #define LOOP_CHECK_INITIALIZED() if (lp_fds == NULL) croak("POE::XS::Loop::Poll hasn't been initialized correctly");
25  
26 #ifdef XS_LOOP_TRACE
27 #define lp_tracing_enabled() 1
28 #else
29 #define lp_tracing_enabled() 0
30 #endif
31  
32 #define lpm_loop_resume_time_watcher(self, next_time) lp_loop_resume_time_watcher(next_time)
33 #define lpm_loop_reset_time_watcher(self, next_time) lp_loop_reset_time_watcher(next_time)
34 #define lpm_loop_watch_filehandle(self, handle, mode) lp_loop_watch_filehandle(handle, mode)
35 #define lpm_loop_ignore_filehandle(self, handle, mode) lp_loop_ignore_filehandle(handle, mode)
36 #define lpm_loop_pause_filehandle(self, handle, mode) lp_loop_ignore_filehandle(handle, mode)
37 #define lpm_loop_resume_filehandle(self, handle, mode) lp_loop_watch_filehandle(handle, mode)
38  
39 /* no ops */
40 #define lp_loop_attach_uidestroy(kernel)
41 #define lp_loop_halt(kernel)
42  
43 /* the next time-based event to be dispatched */
44 static double lp_next_time;
45  
46 /* poll fd structures, lp_fd_count are in use, lp_fd_alloc are available */
47 static struct pollfd *lp_fds;
48 static int lp_fd_count;
49 static int lp_fd_alloc;
50  
51 /* lookup table to translate fd numbers to an index in lp_fds
52 if the entry in lp_fd_lookup is -1 then no entry is allocated */
53 static int *lp_fd_lookup;
54 static int lp_fd_lookup_alloc;
55  
56 #ifdef XS_LOOP_TRACE
57 /* when we started working, used only for tracing */
58 static double lp_start_time;
59 #endif
60  
61 /* functions should be static, hopefully the compiler will inline them
62 into the XS code */
63  
64 static void
65 50 lp_loop_initialize(SV *kernel) {
66 int i;
67  
68 50 poe_initialize();
69  
70 POE_TRACE_CALL(("<cl> loop_initialize()"));
71  
72 50 lp_next_time = 0;
73  
74 50 lp_fd_alloc = START_FD_ALLOC;
75 50 lp_fd_count = 0;
76 50 lp_fds = mymalloc(sizeof(*lp_fds) * START_FD_ALLOC);
77  
78 50 lp_fd_lookup_alloc = START_LOOKUP_ALLOC;
79 50 lp_fd_lookup = mymalloc(sizeof(*lp_fd_lookup) * START_LOOKUP_ALLOC);
80 5050 ▶ for (i = 0; i < lp_fd_lookup_alloc; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < lp_fd_lookup_alloc; ++i) 5000 50 5050
81 5000 lp_fd_lookup[i] = -1;
82  
83 #ifdef XS_LOOP_TRACE
84 lp_start_time = poe_timeh();
85 #endif
86 50 }
87  
88 static void
89 46 lp_loop_finalize(SV *kernel) {
90 POE_TRACE_CALL(("<cl> loop_finalize()"));
91  
92 #ifdef XS_LOOP_TRACE
93 if (lp_fd_count) {
94 int i;
95 POE_TRACE_FILE(("LOOP HANDLE LEAK"));
96 for (i = 0; i < lp_fd_count; ++i) {
97 POE_TRACE_FILE(("Index %d: fd %d mask %x", i, lp_fds[i].fd, lp_fds[i].events));
98 }
99 }
100 #endif
101  
102 46 myfree(lp_fds);
103 46 lp_fds = NULL;
104 46 lp_fd_count = 0;
105 46 lp_fd_alloc = 0;
106 46 myfree(lp_fd_lookup);
107 46 lp_fd_lookup = NULL;
108 46 lp_fd_lookup_alloc = 0;
109 46 }
110  
111 static int
112 888 _get_file_entry(int fd) {
113 888 ▶ if (fd < 0 || fd > lp_fd_lookup_alloc)
Branch: 2 branches
BranchTrueFalseTotal
if (fd < 0 || fd > lp_fd_lookup_alloc) 888 0 888
if (fd < 0 || fd > lp_fd_lookup_alloc) 0 888 888
114 0 return -1;
115  
116 888 return lp_fd_lookup[fd];
117 }
118  
119 /*
120 expand the fd lookup table so that we can store the given fd in it
121 */
122 static void
123 0 _expand_fd_lookup(int fd) {
124 int i;
125 0 int new_alloc = lp_fd_lookup_alloc * 2;
126 0 ▶ if (fd >= new_alloc)
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= new_alloc) 0 0 0
127 0 new_alloc = fd + 1;
128  
129 0 lp_fd_lookup = myrealloc(lp_fd_lookup, sizeof(*lp_fd_lookup) * new_alloc);
130 0 ▶ for (i = lp_fd_lookup_alloc; i < new_alloc; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = lp_fd_lookup_alloc; i < new_alloc; ++i) 0 0 0
131 0 lp_fd_lookup[i] = -1;
132 0 lp_fd_lookup_alloc = new_alloc;
133 0 }
134  
135 static void
136 0 _expand_fds(void) {
137 0 int new_alloc = lp_fd_alloc * 2;
138 0 lp_fds = myrealloc(lp_fds, sizeof(*lp_fds) * new_alloc);
139 0 lp_fd_alloc = new_alloc;
140 0 }
141  
142 static int
143 577 _make_file_entry(int fd) {
144 int entry;
145  
146 577 ▶ if (fd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) 0 577 577
147 0 return -1;
148  
149 577 ▶ if (fd >= lp_fd_lookup_alloc)
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= lp_fd_lookup_alloc) 0 577 577
150 0 _expand_fd_lookup(fd);
151  
152 577 entry = lp_fd_lookup[fd];
153 577 ▶ if (entry == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry == -1) { 338 239 577
154 338 ▶ if (lp_fd_count == lp_fd_alloc)
Branch: 1 branch
BranchTrueFalseTotal
if (lp_fd_count == lp_fd_alloc) 0 338 338
155 0 _expand_fds();
156  
157 338 entry = lp_fd_count++;
158 338 lp_fd_lookup[fd] = entry;
159 338 lp_fds[entry].fd = fd;
160 338 lp_fds[entry].events = 0;
161 338 lp_fds[entry].revents = 0;
162  
163 338 return entry;
164 }
165 else {
166 239 return entry;
167 }
168 }
169  
170 static void
171 307 _release_file_entry(int fd) {
172 307 int entry = _get_file_entry(fd);
173  
174 307 ▶ if (entry < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (entry < 0) 0 307 307
175 0 croak("Attempt to remove a non-existent poll entry");
176  
177 307 fd = lp_fds[entry].fd;
178 307 lp_fd_lookup[fd] = -1;
179  
180 307 ▶ if (entry != lp_fd_count-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry != lp_fd_count-1) { 141 166 307
181 /* move the last entry into place */
182 141 lp_fds[entry] = lp_fds[lp_fd_count-1];
183 141 lp_fd_lookup[lp_fds[entry].fd] = entry;
184 }
185  
186 307 --lp_fd_count;
187 307 }
188  
189 static int
190 1879 _poll_from_poe_mode(int mode) {
191 1879 switch (mode) {
192 701 case MODE_RD:
193 701 return POLLIN;
194  
195 910 case MODE_WR:
196 910 return POLLOUT;
197  
198 268 case MODE_EX:
199 268 return POLLPRI;
200  
201 0 default:
202 0 croak("Unknown filehandle watch mode %d", mode);
203 }
204 }
205  
206 #ifdef XS_LOOP_TRACE
207  
208 static const char *
209 poll_mode_names(int mask) {
210 switch (mask) {
211 case 0:
212 case POLLIN:
213 return "POLLIN";
214  
215 case POLLOUT:
216 return "POLLOUT";
217  
218 case POLLPRI:
219 return "POLLPRI";
220  
221 case POLLIN | POLLOUT:
222 return "POLLIN | POLLOUT";
223  
224 case POLLIN | POLLPRI:
225 return "POLLIN | POLLPRI";
226  
227 case POLLOUT | POLLPRI:
228 return "POLLOUT | POLLPRI";
229  
230 case POLLOUT | POLLIN | POLLPRI:
231 return "POLLOUT | POLLIN | POLLPRI";
232  
233 case POLLNVAL:
234 return "POLLNVAL";
235  
236 default:
237 return "Unknown";
238 }
239 }
240  
241 #endif
242  
243 static void
244 652 lp_loop_do_timeslice(SV *kernel) {
245 652 double delay = 3600;
246 int count;
247 double now;
248 int save_errno;
249  
250 652 ▶ LOOP_CHECK_INITIALIZED();
Branch: 1 branch
BranchTrueFalseTotal
LOOP_CHECK_INITIALIZED(); 0 652 652
251  
252 POE_TRACE_CALL(("<cl> loop_do_timeslice()"));
253  
254 652 poe_test_if_kernel_idle(kernel);
255  
256 652 now = poe_timeh();
257 652 ▶ if (lp_next_time) {
Branch: 1 branch
BranchTrueFalseTotal
if (lp_next_time) { 527 125 652
258 527 delay = lp_next_time - now;
259 527 ▶ if (delay > 3600)
Branch: 1 branch
BranchTrueFalseTotal
if (delay > 3600) 0 527 527
260 0 delay = 3600;
261 }
262 652 ▶ if (delay < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (delay < 0) 340 312 652
263 340 delay = 0;
264  
265 #ifdef XS_LOOP_TRACE
266  
267 {
268 int i;
269 SV *trace_sv = newSVpv("<fh> ,---- XS POLL FDS IN ----\n", 0);
270 for (i = 0; i < lp_fd_count; ++i) {
271 sv_catpvf(trace_sv, "<fh> | Index %2d fd %3d mask %x (%s)\n", i, lp_fds[i].fd, lp_fds[i].events, poll_mode_names(lp_fds[i].events));
272 }
273 sv_catpv(trace_sv, "<fh> `-------------------------");
274 /*POE_TRACE_FILE(("<fh> Delay %f\n", delay));*/
275 POE_TRACE_FILE((POE_SV_FORMAT, trace_sv));
276 SvREFCNT_dec(trace_sv);
277 }
278 POE_TRACE_EVENT(("<ev> Kernel::run() iterating (XS) now(%.4f) timeout(%.4f)"
279 " then(%.4f)\n", now - lp_start_time, delay, (now - lp_start_time) + delay));
280  
281 #endif
282  
283 652 count = poll(lp_fds, lp_fd_count, (int)(delay * 1000));
284 652 save_errno = errno;
285  
286 #ifdef XS_LOOP_TRACE
287 if (poe_tracing_files()) {
288 int i;
289 SV *trace_sv = newSVpvf("<fh> poll() => %d (%d)\n", count, save_errno);
290  
291 sv_catpvf(trace_sv, "<fh> /---- XS POLL FDS OUT ----\n");
292 for (i = 0; i < lp_fd_count; ++i) {
293 if (lp_fds[i].revents) {
294 sv_catpvf(trace_sv, "<fh> | Index %2d fd %d mask %x (%s)\n", i,
295 lp_fds[i].fd, lp_fds[i].revents, poll_mode_names(lp_fds[i].revents));
296 }
297 }
298 sv_catpv(trace_sv, "<fh> `-------------------------");
299 POE_TRACE_FILE((POE_SV_FORMAT, trace_sv));
300 SvREFCNT_dec(trace_sv);
301 }
302 #endif
303  
304 /* POE::Loop::Select puts this after the event dispatch, but as the
305 comment there notes, it's not really idle time when it includes
306 the event dispatch processing, so I've moved it here */
307 POE_STAT_ADD(kernel, "idle_seconds", poe_timeh()-now);
308  
309 652 errno = save_errno;
310 652 ▶ if (count < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count < 0) { 6 646 652
311 6 ▶ if (errno != EINPROGRESS &&
Branch: 1 branch
BranchTrueFalseTotal
if (errno != EINPROGRESS && 6 0 6
312 6 ▶ errno != EWOULDBLOCK &&
Branch: 1 branch
BranchTrueFalseTotal
errno != EWOULDBLOCK && 6 0 6
313 6 ▶ errno != EINTR) {
Branch: 1 branch
BranchTrueFalseTotal
errno != EINTR) { 0 6 6
314 /* pass $! for auto-magical text description of errno */
315 0 poe_trap("<fh> poll error: " POE_SV_FORMAT " (%d)", get_sv("!", 0), errno);
316 }
317 }
318 646 ▶ else if (count) {
Branch: 1 branch
BranchTrueFalseTotal
else if (count) { 268 378 646
319 int mode;
320 int i;
321 268 int *fds[3] = { NULL };
322 268 int counts[3] = { 0, 0, 0 };
323 int masks[3];
324  
325 268 fds[0] = mymalloc(sizeof(int) * lp_fd_count * 3);
326 268 fds[1] = fds[0] + lp_fd_count;
327 268 fds[2] = fds[1] + lp_fd_count;
328 1072 ▶ for (mode = MODE_RD; mode <= MODE_EX; ++mode) {
Branch: 1 branch
BranchTrueFalseTotal
for (mode = MODE_RD; mode <= MODE_EX; ++mode) { 804 268 1072
329 804 masks[mode] = _poll_from_poe_mode(mode);
330 }
331  
332 /* build an array of fds for each event */
333 2059 ▶ for (i = 0; i < lp_fd_count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < lp_fd_count; ++i) { 1791 268 2059
334 1791 ▶ if (lp_fds[i].revents) {
Branch: 1 branch
BranchTrueFalseTotal
if (lp_fds[i].revents) { 573 1218 1791
335 573 int revents = lp_fds[i].revents;
336 2292 ▶ for (mode = MODE_RD; mode <= MODE_EX; ++mode) {
Branch: 1 branch
BranchTrueFalseTotal
for (mode = MODE_RD; mode <= MODE_EX; ++mode) { 1719 573 2292
337 1719 ▶ if ((lp_fds[i].events & masks[mode])
Branch: 1 branch
BranchTrueFalseTotal
if ((lp_fds[i].events & masks[mode]) 712 1007 1719
338 712 ▶ && revents & (masks[mode] | POLLHUP | POLLERR | POLLNVAL)) {
Branch: 1 branch
BranchTrueFalseTotal
&& revents & (masks[mode] | POLLHUP | POLLERR | POLLNVAL)) { 575 137 712
339 575 fds[mode][counts[mode]++] = lp_fds[i].fd;
340 }
341 }
342 }
343 }
344  
345 1072 ▶ for (mode = MODE_RD; mode <= MODE_EX; ++mode) {
Branch: 1 branch
BranchTrueFalseTotal
for (mode = MODE_RD; mode <= MODE_EX; ++mode) { 804 268 1072
346 804 ▶ if (counts[mode])
Branch: 1 branch
BranchTrueFalseTotal
if (counts[mode]) 320 484 804
347 320 poe_enqueue_data_ready(kernel, mode, fds[mode], counts[mode]);
348 }
349 268 myfree(fds[0]);
350 }
351 else {
352 POE_TRACE_FILE(("<fh> poll timed out"));
353 }
354  
355 652 poe_data_ev_dispatch_due(kernel);
356 652 }
357  
358 static void
359 46 lp_loop_run(SV *kernel) {
360 46 ▶ LOOP_CHECK_INITIALIZED();
Branch: 1 branch
BranchTrueFalseTotal
LOOP_CHECK_INITIALIZED(); 0 46 46
361  
362 POE_TRACE_CALL(("<cl> loop_run()"));
363 698 ▶ while (poe_data_ses_count(kernel)) {
Branch: 1 branch
BranchTrueFalseTotal
while (poe_data_ses_count(kernel)) { 652 46 698
364 652 lp_loop_do_timeslice(kernel);
365 }
366 46 }
367  
368 static void
369 358 lp_loop_resume_time_watcher(double next_time) {
370 358 ▶ LOOP_CHECK_INITIALIZED();
Branch: 1 branch
BranchTrueFalseTotal
LOOP_CHECK_INITIALIZED(); 0 358 358
371  
372 POE_TRACE_CALL(("<cl> loop_resume_time_watcher(%.3f) %.3f from now",
373 next_time, next_time - poe_timeh()));
374 358 lp_next_time = next_time;
375 358 }
376  
377 static void
378 496 lp_loop_reset_time_watcher(double next_time) {
379 496 ▶ LOOP_CHECK_INITIALIZED();
Branch: 1 branch
BranchTrueFalseTotal
LOOP_CHECK_INITIALIZED(); 0 496 496
380  
381 POE_TRACE_CALL(("<cl> loop_reset_time_watcher(%.3f) %.3f from now",
382 next_time, next_time - poe_timeh()));
383 496 lp_next_time = next_time;
384 496 }
385  
386 static void
387 220 lp_loop_pause_time_watcher(SV *kernel) {
388 220 ▶ LOOP_CHECK_INITIALIZED();
Branch: 1 branch
BranchTrueFalseTotal
LOOP_CHECK_INITIALIZED(); 0 220 220
389  
390 POE_TRACE_CALL(("<cl> loop_pause_time_watcher()"));
391 220 lp_next_time = 0;
392 220 }
393  
394 static void
395 577 lp_loop_watch_filehandle(PerlIO *handle, int mode) {
396 577 int fd = PerlIO_fileno(handle);
397 577 int entry = _make_file_entry(fd);
398  
399 577 ▶ LOOP_CHECK_INITIALIZED();
Branch: 1 branch
BranchTrueFalseTotal
LOOP_CHECK_INITIALIZED(); 0 577 577
400  
401 POE_TRACE_CALL(("<cl> loop_watch_filehandle(%d, %d %s)",
402 fd, mode, poe_mode_names(mode)));
403  
404 577 lp_fds[entry].events |= _poll_from_poe_mode(mode);
405 577 }
406  
407 static void
408 581 lp_loop_ignore_filehandle(PerlIO *handle, int mode) {
409 581 int fd = PerlIO_fileno(handle);
410 581 int entry = _get_file_entry(fd);
411  
412 581 ▶ LOOP_CHECK_INITIALIZED();
Branch: 1 branch
BranchTrueFalseTotal
LOOP_CHECK_INITIALIZED(); 0 581 581
413  
414 POE_TRACE_CALL(("<cl> loop_ignore_filehandle(%d, %d %s)",
415 fd, mode, poe_mode_names(mode)));
416  
417 581 ▶ if (entry >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (entry >= 0) { 498 83 581
418 498 lp_fds[entry].events &= ~_poll_from_poe_mode(mode);
419  
420 498 ▶ if (lp_fds[entry].events == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lp_fds[entry].events == 0) { 307 191 498
421 307 _release_file_entry(fd);
422 }
423 }
424 581 }
425  
426 MODULE = POE::XS::Loop::Poll PACKAGE = POE::Kernel PREFIX = lp_
427  
428 PROTOTYPES: DISABLE
429  
430 void
431 lp_loop_initialize(kernel)
432 SV *kernel
433  
434 void
435 lp_loop_finalize(kernel)
436 SV *kernel
437  
438 void
439 lp_loop_do_timeslice(kernel)
440 SV *kernel
441  
442 void
443 lp_loop_run(kernel)
444 SV *kernel
445  
446 void
447 lp_loop_halt(kernel)
448  
449 void
450 lp_loop_pause_time_watcher(kernel)
451 SV *kernel
452  
453 void
454 lp_loop_attach_uidestroy(kernel)
455  
456 MODULE = POE::XS::Loop::Poll PACKAGE = POE::Kernel PREFIX = lpm_
457  
458 void
459 lpm_loop_resume_time_watcher(self, next_time)
460 double next_time
461  
462 void
463 lpm_loop_reset_time_watcher(self, next_time);
464 double next_time
465  
466 void
467 lpm_loop_watch_filehandle(self, fh, mode)
468 PerlIO *fh
469 int mode
470  
471 void
472 lpm_loop_ignore_filehandle(self, fh, mode)
473 PerlIO *fh
474 int mode
475  
476 void
477 lpm_loop_pause_filehandle(self, fh, mode)
478 PerlIO *fh
479 int mode
480  
481 void
482 lpm_loop_resume_filehandle(self, fh, mode)
483 PerlIO *fh
484 int mode
485  
486 MODULE = POE::XS::Loop::Poll PACKAGE = POE::XS::Loop::Poll PREFIX = lp_
487  
488 int
489 lp_tracing_enabled()
490  
« poexs.c ↑ Summary