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.52 by root, Sun Oct 22 22:14:54 2006 UTC vs.
Revision 1.70 by root, Tue Oct 24 15:15:56 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines