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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines