… | |
… | |
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 | |
67 | typedef struct aio_cb |
67 | typedef struct aio_cb |
68 | { |
68 | { |
69 | struct aio_cb *volatile next; |
69 | struct aio_cb *volatile next; |
70 | |
|
|
71 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
|
|
72 | |
|
|
73 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
74 | |
70 | |
75 | SV *data, *callback; |
71 | SV *data, *callback; |
76 | SV *fh, *fh2; |
72 | SV *fh, *fh2; |
77 | void *dataptr, *data2ptr; |
73 | void *dataptr, *data2ptr; |
78 | Stat_t *statdata; |
74 | Stat_t *statdata; |
79 | off_t offset; |
75 | off_t offset; |
80 | size_t length; |
76 | size_t length; |
81 | ssize_t result; |
77 | ssize_t result; |
82 | |
78 | |
|
|
79 | STRLEN dataoffset; |
83 | int type; |
80 | int type; |
84 | int fd, fd2; |
81 | int fd, fd2; |
85 | int errorno; |
82 | int errorno; |
86 | STRLEN dataoffset; |
|
|
87 | mode_t mode; /* open */ |
83 | mode_t mode; /* open */ |
|
|
84 | |
88 | unsigned char cancelled; |
85 | unsigned char flags; |
|
|
86 | unsigned char pri; |
|
|
87 | |
|
|
88 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
89 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
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; |
|
|
98 | |
|
|
99 | enum { |
|
|
100 | PRI_MIN = -4, |
|
|
101 | PRI_MAX = 4, |
|
|
102 | |
|
|
103 | DEFAULT_PRI = 0, |
|
|
104 | PRI_BIAS = -PRI_MIN, |
|
|
105 | }; |
|
|
106 | |
|
|
107 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
93 | |
108 | |
94 | static int started, wanted; |
109 | static int started, wanted; |
95 | static volatile int nreqs; |
110 | static volatile int nreqs; |
96 | static int max_outstanding = 1<<30; |
111 | static int max_outstanding = 1<<30; |
97 | static int respipe [2]; |
112 | static int respipe [2]; |
… | |
… | |
101 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
116 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
102 | |
117 | |
103 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
118 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
104 | static volatile aio_req ress, rese; /* queue start, queue end */ |
119 | static volatile aio_req ress, rese; /* queue start, queue end */ |
105 | |
120 | |
|
|
121 | static void req_invoke (aio_req req); |
106 | static void req_free (aio_req req); |
122 | static void req_free (aio_req req); |
107 | |
123 | |
108 | /* must be called at most once */ |
124 | /* must be called at most once */ |
109 | static SV *req_sv (aio_req req, const char *klass) |
125 | static SV *req_sv (aio_req req, const char *klass) |
110 | { |
126 | { |
… | |
… | |
117 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
133 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
118 | } |
134 | } |
119 | |
135 | |
120 | static aio_req SvAIO_REQ (SV *sv) |
136 | static aio_req SvAIO_REQ (SV *sv) |
121 | { |
137 | { |
|
|
138 | MAGIC *mg; |
|
|
139 | |
122 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
140 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
123 | croak ("object of class " AIO_REQ_KLASS " expected"); |
141 | croak ("object of class " AIO_REQ_KLASS " expected"); |
124 | |
142 | |
125 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
143 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
126 | |
144 | |
127 | return mg ? (aio_req)mg->mg_ptr : 0; |
145 | return mg ? (aio_req)mg->mg_ptr : 0; |
128 | } |
146 | } |
129 | |
147 | |
130 | static void aio_grp_feed (aio_req grp) |
148 | static void aio_grp_feed (aio_req grp) |
131 | { |
149 | { |
132 | while (grp->length < grp->fd2) |
150 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
133 | { |
151 | { |
134 | int old_len = grp->length; |
152 | int old_len = grp->length; |
135 | |
153 | |
136 | if (grp->fh2 && SvOK (grp->fh2)) |
154 | if (grp->fh2 && SvOK (grp->fh2)) |
137 | { |
155 | { |
… | |
… | |
156 | break; |
174 | break; |
157 | } |
175 | } |
158 | } |
176 | } |
159 | } |
177 | } |
160 | |
178 | |
|
|
179 | static void aio_grp_dec (aio_req grp) |
|
|
180 | { |
|
|
181 | --grp->length; |
|
|
182 | |
|
|
183 | /* call feeder, if applicable */ |
|
|
184 | aio_grp_feed (grp); |
|
|
185 | |
|
|
186 | /* finish, if done */ |
|
|
187 | if (!grp->length && grp->fd) |
|
|
188 | { |
|
|
189 | req_invoke (grp); |
|
|
190 | req_free (grp); |
|
|
191 | } |
|
|
192 | } |
|
|
193 | |
161 | static void poll_wait () |
194 | static void poll_wait () |
162 | { |
195 | { |
163 | if (nreqs && !ress) |
|
|
164 | { |
|
|
165 | fd_set rfd; |
196 | fd_set rfd; |
|
|
197 | |
|
|
198 | while (nreqs) |
|
|
199 | { |
|
|
200 | aio_req req; |
|
|
201 | pthread_mutex_lock (&reslock); |
|
|
202 | req = ress; |
|
|
203 | pthread_mutex_unlock (&reslock); |
|
|
204 | |
|
|
205 | if (req) |
|
|
206 | return; |
|
|
207 | |
166 | FD_ZERO(&rfd); |
208 | FD_ZERO(&rfd); |
167 | FD_SET(respipe [0], &rfd); |
209 | FD_SET(respipe [0], &rfd); |
168 | |
210 | |
169 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
211 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
170 | } |
212 | } |
… | |
… | |
173 | static void req_invoke (aio_req req) |
215 | static void req_invoke (aio_req req) |
174 | { |
216 | { |
175 | dSP; |
217 | dSP; |
176 | int errorno = errno; |
218 | int errorno = errno; |
177 | |
219 | |
178 | if (req->cancelled || !SvOK (req->callback)) |
220 | if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) |
179 | return; |
221 | return; |
180 | |
222 | |
181 | errno = req->errorno; |
223 | errno = req->errorno; |
182 | |
224 | |
183 | ENTER; |
225 | ENTER; |
… | |
… | |
241 | for (i = 0; i <= AvFILL (av); ++i) |
283 | for (i = 0; i <= AvFILL (av); ++i) |
242 | PUSHs (*av_fetch (av, i, 0)); |
284 | PUSHs (*av_fetch (av, i, 0)); |
243 | } |
285 | } |
244 | break; |
286 | break; |
245 | |
287 | |
|
|
288 | case REQ_NOP: |
246 | case REQ_SLEEP: |
289 | case REQ_SLEEP: |
247 | break; |
290 | break; |
248 | |
291 | |
249 | default: |
292 | default: |
250 | PUSHs (sv_2mortal (newSViv (req->result))); |
293 | PUSHs (sv_2mortal (newSViv (req->result))); |
… | |
… | |
254 | |
297 | |
255 | PUTBACK; |
298 | PUTBACK; |
256 | call_sv (req->callback, G_VOID | G_EVAL); |
299 | call_sv (req->callback, G_VOID | G_EVAL); |
257 | SPAGAIN; |
300 | SPAGAIN; |
258 | |
301 | |
|
|
302 | FREETMPS; |
|
|
303 | LEAVE; |
|
|
304 | |
|
|
305 | errno = errorno; |
|
|
306 | |
259 | if (SvTRUE (ERRSV)) |
307 | if (SvTRUE (ERRSV)) |
260 | { |
308 | { |
261 | req_free (req); |
309 | req_free (req); |
262 | croak (0); |
310 | croak (0); |
263 | } |
311 | } |
264 | |
|
|
265 | FREETMPS; |
|
|
266 | LEAVE; |
|
|
267 | |
|
|
268 | errno = errorno; |
|
|
269 | } |
312 | } |
270 | |
313 | |
271 | static void req_free (aio_req req) |
314 | static void req_free (aio_req req) |
272 | { |
315 | { |
273 | if (req->grp) |
316 | if (req->grp) |
… | |
… | |
279 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
322 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
280 | |
323 | |
281 | if (grp->grp_first == req) |
324 | if (grp->grp_first == req) |
282 | grp->grp_first = req->grp_next; |
325 | grp->grp_first = req->grp_next; |
283 | |
326 | |
284 | --grp->length; |
|
|
285 | |
|
|
286 | /* call feeder, if applicable */ |
|
|
287 | aio_grp_feed (grp); |
327 | 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 | } |
328 | } |
296 | |
329 | |
297 | if (req->self) |
330 | if (req->self) |
298 | { |
331 | { |
299 | sv_unmagic (req->self, PERL_MAGIC_ext); |
332 | sv_unmagic (req->self, PERL_MAGIC_ext); |
… | |
… | |
312 | Safefree (req); |
345 | Safefree (req); |
313 | } |
346 | } |
314 | |
347 | |
315 | static void req_cancel (aio_req req) |
348 | static void req_cancel (aio_req req) |
316 | { |
349 | { |
317 | req->cancelled = 1; |
350 | req->flags |= FLAG_CANCELLED; |
318 | |
351 | |
319 | if (req->type == REQ_GROUP) |
352 | if (req->type == REQ_GROUP) |
320 | { |
353 | { |
321 | aio_req sub; |
354 | aio_req sub; |
322 | |
355 | |
… | |
… | |
355 | pthread_mutex_unlock (&reslock); |
388 | pthread_mutex_unlock (&reslock); |
356 | |
389 | |
357 | if (!req) |
390 | if (!req) |
358 | break; |
391 | break; |
359 | |
392 | |
360 | nreqs--; |
393 | --nreqs; |
361 | |
394 | |
362 | if (req->type == REQ_QUIT) |
395 | if (req->type == REQ_QUIT) |
363 | started--; |
396 | started--; |
364 | else if (req->type == REQ_GROUP && req->length) |
397 | else if (req->type == REQ_GROUP && req->length) |
365 | { |
398 | { |
… | |
… | |
416 | static void req_send (aio_req req) |
449 | static void req_send (aio_req req) |
417 | { |
450 | { |
418 | while (started < wanted && nreqs >= started) |
451 | while (started < wanted && nreqs >= started) |
419 | start_thread (); |
452 | start_thread (); |
420 | |
453 | |
421 | nreqs++; |
454 | ++nreqs; |
422 | |
455 | |
423 | pthread_mutex_lock (&reqlock); |
456 | pthread_mutex_lock (&reqlock); |
424 | |
457 | |
425 | req->next = 0; |
458 | req->next = 0; |
426 | |
459 | |
… | |
… | |
776 | } |
809 | } |
777 | |
810 | |
778 | pthread_mutex_unlock (&reqlock); |
811 | pthread_mutex_unlock (&reqlock); |
779 | |
812 | |
780 | errno = 0; /* strictly unnecessary */ |
813 | errno = 0; /* strictly unnecessary */ |
|
|
814 | type = req->type; /* remember type for QUIT check */ |
781 | |
815 | |
782 | if (!req->cancelled) |
816 | if (!(req->flags & FLAG_CANCELLED)) |
783 | switch (req->type) |
817 | switch (type) |
784 | { |
818 | { |
785 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
819 | 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; |
820 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
787 | |
821 | |
788 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
822 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
… | |
… | |
812 | tv.tv_usec = req->fd2; |
846 | tv.tv_usec = req->fd2; |
813 | |
847 | |
814 | req->result = select (0, 0, 0, 0, &tv); |
848 | req->result = select (0, 0, 0, 0, &tv); |
815 | } |
849 | } |
816 | |
850 | |
|
|
851 | case REQ_GROUP: |
|
|
852 | case REQ_NOP: |
817 | case REQ_QUIT: |
853 | case REQ_QUIT: |
818 | break; |
854 | break; |
819 | |
855 | |
820 | default: |
856 | default: |
821 | req->result = ENOSYS; |
857 | req->result = ENOSYS; |
… | |
… | |
905 | atfork_parent (); |
941 | atfork_parent (); |
906 | } |
942 | } |
907 | |
943 | |
908 | #define dREQ \ |
944 | #define dREQ \ |
909 | aio_req req; \ |
945 | aio_req req; \ |
|
|
946 | int req_pri = next_pri; \ |
|
|
947 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
910 | \ |
948 | \ |
911 | if (SvOK (callback) && !SvROK (callback)) \ |
949 | if (SvOK (callback) && !SvROK (callback)) \ |
912 | croak ("callback must be undef or of reference type"); \ |
950 | croak ("callback must be undef or of reference type"); \ |
913 | \ |
951 | \ |
914 | Newz (0, req, 1, aio_cb); \ |
952 | Newz (0, req, 1, aio_cb); \ |
915 | if (!req) \ |
953 | if (!req) \ |
916 | croak ("out of memory during aio_req allocation"); \ |
954 | croak ("out of memory during aio_req allocation"); \ |
917 | \ |
955 | \ |
918 | req->callback = newSVsv (callback) |
956 | req->callback = newSVsv (callback); \ |
|
|
957 | req->pri = req_pri |
919 | |
958 | |
920 | #define REQ_SEND \ |
959 | #define REQ_SEND \ |
921 | req_send (req); \ |
960 | req_send (req); \ |
922 | \ |
961 | \ |
923 | if (GIMME_V != G_VOID) \ |
962 | if (GIMME_V != G_VOID) \ |
… | |
… | |
1198 | SV * callback |
1237 | SV * callback |
1199 | PROTOTYPE: ;$ |
1238 | PROTOTYPE: ;$ |
1200 | PPCODE: |
1239 | PPCODE: |
1201 | { |
1240 | { |
1202 | dREQ; |
1241 | dREQ; |
|
|
1242 | |
1203 | req->type = REQ_GROUP; |
1243 | req->type = REQ_GROUP; |
1204 | req_send (req); |
1244 | req_send (req); |
|
|
1245 | |
1205 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1246 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1206 | } |
1247 | } |
|
|
1248 | |
|
|
1249 | void |
|
|
1250 | aio_nop (callback=&PL_sv_undef) |
|
|
1251 | SV * callback |
|
|
1252 | PPCODE: |
|
|
1253 | { |
|
|
1254 | dREQ; |
|
|
1255 | |
|
|
1256 | req->type = REQ_NOP; |
|
|
1257 | |
|
|
1258 | REQ_SEND; |
|
|
1259 | } |
|
|
1260 | |
|
|
1261 | #if 0 |
|
|
1262 | |
|
|
1263 | void |
|
|
1264 | aio_pri (int pri = DEFAULT_PRI) |
|
|
1265 | CODE: |
|
|
1266 | if (pri < PRI_MIN) pri = PRI_MIN; |
|
|
1267 | if (pri > PRI_MAX) pri = PRI_MAX; |
|
|
1268 | next_pri = pri + PRI_BIAS; |
|
|
1269 | |
|
|
1270 | #endif |
1207 | |
1271 | |
1208 | void |
1272 | void |
1209 | flush () |
1273 | flush () |
1210 | PROTOTYPE: |
1274 | PROTOTYPE: |
1211 | CODE: |
1275 | CODE: |
… | |
… | |
1264 | cancel (aio_req_ornot req) |
1328 | cancel (aio_req_ornot req) |
1265 | PROTOTYPE: |
1329 | PROTOTYPE: |
1266 | CODE: |
1330 | CODE: |
1267 | req_cancel (req); |
1331 | req_cancel (req); |
1268 | |
1332 | |
|
|
1333 | void |
|
|
1334 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
|
|
1335 | CODE: |
|
|
1336 | SvREFCNT_dec (req->callback); |
|
|
1337 | req->callback = newSVsv (callback); |
|
|
1338 | |
1269 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1339 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1270 | |
1340 | |
1271 | void |
1341 | void |
1272 | add (aio_req grp, ...) |
1342 | add (aio_req grp, ...) |
1273 | PPCODE: |
1343 | PPCODE: |
1274 | { |
1344 | { |
1275 | int i; |
1345 | int i; |
|
|
1346 | aio_req req; |
1276 | |
1347 | |
1277 | if (grp->fd == 2) |
1348 | if (grp->fd == 2) |
1278 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1349 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1279 | |
1350 | |
1280 | for (i = 1; i < items; ++i ) |
1351 | for (i = 1; i < items; ++i ) |
1281 | { |
1352 | { |
1282 | if (GIMME_V != G_VOID) |
1353 | if (GIMME_V != G_VOID) |
1283 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1354 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1284 | |
1355 | |
1285 | aio_req req = SvAIO_REQ (ST (i)); |
1356 | req = SvAIO_REQ (ST (i)); |
1286 | |
1357 | |
1287 | if (req) |
1358 | if (req) |
1288 | { |
1359 | { |
1289 | ++grp->length; |
1360 | ++grp->length; |
1290 | req->grp = grp; |
1361 | req->grp = grp; |
… | |
… | |
1300 | } |
1371 | } |
1301 | } |
1372 | } |
1302 | |
1373 | |
1303 | void |
1374 | void |
1304 | result (aio_req grp, ...) |
1375 | result (aio_req grp, ...) |
1305 | CODE: |
1376 | CODE: |
1306 | { |
1377 | { |
1307 | int i; |
1378 | int i; |
1308 | AV *av = newAV (); |
1379 | AV *av = newAV (); |
1309 | |
1380 | |
1310 | for (i = 1; i < items; ++i ) |
1381 | for (i = 1; i < items; ++i ) |
1311 | av_push (av, newSVsv (ST (i))); |
1382 | av_push (av, newSVsv (ST (i))); |
1312 | |
1383 | |
1313 | SvREFCNT_dec (grp->data); |
1384 | SvREFCNT_dec (grp->data); |
1314 | grp->data = (SV *)av; |
1385 | grp->data = (SV *)av; |
1315 | } |
1386 | } |
1316 | |
1387 | |
1317 | void |
1388 | void |
1318 | feeder_limit (aio_req grp, int limit) |
1389 | feed_limit (aio_req grp, int limit) |
1319 | CODE: |
1390 | CODE: |
1320 | grp->fd2 = limit; |
1391 | grp->fd2 = limit; |
1321 | aio_grp_feed (grp); |
1392 | aio_grp_feed (grp); |
1322 | |
1393 | |
1323 | void |
1394 | void |
1324 | set_feeder (aio_req grp, SV *callback=&PL_sv_undef) |
1395 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1325 | CODE: |
1396 | CODE: |
1326 | { |
1397 | { |
1327 | SvREFCNT_dec (grp->fh2); |
1398 | SvREFCNT_dec (grp->fh2); |
1328 | grp->fh2 = newSVsv (callback); |
1399 | grp->fh2 = newSVsv (callback); |
1329 | |
1400 | |