|
|
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" |
… | |
… | |
66 | |
71 | |
67 | typedef struct aio_cb |
72 | typedef struct aio_cb |
68 | { |
73 | { |
69 | struct aio_cb *volatile next; |
74 | struct aio_cb *volatile next; |
70 | |
75 | |
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; |
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 | |
… | |
… | |
130 | return mg ? (aio_req)mg->mg_ptr : 0; |
156 | return mg ? (aio_req)mg->mg_ptr : 0; |
131 | } |
157 | } |
132 | |
158 | |
133 | static void aio_grp_feed (aio_req grp) |
159 | static void aio_grp_feed (aio_req grp) |
134 | { |
160 | { |
135 | while (grp->length < grp->fd2) |
161 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
136 | { |
162 | { |
137 | int old_len = grp->length; |
163 | int old_len = grp->length; |
138 | |
164 | |
139 | if (grp->fh2 && SvOK (grp->fh2)) |
165 | if (grp->fh2 && SvOK (grp->fh2)) |
140 | { |
166 | { |
… | |
… | |
176 | } |
202 | } |
177 | } |
203 | } |
178 | |
204 | |
179 | static void poll_wait () |
205 | static void poll_wait () |
180 | { |
206 | { |
181 | if (nreqs && !ress) |
|
|
182 | { |
|
|
183 | 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 | |
184 | FD_ZERO(&rfd); |
223 | FD_ZERO(&rfd); |
185 | FD_SET(respipe [0], &rfd); |
224 | FD_SET(respipe [0], &rfd); |
186 | |
225 | |
187 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
226 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
188 | } |
227 | } |
… | |
… | |
191 | static void req_invoke (aio_req req) |
230 | static void req_invoke (aio_req req) |
192 | { |
231 | { |
193 | dSP; |
232 | dSP; |
194 | int errorno = errno; |
233 | int errorno = errno; |
195 | |
234 | |
196 | if (req->cancelled || !SvOK (req->callback)) |
235 | if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) |
197 | return; |
236 | return; |
198 | |
237 | |
199 | errno = req->errorno; |
238 | errno = req->errorno; |
200 | |
239 | |
201 | ENTER; |
240 | ENTER; |
… | |
… | |
321 | Safefree (req); |
360 | Safefree (req); |
322 | } |
361 | } |
323 | |
362 | |
324 | static void req_cancel (aio_req req) |
363 | static void req_cancel (aio_req req) |
325 | { |
364 | { |
326 | req->cancelled = 1; |
365 | req->flags |= FLAG_CANCELLED; |
327 | |
366 | |
328 | if (req->type == REQ_GROUP) |
367 | if (req->type == REQ_GROUP) |
329 | { |
368 | { |
330 | aio_req sub; |
369 | aio_req sub; |
331 | |
370 | |
… | |
… | |
785 | } |
824 | } |
786 | |
825 | |
787 | pthread_mutex_unlock (&reqlock); |
826 | pthread_mutex_unlock (&reqlock); |
788 | |
827 | |
789 | errno = 0; /* strictly unnecessary */ |
828 | errno = 0; /* strictly unnecessary */ |
790 | |
|
|
791 | if (!req->cancelled) |
|
|
792 | switch (type = req->type) /* remember type for QUIT check */ |
829 | type = req->type; /* remember type for QUIT check */ |
|
|
830 | |
|
|
831 | if (!(req->flags & FLAG_CANCELLED)) |
|
|
832 | switch (type) |
793 | { |
833 | { |
794 | 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; |
795 | 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; |
796 | |
836 | |
797 | 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; |
… | |
… | |
821 | tv.tv_usec = req->fd2; |
861 | tv.tv_usec = req->fd2; |
822 | |
862 | |
823 | req->result = select (0, 0, 0, 0, &tv); |
863 | req->result = select (0, 0, 0, 0, &tv); |
824 | } |
864 | } |
825 | |
865 | |
826 | case REQ_GROUP: /*TODO: should not be handled here */ |
866 | case REQ_GROUP: |
827 | case REQ_NOP: /*TODO: should not be handled here */ |
867 | case REQ_NOP: |
828 | case REQ_QUIT: |
868 | case REQ_QUIT: |
829 | break; |
869 | break; |
830 | |
870 | |
831 | default: |
871 | default: |
832 | req->result = ENOSYS; |
872 | req->result = ENOSYS; |
… | |
… | |
916 | atfork_parent (); |
956 | atfork_parent (); |
917 | } |
957 | } |
918 | |
958 | |
919 | #define dREQ \ |
959 | #define dREQ \ |
920 | aio_req req; \ |
960 | aio_req req; \ |
|
|
961 | int req_pri = next_pri; \ |
|
|
962 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
921 | \ |
963 | \ |
922 | if (SvOK (callback) && !SvROK (callback)) \ |
964 | if (SvOK (callback) && !SvROK (callback)) \ |
923 | croak ("callback must be undef or of reference type"); \ |
965 | croak ("callback must be undef or of reference type"); \ |
924 | \ |
966 | \ |
925 | Newz (0, req, 1, aio_cb); \ |
967 | Newz (0, req, 1, aio_cb); \ |
926 | if (!req) \ |
968 | if (!req) \ |
927 | croak ("out of memory during aio_req allocation"); \ |
969 | croak ("out of memory during aio_req allocation"); \ |
928 | \ |
970 | \ |
929 | req->callback = newSVsv (callback) |
971 | req->callback = newSVsv (callback); \ |
|
|
972 | req->pri = req_pri |
930 | |
973 | |
931 | #define REQ_SEND \ |
974 | #define REQ_SEND \ |
932 | req_send (req); \ |
975 | req_send (req); \ |
933 | \ |
976 | \ |
934 | if (GIMME_V != G_VOID) \ |
977 | if (GIMME_V != G_VOID) \ |
… | |
… | |
1209 | SV * callback |
1252 | SV * callback |
1210 | PROTOTYPE: ;$ |
1253 | PROTOTYPE: ;$ |
1211 | PPCODE: |
1254 | PPCODE: |
1212 | { |
1255 | { |
1213 | dREQ; |
1256 | dREQ; |
|
|
1257 | |
1214 | req->type = REQ_GROUP; |
1258 | req->type = REQ_GROUP; |
1215 | req_send (req); |
1259 | req_send (req); |
|
|
1260 | |
1216 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1261 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1217 | } |
1262 | } |
1218 | |
1263 | |
1219 | void |
1264 | void |
1220 | aio_nop (callback=&PL_sv_undef) |
1265 | aio_nop (callback=&PL_sv_undef) |
… | |
… | |
1225 | |
1270 | |
1226 | req->type = REQ_NOP; |
1271 | req->type = REQ_NOP; |
1227 | |
1272 | |
1228 | REQ_SEND; |
1273 | REQ_SEND; |
1229 | } |
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 |
1230 | |
1286 | |
1231 | void |
1287 | void |
1232 | flush () |
1288 | flush () |
1233 | PROTOTYPE: |
1289 | PROTOTYPE: |
1234 | CODE: |
1290 | CODE: |
… | |
… | |
1287 | cancel (aio_req_ornot req) |
1343 | cancel (aio_req_ornot req) |
1288 | PROTOTYPE: |
1344 | PROTOTYPE: |
1289 | CODE: |
1345 | CODE: |
1290 | req_cancel (req); |
1346 | req_cancel (req); |
1291 | |
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 | |
1292 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1354 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1293 | |
1355 | |
1294 | void |
1356 | void |
1295 | add (aio_req grp, ...) |
1357 | add (aio_req grp, ...) |
1296 | PPCODE: |
1358 | PPCODE: |
… | |
… | |
1337 | SvREFCNT_dec (grp->data); |
1399 | SvREFCNT_dec (grp->data); |
1338 | grp->data = (SV *)av; |
1400 | grp->data = (SV *)av; |
1339 | } |
1401 | } |
1340 | |
1402 | |
1341 | void |
1403 | 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) |
1404 | feed_limit (aio_req grp, int limit) |
1353 | CODE: |
1405 | CODE: |
1354 | grp->fd2 = limit; |
1406 | grp->fd2 = limit; |
1355 | aio_grp_feed (grp); |
1407 | aio_grp_feed (grp); |
1356 | |
1408 | |
1357 | void |
1409 | void |
1358 | set_feeder (aio_req grp, SV *callback=&PL_sv_undef) |
1410 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1359 | CODE: |
1411 | CODE: |
1360 | { |
1412 | { |
1361 | SvREFCNT_dec (grp->fh2); |
1413 | SvREFCNT_dec (grp->fh2); |
1362 | grp->fh2 = newSVsv (callback); |
1414 | grp->fh2 = newSVsv (callback); |
1363 | |
1415 | |