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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines