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.4 by root, Sun Jul 10 20:57:00 2005 UTC vs.
Revision 1.32 by root, Mon Aug 22 23:20:37 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines