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