lib/IO/Ppoll.xs

filter: Statementstmt 95.3% 62 / 65 Branchbran 72.7% 32 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.2% 94 / 109 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/IO/Ppoll.pm ↑ Summary
1 /* You may distribute under the terms of either the GNU General Public License
2 * or the Artistic License (the same terms as Perl itself)
3 *
4 * (C) Paul Evans, 2007,2008 -- leonerd@leonerd.org.uk
5 */
6  
7 #include "EXTERN.h"
8 #include "perl.h"
9 #include "XSUB.h"
10  
11 #include "poll.h"
12  
13 /* mPUSHi is a 5.8 thing. We'll have to define it ourselves to support older
14 * perls
15 */
16  
17 #ifndef PUSHmortal
18 # define PUSHmortal PUSHs(sv_newmortal())
19 #endif
20  
21 #ifndef mPUSHi
22 # define mPUSHi(i) sv_setiv_mg(PUSHmortal, (IV)(i))
23 #endif
24  
25 MODULE = IO::Ppoll PACKAGE = IO::Ppoll
26  
27 BOOT:
28 {
29 HV *stash;
30 4 stash = gv_stashpvn("IO::Ppoll", 9, TRUE);
31 4 newCONSTSUB(stash, "POLLIN", newSViv(POLLIN));
32 4 newCONSTSUB(stash, "POLLOUT", newSViv(POLLOUT));
33 4 newCONSTSUB(stash, "POLLPRI", newSViv(POLLPRI));
34 4 newCONSTSUB(stash, "POLLERR", newSViv(POLLERR));
35 4 newCONSTSUB(stash, "POLLHUP", newSViv(POLLHUP));
36 4 newCONSTSUB(stash, "POLLNVAL",newSViv(POLLNVAL));
37 }
38  
39 SV *
40 get_events(fds, nfds, fd)
41 SV *fds
42 int nfds
43 int fd
44 INIT:
45 int i;
46 struct pollfd *fds_real;
47 CODE:
48 4 fds_real = (struct pollfd *)SvPV_nolen(fds);
49 4 ▶ for(i = 0; i < nfds; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfds; i++) { 4 0 4
50 4 ▶ if(fds_real[i].fd == fd)
Branch: 1 branch
BranchTrueFalseTotal
if(fds_real[i].fd == fd) 4 0 4
51 4 XSRETURN_IV(fds_real[i].events);
52 }
53 0 XSRETURN_NO;
54  
55 int
56 get_revents(fds, nfds, fd)
57 SV *fds
58 int nfds
59 int fd
60 INIT:
61 int i;
62 struct pollfd *fds_real;
63 CODE:
64 3 fds_real = (struct pollfd *)SvPV_nolen(fds);
65 6 ▶ for(i = 0; i < nfds; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfds; i++) { 5 1 6
66 5 ▶ if(fds_real[i].fd == fd)
Branch: 1 branch
BranchTrueFalseTotal
if(fds_real[i].fd == fd) 2 3 5
67 2 XSRETURN_IV(fds_real[i].revents);
68 }
69 1 XSRETURN_NO;
70  
71 void
72 get_fds(fds, nfds)
73 SV *fds
74 int nfds
75 INIT:
76 int i;
77 struct pollfd *fds_real;
78 PPCODE:
79 3 fds_real = (struct pollfd *)SvPV_nolen(fds);
80 3 ▶ EXTEND(SP, nfds);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, nfds); 3 0 3
EXTEND(SP, nfds); 0 3 3
81 4 ▶ for(i = 0; i < nfds; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfds; i++) { 1 3 4
82 1 int fd = fds_real[i].fd;
83 1 mPUSHi(fd);
84 }
85  
86 void
87 get_fds_for(fds, nfds, events)
88 SV *fds
89 int nfds
90 int events
91 INIT:
92 int i;
93 struct pollfd *fds_real;
94 PPCODE:
95 1 fds_real = (struct pollfd *)SvPV_nolen(fds);
96 1 ▶ EXTEND(SP, nfds);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, nfds); 1 0 1
EXTEND(SP, nfds); 0 1 1
97 3 ▶ for(i = 0; i < nfds; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfds; i++) { 2 1 3
98 int fd;
99 2 ▶ if((fds_real[i].revents & events) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if((fds_real[i].revents & events) == 0) 1 1 2
100 1 continue;
101 1 fd = fds_real[i].fd;
102 1 mPUSHi(fd);
103 }
104  
105 void
106 mas_events(fds, nfds, fd, maskbits, setbits)
107 SV *fds
108 int &nfds
109 int fd
110 int maskbits
111 int setbits
112 CODE:
113 6 struct pollfd *fds_real = (struct pollfd *)SvPV_nolen(fds);
114 int i;
115 7 ▶ for(i = 0; i < nfds; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfds; i++) { 4 3 7
116 4 ▶ if(fds_real[i].fd == fd) {
Branch: 1 branch
BranchTrueFalseTotal
if(fds_real[i].fd == fd) { 3 1 4
117 3 fds_real[i].events &= maskbits;
118 3 fds_real[i].events |= setbits;
119 3 break;
120 }
121 }
122 6 ▶ if(i == nfds) {
Branch: 1 branch
BranchTrueFalseTotal
if(i == nfds) { 3 3 6
123 3 nfds++;
124 3 ▶ SvGROW(fds, nfds * sizeof(struct pollfd));
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(fds, nfds * sizeof(struct pollfd)); 1 2 3
SvGROW(fds, nfds * sizeof(struct pollfd)); 0 1 1
125 3 SvCUR_set(fds, nfds * sizeof(struct pollfd));
126 3 SvPOK_only(fds);
127 3 fds_real = (struct pollfd *)SvPV(fds, PL_na);
128 3 fds_real[i].fd = fd;
129 3 fds_real[i].events = setbits;
130 }
131 OUTPUT:
132 nfds
133  
134 void
135 del_events(fds, nfds, fd)
136 SV *fds
137 int &nfds
138 int fd
139 INIT:
140 struct pollfd *fds_real;
141 CODE:
142 1 fds_real = (struct pollfd *)SvPV_nolen(fds);
143 int i;
144 1 ▶ for(i = 0; i < nfds; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfds; i++) { 1 0 1
145 1 ▶ if(fds_real[i].fd == fd) {
Branch: 1 branch
BranchTrueFalseTotal
if(fds_real[i].fd == fd) { 1 0 1
146 /* Since we don't care about the ordering here, just move the
147 * top one down */
148 1 fds_real[i] = fds_real[nfds-1];
149 1 nfds--;
150 1 SvCUR_set(fds, nfds * sizeof(struct pollfd));
151 1 break;
152 }
153 }
154 OUTPUT:
155 nfds
156  
157 int
158 do_poll(fds, nfds, timeout, sigmask)
159 SV *fds
160 int nfds
161 SV *timeout
162 SV *sigmask
163 INIT:
164 struct pollfd *fds_real;
165 struct timespec timeout_real; char timeout_valid;
166 sigset_t *sigmask_real;
167 int pollret;
168 CODE:
169 3 fds_real = (struct pollfd *)SvPV_nolen(fds);
170  
171 3 ▶ if(SvOK(timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(timeout)) { 3 0 3
172 3 ▶ if(SvNOK(timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvNOK(timeout)) { 2 1 3
173 2 double timeout_msec = SvNV(timeout);
174 2 timeout_real.tv_sec = ((long)timeout_msec) / 1000;
175 2 timeout_real.tv_nsec = 1000000 * (timeout_msec - 1000*timeout_real.tv_sec);
176 }
177 else {
178 1 long timeout_msec = SvIV(timeout);
179 1 timeout_real.tv_sec = timeout_msec / 1000;
180 1 timeout_real.tv_nsec = 1000000 * (timeout_msec % 1000);
181 }
182 3 timeout_valid = 1;
183 }
184 else
185 0 timeout_valid = 0;
186  
187 3 ▶ if(SvOK(sigmask)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(sigmask)) { 3 0 3
188 /* This code borrowed from POSIX.xs */
189 #if PERL_VERSION > 15 || PERL_VERSION == 15 && PERL_SUBVERSION > 2
190 6 sigmask_real = (sigset_t *) SvPV_nolen(SvRV(sigmask));
191 #else
192 IV tmp = SvIV((SV*)SvRV(sigmask));
193 sigmask_real = INT2PTR(sigset_t*, tmp);
194 #endif
195 }
196 else
197 0 sigmask_real = NULL;
198  
199 3 ▶ RETVAL = ppoll(fds_real, nfds, timeout_valid ? &timeout_real : NULL, sigmask_real);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ppoll(fds_real, nfds, timeout_valid ? &timeout_real : NULL, sigmask_real); 3 0 3
200 OUTPUT:
201 RETVAL
« blib/lib/IO/Ppoll.pm ↑ Summary