ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.11 by root, Wed Jul 20 21:55:27 2005 UTC vs.
Revision 1.29 by root, Wed Aug 17 06:12:10 2005 UTC

1#define _XOPEN_SOURCE 500 1#define _REENTRANT 1
2#include <errno.h>
2 3
3#include "EXTERN.h" 4#include "EXTERN.h"
4#include "perl.h" 5#include "perl.h"
5#include "XSUB.h" 6#include "XSUB.h"
7
8#include "autoconf/config.h"
6 9
7#include <sys/types.h> 10#include <sys/types.h>
8#include <sys/stat.h> 11#include <sys/stat.h>
9 12
10#include <unistd.h> 13#include <unistd.h>
11#include <fcntl.h> 14#include <fcntl.h>
12#include <signal.h> 15#include <signal.h>
13#include <sched.h> 16#include <sched.h>
14#if __linux
15#include <sys/syscall.h>
16#endif
17 17
18#include <pthread.h> 18#include <pthread.h>
19 19
20typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 20typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
21typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 21typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
29 29
30enum { 30enum {
31 REQ_QUIT, 31 REQ_QUIT,
32 REQ_OPEN, REQ_CLOSE, 32 REQ_OPEN, REQ_CLOSE,
33 REQ_READ, REQ_WRITE, REQ_READAHEAD, 33 REQ_READ, REQ_WRITE, REQ_READAHEAD,
34 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, 34 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
35 REQ_FSYNC, REQ_FDATASYNC, 35 REQ_FSYNC, REQ_FDATASYNC,
36 REQ_UNLINK, REQ_RMDIR,
37 REQ_SYMLINK,
36}; 38};
37 39
38typedef struct aio_cb { 40typedef struct aio_cb {
39 struct aio_cb *volatile next; 41 struct aio_cb *volatile next;
40 42
44 off_t offset; 46 off_t offset;
45 size_t length; 47 size_t length;
46 ssize_t result; 48 ssize_t result;
47 mode_t mode; /* open */ 49 mode_t mode; /* open */
48 int errorno; 50 int errorno;
49 SV *data, *callback; 51 SV *data, *callback, *fh;
50 void *dataptr; 52 void *dataptr, *data2ptr;
51 STRLEN dataoffset; 53 STRLEN dataoffset;
52 54
53 Stat_t *statdata; 55 Stat_t *statdata;
54} aio_cb; 56} aio_cb;
55 57
64static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
65static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 67static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
66 68
67static volatile aio_req reqs, reqe; /* queue start, queue end */ 69static volatile aio_req reqs, reqe; /* queue start, queue end */
68static volatile aio_req ress, rese; /* queue start, queue end */ 70static volatile aio_req ress, rese; /* queue start, queue end */
71
72static void free_req (aio_req req)
73{
74 if (req->data)
75 SvREFCNT_dec (req->data);
76
77 if (req->fh)
78 SvREFCNT_dec (req->fh);
79
80 if (req->statdata)
81 Safefree (req->statdata);
82
83 if (req->callback)
84 SvREFCNT_dec (req->callback);
85
86 Safefree (req);
87}
69 88
70static void 89static void
71poll_wait () 90poll_wait ()
72{ 91{
73 if (nreqs && !ress) 92 if (nreqs && !ress)
83static int 102static int
84poll_cb () 103poll_cb ()
85{ 104{
86 dSP; 105 dSP;
87 int count = 0; 106 int count = 0;
107 int do_croak = 0;
88 aio_req req, prv; 108 aio_req req;
89 109
90 static int rl;//D 110 for (;;)
91 //printf ("%d ENTER\n", ++rl);//D
92
93 pthread_mutex_lock (&reslock);
94
95 {
96 /* read any signals sent by the worker threads */
97 char buf [32];
98 while (read (respipe [0], buf, 32) > 0)
99 ;
100 }
101
102 req = ress;
103 ress = rese = 0;
104
105 pthread_mutex_unlock (&reslock);
106
107 while (req)
108 { 111 {
112 pthread_mutex_lock (&reslock);
113 req = ress;
114
115 if (req)
116 {
117 ress = req->next;
118
119 if (!ress)
120 {
121 rese = 0;
122
123 /* read any signals sent by the worker threads */
124 char buf [32];
125 while (read (respipe [0], buf, 32) == 32)
126 ;
127 }
128 }
129
130 pthread_mutex_unlock (&reslock);
131
132 if (!req)
133 break;
134
109 nreqs--; 135 nreqs--;
110 //printf ("%d count %d %p->%p\n", rl, count, req, req->next);//D
111 136
112 if (req->type == REQ_QUIT) 137 if (req->type == REQ_QUIT)
113 started--; 138 started--;
114 else 139 else
115 { 140 {
116 int errorno = errno; 141 int errorno = errno;
117 errno = req->errorno; 142 errno = req->errorno;
118 143
119 if (req->type == REQ_READ) 144 if (req->type == REQ_READ)
120 SvCUR_set (req->data, req->dataoffset 145 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
121 + req->result > 0 ? req->result : 0);
122 146
147 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
148 SvREADONLY_off (req->data);
149
123 if (req->data) 150 if (req->statdata)
124 SvREFCNT_dec (req->data);
125
126 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
127 { 151 {
128 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 152 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
129 PL_laststatval = req->result; 153 PL_laststatval = req->result;
130 PL_statcache = *(req->statdata); 154 PL_statcache = *(req->statdata);
131
132 Safefree (req->statdata);
133 } 155 }
134 156
157 ENTER;
135 PUSHMARK (SP); 158 PUSHMARK (SP);
136 XPUSHs (sv_2mortal (newSViv (req->result))); 159 XPUSHs (sv_2mortal (newSViv (req->result)));
137 160
138 if (req->type == REQ_OPEN) 161 if (req->type == REQ_OPEN)
139 { 162 {
142 165
143 PUTBACK; 166 PUTBACK;
144 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 167 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
145 SPAGAIN; 168 SPAGAIN;
146 169
147 fh = POPs; 170 fh = SvREFCNT_inc (POPs);
148 171
149 PUSHMARK (SP); 172 PUSHMARK (SP);
150 XPUSHs (fh); 173 XPUSHs (sv_2mortal (fh));
151 } 174 }
152 175
153 if (SvOK (req->callback)) 176 if (SvOK (req->callback))
154 { 177 {
155 PUTBACK; 178 PUTBACK;
156 call_sv (req->callback, G_VOID | G_EVAL); 179 call_sv (req->callback, G_VOID | G_EVAL);
157 SPAGAIN; 180 SPAGAIN;
181
182 if (SvTRUE (ERRSV))
183 {
184 free_req (req);
185 croak (0);
186 }
158 } 187 }
188
159 189 LEAVE;
160 if (req->callback)
161 SvREFCNT_dec (req->callback);
162 190
163 errno = errorno; 191 errno = errorno;
164 count++; 192 count++;
165 } 193 }
166 194
167 prv = req; 195 free_req (req);
168 req = req->next;
169 Safefree (prv);
170
171 /* TODO: croak on errors? */
172 } 196 }
173 197
174 //printf ("%d LEAVE %p %p\n", rl--, ress, rese);//D
175 return count; 198 return count;
176} 199}
177 200
178static void *aio_proc(void *arg); 201static void *aio_proc(void *arg);
179 202
215 reqe = reqs = req; 238 reqe = reqs = req;
216 239
217 pthread_cond_signal (&reqwait); 240 pthread_cond_signal (&reqwait);
218 pthread_mutex_unlock (&reqlock); 241 pthread_mutex_unlock (&reqlock);
219 242
220 while (nreqs > max_outstanding) 243 if (nreqs > max_outstanding)
244 for (;;)
245 {
246 poll_cb ();
247
248 if (nreqs <= max_outstanding)
249 break;
250
251 poll_wait ();
252 }
253}
254
255static void
256end_thread (void)
257{
258 aio_req req;
259 Newz (0, req, 1, aio_cb);
260 req->type = REQ_QUIT;
261
262 send_req (req);
263}
264
265static void min_parallel (int nthreads)
266{
267 while (nthreads > started)
268 start_thread ();
269}
270
271static void max_parallel (int nthreads)
272{
273 int cur = started;
274
275 while (cur > nthreads)
276 {
277 end_thread ();
278 cur--;
279 }
280
281 while (started > nthreads)
221 { 282 {
222 poll_wait (); 283 poll_wait ();
223 poll_cb (); 284 poll_cb ();
224 } 285 }
225} 286}
226 287
227static void 288static void create_pipe ()
228end_thread (void)
229{ 289{
290 if (pipe (respipe))
291 croak ("unable to initialize result pipe");
292
293 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
294 croak ("cannot set result pipe to nonblocking mode");
295
296 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
297 croak ("cannot set result pipe to nonblocking mode");
298}
299
300static void atfork_prepare (void)
301{
302 pthread_mutex_lock (&reqlock);
303 pthread_mutex_lock (&reslock);
304}
305
306static void atfork_parent (void)
307{
308 pthread_mutex_unlock (&reslock);
309 pthread_mutex_unlock (&reqlock);
310}
311
312static void atfork_child (void)
313{
230 aio_req req; 314 aio_req prv;
231 New (0, req, 1, aio_cb);
232 req->type = REQ_QUIT;
233 315
234 send_req (req); 316 int restart = started;
235} 317 started = 0;
236 318
237static void 319 while (reqs)
238read_write (int dowrite, int fd, off_t offset, size_t length,
239 SV *data, STRLEN dataoffset, SV *callback)
240{
241 aio_req req;
242 STRLEN svlen;
243 char *svptr = SvPV (data, svlen);
244
245 SvUPGRADE (data, SVt_PV);
246 SvPOK_on (data);
247
248 if (dataoffset < 0)
249 dataoffset += svlen;
250
251 if (dataoffset < 0 || dataoffset > svlen)
252 croak ("data offset outside of string");
253
254 if (dowrite)
255 { 320 {
256 /* write: check length and adjust. */ 321 prv = reqs;
257 if (length < 0 || length + dataoffset > svlen) 322 reqs = prv->next;
258 length = svlen - dataoffset; 323 free_req (prv);
259 } 324 }
260 else 325
326 reqs = reqe = 0;
327
328 while (ress)
261 { 329 {
262 /* read: grow scalar as necessary */ 330 prv = ress;
263 svptr = SvGROW (data, length + dataoffset); 331 ress = prv->next;
332 free_req (prv);
333 }
334
335 ress = rese = 0;
336
337 close (respipe [0]);
338 close (respipe [1]);
339 create_pipe ();
340
341 atfork_parent ();
342
343 min_parallel (restart);
344}
345
346/*****************************************************************************/
347/* work around various missing functions */
348
349#if !HAVE_PREADWRITE
350# define pread aio_pread
351# define pwrite aio_pwrite
352
353/*
354 * make our pread/pwrite safe against themselves, but not against
355 * normal read/write by using a mutex. slows down execution a lot,
356 * but that's your problem, not mine.
357 */
358static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER;
359
360static ssize_t
361pread (int fd, void *buf, size_t count, off_t offset)
362{
363 ssize_t res;
364 off_t ooffset;
365
366 pthread_mutex_lock (&iolock);
367 ooffset = lseek (fd, 0, SEEK_CUR);
368 lseek (fd, offset, SEEK_SET);
369 res = read (fd, buf, count);
370 lseek (fd, ooffset, SEEK_SET);
371 pthread_mutex_unlock (&iolock);
372
373 return res;
374}
375
376static ssize_t
377pwrite (int fd, void *buf, size_t count, off_t offset)
378{
379 ssize_t res;
380 off_t ooffset;
381
382 pthread_mutex_lock (&iolock);
383 ooffset = lseek (fd, 0, SEEK_CUR);
384 lseek (fd, offset, SEEK_SET);
385 res = write (fd, buf, count);
386 lseek (fd, offset, SEEK_SET);
387 pthread_mutex_unlock (&iolock);
388
389 return res;
390}
391#endif
392
393#if !HAVE_FDATASYNC
394# define fdatasync fsync
395#endif
396
397#if !HAVE_READAHEAD
398# define readahead aio_readahead
399
400static char readahead_buf[4096];
401
402static ssize_t
403readahead (int fd, off_t offset, size_t count)
404{
405 while (count > 0)
264 } 406 {
407 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
265 408
266 if (length < 0) 409 pread (fd, readahead_buf, len, offset);
267 croak ("length must not be negative"); 410 offset += len;
411 count -= len;
412 }
268 413
269 Newz (0, req, 1, aio_cb); 414 errno = 0;
270
271 if (!req)
272 croak ("out of memory during aio_req allocation");
273
274 req->type = dowrite ? REQ_WRITE : REQ_READ;
275 req->fd = fd;
276 req->offset = offset;
277 req->length = length;
278 req->data = SvREFCNT_inc (data);
279 req->dataptr = (char *)svptr + dataoffset;
280 req->callback = SvREFCNT_inc (callback);
281
282 send_req (req);
283} 415}
416#endif
417
418/*****************************************************************************/
284 419
285static void * 420static void *
286aio_proc (void *thr_arg) 421aio_proc (void *thr_arg)
287{ 422{
288 aio_req req; 423 aio_req req;
316 451
317 switch (type) 452 switch (type)
318 { 453 {
319 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 454 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
320 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 455 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
321#if SYS_readahead 456
322 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 457 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
323#else
324 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
325#endif
326 458
327 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 459 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
328 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 460 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
329 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 461 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
330 462
331 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 463 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
332 case REQ_CLOSE: req->result = close (req->fd); break; 464 case REQ_CLOSE: req->result = close (req->fd); break;
333 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 465 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
466 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
467 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
334 468
469 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
335 case REQ_FSYNC: req->result = fsync (req->fd); break; 470 case REQ_FSYNC: req->result = fsync (req->fd); break;
336 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
337 471
338 case REQ_QUIT: 472 case REQ_QUIT:
339 break; 473 break;
340 474
341 default: 475 default:
367 while (type != REQ_QUIT); 501 while (type != REQ_QUIT);
368 502
369 return 0; 503 return 0;
370} 504}
371 505
506#define dREQ \
507 aio_req req; \
508 \
509 if (SvOK (callback) && !SvROK (callback)) \
510 croak ("clalback must be undef or of reference type"); \
511 \
512 Newz (0, req, 1, aio_cb); \
513 if (!req) \
514 croak ("out of memory during aio_req allocation"); \
515 \
516 req->callback = newSVsv (callback);
517
372MODULE = IO::AIO PACKAGE = IO::AIO 518MODULE = IO::AIO PACKAGE = IO::AIO
373 519
374PROTOTYPES: ENABLE 520PROTOTYPES: ENABLE
375 521
376BOOT: 522BOOT:
377{ 523{
378 if (pipe (respipe)) 524 create_pipe ();
379 croak ("unable to initialize result pipe"); 525 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
380
381 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
382 croak ("cannot set result pipe to nonblocking mode");
383
384 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
385 croak ("cannot set result pipe to nonblocking mode");
386} 526}
387 527
388void 528void
389min_parallel(nthreads) 529min_parallel(nthreads)
390 int nthreads 530 int nthreads
391 PROTOTYPE: $ 531 PROTOTYPE: $
392 CODE:
393 while (nthreads > started)
394 start_thread ();
395 532
396void 533void
397max_parallel(nthreads) 534max_parallel(nthreads)
398 int nthreads 535 int nthreads
399 PROTOTYPE: $ 536 PROTOTYPE: $
400 CODE:
401{
402 int cur = started;
403 while (cur > nthreads)
404 {
405 end_thread ();
406 cur--;
407 }
408
409 while (started > nthreads)
410 {
411 poll_wait ();
412 poll_cb ();
413 }
414}
415 537
416int 538int
417max_outstanding(nreqs) 539max_outstanding(nreqs)
418 int nreqs 540 int nreqs
419 PROTOTYPE: $ 541 PROTOTYPE: $
428 int mode 550 int mode
429 SV * callback 551 SV * callback
430 PROTOTYPE: $$$;$ 552 PROTOTYPE: $$$;$
431 CODE: 553 CODE:
432{ 554{
433 aio_req req; 555 dREQ;
434
435 Newz (0, req, 1, aio_cb);
436
437 if (!req)
438 croak ("out of memory during aio_req allocation");
439 556
440 req->type = REQ_OPEN; 557 req->type = REQ_OPEN;
441 req->data = newSVsv (pathname); 558 req->data = newSVsv (pathname);
442 req->dataptr = SvPV_nolen (req->data); 559 req->dataptr = SvPVbyte_nolen (req->data);
443 req->fd = flags; 560 req->fd = flags;
444 req->mode = mode; 561 req->mode = mode;
445 req->callback = SvREFCNT_inc (callback);
446 562
447 send_req (req); 563 send_req (req);
448} 564}
449 565
450void 566void
451aio_close(fh,callback=&PL_sv_undef) 567aio_close(fh,callback=&PL_sv_undef)
452 InputStream fh 568 SV * fh
453 SV * callback 569 SV * callback
454 PROTOTYPE: $;$ 570 PROTOTYPE: $;$
455 ALIAS: 571 ALIAS:
456 aio_close = REQ_CLOSE 572 aio_close = REQ_CLOSE
457 aio_fsync = REQ_FSYNC 573 aio_fsync = REQ_FSYNC
458 aio_fdatasync = REQ_FDATASYNC 574 aio_fdatasync = REQ_FDATASYNC
459 CODE: 575 CODE:
460{ 576{
461 aio_req req; 577 dREQ;
462
463 Newz (0, req, 1, aio_cb);
464
465 if (!req)
466 croak ("out of memory during aio_req allocation");
467 578
468 req->type = ix; 579 req->type = ix;
580 req->fh = newSVsv (fh);
469 req->fd = PerlIO_fileno (fh); 581 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
470 req->callback = SvREFCNT_inc (callback);
471 582
472 send_req (req); 583 send_req (req);
473} 584}
474 585
475void 586void
476aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 587aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
477 InputStream fh 588 SV * fh
478 UV offset 589 UV offset
479 IV length 590 IV length
480 SV * data 591 SV * data
481 IV dataoffset 592 IV dataoffset
482 SV * callback 593 SV * callback
594 ALIAS:
595 aio_read = REQ_READ
596 aio_write = REQ_WRITE
483 PROTOTYPE: $$$$$;$ 597 PROTOTYPE: $$$$$;$
484 CODE: 598 CODE:
485 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 599{
600 aio_req req;
601 STRLEN svlen;
602 char *svptr = SvPVbyte (data, svlen);
486 603
487void 604 SvUPGRADE (data, SVt_PV);
488aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 605 SvPOK_on (data);
489 OutputStream fh 606
490 UV offset 607 if (dataoffset < 0)
491 IV length 608 dataoffset += svlen;
492 SV * data 609
493 IV dataoffset 610 if (dataoffset < 0 || dataoffset > svlen)
494 SV * callback 611 croak ("data offset outside of string");
495 PROTOTYPE: $$$$$;$ 612
496 CODE: 613 if (ix == REQ_WRITE)
497 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 614 {
615 /* write: check length and adjust. */
616 if (length < 0 || length + dataoffset > svlen)
617 length = svlen - dataoffset;
618 }
619 else
620 {
621 /* read: grow scalar as necessary */
622 svptr = SvGROW (data, length + dataoffset);
623 }
624
625 if (length < 0)
626 croak ("length must not be negative");
627
628 {
629 dREQ;
630
631 req->type = ix;
632 req->fh = newSVsv (fh);
633 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
634 : IoOFP (sv_2io (fh)));
635 req->offset = offset;
636 req->length = length;
637 req->data = SvREFCNT_inc (data);
638 req->dataptr = (char *)svptr + dataoffset;
639
640 if (!SvREADONLY (data))
641 {
642 SvREADONLY_on (data);
643 req->data2ptr = (void *)data;
644 }
645
646 send_req (req);
647 }
648}
498 649
499void 650void
500aio_readahead(fh,offset,length,callback=&PL_sv_undef) 651aio_readahead(fh,offset,length,callback=&PL_sv_undef)
501 InputStream fh 652 SV * fh
502 UV offset 653 UV offset
503 IV length 654 IV length
504 SV * callback 655 SV * callback
505 PROTOTYPE: $$$;$ 656 PROTOTYPE: $$$;$
506 CODE: 657 CODE:
507{ 658{
508 aio_req req; 659 dREQ;
509
510 if (length < 0)
511 croak ("length must not be negative");
512
513 Newz (0, req, 1, aio_cb);
514
515 if (!req)
516 croak ("out of memory during aio_req allocation");
517 660
518 req->type = REQ_READAHEAD; 661 req->type = REQ_READAHEAD;
662 req->fh = newSVsv (fh);
519 req->fd = PerlIO_fileno (fh); 663 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
520 req->offset = offset; 664 req->offset = offset;
521 req->length = length; 665 req->length = length;
522 req->callback = SvREFCNT_inc (callback);
523 666
524 send_req (req); 667 send_req (req);
525} 668}
526 669
527void 670void
531 ALIAS: 674 ALIAS:
532 aio_stat = REQ_STAT 675 aio_stat = REQ_STAT
533 aio_lstat = REQ_LSTAT 676 aio_lstat = REQ_LSTAT
534 CODE: 677 CODE:
535{ 678{
536 aio_req req; 679 dREQ;
537
538 Newz (0, req, 1, aio_cb);
539
540 if (!req)
541 croak ("out of memory during aio_req allocation");
542 680
543 New (0, req->statdata, 1, Stat_t); 681 New (0, req->statdata, 1, Stat_t);
544
545 if (!req->statdata) 682 if (!req->statdata)
683 {
684 free_req (req);
546 croak ("out of memory during aio_req->statdata allocation"); 685 croak ("out of memory during aio_req->statdata allocation");
686 }
547 687
548 if (SvPOK (fh_or_path)) 688 if (SvPOK (fh_or_path))
549 { 689 {
550 req->type = ix; 690 req->type = ix;
551 req->data = newSVsv (fh_or_path); 691 req->data = newSVsv (fh_or_path);
552 req->dataptr = SvPV_nolen (req->data); 692 req->dataptr = SvPVbyte_nolen (req->data);
553 } 693 }
554 else 694 else
555 { 695 {
556 req->type = REQ_FSTAT; 696 req->type = REQ_FSTAT;
697 req->fh = newSVsv (fh_or_path);
557 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 698 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
558 } 699 }
559
560 req->callback = SvREFCNT_inc (callback);
561 700
562 send_req (req); 701 send_req (req);
563} 702}
564 703
565void 704void
566aio_unlink(pathname,callback=&PL_sv_undef) 705aio_unlink(pathname,callback=&PL_sv_undef)
567 SV * pathname 706 SV * pathname
568 SV * callback 707 SV * callback
708 ALIAS:
709 aio_unlink = REQ_UNLINK
710 aio_rmdir = REQ_RMDIR
569 CODE: 711 CODE:
570{ 712{
571 aio_req req; 713 dREQ;
572 714
573 Newz (0, req, 1, aio_cb); 715 req->type = ix;
716 req->data = newSVsv (pathname);
717 req->dataptr = SvPVbyte_nolen (req->data);
574 718
575 if (!req) 719 send_req (req);
576 croak ("out of memory during aio_req allocation"); 720}
721
722void
723aio_symlink(oldpath,newpath,callback=&PL_sv_undef)
724 SV * oldpath
725 SV * newpath
726 SV * callback
727 CODE:
728{
729 dREQ;
577 730
578 req->type = REQ_UNLINK; 731 req->type = REQ_SYMLINK;
732 req->fh = newSVsv (oldpath);
733 req->data2ptr = SvPVbyte_nolen (req->fh);
579 req->data = newSVsv (pathname); 734 req->data = newSVsv (newpath);
580 req->dataptr = SvPV_nolen (req->data); 735 req->dataptr = SvPVbyte_nolen (req->data);
581 req->callback = SvREFCNT_inc (callback);
582 736
583 send_req (req); 737 send_req (req);
584} 738}
585 739
586void 740void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines