1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include <sys/types.h> |
5 | #include <sys/types.h> |
|
|
6 | #include <sys/stat.h> |
6 | #include <unistd.h> |
7 | #include <unistd.h> |
7 | #include <fcntl.h> |
8 | #include <fcntl.h> |
8 | #include <signal.h> |
9 | #include <signal.h> |
9 | #include <sched.h> |
10 | #include <sched.h> |
10 | |
11 | |
|
|
12 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
13 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
11 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
14 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
12 | |
15 | |
13 | #define STACKSIZE 1024 /* yeah */ |
16 | #define STACKSIZE 1024 /* yeah */ |
14 | |
17 | |
15 | enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN, REQ_CLOSE }; |
18 | enum { REQ_QUIT, REQ_OPEN, REQ_CLOSE, REQ_READ, REQ_WRITE, REQ_STAT, REQ_LSTAT, REQ_FSTAT}; |
16 | |
19 | |
17 | typedef struct { |
20 | typedef struct { |
18 | char stack[STACKSIZE]; |
21 | char stack[STACKSIZE]; |
19 | } aio_thread; |
22 | } aio_thread; |
20 | |
23 | |
21 | typedef struct { |
24 | typedef struct { |
22 | int type; |
25 | int type; |
23 | aio_thread *thread; |
26 | aio_thread *thread; |
24 | |
27 | |
25 | /* read/write */ |
|
|
26 | int fd; |
28 | int fd; |
27 | off_t offset; |
29 | off_t offset; |
28 | size_t length; |
30 | size_t length; |
29 | ssize_t result; |
31 | ssize_t result; |
30 | mode_t mode; /* open */ |
32 | mode_t mode; /* open */ |
31 | int errorno; |
33 | int errorno; |
32 | SV *data, *callback; |
34 | SV *data, *callback; |
33 | void *dataptr; |
35 | void *dataptr; |
34 | STRLEN dataoffset; |
36 | STRLEN dataoffset; |
|
|
37 | |
|
|
38 | struct stat64 *statdata; |
35 | } aio_cb; |
39 | } aio_cb; |
36 | |
40 | |
37 | typedef aio_cb *aio_req; |
41 | typedef aio_cb *aio_req; |
38 | |
42 | |
39 | static int started; |
43 | static int started; |
… | |
… | |
79 | SV *data, STRLEN dataoffset, SV*callback) |
83 | SV *data, STRLEN dataoffset, SV*callback) |
80 | { |
84 | { |
81 | aio_req req; |
85 | aio_req req; |
82 | STRLEN svlen; |
86 | STRLEN svlen; |
83 | char *svptr = SvPV (data, svlen); |
87 | char *svptr = SvPV (data, svlen); |
|
|
88 | |
|
|
89 | SvUPGRADE (data, SVt_PV); |
|
|
90 | SvPOK_on (data); |
84 | |
91 | |
85 | if (dataoffset < 0) |
92 | if (dataoffset < 0) |
86 | dataoffset += svlen; |
93 | dataoffset += svlen; |
87 | |
94 | |
88 | if (dataoffset < 0 || dataoffset > svlen) |
95 | if (dataoffset < 0 || dataoffset > svlen) |
… | |
… | |
101 | } |
108 | } |
102 | |
109 | |
103 | if (length < 0) |
110 | if (length < 0) |
104 | croak ("length must not be negative"); |
111 | croak ("length must not be negative"); |
105 | |
112 | |
106 | New (0, req, 1, aio_cb); |
113 | Newz (0, req, 1, aio_cb); |
107 | |
114 | |
108 | if (!req) |
115 | if (!req) |
109 | croak ("out of memory during aio_req allocation"); |
116 | croak ("out of memory during aio_req allocation"); |
110 | |
117 | |
111 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
118 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
… | |
… | |
140 | |
147 | |
141 | if (req->type == REQ_READ) |
148 | if (req->type == REQ_READ) |
142 | SvCUR_set (req->data, req->dataoffset |
149 | SvCUR_set (req->data, req->dataoffset |
143 | + req->result > 0 ? req->result : 0); |
150 | + req->result > 0 ? req->result : 0); |
144 | |
151 | |
|
|
152 | if (req->data) |
|
|
153 | SvREFCNT_dec (req->data); |
|
|
154 | |
|
|
155 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
156 | { |
|
|
157 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
158 | PL_laststatval = req->result; |
|
|
159 | PL_statcache.st_dev = req->statdata->st_dev; |
|
|
160 | PL_statcache.st_ino = req->statdata->st_ino; |
|
|
161 | PL_statcache.st_mode = req->statdata->st_mode; |
|
|
162 | PL_statcache.st_nlink = req->statdata->st_nlink; |
|
|
163 | PL_statcache.st_uid = req->statdata->st_uid; |
|
|
164 | PL_statcache.st_gid = req->statdata->st_gid; |
|
|
165 | PL_statcache.st_rdev = req->statdata->st_rdev; |
|
|
166 | PL_statcache.st_size = req->statdata->st_size; |
|
|
167 | PL_statcache.st_atime = req->statdata->st_atime; |
|
|
168 | PL_statcache.st_mtime = req->statdata->st_mtime; |
|
|
169 | PL_statcache.st_ctime = req->statdata->st_ctime; |
|
|
170 | PL_statcache.st_blksize = req->statdata->st_blksize; |
|
|
171 | PL_statcache.st_blocks = req->statdata->st_blocks; |
|
|
172 | |
|
|
173 | Safefree (req->statdata); |
|
|
174 | } |
|
|
175 | |
145 | PUSHMARK (SP); |
176 | PUSHMARK (SP); |
146 | XPUSHs (sv_2mortal (newSViv (req->result))); |
177 | XPUSHs (sv_2mortal (newSViv (req->result))); |
147 | PUTBACK; |
178 | PUTBACK; |
148 | call_sv (req->callback, G_VOID); |
179 | call_sv (req->callback, G_VOID); |
149 | SPAGAIN; |
180 | SPAGAIN; |
150 | |
181 | |
151 | SvREFCNT_dec (req->data); |
182 | if (req->callback) |
152 | SvREFCNT_dec (req->callback); |
183 | SvREFCNT_dec (req->callback); |
153 | |
184 | |
154 | errno = errorno; |
185 | errno = errorno; |
155 | nreqs--; |
186 | nreqs--; |
156 | count++; |
187 | count++; |
157 | } |
188 | } |
… | |
… | |
172 | { |
203 | { |
173 | aio_thread *thr = thr_arg; |
204 | aio_thread *thr = thr_arg; |
174 | aio_req req; |
205 | aio_req req; |
175 | int errno; |
206 | int errno; |
176 | |
207 | |
|
|
208 | /* this is very much x86 and kernel-specific :(:(:( */ |
177 | /* we rely on gcc's ability to create closures. */ |
209 | /* we rely on gcc's ability to create closures. */ |
178 | _syscall3(int,lseek,int,fd,off_t,offset,int,whence) |
|
|
179 | _syscall3(int,read,int,fd,char *,buf,off_t,count) |
210 | _syscall3(int,read,int,fd,char *,buf,size_t,count) |
180 | _syscall3(int,write,int,fd,char *,buf,off_t,count) |
211 | _syscall3(int,write,int,fd,char *,buf,size_t,count) |
|
|
212 | |
181 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
213 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
182 | _syscall1(int,close,int,fd) |
214 | _syscall1(int,close,int,fd) |
|
|
215 | |
|
|
216 | _syscall5(int,pread,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
|
|
217 | _syscall5(int,pwrite,int,fd,char *,buf,size_t,count,unsigned int,offset_lo,unsigned int,offset_hi) |
|
|
218 | |
|
|
219 | _syscall2(int,stat64, const char *, filename, struct stat64 *, buf) |
|
|
220 | _syscall2(int,lstat64, const char *, filename, struct stat64 *, buf) |
|
|
221 | _syscall2(int,fstat64, int, fd, struct stat64 *, buf) |
183 | |
222 | |
184 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
223 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
185 | |
224 | |
186 | /* then loop */ |
225 | /* then loop */ |
187 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
226 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
188 | { |
227 | { |
189 | req->thread = thr; |
228 | req->thread = thr; |
190 | errno = 0; |
229 | errno = 0; /* strictly unnecessary */ |
191 | |
230 | |
192 | if (req->type == REQ_READ || req->type == REQ_WRITE) |
|
|
193 | { |
|
|
194 | if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) |
|
|
195 | { |
|
|
196 | if (req->type == REQ_READ) |
231 | if (req->type == REQ_READ) |
197 | req->result = read (req->fd, req->dataptr, req->length); |
232 | req->result = pread (req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); |
198 | else |
233 | else if (req->type == REQ_WRITE) |
199 | req->result = write(req->fd, req->dataptr, req->length); |
234 | req->result = pwrite(req->fd, req->dataptr, req->length, req->offset & 0xffffffff, req->offset >> 32); |
200 | } |
|
|
201 | } |
|
|
202 | else if (req->type == REQ_OPEN) |
235 | else if (req->type == REQ_OPEN) |
203 | { |
|
|
204 | req->result = open (req->dataptr, req->fd, req->mode); |
236 | req->result = open (req->dataptr, req->fd, req->mode); |
205 | } |
|
|
206 | else if (req->type == REQ_CLOSE) |
237 | else if (req->type == REQ_CLOSE) |
207 | { |
|
|
208 | req->result = close (req->fd); |
238 | req->result = close (req->fd); |
209 | } |
239 | else if (req->type == REQ_STAT) |
|
|
240 | req->result = stat64 (req->dataptr, req->statdata); |
|
|
241 | else if (req->type == REQ_LSTAT) |
|
|
242 | req->result = lstat64 (req->dataptr, req->statdata); |
|
|
243 | else if (req->type == REQ_FSTAT) |
|
|
244 | req->result = fstat64 (req->fd, req->statdata); |
210 | else |
245 | else |
211 | { |
246 | { |
212 | write (respipe[1], (void *)&req, sizeof (req)); |
247 | write (respipe[1], (void *)&req, sizeof (req)); |
213 | break; |
248 | break; |
214 | } |
249 | } |
… | |
… | |
255 | { |
290 | { |
256 | end_thread (); |
291 | end_thread (); |
257 | cur--; |
292 | cur--; |
258 | } |
293 | } |
259 | |
294 | |
260 | poll_cb (); |
|
|
261 | while (started > nthreads) |
295 | while (started > nthreads) |
262 | { |
296 | { |
263 | sched_yield (); |
297 | fd_set rfd; |
264 | fcntl (respipe[0], F_SETFL, 0); |
298 | FD_ZERO(&rfd); |
|
|
299 | FD_SET(respipe[0], &rfd); |
|
|
300 | |
|
|
301 | select (respipe[0] + 1, &rfd, 0, 0, 0); |
265 | poll_cb (); |
302 | poll_cb (); |
266 | fcntl (respipe[0], F_SETFL, O_NONBLOCK); |
|
|
267 | } |
303 | } |
268 | |
304 | |
269 | void |
305 | void |
|
|
306 | aio_open(pathname,flags,mode,callback) |
|
|
307 | SV * pathname |
|
|
308 | int flags |
|
|
309 | int mode |
|
|
310 | SV * callback |
|
|
311 | PROTOTYPE: $$$$ |
|
|
312 | CODE: |
|
|
313 | aio_req req; |
|
|
314 | |
|
|
315 | Newz (0, req, 1, aio_cb); |
|
|
316 | |
|
|
317 | if (!req) |
|
|
318 | croak ("out of memory during aio_req allocation"); |
|
|
319 | |
|
|
320 | req->type = REQ_OPEN; |
|
|
321 | req->data = newSVsv (pathname); |
|
|
322 | req->dataptr = SvPV_nolen (req->data); |
|
|
323 | req->fd = flags; |
|
|
324 | req->mode = mode; |
|
|
325 | req->callback = SvREFCNT_inc (callback); |
|
|
326 | |
|
|
327 | send_req (req); |
|
|
328 | |
|
|
329 | void |
|
|
330 | aio_close(fh,callback) |
|
|
331 | InputStream fh |
|
|
332 | SV * callback |
|
|
333 | PROTOTYPE: $$ |
|
|
334 | CODE: |
|
|
335 | aio_req req; |
|
|
336 | |
|
|
337 | Newz (0, req, 1, aio_cb); |
|
|
338 | |
|
|
339 | if (!req) |
|
|
340 | croak ("out of memory during aio_req allocation"); |
|
|
341 | |
|
|
342 | req->type = REQ_CLOSE; |
|
|
343 | req->fd = PerlIO_fileno (fh); |
|
|
344 | req->callback = SvREFCNT_inc (callback); |
|
|
345 | |
|
|
346 | send_req (req); |
|
|
347 | |
|
|
348 | void |
270 | aio_read(fh,offset,length,data,dataoffset,callback) |
349 | aio_read(fh,offset,length,data,dataoffset,callback) |
271 | InOutStream fh |
350 | InputStream fh |
272 | UV offset |
351 | UV offset |
273 | IV length |
352 | IV length |
274 | SV * data |
353 | SV * data |
275 | IV dataoffset |
354 | IV dataoffset |
276 | SV * callback |
355 | SV * callback |
277 | PROTOTYPE: $$$$$$ |
356 | PROTOTYPE: $$$$$$ |
278 | ALIAS: |
357 | CODE: |
279 | aio_write = 1 |
|
|
280 | CODE: |
|
|
281 | SvUPGRADE (data, SVt_PV); |
|
|
282 | SvPOK_on (data); |
|
|
283 | read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
358 | read_write (aTHX_ 0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
284 | |
359 | |
285 | void |
360 | void |
286 | aio_open(pathname,flags,mode,callback) |
361 | aio_write(fh,offset,length,data,dataoffset,callback) |
287 | char * pathname |
362 | OutputStream fh |
288 | int flags |
363 | UV offset |
289 | int mode |
364 | IV length |
|
|
365 | SV * data |
|
|
366 | IV dataoffset |
290 | SV * callback |
367 | SV * callback |
291 | PROTOTYPE: $$$$ |
368 | PROTOTYPE: $$$$$$ |
|
|
369 | CODE: |
|
|
370 | read_write (aTHX_ 1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
371 | |
|
|
372 | void |
|
|
373 | aio_stat(fh_or_path,callback) |
|
|
374 | SV * fh_or_path |
|
|
375 | SV * callback |
|
|
376 | PROTOTYPE: $$ |
|
|
377 | ALIAS: |
|
|
378 | aio_lstat = 1 |
292 | CODE: |
379 | CODE: |
293 | aio_req req; |
380 | aio_req req; |
294 | |
381 | |
295 | New (0, req, 1, aio_cb); |
382 | Newz (0, req, 1, aio_cb); |
296 | |
383 | |
297 | if (!req) |
384 | if (!req) |
298 | croak ("out of memory during aio_req allocation"); |
385 | croak ("out of memory during aio_req allocation"); |
299 | |
386 | |
300 | req->type = REQ_OPEN; |
387 | New (0, req->statdata, 1, struct stat64); |
301 | req->dataptr = pathname; |
|
|
302 | req->fd = flags; |
|
|
303 | req->mode = mode; |
|
|
304 | req->callback = SvREFCNT_inc (callback); |
|
|
305 | |
388 | |
306 | send_req (req); |
|
|
307 | |
|
|
308 | void |
|
|
309 | aio_close(fh,callback) |
|
|
310 | InOutStream fh |
|
|
311 | SV * callback |
|
|
312 | PROTOTYPE: $ |
|
|
313 | CODE: |
|
|
314 | aio_req req; |
|
|
315 | |
|
|
316 | New (0, req, 1, aio_cb); |
|
|
317 | |
|
|
318 | if (!req) |
389 | if (!req->statdata) |
319 | croak ("out of memory during aio_req allocation"); |
390 | croak ("out of memory during aio_req->statdata allocation"); |
320 | |
391 | |
|
|
392 | if (SvPOK (fh_or_path)) |
|
|
393 | { |
|
|
394 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
|
|
395 | req->data = newSVsv (fh_or_path); |
|
|
396 | req->dataptr = SvPV_nolen (req->data); |
|
|
397 | } |
|
|
398 | else |
|
|
399 | { |
321 | req->type = REQ_CLOSE; |
400 | req->type = REQ_FSTAT; |
322 | req->fd = PerlIO_fileno (fh); |
401 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
402 | } |
|
|
403 | |
323 | req->callback = SvREFCNT_inc (callback); |
404 | req->callback = SvREFCNT_inc (callback); |
324 | |
405 | |
325 | send_req (req); |
406 | send_req (req); |
326 | |
407 | |
327 | int |
408 | int |