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.44 by root, Sat Oct 21 23:20:29 2006 UTC vs.
Revision 1.65 by root, Tue Oct 24 00:26:32 2006 UTC

1#if __linux
2# define _GNU_SOURCE
3#endif
4
1#define _REENTRANT 1 5#define _REENTRANT 1
6
2#include <errno.h> 7#include <errno.h>
3 8
4#include "EXTERN.h" 9#include "EXTERN.h"
5#include "perl.h" 10#include "perl.h"
6#include "XSUB.h" 11#include "XSUB.h"
9 14
10#include <pthread.h> 15#include <pthread.h>
11 16
12#include <stddef.h> 17#include <stddef.h>
13#include <errno.h> 18#include <errno.h>
19#include <sys/time.h>
20#include <sys/select.h>
14#include <sys/types.h> 21#include <sys/types.h>
15#include <sys/stat.h> 22#include <sys/stat.h>
16#include <limits.h> 23#include <limits.h>
17#include <unistd.h> 24#include <unistd.h>
18#include <fcntl.h> 25#include <fcntl.h>
39# define NAME_MAX 4096 46# define NAME_MAX 4096
40#endif 47#endif
41 48
42#if __ia64 49#if __ia64
43# define STACKSIZE 65536 50# define STACKSIZE 65536
51#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
52# define STACKSIZE PTHREAD_STACK_MIN
44#else 53#else
45# define STACKSIZE 8192 54# define STACKSIZE 16384
46#endif 55#endif
56
57/* buffer size for various temporary buffers */
58#define AIO_BUFSIZE 65536
59
60#define dBUF \
61 char *aio_buf = malloc (AIO_BUFSIZE); \
62 if (!aio_buf) \
63 return -1;
64
65#define fBUF free (aio_buf)
47 66
48enum { 67enum {
49 REQ_QUIT, 68 REQ_QUIT,
50 REQ_OPEN, REQ_CLOSE, 69 REQ_OPEN, REQ_CLOSE,
51 REQ_READ, REQ_WRITE, REQ_READAHEAD, 70 REQ_READ, REQ_WRITE, REQ_READAHEAD,
53 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 72 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
54 REQ_FSYNC, REQ_FDATASYNC, 73 REQ_FSYNC, REQ_FDATASYNC,
55 REQ_UNLINK, REQ_RMDIR, REQ_RENAME, 74 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
56 REQ_READDIR, 75 REQ_READDIR,
57 REQ_LINK, REQ_SYMLINK, 76 REQ_LINK, REQ_SYMLINK,
58 REQ_GROUP, 77 REQ_GROUP, REQ_NOP,
78 REQ_SLEEP,
59}; 79};
60 80
61#define AIO_REQ_KLASS "IO::AIO::REQ" 81#define AIO_REQ_KLASS "IO::AIO::REQ"
62#define AIO_GRP_KLASS "IO::AIO::GRP" 82#define AIO_GRP_KLASS "IO::AIO::GRP"
63 83
64typedef struct aio_cb 84typedef struct aio_cb
65{ 85{
66 struct aio_cb *grp, *grp_prev, *grp_next;
67
68 struct aio_cb *volatile next; 86 struct aio_cb *volatile next;
69
70 SV *self; /* the perl counterpart of this request, if any */
71 87
72 SV *data, *callback; 88 SV *data, *callback;
73 SV *fh, *fh2; 89 SV *fh, *fh2;
74 void *dataptr, *data2ptr; 90 void *dataptr, *data2ptr;
75 Stat_t *statdata; 91 Stat_t *statdata;
76 off_t offset; 92 off_t offset;
77 size_t length; 93 size_t length;
78 ssize_t result; 94 ssize_t result;
79 95
96 STRLEN dataoffset;
80 int type; 97 int type;
81 int fd, fd2; 98 int fd, fd2;
82 int errorno; 99 int errorno;
83 STRLEN dataoffset;
84 mode_t mode; /* open */ 100 mode_t mode; /* open */
101
85 unsigned char cancelled; 102 unsigned char flags;
103 unsigned char pri;
104
105 SV *self; /* the perl counterpart of this request, if any */
106 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
86} aio_cb; 107} aio_cb;
108
109enum {
110 FLAG_CANCELLED = 0x01,
111};
87 112
88typedef aio_cb *aio_req; 113typedef aio_cb *aio_req;
89typedef aio_cb *aio_req_ornot; 114typedef aio_cb *aio_req_ornot;
90typedef aio_cb *aio_group; 115
116enum {
117 PRI_MIN = -4,
118 PRI_MAX = 4,
119
120 DEFAULT_PRI = 0,
121 PRI_BIAS = -PRI_MIN,
122};
123
124static int next_pri = DEFAULT_PRI + PRI_BIAS;
91 125
92static int started, wanted; 126static int started, wanted;
93static volatile int nreqs; 127static volatile int nreqs;
94static int max_outstanding = 1<<30; 128static int max_outstanding = 1<<30;
95static int respipe [2]; 129static int respipe [2];
96 130
131#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
132# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
133#else
134# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
135#endif
136
97static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 137static pthread_mutex_t reslock = AIO_MUTEX_INIT;
98static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 138static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
99static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 139static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
100 140
101static volatile aio_req reqs, reqe; /* queue start, queue end */ 141static volatile aio_req reqs, reqe; /* queue start, queue end */
102static volatile aio_req ress, rese; /* queue start, queue end */ 142static volatile aio_req ress, rese; /* queue start, queue end */
103 143
144static void req_invoke (aio_req req);
145static void req_free (aio_req req);
146
104/* must be called at most once */ 147/* must be called at most once */
105static SV *req_sv (aio_req req, const char *klass) 148static SV *req_sv (aio_req req, const char *klass)
106{ 149{
150 if (!req->self)
151 {
107 req->self = (SV *)newHV (); 152 req->self = (SV *)newHV ();
108 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); 153 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
154 }
109 155
110 return sv_2mortal (sv_bless (newRV_noinc (req->self), gv_stashpv (klass, 1))); 156 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
111} 157}
112 158
113static aio_req SvAIO_REQ (SV *sv, const char *klass) 159static aio_req SvAIO_REQ (SV *sv)
114{ 160{
161 MAGIC *mg;
162
115 if (!sv_derived_from (sv, klass) || !SvROK (sv)) 163 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
116 croak ("object of class %s expected", klass); 164 croak ("object of class " AIO_REQ_KLASS " expected");
117 165
118 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 166 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
119 167
120 return mg ? (aio_req)mg->mg_ptr : 0; 168 return mg ? (aio_req)mg->mg_ptr : 0;
121} 169}
122 170
171static void aio_grp_feed (aio_req grp)
172{
173 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED))
174 {
175 int old_len = grp->length;
176
177 if (grp->fh2 && SvOK (grp->fh2))
178 {
179 dSP;
180
181 ENTER;
182 SAVETMPS;
183 PUSHMARK (SP);
184 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
185 PUTBACK;
186 call_sv (grp->fh2, G_VOID | G_EVAL);
187 SPAGAIN;
188 FREETMPS;
189 LEAVE;
190 }
191
192 /* stop if no progress has been made */
193 if (old_len == grp->length)
194 {
195 SvREFCNT_dec (grp->fh2);
196 grp->fh2 = 0;
197 break;
198 }
199 }
200}
201
202static void aio_grp_dec (aio_req grp)
203{
204 --grp->length;
205
206 /* call feeder, if applicable */
207 aio_grp_feed (grp);
208
209 /* finish, if done */
210 if (!grp->length && grp->fd)
211 {
212 req_invoke (grp);
213 req_free (grp);
214 }
215}
216
217static void poll_wait ()
218{
219 fd_set rfd;
220
221 while (nreqs)
222 {
223 aio_req req;
224#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
225 pthread_mutex_lock (&reslock);
226#endif
227 req = ress;
228#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
229 pthread_mutex_unlock (&reslock);
230#endif
231
232 if (req)
233 return;
234
235 FD_ZERO(&rfd);
236 FD_SET(respipe [0], &rfd);
237
238 select (respipe [0] + 1, &rfd, 0, 0, 0);
239 }
240}
241
242static void req_invoke (aio_req req)
243{
244 dSP;
245 int errorno = errno;
246
247 if (req->flags & FLAG_CANCELLED || !SvOK (req->callback))
248 return;
249
250 errno = req->errorno;
251
252 ENTER;
253 SAVETMPS;
254 PUSHMARK (SP);
255 EXTEND (SP, 1);
256
257 switch (req->type)
258 {
259 case REQ_READDIR:
260 {
261 SV *rv = &PL_sv_undef;
262
263 if (req->result >= 0)
264 {
265 char *buf = req->data2ptr;
266 AV *av = newAV ();
267
268 while (req->result)
269 {
270 SV *sv = newSVpv (buf, 0);
271
272 av_push (av, sv);
273 buf += SvCUR (sv) + 1;
274 req->result--;
275 }
276
277 rv = sv_2mortal (newRV_noinc ((SV *)av));
278 }
279
280 PUSHs (rv);
281 }
282 break;
283
284 case REQ_OPEN:
285 {
286 /* convert fd to fh */
287 SV *fh;
288
289 PUSHs (sv_2mortal (newSViv (req->result)));
290 PUTBACK;
291 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
292 SPAGAIN;
293
294 fh = SvREFCNT_inc (POPs);
295
296 PUSHMARK (SP);
297 XPUSHs (sv_2mortal (fh));
298 }
299 break;
300
301 case REQ_GROUP:
302 req->fd = 2; /* mark group as finished */
303
304 if (req->data)
305 {
306 int i;
307 AV *av = (AV *)req->data;
308
309 EXTEND (SP, AvFILL (av) + 1);
310 for (i = 0; i <= AvFILL (av); ++i)
311 PUSHs (*av_fetch (av, i, 0));
312 }
313 break;
314
315 case REQ_NOP:
316 case REQ_SLEEP:
317 break;
318
319 default:
320 PUSHs (sv_2mortal (newSViv (req->result)));
321 break;
322 }
323
324
325 PUTBACK;
326 call_sv (req->callback, G_VOID | G_EVAL);
327 SPAGAIN;
328
329 FREETMPS;
330 LEAVE;
331
332 errno = errorno;
333
334 if (SvTRUE (ERRSV))
335 {
336 req_free (req);
337 croak (0);
338 }
339}
340
123static void req_free (aio_req req) 341static void req_free (aio_req req)
124{ 342{
343 if (req->grp)
344 {
345 aio_req grp = req->grp;
346
347 /* unlink request */
348 if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
349 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
350
351 if (grp->grp_first == req)
352 grp->grp_first = req->grp_next;
353
354 aio_grp_dec (grp);
355 }
356
125 if (req->self) 357 if (req->self)
126 { 358 {
127 sv_unmagic (req->self, PERL_MAGIC_ext); 359 sv_unmagic (req->self, PERL_MAGIC_ext);
128 SvREFCNT_dec (req->self); 360 SvREFCNT_dec (req->self);
129 } 361 }
130 362
131 if (req->data)
132 SvREFCNT_dec (req->data); 363 SvREFCNT_dec (req->data);
133
134 if (req->fh)
135 SvREFCNT_dec (req->fh); 364 SvREFCNT_dec (req->fh);
136
137 if (req->fh2)
138 SvREFCNT_dec (req->fh2); 365 SvREFCNT_dec (req->fh2);
139
140 if (req->statdata)
141 Safefree (req->statdata);
142
143 if (req->callback)
144 SvREFCNT_dec (req->callback); 366 SvREFCNT_dec (req->callback);
367 Safefree (req->statdata);
145 368
146 if (req->type == REQ_READDIR && req->result >= 0) 369 if (req->type == REQ_READDIR && req->result >= 0)
147 free (req->data2ptr); 370 free (req->data2ptr);
148 371
149 Safefree (req); 372 Safefree (req);
150} 373}
151 374
152static void 375static void req_cancel (aio_req req)
153poll_wait ()
154{ 376{
155 if (nreqs && !ress) 377 req->flags |= FLAG_CANCELLED;
156 {
157 fd_set rfd;
158 FD_ZERO(&rfd);
159 FD_SET(respipe [0], &rfd);
160 378
161 select (respipe [0] + 1, &rfd, 0, 0, 0); 379 if (req->type == REQ_GROUP)
162 } 380 {
163} 381 aio_req sub;
164 382
165static int 383 for (sub = req->grp_first; sub; sub = sub->grp_next)
166poll_cb () 384 req_cancel (sub);
385 }
386}
387
388static int poll_cb ()
167{ 389{
168 dSP; 390 dSP;
169 int count = 0; 391 int count = 0;
170 int do_croak = 0; 392 int do_croak = 0;
171 aio_req req; 393 aio_req req;
193 pthread_mutex_unlock (&reslock); 415 pthread_mutex_unlock (&reslock);
194 416
195 if (!req) 417 if (!req)
196 break; 418 break;
197 419
198 nreqs--; 420 --nreqs;
199 421
200 if (req->type == REQ_QUIT) 422 if (req->type == REQ_QUIT)
201 started--; 423 started--;
424 else if (req->type == REQ_GROUP && req->length)
425 {
426 req->fd = 1; /* mark request as delayed */
427 continue;
428 }
202 else 429 else
203 { 430 {
204 int errorno = errno;
205 errno = req->errorno;
206
207 if (req->type == REQ_READ) 431 if (req->type == REQ_READ)
208 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); 432 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
209 433
210 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) 434 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
211 SvREADONLY_off (req->data); 435 SvREADONLY_off (req->data);
215 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 439 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
216 PL_laststatval = req->result; 440 PL_laststatval = req->result;
217 PL_statcache = *(req->statdata); 441 PL_statcache = *(req->statdata);
218 } 442 }
219 443
220 ENTER; 444 req_invoke (req);
221 PUSHMARK (SP);
222 445
223 if (req->type == REQ_READDIR)
224 {
225 SV *rv = &PL_sv_undef;
226
227 if (req->result >= 0)
228 {
229 char *buf = req->data2ptr;
230 AV *av = newAV ();
231
232 while (req->result)
233 {
234 SV *sv = newSVpv (buf, 0);
235
236 av_push (av, sv);
237 buf += SvCUR (sv) + 1;
238 req->result--;
239 }
240
241 rv = sv_2mortal (newRV_noinc ((SV *)av));
242 }
243
244 XPUSHs (rv);
245 }
246 else
247 {
248 XPUSHs (sv_2mortal (newSViv (req->result)));
249
250 if (req->type == REQ_OPEN)
251 {
252 /* convert fd to fh */
253 SV *fh;
254
255 PUTBACK;
256 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
257 SPAGAIN;
258
259 fh = SvREFCNT_inc (POPs);
260
261 PUSHMARK (SP);
262 XPUSHs (sv_2mortal (fh));
263 }
264 }
265
266 if (SvOK (req->callback) && !req->cancelled)
267 {
268 PUTBACK;
269 call_sv (req->callback, G_VOID | G_EVAL);
270 SPAGAIN;
271
272 if (SvTRUE (ERRSV))
273 {
274 req_free (req);
275 croak (0);
276 }
277 }
278
279 LEAVE;
280
281 errno = errorno;
282 count++; 446 count++;
283 } 447 }
284 448
285 req_free (req); 449 req_free (req);
286 } 450 }
288 return count; 452 return count;
289} 453}
290 454
291static void *aio_proc(void *arg); 455static void *aio_proc(void *arg);
292 456
293static void
294start_thread (void) 457static void start_thread (void)
295{ 458{
296 sigset_t fullsigset, oldsigset; 459 sigset_t fullsigset, oldsigset;
297 pthread_t tid; 460 pthread_t tid;
298 pthread_attr_t attr; 461 pthread_attr_t attr;
299 462
308 started++; 471 started++;
309 472
310 sigprocmask (SIG_SETMASK, &oldsigset, 0); 473 sigprocmask (SIG_SETMASK, &oldsigset, 0);
311} 474}
312 475
313static void
314req_send (aio_req req) 476static void req_send (aio_req req)
315{ 477{
316 while (started < wanted && nreqs >= started) 478 while (started < wanted && nreqs >= started)
317 start_thread (); 479 start_thread ();
318 480
319 nreqs++; 481 ++nreqs;
320 482
321 pthread_mutex_lock (&reqlock); 483 pthread_mutex_lock (&reqlock);
322 484
323 req->next = 0; 485 req->next = 0;
324 486
343 505
344 poll_wait (); 506 poll_wait ();
345 } 507 }
346} 508}
347 509
348static void 510static void end_thread (void)
349end_thread (void)
350{ 511{
351 aio_req req; 512 aio_req req;
352 Newz (0, req, 1, aio_cb); 513 Newz (0, req, 1, aio_cb);
353 req->type = REQ_QUIT; 514 req->type = REQ_QUIT;
354 515
405 * normal read/write by using a mutex. slows down execution a lot, 566 * normal read/write by using a mutex. slows down execution a lot,
406 * but that's your problem, not mine. 567 * but that's your problem, not mine.
407 */ 568 */
408static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; 569static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
409 570
410static ssize_t 571static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
411pread (int fd, void *buf, size_t count, off_t offset)
412{ 572{
413 ssize_t res; 573 ssize_t res;
414 off_t ooffset; 574 off_t ooffset;
415 575
416 pthread_mutex_lock (&preadwritelock); 576 pthread_mutex_lock (&preadwritelock);
421 pthread_mutex_unlock (&preadwritelock); 581 pthread_mutex_unlock (&preadwritelock);
422 582
423 return res; 583 return res;
424} 584}
425 585
426static ssize_t
427pwrite (int fd, void *buf, size_t count, off_t offset) 586static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
428{ 587{
429 ssize_t res; 588 ssize_t res;
430 off_t ooffset; 589 off_t ooffset;
431 590
432 pthread_mutex_lock (&preadwritelock); 591 pthread_mutex_lock (&preadwritelock);
445#endif 604#endif
446 605
447#if !HAVE_READAHEAD 606#if !HAVE_READAHEAD
448# define readahead aio_readahead 607# define readahead aio_readahead
449 608
450static ssize_t
451readahead (int fd, off_t offset, size_t count) 609static ssize_t readahead (int fd, off_t offset, size_t count)
452{ 610{
453 char readahead_buf[4096]; 611 dBUF;
454 612
455 while (count > 0) 613 while (count > 0)
456 { 614 {
457 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 615 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
458 616
459 pread (fd, readahead_buf, len, offset); 617 pread (fd, aio_buf, len, offset);
460 offset += len; 618 offset += len;
461 count -= len; 619 count -= len;
462 } 620 }
463 621
622 fBUF;
623
464 errno = 0; 624 errno = 0;
465} 625}
466#endif 626#endif
467 627
468#if !HAVE_READDIR_R 628#if !HAVE_READDIR_R
469# define readdir_r aio_readdir_r 629# define readdir_r aio_readdir_r
470 630
471static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; 631static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
472 632
473static int
474readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 633static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
475{ 634{
476 struct dirent *e; 635 struct dirent *e;
477 int errorno; 636 int errorno;
478 637
479 pthread_mutex_lock (&readdirlock); 638 pthread_mutex_lock (&readdirlock);
495 return e ? 0 : -1; 654 return e ? 0 : -1;
496} 655}
497#endif 656#endif
498 657
499/* sendfile always needs emulation */ 658/* sendfile always needs emulation */
500static ssize_t
501sendfile_ (int ofd, int ifd, off_t offset, size_t count) 659static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count)
502{ 660{
503 ssize_t res; 661 ssize_t res;
504 662
505 if (!count) 663 if (!count)
506 return 0; 664 return 0;
555#endif 713#endif
556 ) 714 )
557 ) 715 )
558 { 716 {
559 /* emulate sendfile. this is a major pain in the ass */ 717 /* emulate sendfile. this is a major pain in the ass */
560 char buf[4096]; 718 dBUF;
719
561 res = 0; 720 res = 0;
562 721
563 while (count) 722 while (count)
564 { 723 {
565 ssize_t cnt; 724 ssize_t cnt;
566 725
567 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 726 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
568 727
569 if (cnt <= 0) 728 if (cnt <= 0)
570 { 729 {
571 if (cnt && !res) res = -1; 730 if (cnt && !res) res = -1;
572 break; 731 break;
573 } 732 }
574 733
575 cnt = write (ofd, buf, cnt); 734 cnt = write (ofd, aio_buf, cnt);
576 735
577 if (cnt <= 0) 736 if (cnt <= 0)
578 { 737 {
579 if (cnt && !res) res = -1; 738 if (cnt && !res) res = -1;
580 break; 739 break;
582 741
583 offset += cnt; 742 offset += cnt;
584 res += cnt; 743 res += cnt;
585 count -= cnt; 744 count -= cnt;
586 } 745 }
746
747 fBUF;
587 } 748 }
588 749
589 return res; 750 return res;
590} 751}
591 752
592/* read a full directory */ 753/* read a full directory */
593static int
594scandir_ (const char *path, void **namesp) 754static int scandir_ (const char *path, void **namesp)
595{ 755{
596 DIR *dirp = opendir (path); 756 DIR *dirp;
597 union 757 union
598 { 758 {
599 struct dirent d; 759 struct dirent d;
600 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 760 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
601 } u; 761 } *u;
602 struct dirent *entp; 762 struct dirent *entp;
603 char *name, *names; 763 char *name, *names;
604 int memlen = 4096; 764 int memlen = 4096;
605 int memofs = 0; 765 int memofs = 0;
606 int res = 0; 766 int res = 0;
607 int errorno; 767 int errorno;
608 768
769 dirp = opendir (path);
609 if (!dirp) 770 if (!dirp)
610 return -1; 771 return -1;
611 772
773 u = malloc (sizeof (*u));
612 names = malloc (memlen); 774 names = malloc (memlen);
613 775
776 if (u && names)
614 for (;;) 777 for (;;)
615 { 778 {
779 errno = 0;
616 errno = 0, readdir_r (dirp, &u.d, &entp); 780 readdir_r (dirp, &u->d, &entp);
617 781
618 if (!entp) 782 if (!entp)
619 break; 783 break;
620 784
621 name = entp->d_name; 785 name = entp->d_name;
622 786
623 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 787 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
624 { 788 {
625 int len = strlen (name) + 1; 789 int len = strlen (name) + 1;
626 790
627 res++; 791 res++;
628 792
629 while (memofs + len > memlen) 793 while (memofs + len > memlen)
630 { 794 {
631 memlen *= 2; 795 memlen *= 2;
632 names = realloc (names, memlen); 796 names = realloc (names, memlen);
633 if (!names) 797 if (!names)
634 break; 798 break;
635 } 799 }
636 800
637 memcpy (names + memofs, name, len); 801 memcpy (names + memofs, name, len);
638 memofs += len; 802 memofs += len;
639 } 803 }
640 } 804 }
641 805
642 errorno = errno; 806 errorno = errno;
807 free (u);
643 closedir (dirp); 808 closedir (dirp);
644 809
645 if (errorno) 810 if (errorno)
646 { 811 {
647 free (names); 812 free (names);
653 return res; 818 return res;
654} 819}
655 820
656/*****************************************************************************/ 821/*****************************************************************************/
657 822
658static void *
659aio_proc (void *thr_arg) 823static void *aio_proc (void *thr_arg)
660{ 824{
661 aio_req req; 825 aio_req req;
662 int type; 826 int type;
663 827
664 do 828 do
682 } 846 }
683 847
684 pthread_mutex_unlock (&reqlock); 848 pthread_mutex_unlock (&reqlock);
685 849
686 errno = 0; /* strictly unnecessary */ 850 errno = 0; /* strictly unnecessary */
851 type = req->type; /* remember type for QUIT check */
687 852
688 if (!req->cancelled) 853 if (!(req->flags & FLAG_CANCELLED))
689 switch (req->type) 854 switch (type)
690 { 855 {
691 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 856 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
692 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 857 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
693 858
694 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 859 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
708 873
709 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 874 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
710 case REQ_FSYNC: req->result = fsync (req->fd); break; 875 case REQ_FSYNC: req->result = fsync (req->fd); break;
711 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 876 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
712 877
878 case REQ_SLEEP:
879 {
880 struct timeval tv;
881
882 tv.tv_sec = req->fd;
883 tv.tv_usec = req->fd2;
884
885 req->result = select (0, 0, 0, 0, &tv);
886 }
887
888 case REQ_GROUP:
889 case REQ_NOP:
713 case REQ_QUIT: 890 case REQ_QUIT:
714 break; 891 break;
715 892
716 default: 893 default:
717 req->result = ENOSYS; 894 req->result = ENOSYS;
801 atfork_parent (); 978 atfork_parent ();
802} 979}
803 980
804#define dREQ \ 981#define dREQ \
805 aio_req req; \ 982 aio_req req; \
983 int req_pri = next_pri; \
984 next_pri = DEFAULT_PRI + PRI_BIAS; \
806 \ 985 \
807 if (SvOK (callback) && !SvROK (callback)) \ 986 if (SvOK (callback) && !SvROK (callback)) \
808 croak ("callback must be undef or of reference type"); \ 987 croak ("callback must be undef or of reference type"); \
809 \ 988 \
810 Newz (0, req, 1, aio_cb); \ 989 Newz (0, req, 1, aio_cb); \
811 if (!req) \ 990 if (!req) \
812 croak ("out of memory during aio_req allocation"); \ 991 croak ("out of memory during aio_req allocation"); \
813 \ 992 \
814 req->callback = newSVsv (callback) 993 req->callback = newSVsv (callback); \
994 req->pri = req_pri
815 995
816#define REQ_SEND \ 996#define REQ_SEND \
817 req_send (req); \ 997 req_send (req); \
818 \ 998 \
819 if (GIMME_V != G_VOID) \ 999 if (GIMME_V != G_VOID) \
1073 1253
1074 REQ_SEND; 1254 REQ_SEND;
1075} 1255}
1076 1256
1077void 1257void
1258aio_sleep (delay,callback=&PL_sv_undef)
1259 double delay
1260 SV * callback
1261 PPCODE:
1262{
1263 dREQ;
1264
1265 req->type = REQ_SLEEP;
1266 req->fd = delay < 0. ? 0 : delay;
1267 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd);
1268
1269 REQ_SEND;
1270}
1271
1272void
1078aio_group (callback=&PL_sv_undef) 1273aio_group (callback=&PL_sv_undef)
1079 SV * callback 1274 SV * callback
1080 PROTOTYPE: ;& 1275 PROTOTYPE: ;$
1081 PPCODE: 1276 PPCODE:
1082{ 1277{
1083 dREQ; 1278 dREQ;
1279
1084 req->type = REQ_GROUP; 1280 req->type = REQ_GROUP;
1281 req_send (req);
1282
1085 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1283 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1086} 1284}
1285
1286void
1287aio_nop (callback=&PL_sv_undef)
1288 SV * callback
1289 PPCODE:
1290{
1291 dREQ;
1292
1293 req->type = REQ_NOP;
1294
1295 REQ_SEND;
1296}
1297
1298#if 0
1299
1300void
1301aio_pri (int pri = DEFAULT_PRI)
1302 CODE:
1303 if (pri < PRI_MIN) pri = PRI_MIN;
1304 if (pri > PRI_MAX) pri = PRI_MAX;
1305 next_pri = pri + PRI_BIAS;
1306
1307#endif
1087 1308
1088void 1309void
1089flush () 1310flush ()
1090 PROTOTYPE: 1311 PROTOTYPE:
1091 CODE: 1312 CODE:
1134 CODE: 1355 CODE:
1135 RETVAL = nreqs; 1356 RETVAL = nreqs;
1136 OUTPUT: 1357 OUTPUT:
1137 RETVAL 1358 RETVAL
1138 1359
1360PROTOTYPES: DISABLE
1361
1139MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1362MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1140 1363
1141void 1364void
1142cancel (aio_req_ornot req) 1365cancel (aio_req_ornot req)
1143 PROTOTYPE: 1366 PROTOTYPE:
1144 CODE: 1367 CODE:
1145 req->cancelled = 1; 1368 req_cancel (req);
1146 1369
1370void
1371cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1372 CODE:
1373 SvREFCNT_dec (req->callback);
1374 req->callback = newSVsv (callback);
1375
1376MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1377
1378void
1379add (aio_req grp, ...)
1380 PPCODE:
1381{
1382 int i;
1383 aio_req req;
1384
1385 if (grp->fd == 2)
1386 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1387
1388 for (i = 1; i < items; ++i )
1389 {
1390 if (GIMME_V != G_VOID)
1391 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1392
1393 req = SvAIO_REQ (ST (i));
1394
1395 if (req)
1396 {
1397 ++grp->length;
1398 req->grp = grp;
1399
1400 req->grp_prev = 0;
1401 req->grp_next = grp->grp_first;
1402
1403 if (grp->grp_first)
1404 grp->grp_first->grp_prev = req;
1405
1406 grp->grp_first = req;
1407 }
1408 }
1409}
1410
1411void
1412result (aio_req grp, ...)
1413 CODE:
1414{
1415 int i;
1416 AV *av = newAV ();
1417
1418 for (i = 1; i < items; ++i )
1419 av_push (av, newSVsv (ST (i)));
1420
1421 SvREFCNT_dec (grp->data);
1422 grp->data = (SV *)av;
1423}
1424
1425void
1426feed_limit (aio_req grp, int limit)
1427 CODE:
1428 grp->fd2 = limit;
1429 aio_grp_feed (grp);
1430
1431void
1432feed (aio_req grp, SV *callback=&PL_sv_undef)
1433 CODE:
1434{
1435 SvREFCNT_dec (grp->fh2);
1436 grp->fh2 = newSVsv (callback);
1437
1438 if (grp->fd2 <= 0)
1439 grp->fd2 = 2;
1440
1441 aio_grp_feed (grp);
1442}
1443

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines