… | |
… | |
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 | |
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 | |
|
|
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]; |
… | |
… | |
130 | return mg ? (aio_req)mg->mg_ptr : 0; |
145 | return mg ? (aio_req)mg->mg_ptr : 0; |
131 | } |
146 | } |
132 | |
147 | |
133 | static void aio_grp_feed (aio_req grp) |
148 | static void aio_grp_feed (aio_req grp) |
134 | { |
149 | { |
135 | while (grp->length < grp->fd2) |
150 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
136 | { |
151 | { |
137 | int old_len = grp->length; |
152 | int old_len = grp->length; |
138 | |
153 | |
139 | if (grp->fh2 && SvOK (grp->fh2)) |
154 | if (grp->fh2 && SvOK (grp->fh2)) |
140 | { |
155 | { |
… | |
… | |
176 | } |
191 | } |
177 | } |
192 | } |
178 | |
193 | |
179 | static void poll_wait () |
194 | static void poll_wait () |
180 | { |
195 | { |
181 | if (nreqs && !ress) |
|
|
182 | { |
|
|
183 | 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 | |
184 | FD_ZERO(&rfd); |
208 | FD_ZERO(&rfd); |
185 | FD_SET(respipe [0], &rfd); |
209 | FD_SET(respipe [0], &rfd); |
186 | |
210 | |
187 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
211 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
188 | } |
212 | } |
… | |
… | |
191 | static void req_invoke (aio_req req) |
215 | static void req_invoke (aio_req req) |
192 | { |
216 | { |
193 | dSP; |
217 | dSP; |
194 | int errorno = errno; |
218 | int errorno = errno; |
195 | |
219 | |
196 | if (req->cancelled || !SvOK (req->callback)) |
220 | if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) |
197 | return; |
221 | return; |
198 | |
222 | |
199 | errno = req->errorno; |
223 | errno = req->errorno; |
200 | |
224 | |
201 | ENTER; |
225 | ENTER; |
… | |
… | |
321 | Safefree (req); |
345 | Safefree (req); |
322 | } |
346 | } |
323 | |
347 | |
324 | static void req_cancel (aio_req req) |
348 | static void req_cancel (aio_req req) |
325 | { |
349 | { |
326 | req->cancelled = 1; |
350 | req->flags |= FLAG_CANCELLED; |
327 | |
351 | |
328 | if (req->type == REQ_GROUP) |
352 | if (req->type == REQ_GROUP) |
329 | { |
353 | { |
330 | aio_req sub; |
354 | aio_req sub; |
331 | |
355 | |
… | |
… | |
785 | } |
809 | } |
786 | |
810 | |
787 | pthread_mutex_unlock (&reqlock); |
811 | pthread_mutex_unlock (&reqlock); |
788 | |
812 | |
789 | errno = 0; /* strictly unnecessary */ |
813 | errno = 0; /* strictly unnecessary */ |
790 | |
|
|
791 | if (!req->cancelled) |
|
|
792 | switch (type = req->type) /* remember type for QUIT check */ |
814 | type = req->type; /* remember type for QUIT check */ |
|
|
815 | |
|
|
816 | if (!(req->flags & FLAG_CANCELLED)) |
|
|
817 | switch (type) |
793 | { |
818 | { |
794 | 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; |
795 | 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; |
796 | |
821 | |
797 | 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; |
… | |
… | |
821 | tv.tv_usec = req->fd2; |
846 | tv.tv_usec = req->fd2; |
822 | |
847 | |
823 | req->result = select (0, 0, 0, 0, &tv); |
848 | req->result = select (0, 0, 0, 0, &tv); |
824 | } |
849 | } |
825 | |
850 | |
826 | case REQ_GROUP: /*TODO: should not be handled here */ |
851 | case REQ_GROUP: |
827 | case REQ_NOP: /*TODO: should not be handled here */ |
852 | case REQ_NOP: |
828 | case REQ_QUIT: |
853 | case REQ_QUIT: |
829 | break; |
854 | break; |
830 | |
855 | |
831 | default: |
856 | default: |
832 | req->result = ENOSYS; |
857 | req->result = ENOSYS; |
… | |
… | |
916 | atfork_parent (); |
941 | atfork_parent (); |
917 | } |
942 | } |
918 | |
943 | |
919 | #define dREQ \ |
944 | #define dREQ \ |
920 | aio_req req; \ |
945 | aio_req req; \ |
|
|
946 | int req_pri = next_pri; \ |
|
|
947 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
921 | \ |
948 | \ |
922 | if (SvOK (callback) && !SvROK (callback)) \ |
949 | if (SvOK (callback) && !SvROK (callback)) \ |
923 | croak ("callback must be undef or of reference type"); \ |
950 | croak ("callback must be undef or of reference type"); \ |
924 | \ |
951 | \ |
925 | Newz (0, req, 1, aio_cb); \ |
952 | Newz (0, req, 1, aio_cb); \ |
926 | if (!req) \ |
953 | if (!req) \ |
927 | croak ("out of memory during aio_req allocation"); \ |
954 | croak ("out of memory during aio_req allocation"); \ |
928 | \ |
955 | \ |
929 | req->callback = newSVsv (callback) |
956 | req->callback = newSVsv (callback); \ |
|
|
957 | req->pri = req_pri |
930 | |
958 | |
931 | #define REQ_SEND \ |
959 | #define REQ_SEND \ |
932 | req_send (req); \ |
960 | req_send (req); \ |
933 | \ |
961 | \ |
934 | if (GIMME_V != G_VOID) \ |
962 | if (GIMME_V != G_VOID) \ |
… | |
… | |
1209 | SV * callback |
1237 | SV * callback |
1210 | PROTOTYPE: ;$ |
1238 | PROTOTYPE: ;$ |
1211 | PPCODE: |
1239 | PPCODE: |
1212 | { |
1240 | { |
1213 | dREQ; |
1241 | dREQ; |
|
|
1242 | |
1214 | req->type = REQ_GROUP; |
1243 | req->type = REQ_GROUP; |
1215 | req_send (req); |
1244 | req_send (req); |
|
|
1245 | |
1216 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1246 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1217 | } |
1247 | } |
1218 | |
1248 | |
1219 | void |
1249 | void |
1220 | aio_nop (callback=&PL_sv_undef) |
1250 | aio_nop (callback=&PL_sv_undef) |
… | |
… | |
1225 | |
1255 | |
1226 | req->type = REQ_NOP; |
1256 | req->type = REQ_NOP; |
1227 | |
1257 | |
1228 | REQ_SEND; |
1258 | REQ_SEND; |
1229 | } |
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 |
1230 | |
1271 | |
1231 | void |
1272 | void |
1232 | flush () |
1273 | flush () |
1233 | PROTOTYPE: |
1274 | PROTOTYPE: |
1234 | CODE: |
1275 | CODE: |
… | |
… | |
1287 | cancel (aio_req_ornot req) |
1328 | cancel (aio_req_ornot req) |
1288 | PROTOTYPE: |
1329 | PROTOTYPE: |
1289 | CODE: |
1330 | CODE: |
1290 | req_cancel (req); |
1331 | req_cancel (req); |
1291 | |
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 | |
1292 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1339 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1293 | |
1340 | |
1294 | void |
1341 | void |
1295 | add (aio_req grp, ...) |
1342 | add (aio_req grp, ...) |
1296 | PPCODE: |
1343 | PPCODE: |
… | |
… | |
1337 | SvREFCNT_dec (grp->data); |
1384 | SvREFCNT_dec (grp->data); |
1338 | grp->data = (SV *)av; |
1385 | grp->data = (SV *)av; |
1339 | } |
1386 | } |
1340 | |
1387 | |
1341 | void |
1388 | void |
1342 | lock (aio_req grp) |
|
|
1343 | CODE: |
|
|
1344 | ++grp->length; |
|
|
1345 | |
|
|
1346 | void |
|
|
1347 | unlock (aio_req grp) |
|
|
1348 | CODE: |
|
|
1349 | aio_grp_dec (grp); |
|
|
1350 | |
|
|
1351 | void |
|
|
1352 | feeder_limit (aio_req grp, int limit) |
1389 | feed_limit (aio_req grp, int limit) |
1353 | CODE: |
1390 | CODE: |
1354 | grp->fd2 = limit; |
1391 | grp->fd2 = limit; |
1355 | aio_grp_feed (grp); |
1392 | aio_grp_feed (grp); |
1356 | |
1393 | |
1357 | void |
1394 | void |
1358 | set_feeder (aio_req grp, SV *callback=&PL_sv_undef) |
1395 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1359 | CODE: |
1396 | CODE: |
1360 | { |
1397 | { |
1361 | SvREFCNT_dec (grp->fh2); |
1398 | SvREFCNT_dec (grp->fh2); |
1362 | grp->fh2 = newSVsv (callback); |
1399 | grp->fh2 = newSVsv (callback); |
1363 | |
1400 | |