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.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;
105 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}
198
199static void req_invoke (aio_req req);
106static void req_free (aio_req req); 200static void req_free (aio_req req);
107 201
108/* must be called at most once */ 202/* must be called at most once */
109static SV *req_sv (aio_req req, const char *klass) 203static SV *req_sv (aio_req req, const char *klass)
110{ 204{
117 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)));
118} 212}
119 213
120static aio_req SvAIO_REQ (SV *sv) 214static aio_req SvAIO_REQ (SV *sv)
121{ 215{
216 MAGIC *mg;
217
122 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) 218 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
123 croak ("object of class " AIO_REQ_KLASS " expected"); 219 croak ("object of class " AIO_REQ_KLASS " expected");
124 220
125 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 221 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
126 222
127 return mg ? (aio_req)mg->mg_ptr : 0; 223 return mg ? (aio_req)mg->mg_ptr : 0;
128} 224}
129 225
130static void aio_grp_feed (aio_req grp) 226static void aio_grp_feed (aio_req grp)
131{ 227{
132 while (grp->length < grp->fd2) 228 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED))
133 { 229 {
134 int old_len = grp->length; 230 int old_len = grp->length;
135 231
136 if (grp->fh2 && SvOK (grp->fh2)) 232 if (grp->fh2 && SvOK (grp->fh2))
137 { 233 {
140 ENTER; 236 ENTER;
141 SAVETMPS; 237 SAVETMPS;
142 PUSHMARK (SP); 238 PUSHMARK (SP);
143 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 239 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
144 PUTBACK; 240 PUTBACK;
145 call_sv (grp->fh2, G_VOID | G_EVAL); 241 call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR);
146 SPAGAIN; 242 SPAGAIN;
147 FREETMPS; 243 FREETMPS;
148 LEAVE; 244 LEAVE;
149 } 245 }
150 246
156 break; 252 break;
157 } 253 }
158 } 254 }
159} 255}
160 256
257static void aio_grp_dec (aio_req grp)
258{
259 --grp->length;
260
261 /* call feeder, if applicable */
262 aio_grp_feed (grp);
263
264 /* finish, if done */
265 if (!grp->length && grp->fd)
266 {
267 req_invoke (grp);
268 req_free (grp);
269 }
270}
271
161static void poll_wait () 272static void poll_wait ()
162{ 273{
163 if (nreqs && !ress)
164 {
165 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
166 FD_ZERO(&rfd); 290 FD_ZERO(&rfd);
167 FD_SET(respipe [0], &rfd); 291 FD_SET(respipe [0], &rfd);
168 292
169 select (respipe [0] + 1, &rfd, 0, 0, 0); 293 select (respipe [0] + 1, &rfd, 0, 0, 0);
170 } 294 }
171} 295}
172 296
173static void req_invoke (aio_req req) 297static void req_invoke (aio_req req)
174{ 298{
175 dSP; 299 dSP;
176 int errorno = errno;
177 300
178 if (req->cancelled || !SvOK (req->callback)) 301 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
179 return; 302 {
180
181 errno = req->errorno; 303 errno = req->errorno;
182 304
183 ENTER; 305 ENTER;
184 SAVETMPS; 306 SAVETMPS;
185 PUSHMARK (SP); 307 PUSHMARK (SP);
186 EXTEND (SP, 1); 308 EXTEND (SP, 1);
187 309
188 switch (req->type) 310 switch (req->type)
189 {
190 case REQ_READDIR:
191 { 311 {
192 SV *rv = &PL_sv_undef; 312 case REQ_READDIR:
193
194 if (req->result >= 0)
195 { 313 {
196 char *buf = req->data2ptr; 314 SV *rv = &PL_sv_undef;
197 AV *av = newAV ();
198 315
199 while (req->result) 316 if (req->result >= 0)
200 { 317 {
318 char *buf = req->data2ptr;
319 AV *av = newAV ();
320
321 while (req->result)
322 {
201 SV *sv = newSVpv (buf, 0); 323 SV *sv = newSVpv (buf, 0);
202 324
203 av_push (av, sv); 325 av_push (av, sv);
204 buf += SvCUR (sv) + 1; 326 buf += SvCUR (sv) + 1;
205 req->result--; 327 req->result--;
328 }
329
330 rv = sv_2mortal (newRV_noinc ((SV *)av));
206 } 331 }
207 332
208 rv = sv_2mortal (newRV_noinc ((SV *)av)); 333 PUSHs (rv);
209 } 334 }
335 break;
210 336
211 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;
212 } 375 }
213 break;
214 376
215 case REQ_OPEN:
216 {
217 /* convert fd to fh */
218 SV *fh;
219 377
220 PUSHs (sv_2mortal (newSViv (req->result)));
221 PUTBACK; 378 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); 379 call_sv (req->callback, G_VOID | G_EVAL);
257 SPAGAIN; 380 SPAGAIN;
258 381
259 if (SvTRUE (ERRSV))
260 {
261 req_free (req);
262 croak (0);
263 }
264
265 FREETMPS; 382 FREETMPS;
266 LEAVE; 383 LEAVE;
384 }
267 385
268 errno = errorno;
269}
270
271static void req_free (aio_req req)
272{
273 if (req->grp) 386 if (req->grp)
274 { 387 {
275 aio_req grp = req->grp; 388 aio_req grp = req->grp;
276 389
277 /* unlink request */ 390 /* unlink request */
279 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; 392 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
280 393
281 if (grp->grp_first == req) 394 if (grp->grp_first == req)
282 grp->grp_first = req->grp_next; 395 grp->grp_first = req->grp_next;
283 396
284 --grp->length;
285
286 /* call feeder, if applicable */
287 aio_grp_feed (grp); 397 aio_grp_dec (grp);
398 }
288 399
289 /* finish, if done */ 400 if (SvTRUE (ERRSV))
290 if (!grp->length && grp->fd) 401 {
291 {
292 req_invoke (grp);
293 req_free (grp); 402 req_free (req);
294 } 403 croak (0);
295 } 404 }
405}
296 406
407static void req_free (aio_req req)
408{
297 if (req->self) 409 if (req->self)
298 { 410 {
299 sv_unmagic (req->self, PERL_MAGIC_ext); 411 sv_unmagic (req->self, PERL_MAGIC_ext);
300 SvREFCNT_dec (req->self); 412 SvREFCNT_dec (req->self);
301 } 413 }
312 Safefree (req); 424 Safefree (req);
313} 425}
314 426
315static void req_cancel (aio_req req) 427static void req_cancel (aio_req req)
316{ 428{
317 req->cancelled = 1; 429 req->flags |= FLAG_CANCELLED;
318 430
319 if (req->type == REQ_GROUP) 431 if (req->type == REQ_GROUP)
320 { 432 {
321 aio_req sub; 433 aio_req sub;
322 434
333 aio_req req; 445 aio_req req;
334 446
335 for (;;) 447 for (;;)
336 { 448 {
337 pthread_mutex_lock (&reslock); 449 pthread_mutex_lock (&reslock);
338 req = ress; 450 req = reqq_shift (&res_queue);
339 451
340 if (req) 452 if (req)
341 { 453 {
342 ress = req->next;
343
344 if (!ress) 454 if (!res_queue.size)
345 { 455 {
346 /* read any signals sent by the worker threads */ 456 /* read any signals sent by the worker threads */
347 char buf [32]; 457 char buf [32];
348 while (read (respipe [0], buf, 32) == 32) 458 while (read (respipe [0], buf, 32) == 32)
349 ; 459 ;
350
351 rese = 0;
352 } 460 }
353 } 461 }
354 462
355 pthread_mutex_unlock (&reslock); 463 pthread_mutex_unlock (&reslock);
356 464
357 if (!req) 465 if (!req)
358 break; 466 break;
359 467
360 nreqs--; 468 --nreqs;
361 469
362 if (req->type == REQ_QUIT) 470 if (req->type == REQ_QUIT)
363 started--; 471 started--;
364 else if (req->type == REQ_GROUP && req->length) 472 else if (req->type == REQ_GROUP && req->length)
365 { 473 {
416static void req_send (aio_req req) 524static void req_send (aio_req req)
417{ 525{
418 while (started < wanted && nreqs >= started) 526 while (started < wanted && nreqs >= started)
419 start_thread (); 527 start_thread ();
420 528
421 nreqs++; 529 ++nreqs;
422 530
423 pthread_mutex_lock (&reqlock); 531 pthread_mutex_lock (&reqlock);
424 532 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); 533 pthread_cond_signal (&reqwait);
436 pthread_mutex_unlock (&reqlock); 534 pthread_mutex_unlock (&reqlock);
437 535
438 if (nreqs > max_outstanding) 536 if (nreqs > max_outstanding)
439 for (;;) 537 for (;;)
448} 546}
449 547
450static void end_thread (void) 548static void end_thread (void)
451{ 549{
452 aio_req req; 550 aio_req req;
551
453 Newz (0, req, 1, aio_cb); 552 Newz (0, req, 1, aio_cb);
553
454 req->type = REQ_QUIT; 554 req->type = REQ_QUIT;
555 req->pri = PRI_MAX + PRI_BIAS;
455 556
456 req_send (req); 557 req_send (req);
457} 558}
458 559
459static void min_parallel (int nthreads) 560static void min_parallel (int nthreads)
546#if !HAVE_READAHEAD 647#if !HAVE_READAHEAD
547# define readahead aio_readahead 648# define readahead aio_readahead
548 649
549static ssize_t readahead (int fd, off_t offset, size_t count) 650static ssize_t readahead (int fd, off_t offset, size_t count)
550{ 651{
551 char readahead_buf[4096]; 652 dBUF;
552 653
553 while (count > 0) 654 while (count > 0)
554 { 655 {
555 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 656 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
556 657
557 pread (fd, readahead_buf, len, offset); 658 pread (fd, aio_buf, len, offset);
558 offset += len; 659 offset += len;
559 count -= len; 660 count -= len;
560 } 661 }
662
663 fBUF;
561 664
562 errno = 0; 665 errno = 0;
563} 666}
564#endif 667#endif
565 668
651#endif 754#endif
652 ) 755 )
653 ) 756 )
654 { 757 {
655 /* emulate sendfile. this is a major pain in the ass */ 758 /* emulate sendfile. this is a major pain in the ass */
656 char buf[4096]; 759 dBUF;
760
657 res = 0; 761 res = 0;
658 762
659 while (count) 763 while (count)
660 { 764 {
661 ssize_t cnt; 765 ssize_t cnt;
662 766
663 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 767 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
664 768
665 if (cnt <= 0) 769 if (cnt <= 0)
666 { 770 {
667 if (cnt && !res) res = -1; 771 if (cnt && !res) res = -1;
668 break; 772 break;
669 } 773 }
670 774
671 cnt = write (ofd, buf, cnt); 775 cnt = write (ofd, aio_buf, cnt);
672 776
673 if (cnt <= 0) 777 if (cnt <= 0)
674 { 778 {
675 if (cnt && !res) res = -1; 779 if (cnt && !res) res = -1;
676 break; 780 break;
678 782
679 offset += cnt; 783 offset += cnt;
680 res += cnt; 784 res += cnt;
681 count -= cnt; 785 count -= cnt;
682 } 786 }
787
788 fBUF;
683 } 789 }
684 790
685 return res; 791 return res;
686} 792}
687 793
688/* read a full directory */ 794/* read a full directory */
689static int scandir_ (const char *path, void **namesp) 795static int scandir_ (const char *path, void **namesp)
690{ 796{
691 DIR *dirp = opendir (path); 797 DIR *dirp;
692 union 798 union
693 { 799 {
694 struct dirent d; 800 struct dirent d;
695 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 801 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
696 } u; 802 } *u;
697 struct dirent *entp; 803 struct dirent *entp;
698 char *name, *names; 804 char *name, *names;
699 int memlen = 4096; 805 int memlen = 4096;
700 int memofs = 0; 806 int memofs = 0;
701 int res = 0; 807 int res = 0;
702 int errorno; 808 int errorno;
703 809
810 dirp = opendir (path);
704 if (!dirp) 811 if (!dirp)
705 return -1; 812 return -1;
706 813
814 u = malloc (sizeof (*u));
707 names = malloc (memlen); 815 names = malloc (memlen);
708 816
817 if (u && names)
709 for (;;) 818 for (;;)
710 { 819 {
820 errno = 0;
711 errno = 0, readdir_r (dirp, &u.d, &entp); 821 readdir_r (dirp, &u->d, &entp);
712 822
713 if (!entp) 823 if (!entp)
714 break; 824 break;
715 825
716 name = entp->d_name; 826 name = entp->d_name;
717 827
718 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 828 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
719 { 829 {
720 int len = strlen (name) + 1; 830 int len = strlen (name) + 1;
721 831
722 res++; 832 res++;
723 833
724 while (memofs + len > memlen) 834 while (memofs + len > memlen)
725 { 835 {
726 memlen *= 2; 836 memlen *= 2;
727 names = realloc (names, memlen); 837 names = realloc (names, memlen);
728 if (!names) 838 if (!names)
729 break; 839 break;
730 } 840 }
731 841
732 memcpy (names + memofs, name, len); 842 memcpy (names + memofs, name, len);
733 memofs += len; 843 memofs += len;
734 } 844 }
735 } 845 }
736 846
737 errorno = errno; 847 errorno = errno;
848 free (u);
738 closedir (dirp); 849 closedir (dirp);
739 850
740 if (errorno) 851 if (errorno)
741 { 852 {
742 free (names); 853 free (names);
759 { 870 {
760 pthread_mutex_lock (&reqlock); 871 pthread_mutex_lock (&reqlock);
761 872
762 for (;;) 873 for (;;)
763 { 874 {
764 req = reqs; 875 req = reqq_shift (&req_queue);
765
766 if (reqs)
767 {
768 reqs = reqs->next;
769 if (!reqs) reqe = 0;
770 }
771 876
772 if (req) 877 if (req)
773 break; 878 break;
774 879
775 pthread_cond_wait (&reqwait, &reqlock); 880 pthread_cond_wait (&reqwait, &reqlock);
776 } 881 }
777 882
778 pthread_mutex_unlock (&reqlock); 883 pthread_mutex_unlock (&reqlock);
779 884
780 errno = 0; /* strictly unnecessary */ 885 errno = 0; /* strictly unnecessary */
886 type = req->type; /* remember type for QUIT check */
781 887
782 if (!req->cancelled) 888 if (!(req->flags & FLAG_CANCELLED))
783 switch (req->type) 889 switch (type)
784 { 890 {
785 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;
786 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;
787 893
788 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;
802 908
803 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 909 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
804 case REQ_FSYNC: req->result = fsync (req->fd); break; 910 case REQ_FSYNC: req->result = fsync (req->fd); break;
805 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 911 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
806 912
807 case REQ_SLEEP: 913 case REQ_BUSY:
808 { 914 {
809 struct timeval tv; 915 struct timeval tv;
810 916
811 tv.tv_sec = req->fd; 917 tv.tv_sec = req->fd;
812 tv.tv_usec = req->fd2; 918 tv.tv_usec = req->fd2;
813 919
814 req->result = select (0, 0, 0, 0, &tv); 920 req->result = select (0, 0, 0, 0, &tv);
815 } 921 }
816 922
923 case REQ_GROUP:
924 case REQ_NOP:
817 case REQ_QUIT: 925 case REQ_QUIT:
818 break; 926 break;
819 927
820 default: 928 default:
821 req->result = ENOSYS; 929 req->result = ENOSYS;
824 932
825 req->errorno = errno; 933 req->errorno = errno;
826 934
827 pthread_mutex_lock (&reslock); 935 pthread_mutex_lock (&reslock);
828 936
829 req->next = 0; 937 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 */ 938 /* write a dummy byte to the pipe so fh becomes ready */
841 write (respipe [1], &respipe, 1); 939 write (respipe [1], &respipe, 1);
842 }
843 940
844 pthread_mutex_unlock (&reslock); 941 pthread_mutex_unlock (&reslock);
845 } 942 }
846 while (type != REQ_QUIT); 943 while (type != REQ_QUIT);
847 944
878{ 975{
879 aio_req prv; 976 aio_req prv;
880 977
881 started = 0; 978 started = 0;
882 979
883 while (reqs) 980 while (prv = reqq_shift (&req_queue))
884 {
885 prv = reqs;
886 reqs = prv->next;
887 req_free (prv); 981 req_free (prv);
888 }
889 982
890 reqs = reqe = 0; 983 while (prv = reqq_shift (&res_queue))
891
892 while (ress)
893 {
894 prv = ress;
895 ress = prv->next;
896 req_free (prv); 984 req_free (prv);
897 } 985
898
899 ress = rese = 0;
900
901 close (respipe [0]); 986 close (respipe [0]);
902 close (respipe [1]); 987 close (respipe [1]);
903 create_pipe (); 988 create_pipe ();
904 989
905 atfork_parent (); 990 atfork_parent ();
906} 991}
907 992
908#define dREQ \ 993#define dREQ \
909 aio_req req; \ 994 aio_req req; \
995 int req_pri = next_pri; \
996 next_pri = DEFAULT_PRI + PRI_BIAS; \
910 \ 997 \
911 if (SvOK (callback) && !SvROK (callback)) \ 998 if (SvOK (callback) && !SvROK (callback)) \
912 croak ("callback must be undef or of reference type"); \ 999 croak ("callback must be undef or of reference type"); \
913 \ 1000 \
914 Newz (0, req, 1, aio_cb); \ 1001 Newz (0, req, 1, aio_cb); \
915 if (!req) \ 1002 if (!req) \
916 croak ("out of memory during aio_req allocation"); \ 1003 croak ("out of memory during aio_req allocation"); \
917 \ 1004 \
918 req->callback = newSVsv (callback) 1005 req->callback = newSVsv (callback); \
1006 req->pri = req_pri
919 1007
920#define REQ_SEND \ 1008#define REQ_SEND \
921 req_send (req); \ 1009 req_send (req); \
922 \ 1010 \
923 if (GIMME_V != G_VOID) \ 1011 if (GIMME_V != G_VOID) \
1177 1265
1178 REQ_SEND; 1266 REQ_SEND;
1179} 1267}
1180 1268
1181void 1269void
1182aio_sleep (delay,callback=&PL_sv_undef) 1270aio_busy (delay,callback=&PL_sv_undef)
1183 double delay 1271 double delay
1184 SV * callback 1272 SV * callback
1185 PPCODE: 1273 PPCODE:
1186{ 1274{
1187 dREQ; 1275 dREQ;
1188 1276
1189 req->type = REQ_SLEEP; 1277 req->type = REQ_BUSY;
1190 req->fd = delay < 0. ? 0 : delay; 1278 req->fd = delay < 0. ? 0 : delay;
1191 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); 1279 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd);
1192 1280
1193 REQ_SEND; 1281 REQ_SEND;
1194} 1282}
1198 SV * callback 1286 SV * callback
1199 PROTOTYPE: ;$ 1287 PROTOTYPE: ;$
1200 PPCODE: 1288 PPCODE:
1201{ 1289{
1202 dREQ; 1290 dREQ;
1291
1203 req->type = REQ_GROUP; 1292 req->type = REQ_GROUP;
1204 req_send (req); 1293 req_send (req);
1294
1205 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1295 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1206} 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;
1207 1324
1208void 1325void
1209flush () 1326flush ()
1210 PROTOTYPE: 1327 PROTOTYPE:
1211 CODE: 1328 CODE:
1260 1377
1261MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1378MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1262 1379
1263void 1380void
1264cancel (aio_req_ornot req) 1381cancel (aio_req_ornot req)
1265 PROTOTYPE:
1266 CODE: 1382 CODE:
1267 req_cancel (req); 1383 req_cancel (req);
1268 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
1269MODULE = IO::AIO PACKAGE = IO::AIO::GRP 1391MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1270 1392
1271void 1393void
1272add (aio_req grp, ...) 1394add (aio_req grp, ...)
1273 PPCODE: 1395 PPCODE:
1274{ 1396{
1275 int i; 1397 int i;
1398 aio_req req;
1276 1399
1277 if (grp->fd == 2) 1400 if (grp->fd == 2)
1278 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");
1279 1402
1280 for (i = 1; i < items; ++i ) 1403 for (i = 1; i < items; ++i )
1281 { 1404 {
1282 if (GIMME_V != G_VOID) 1405 if (GIMME_V != G_VOID)
1283 XPUSHs (sv_2mortal (newSVsv (ST (i)))); 1406 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1284 1407
1285 aio_req req = SvAIO_REQ (ST (i)); 1408 req = SvAIO_REQ (ST (i));
1286 1409
1287 if (req) 1410 if (req)
1288 { 1411 {
1289 ++grp->length; 1412 ++grp->length;
1290 req->grp = grp; 1413 req->grp = grp;
1300 } 1423 }
1301} 1424}
1302 1425
1303void 1426void
1304result (aio_req grp, ...) 1427result (aio_req grp, ...)
1305 CODE: 1428 CODE:
1306{ 1429{
1307 int i; 1430 int i;
1308 AV *av = newAV (); 1431 AV *av = newAV ();
1309 1432
1310 for (i = 1; i < items; ++i ) 1433 for (i = 1; i < items; ++i )
1311 av_push (av, newSVsv (ST (i))); 1434 av_push (av, newSVsv (ST (i)));
1312 1435
1313 SvREFCNT_dec (grp->data); 1436 SvREFCNT_dec (grp->data);
1314 grp->data = (SV *)av; 1437 grp->data = (SV *)av;
1315} 1438}
1316 1439
1317void 1440void
1318feeder_limit (aio_req grp, int limit) 1441limit (aio_req grp, int limit)
1319 CODE: 1442 CODE:
1320 grp->fd2 = limit; 1443 grp->fd2 = limit;
1321 aio_grp_feed (grp); 1444 aio_grp_feed (grp);
1322 1445
1323void 1446void
1324set_feeder (aio_req grp, SV *callback=&PL_sv_undef) 1447feed (aio_req grp, SV *callback=&PL_sv_undef)
1325 CODE: 1448 CODE:
1326{ 1449{
1327 SvREFCNT_dec (grp->fh2); 1450 SvREFCNT_dec (grp->fh2);
1328 grp->fh2 = newSVsv (callback); 1451 grp->fh2 = newSVsv (callback);
1329 1452

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines