… | |
… | |
55 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
55 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
56 | REQ_FSYNC, REQ_FDATASYNC, |
56 | REQ_FSYNC, REQ_FDATASYNC, |
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
57 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
58 | REQ_READDIR, |
58 | REQ_READDIR, |
59 | REQ_LINK, REQ_SYMLINK, |
59 | REQ_LINK, REQ_SYMLINK, |
|
|
60 | REQ_GROUP, REQ_NOP, |
60 | REQ_SLEEP, |
61 | REQ_SLEEP, |
61 | REQ_GROUP, |
|
|
62 | }; |
62 | }; |
63 | |
63 | |
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
64 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
65 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
66 | |
66 | |
… | |
… | |
83 | int type; |
83 | int type; |
84 | int fd, fd2; |
84 | int fd, fd2; |
85 | int errorno; |
85 | int errorno; |
86 | STRLEN dataoffset; |
86 | STRLEN dataoffset; |
87 | mode_t mode; /* open */ |
87 | mode_t mode; /* open */ |
|
|
88 | unsigned char pri; |
88 | unsigned char cancelled; |
89 | unsigned char flags; |
89 | } aio_cb; |
90 | } aio_cb; |
|
|
91 | |
|
|
92 | enum { |
|
|
93 | FLAG_CANCELLED = 0x01, |
|
|
94 | }; |
90 | |
95 | |
91 | typedef aio_cb *aio_req; |
96 | typedef aio_cb *aio_req; |
92 | typedef aio_cb *aio_req_ornot; |
97 | typedef aio_cb *aio_req_ornot; |
93 | |
98 | |
94 | static int started, wanted; |
99 | static int started, wanted; |
… | |
… | |
101 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
106 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
102 | |
107 | |
103 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
108 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
104 | static volatile aio_req ress, rese; /* queue start, queue end */ |
109 | static volatile aio_req ress, rese; /* queue start, queue end */ |
105 | |
110 | |
|
|
111 | static void req_invoke (aio_req req); |
106 | static void req_free (aio_req req); |
112 | static void req_free (aio_req req); |
107 | |
113 | |
108 | /* must be called at most once */ |
114 | /* must be called at most once */ |
109 | static SV *req_sv (aio_req req, const char *klass) |
115 | static SV *req_sv (aio_req req, const char *klass) |
110 | { |
116 | { |
… | |
… | |
117 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
123 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
118 | } |
124 | } |
119 | |
125 | |
120 | static aio_req SvAIO_REQ (SV *sv) |
126 | static aio_req SvAIO_REQ (SV *sv) |
121 | { |
127 | { |
|
|
128 | MAGIC *mg; |
|
|
129 | |
122 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
130 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
123 | croak ("object of class " AIO_REQ_KLASS " expected"); |
131 | croak ("object of class " AIO_REQ_KLASS " expected"); |
124 | |
132 | |
125 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
133 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
126 | |
134 | |
127 | return mg ? (aio_req)mg->mg_ptr : 0; |
135 | return mg ? (aio_req)mg->mg_ptr : 0; |
128 | } |
136 | } |
129 | |
137 | |
130 | static void aio_grp_feed (aio_req grp) |
138 | static void aio_grp_feed (aio_req grp) |
131 | { |
139 | { |
132 | while (grp->length < grp->fd2) |
140 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
133 | { |
141 | { |
134 | int old_len = grp->length; |
142 | int old_len = grp->length; |
135 | |
143 | |
136 | if (grp->fh2 && SvOK (grp->fh2)) |
144 | if (grp->fh2 && SvOK (grp->fh2)) |
137 | { |
145 | { |
… | |
… | |
156 | break; |
164 | break; |
157 | } |
165 | } |
158 | } |
166 | } |
159 | } |
167 | } |
160 | |
168 | |
|
|
169 | static void aio_grp_dec (aio_req grp) |
|
|
170 | { |
|
|
171 | --grp->length; |
|
|
172 | |
|
|
173 | /* call feeder, if applicable */ |
|
|
174 | aio_grp_feed (grp); |
|
|
175 | |
|
|
176 | /* finish, if done */ |
|
|
177 | if (!grp->length && grp->fd) |
|
|
178 | { |
|
|
179 | req_invoke (grp); |
|
|
180 | req_free (grp); |
|
|
181 | } |
|
|
182 | } |
|
|
183 | |
161 | static void poll_wait () |
184 | static void poll_wait () |
162 | { |
185 | { |
163 | if (nreqs && !ress) |
186 | if (nreqs && !ress) |
164 | { |
187 | { |
165 | fd_set rfd; |
188 | fd_set rfd; |
… | |
… | |
173 | static void req_invoke (aio_req req) |
196 | static void req_invoke (aio_req req) |
174 | { |
197 | { |
175 | dSP; |
198 | dSP; |
176 | int errorno = errno; |
199 | int errorno = errno; |
177 | |
200 | |
178 | if (req->cancelled || !SvOK (req->callback)) |
201 | if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) |
179 | return; |
202 | return; |
180 | |
203 | |
181 | errno = req->errorno; |
204 | errno = req->errorno; |
182 | |
205 | |
183 | ENTER; |
206 | ENTER; |
… | |
… | |
241 | for (i = 0; i <= AvFILL (av); ++i) |
264 | for (i = 0; i <= AvFILL (av); ++i) |
242 | PUSHs (*av_fetch (av, i, 0)); |
265 | PUSHs (*av_fetch (av, i, 0)); |
243 | } |
266 | } |
244 | break; |
267 | break; |
245 | |
268 | |
|
|
269 | case REQ_NOP: |
246 | case REQ_SLEEP: |
270 | case REQ_SLEEP: |
247 | break; |
271 | break; |
248 | |
272 | |
249 | default: |
273 | default: |
250 | PUSHs (sv_2mortal (newSViv (req->result))); |
274 | PUSHs (sv_2mortal (newSViv (req->result))); |
… | |
… | |
254 | |
278 | |
255 | PUTBACK; |
279 | PUTBACK; |
256 | call_sv (req->callback, G_VOID | G_EVAL); |
280 | call_sv (req->callback, G_VOID | G_EVAL); |
257 | SPAGAIN; |
281 | SPAGAIN; |
258 | |
282 | |
|
|
283 | FREETMPS; |
|
|
284 | LEAVE; |
|
|
285 | |
|
|
286 | errno = errorno; |
|
|
287 | |
259 | if (SvTRUE (ERRSV)) |
288 | if (SvTRUE (ERRSV)) |
260 | { |
289 | { |
261 | req_free (req); |
290 | req_free (req); |
262 | croak (0); |
291 | croak (0); |
263 | } |
292 | } |
264 | |
|
|
265 | FREETMPS; |
|
|
266 | LEAVE; |
|
|
267 | |
|
|
268 | errno = errorno; |
|
|
269 | } |
293 | } |
270 | |
294 | |
271 | static void req_free (aio_req req) |
295 | static void req_free (aio_req req) |
272 | { |
296 | { |
273 | if (req->grp) |
297 | if (req->grp) |
… | |
… | |
279 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
303 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
280 | |
304 | |
281 | if (grp->grp_first == req) |
305 | if (grp->grp_first == req) |
282 | grp->grp_first = req->grp_next; |
306 | grp->grp_first = req->grp_next; |
283 | |
307 | |
284 | --grp->length; |
|
|
285 | |
|
|
286 | /* call feeder, if applicable */ |
|
|
287 | aio_grp_feed (grp); |
308 | aio_grp_dec (grp); |
288 | |
|
|
289 | /* finish, if done */ |
|
|
290 | if (!grp->length && grp->fd) |
|
|
291 | { |
|
|
292 | req_invoke (grp); |
|
|
293 | req_free (grp); |
|
|
294 | } |
|
|
295 | } |
309 | } |
296 | |
310 | |
297 | if (req->self) |
311 | if (req->self) |
298 | { |
312 | { |
299 | sv_unmagic (req->self, PERL_MAGIC_ext); |
313 | sv_unmagic (req->self, PERL_MAGIC_ext); |
… | |
… | |
312 | Safefree (req); |
326 | Safefree (req); |
313 | } |
327 | } |
314 | |
328 | |
315 | static void req_cancel (aio_req req) |
329 | static void req_cancel (aio_req req) |
316 | { |
330 | { |
317 | req->cancelled = 1; |
331 | req->flags |= FLAG_CANCELLED; |
318 | |
332 | |
319 | if (req->type == REQ_GROUP) |
333 | if (req->type == REQ_GROUP) |
320 | { |
334 | { |
321 | aio_req sub; |
335 | aio_req sub; |
322 | |
336 | |
… | |
… | |
355 | pthread_mutex_unlock (&reslock); |
369 | pthread_mutex_unlock (&reslock); |
356 | |
370 | |
357 | if (!req) |
371 | if (!req) |
358 | break; |
372 | break; |
359 | |
373 | |
360 | nreqs--; |
374 | --nreqs; |
361 | |
375 | |
362 | if (req->type == REQ_QUIT) |
376 | if (req->type == REQ_QUIT) |
363 | started--; |
377 | started--; |
364 | else if (req->type == REQ_GROUP && req->length) |
378 | else if (req->type == REQ_GROUP && req->length) |
365 | { |
379 | { |
… | |
… | |
416 | static void req_send (aio_req req) |
430 | static void req_send (aio_req req) |
417 | { |
431 | { |
418 | while (started < wanted && nreqs >= started) |
432 | while (started < wanted && nreqs >= started) |
419 | start_thread (); |
433 | start_thread (); |
420 | |
434 | |
421 | nreqs++; |
435 | ++nreqs; |
422 | |
436 | |
423 | pthread_mutex_lock (&reqlock); |
437 | pthread_mutex_lock (&reqlock); |
424 | |
438 | |
425 | req->next = 0; |
439 | req->next = 0; |
426 | |
440 | |
… | |
… | |
777 | |
791 | |
778 | pthread_mutex_unlock (&reqlock); |
792 | pthread_mutex_unlock (&reqlock); |
779 | |
793 | |
780 | errno = 0; /* strictly unnecessary */ |
794 | errno = 0; /* strictly unnecessary */ |
781 | |
795 | |
782 | if (!req->cancelled) |
796 | if (!(req->flags & FLAG_CANCELLED)) |
783 | switch (req->type) |
797 | switch (type = req->type) /* remember type for QUIT check */ |
784 | { |
798 | { |
785 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
799 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
786 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
800 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
787 | |
801 | |
788 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
802 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
… | |
… | |
812 | tv.tv_usec = req->fd2; |
826 | tv.tv_usec = req->fd2; |
813 | |
827 | |
814 | req->result = select (0, 0, 0, 0, &tv); |
828 | req->result = select (0, 0, 0, 0, &tv); |
815 | } |
829 | } |
816 | |
830 | |
|
|
831 | case REQ_GROUP: |
|
|
832 | case REQ_NOP: |
817 | case REQ_QUIT: |
833 | case REQ_QUIT: |
818 | break; |
834 | break; |
819 | |
835 | |
820 | default: |
836 | default: |
821 | req->result = ENOSYS; |
837 | req->result = ENOSYS; |
… | |
… | |
1204 | req_send (req); |
1220 | req_send (req); |
1205 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1221 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1206 | } |
1222 | } |
1207 | |
1223 | |
1208 | void |
1224 | void |
|
|
1225 | aio_nop (callback=&PL_sv_undef) |
|
|
1226 | SV * callback |
|
|
1227 | PPCODE: |
|
|
1228 | { |
|
|
1229 | dREQ; |
|
|
1230 | |
|
|
1231 | req->type = REQ_NOP; |
|
|
1232 | |
|
|
1233 | REQ_SEND; |
|
|
1234 | } |
|
|
1235 | |
|
|
1236 | void |
1209 | flush () |
1237 | flush () |
1210 | PROTOTYPE: |
1238 | PROTOTYPE: |
1211 | CODE: |
1239 | CODE: |
1212 | while (nreqs) |
1240 | while (nreqs) |
1213 | { |
1241 | { |
… | |
… | |
1264 | cancel (aio_req_ornot req) |
1292 | cancel (aio_req_ornot req) |
1265 | PROTOTYPE: |
1293 | PROTOTYPE: |
1266 | CODE: |
1294 | CODE: |
1267 | req_cancel (req); |
1295 | req_cancel (req); |
1268 | |
1296 | |
|
|
1297 | void |
|
|
1298 | cb (aio_req req, SV *callback=&PL_sv_undef) |
|
|
1299 | CODE: |
|
|
1300 | SvREFCNT_dec (req->callback); |
|
|
1301 | req->callback = newSVsv (callback); |
|
|
1302 | |
1269 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1303 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1270 | |
1304 | |
1271 | void |
1305 | void |
1272 | add (aio_req grp, ...) |
1306 | add (aio_req grp, ...) |
1273 | PPCODE: |
1307 | PPCODE: |
1274 | { |
1308 | { |
1275 | int i; |
1309 | int i; |
|
|
1310 | aio_req req; |
1276 | |
1311 | |
1277 | if (grp->fd == 2) |
1312 | if (grp->fd == 2) |
1278 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1313 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1279 | |
1314 | |
1280 | for (i = 1; i < items; ++i ) |
1315 | for (i = 1; i < items; ++i ) |
1281 | { |
1316 | { |
1282 | if (GIMME_V != G_VOID) |
1317 | if (GIMME_V != G_VOID) |
1283 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1318 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1284 | |
1319 | |
1285 | aio_req req = SvAIO_REQ (ST (i)); |
1320 | req = SvAIO_REQ (ST (i)); |
1286 | |
1321 | |
1287 | if (req) |
1322 | if (req) |
1288 | { |
1323 | { |
1289 | ++grp->length; |
1324 | ++grp->length; |
1290 | req->grp = grp; |
1325 | req->grp = grp; |
… | |
… | |
1300 | } |
1335 | } |
1301 | } |
1336 | } |
1302 | |
1337 | |
1303 | void |
1338 | void |
1304 | result (aio_req grp, ...) |
1339 | result (aio_req grp, ...) |
1305 | CODE: |
1340 | CODE: |
1306 | { |
1341 | { |
1307 | int i; |
1342 | int i; |
1308 | AV *av = newAV (); |
1343 | AV *av = newAV (); |
1309 | |
1344 | |
1310 | for (i = 1; i < items; ++i ) |
1345 | for (i = 1; i < items; ++i ) |
1311 | av_push (av, newSVsv (ST (i))); |
1346 | av_push (av, newSVsv (ST (i))); |
1312 | |
1347 | |
1313 | SvREFCNT_dec (grp->data); |
1348 | SvREFCNT_dec (grp->data); |
1314 | grp->data = (SV *)av; |
1349 | grp->data = (SV *)av; |
1315 | } |
1350 | } |
1316 | |
1351 | |
1317 | void |
1352 | void |
1318 | feeder_limit (aio_req grp, int limit) |
1353 | feed_limit (aio_req grp, int limit) |
1319 | CODE: |
1354 | CODE: |
1320 | grp->fd2 = limit; |
1355 | grp->fd2 = limit; |
1321 | aio_grp_feed (grp); |
1356 | aio_grp_feed (grp); |
1322 | |
1357 | |
1323 | void |
1358 | void |
1324 | set_feeder (aio_req grp, SV *callback=&PL_sv_undef) |
1359 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1325 | CODE: |
1360 | CODE: |
1326 | { |
1361 | { |
1327 | SvREFCNT_dec (grp->fh2); |
1362 | SvREFCNT_dec (grp->fh2); |
1328 | grp->fh2 = newSVsv (callback); |
1363 | grp->fh2 = newSVsv (callback); |
1329 | |
1364 | |