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.28 by root, Wed Aug 17 05:06:59 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 atfork_parent ();
338
339 min_parallel (restart);
340}
341
342/*****************************************************************************/
343/* work around various missing functions */
344
345#if !HAVE_PREADWRITE
346# define pread aio_pread
347# define pwrite aio_pwrite
348
349/*
350 * make our pread/pwrite safe against themselves, but not against
351 * normal read/write by using a mutex. slows down execution a lot,
352 * but that's your problem, not mine.
353 */
354static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER;
355
356static ssize_t
357pread (int fd, void *buf, size_t count, off_t offset)
358{
359 ssize_t res;
360 off_t ooffset;
361
362 pthread_mutex_lock (&iolock);
363 ooffset = lseek (fd, 0, SEEK_CUR);
364 lseek (fd, offset, SEEK_SET);
365 res = read (fd, buf, count);
366 lseek (fd, ooffset, SEEK_SET);
367 pthread_mutex_unlock (&iolock);
368
369 return res;
370}
371
372static ssize_t
373pwrite (int fd, void *buf, size_t count, off_t offset)
374{
375 ssize_t res;
376 off_t ooffset;
377
378 pthread_mutex_lock (&iolock);
379 ooffset = lseek (fd, 0, SEEK_CUR);
380 lseek (fd, offset, SEEK_SET);
381 res = write (fd, buf, count);
382 lseek (fd, offset, SEEK_SET);
383 pthread_mutex_unlock (&iolock);
384
385 return res;
386}
387#endif
388
389#if !HAVE_FDATASYNC
390# define fdatasync fsync
391#endif
392
393#if !HAVE_READAHEAD
394# define readahead aio_readahead
395
396static char readahead_buf[4096];
397
398static ssize_t
399readahead (int fd, off_t offset, size_t count)
400{
401 while (count > 0)
264 } 402 {
403 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
265 404
266 if (length < 0) 405 pread (fd, readahead_buf, len, offset);
267 croak ("length must not be negative"); 406 offset += len;
407 count -= len;
408 }
268 409
269 Newz (0, req, 1, aio_cb); 410 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} 411}
412#endif
413
414/*****************************************************************************/
284 415
285static void * 416static void *
286aio_proc (void *thr_arg) 417aio_proc (void *thr_arg)
287{ 418{
288 aio_req req; 419 aio_req req;
316 447
317 switch (type) 448 switch (type)
318 { 449 {
319 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 450 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; 451 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
321#if SYS_readahead 452
322 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 453 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 454
327 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 455 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
328 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 456 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
329 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 457 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
330 458
331 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 459 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
332 case REQ_CLOSE: req->result = close (req->fd); break; 460 case REQ_CLOSE: req->result = close (req->fd); break;
333 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 461 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
462 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
463 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
334 464
465 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
335 case REQ_FSYNC: req->result = fsync (req->fd); break; 466 case REQ_FSYNC: req->result = fsync (req->fd); break;
336 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
337 467
338 case REQ_QUIT: 468 case REQ_QUIT:
339 break; 469 break;
340 470
341 default: 471 default:
367 while (type != REQ_QUIT); 497 while (type != REQ_QUIT);
368 498
369 return 0; 499 return 0;
370} 500}
371 501
502#define dREQ \
503 aio_req req; \
504 \
505 if (SvOK (callback) && !SvROK (callback)) \
506 croak ("clalback must be undef or of reference type"); \
507 \
508 Newz (0, req, 1, aio_cb); \
509 if (!req) \
510 croak ("out of memory during aio_req allocation"); \
511 \
512 req->callback = newSVsv (callback);
513
372MODULE = IO::AIO PACKAGE = IO::AIO 514MODULE = IO::AIO PACKAGE = IO::AIO
373 515
374PROTOTYPES: ENABLE 516PROTOTYPES: ENABLE
375 517
376BOOT: 518BOOT:
377{ 519{
378 if (pipe (respipe)) 520 create_pipe ();
379 croak ("unable to initialize result pipe"); 521 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} 522}
387 523
388void 524void
389min_parallel(nthreads) 525min_parallel(nthreads)
390 int nthreads 526 int nthreads
391 PROTOTYPE: $ 527 PROTOTYPE: $
392 CODE:
393 while (nthreads > started)
394 start_thread ();
395 528
396void 529void
397max_parallel(nthreads) 530max_parallel(nthreads)
398 int nthreads 531 int nthreads
399 PROTOTYPE: $ 532 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 533
416int 534int
417max_outstanding(nreqs) 535max_outstanding(nreqs)
418 int nreqs 536 int nreqs
419 PROTOTYPE: $ 537 PROTOTYPE: $
428 int mode 546 int mode
429 SV * callback 547 SV * callback
430 PROTOTYPE: $$$;$ 548 PROTOTYPE: $$$;$
431 CODE: 549 CODE:
432{ 550{
433 aio_req req; 551 dREQ;
434
435 Newz (0, req, 1, aio_cb);
436
437 if (!req)
438 croak ("out of memory during aio_req allocation");
439 552
440 req->type = REQ_OPEN; 553 req->type = REQ_OPEN;
441 req->data = newSVsv (pathname); 554 req->data = newSVsv (pathname);
442 req->dataptr = SvPV_nolen (req->data); 555 req->dataptr = SvPVbyte_nolen (req->data);
443 req->fd = flags; 556 req->fd = flags;
444 req->mode = mode; 557 req->mode = mode;
445 req->callback = SvREFCNT_inc (callback);
446 558
447 send_req (req); 559 send_req (req);
448} 560}
449 561
450void 562void
451aio_close(fh,callback=&PL_sv_undef) 563aio_close(fh,callback=&PL_sv_undef)
452 InputStream fh 564 SV * fh
453 SV * callback 565 SV * callback
454 PROTOTYPE: $;$ 566 PROTOTYPE: $;$
455 ALIAS: 567 ALIAS:
456 aio_close = REQ_CLOSE 568 aio_close = REQ_CLOSE
457 aio_fsync = REQ_FSYNC 569 aio_fsync = REQ_FSYNC
458 aio_fdatasync = REQ_FDATASYNC 570 aio_fdatasync = REQ_FDATASYNC
459 CODE: 571 CODE:
460{ 572{
461 aio_req req; 573 dREQ;
462
463 Newz (0, req, 1, aio_cb);
464
465 if (!req)
466 croak ("out of memory during aio_req allocation");
467 574
468 req->type = ix; 575 req->type = ix;
576 req->fh = newSVsv (fh);
469 req->fd = PerlIO_fileno (fh); 577 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
470 req->callback = SvREFCNT_inc (callback);
471 578
472 send_req (req); 579 send_req (req);
473} 580}
474 581
475void 582void
476aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 583aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
477 InputStream fh 584 SV * fh
478 UV offset 585 UV offset
479 IV length 586 IV length
480 SV * data 587 SV * data
481 IV dataoffset 588 IV dataoffset
482 SV * callback 589 SV * callback
590 ALIAS:
591 aio_read = REQ_READ
592 aio_write = REQ_WRITE
483 PROTOTYPE: $$$$$;$ 593 PROTOTYPE: $$$$$;$
484 CODE: 594 CODE:
485 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 595{
596 aio_req req;
597 STRLEN svlen;
598 char *svptr = SvPVbyte (data, svlen);
486 599
487void 600 SvUPGRADE (data, SVt_PV);
488aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 601 SvPOK_on (data);
489 OutputStream fh 602
490 UV offset 603 if (dataoffset < 0)
491 IV length 604 dataoffset += svlen;
492 SV * data 605
493 IV dataoffset 606 if (dataoffset < 0 || dataoffset > svlen)
494 SV * callback 607 croak ("data offset outside of string");
495 PROTOTYPE: $$$$$;$ 608
496 CODE: 609 if (ix == REQ_WRITE)
497 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); 610 {
611 /* write: check length and adjust. */
612 if (length < 0 || length + dataoffset > svlen)
613 length = svlen - dataoffset;
614 }
615 else
616 {
617 /* read: grow scalar as necessary */
618 svptr = SvGROW (data, length + dataoffset);
619 }
620
621 if (length < 0)
622 croak ("length must not be negative");
623
624 {
625 dREQ;
626
627 req->type = ix;
628 req->fh = newSVsv (fh);
629 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
630 : IoOFP (sv_2io (fh)));
631 req->offset = offset;
632 req->length = length;
633 req->data = SvREFCNT_inc (data);
634 req->dataptr = (char *)svptr + dataoffset;
635
636 if (!SvREADONLY (data))
637 {
638 SvREADONLY_on (data);
639 req->data2ptr = (void *)data;
640 }
641
642 send_req (req);
643 }
644}
498 645
499void 646void
500aio_readahead(fh,offset,length,callback=&PL_sv_undef) 647aio_readahead(fh,offset,length,callback=&PL_sv_undef)
501 InputStream fh 648 SV * fh
502 UV offset 649 UV offset
503 IV length 650 IV length
504 SV * callback 651 SV * callback
505 PROTOTYPE: $$$;$ 652 PROTOTYPE: $$$;$
506 CODE: 653 CODE:
507{ 654{
508 aio_req req; 655 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 656
518 req->type = REQ_READAHEAD; 657 req->type = REQ_READAHEAD;
658 req->fh = newSVsv (fh);
519 req->fd = PerlIO_fileno (fh); 659 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
520 req->offset = offset; 660 req->offset = offset;
521 req->length = length; 661 req->length = length;
522 req->callback = SvREFCNT_inc (callback);
523 662
524 send_req (req); 663 send_req (req);
525} 664}
526 665
527void 666void
531 ALIAS: 670 ALIAS:
532 aio_stat = REQ_STAT 671 aio_stat = REQ_STAT
533 aio_lstat = REQ_LSTAT 672 aio_lstat = REQ_LSTAT
534 CODE: 673 CODE:
535{ 674{
536 aio_req req; 675 dREQ;
537
538 Newz (0, req, 1, aio_cb);
539
540 if (!req)
541 croak ("out of memory during aio_req allocation");
542 676
543 New (0, req->statdata, 1, Stat_t); 677 New (0, req->statdata, 1, Stat_t);
544
545 if (!req->statdata) 678 if (!req->statdata)
679 {
680 free_req (req);
546 croak ("out of memory during aio_req->statdata allocation"); 681 croak ("out of memory during aio_req->statdata allocation");
682 }
547 683
548 if (SvPOK (fh_or_path)) 684 if (SvPOK (fh_or_path))
549 { 685 {
550 req->type = ix; 686 req->type = ix;
551 req->data = newSVsv (fh_or_path); 687 req->data = newSVsv (fh_or_path);
552 req->dataptr = SvPV_nolen (req->data); 688 req->dataptr = SvPVbyte_nolen (req->data);
553 } 689 }
554 else 690 else
555 { 691 {
556 req->type = REQ_FSTAT; 692 req->type = REQ_FSTAT;
693 req->fh = newSVsv (fh_or_path);
557 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 694 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
558 } 695 }
559
560 req->callback = SvREFCNT_inc (callback);
561 696
562 send_req (req); 697 send_req (req);
563} 698}
564 699
565void 700void
566aio_unlink(pathname,callback=&PL_sv_undef) 701aio_unlink(pathname,callback=&PL_sv_undef)
567 SV * pathname 702 SV * pathname
568 SV * callback 703 SV * callback
704 ALIAS:
705 aio_unlink = REQ_UNLINK
706 aio_rmdir = REQ_RMDIR
569 CODE: 707 CODE:
570{ 708{
571 aio_req req; 709 dREQ;
572 710
573 Newz (0, req, 1, aio_cb); 711 req->type = ix;
712 req->data = newSVsv (pathname);
713 req->dataptr = SvPVbyte_nolen (req->data);
574 714
575 if (!req) 715 send_req (req);
576 croak ("out of memory during aio_req allocation"); 716}
717
718void
719aio_symlink(oldpath,newpath,callback=&PL_sv_undef)
720 SV * oldpath
721 SV * newpath
722 SV * callback
723 CODE:
724{
725 dREQ;
577 726
578 req->type = REQ_UNLINK; 727 req->type = REQ_SYMLINK;
728 req->fh = newSVsv (oldpath);
729 req->data2ptr = SvPVbyte_nolen (req->fh);
579 req->data = newSVsv (pathname); 730 req->data = newSVsv (newpath);
580 req->dataptr = SvPV_nolen (req->data); 731 req->dataptr = SvPVbyte_nolen (req->data);
581 req->callback = SvREFCNT_inc (callback);
582 732
583 send_req (req); 733 send_req (req);
584} 734}
585 735
586void 736void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines