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.56 by root, Mon Oct 23 14:49:51 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,
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_GROUP, REQ_NOP, 80 REQ_GROUP, REQ_NOP,
61 REQ_SLEEP, 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 */
130 return mg ? (aio_req)mg->mg_ptr : 0; 223 return mg ? (aio_req)mg->mg_ptr : 0;
131} 224}
132 225
133static void aio_grp_feed (aio_req grp) 226static void aio_grp_feed (aio_req grp)
134{ 227{
135 while (grp->length < grp->fd2) 228 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED))
136 { 229 {
137 int old_len = grp->length; 230 int old_len = grp->length;
138 231
139 if (grp->fh2 && SvOK (grp->fh2)) 232 if (grp->fh2 && SvOK (grp->fh2))
140 { 233 {
143 ENTER; 236 ENTER;
144 SAVETMPS; 237 SAVETMPS;
145 PUSHMARK (SP); 238 PUSHMARK (SP);
146 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 239 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
147 PUTBACK; 240 PUTBACK;
148 call_sv (grp->fh2, G_VOID | G_EVAL); 241 call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR);
149 SPAGAIN; 242 SPAGAIN;
150 FREETMPS; 243 FREETMPS;
151 LEAVE; 244 LEAVE;
152 } 245 }
153 246
176 } 269 }
177} 270}
178 271
179static void poll_wait () 272static void poll_wait ()
180{ 273{
181 if (nreqs && !ress)
182 {
183 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
184 FD_ZERO(&rfd); 290 FD_ZERO(&rfd);
185 FD_SET(respipe [0], &rfd); 291 FD_SET(respipe [0], &rfd);
186 292
187 select (respipe [0] + 1, &rfd, 0, 0, 0); 293 select (respipe [0] + 1, &rfd, 0, 0, 0);
188 } 294 }
189} 295}
190 296
191static void req_invoke (aio_req req) 297static void req_invoke (aio_req req)
192{ 298{
193 dSP; 299 dSP;
194 int errorno = errno;
195 300
196 if (req->cancelled || !SvOK (req->callback)) 301 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
197 return; 302 {
198
199 errno = req->errorno; 303 errno = req->errorno;
200 304
201 ENTER; 305 ENTER;
202 SAVETMPS; 306 SAVETMPS;
203 PUSHMARK (SP); 307 PUSHMARK (SP);
204 EXTEND (SP, 1); 308 EXTEND (SP, 1);
205 309
206 switch (req->type) 310 switch (req->type)
207 {
208 case REQ_READDIR:
209 { 311 {
210 SV *rv = &PL_sv_undef; 312 case REQ_READDIR:
211
212 if (req->result >= 0)
213 { 313 {
214 char *buf = req->data2ptr; 314 SV *rv = &PL_sv_undef;
215 AV *av = newAV ();
216 315
217 while (req->result) 316 if (req->result >= 0)
218 { 317 {
318 char *buf = req->data2ptr;
319 AV *av = newAV ();
320
321 while (req->result)
322 {
219 SV *sv = newSVpv (buf, 0); 323 SV *sv = newSVpv (buf, 0);
220 324
221 av_push (av, sv); 325 av_push (av, sv);
222 buf += SvCUR (sv) + 1; 326 buf += SvCUR (sv) + 1;
223 req->result--; 327 req->result--;
328 }
329
330 rv = sv_2mortal (newRV_noinc ((SV *)av));
224 } 331 }
225 332
226 rv = sv_2mortal (newRV_noinc ((SV *)av)); 333 PUSHs (rv);
227 } 334 }
335 break;
228 336
229 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;
230 } 375 }
231 break;
232 376
233 case REQ_OPEN:
234 {
235 /* convert fd to fh */
236 SV *fh;
237 377
238 PUSHs (sv_2mortal (newSViv (req->result)));
239 PUTBACK; 378 PUTBACK;
240 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
241 SPAGAIN;
242
243 fh = SvREFCNT_inc (POPs);
244
245 PUSHMARK (SP);
246 XPUSHs (sv_2mortal (fh));
247 }
248 break;
249
250 case REQ_GROUP:
251 req->fd = 2; /* mark group as finished */
252
253 if (req->data)
254 {
255 int i;
256 AV *av = (AV *)req->data;
257
258 EXTEND (SP, AvFILL (av) + 1);
259 for (i = 0; i <= AvFILL (av); ++i)
260 PUSHs (*av_fetch (av, i, 0));
261 }
262 break;
263
264 case REQ_NOP:
265 case REQ_SLEEP:
266 break;
267
268 default:
269 PUSHs (sv_2mortal (newSViv (req->result)));
270 break;
271 }
272
273
274 PUTBACK;
275 call_sv (req->callback, G_VOID | G_EVAL); 379 call_sv (req->callback, G_VOID | G_EVAL);
276 SPAGAIN; 380 SPAGAIN;
277 381
278 FREETMPS; 382 FREETMPS;
279 LEAVE; 383 LEAVE;
280
281 errno = errorno;
282
283 if (SvTRUE (ERRSV))
284 { 384 }
285 req_free (req);
286 croak (0);
287 }
288}
289 385
290static void req_free (aio_req req)
291{
292 if (req->grp) 386 if (req->grp)
293 { 387 {
294 aio_req grp = req->grp; 388 aio_req grp = req->grp;
295 389
296 /* unlink request */ 390 /* unlink request */
301 grp->grp_first = req->grp_next; 395 grp->grp_first = req->grp_next;
302 396
303 aio_grp_dec (grp); 397 aio_grp_dec (grp);
304 } 398 }
305 399
400 if (SvTRUE (ERRSV))
401 {
402 req_free (req);
403 croak (0);
404 }
405}
406
407static void req_free (aio_req req)
408{
306 if (req->self) 409 if (req->self)
307 { 410 {
308 sv_unmagic (req->self, PERL_MAGIC_ext); 411 sv_unmagic (req->self, PERL_MAGIC_ext);
309 SvREFCNT_dec (req->self); 412 SvREFCNT_dec (req->self);
310 } 413 }
321 Safefree (req); 424 Safefree (req);
322} 425}
323 426
324static void req_cancel (aio_req req) 427static void req_cancel (aio_req req)
325{ 428{
326 req->cancelled = 1; 429 req->flags |= FLAG_CANCELLED;
327 430
328 if (req->type == REQ_GROUP) 431 if (req->type == REQ_GROUP)
329 { 432 {
330 aio_req sub; 433 aio_req sub;
331 434
342 aio_req req; 445 aio_req req;
343 446
344 for (;;) 447 for (;;)
345 { 448 {
346 pthread_mutex_lock (&reslock); 449 pthread_mutex_lock (&reslock);
347 req = ress; 450 req = reqq_shift (&res_queue);
348 451
349 if (req) 452 if (req)
350 { 453 {
351 ress = req->next;
352
353 if (!ress) 454 if (!res_queue.size)
354 { 455 {
355 /* read any signals sent by the worker threads */ 456 /* read any signals sent by the worker threads */
356 char buf [32]; 457 char buf [32];
357 while (read (respipe [0], buf, 32) == 32) 458 while (read (respipe [0], buf, 32) == 32)
358 ; 459 ;
359
360 rese = 0;
361 } 460 }
362 } 461 }
363 462
364 pthread_mutex_unlock (&reslock); 463 pthread_mutex_unlock (&reslock);
365 464
428 start_thread (); 527 start_thread ();
429 528
430 ++nreqs; 529 ++nreqs;
431 530
432 pthread_mutex_lock (&reqlock); 531 pthread_mutex_lock (&reqlock);
433 532 reqq_push (&req_queue, req);
434 req->next = 0;
435
436 if (reqe)
437 {
438 reqe->next = req;
439 reqe = req;
440 }
441 else
442 reqe = reqs = req;
443
444 pthread_cond_signal (&reqwait); 533 pthread_cond_signal (&reqwait);
445 pthread_mutex_unlock (&reqlock); 534 pthread_mutex_unlock (&reqlock);
446 535
447 if (nreqs > max_outstanding) 536 if (nreqs > max_outstanding)
448 for (;;) 537 for (;;)
457} 546}
458 547
459static void end_thread (void) 548static void end_thread (void)
460{ 549{
461 aio_req req; 550 aio_req req;
551
462 Newz (0, req, 1, aio_cb); 552 Newz (0, req, 1, aio_cb);
553
463 req->type = REQ_QUIT; 554 req->type = REQ_QUIT;
555 req->pri = PRI_MAX + PRI_BIAS;
464 556
465 req_send (req); 557 req_send (req);
466} 558}
467 559
468static void min_parallel (int nthreads) 560static void min_parallel (int nthreads)
555#if !HAVE_READAHEAD 647#if !HAVE_READAHEAD
556# define readahead aio_readahead 648# define readahead aio_readahead
557 649
558static ssize_t readahead (int fd, off_t offset, size_t count) 650static ssize_t readahead (int fd, off_t offset, size_t count)
559{ 651{
560 char readahead_buf[4096]; 652 dBUF;
561 653
562 while (count > 0) 654 while (count > 0)
563 { 655 {
564 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 656 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
565 657
566 pread (fd, readahead_buf, len, offset); 658 pread (fd, aio_buf, len, offset);
567 offset += len; 659 offset += len;
568 count -= len; 660 count -= len;
569 } 661 }
662
663 fBUF;
570 664
571 errno = 0; 665 errno = 0;
572} 666}
573#endif 667#endif
574 668
660#endif 754#endif
661 ) 755 )
662 ) 756 )
663 { 757 {
664 /* emulate sendfile. this is a major pain in the ass */ 758 /* emulate sendfile. this is a major pain in the ass */
665 char buf[4096]; 759 dBUF;
760
666 res = 0; 761 res = 0;
667 762
668 while (count) 763 while (count)
669 { 764 {
670 ssize_t cnt; 765 ssize_t cnt;
671 766
672 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 767 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
673 768
674 if (cnt <= 0) 769 if (cnt <= 0)
675 { 770 {
676 if (cnt && !res) res = -1; 771 if (cnt && !res) res = -1;
677 break; 772 break;
678 } 773 }
679 774
680 cnt = write (ofd, buf, cnt); 775 cnt = write (ofd, aio_buf, cnt);
681 776
682 if (cnt <= 0) 777 if (cnt <= 0)
683 { 778 {
684 if (cnt && !res) res = -1; 779 if (cnt && !res) res = -1;
685 break; 780 break;
687 782
688 offset += cnt; 783 offset += cnt;
689 res += cnt; 784 res += cnt;
690 count -= cnt; 785 count -= cnt;
691 } 786 }
787
788 fBUF;
692 } 789 }
693 790
694 return res; 791 return res;
695} 792}
696 793
697/* read a full directory */ 794/* read a full directory */
698static int scandir_ (const char *path, void **namesp) 795static int scandir_ (const char *path, void **namesp)
699{ 796{
700 DIR *dirp = opendir (path); 797 DIR *dirp;
701 union 798 union
702 { 799 {
703 struct dirent d; 800 struct dirent d;
704 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 801 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
705 } u; 802 } *u;
706 struct dirent *entp; 803 struct dirent *entp;
707 char *name, *names; 804 char *name, *names;
708 int memlen = 4096; 805 int memlen = 4096;
709 int memofs = 0; 806 int memofs = 0;
710 int res = 0; 807 int res = 0;
711 int errorno; 808 int errorno;
712 809
810 dirp = opendir (path);
713 if (!dirp) 811 if (!dirp)
714 return -1; 812 return -1;
715 813
814 u = malloc (sizeof (*u));
716 names = malloc (memlen); 815 names = malloc (memlen);
717 816
817 if (u && names)
718 for (;;) 818 for (;;)
719 { 819 {
820 errno = 0;
720 errno = 0, readdir_r (dirp, &u.d, &entp); 821 readdir_r (dirp, &u->d, &entp);
721 822
722 if (!entp) 823 if (!entp)
723 break; 824 break;
724 825
725 name = entp->d_name; 826 name = entp->d_name;
726 827
727 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 828 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
728 { 829 {
729 int len = strlen (name) + 1; 830 int len = strlen (name) + 1;
730 831
731 res++; 832 res++;
732 833
733 while (memofs + len > memlen) 834 while (memofs + len > memlen)
734 { 835 {
735 memlen *= 2; 836 memlen *= 2;
736 names = realloc (names, memlen); 837 names = realloc (names, memlen);
737 if (!names) 838 if (!names)
738 break; 839 break;
739 } 840 }
740 841
741 memcpy (names + memofs, name, len); 842 memcpy (names + memofs, name, len);
742 memofs += len; 843 memofs += len;
743 } 844 }
744 } 845 }
745 846
746 errorno = errno; 847 errorno = errno;
848 free (u);
747 closedir (dirp); 849 closedir (dirp);
748 850
749 if (errorno) 851 if (errorno)
750 { 852 {
751 free (names); 853 free (names);
768 { 870 {
769 pthread_mutex_lock (&reqlock); 871 pthread_mutex_lock (&reqlock);
770 872
771 for (;;) 873 for (;;)
772 { 874 {
773 req = reqs; 875 req = reqq_shift (&req_queue);
774
775 if (reqs)
776 {
777 reqs = reqs->next;
778 if (!reqs) reqe = 0;
779 }
780 876
781 if (req) 877 if (req)
782 break; 878 break;
783 879
784 pthread_cond_wait (&reqwait, &reqlock); 880 pthread_cond_wait (&reqwait, &reqlock);
785 } 881 }
786 882
787 pthread_mutex_unlock (&reqlock); 883 pthread_mutex_unlock (&reqlock);
788 884
789 errno = 0; /* strictly unnecessary */ 885 errno = 0; /* strictly unnecessary */
790
791 if (!req->cancelled)
792 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)
793 { 890 {
794 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;
795 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;
796 893
797 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;
811 908
812 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 909 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
813 case REQ_FSYNC: req->result = fsync (req->fd); break; 910 case REQ_FSYNC: req->result = fsync (req->fd); break;
814 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 911 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
815 912
816 case REQ_SLEEP: 913 case REQ_BUSY:
817 { 914 {
818 struct timeval tv; 915 struct timeval tv;
819 916
820 tv.tv_sec = req->fd; 917 tv.tv_sec = req->fd;
821 tv.tv_usec = req->fd2; 918 tv.tv_usec = req->fd2;
835 932
836 req->errorno = errno; 933 req->errorno = errno;
837 934
838 pthread_mutex_lock (&reslock); 935 pthread_mutex_lock (&reslock);
839 936
840 req->next = 0; 937 if (!reqq_push (&res_queue, req))
841
842 if (rese)
843 {
844 rese->next = req;
845 rese = req;
846 }
847 else
848 {
849 rese = ress = req;
850
851 /* write a dummy byte to the pipe so fh becomes ready */ 938 /* write a dummy byte to the pipe so fh becomes ready */
852 write (respipe [1], &respipe, 1); 939 write (respipe [1], &respipe, 1);
853 }
854 940
855 pthread_mutex_unlock (&reslock); 941 pthread_mutex_unlock (&reslock);
856 } 942 }
857 while (type != REQ_QUIT); 943 while (type != REQ_QUIT);
858 944
889{ 975{
890 aio_req prv; 976 aio_req prv;
891 977
892 started = 0; 978 started = 0;
893 979
894 while (reqs) 980 while (prv = reqq_shift (&req_queue))
895 {
896 prv = reqs;
897 reqs = prv->next;
898 req_free (prv); 981 req_free (prv);
899 }
900 982
901 reqs = reqe = 0; 983 while (prv = reqq_shift (&res_queue))
902
903 while (ress)
904 {
905 prv = ress;
906 ress = prv->next;
907 req_free (prv); 984 req_free (prv);
908 } 985
909
910 ress = rese = 0;
911
912 close (respipe [0]); 986 close (respipe [0]);
913 close (respipe [1]); 987 close (respipe [1]);
914 create_pipe (); 988 create_pipe ();
915 989
916 atfork_parent (); 990 atfork_parent ();
917} 991}
918 992
919#define dREQ \ 993#define dREQ \
920 aio_req req; \ 994 aio_req req; \
995 int req_pri = next_pri; \
996 next_pri = DEFAULT_PRI + PRI_BIAS; \
921 \ 997 \
922 if (SvOK (callback) && !SvROK (callback)) \ 998 if (SvOK (callback) && !SvROK (callback)) \
923 croak ("callback must be undef or of reference type"); \ 999 croak ("callback must be undef or of reference type"); \
924 \ 1000 \
925 Newz (0, req, 1, aio_cb); \ 1001 Newz (0, req, 1, aio_cb); \
926 if (!req) \ 1002 if (!req) \
927 croak ("out of memory during aio_req allocation"); \ 1003 croak ("out of memory during aio_req allocation"); \
928 \ 1004 \
929 req->callback = newSVsv (callback) 1005 req->callback = newSVsv (callback); \
1006 req->pri = req_pri
930 1007
931#define REQ_SEND \ 1008#define REQ_SEND \
932 req_send (req); \ 1009 req_send (req); \
933 \ 1010 \
934 if (GIMME_V != G_VOID) \ 1011 if (GIMME_V != G_VOID) \
1188 1265
1189 REQ_SEND; 1266 REQ_SEND;
1190} 1267}
1191 1268
1192void 1269void
1193aio_sleep (delay,callback=&PL_sv_undef) 1270aio_busy (delay,callback=&PL_sv_undef)
1194 double delay 1271 double delay
1195 SV * callback 1272 SV * callback
1196 PPCODE: 1273 PPCODE:
1197{ 1274{
1198 dREQ; 1275 dREQ;
1199 1276
1200 req->type = REQ_SLEEP; 1277 req->type = REQ_BUSY;
1201 req->fd = delay < 0. ? 0 : delay; 1278 req->fd = delay < 0. ? 0 : delay;
1202 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); 1279 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd);
1203 1280
1204 REQ_SEND; 1281 REQ_SEND;
1205} 1282}
1209 SV * callback 1286 SV * callback
1210 PROTOTYPE: ;$ 1287 PROTOTYPE: ;$
1211 PPCODE: 1288 PPCODE:
1212{ 1289{
1213 dREQ; 1290 dREQ;
1291
1214 req->type = REQ_GROUP; 1292 req->type = REQ_GROUP;
1215 req_send (req); 1293 req_send (req);
1294
1216 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1295 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1217} 1296}
1218 1297
1219void 1298void
1220aio_nop (callback=&PL_sv_undef) 1299aio_nop (callback=&PL_sv_undef)
1225 1304
1226 req->type = REQ_NOP; 1305 req->type = REQ_NOP;
1227 1306
1228 REQ_SEND; 1307 REQ_SEND;
1229} 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;
1230 1324
1231void 1325void
1232flush () 1326flush ()
1233 PROTOTYPE: 1327 PROTOTYPE:
1234 CODE: 1328 CODE:
1283 1377
1284MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1378MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1285 1379
1286void 1380void
1287cancel (aio_req_ornot req) 1381cancel (aio_req_ornot req)
1288 PROTOTYPE:
1289 CODE: 1382 CODE:
1290 req_cancel (req); 1383 req_cancel (req);
1291 1384
1292void 1385void
1293cb (aio_req req, SV *callback=&PL_sv_undef) 1386cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1294 CODE: 1387 CODE:
1295 SvREFCNT_dec (req->callback); 1388 SvREFCNT_dec (req->callback);
1296 req->callback = newSVsv (callback); 1389 req->callback = newSVsv (callback);
1297 1390
1298MODULE = IO::AIO PACKAGE = IO::AIO::GRP 1391MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1343 SvREFCNT_dec (grp->data); 1436 SvREFCNT_dec (grp->data);
1344 grp->data = (SV *)av; 1437 grp->data = (SV *)av;
1345} 1438}
1346 1439
1347void 1440void
1348feed_limit (aio_req grp, int limit) 1441limit (aio_req grp, int limit)
1349 CODE: 1442 CODE:
1350 grp->fd2 = limit; 1443 grp->fd2 = limit;
1351 aio_grp_feed (grp); 1444 aio_grp_feed (grp);
1352 1445
1353void 1446void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines