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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines