|
|
1 | #if __linux |
|
|
2 | # define _GNU_SOURCE |
|
|
3 | #endif |
|
|
4 | |
1 | #define _REENTRANT 1 |
5 | #define _REENTRANT 1 |
|
|
6 | |
2 | #include <errno.h> |
7 | #include <errno.h> |
3 | |
8 | |
4 | #include "EXTERN.h" |
9 | #include "EXTERN.h" |
5 | #include "perl.h" |
10 | #include "perl.h" |
6 | #include "XSUB.h" |
11 | #include "XSUB.h" |
… | |
… | |
55 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
60 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
56 | REQ_FSYNC, REQ_FDATASYNC, |
61 | REQ_FSYNC, REQ_FDATASYNC, |
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
62 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
58 | REQ_READDIR, |
63 | REQ_READDIR, |
59 | REQ_LINK, REQ_SYMLINK, |
64 | REQ_LINK, REQ_SYMLINK, |
|
|
65 | REQ_GROUP, REQ_NOP, |
60 | REQ_SLEEP, |
66 | REQ_SLEEP, |
61 | REQ_GROUP, |
|
|
62 | }; |
67 | }; |
63 | |
68 | |
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
69 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
70 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
66 | |
71 | |
67 | typedef struct aio_cb |
72 | typedef struct aio_cb |
68 | { |
73 | { |
69 | struct aio_cb *grp, *grp_prev, *grp_next; |
|
|
70 | |
|
|
71 | struct aio_cb *volatile next; |
74 | struct aio_cb *volatile next; |
72 | |
|
|
73 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
74 | |
75 | |
75 | SV *data, *callback; |
76 | SV *data, *callback; |
76 | SV *fh, *fh2; |
77 | SV *fh, *fh2; |
77 | void *dataptr, *data2ptr; |
78 | void *dataptr, *data2ptr; |
78 | Stat_t *statdata; |
79 | Stat_t *statdata; |
79 | off_t offset; |
80 | off_t offset; |
80 | size_t length; |
81 | size_t length; |
81 | ssize_t result; |
82 | ssize_t result; |
82 | |
83 | |
|
|
84 | STRLEN dataoffset; |
83 | int type; |
85 | int type; |
84 | int fd, fd2; |
86 | int fd, fd2; |
85 | int errorno; |
87 | int errorno; |
86 | STRLEN dataoffset; |
|
|
87 | mode_t mode; /* open */ |
88 | mode_t mode; /* open */ |
|
|
89 | |
88 | unsigned char cancelled; |
90 | unsigned char flags; |
|
|
91 | unsigned char pri; |
|
|
92 | |
|
|
93 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
94 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
89 | } aio_cb; |
95 | } aio_cb; |
|
|
96 | |
|
|
97 | enum { |
|
|
98 | FLAG_CANCELLED = 0x01, |
|
|
99 | }; |
90 | |
100 | |
91 | typedef aio_cb *aio_req; |
101 | typedef aio_cb *aio_req; |
92 | typedef aio_cb *aio_req_ornot; |
102 | typedef aio_cb *aio_req_ornot; |
|
|
103 | |
|
|
104 | enum { |
|
|
105 | PRI_MIN = -4, |
|
|
106 | PRI_MAX = 4, |
|
|
107 | |
|
|
108 | DEFAULT_PRI = 0, |
|
|
109 | PRI_BIAS = -PRI_MIN, |
|
|
110 | }; |
|
|
111 | |
|
|
112 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
93 | |
113 | |
94 | static int started, wanted; |
114 | static int started, wanted; |
95 | static volatile int nreqs; |
115 | static volatile int nreqs; |
96 | static int max_outstanding = 1<<30; |
116 | static int max_outstanding = 1<<30; |
97 | static int respipe [2]; |
117 | static int respipe [2]; |
98 | |
118 | |
|
|
119 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
|
|
120 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
|
|
121 | #else |
|
|
122 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
123 | #endif |
|
|
124 | |
99 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
125 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
100 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
126 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
101 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
127 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
102 | |
128 | |
103 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
129 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
104 | static volatile aio_req ress, rese; /* queue start, queue end */ |
130 | static volatile aio_req ress, rese; /* queue start, queue end */ |
105 | |
131 | |
|
|
132 | static void req_invoke (aio_req req); |
106 | static void req_free (aio_req req); |
133 | static void req_free (aio_req req); |
107 | |
134 | |
108 | /* must be called at most once */ |
135 | /* must be called at most once */ |
109 | static SV *req_sv (aio_req req, const char *klass) |
136 | static SV *req_sv (aio_req req, const char *klass) |
110 | { |
137 | { |
|
|
138 | if (!req->self) |
|
|
139 | { |
111 | req->self = (SV *)newHV (); |
140 | req->self = (SV *)newHV (); |
112 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
141 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
142 | } |
113 | |
143 | |
114 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
144 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
115 | } |
145 | } |
116 | |
146 | |
117 | static aio_req SvAIO_REQ (SV *sv) |
147 | static aio_req SvAIO_REQ (SV *sv) |
118 | { |
148 | { |
|
|
149 | MAGIC *mg; |
|
|
150 | |
119 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
151 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
120 | croak ("object of class " AIO_REQ_KLASS " expected"); |
152 | croak ("object of class " AIO_REQ_KLASS " expected"); |
121 | |
153 | |
122 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
154 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
123 | |
155 | |
124 | return mg ? (aio_req)mg->mg_ptr : 0; |
156 | return mg ? (aio_req)mg->mg_ptr : 0; |
125 | } |
157 | } |
126 | |
158 | |
|
|
159 | static void aio_grp_feed (aio_req grp) |
|
|
160 | { |
|
|
161 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
|
|
162 | { |
|
|
163 | int old_len = grp->length; |
|
|
164 | |
|
|
165 | if (grp->fh2 && SvOK (grp->fh2)) |
|
|
166 | { |
|
|
167 | dSP; |
|
|
168 | |
|
|
169 | ENTER; |
|
|
170 | SAVETMPS; |
|
|
171 | PUSHMARK (SP); |
|
|
172 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
|
|
173 | PUTBACK; |
|
|
174 | call_sv (grp->fh2, G_VOID | G_EVAL); |
|
|
175 | SPAGAIN; |
|
|
176 | FREETMPS; |
|
|
177 | LEAVE; |
|
|
178 | } |
|
|
179 | |
|
|
180 | /* stop if no progress has been made */ |
|
|
181 | if (old_len == grp->length) |
|
|
182 | { |
|
|
183 | SvREFCNT_dec (grp->fh2); |
|
|
184 | grp->fh2 = 0; |
|
|
185 | break; |
|
|
186 | } |
|
|
187 | } |
|
|
188 | } |
|
|
189 | |
|
|
190 | static void aio_grp_dec (aio_req grp) |
|
|
191 | { |
|
|
192 | --grp->length; |
|
|
193 | |
|
|
194 | /* call feeder, if applicable */ |
|
|
195 | aio_grp_feed (grp); |
|
|
196 | |
|
|
197 | /* finish, if done */ |
|
|
198 | if (!grp->length && grp->fd) |
|
|
199 | { |
|
|
200 | req_invoke (grp); |
|
|
201 | req_free (grp); |
|
|
202 | } |
|
|
203 | } |
|
|
204 | |
127 | static void poll_wait () |
205 | static void poll_wait () |
128 | { |
206 | { |
129 | if (nreqs && !ress) |
|
|
130 | { |
|
|
131 | fd_set rfd; |
207 | fd_set rfd; |
|
|
208 | |
|
|
209 | while (nreqs) |
|
|
210 | { |
|
|
211 | aio_req req; |
|
|
212 | #if !(__x86 || __x86_64) /* safe without sempahore on this archs */ |
|
|
213 | pthread_mutex_lock (&reslock); |
|
|
214 | #endif |
|
|
215 | req = ress; |
|
|
216 | #if !(__x86 || __x86_64) /* safe without sempahore on this archs */ |
|
|
217 | pthread_mutex_unlock (&reslock); |
|
|
218 | #endif |
|
|
219 | |
|
|
220 | if (req) |
|
|
221 | return; |
|
|
222 | |
132 | FD_ZERO(&rfd); |
223 | FD_ZERO(&rfd); |
133 | FD_SET(respipe [0], &rfd); |
224 | FD_SET(respipe [0], &rfd); |
134 | |
225 | |
135 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
226 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
136 | } |
227 | } |
… | |
… | |
139 | static void req_invoke (aio_req req) |
230 | static void req_invoke (aio_req req) |
140 | { |
231 | { |
141 | dSP; |
232 | dSP; |
142 | int errorno = errno; |
233 | int errorno = errno; |
143 | |
234 | |
144 | if (req->cancelled || !SvOK (req->callback)) |
235 | if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) |
145 | return; |
236 | return; |
146 | |
237 | |
147 | errno = req->errorno; |
238 | errno = req->errorno; |
148 | |
239 | |
149 | ENTER; |
240 | ENTER; |
|
|
241 | SAVETMPS; |
150 | PUSHMARK (SP); |
242 | PUSHMARK (SP); |
151 | EXTEND (SP, 1); |
243 | EXTEND (SP, 1); |
152 | |
244 | |
153 | switch (req->type) |
245 | switch (req->type) |
154 | { |
246 | { |
… | |
… | |
193 | XPUSHs (sv_2mortal (fh)); |
285 | XPUSHs (sv_2mortal (fh)); |
194 | } |
286 | } |
195 | break; |
287 | break; |
196 | |
288 | |
197 | case REQ_GROUP: |
289 | case REQ_GROUP: |
|
|
290 | req->fd = 2; /* mark group as finished */ |
|
|
291 | |
198 | if (req->data) |
292 | if (req->data) |
199 | { |
293 | { |
200 | int i; |
294 | int i; |
201 | AV *av = (AV *)req->data; |
295 | AV *av = (AV *)req->data; |
202 | |
296 | |
… | |
… | |
204 | for (i = 0; i <= AvFILL (av); ++i) |
298 | for (i = 0; i <= AvFILL (av); ++i) |
205 | PUSHs (*av_fetch (av, i, 0)); |
299 | PUSHs (*av_fetch (av, i, 0)); |
206 | } |
300 | } |
207 | break; |
301 | break; |
208 | |
302 | |
|
|
303 | case REQ_NOP: |
209 | case REQ_SLEEP: |
304 | case REQ_SLEEP: |
210 | break; |
305 | break; |
211 | |
306 | |
212 | default: |
307 | default: |
213 | PUSHs (sv_2mortal (newSViv (req->result))); |
308 | PUSHs (sv_2mortal (newSViv (req->result))); |
… | |
… | |
217 | |
312 | |
218 | PUTBACK; |
313 | PUTBACK; |
219 | call_sv (req->callback, G_VOID | G_EVAL); |
314 | call_sv (req->callback, G_VOID | G_EVAL); |
220 | SPAGAIN; |
315 | SPAGAIN; |
221 | |
316 | |
|
|
317 | FREETMPS; |
|
|
318 | LEAVE; |
|
|
319 | |
|
|
320 | errno = errorno; |
|
|
321 | |
222 | if (SvTRUE (ERRSV)) |
322 | if (SvTRUE (ERRSV)) |
223 | { |
323 | { |
224 | req_free (req); |
324 | req_free (req); |
225 | croak (0); |
325 | croak (0); |
226 | } |
326 | } |
227 | |
|
|
228 | LEAVE; |
|
|
229 | |
|
|
230 | errno = errorno; |
|
|
231 | } |
327 | } |
232 | |
328 | |
233 | static void req_free (aio_req req) |
329 | static void req_free (aio_req req) |
234 | { |
330 | { |
235 | if (req->grp) |
331 | if (req->grp) |
236 | { |
332 | { |
237 | aio_req grp = req->grp; |
333 | aio_req grp = req->grp; |
238 | |
334 | |
239 | /* unlink request */ |
335 | /* unlink request */ |
240 | req->grp_next->grp_prev = req->grp_prev; |
336 | if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; |
241 | req->grp_prev->grp_next = req->grp_next; |
337 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
242 | |
338 | |
243 | if (grp->grp_next == grp && grp->fd) |
339 | if (grp->grp_first == req) |
244 | { |
340 | grp->grp_first = req->grp_next; |
245 | req_invoke (grp); |
341 | |
246 | req_free (grp); |
342 | aio_grp_dec (grp); |
247 | } |
|
|
248 | } |
343 | } |
249 | |
344 | |
250 | if (req->self) |
345 | if (req->self) |
251 | { |
346 | { |
252 | sv_unmagic (req->self, PERL_MAGIC_ext); |
347 | sv_unmagic (req->self, PERL_MAGIC_ext); |
253 | SvREFCNT_dec (req->self); |
348 | SvREFCNT_dec (req->self); |
254 | } |
349 | } |
255 | |
350 | |
256 | if (req->data) |
|
|
257 | SvREFCNT_dec (req->data); |
351 | SvREFCNT_dec (req->data); |
258 | |
|
|
259 | if (req->fh) |
|
|
260 | SvREFCNT_dec (req->fh); |
352 | SvREFCNT_dec (req->fh); |
261 | |
|
|
262 | if (req->fh2) |
|
|
263 | SvREFCNT_dec (req->fh2); |
353 | SvREFCNT_dec (req->fh2); |
264 | |
|
|
265 | if (req->statdata) |
|
|
266 | Safefree (req->statdata); |
|
|
267 | |
|
|
268 | if (req->callback) |
|
|
269 | SvREFCNT_dec (req->callback); |
354 | SvREFCNT_dec (req->callback); |
|
|
355 | Safefree (req->statdata); |
270 | |
356 | |
271 | if (req->type == REQ_READDIR && req->result >= 0) |
357 | if (req->type == REQ_READDIR && req->result >= 0) |
272 | free (req->data2ptr); |
358 | free (req->data2ptr); |
273 | |
359 | |
274 | Safefree (req); |
360 | Safefree (req); |
275 | } |
361 | } |
276 | |
362 | |
277 | static void req_cancel (aio_req req) |
363 | static void req_cancel (aio_req req) |
278 | { |
364 | { |
279 | req->cancelled = 1; |
365 | req->flags |= FLAG_CANCELLED; |
280 | |
366 | |
281 | if (req->type == REQ_GROUP) |
367 | if (req->type == REQ_GROUP) |
282 | { |
368 | { |
283 | aio_req sub; |
369 | aio_req sub; |
284 | |
370 | |
285 | for (sub = req->grp_next; sub != req; sub = sub->grp_next) |
371 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
286 | req_cancel (sub); |
372 | req_cancel (sub); |
287 | } |
373 | } |
288 | } |
374 | } |
289 | |
375 | |
290 | static int poll_cb () |
376 | static int poll_cb () |
… | |
… | |
317 | pthread_mutex_unlock (&reslock); |
403 | pthread_mutex_unlock (&reslock); |
318 | |
404 | |
319 | if (!req) |
405 | if (!req) |
320 | break; |
406 | break; |
321 | |
407 | |
322 | nreqs--; |
408 | --nreqs; |
323 | |
409 | |
324 | if (req->type == REQ_QUIT) |
410 | if (req->type == REQ_QUIT) |
325 | started--; |
411 | started--; |
326 | else if (req->type == REQ_GROUP && req->grp_next != req) |
412 | else if (req->type == REQ_GROUP && req->length) |
327 | { |
413 | { |
328 | req->fd = 1; /* mark request as delayed */ |
414 | req->fd = 1; /* mark request as delayed */ |
329 | continue; |
415 | continue; |
330 | } |
416 | } |
331 | else |
417 | else |
… | |
… | |
378 | static void req_send (aio_req req) |
464 | static void req_send (aio_req req) |
379 | { |
465 | { |
380 | while (started < wanted && nreqs >= started) |
466 | while (started < wanted && nreqs >= started) |
381 | start_thread (); |
467 | start_thread (); |
382 | |
468 | |
383 | nreqs++; |
469 | ++nreqs; |
384 | |
470 | |
385 | pthread_mutex_lock (&reqlock); |
471 | pthread_mutex_lock (&reqlock); |
386 | |
472 | |
387 | req->next = 0; |
473 | req->next = 0; |
388 | |
474 | |
… | |
… | |
738 | } |
824 | } |
739 | |
825 | |
740 | pthread_mutex_unlock (&reqlock); |
826 | pthread_mutex_unlock (&reqlock); |
741 | |
827 | |
742 | errno = 0; /* strictly unnecessary */ |
828 | errno = 0; /* strictly unnecessary */ |
|
|
829 | type = req->type; /* remember type for QUIT check */ |
743 | |
830 | |
744 | if (!req->cancelled) |
831 | if (!(req->flags & FLAG_CANCELLED)) |
745 | switch (req->type) |
832 | switch (type) |
746 | { |
833 | { |
747 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
834 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
748 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
835 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
749 | |
836 | |
750 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
837 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
… | |
… | |
774 | tv.tv_usec = req->fd2; |
861 | tv.tv_usec = req->fd2; |
775 | |
862 | |
776 | req->result = select (0, 0, 0, 0, &tv); |
863 | req->result = select (0, 0, 0, 0, &tv); |
777 | } |
864 | } |
778 | |
865 | |
|
|
866 | case REQ_GROUP: |
|
|
867 | case REQ_NOP: |
779 | case REQ_QUIT: |
868 | case REQ_QUIT: |
780 | break; |
869 | break; |
781 | |
870 | |
782 | default: |
871 | default: |
783 | req->result = ENOSYS; |
872 | req->result = ENOSYS; |
… | |
… | |
867 | atfork_parent (); |
956 | atfork_parent (); |
868 | } |
957 | } |
869 | |
958 | |
870 | #define dREQ \ |
959 | #define dREQ \ |
871 | aio_req req; \ |
960 | aio_req req; \ |
|
|
961 | int req_pri = next_pri; \ |
|
|
962 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
872 | \ |
963 | \ |
873 | if (SvOK (callback) && !SvROK (callback)) \ |
964 | if (SvOK (callback) && !SvROK (callback)) \ |
874 | croak ("callback must be undef or of reference type"); \ |
965 | croak ("callback must be undef or of reference type"); \ |
875 | \ |
966 | \ |
876 | Newz (0, req, 1, aio_cb); \ |
967 | Newz (0, req, 1, aio_cb); \ |
877 | if (!req) \ |
968 | if (!req) \ |
878 | croak ("out of memory during aio_req allocation"); \ |
969 | croak ("out of memory during aio_req allocation"); \ |
879 | \ |
970 | \ |
880 | req->callback = newSVsv (callback) |
971 | req->callback = newSVsv (callback); \ |
|
|
972 | req->pri = req_pri |
881 | |
973 | |
882 | #define REQ_SEND \ |
974 | #define REQ_SEND \ |
883 | req_send (req); \ |
975 | req_send (req); \ |
884 | \ |
976 | \ |
885 | if (GIMME_V != G_VOID) \ |
977 | if (GIMME_V != G_VOID) \ |
… | |
… | |
1160 | SV * callback |
1252 | SV * callback |
1161 | PROTOTYPE: ;$ |
1253 | PROTOTYPE: ;$ |
1162 | PPCODE: |
1254 | PPCODE: |
1163 | { |
1255 | { |
1164 | dREQ; |
1256 | dREQ; |
|
|
1257 | |
1165 | req->type = REQ_GROUP; |
1258 | req->type = REQ_GROUP; |
1166 | req->grp_next = req; |
|
|
1167 | req->grp_prev = req; |
|
|
1168 | |
|
|
1169 | req_send (req); |
1259 | req_send (req); |
|
|
1260 | |
1170 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1261 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1171 | } |
1262 | } |
|
|
1263 | |
|
|
1264 | void |
|
|
1265 | aio_nop (callback=&PL_sv_undef) |
|
|
1266 | SV * callback |
|
|
1267 | PPCODE: |
|
|
1268 | { |
|
|
1269 | dREQ; |
|
|
1270 | |
|
|
1271 | req->type = REQ_NOP; |
|
|
1272 | |
|
|
1273 | REQ_SEND; |
|
|
1274 | } |
|
|
1275 | |
|
|
1276 | #if 0 |
|
|
1277 | |
|
|
1278 | void |
|
|
1279 | aio_pri (int pri = DEFAULT_PRI) |
|
|
1280 | CODE: |
|
|
1281 | if (pri < PRI_MIN) pri = PRI_MIN; |
|
|
1282 | if (pri > PRI_MAX) pri = PRI_MAX; |
|
|
1283 | next_pri = pri + PRI_BIAS; |
|
|
1284 | |
|
|
1285 | #endif |
1172 | |
1286 | |
1173 | void |
1287 | void |
1174 | flush () |
1288 | flush () |
1175 | PROTOTYPE: |
1289 | PROTOTYPE: |
1176 | CODE: |
1290 | CODE: |
… | |
… | |
1229 | cancel (aio_req_ornot req) |
1343 | cancel (aio_req_ornot req) |
1230 | PROTOTYPE: |
1344 | PROTOTYPE: |
1231 | CODE: |
1345 | CODE: |
1232 | req_cancel (req); |
1346 | req_cancel (req); |
1233 | |
1347 | |
|
|
1348 | void |
|
|
1349 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
|
|
1350 | CODE: |
|
|
1351 | SvREFCNT_dec (req->callback); |
|
|
1352 | req->callback = newSVsv (callback); |
|
|
1353 | |
1234 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1354 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1235 | |
1355 | |
1236 | void |
1356 | void |
1237 | add (aio_req grp, ...) |
1357 | add (aio_req grp, ...) |
1238 | PPCODE: |
1358 | PPCODE: |
1239 | { |
1359 | { |
1240 | int i; |
1360 | int i; |
|
|
1361 | aio_req req; |
|
|
1362 | |
|
|
1363 | if (grp->fd == 2) |
|
|
1364 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1241 | |
1365 | |
1242 | for (i = 1; i < items; ++i ) |
1366 | for (i = 1; i < items; ++i ) |
1243 | { |
1367 | { |
1244 | if (GIMME_V != G_VOID) |
1368 | if (GIMME_V != G_VOID) |
1245 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1369 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1246 | |
1370 | |
1247 | aio_req req = SvAIO_REQ (ST (i)); |
1371 | req = SvAIO_REQ (ST (i)); |
1248 | |
1372 | |
1249 | if (req) |
1373 | if (req) |
1250 | { |
1374 | { |
1251 | req->grp_prev = grp; |
1375 | ++grp->length; |
1252 | req->grp_next = grp->grp_next; |
|
|
1253 | grp->grp_next->grp_prev = req; |
|
|
1254 | grp->grp_next = req; |
|
|
1255 | |
|
|
1256 | req->grp = grp; |
1376 | req->grp = grp; |
|
|
1377 | |
|
|
1378 | req->grp_prev = 0; |
|
|
1379 | req->grp_next = grp->grp_first; |
|
|
1380 | |
|
|
1381 | if (grp->grp_first) |
|
|
1382 | grp->grp_first->grp_prev = req; |
|
|
1383 | |
|
|
1384 | grp->grp_first = req; |
1257 | } |
1385 | } |
1258 | } |
1386 | } |
1259 | } |
1387 | } |
1260 | |
1388 | |
1261 | void |
1389 | void |
1262 | result (aio_req grp, ...) |
1390 | result (aio_req grp, ...) |
|
|
1391 | CODE: |
|
|
1392 | { |
|
|
1393 | int i; |
|
|
1394 | AV *av = newAV (); |
|
|
1395 | |
|
|
1396 | for (i = 1; i < items; ++i ) |
|
|
1397 | av_push (av, newSVsv (ST (i))); |
|
|
1398 | |
|
|
1399 | SvREFCNT_dec (grp->data); |
|
|
1400 | grp->data = (SV *)av; |
|
|
1401 | } |
|
|
1402 | |
|
|
1403 | void |
|
|
1404 | feed_limit (aio_req grp, int limit) |
1263 | CODE: |
1405 | CODE: |
1264 | { |
1406 | grp->fd2 = limit; |
1265 | int i; |
1407 | aio_grp_feed (grp); |
1266 | AV *av = newAV (); |
|
|
1267 | |
1408 | |
1268 | if (grp->data) |
1409 | void |
|
|
1410 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
|
|
1411 | CODE: |
|
|
1412 | { |
1269 | SvREFCNT_dec (grp->data); |
1413 | SvREFCNT_dec (grp->fh2); |
|
|
1414 | grp->fh2 = newSVsv (callback); |
1270 | |
1415 | |
1271 | for (i = 1; i < items; ++i ) |
1416 | if (grp->fd2 <= 0) |
1272 | av_push (av, newSVsv (ST (i))); |
1417 | grp->fd2 = 2; |
1273 | |
1418 | |
1274 | grp->data = (SV *)av; |
1419 | aio_grp_feed (grp); |
1275 | } |
1420 | } |
1276 | |
1421 | |