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.6 by root, Mon Jul 11 01:03:17 2005 UTC vs.
Revision 1.34 by root, Tue Aug 23 00:03:14 2005 UTC

1#define _REENTRANT 1
2#include <errno.h>
3
1#include "EXTERN.h" 4#include "EXTERN.h"
2#include "perl.h" 5#include "perl.h"
3#include "XSUB.h" 6#include "XSUB.h"
4 7
8#include "autoconf/config.h"
9
10#include <pthread.h>
11
5#include <sys/types.h> 12#include <sys/types.h>
6#include <sys/stat.h> 13#include <sys/stat.h>
14
7#include <unistd.h> 15#include <unistd.h>
8#include <fcntl.h> 16#include <fcntl.h>
9#include <signal.h> 17#include <signal.h>
10#include <sched.h> 18#include <sched.h>
11#include <endian.h>
12 19
13#include <pthread.h> 20#if HAVE_SENDFILE
21# if __linux
22# include <sys/sendfile.h>
23# elif __freebsd
14#include <sys/syscall.h> 24# include <sys/socket.h>
15 25# include <sys/uio.h>
16typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 26# elif __hpux
17typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 27# include <sys/socket.h>
18typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ 28# else
29# error sendfile support requested but not available
30# endif
31#endif
19 32
20#if __ia64 33#if __ia64
21# define STACKSIZE 65536 34# define STACKSIZE 65536
22#else 35#else
23# define STACKSIZE 4096 36# define STACKSIZE 4096
25 38
26enum { 39enum {
27 REQ_QUIT, 40 REQ_QUIT,
28 REQ_OPEN, REQ_CLOSE, 41 REQ_OPEN, REQ_CLOSE,
29 REQ_READ, REQ_WRITE, REQ_READAHEAD, 42 REQ_READ, REQ_WRITE, REQ_READAHEAD,
43 REQ_SENDFILE,
30 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, 44 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
31 REQ_FSYNC, REQ_FDATASYNC, 45 REQ_FSYNC, REQ_FDATASYNC,
46 REQ_UNLINK, REQ_RMDIR,
47 REQ_SYMLINK,
32}; 48};
33 49
34typedef struct aio_cb { 50typedef struct aio_cb {
35 struct aio_cb *volatile next; 51 struct aio_cb *volatile next;
36 52
37 int type; 53 int type;
38 54
39 int fd; 55 int fd, fd2;
40 off_t offset; 56 off_t offset;
41 size_t length; 57 size_t length;
42 ssize_t result; 58 ssize_t result;
43 mode_t mode; /* open */ 59 mode_t mode; /* open */
44 int errorno; 60 int errorno;
45 SV *data, *callback; 61 SV *data, *callback;
46 void *dataptr; 62 SV *fh, *fh2;
63 void *dataptr, *data2ptr;
47 STRLEN dataoffset; 64 STRLEN dataoffset;
48 65
49 Stat_t *statdata; 66 Stat_t *statdata;
50} aio_cb; 67} aio_cb;
51 68
52typedef aio_cb *aio_req; 69typedef aio_cb *aio_req;
53 70
54static int started; 71static int started, wanted;
55static int nreqs; 72static volatile int nreqs;
56static int max_outstanding = 1<<30; 73static int max_outstanding = 1<<30;
57static int respipe [2]; 74static int respipe [2];
58 75
59static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 76static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
60static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 77static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
61static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 78static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
62 79
63static volatile aio_req reqs, reqe; /* queue start, queue end */ 80static volatile aio_req reqs, reqe; /* queue start, queue end */
64static volatile aio_req ress, rese; /* queue start, queue end */ 81static volatile aio_req ress, rese; /* queue start, queue end */
65 82
83static void free_req (aio_req req)
84{
85 if (req->data)
86 SvREFCNT_dec (req->data);
87
88 if (req->fh)
89 SvREFCNT_dec (req->fh);
90
91 if (req->fh2)
92 SvREFCNT_dec (req->fh2);
93
94 if (req->statdata)
95 Safefree (req->statdata);
96
97 if (req->callback)
98 SvREFCNT_dec (req->callback);
99
100 Safefree (req);
101}
102
66static void 103static void
67poll_wait () 104poll_wait ()
68{ 105{
69 if (!nreqs) 106 if (nreqs && !ress)
70 return; 107 {
71
72 fd_set rfd; 108 fd_set rfd;
73 FD_ZERO(&rfd); 109 FD_ZERO(&rfd);
74 FD_SET(respipe [0], &rfd); 110 FD_SET(respipe [0], &rfd);
75 111
76 select (respipe [0] + 1, &rfd, 0, 0, 0); 112 select (respipe [0] + 1, &rfd, 0, 0, 0);
113 }
77} 114}
78 115
79static int 116static int
80poll_cb () 117poll_cb ()
81{ 118{
82 dSP; 119 dSP;
83 int count = 0; 120 int count = 0;
121 int do_croak = 0;
84 aio_req req; 122 aio_req req;
85
86 {
87 /* read and signals sent by the worker threads */
88 char buf [32];
89 while (read (respipe [0], buf, 32) > 0)
90 ;
91 }
92 123
93 for (;;) 124 for (;;)
94 { 125 {
95 pthread_mutex_lock (&reslock); 126 pthread_mutex_lock (&reslock);
96
97 req = ress; 127 req = ress;
98 128
99 if (ress) 129 if (req)
100 { 130 {
101 ress = ress->next; 131 ress = req->next;
132
102 if (!ress) rese = 0; 133 if (!ress)
134 {
135 /* read any signals sent by the worker threads */
136 char buf [32];
137 while (read (respipe [0], buf, 32) == 32)
138 ;
139
140 rese = 0;
141 }
103 } 142 }
104 143
105 pthread_mutex_unlock (&reslock); 144 pthread_mutex_unlock (&reslock);
106 145
107 if (!req) 146 if (!req)
115 { 154 {
116 int errorno = errno; 155 int errorno = errno;
117 errno = req->errorno; 156 errno = req->errorno;
118 157
119 if (req->type == REQ_READ) 158 if (req->type == REQ_READ)
120 SvCUR_set (req->data, req->dataoffset 159 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
121 + req->result > 0 ? req->result : 0);
122 160
161 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
162 SvREADONLY_off (req->data);
163
123 if (req->data) 164 if (req->statdata)
124 SvREFCNT_dec (req->data);
125
126 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
127 { 165 {
128 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 166 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
129 PL_laststatval = req->result; 167 PL_laststatval = req->result;
130 PL_statcache = *(req->statdata); 168 PL_statcache = *(req->statdata);
131
132 Safefree (req->statdata);
133 } 169 }
134 170
171 ENTER;
135 PUSHMARK (SP); 172 PUSHMARK (SP);
136 XPUSHs (sv_2mortal (newSViv (req->result))); 173 XPUSHs (sv_2mortal (newSViv (req->result)));
137 174
138 if (req->type == REQ_OPEN) 175 if (req->type == REQ_OPEN)
139 { 176 {
142 179
143 PUTBACK; 180 PUTBACK;
144 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 181 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
145 SPAGAIN; 182 SPAGAIN;
146 183
147 fh = POPs; 184 fh = SvREFCNT_inc (POPs);
148 185
149 PUSHMARK (SP); 186 PUSHMARK (SP);
150 XPUSHs (fh); 187 XPUSHs (sv_2mortal (fh));
151 } 188 }
152 189
190 if (SvOK (req->callback))
191 {
153 PUTBACK; 192 PUTBACK;
154 call_sv (req->callback, G_VOID | G_EVAL); 193 call_sv (req->callback, G_VOID | G_EVAL);
155 SPAGAIN; 194 SPAGAIN;
195
196 if (SvTRUE (ERRSV))
197 {
198 free_req (req);
199 croak (0);
200 }
156 201 }
157 if (req->callback) 202
158 SvREFCNT_dec (req->callback); 203 LEAVE;
159 204
160 errno = errorno; 205 errno = errorno;
161 count++; 206 count++;
162 } 207 }
163 208
164 Safefree (req); 209 free_req (req);
165 } 210 }
166 211
167 return count; 212 return count;
168} 213}
169 214
190} 235}
191 236
192static void 237static void
193send_req (aio_req req) 238send_req (aio_req req)
194{ 239{
240 while (started < wanted && nreqs >= started)
241 start_thread ();
242
195 nreqs++; 243 nreqs++;
196 244
197 pthread_mutex_lock (&reqlock); 245 pthread_mutex_lock (&reqlock);
198 246
199 req->next = 0; 247 req->next = 0;
207 reqe = reqs = req; 255 reqe = reqs = req;
208 256
209 pthread_cond_signal (&reqwait); 257 pthread_cond_signal (&reqwait);
210 pthread_mutex_unlock (&reqlock); 258 pthread_mutex_unlock (&reqlock);
211 259
212 while (nreqs > max_outstanding) 260 if (nreqs > max_outstanding)
261 for (;;)
262 {
263 poll_cb ();
264
265 if (nreqs <= max_outstanding)
266 break;
267
268 poll_wait ();
269 }
270}
271
272static void
273end_thread (void)
274{
275 aio_req req;
276 Newz (0, req, 1, aio_cb);
277 req->type = REQ_QUIT;
278
279 send_req (req);
280}
281
282static void min_parallel (int nthreads)
283{
284 if (wanted < nthreads)
285 wanted = nthreads;
286}
287
288static void max_parallel (int nthreads)
289{
290 int cur = started;
291
292 if (wanted > nthreads)
293 wanted = nthreads;
294
295 while (cur > wanted)
296 {
297 end_thread ();
298 cur--;
299 }
300
301 while (started > wanted)
213 { 302 {
214 poll_wait (); 303 poll_wait ();
215 poll_cb (); 304 poll_cb ();
216 } 305 }
217} 306}
218 307
219static void 308static void create_pipe ()
220end_thread (void)
221{ 309{
310 if (pipe (respipe))
311 croak ("unable to initialize result pipe");
312
313 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
314 croak ("cannot set result pipe to nonblocking mode");
315
316 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
317 croak ("cannot set result pipe to nonblocking mode");
318}
319
320static void atfork_prepare (void)
321{
322 pthread_mutex_lock (&reqlock);
323 pthread_mutex_lock (&reslock);
324}
325
326static void atfork_parent (void)
327{
328 pthread_mutex_unlock (&reslock);
329 pthread_mutex_unlock (&reqlock);
330}
331
332static void atfork_child (void)
333{
222 aio_req req; 334 aio_req prv;
223 New (0, req, 1, aio_cb);
224 req->type = REQ_QUIT;
225 335
226 send_req (req); 336 started = 0;
227}
228 337
229static void 338 while (reqs)
230read_write (int dowrite, int fd, off_t offset, size_t length,
231 SV *data, STRLEN dataoffset, SV *callback)
232{
233 aio_req req;
234 STRLEN svlen;
235 char *svptr = SvPV (data, svlen);
236
237 SvUPGRADE (data, SVt_PV);
238 SvPOK_on (data);
239
240 if (dataoffset < 0)
241 dataoffset += svlen;
242
243 if (dataoffset < 0 || dataoffset > svlen)
244 croak ("data offset outside of string");
245
246 if (dowrite)
247 { 339 {
248 /* write: check length and adjust. */ 340 prv = reqs;
249 if (length < 0 || length + dataoffset > svlen) 341 reqs = prv->next;
250 length = svlen - dataoffset; 342 free_req (prv);
251 } 343 }
252 else 344
345 reqs = reqe = 0;
346
347 while (ress)
253 { 348 {
254 /* read: grow scalar as necessary */ 349 prv = ress;
255 svptr = SvGROW (data, length + dataoffset); 350 ress = prv->next;
351 free_req (prv);
256 } 352 }
353
354 ress = rese = 0;
257 355
258 if (length < 0) 356 close (respipe [0]);
259 croak ("length must not be negative"); 357 close (respipe [1]);
358 create_pipe ();
260 359
261 Newz (0, req, 1, aio_cb); 360 atfork_parent ();
262
263 if (!req)
264 croak ("out of memory during aio_req allocation");
265
266 req->type = dowrite ? REQ_WRITE : REQ_READ;
267 req->fd = fd;
268 req->offset = offset;
269 req->length = length;
270 req->data = SvREFCNT_inc (data);
271 req->dataptr = (char *)svptr + dataoffset;
272 req->callback = SvREFCNT_inc (callback);
273
274 send_req (req);
275} 361}
362
363/*****************************************************************************/
364/* work around various missing functions */
365
366#if !HAVE_PREADWRITE
367# define pread aio_pread
368# define pwrite aio_pwrite
369
370/*
371 * make our pread/pwrite safe against themselves, but not against
372 * normal read/write by using a mutex. slows down execution a lot,
373 * but that's your problem, not mine.
374 */
375static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER;
376
377static ssize_t
378pread (int fd, void *buf, size_t count, off_t offset)
379{
380 ssize_t res;
381 off_t ooffset;
382
383 pthread_mutex_lock (&iolock);
384 ooffset = lseek (fd, 0, SEEK_CUR);
385 lseek (fd, offset, SEEK_SET);
386 res = read (fd, buf, count);
387 lseek (fd, ooffset, SEEK_SET);
388 pthread_mutex_unlock (&iolock);
389
390 return res;
391}
392
393static ssize_t
394pwrite (int fd, void *buf, size_t count, off_t offset)
395{
396 ssize_t res;
397 off_t ooffset;
398
399 pthread_mutex_lock (&iolock);
400 ooffset = lseek (fd, 0, SEEK_CUR);
401 lseek (fd, offset, SEEK_SET);
402 res = write (fd, buf, count);
403 lseek (fd, offset, SEEK_SET);
404 pthread_mutex_unlock (&iolock);
405
406 return res;
407}
408#endif
409
410#if !HAVE_FDATASYNC
411# define fdatasync fsync
412#endif
413
414#if !HAVE_READAHEAD
415# define readahead aio_readahead
416
417static char readahead_buf[4096];
418
419static ssize_t
420readahead (int fd, off_t offset, size_t count)
421{
422 while (count > 0)
423 {
424 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
425
426 pread (fd, readahead_buf, len, offset);
427 offset += len;
428 count -= len;
429 }
430
431 errno = 0;
432}
433#endif
434
435/* sendfile always needs emulation */
436static ssize_t
437sendfile_ (int ofd, int ifd, off_t offset, size_t count)
438{
439 ssize_t res;
440
441 if (!count)
442 return 0;
443
444#if __linux
445 res = sendfile (ofd, ifd, &offset, count);
446
447#elif __freebsd
448 /*
449 * Of course, the freebsd sendfile is a dire hack with no thoughts
450 * wasted on making it similar to other i/o functions.
451 */
452 {
453 off_t sbytes;
454 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
455
456 if (!res && errno == EAGAIN)
457 /* maybe on others, too, as usual, the manpage leaves you guessing */
458 res = sbytes;
459 }
460
461#elif __hpux
462 res = sendfile (ofd, ifd, offset, count, 0, 0);
463
464#else
465 res = -1;
466 errno = ENOSYS;
467#endif
468
469 if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK))
470 {
471 /* emulate sendfile. this is a major pain in the ass */
472 char *buf = malloc (4096);
473 res = 0;
474
475 for (;;)
476 {
477 ssize_t cnt;
478
479 cnt = pread (ifd, buf, 4096, offset);
480
481 if (cnt <= 0)
482 {
483 if (cnt && !res) res = -1;
484 break;
485 }
486
487 cnt = write (ofd, buf, cnt);
488
489 if (cnt <= 0)
490 {
491 if (cnt && !res) res = -1;
492 break;
493 }
494
495 offset += cnt;
496 res += cnt;
497 }
498
499 {
500 int errorno = errno;
501 free (buf);
502 errno = errorno;
503 }
504 }
505
506 return res;
507}
508
509/*****************************************************************************/
276 510
277static void * 511static void *
278aio_proc (void *thr_arg) 512aio_proc (void *thr_arg)
279{ 513{
280 aio_req req; 514 aio_req req;
306 540
307 type = req->type; 541 type = req->type;
308 542
309 switch (type) 543 switch (type)
310 { 544 {
311 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; 545 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
312 case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; 546 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
313#if SYS_readahead 547
314 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 548 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
315#else 549 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
316 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
317#endif
318 550
319 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; 551 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
320 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; 552 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
321 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; 553 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
322 554
323 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; 555 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
324 case REQ_CLOSE: req->result = close (req->fd); break; 556 case REQ_CLOSE: req->result = close (req->fd); break;
325 case REQ_UNLINK: req->result = unlink (req->dataptr); break; 557 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
558 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
559 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
326 560
561 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
327 case REQ_FSYNC: req->result = fsync (req->fd); break; 562 case REQ_FSYNC: req->result = fsync (req->fd); break;
328 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
329 563
330 case REQ_QUIT: 564 case REQ_QUIT:
331 break; 565 break;
332 566
333 default: 567 default:
359 while (type != REQ_QUIT); 593 while (type != REQ_QUIT);
360 594
361 return 0; 595 return 0;
362} 596}
363 597
598#define dREQ \
599 aio_req req; \
600 \
601 if (SvOK (callback) && !SvROK (callback)) \
602 croak ("clalback must be undef or of reference type"); \
603 \
604 Newz (0, req, 1, aio_cb); \
605 if (!req) \
606 croak ("out of memory during aio_req allocation"); \
607 \
608 req->callback = newSVsv (callback);
609
364MODULE = IO::AIO PACKAGE = IO::AIO 610MODULE = IO::AIO PACKAGE = IO::AIO
365 611
612PROTOTYPES: ENABLE
613
366BOOT: 614BOOT:
367{ 615{
368 if (pipe (respipe)) 616 create_pipe ();
369 croak ("unable to initialize result pipe"); 617 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
370
371 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
372 croak ("cannot set result pipe to nonblocking mode");
373
374 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
375 croak ("cannot set result pipe to nonblocking mode");
376} 618}
377 619
378void 620void
379min_parallel(nthreads) 621min_parallel(nthreads)
380 int nthreads 622 int nthreads
381 PROTOTYPE: $ 623 PROTOTYPE: $
382 CODE:
383 while (nthreads > started)
384 start_thread ();
385 624
386void 625void
387max_parallel(nthreads) 626max_parallel(nthreads)
388 int nthreads 627 int nthreads
389 PROTOTYPE: $ 628 PROTOTYPE: $
390 CODE:
391{
392 int cur = started;
393 while (cur > nthreads)
394 {
395 end_thread ();
396 cur--;
397 }
398
399 while (started > nthreads)
400 {
401 poll_wait ();
402 poll_cb ();
403 }
404}
405 629
406int 630int
407max_outstanding(nreqs) 631max_outstanding(nreqs)
408 int nreqs 632 int nreqs
409 PROTOTYPE: $ 633 PROTOTYPE: $
410 CODE: 634 CODE:
411 RETVAL = max_outstanding; 635 RETVAL = max_outstanding;
412 max_outstanding = nreqs; 636 max_outstanding = nreqs;
413 637
414void 638void
415aio_open(pathname,flags,mode,callback) 639aio_open(pathname,flags,mode,callback=&PL_sv_undef)
416 SV * pathname 640 SV * pathname
417 int flags 641 int flags
418 int mode 642 int mode
419 SV * callback 643 SV * callback
420 PROTOTYPE: $$$$ 644 PROTOTYPE: $$$;$
421 CODE: 645 CODE:
422{ 646{
423 aio_req req; 647 dREQ;
424
425 Newz (0, req, 1, aio_cb);
426
427 if (!req)
428 croak ("out of memory during aio_req allocation");
429 648
430 req->type = REQ_OPEN; 649 req->type = REQ_OPEN;
431 req->data = newSVsv (pathname); 650 req->data = newSVsv (pathname);
432 req->dataptr = SvPV_nolen (req->data); 651 req->dataptr = SvPVbyte_nolen (req->data);
433 req->fd = flags; 652 req->fd = flags;
434 req->mode = mode; 653 req->mode = mode;
435 req->callback = SvREFCNT_inc (callback);
436 654
437 send_req (req); 655 send_req (req);
438} 656}
439 657
440void 658void
441aio_close(fh,callback) 659aio_close(fh,callback=&PL_sv_undef)
442 InputStream fh 660 SV * fh
443 SV * callback 661 SV * callback
444 PROTOTYPE: $$ 662 PROTOTYPE: $;$
445 ALIAS: 663 ALIAS:
446 aio_close = REQ_CLOSE 664 aio_close = REQ_CLOSE
447 aio_fsync = REQ_FSYNC 665 aio_fsync = REQ_FSYNC
448 aio_fdatasync = REQ_FDATASYNC 666 aio_fdatasync = REQ_FDATASYNC
449 CODE: 667 CODE:
450{ 668{
669 dREQ;
670
671 req->type = ix;
672 req->fh = newSVsv (fh);
673 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
674
675 send_req (req);
676}
677
678void
679aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
680 SV * fh
681 UV offset
682 UV length
683 SV * data
684 UV dataoffset
685 SV * callback
686 ALIAS:
687 aio_read = REQ_READ
688 aio_write = REQ_WRITE
689 PROTOTYPE: $$$$$;$
690 CODE:
691{
451 aio_req req; 692 aio_req req;
693 STRLEN svlen;
694 char *svptr = SvPVbyte (data, svlen);
452 695
453 Newz (0, req, 1, aio_cb); 696 SvUPGRADE (data, SVt_PV);
697 SvPOK_on (data);
454 698
455 if (!req) 699 if (dataoffset < 0)
456 croak ("out of memory during aio_req allocation"); 700 dataoffset += svlen;
457 701
458 req->type = ix; 702 if (dataoffset < 0 || dataoffset > svlen)
459 req->fd = PerlIO_fileno (fh); 703 croak ("data offset outside of string");
460 req->callback = SvREFCNT_inc (callback);
461 704
462 send_req (req); 705 if (ix == REQ_WRITE)
463} 706 {
464 707 /* write: check length and adjust. */
465void 708 if (length < 0 || length + dataoffset > svlen)
466aio_read(fh,offset,length,data,dataoffset,callback) 709 length = svlen - dataoffset;
467 InputStream fh 710 }
468 UV offset 711 else
469 IV length 712 {
470 SV * data 713 /* read: grow scalar as necessary */
471 IV dataoffset 714 svptr = SvGROW (data, length + dataoffset);
472 SV * callback 715 }
473 PROTOTYPE: $$$$$$
474 CODE:
475 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
476
477void
478aio_write(fh,offset,length,data,dataoffset,callback)
479 OutputStream fh
480 UV offset
481 IV length
482 SV * data
483 IV dataoffset
484 SV * callback
485 PROTOTYPE: $$$$$$
486 CODE:
487 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
488
489void
490aio_readahead(fh,offset,length,callback)
491 InputStream fh
492 UV offset
493 IV length
494 SV * callback
495 PROTOTYPE: $$$$
496 CODE:
497{
498 aio_req req;
499 716
500 if (length < 0) 717 if (length < 0)
501 croak ("length must not be negative"); 718 croak ("length must not be negative");
502 719
503 Newz (0, req, 1, aio_cb); 720 {
721 dREQ;
504 722
505 if (!req) 723 req->type = ix;
506 croak ("out of memory during aio_req allocation"); 724 req->fh = newSVsv (fh);
725 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
726 : IoOFP (sv_2io (fh)));
727 req->offset = offset;
728 req->length = length;
729 req->data = SvREFCNT_inc (data);
730 req->dataptr = (char *)svptr + dataoffset;
731
732 if (!SvREADONLY (data))
733 {
734 SvREADONLY_on (data);
735 req->data2ptr = (void *)data;
736 }
737
738 send_req (req);
739 }
740}
741
742void
743aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef)
744 SV * out_fh
745 SV * in_fh
746 UV in_offset
747 UV length
748 SV * callback
749 PROTOTYPE: $$$$;$
750 CODE:
751{
752 dREQ;
753
754 req->type = REQ_SENDFILE;
755 req->fh = newSVsv (out_fh);
756 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
757 req->fh2 = newSVsv (in_fh);
758 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
759 req->offset = in_offset;
760 req->length = length;
761
762 send_req (req);
763}
764
765void
766aio_readahead(fh,offset,length,callback=&PL_sv_undef)
767 SV * fh
768 UV offset
769 IV length
770 SV * callback
771 PROTOTYPE: $$$;$
772 CODE:
773{
774 dREQ;
507 775
508 req->type = REQ_READAHEAD; 776 req->type = REQ_READAHEAD;
777 req->fh = newSVsv (fh);
509 req->fd = PerlIO_fileno (fh); 778 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
510 req->offset = offset; 779 req->offset = offset;
511 req->length = length; 780 req->length = length;
512 req->callback = SvREFCNT_inc (callback);
513 781
514 send_req (req); 782 send_req (req);
515} 783}
516 784
517void 785void
518aio_stat(fh_or_path,callback) 786aio_stat(fh_or_path,callback=&PL_sv_undef)
519 SV * fh_or_path 787 SV * fh_or_path
520 SV * callback 788 SV * callback
521 PROTOTYPE: $$
522 ALIAS: 789 ALIAS:
790 aio_stat = REQ_STAT
523 aio_lstat = 1 791 aio_lstat = REQ_LSTAT
524 CODE: 792 CODE:
525{ 793{
526 aio_req req; 794 dREQ;
527
528 Newz (0, req, 1, aio_cb);
529
530 if (!req)
531 croak ("out of memory during aio_req allocation");
532 795
533 New (0, req->statdata, 1, Stat_t); 796 New (0, req->statdata, 1, Stat_t);
534
535 if (!req->statdata) 797 if (!req->statdata)
798 {
799 free_req (req);
536 croak ("out of memory during aio_req->statdata allocation"); 800 croak ("out of memory during aio_req->statdata allocation");
801 }
537 802
538 if (SvPOK (fh_or_path)) 803 if (SvPOK (fh_or_path))
539 { 804 {
540 req->type = ix ? REQ_LSTAT : REQ_STAT; 805 req->type = ix;
541 req->data = newSVsv (fh_or_path); 806 req->data = newSVsv (fh_or_path);
542 req->dataptr = SvPV_nolen (req->data); 807 req->dataptr = SvPVbyte_nolen (req->data);
543 } 808 }
544 else 809 else
545 { 810 {
546 req->type = REQ_FSTAT; 811 req->type = REQ_FSTAT;
812 req->fh = newSVsv (fh_or_path);
547 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 813 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
548 } 814 }
549 815
550 req->callback = SvREFCNT_inc (callback);
551
552 send_req (req); 816 send_req (req);
553} 817}
554 818
555void 819void
556aio_unlink(pathname,callback) 820aio_unlink(pathname,callback=&PL_sv_undef)
557 SV * pathname 821 SV * pathname
558 SV * callback 822 SV * callback
559 PROTOTYPE: $$ 823 ALIAS:
824 aio_unlink = REQ_UNLINK
825 aio_rmdir = REQ_RMDIR
560 CODE: 826 CODE:
561{ 827{
562 aio_req req; 828 dREQ;
563 829
564 Newz (0, req, 1, aio_cb); 830 req->type = ix;
831 req->data = newSVsv (pathname);
832 req->dataptr = SvPVbyte_nolen (req->data);
565 833
566 if (!req) 834 send_req (req);
567 croak ("out of memory during aio_req allocation"); 835}
836
837void
838aio_symlink(oldpath,newpath,callback=&PL_sv_undef)
839 SV * oldpath
840 SV * newpath
841 SV * callback
842 CODE:
843{
844 dREQ;
568 845
569 req->type = REQ_UNLINK; 846 req->type = REQ_SYMLINK;
847 req->fh = newSVsv (oldpath);
848 req->data2ptr = SvPVbyte_nolen (req->fh);
570 req->data = newSVsv (pathname); 849 req->data = newSVsv (newpath);
571 req->dataptr = SvPV_nolen (req->data); 850 req->dataptr = SvPVbyte_nolen (req->data);
572 req->callback = SvREFCNT_inc (callback);
573 851
574 send_req (req); 852 send_req (req);
575} 853}
576 854
577void 855void
582 { 860 {
583 poll_wait (); 861 poll_wait ();
584 poll_cb (); 862 poll_cb ();
585 } 863 }
586 864
865void
866poll()
867 PROTOTYPE:
868 CODE:
869 if (nreqs)
870 {
871 poll_wait ();
872 poll_cb ();
873 }
874
587int 875int
588poll_fileno() 876poll_fileno()
589 PROTOTYPE: 877 PROTOTYPE:
590 CODE: 878 CODE:
591 RETVAL = respipe [0]; 879 RETVAL = respipe [0];

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines