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.49 by root, Sun Oct 22 13:33:28 2006 UTC vs.
Revision 1.67 by root, Tue Oct 24 03:17:39 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"
41# define NAME_MAX 4096 46# define NAME_MAX 4096
42#endif 47#endif
43 48
44#if __ia64 49#if __ia64
45# define STACKSIZE 65536 50# define STACKSIZE 65536
51#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
52# define STACKSIZE PTHREAD_STACK_MIN
46#else 53#else
47# define STACKSIZE 8192 54# define STACKSIZE 16384
48#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)
49 66
50enum { 67enum {
51 REQ_QUIT, 68 REQ_QUIT,
52 REQ_OPEN, REQ_CLOSE, 69 REQ_OPEN, REQ_CLOSE,
53 REQ_READ, REQ_WRITE, REQ_READAHEAD, 70 REQ_READ, REQ_WRITE, REQ_READAHEAD,
55 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 72 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
56 REQ_FSYNC, REQ_FDATASYNC, 73 REQ_FSYNC, REQ_FDATASYNC,
57 REQ_UNLINK, REQ_RMDIR, REQ_RENAME, 74 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
58 REQ_READDIR, 75 REQ_READDIR,
59 REQ_LINK, REQ_SYMLINK, 76 REQ_LINK, REQ_SYMLINK,
77 REQ_GROUP, REQ_NOP,
60 REQ_SLEEP, 78 REQ_SLEEP,
61 REQ_GROUP,
62}; 79};
63 80
64#define AIO_REQ_KLASS "IO::AIO::REQ" 81#define AIO_REQ_KLASS "IO::AIO::REQ"
65#define AIO_GRP_KLASS "IO::AIO::GRP" 82#define AIO_GRP_KLASS "IO::AIO::GRP"
66 83
67typedef struct aio_cb 84typedef struct aio_cb
68{ 85{
69 struct aio_cb *volatile next; 86 struct aio_cb *volatile next;
70
71 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
72
73 SV *self; /* the perl counterpart of this request, if any */
74 87
75 SV *data, *callback; 88 SV *data, *callback;
76 SV *fh, *fh2; 89 SV *fh, *fh2;
77 void *dataptr, *data2ptr; 90 void *dataptr, *data2ptr;
78 Stat_t *statdata; 91 Stat_t *statdata;
79 off_t offset; 92 off_t offset;
80 size_t length; 93 size_t length;
81 ssize_t result; 94 ssize_t result;
82 95
96 STRLEN dataoffset;
83 int type; 97 int type;
84 int fd, fd2; 98 int fd, fd2;
85 int errorno; 99 int errorno;
86 STRLEN dataoffset;
87 mode_t mode; /* open */ 100 mode_t mode; /* open */
101
88 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;
89} aio_cb; 107} aio_cb;
108
109enum {
110 FLAG_CANCELLED = 0x01,
111};
90 112
91typedef aio_cb *aio_req; 113typedef aio_cb *aio_req;
92typedef 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 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
123};
124
125static int next_pri = DEFAULT_PRI + PRI_BIAS;
93 126
94static int started, wanted; 127static int started, wanted;
95static volatile int nreqs; 128static volatile int nreqs;
96static int max_outstanding = 1<<30; 129static int max_outstanding = 1<<30;
97static int respipe [2]; 130static int respipe [2];
98 131
132#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
133# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
134#else
135# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
136#endif
137
99static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 138static pthread_mutex_t reslock = AIO_MUTEX_INIT;
100static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 139static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
101static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 140static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
102 141
103static volatile aio_req reqs, reqe; /* queue start, queue end */ 142/*
104static volatile aio_req ress, rese; /* queue start, queue end */ 143 * a somewhat faster data structure might be nice, but
144 * with 8 priorities this actually needs <20 insns
145 * per shift, the most expensive operation.
146 */
147typedef struct {
148 aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
149 int size;
150} reqq;
105 151
152static reqq req_queue;
153static reqq res_queue;
154
155int reqq_push (reqq *q, aio_req req)
156{
157 int pri = req->pri;
158 req->next = 0;
159
160 if (q->qe[pri])
161 {
162 q->qe[pri]->next = req;
163 q->qe[pri] = req;
164 }
165 else
166 q->qe[pri] = q->qs[pri] = req;
167
168 return q->size++;
169}
170
171aio_req reqq_shift (reqq *q)
172{
173 int pri;
174
175 if (!q->size)
176 return 0;
177
178 --q->size;
179
180 for (pri = NUM_PRI; pri--; )
181 {
182 aio_req req = q->qs[pri];
183
184 if (req)
185 {
186 if (!(q->qs[pri] = req->next))
187 q->qe[pri] = 0;
188
189 return req;
190 }
191 }
192
193 abort ();
194}
195
196static void req_invoke (aio_req req);
106static void req_free (aio_req req); 197static void req_free (aio_req req);
107 198
108/* must be called at most once */ 199/* must be called at most once */
109static SV *req_sv (aio_req req, const char *klass) 200static SV *req_sv (aio_req req, const char *klass)
110{ 201{
117 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); 208 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
118} 209}
119 210
120static aio_req SvAIO_REQ (SV *sv) 211static aio_req SvAIO_REQ (SV *sv)
121{ 212{
213 MAGIC *mg;
214
122 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) 215 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
123 croak ("object of class " AIO_REQ_KLASS " expected"); 216 croak ("object of class " AIO_REQ_KLASS " expected");
124 217
125 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 218 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
126 219
127 return mg ? (aio_req)mg->mg_ptr : 0; 220 return mg ? (aio_req)mg->mg_ptr : 0;
128} 221}
129 222
130static void aio_grp_feed (aio_req grp) 223static void aio_grp_feed (aio_req grp)
131{ 224{
132 while (grp->length < grp->fd2) 225 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED))
133 { 226 {
134 int old_len = grp->length; 227 int old_len = grp->length;
135 228
136 if (grp->fh2 && SvOK (grp->fh2)) 229 if (grp->fh2 && SvOK (grp->fh2))
137 { 230 {
140 ENTER; 233 ENTER;
141 SAVETMPS; 234 SAVETMPS;
142 PUSHMARK (SP); 235 PUSHMARK (SP);
143 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 236 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
144 PUTBACK; 237 PUTBACK;
145 call_sv (grp->fh2, G_VOID | G_EVAL); 238 call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR);
146 SPAGAIN; 239 SPAGAIN;
147 FREETMPS; 240 FREETMPS;
148 LEAVE; 241 LEAVE;
149 } 242 }
150 243
156 break; 249 break;
157 } 250 }
158 } 251 }
159} 252}
160 253
254static void aio_grp_dec (aio_req grp)
255{
256 --grp->length;
257
258 /* call feeder, if applicable */
259 aio_grp_feed (grp);
260
261 /* finish, if done */
262 if (!grp->length && grp->fd)
263 {
264 req_invoke (grp);
265 req_free (grp);
266 }
267}
268
161static void poll_wait () 269static void poll_wait ()
162{ 270{
163 if (nreqs && !ress)
164 {
165 fd_set rfd; 271 fd_set rfd;
272
273 while (nreqs)
274 {
275 int size;
276#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
277 pthread_mutex_lock (&reslock);
278#endif
279 size = res_queue.size;
280#if !(__i386 || __x86_64) /* safe without sempahore on this archs */
281 pthread_mutex_unlock (&reslock);
282#endif
283
284 if (size)
285 return;
286
166 FD_ZERO(&rfd); 287 FD_ZERO(&rfd);
167 FD_SET(respipe [0], &rfd); 288 FD_SET(respipe [0], &rfd);
168 289
169 select (respipe [0] + 1, &rfd, 0, 0, 0); 290 select (respipe [0] + 1, &rfd, 0, 0, 0);
170 } 291 }
171} 292}
172 293
173static void req_invoke (aio_req req) 294static void req_invoke (aio_req req)
174{ 295{
175 dSP; 296 dSP;
176 int errorno = errno;
177 297
178 if (req->cancelled || !SvOK (req->callback)) 298 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
179 return; 299 {
180
181 errno = req->errorno; 300 errno = req->errorno;
182 301
183 ENTER; 302 ENTER;
184 SAVETMPS; 303 SAVETMPS;
185 PUSHMARK (SP); 304 PUSHMARK (SP);
186 EXTEND (SP, 1); 305 EXTEND (SP, 1);
187 306
188 switch (req->type) 307 switch (req->type)
189 {
190 case REQ_READDIR:
191 { 308 {
192 SV *rv = &PL_sv_undef; 309 case REQ_READDIR:
193
194 if (req->result >= 0)
195 { 310 {
196 char *buf = req->data2ptr; 311 SV *rv = &PL_sv_undef;
197 AV *av = newAV ();
198 312
199 while (req->result) 313 if (req->result >= 0)
200 { 314 {
315 char *buf = req->data2ptr;
316 AV *av = newAV ();
317
318 while (req->result)
319 {
201 SV *sv = newSVpv (buf, 0); 320 SV *sv = newSVpv (buf, 0);
202 321
203 av_push (av, sv); 322 av_push (av, sv);
204 buf += SvCUR (sv) + 1; 323 buf += SvCUR (sv) + 1;
205 req->result--; 324 req->result--;
325 }
326
327 rv = sv_2mortal (newRV_noinc ((SV *)av));
206 } 328 }
207 329
208 rv = sv_2mortal (newRV_noinc ((SV *)av)); 330 PUSHs (rv);
209 } 331 }
332 break;
210 333
211 PUSHs (rv); 334 case REQ_OPEN:
335 {
336 /* convert fd to fh */
337 SV *fh;
338
339 PUSHs (sv_2mortal (newSViv (req->result)));
340 PUTBACK;
341 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
342 SPAGAIN;
343
344 fh = SvREFCNT_inc (POPs);
345
346 PUSHMARK (SP);
347 XPUSHs (sv_2mortal (fh));
348 }
349 break;
350
351 case REQ_GROUP:
352 req->fd = 2; /* mark group as finished */
353
354 if (req->data)
355 {
356 int i;
357 AV *av = (AV *)req->data;
358
359 EXTEND (SP, AvFILL (av) + 1);
360 for (i = 0; i <= AvFILL (av); ++i)
361 PUSHs (*av_fetch (av, i, 0));
362 }
363 break;
364
365 case REQ_NOP:
366 case REQ_SLEEP:
367 break;
368
369 default:
370 PUSHs (sv_2mortal (newSViv (req->result)));
371 break;
212 } 372 }
213 break;
214 373
215 case REQ_OPEN:
216 {
217 /* convert fd to fh */
218 SV *fh;
219 374
220 PUSHs (sv_2mortal (newSViv (req->result)));
221 PUTBACK; 375 PUTBACK;
222 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
223 SPAGAIN;
224
225 fh = SvREFCNT_inc (POPs);
226
227 PUSHMARK (SP);
228 XPUSHs (sv_2mortal (fh));
229 }
230 break;
231
232 case REQ_GROUP:
233 req->fd = 2; /* mark group as finished */
234
235 if (req->data)
236 {
237 int i;
238 AV *av = (AV *)req->data;
239
240 EXTEND (SP, AvFILL (av) + 1);
241 for (i = 0; i <= AvFILL (av); ++i)
242 PUSHs (*av_fetch (av, i, 0));
243 }
244 break;
245
246 case REQ_SLEEP:
247 break;
248
249 default:
250 PUSHs (sv_2mortal (newSViv (req->result)));
251 break;
252 }
253
254
255 PUTBACK;
256 call_sv (req->callback, G_VOID | G_EVAL); 376 call_sv (req->callback, G_VOID | G_EVAL);
257 SPAGAIN; 377 SPAGAIN;
258 378
259 if (SvTRUE (ERRSV))
260 {
261 req_free (req);
262 croak (0);
263 }
264
265 FREETMPS; 379 FREETMPS;
266 LEAVE; 380 LEAVE;
381 }
267 382
268 errno = errorno;
269}
270
271static void req_free (aio_req req)
272{
273 if (req->grp) 383 if (req->grp)
274 { 384 {
275 aio_req grp = req->grp; 385 aio_req grp = req->grp;
276 386
277 /* unlink request */ 387 /* unlink request */
279 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; 389 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
280 390
281 if (grp->grp_first == req) 391 if (grp->grp_first == req)
282 grp->grp_first = req->grp_next; 392 grp->grp_first = req->grp_next;
283 393
284 --grp->length;
285
286 /* call feeder, if applicable */
287 aio_grp_feed (grp); 394 aio_grp_dec (grp);
395 }
288 396
289 /* finish, if done */ 397 if (SvTRUE (ERRSV))
290 if (!grp->length && grp->fd) 398 {
291 {
292 req_invoke (grp);
293 req_free (grp); 399 req_free (req);
294 } 400 croak (0);
295 } 401 }
402}
296 403
404static void req_free (aio_req req)
405{
297 if (req->self) 406 if (req->self)
298 { 407 {
299 sv_unmagic (req->self, PERL_MAGIC_ext); 408 sv_unmagic (req->self, PERL_MAGIC_ext);
300 SvREFCNT_dec (req->self); 409 SvREFCNT_dec (req->self);
301 } 410 }
312 Safefree (req); 421 Safefree (req);
313} 422}
314 423
315static void req_cancel (aio_req req) 424static void req_cancel (aio_req req)
316{ 425{
317 req->cancelled = 1; 426 req->flags |= FLAG_CANCELLED;
318 427
319 if (req->type == REQ_GROUP) 428 if (req->type == REQ_GROUP)
320 { 429 {
321 aio_req sub; 430 aio_req sub;
322 431
333 aio_req req; 442 aio_req req;
334 443
335 for (;;) 444 for (;;)
336 { 445 {
337 pthread_mutex_lock (&reslock); 446 pthread_mutex_lock (&reslock);
338 req = ress; 447 req = reqq_shift (&res_queue);
339 448
340 if (req) 449 if (req)
341 { 450 {
342 ress = req->next;
343
344 if (!ress) 451 if (!res_queue.size)
345 { 452 {
346 /* read any signals sent by the worker threads */ 453 /* read any signals sent by the worker threads */
347 char buf [32]; 454 char buf [32];
348 while (read (respipe [0], buf, 32) == 32) 455 while (read (respipe [0], buf, 32) == 32)
349 ; 456 ;
350
351 rese = 0;
352 } 457 }
353 } 458 }
354 459
355 pthread_mutex_unlock (&reslock); 460 pthread_mutex_unlock (&reslock);
356 461
357 if (!req) 462 if (!req)
358 break; 463 break;
359 464
360 nreqs--; 465 --nreqs;
361 466
362 if (req->type == REQ_QUIT) 467 if (req->type == REQ_QUIT)
363 started--; 468 started--;
364 else if (req->type == REQ_GROUP && req->length) 469 else if (req->type == REQ_GROUP && req->length)
365 { 470 {
416static void req_send (aio_req req) 521static void req_send (aio_req req)
417{ 522{
418 while (started < wanted && nreqs >= started) 523 while (started < wanted && nreqs >= started)
419 start_thread (); 524 start_thread ();
420 525
421 nreqs++; 526 ++nreqs;
422 527
423 pthread_mutex_lock (&reqlock); 528 pthread_mutex_lock (&reqlock);
424 529 reqq_push (&req_queue, req);
425 req->next = 0;
426
427 if (reqe)
428 {
429 reqe->next = req;
430 reqe = req;
431 }
432 else
433 reqe = reqs = req;
434
435 pthread_cond_signal (&reqwait); 530 pthread_cond_signal (&reqwait);
436 pthread_mutex_unlock (&reqlock); 531 pthread_mutex_unlock (&reqlock);
437 532
438 if (nreqs > max_outstanding) 533 if (nreqs > max_outstanding)
439 for (;;) 534 for (;;)
448} 543}
449 544
450static void end_thread (void) 545static void end_thread (void)
451{ 546{
452 aio_req req; 547 aio_req req;
548
453 Newz (0, req, 1, aio_cb); 549 Newz (0, req, 1, aio_cb);
550
454 req->type = REQ_QUIT; 551 req->type = REQ_QUIT;
552 req->pri = PRI_MAX + PRI_BIAS;
455 553
456 req_send (req); 554 req_send (req);
457} 555}
458 556
459static void min_parallel (int nthreads) 557static void min_parallel (int nthreads)
546#if !HAVE_READAHEAD 644#if !HAVE_READAHEAD
547# define readahead aio_readahead 645# define readahead aio_readahead
548 646
549static ssize_t readahead (int fd, off_t offset, size_t count) 647static ssize_t readahead (int fd, off_t offset, size_t count)
550{ 648{
551 char readahead_buf[4096]; 649 dBUF;
552 650
553 while (count > 0) 651 while (count > 0)
554 { 652 {
555 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 653 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
556 654
557 pread (fd, readahead_buf, len, offset); 655 pread (fd, aio_buf, len, offset);
558 offset += len; 656 offset += len;
559 count -= len; 657 count -= len;
560 } 658 }
659
660 fBUF;
561 661
562 errno = 0; 662 errno = 0;
563} 663}
564#endif 664#endif
565 665
651#endif 751#endif
652 ) 752 )
653 ) 753 )
654 { 754 {
655 /* emulate sendfile. this is a major pain in the ass */ 755 /* emulate sendfile. this is a major pain in the ass */
656 char buf[4096]; 756 dBUF;
757
657 res = 0; 758 res = 0;
658 759
659 while (count) 760 while (count)
660 { 761 {
661 ssize_t cnt; 762 ssize_t cnt;
662 763
663 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 764 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
664 765
665 if (cnt <= 0) 766 if (cnt <= 0)
666 { 767 {
667 if (cnt && !res) res = -1; 768 if (cnt && !res) res = -1;
668 break; 769 break;
669 } 770 }
670 771
671 cnt = write (ofd, buf, cnt); 772 cnt = write (ofd, aio_buf, cnt);
672 773
673 if (cnt <= 0) 774 if (cnt <= 0)
674 { 775 {
675 if (cnt && !res) res = -1; 776 if (cnt && !res) res = -1;
676 break; 777 break;
678 779
679 offset += cnt; 780 offset += cnt;
680 res += cnt; 781 res += cnt;
681 count -= cnt; 782 count -= cnt;
682 } 783 }
784
785 fBUF;
683 } 786 }
684 787
685 return res; 788 return res;
686} 789}
687 790
688/* read a full directory */ 791/* read a full directory */
689static int scandir_ (const char *path, void **namesp) 792static int scandir_ (const char *path, void **namesp)
690{ 793{
691 DIR *dirp = opendir (path); 794 DIR *dirp;
692 union 795 union
693 { 796 {
694 struct dirent d; 797 struct dirent d;
695 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 798 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
696 } u; 799 } *u;
697 struct dirent *entp; 800 struct dirent *entp;
698 char *name, *names; 801 char *name, *names;
699 int memlen = 4096; 802 int memlen = 4096;
700 int memofs = 0; 803 int memofs = 0;
701 int res = 0; 804 int res = 0;
702 int errorno; 805 int errorno;
703 806
807 dirp = opendir (path);
704 if (!dirp) 808 if (!dirp)
705 return -1; 809 return -1;
706 810
811 u = malloc (sizeof (*u));
707 names = malloc (memlen); 812 names = malloc (memlen);
708 813
814 if (u && names)
709 for (;;) 815 for (;;)
710 { 816 {
817 errno = 0;
711 errno = 0, readdir_r (dirp, &u.d, &entp); 818 readdir_r (dirp, &u->d, &entp);
712 819
713 if (!entp) 820 if (!entp)
714 break; 821 break;
715 822
716 name = entp->d_name; 823 name = entp->d_name;
717 824
718 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 825 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
719 { 826 {
720 int len = strlen (name) + 1; 827 int len = strlen (name) + 1;
721 828
722 res++; 829 res++;
723 830
724 while (memofs + len > memlen) 831 while (memofs + len > memlen)
725 { 832 {
726 memlen *= 2; 833 memlen *= 2;
727 names = realloc (names, memlen); 834 names = realloc (names, memlen);
728 if (!names) 835 if (!names)
729 break; 836 break;
730 } 837 }
731 838
732 memcpy (names + memofs, name, len); 839 memcpy (names + memofs, name, len);
733 memofs += len; 840 memofs += len;
734 } 841 }
735 } 842 }
736 843
737 errorno = errno; 844 errorno = errno;
845 free (u);
738 closedir (dirp); 846 closedir (dirp);
739 847
740 if (errorno) 848 if (errorno)
741 { 849 {
742 free (names); 850 free (names);
759 { 867 {
760 pthread_mutex_lock (&reqlock); 868 pthread_mutex_lock (&reqlock);
761 869
762 for (;;) 870 for (;;)
763 { 871 {
764 req = reqs; 872 req = reqq_shift (&req_queue);
765
766 if (reqs)
767 {
768 reqs = reqs->next;
769 if (!reqs) reqe = 0;
770 }
771 873
772 if (req) 874 if (req)
773 break; 875 break;
774 876
775 pthread_cond_wait (&reqwait, &reqlock); 877 pthread_cond_wait (&reqwait, &reqlock);
776 } 878 }
777 879
778 pthread_mutex_unlock (&reqlock); 880 pthread_mutex_unlock (&reqlock);
779 881
780 errno = 0; /* strictly unnecessary */ 882 errno = 0; /* strictly unnecessary */
883 type = req->type; /* remember type for QUIT check */
781 884
782 if (!req->cancelled) 885 if (!(req->flags & FLAG_CANCELLED))
783 switch (req->type) 886 switch (type)
784 { 887 {
785 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 888 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
786 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 889 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
787 890
788 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 891 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
812 tv.tv_usec = req->fd2; 915 tv.tv_usec = req->fd2;
813 916
814 req->result = select (0, 0, 0, 0, &tv); 917 req->result = select (0, 0, 0, 0, &tv);
815 } 918 }
816 919
920 case REQ_GROUP:
921 case REQ_NOP:
817 case REQ_QUIT: 922 case REQ_QUIT:
818 break; 923 break;
819 924
820 default: 925 default:
821 req->result = ENOSYS; 926 req->result = ENOSYS;
824 929
825 req->errorno = errno; 930 req->errorno = errno;
826 931
827 pthread_mutex_lock (&reslock); 932 pthread_mutex_lock (&reslock);
828 933
829 req->next = 0; 934 if (!reqq_push (&res_queue, req))
830
831 if (rese)
832 {
833 rese->next = req;
834 rese = req;
835 }
836 else
837 {
838 rese = ress = req;
839
840 /* write a dummy byte to the pipe so fh becomes ready */ 935 /* write a dummy byte to the pipe so fh becomes ready */
841 write (respipe [1], &respipe, 1); 936 write (respipe [1], &respipe, 1);
842 }
843 937
844 pthread_mutex_unlock (&reslock); 938 pthread_mutex_unlock (&reslock);
845 } 939 }
846 while (type != REQ_QUIT); 940 while (type != REQ_QUIT);
847 941
878{ 972{
879 aio_req prv; 973 aio_req prv;
880 974
881 started = 0; 975 started = 0;
882 976
883 while (reqs) 977 while (prv = reqq_shift (&req_queue))
884 {
885 prv = reqs;
886 reqs = prv->next;
887 req_free (prv); 978 req_free (prv);
888 }
889 979
890 reqs = reqe = 0; 980 while (prv = reqq_shift (&res_queue))
891
892 while (ress)
893 {
894 prv = ress;
895 ress = prv->next;
896 req_free (prv); 981 req_free (prv);
897 } 982
898
899 ress = rese = 0;
900
901 close (respipe [0]); 983 close (respipe [0]);
902 close (respipe [1]); 984 close (respipe [1]);
903 create_pipe (); 985 create_pipe ();
904 986
905 atfork_parent (); 987 atfork_parent ();
906} 988}
907 989
908#define dREQ \ 990#define dREQ \
909 aio_req req; \ 991 aio_req req; \
992 int req_pri = next_pri; \
993 next_pri = DEFAULT_PRI + PRI_BIAS; \
910 \ 994 \
911 if (SvOK (callback) && !SvROK (callback)) \ 995 if (SvOK (callback) && !SvROK (callback)) \
912 croak ("callback must be undef or of reference type"); \ 996 croak ("callback must be undef or of reference type"); \
913 \ 997 \
914 Newz (0, req, 1, aio_cb); \ 998 Newz (0, req, 1, aio_cb); \
915 if (!req) \ 999 if (!req) \
916 croak ("out of memory during aio_req allocation"); \ 1000 croak ("out of memory during aio_req allocation"); \
917 \ 1001 \
918 req->callback = newSVsv (callback) 1002 req->callback = newSVsv (callback); \
1003 req->pri = req_pri
919 1004
920#define REQ_SEND \ 1005#define REQ_SEND \
921 req_send (req); \ 1006 req_send (req); \
922 \ 1007 \
923 if (GIMME_V != G_VOID) \ 1008 if (GIMME_V != G_VOID) \
1198 SV * callback 1283 SV * callback
1199 PROTOTYPE: ;$ 1284 PROTOTYPE: ;$
1200 PPCODE: 1285 PPCODE:
1201{ 1286{
1202 dREQ; 1287 dREQ;
1288
1203 req->type = REQ_GROUP; 1289 req->type = REQ_GROUP;
1204 req_send (req); 1290 req_send (req);
1291
1205 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1292 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1206} 1293}
1294
1295void
1296aio_nop (callback=&PL_sv_undef)
1297 SV * callback
1298 PPCODE:
1299{
1300 dREQ;
1301
1302 req->type = REQ_NOP;
1303
1304 REQ_SEND;
1305}
1306
1307void
1308aioreq_pri (int pri = DEFAULT_PRI)
1309 CODE:
1310 if (pri < PRI_MIN) pri = PRI_MIN;
1311 if (pri > PRI_MAX) pri = PRI_MAX;
1312 next_pri = pri + PRI_BIAS;
1207 1313
1208void 1314void
1209flush () 1315flush ()
1210 PROTOTYPE: 1316 PROTOTYPE:
1211 CODE: 1317 CODE:
1260 1366
1261MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1367MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1262 1368
1263void 1369void
1264cancel (aio_req_ornot req) 1370cancel (aio_req_ornot req)
1265 PROTOTYPE:
1266 CODE: 1371 CODE:
1267 req_cancel (req); 1372 req_cancel (req);
1268 1373
1374void
1375cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1376 CODE:
1377 SvREFCNT_dec (req->callback);
1378 req->callback = newSVsv (callback);
1379
1269MODULE = IO::AIO PACKAGE = IO::AIO::GRP 1380MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1270 1381
1271void 1382void
1272add (aio_req grp, ...) 1383add (aio_req grp, ...)
1273 PPCODE: 1384 PPCODE:
1274{ 1385{
1275 int i; 1386 int i;
1387 aio_req req;
1276 1388
1277 if (grp->fd == 2) 1389 if (grp->fd == 2)
1278 croak ("cannot add requests to IO::AIO::GRP after the group finished"); 1390 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1279 1391
1280 for (i = 1; i < items; ++i ) 1392 for (i = 1; i < items; ++i )
1281 { 1393 {
1282 if (GIMME_V != G_VOID) 1394 if (GIMME_V != G_VOID)
1283 XPUSHs (sv_2mortal (newSVsv (ST (i)))); 1395 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1284 1396
1285 aio_req req = SvAIO_REQ (ST (i)); 1397 req = SvAIO_REQ (ST (i));
1286 1398
1287 if (req) 1399 if (req)
1288 { 1400 {
1289 ++grp->length; 1401 ++grp->length;
1290 req->grp = grp; 1402 req->grp = grp;
1300 } 1412 }
1301} 1413}
1302 1414
1303void 1415void
1304result (aio_req grp, ...) 1416result (aio_req grp, ...)
1305 CODE: 1417 CODE:
1306{ 1418{
1307 int i; 1419 int i;
1308 AV *av = newAV (); 1420 AV *av = newAV ();
1309 1421
1310 for (i = 1; i < items; ++i ) 1422 for (i = 1; i < items; ++i )
1311 av_push (av, newSVsv (ST (i))); 1423 av_push (av, newSVsv (ST (i)));
1312 1424
1313 SvREFCNT_dec (grp->data); 1425 SvREFCNT_dec (grp->data);
1314 grp->data = (SV *)av; 1426 grp->data = (SV *)av;
1315} 1427}
1316 1428
1317void 1429void
1318feeder_limit (aio_req grp, int limit) 1430limit (aio_req grp, int limit)
1319 CODE: 1431 CODE:
1320 grp->fd2 = limit; 1432 grp->fd2 = limit;
1321 aio_grp_feed (grp); 1433 aio_grp_feed (grp);
1322 1434
1323void 1435void
1324set_feeder (aio_req grp, SV *callback=&PL_sv_undef) 1436feed (aio_req grp, SV *callback=&PL_sv_undef)
1325 CODE: 1437 CODE:
1326{ 1438{
1327 SvREFCNT_dec (grp->fh2); 1439 SvREFCNT_dec (grp->fh2);
1328 grp->fh2 = newSVsv (callback); 1440 grp->fh2 = newSVsv (callback);
1329 1441

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines