|
|
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 *volatile next; |
74 | 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 | |
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 | |
… | |
… | |
118 | 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))); |
119 | } |
145 | } |
120 | |
146 | |
121 | static aio_req SvAIO_REQ (SV *sv) |
147 | static aio_req SvAIO_REQ (SV *sv) |
122 | { |
148 | { |
|
|
149 | MAGIC *mg; |
|
|
150 | |
123 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
151 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
124 | croak ("object of class " AIO_REQ_KLASS " expected"); |
152 | croak ("object of class " AIO_REQ_KLASS " expected"); |
125 | |
153 | |
126 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
154 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
127 | |
155 | |
128 | return mg ? (aio_req)mg->mg_ptr : 0; |
156 | return mg ? (aio_req)mg->mg_ptr : 0; |
129 | } |
157 | } |
130 | |
158 | |
131 | static void aio_grp_feed (aio_req grp) |
159 | static void aio_grp_feed (aio_req grp) |
132 | { |
160 | { |
133 | while (grp->length < grp->fd2) |
161 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
134 | { |
162 | { |
135 | int old_len = grp->length; |
163 | int old_len = grp->length; |
136 | |
164 | |
137 | if (grp->fh2 && SvOK (grp->fh2)) |
165 | if (grp->fh2 && SvOK (grp->fh2)) |
138 | { |
166 | { |
… | |
… | |
174 | } |
202 | } |
175 | } |
203 | } |
176 | |
204 | |
177 | static void poll_wait () |
205 | static void poll_wait () |
178 | { |
206 | { |
179 | if (nreqs && !ress) |
|
|
180 | { |
|
|
181 | 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 | |
182 | FD_ZERO(&rfd); |
223 | FD_ZERO(&rfd); |
183 | FD_SET(respipe [0], &rfd); |
224 | FD_SET(respipe [0], &rfd); |
184 | |
225 | |
185 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
226 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
186 | } |
227 | } |
… | |
… | |
189 | static void req_invoke (aio_req req) |
230 | static void req_invoke (aio_req req) |
190 | { |
231 | { |
191 | dSP; |
232 | dSP; |
192 | int errorno = errno; |
233 | int errorno = errno; |
193 | |
234 | |
194 | if (req->cancelled || !SvOK (req->callback)) |
235 | if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) |
195 | return; |
236 | return; |
196 | |
237 | |
197 | errno = req->errorno; |
238 | errno = req->errorno; |
198 | |
239 | |
199 | ENTER; |
240 | ENTER; |
… | |
… | |
257 | for (i = 0; i <= AvFILL (av); ++i) |
298 | for (i = 0; i <= AvFILL (av); ++i) |
258 | PUSHs (*av_fetch (av, i, 0)); |
299 | PUSHs (*av_fetch (av, i, 0)); |
259 | } |
300 | } |
260 | break; |
301 | break; |
261 | |
302 | |
|
|
303 | case REQ_NOP: |
262 | case REQ_SLEEP: |
304 | case REQ_SLEEP: |
263 | break; |
305 | break; |
264 | |
306 | |
265 | default: |
307 | default: |
266 | PUSHs (sv_2mortal (newSViv (req->result))); |
308 | PUSHs (sv_2mortal (newSViv (req->result))); |
… | |
… | |
318 | Safefree (req); |
360 | Safefree (req); |
319 | } |
361 | } |
320 | |
362 | |
321 | static void req_cancel (aio_req req) |
363 | static void req_cancel (aio_req req) |
322 | { |
364 | { |
323 | req->cancelled = 1; |
365 | req->flags |= FLAG_CANCELLED; |
324 | |
366 | |
325 | if (req->type == REQ_GROUP) |
367 | if (req->type == REQ_GROUP) |
326 | { |
368 | { |
327 | aio_req sub; |
369 | aio_req sub; |
328 | |
370 | |
… | |
… | |
782 | } |
824 | } |
783 | |
825 | |
784 | pthread_mutex_unlock (&reqlock); |
826 | pthread_mutex_unlock (&reqlock); |
785 | |
827 | |
786 | errno = 0; /* strictly unnecessary */ |
828 | errno = 0; /* strictly unnecessary */ |
787 | |
|
|
788 | if (!req->cancelled) |
|
|
789 | 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) |
790 | { |
833 | { |
791 | 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; |
792 | 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; |
793 | |
836 | |
794 | 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; |
… | |
… | |
818 | tv.tv_usec = req->fd2; |
861 | tv.tv_usec = req->fd2; |
819 | |
862 | |
820 | req->result = select (0, 0, 0, 0, &tv); |
863 | req->result = select (0, 0, 0, 0, &tv); |
821 | } |
864 | } |
822 | |
865 | |
|
|
866 | case REQ_GROUP: |
|
|
867 | case REQ_NOP: |
823 | case REQ_QUIT: |
868 | case REQ_QUIT: |
824 | break; |
869 | break; |
825 | |
870 | |
826 | default: |
871 | default: |
827 | req->result = ENOSYS; |
872 | req->result = ENOSYS; |
… | |
… | |
832 | |
877 | |
833 | pthread_mutex_lock (&reslock); |
878 | pthread_mutex_lock (&reslock); |
834 | |
879 | |
835 | req->next = 0; |
880 | req->next = 0; |
836 | |
881 | |
837 | printf ("queue rese %p\n", rese);//D |
|
|
838 | if (rese) |
882 | if (rese) |
839 | { |
883 | { |
840 | rese->next = req; |
884 | rese->next = req; |
841 | rese = req; |
885 | rese = req; |
842 | } |
886 | } |
… | |
… | |
912 | atfork_parent (); |
956 | atfork_parent (); |
913 | } |
957 | } |
914 | |
958 | |
915 | #define dREQ \ |
959 | #define dREQ \ |
916 | aio_req req; \ |
960 | aio_req req; \ |
|
|
961 | int req_pri = next_pri; \ |
|
|
962 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
917 | \ |
963 | \ |
918 | if (SvOK (callback) && !SvROK (callback)) \ |
964 | if (SvOK (callback) && !SvROK (callback)) \ |
919 | croak ("callback must be undef or of reference type"); \ |
965 | croak ("callback must be undef or of reference type"); \ |
920 | \ |
966 | \ |
921 | Newz (0, req, 1, aio_cb); \ |
967 | Newz (0, req, 1, aio_cb); \ |
922 | if (!req) \ |
968 | if (!req) \ |
923 | croak ("out of memory during aio_req allocation"); \ |
969 | croak ("out of memory during aio_req allocation"); \ |
924 | \ |
970 | \ |
925 | req->callback = newSVsv (callback) |
971 | req->callback = newSVsv (callback); \ |
|
|
972 | req->pri = req_pri |
926 | |
973 | |
927 | #define REQ_SEND \ |
974 | #define REQ_SEND \ |
928 | req_send (req); \ |
975 | req_send (req); \ |
929 | \ |
976 | \ |
930 | if (GIMME_V != G_VOID) \ |
977 | if (GIMME_V != G_VOID) \ |
… | |
… | |
1205 | SV * callback |
1252 | SV * callback |
1206 | PROTOTYPE: ;$ |
1253 | PROTOTYPE: ;$ |
1207 | PPCODE: |
1254 | PPCODE: |
1208 | { |
1255 | { |
1209 | dREQ; |
1256 | dREQ; |
|
|
1257 | |
1210 | req->type = REQ_GROUP; |
1258 | req->type = REQ_GROUP; |
1211 | req_send (req); |
1259 | req_send (req); |
|
|
1260 | |
1212 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1261 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1213 | } |
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 |
1214 | |
1286 | |
1215 | void |
1287 | void |
1216 | flush () |
1288 | flush () |
1217 | PROTOTYPE: |
1289 | PROTOTYPE: |
1218 | CODE: |
1290 | CODE: |
… | |
… | |
1271 | cancel (aio_req_ornot req) |
1343 | cancel (aio_req_ornot req) |
1272 | PROTOTYPE: |
1344 | PROTOTYPE: |
1273 | CODE: |
1345 | CODE: |
1274 | req_cancel (req); |
1346 | req_cancel (req); |
1275 | |
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 | |
1276 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1354 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1277 | |
1355 | |
1278 | void |
1356 | void |
1279 | add (aio_req grp, ...) |
1357 | add (aio_req grp, ...) |
1280 | PPCODE: |
1358 | PPCODE: |
1281 | { |
1359 | { |
1282 | int i; |
1360 | int i; |
|
|
1361 | aio_req req; |
1283 | |
1362 | |
1284 | if (grp->fd == 2) |
1363 | if (grp->fd == 2) |
1285 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1364 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1286 | |
1365 | |
1287 | for (i = 1; i < items; ++i ) |
1366 | for (i = 1; i < items; ++i ) |
1288 | { |
1367 | { |
1289 | if (GIMME_V != G_VOID) |
1368 | if (GIMME_V != G_VOID) |
1290 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1369 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1291 | |
1370 | |
1292 | aio_req req = SvAIO_REQ (ST (i)); |
1371 | req = SvAIO_REQ (ST (i)); |
1293 | |
1372 | |
1294 | if (req) |
1373 | if (req) |
1295 | { |
1374 | { |
1296 | ++grp->length; |
1375 | ++grp->length; |
1297 | req->grp = grp; |
1376 | req->grp = grp; |
… | |
… | |
1320 | SvREFCNT_dec (grp->data); |
1399 | SvREFCNT_dec (grp->data); |
1321 | grp->data = (SV *)av; |
1400 | grp->data = (SV *)av; |
1322 | } |
1401 | } |
1323 | |
1402 | |
1324 | void |
1403 | void |
1325 | lock (aio_req grp) |
|
|
1326 | CODE: |
|
|
1327 | ++grp->length; |
|
|
1328 | |
|
|
1329 | void |
|
|
1330 | unlock (aio_req grp) |
|
|
1331 | CODE: |
|
|
1332 | aio_grp_dec (grp); |
|
|
1333 | |
|
|
1334 | void |
|
|
1335 | feeder_limit (aio_req grp, int limit) |
1404 | feed_limit (aio_req grp, int limit) |
1336 | CODE: |
1405 | CODE: |
1337 | grp->fd2 = limit; |
1406 | grp->fd2 = limit; |
1338 | aio_grp_feed (grp); |
1407 | aio_grp_feed (grp); |
1339 | |
1408 | |
1340 | void |
1409 | void |
1341 | set_feeder (aio_req grp, SV *callback=&PL_sv_undef) |
1410 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1342 | CODE: |
1411 | CODE: |
1343 | { |
1412 | { |
1344 | SvREFCNT_dec (grp->fh2); |
1413 | SvREFCNT_dec (grp->fh2); |
1345 | grp->fh2 = newSVsv (callback); |
1414 | grp->fh2 = newSVsv (callback); |
1346 | |
1415 | |