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.44 by root, Sat Oct 21 23:20:29 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"
9 17
10#include <pthread.h> 18#include <pthread.h>
11 19
12#include <stddef.h> 20#include <stddef.h>
13#include <errno.h> 21#include <errno.h>
22#include <sys/time.h>
23#include <sys/select.h>
14#include <sys/types.h> 24#include <sys/types.h>
15#include <sys/stat.h> 25#include <sys/stat.h>
16#include <limits.h> 26#include <limits.h>
17#include <unistd.h> 27#include <unistd.h>
18#include <fcntl.h> 28#include <fcntl.h>
39# define NAME_MAX 4096 49# define NAME_MAX 4096
40#endif 50#endif
41 51
42#if __ia64 52#if __ia64
43# define STACKSIZE 65536 53# define STACKSIZE 65536
54#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
55# define STACKSIZE PTHREAD_STACK_MIN
44#else 56#else
45# define STACKSIZE 8192 57# define STACKSIZE 16384
46#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)
47 69
48enum { 70enum {
49 REQ_QUIT, 71 REQ_QUIT,
50 REQ_OPEN, REQ_CLOSE, 72 REQ_OPEN, REQ_CLOSE,
51 REQ_READ, REQ_WRITE, REQ_READAHEAD, 73 REQ_READ, REQ_WRITE, REQ_READAHEAD,
53 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 75 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
54 REQ_FSYNC, REQ_FDATASYNC, 76 REQ_FSYNC, REQ_FDATASYNC,
55 REQ_UNLINK, REQ_RMDIR, REQ_RENAME, 77 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
56 REQ_READDIR, 78 REQ_READDIR,
57 REQ_LINK, REQ_SYMLINK, 79 REQ_LINK, REQ_SYMLINK,
58 REQ_GROUP, 80 REQ_GROUP, REQ_NOP,
81 REQ_BUSY,
59}; 82};
60 83
61#define AIO_REQ_KLASS "IO::AIO::REQ" 84#define AIO_REQ_KLASS "IO::AIO::REQ"
62#define AIO_GRP_KLASS "IO::AIO::GRP" 85#define AIO_GRP_KLASS "IO::AIO::GRP"
63 86
64typedef struct aio_cb 87typedef struct aio_cb
65{ 88{
66 struct aio_cb *grp, *grp_prev, *grp_next;
67
68 struct aio_cb *volatile next; 89 struct aio_cb *volatile next;
69
70 SV *self; /* the perl counterpart of this request, if any */
71 90
72 SV *data, *callback; 91 SV *data, *callback;
73 SV *fh, *fh2; 92 SV *fh, *fh2;
74 void *dataptr, *data2ptr; 93 void *dataptr, *data2ptr;
75 Stat_t *statdata; 94 Stat_t *statdata;
76 off_t offset; 95 off_t offset;
77 size_t length; 96 size_t length;
78 ssize_t result; 97 ssize_t result;
79 98
99 STRLEN dataoffset;
80 int type; 100 int type;
81 int fd, fd2; 101 int fd, fd2;
82 int errorno; 102 int errorno;
83 STRLEN dataoffset;
84 mode_t mode; /* open */ 103 mode_t mode; /* open */
104
85 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;
86} aio_cb; 110} aio_cb;
111
112enum {
113 FLAG_CANCELLED = 0x01,
114};
87 115
88typedef aio_cb *aio_req; 116typedef aio_cb *aio_req;
89typedef aio_cb *aio_req_ornot; 117typedef aio_cb *aio_req_ornot;
90typedef aio_cb *aio_group; 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;
91 129
92static int started, wanted; 130static int started, wanted;
93static volatile int nreqs; 131static volatile int nreqs;
94static int max_outstanding = 1<<30; 132static int max_outstanding = 1<<30;
95static int respipe [2]; 133static int respipe [2];
96 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
97static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 141static pthread_mutex_t reslock = AIO_MUTEX_INIT;
98static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 142static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
99static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 143static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
100 144
101static volatile aio_req reqs, reqe; /* queue start, queue end */ 145/*
102static 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}
198
199static void req_invoke (aio_req req);
200static void req_free (aio_req req);
103 201
104/* must be called at most once */ 202/* must be called at most once */
105static SV *req_sv (aio_req req, const char *klass) 203static SV *req_sv (aio_req req, const char *klass)
106{ 204{
205 if (!req->self)
206 {
107 req->self = (SV *)newHV (); 207 req->self = (SV *)newHV ();
108 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); 208 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
209 }
109 210
110 return sv_2mortal (sv_bless (newRV_noinc (req->self), gv_stashpv (klass, 1))); 211 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
111} 212}
112 213
113static aio_req SvAIO_REQ (SV *sv, const char *klass) 214static aio_req SvAIO_REQ (SV *sv)
114{ 215{
216 MAGIC *mg;
217
115 if (!sv_derived_from (sv, klass) || !SvROK (sv)) 218 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
116 croak ("object of class %s expected", klass); 219 croak ("object of class " AIO_REQ_KLASS " expected");
117 220
118 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 221 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
119 222
120 return mg ? (aio_req)mg->mg_ptr : 0; 223 return mg ? (aio_req)mg->mg_ptr : 0;
224}
225
226static void aio_grp_feed (aio_req grp)
227{
228 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED))
229 {
230 int old_len = grp->length;
231
232 if (grp->fh2 && SvOK (grp->fh2))
233 {
234 dSP;
235
236 ENTER;
237 SAVETMPS;
238 PUSHMARK (SP);
239 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
240 PUTBACK;
241 call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR);
242 SPAGAIN;
243 FREETMPS;
244 LEAVE;
245 }
246
247 /* stop if no progress has been made */
248 if (old_len == grp->length)
249 {
250 SvREFCNT_dec (grp->fh2);
251 grp->fh2 = 0;
252 break;
253 }
254 }
255}
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
272static void poll_wait ()
273{
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
290 FD_ZERO(&rfd);
291 FD_SET(respipe [0], &rfd);
292
293 select (respipe [0] + 1, &rfd, 0, 0, 0);
294 }
295}
296
297static void req_invoke (aio_req req)
298{
299 dSP;
300
301 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback))
302 {
303 errno = req->errorno;
304
305 ENTER;
306 SAVETMPS;
307 PUSHMARK (SP);
308 EXTEND (SP, 1);
309
310 switch (req->type)
311 {
312 case REQ_READDIR:
313 {
314 SV *rv = &PL_sv_undef;
315
316 if (req->result >= 0)
317 {
318 char *buf = req->data2ptr;
319 AV *av = newAV ();
320
321 while (req->result)
322 {
323 SV *sv = newSVpv (buf, 0);
324
325 av_push (av, sv);
326 buf += SvCUR (sv) + 1;
327 req->result--;
328 }
329
330 rv = sv_2mortal (newRV_noinc ((SV *)av));
331 }
332
333 PUSHs (rv);
334 }
335 break;
336
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;
375 }
376
377
378 PUTBACK;
379 call_sv (req->callback, G_VOID | G_EVAL);
380 SPAGAIN;
381
382 FREETMPS;
383 LEAVE;
384 }
385
386 if (req->grp)
387 {
388 aio_req grp = req->grp;
389
390 /* unlink request */
391 if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
392 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
393
394 if (grp->grp_first == req)
395 grp->grp_first = req->grp_next;
396
397 aio_grp_dec (grp);
398 }
399
400 if (SvTRUE (ERRSV))
401 {
402 req_free (req);
403 croak (0);
404 }
121} 405}
122 406
123static void req_free (aio_req req) 407static void req_free (aio_req req)
124{ 408{
125 if (req->self) 409 if (req->self)
126 { 410 {
127 sv_unmagic (req->self, PERL_MAGIC_ext); 411 sv_unmagic (req->self, PERL_MAGIC_ext);
128 SvREFCNT_dec (req->self); 412 SvREFCNT_dec (req->self);
129 } 413 }
130 414
131 if (req->data)
132 SvREFCNT_dec (req->data); 415 SvREFCNT_dec (req->data);
133
134 if (req->fh)
135 SvREFCNT_dec (req->fh); 416 SvREFCNT_dec (req->fh);
136
137 if (req->fh2)
138 SvREFCNT_dec (req->fh2); 417 SvREFCNT_dec (req->fh2);
139
140 if (req->statdata)
141 Safefree (req->statdata);
142
143 if (req->callback)
144 SvREFCNT_dec (req->callback); 418 SvREFCNT_dec (req->callback);
419 Safefree (req->statdata);
145 420
146 if (req->type == REQ_READDIR && req->result >= 0) 421 if (req->type == REQ_READDIR && req->result >= 0)
147 free (req->data2ptr); 422 free (req->data2ptr);
148 423
149 Safefree (req); 424 Safefree (req);
150} 425}
151 426
152static void 427static void req_cancel (aio_req req)
153poll_wait ()
154{ 428{
155 if (nreqs && !ress) 429 req->flags |= FLAG_CANCELLED;
156 {
157 fd_set rfd;
158 FD_ZERO(&rfd);
159 FD_SET(respipe [0], &rfd);
160 430
161 select (respipe [0] + 1, &rfd, 0, 0, 0); 431 if (req->type == REQ_GROUP)
162 } 432 {
163} 433 aio_req sub;
164 434
165static int 435 for (sub = req->grp_first; sub; sub = sub->grp_next)
166poll_cb () 436 req_cancel (sub);
437 }
438}
439
440static int poll_cb ()
167{ 441{
168 dSP; 442 dSP;
169 int count = 0; 443 int count = 0;
170 int do_croak = 0; 444 int do_croak = 0;
171 aio_req req; 445 aio_req req;
172 446
173 for (;;) 447 for (;;)
174 { 448 {
175 pthread_mutex_lock (&reslock); 449 pthread_mutex_lock (&reslock);
176 req = ress; 450 req = reqq_shift (&res_queue);
177 451
178 if (req) 452 if (req)
179 { 453 {
180 ress = req->next;
181
182 if (!ress) 454 if (!res_queue.size)
183 { 455 {
184 /* read any signals sent by the worker threads */ 456 /* read any signals sent by the worker threads */
185 char buf [32]; 457 char buf [32];
186 while (read (respipe [0], buf, 32) == 32) 458 while (read (respipe [0], buf, 32) == 32)
187 ; 459 ;
188
189 rese = 0;
190 } 460 }
191 } 461 }
192 462
193 pthread_mutex_unlock (&reslock); 463 pthread_mutex_unlock (&reslock);
194 464
195 if (!req) 465 if (!req)
196 break; 466 break;
197 467
198 nreqs--; 468 --nreqs;
199 469
200 if (req->type == REQ_QUIT) 470 if (req->type == REQ_QUIT)
201 started--; 471 started--;
472 else if (req->type == REQ_GROUP && req->length)
473 {
474 req->fd = 1; /* mark request as delayed */
475 continue;
476 }
202 else 477 else
203 { 478 {
204 int errorno = errno;
205 errno = req->errorno;
206
207 if (req->type == REQ_READ) 479 if (req->type == REQ_READ)
208 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); 480 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
209 481
210 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) 482 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
211 SvREADONLY_off (req->data); 483 SvREADONLY_off (req->data);
215 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 487 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
216 PL_laststatval = req->result; 488 PL_laststatval = req->result;
217 PL_statcache = *(req->statdata); 489 PL_statcache = *(req->statdata);
218 } 490 }
219 491
220 ENTER; 492 req_invoke (req);
221 PUSHMARK (SP);
222 493
223 if (req->type == REQ_READDIR)
224 {
225 SV *rv = &PL_sv_undef;
226
227 if (req->result >= 0)
228 {
229 char *buf = req->data2ptr;
230 AV *av = newAV ();
231
232 while (req->result)
233 {
234 SV *sv = newSVpv (buf, 0);
235
236 av_push (av, sv);
237 buf += SvCUR (sv) + 1;
238 req->result--;
239 }
240
241 rv = sv_2mortal (newRV_noinc ((SV *)av));
242 }
243
244 XPUSHs (rv);
245 }
246 else
247 {
248 XPUSHs (sv_2mortal (newSViv (req->result)));
249
250 if (req->type == REQ_OPEN)
251 {
252 /* convert fd to fh */
253 SV *fh;
254
255 PUTBACK;
256 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
257 SPAGAIN;
258
259 fh = SvREFCNT_inc (POPs);
260
261 PUSHMARK (SP);
262 XPUSHs (sv_2mortal (fh));
263 }
264 }
265
266 if (SvOK (req->callback) && !req->cancelled)
267 {
268 PUTBACK;
269 call_sv (req->callback, G_VOID | G_EVAL);
270 SPAGAIN;
271
272 if (SvTRUE (ERRSV))
273 {
274 req_free (req);
275 croak (0);
276 }
277 }
278
279 LEAVE;
280
281 errno = errorno;
282 count++; 494 count++;
283 } 495 }
284 496
285 req_free (req); 497 req_free (req);
286 } 498 }
288 return count; 500 return count;
289} 501}
290 502
291static void *aio_proc(void *arg); 503static void *aio_proc(void *arg);
292 504
293static void
294start_thread (void) 505static void start_thread (void)
295{ 506{
296 sigset_t fullsigset, oldsigset; 507 sigset_t fullsigset, oldsigset;
297 pthread_t tid; 508 pthread_t tid;
298 pthread_attr_t attr; 509 pthread_attr_t attr;
299 510
308 started++; 519 started++;
309 520
310 sigprocmask (SIG_SETMASK, &oldsigset, 0); 521 sigprocmask (SIG_SETMASK, &oldsigset, 0);
311} 522}
312 523
313static void
314req_send (aio_req req) 524static void req_send (aio_req req)
315{ 525{
316 while (started < wanted && nreqs >= started) 526 while (started < wanted && nreqs >= started)
317 start_thread (); 527 start_thread ();
318 528
319 nreqs++; 529 ++nreqs;
320 530
321 pthread_mutex_lock (&reqlock); 531 pthread_mutex_lock (&reqlock);
322 532 reqq_push (&req_queue, req);
323 req->next = 0;
324
325 if (reqe)
326 {
327 reqe->next = req;
328 reqe = req;
329 }
330 else
331 reqe = reqs = req;
332
333 pthread_cond_signal (&reqwait); 533 pthread_cond_signal (&reqwait);
334 pthread_mutex_unlock (&reqlock); 534 pthread_mutex_unlock (&reqlock);
335 535
336 if (nreqs > max_outstanding) 536 if (nreqs > max_outstanding)
337 for (;;) 537 for (;;)
343 543
344 poll_wait (); 544 poll_wait ();
345 } 545 }
346} 546}
347 547
348static void 548static void end_thread (void)
349end_thread (void)
350{ 549{
351 aio_req req; 550 aio_req req;
551
352 Newz (0, req, 1, aio_cb); 552 Newz (0, req, 1, aio_cb);
553
353 req->type = REQ_QUIT; 554 req->type = REQ_QUIT;
555 req->pri = PRI_MAX + PRI_BIAS;
354 556
355 req_send (req); 557 req_send (req);
356} 558}
357 559
358static void min_parallel (int nthreads) 560static void min_parallel (int nthreads)
405 * normal read/write by using a mutex. slows down execution a lot, 607 * normal read/write by using a mutex. slows down execution a lot,
406 * but that's your problem, not mine. 608 * but that's your problem, not mine.
407 */ 609 */
408static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; 610static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
409 611
410static ssize_t 612static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
411pread (int fd, void *buf, size_t count, off_t offset)
412{ 613{
413 ssize_t res; 614 ssize_t res;
414 off_t ooffset; 615 off_t ooffset;
415 616
416 pthread_mutex_lock (&preadwritelock); 617 pthread_mutex_lock (&preadwritelock);
421 pthread_mutex_unlock (&preadwritelock); 622 pthread_mutex_unlock (&preadwritelock);
422 623
423 return res; 624 return res;
424} 625}
425 626
426static ssize_t
427pwrite (int fd, void *buf, size_t count, off_t offset) 627static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
428{ 628{
429 ssize_t res; 629 ssize_t res;
430 off_t ooffset; 630 off_t ooffset;
431 631
432 pthread_mutex_lock (&preadwritelock); 632 pthread_mutex_lock (&preadwritelock);
445#endif 645#endif
446 646
447#if !HAVE_READAHEAD 647#if !HAVE_READAHEAD
448# define readahead aio_readahead 648# define readahead aio_readahead
449 649
450static ssize_t
451readahead (int fd, off_t offset, size_t count) 650static ssize_t readahead (int fd, off_t offset, size_t count)
452{ 651{
453 char readahead_buf[4096]; 652 dBUF;
454 653
455 while (count > 0) 654 while (count > 0)
456 { 655 {
457 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 656 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
458 657
459 pread (fd, readahead_buf, len, offset); 658 pread (fd, aio_buf, len, offset);
460 offset += len; 659 offset += len;
461 count -= len; 660 count -= len;
462 } 661 }
463 662
663 fBUF;
664
464 errno = 0; 665 errno = 0;
465} 666}
466#endif 667#endif
467 668
468#if !HAVE_READDIR_R 669#if !HAVE_READDIR_R
469# define readdir_r aio_readdir_r 670# define readdir_r aio_readdir_r
470 671
471static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; 672static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
472 673
473static int
474readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 674static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
475{ 675{
476 struct dirent *e; 676 struct dirent *e;
477 int errorno; 677 int errorno;
478 678
479 pthread_mutex_lock (&readdirlock); 679 pthread_mutex_lock (&readdirlock);
495 return e ? 0 : -1; 695 return e ? 0 : -1;
496} 696}
497#endif 697#endif
498 698
499/* sendfile always needs emulation */ 699/* sendfile always needs emulation */
500static ssize_t
501sendfile_ (int ofd, int ifd, off_t offset, size_t count) 700static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count)
502{ 701{
503 ssize_t res; 702 ssize_t res;
504 703
505 if (!count) 704 if (!count)
506 return 0; 705 return 0;
555#endif 754#endif
556 ) 755 )
557 ) 756 )
558 { 757 {
559 /* emulate sendfile. this is a major pain in the ass */ 758 /* emulate sendfile. this is a major pain in the ass */
560 char buf[4096]; 759 dBUF;
760
561 res = 0; 761 res = 0;
562 762
563 while (count) 763 while (count)
564 { 764 {
565 ssize_t cnt; 765 ssize_t cnt;
566 766
567 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 767 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
568 768
569 if (cnt <= 0) 769 if (cnt <= 0)
570 { 770 {
571 if (cnt && !res) res = -1; 771 if (cnt && !res) res = -1;
572 break; 772 break;
573 } 773 }
574 774
575 cnt = write (ofd, buf, cnt); 775 cnt = write (ofd, aio_buf, cnt);
576 776
577 if (cnt <= 0) 777 if (cnt <= 0)
578 { 778 {
579 if (cnt && !res) res = -1; 779 if (cnt && !res) res = -1;
580 break; 780 break;
582 782
583 offset += cnt; 783 offset += cnt;
584 res += cnt; 784 res += cnt;
585 count -= cnt; 785 count -= cnt;
586 } 786 }
787
788 fBUF;
587 } 789 }
588 790
589 return res; 791 return res;
590} 792}
591 793
592/* read a full directory */ 794/* read a full directory */
593static int
594scandir_ (const char *path, void **namesp) 795static int scandir_ (const char *path, void **namesp)
595{ 796{
596 DIR *dirp = opendir (path); 797 DIR *dirp;
597 union 798 union
598 { 799 {
599 struct dirent d; 800 struct dirent d;
600 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 801 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
601 } u; 802 } *u;
602 struct dirent *entp; 803 struct dirent *entp;
603 char *name, *names; 804 char *name, *names;
604 int memlen = 4096; 805 int memlen = 4096;
605 int memofs = 0; 806 int memofs = 0;
606 int res = 0; 807 int res = 0;
607 int errorno; 808 int errorno;
608 809
810 dirp = opendir (path);
609 if (!dirp) 811 if (!dirp)
610 return -1; 812 return -1;
611 813
814 u = malloc (sizeof (*u));
612 names = malloc (memlen); 815 names = malloc (memlen);
613 816
817 if (u && names)
614 for (;;) 818 for (;;)
615 { 819 {
820 errno = 0;
616 errno = 0, readdir_r (dirp, &u.d, &entp); 821 readdir_r (dirp, &u->d, &entp);
617 822
618 if (!entp) 823 if (!entp)
619 break; 824 break;
620 825
621 name = entp->d_name; 826 name = entp->d_name;
622 827
623 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 828 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
624 { 829 {
625 int len = strlen (name) + 1; 830 int len = strlen (name) + 1;
626 831
627 res++; 832 res++;
628 833
629 while (memofs + len > memlen) 834 while (memofs + len > memlen)
630 { 835 {
631 memlen *= 2; 836 memlen *= 2;
632 names = realloc (names, memlen); 837 names = realloc (names, memlen);
633 if (!names) 838 if (!names)
634 break; 839 break;
635 } 840 }
636 841
637 memcpy (names + memofs, name, len); 842 memcpy (names + memofs, name, len);
638 memofs += len; 843 memofs += len;
639 } 844 }
640 } 845 }
641 846
642 errorno = errno; 847 errorno = errno;
848 free (u);
643 closedir (dirp); 849 closedir (dirp);
644 850
645 if (errorno) 851 if (errorno)
646 { 852 {
647 free (names); 853 free (names);
653 return res; 859 return res;
654} 860}
655 861
656/*****************************************************************************/ 862/*****************************************************************************/
657 863
658static void *
659aio_proc (void *thr_arg) 864static void *aio_proc (void *thr_arg)
660{ 865{
661 aio_req req; 866 aio_req req;
662 int type; 867 int type;
663 868
664 do 869 do
665 { 870 {
666 pthread_mutex_lock (&reqlock); 871 pthread_mutex_lock (&reqlock);
667 872
668 for (;;) 873 for (;;)
669 { 874 {
670 req = reqs; 875 req = reqq_shift (&req_queue);
671
672 if (reqs)
673 {
674 reqs = reqs->next;
675 if (!reqs) reqe = 0;
676 }
677 876
678 if (req) 877 if (req)
679 break; 878 break;
680 879
681 pthread_cond_wait (&reqwait, &reqlock); 880 pthread_cond_wait (&reqwait, &reqlock);
682 } 881 }
683 882
684 pthread_mutex_unlock (&reqlock); 883 pthread_mutex_unlock (&reqlock);
685 884
686 errno = 0; /* strictly unnecessary */ 885 errno = 0; /* strictly unnecessary */
886 type = req->type; /* remember type for QUIT check */
687 887
688 if (!req->cancelled) 888 if (!(req->flags & FLAG_CANCELLED))
689 switch (req->type) 889 switch (type)
690 { 890 {
691 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;
692 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;
693 893
694 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;
708 908
709 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 909 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
710 case REQ_FSYNC: req->result = fsync (req->fd); break; 910 case REQ_FSYNC: req->result = fsync (req->fd); break;
711 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 911 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
712 912
913 case REQ_BUSY:
914 {
915 struct timeval tv;
916
917 tv.tv_sec = req->fd;
918 tv.tv_usec = req->fd2;
919
920 req->result = select (0, 0, 0, 0, &tv);
921 }
922
923 case REQ_GROUP:
924 case REQ_NOP:
713 case REQ_QUIT: 925 case REQ_QUIT:
714 break; 926 break;
715 927
716 default: 928 default:
717 req->result = ENOSYS; 929 req->result = ENOSYS;
720 932
721 req->errorno = errno; 933 req->errorno = errno;
722 934
723 pthread_mutex_lock (&reslock); 935 pthread_mutex_lock (&reslock);
724 936
725 req->next = 0; 937 if (!reqq_push (&res_queue, req))
726
727 if (rese)
728 {
729 rese->next = req;
730 rese = req;
731 }
732 else
733 {
734 rese = ress = req;
735
736 /* write a dummy byte to the pipe so fh becomes ready */ 938 /* write a dummy byte to the pipe so fh becomes ready */
737 write (respipe [1], &respipe, 1); 939 write (respipe [1], &respipe, 1);
738 }
739 940
740 pthread_mutex_unlock (&reslock); 941 pthread_mutex_unlock (&reslock);
741 } 942 }
742 while (type != REQ_QUIT); 943 while (type != REQ_QUIT);
743 944
774{ 975{
775 aio_req prv; 976 aio_req prv;
776 977
777 started = 0; 978 started = 0;
778 979
779 while (reqs) 980 while (prv = reqq_shift (&req_queue))
780 {
781 prv = reqs;
782 reqs = prv->next;
783 req_free (prv); 981 req_free (prv);
784 }
785 982
786 reqs = reqe = 0; 983 while (prv = reqq_shift (&res_queue))
787
788 while (ress)
789 {
790 prv = ress;
791 ress = prv->next;
792 req_free (prv); 984 req_free (prv);
793 } 985
794
795 ress = rese = 0;
796
797 close (respipe [0]); 986 close (respipe [0]);
798 close (respipe [1]); 987 close (respipe [1]);
799 create_pipe (); 988 create_pipe ();
800 989
801 atfork_parent (); 990 atfork_parent ();
802} 991}
803 992
804#define dREQ \ 993#define dREQ \
805 aio_req req; \ 994 aio_req req; \
995 int req_pri = next_pri; \
996 next_pri = DEFAULT_PRI + PRI_BIAS; \
806 \ 997 \
807 if (SvOK (callback) && !SvROK (callback)) \ 998 if (SvOK (callback) && !SvROK (callback)) \
808 croak ("callback must be undef or of reference type"); \ 999 croak ("callback must be undef or of reference type"); \
809 \ 1000 \
810 Newz (0, req, 1, aio_cb); \ 1001 Newz (0, req, 1, aio_cb); \
811 if (!req) \ 1002 if (!req) \
812 croak ("out of memory during aio_req allocation"); \ 1003 croak ("out of memory during aio_req allocation"); \
813 \ 1004 \
814 req->callback = newSVsv (callback) 1005 req->callback = newSVsv (callback); \
1006 req->pri = req_pri
815 1007
816#define REQ_SEND \ 1008#define REQ_SEND \
817 req_send (req); \ 1009 req_send (req); \
818 \ 1010 \
819 if (GIMME_V != G_VOID) \ 1011 if (GIMME_V != G_VOID) \
1073 1265
1074 REQ_SEND; 1266 REQ_SEND;
1075} 1267}
1076 1268
1077void 1269void
1270aio_busy (delay,callback=&PL_sv_undef)
1271 double delay
1272 SV * callback
1273 PPCODE:
1274{
1275 dREQ;
1276
1277 req->type = REQ_BUSY;
1278 req->fd = delay < 0. ? 0 : delay;
1279 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd);
1280
1281 REQ_SEND;
1282}
1283
1284void
1078aio_group (callback=&PL_sv_undef) 1285aio_group (callback=&PL_sv_undef)
1079 SV * callback 1286 SV * callback
1080 PROTOTYPE: ;& 1287 PROTOTYPE: ;$
1081 PPCODE: 1288 PPCODE:
1082{ 1289{
1083 dREQ; 1290 dREQ;
1291
1084 req->type = REQ_GROUP; 1292 req->type = REQ_GROUP;
1293 req_send (req);
1294
1085 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1295 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1086} 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;
1087 1324
1088void 1325void
1089flush () 1326flush ()
1090 PROTOTYPE: 1327 PROTOTYPE:
1091 CODE: 1328 CODE:
1134 CODE: 1371 CODE:
1135 RETVAL = nreqs; 1372 RETVAL = nreqs;
1136 OUTPUT: 1373 OUTPUT:
1137 RETVAL 1374 RETVAL
1138 1375
1376PROTOTYPES: DISABLE
1377
1139MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1378MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1140 1379
1141void 1380void
1142cancel (aio_req_ornot req) 1381cancel (aio_req_ornot req)
1143 PROTOTYPE:
1144 CODE: 1382 CODE:
1145 req->cancelled = 1; 1383 req_cancel (req);
1146 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
1391MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1392
1393void
1394add (aio_req grp, ...)
1395 PPCODE:
1396{
1397 int i;
1398 aio_req req;
1399
1400 if (grp->fd == 2)
1401 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1402
1403 for (i = 1; i < items; ++i )
1404 {
1405 if (GIMME_V != G_VOID)
1406 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1407
1408 req = SvAIO_REQ (ST (i));
1409
1410 if (req)
1411 {
1412 ++grp->length;
1413 req->grp = grp;
1414
1415 req->grp_prev = 0;
1416 req->grp_next = grp->grp_first;
1417
1418 if (grp->grp_first)
1419 grp->grp_first->grp_prev = req;
1420
1421 grp->grp_first = req;
1422 }
1423 }
1424}
1425
1426void
1427result (aio_req grp, ...)
1428 CODE:
1429{
1430 int i;
1431 AV *av = newAV ();
1432
1433 for (i = 1; i < items; ++i )
1434 av_push (av, newSVsv (ST (i)));
1435
1436 SvREFCNT_dec (grp->data);
1437 grp->data = (SV *)av;
1438}
1439
1440void
1441limit (aio_req grp, int limit)
1442 CODE:
1443 grp->fd2 = limit;
1444 aio_grp_feed (grp);
1445
1446void
1447feed (aio_req grp, SV *callback=&PL_sv_undef)
1448 CODE:
1449{
1450 SvREFCNT_dec (grp->fh2);
1451 grp->fh2 = newSVsv (callback);
1452
1453 if (grp->fd2 <= 0)
1454 grp->fd2 = 2;
1455
1456 aio_grp_feed (grp);
1457}
1458

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines