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.69 by root, Tue Oct 24 11:57:30 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines