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.43 by root, Sat Oct 21 23:06:04 2006 UTC vs.
Revision 1.66 by root, Tue Oct 24 00:34:47 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,
77 REQ_GROUP, REQ_NOP,
78 REQ_SLEEP,
58}; 79};
59 80
60#define AIO_CB_KLASS "IO::AIO::CB" 81#define AIO_REQ_KLASS "IO::AIO::REQ"
82#define AIO_GRP_KLASS "IO::AIO::GRP"
61 83
62typedef struct aio_cb 84typedef struct aio_cb
63{ 85{
64 struct aio_cb *grp_prev, *grp_next;
65 struct aio_grp *grp;
66
67 struct aio_cb *volatile next; 86 struct aio_cb *volatile next;
68
69 SV *self; /* the perl counterpart of this request, if any */
70 87
71 SV *data, *callback; 88 SV *data, *callback;
72 SV *fh, *fh2; 89 SV *fh, *fh2;
73 void *dataptr, *data2ptr; 90 void *dataptr, *data2ptr;
74 Stat_t *statdata; 91 Stat_t *statdata;
75 off_t offset; 92 off_t offset;
76 size_t length; 93 size_t length;
77 ssize_t result; 94 ssize_t result;
78 95
96 STRLEN dataoffset;
79 int type; 97 int type;
80 int fd, fd2; 98 int fd, fd2;
81 int errorno; 99 int errorno;
82 STRLEN dataoffset;
83 mode_t mode; /* open */ 100 mode_t mode; /* open */
101
84 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;
85} aio_cb; 107} aio_cb;
108
109enum {
110 FLAG_CANCELLED = 0x01,
111};
86 112
87typedef aio_cb *aio_req; 113typedef aio_cb *aio_req;
88typedef aio_cb *aio_req_ornot; 114typedef aio_cb *aio_req_ornot;
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;
89 125
90static int started, wanted; 126static int started, wanted;
91static volatile int nreqs; 127static volatile int nreqs;
92static int max_outstanding = 1<<30; 128static int max_outstanding = 1<<30;
93static int respipe [2]; 129static int respipe [2];
94 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
95static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 137static pthread_mutex_t reslock = AIO_MUTEX_INIT;
96static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 138static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
97static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 139static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
98 140
99static volatile aio_req reqs, reqe; /* queue start, queue end */ 141static volatile aio_req reqs, reqe; /* queue start, queue end */
100static volatile aio_req ress, rese; /* queue start, queue end */ 142static volatile aio_req ress, rese; /* queue start, queue end */
101 143
102typedef struct aio_grp 144static void req_invoke (aio_req req);
103{ 145static void req_free (aio_req req);
104 struct aio_cb *first, *last;
105 SV *callback;
106 int busycount;
107} aio_grp;
108 146
109static void aio_grp_begin (aio_grp *grp) 147/* must be called at most once */
148static SV *req_sv (aio_req req, const char *klass)
110{ 149{
111 ++grp->busycount; 150 if (!req->self)
112} 151 {
152 req->self = (SV *)newHV ();
153 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
154 }
113 155
156 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
157}
158
159static aio_req SvAIO_REQ (SV *sv)
160{
161 MAGIC *mg;
162
163 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
164 croak ("object of class " AIO_REQ_KLASS " expected");
165
166 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
167
168 return mg ? (aio_req)mg->mg_ptr : 0;
169}
170
114static void aio_grp_end (aio_grp *grp) 171static void aio_grp_feed (aio_req grp)
115{ 172{
116 --grp->busycount; 173 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED))
174 {
175 int old_len = grp->length;
117 176
118 if (grp->busycount) 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))
119 return; 248 return;
120 249
121 SvREFCNT_dec (grp->callback); 250 errno = req->errorno;
122 grp->callback = 0;
123}
124 251
125static aio_grp *aio_grp_new () 252 ENTER;
126{ 253 SAVETMPS;
127 aio_grp *grp; 254 PUSHMARK (SP);
255 EXTEND (SP, 1);
128 256
129 Newz (0, grp, 1, aio_grp); 257 switch (req->type)
130 aio_grp_begin (grp); 258 {
259 case REQ_READDIR:
260 {
261 SV *rv = &PL_sv_undef;
131 262
132 return grp; 263 if (req->result >= 0)
133} 264 {
265 char *buf = req->data2ptr;
266 AV *av = newAV ();
134 267
135/* must be called at most once */ 268 while (req->result)
136static SV *req_sv (aio_req req) 269 {
137{ 270 SV *sv = newSVpv (buf, 0);
138 req->self = (SV *)newHV ();
139 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
140 271
141 return sv_bless (newRV_noinc (req->self), gv_stashpv (AIO_CB_KLASS, 1)); 272 av_push (av, sv);
142} 273 buf += SvCUR (sv) + 1;
274 req->result--;
275 }
143 276
144static aio_req SvAIO_REQ (SV *sv) 277 rv = sv_2mortal (newRV_noinc ((SV *)av));
145{ 278 }
146 if (!sv_derived_from (sv, AIO_CB_KLASS) || !SvROK (sv))
147 croak ("object of class " AIO_CB_KLASS " expected");
148 279
149 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 280 PUSHs (rv);
281 }
282 break;
150 283
151 return mg ? (aio_req)mg->mg_ptr : 0; 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 }
152} 339}
153 340
154static void req_free (aio_req req) 341static void req_free (aio_req req)
155{ 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
156 if (req->self) 357 if (req->self)
157 { 358 {
158 sv_unmagic (req->self, PERL_MAGIC_ext); 359 sv_unmagic (req->self, PERL_MAGIC_ext);
159 SvREFCNT_dec (req->self); 360 SvREFCNT_dec (req->self);
160 } 361 }
161 362
162 if (req->data)
163 SvREFCNT_dec (req->data); 363 SvREFCNT_dec (req->data);
164
165 if (req->fh)
166 SvREFCNT_dec (req->fh); 364 SvREFCNT_dec (req->fh);
167
168 if (req->fh2)
169 SvREFCNT_dec (req->fh2); 365 SvREFCNT_dec (req->fh2);
170
171 if (req->statdata)
172 Safefree (req->statdata);
173
174 if (req->callback)
175 SvREFCNT_dec (req->callback); 366 SvREFCNT_dec (req->callback);
367 Safefree (req->statdata);
176 368
177 if (req->type == REQ_READDIR && req->result >= 0) 369 if (req->type == REQ_READDIR && req->result >= 0)
178 free (req->data2ptr); 370 free (req->data2ptr);
179 371
180 Safefree (req); 372 Safefree (req);
181} 373}
182 374
183static void 375static void req_cancel (aio_req req)
184poll_wait ()
185{ 376{
186 if (nreqs && !ress) 377 req->flags |= FLAG_CANCELLED;
187 {
188 fd_set rfd;
189 FD_ZERO(&rfd);
190 FD_SET(respipe [0], &rfd);
191 378
192 select (respipe [0] + 1, &rfd, 0, 0, 0); 379 if (req->type == REQ_GROUP)
193 } 380 {
194} 381 aio_req sub;
195 382
196static int 383 for (sub = req->grp_first; sub; sub = sub->grp_next)
197poll_cb () 384 req_cancel (sub);
385 }
386}
387
388static int poll_cb ()
198{ 389{
199 dSP; 390 dSP;
200 int count = 0; 391 int count = 0;
201 int do_croak = 0; 392 int do_croak = 0;
202 aio_req req; 393 aio_req req;
224 pthread_mutex_unlock (&reslock); 415 pthread_mutex_unlock (&reslock);
225 416
226 if (!req) 417 if (!req)
227 break; 418 break;
228 419
229 nreqs--; 420 --nreqs;
230 421
231 if (req->type == REQ_QUIT) 422 if (req->type == REQ_QUIT)
232 started--; 423 started--;
424 else if (req->type == REQ_GROUP && req->length)
425 {
426 req->fd = 1; /* mark request as delayed */
427 continue;
428 }
233 else 429 else
234 { 430 {
235 int errorno = errno;
236 errno = req->errorno;
237
238 if (req->type == REQ_READ) 431 if (req->type == REQ_READ)
239 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));
240 433
241 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) 434 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
242 SvREADONLY_off (req->data); 435 SvREADONLY_off (req->data);
246 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 439 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
247 PL_laststatval = req->result; 440 PL_laststatval = req->result;
248 PL_statcache = *(req->statdata); 441 PL_statcache = *(req->statdata);
249 } 442 }
250 443
251 ENTER; 444 req_invoke (req);
252 PUSHMARK (SP);
253 445
254 if (req->type == REQ_READDIR)
255 {
256 SV *rv = &PL_sv_undef;
257
258 if (req->result >= 0)
259 {
260 char *buf = req->data2ptr;
261 AV *av = newAV ();
262
263 while (req->result)
264 {
265 SV *sv = newSVpv (buf, 0);
266
267 av_push (av, sv);
268 buf += SvCUR (sv) + 1;
269 req->result--;
270 }
271
272 rv = sv_2mortal (newRV_noinc ((SV *)av));
273 }
274
275 XPUSHs (rv);
276 }
277 else
278 {
279 XPUSHs (sv_2mortal (newSViv (req->result)));
280
281 if (req->type == REQ_OPEN)
282 {
283 /* convert fd to fh */
284 SV *fh;
285
286 PUTBACK;
287 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
288 SPAGAIN;
289
290 fh = SvREFCNT_inc (POPs);
291
292 PUSHMARK (SP);
293 XPUSHs (sv_2mortal (fh));
294 }
295 }
296
297 if (SvOK (req->callback) && !req->cancelled)
298 {
299 PUTBACK;
300 call_sv (req->callback, G_VOID | G_EVAL);
301 SPAGAIN;
302
303 if (SvTRUE (ERRSV))
304 {
305 req_free (req);
306 croak (0);
307 }
308 }
309
310 LEAVE;
311
312 errno = errorno;
313 count++; 446 count++;
314 } 447 }
315 448
316 req_free (req); 449 req_free (req);
317 } 450 }
319 return count; 452 return count;
320} 453}
321 454
322static void *aio_proc(void *arg); 455static void *aio_proc(void *arg);
323 456
324static void
325start_thread (void) 457static void start_thread (void)
326{ 458{
327 sigset_t fullsigset, oldsigset; 459 sigset_t fullsigset, oldsigset;
328 pthread_t tid; 460 pthread_t tid;
329 pthread_attr_t attr; 461 pthread_attr_t attr;
330 462
339 started++; 471 started++;
340 472
341 sigprocmask (SIG_SETMASK, &oldsigset, 0); 473 sigprocmask (SIG_SETMASK, &oldsigset, 0);
342} 474}
343 475
344static void
345req_send (aio_req req) 476static void req_send (aio_req req)
346{ 477{
347 while (started < wanted && nreqs >= started) 478 while (started < wanted && nreqs >= started)
348 start_thread (); 479 start_thread ();
349 480
350 nreqs++; 481 ++nreqs;
351 482
352 pthread_mutex_lock (&reqlock); 483 pthread_mutex_lock (&reqlock);
353 484
354 req->next = 0; 485 req->next = 0;
355 486
374 505
375 poll_wait (); 506 poll_wait ();
376 } 507 }
377} 508}
378 509
379static void 510static void end_thread (void)
380end_thread (void)
381{ 511{
382 aio_req req; 512 aio_req req;
383 Newz (0, req, 1, aio_cb); 513 Newz (0, req, 1, aio_cb);
384 req->type = REQ_QUIT; 514 req->type = REQ_QUIT;
385 515
436 * 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,
437 * but that's your problem, not mine. 567 * but that's your problem, not mine.
438 */ 568 */
439static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; 569static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
440 570
441static ssize_t 571static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
442pread (int fd, void *buf, size_t count, off_t offset)
443{ 572{
444 ssize_t res; 573 ssize_t res;
445 off_t ooffset; 574 off_t ooffset;
446 575
447 pthread_mutex_lock (&preadwritelock); 576 pthread_mutex_lock (&preadwritelock);
452 pthread_mutex_unlock (&preadwritelock); 581 pthread_mutex_unlock (&preadwritelock);
453 582
454 return res; 583 return res;
455} 584}
456 585
457static ssize_t
458pwrite (int fd, void *buf, size_t count, off_t offset) 586static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
459{ 587{
460 ssize_t res; 588 ssize_t res;
461 off_t ooffset; 589 off_t ooffset;
462 590
463 pthread_mutex_lock (&preadwritelock); 591 pthread_mutex_lock (&preadwritelock);
476#endif 604#endif
477 605
478#if !HAVE_READAHEAD 606#if !HAVE_READAHEAD
479# define readahead aio_readahead 607# define readahead aio_readahead
480 608
481static ssize_t
482readahead (int fd, off_t offset, size_t count) 609static ssize_t readahead (int fd, off_t offset, size_t count)
483{ 610{
484 char readahead_buf[4096]; 611 dBUF;
485 612
486 while (count > 0) 613 while (count > 0)
487 { 614 {
488 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 615 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
489 616
490 pread (fd, readahead_buf, len, offset); 617 pread (fd, aio_buf, len, offset);
491 offset += len; 618 offset += len;
492 count -= len; 619 count -= len;
493 } 620 }
494 621
622 fBUF;
623
495 errno = 0; 624 errno = 0;
496} 625}
497#endif 626#endif
498 627
499#if !HAVE_READDIR_R 628#if !HAVE_READDIR_R
500# define readdir_r aio_readdir_r 629# define readdir_r aio_readdir_r
501 630
502static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; 631static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
503 632
504static int
505readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 633static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
506{ 634{
507 struct dirent *e; 635 struct dirent *e;
508 int errorno; 636 int errorno;
509 637
510 pthread_mutex_lock (&readdirlock); 638 pthread_mutex_lock (&readdirlock);
526 return e ? 0 : -1; 654 return e ? 0 : -1;
527} 655}
528#endif 656#endif
529 657
530/* sendfile always needs emulation */ 658/* sendfile always needs emulation */
531static ssize_t
532sendfile_ (int ofd, int ifd, off_t offset, size_t count) 659static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count)
533{ 660{
534 ssize_t res; 661 ssize_t res;
535 662
536 if (!count) 663 if (!count)
537 return 0; 664 return 0;
586#endif 713#endif
587 ) 714 )
588 ) 715 )
589 { 716 {
590 /* emulate sendfile. this is a major pain in the ass */ 717 /* emulate sendfile. this is a major pain in the ass */
591 char buf[4096]; 718 dBUF;
719
592 res = 0; 720 res = 0;
593 721
594 while (count) 722 while (count)
595 { 723 {
596 ssize_t cnt; 724 ssize_t cnt;
597 725
598 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 726 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
599 727
600 if (cnt <= 0) 728 if (cnt <= 0)
601 { 729 {
602 if (cnt && !res) res = -1; 730 if (cnt && !res) res = -1;
603 break; 731 break;
604 } 732 }
605 733
606 cnt = write (ofd, buf, cnt); 734 cnt = write (ofd, aio_buf, cnt);
607 735
608 if (cnt <= 0) 736 if (cnt <= 0)
609 { 737 {
610 if (cnt && !res) res = -1; 738 if (cnt && !res) res = -1;
611 break; 739 break;
613 741
614 offset += cnt; 742 offset += cnt;
615 res += cnt; 743 res += cnt;
616 count -= cnt; 744 count -= cnt;
617 } 745 }
746
747 fBUF;
618 } 748 }
619 749
620 return res; 750 return res;
621} 751}
622 752
623/* read a full directory */ 753/* read a full directory */
624static int
625scandir_ (const char *path, void **namesp) 754static int scandir_ (const char *path, void **namesp)
626{ 755{
627 DIR *dirp = opendir (path); 756 DIR *dirp;
628 union 757 union
629 { 758 {
630 struct dirent d; 759 struct dirent d;
631 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 760 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
632 } u; 761 } *u;
633 struct dirent *entp; 762 struct dirent *entp;
634 char *name, *names; 763 char *name, *names;
635 int memlen = 4096; 764 int memlen = 4096;
636 int memofs = 0; 765 int memofs = 0;
637 int res = 0; 766 int res = 0;
638 int errorno; 767 int errorno;
639 768
769 dirp = opendir (path);
640 if (!dirp) 770 if (!dirp)
641 return -1; 771 return -1;
642 772
773 u = malloc (sizeof (*u));
643 names = malloc (memlen); 774 names = malloc (memlen);
644 775
776 if (u && names)
645 for (;;) 777 for (;;)
646 { 778 {
779 errno = 0;
647 errno = 0, readdir_r (dirp, &u.d, &entp); 780 readdir_r (dirp, &u->d, &entp);
648 781
649 if (!entp) 782 if (!entp)
650 break; 783 break;
651 784
652 name = entp->d_name; 785 name = entp->d_name;
653 786
654 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 787 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
655 { 788 {
656 int len = strlen (name) + 1; 789 int len = strlen (name) + 1;
657 790
658 res++; 791 res++;
659 792
660 while (memofs + len > memlen) 793 while (memofs + len > memlen)
661 { 794 {
662 memlen *= 2; 795 memlen *= 2;
663 names = realloc (names, memlen); 796 names = realloc (names, memlen);
664 if (!names) 797 if (!names)
665 break; 798 break;
666 } 799 }
667 800
668 memcpy (names + memofs, name, len); 801 memcpy (names + memofs, name, len);
669 memofs += len; 802 memofs += len;
670 } 803 }
671 } 804 }
672 805
673 errorno = errno; 806 errorno = errno;
807 free (u);
674 closedir (dirp); 808 closedir (dirp);
675 809
676 if (errorno) 810 if (errorno)
677 { 811 {
678 free (names); 812 free (names);
684 return res; 818 return res;
685} 819}
686 820
687/*****************************************************************************/ 821/*****************************************************************************/
688 822
689static void *
690aio_proc (void *thr_arg) 823static void *aio_proc (void *thr_arg)
691{ 824{
692 aio_req req; 825 aio_req req;
693 int type; 826 int type;
694 827
695 do 828 do
713 } 846 }
714 847
715 pthread_mutex_unlock (&reqlock); 848 pthread_mutex_unlock (&reqlock);
716 849
717 errno = 0; /* strictly unnecessary */ 850 errno = 0; /* strictly unnecessary */
851 type = req->type; /* remember type for QUIT check */
718 852
719 if (!req->cancelled) 853 if (!(req->flags & FLAG_CANCELLED))
720 switch (req->type) 854 switch (type)
721 { 855 {
722 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;
723 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;
724 858
725 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;
739 873
740 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 874 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
741 case REQ_FSYNC: req->result = fsync (req->fd); break; 875 case REQ_FSYNC: req->result = fsync (req->fd); break;
742 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 876 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
743 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:
744 case REQ_QUIT: 890 case REQ_QUIT:
745 break; 891 break;
746 892
747 default: 893 default:
748 req->result = ENOSYS; 894 req->result = ENOSYS;
832 atfork_parent (); 978 atfork_parent ();
833} 979}
834 980
835#define dREQ \ 981#define dREQ \
836 aio_req req; \ 982 aio_req req; \
983 int req_pri = next_pri; \
984 next_pri = DEFAULT_PRI + PRI_BIAS; \
837 \ 985 \
838 if (SvOK (callback) && !SvROK (callback)) \ 986 if (SvOK (callback) && !SvROK (callback)) \
839 croak ("callback must be undef or of reference type"); \ 987 croak ("callback must be undef or of reference type"); \
840 \ 988 \
841 Newz (0, req, 1, aio_cb); \ 989 Newz (0, req, 1, aio_cb); \
842 if (!req) \ 990 if (!req) \
843 croak ("out of memory during aio_req allocation"); \ 991 croak ("out of memory during aio_req allocation"); \
844 \ 992 \
845 req->callback = newSVsv (callback) 993 req->callback = newSVsv (callback); \
994 req->pri = req_pri
846 995
847#define REQ_SEND \ 996#define REQ_SEND \
848 req_send (req); \ 997 req_send (req); \
849 \ 998 \
850 if (GIMME_V != G_VOID) \ 999 if (GIMME_V != G_VOID) \
851 XPUSHs (req_sv (req)); 1000 XPUSHs (req_sv (req, AIO_REQ_KLASS));
852 1001
853MODULE = IO::AIO PACKAGE = IO::AIO 1002MODULE = IO::AIO PACKAGE = IO::AIO
854 1003
855PROTOTYPES: ENABLE 1004PROTOTYPES: ENABLE
856 1005
1103 req->dataptr = SvPVbyte_nolen (req->data); 1252 req->dataptr = SvPVbyte_nolen (req->data);
1104 1253
1105 REQ_SEND; 1254 REQ_SEND;
1106} 1255}
1107 1256
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
1273aio_group (callback=&PL_sv_undef)
1274 SV * callback
1275 PROTOTYPE: ;$
1276 PPCODE:
1277{
1278 dREQ;
1279
1280 req->type = REQ_GROUP;
1281 req_send (req);
1282
1283 XPUSHs (req_sv (req, AIO_GRP_KLASS));
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
1108#if 0 1298#if 0
1109 1299
1110# undocumented, because it does not cancel active requests
1111void 1300void
1112cancel_most_requests () 1301aio_pri (int pri = DEFAULT_PRI)
1113 PROTOTYPE: 1302 CODE:
1114 CODE: 1303 if (pri < PRI_MIN) pri = PRI_MIN;
1115{ 1304 if (pri > PRI_MAX) pri = PRI_MAX;
1116 aio_req *req; 1305 next_pri = pri + PRI_BIAS;
1117
1118 pthread_mutex_lock (&reqlock);
1119 for (req = reqs; req; req = req->next)
1120 req->flags |= 1;
1121 pthread_mutex_unlock (&reqlock);
1122
1123 pthread_mutex_lock (&reslock);
1124 for (req = ress; req; req = req->next)
1125 req->flags |= 1;
1126 pthread_mutex_unlock (&reslock);
1127}
1128 1306
1129#endif 1307#endif
1130 1308
1131void 1309void
1132flush () 1310flush ()
1177 CODE: 1355 CODE:
1178 RETVAL = nreqs; 1356 RETVAL = nreqs;
1179 OUTPUT: 1357 OUTPUT:
1180 RETVAL 1358 RETVAL
1181 1359
1360PROTOTYPES: DISABLE
1361
1182MODULE = IO::AIO PACKAGE = IO::AIO::CB 1362MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1183 1363
1184void 1364void
1185cancel (aio_req_ornot req) 1365cancel (aio_req_ornot req)
1186 PROTOTYPE:
1187 CODE: 1366 CODE:
1188 req->cancelled = 1; 1367 req_cancel (req);
1189 1368
1369void
1370cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1371 CODE:
1372 SvREFCNT_dec (req->callback);
1373 req->callback = newSVsv (callback);
1374
1375MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1376
1377void
1378add (aio_req grp, ...)
1379 PPCODE:
1380{
1381 int i;
1382 aio_req req;
1383
1384 if (grp->fd == 2)
1385 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1386
1387 for (i = 1; i < items; ++i )
1388 {
1389 if (GIMME_V != G_VOID)
1390 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1391
1392 req = SvAIO_REQ (ST (i));
1393
1394 if (req)
1395 {
1396 ++grp->length;
1397 req->grp = grp;
1398
1399 req->grp_prev = 0;
1400 req->grp_next = grp->grp_first;
1401
1402 if (grp->grp_first)
1403 grp->grp_first->grp_prev = req;
1404
1405 grp->grp_first = req;
1406 }
1407 }
1408}
1409
1410void
1411result (aio_req grp, ...)
1412 CODE:
1413{
1414 int i;
1415 AV *av = newAV ();
1416
1417 for (i = 1; i < items; ++i )
1418 av_push (av, newSVsv (ST (i)));
1419
1420 SvREFCNT_dec (grp->data);
1421 grp->data = (SV *)av;
1422}
1423
1424void
1425feed_limit (aio_req grp, int limit)
1426 CODE:
1427 grp->fd2 = limit;
1428 aio_grp_feed (grp);
1429
1430void
1431feed (aio_req grp, SV *callback=&PL_sv_undef)
1432 CODE:
1433{
1434 SvREFCNT_dec (grp->fh2);
1435 grp->fh2 = newSVsv (callback);
1436
1437 if (grp->fd2 <= 0)
1438 grp->fd2 = 2;
1439
1440 aio_grp_feed (grp);
1441}
1442

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines