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.43 by root, Sat Oct 21 23:06:04 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,
80 REQ_GROUP, REQ_NOP,
81 REQ_BUSY,
58}; 82};
59 83
60#define AIO_CB_KLASS "IO::AIO::CB" 84#define AIO_REQ_KLASS "IO::AIO::REQ"
85#define AIO_GRP_KLASS "IO::AIO::GRP"
61 86
62typedef struct aio_cb 87typedef struct aio_cb
63{ 88{
64 struct aio_cb *grp_prev, *grp_next;
65 struct aio_grp *grp;
66
67 struct aio_cb *volatile next; 89 struct aio_cb *volatile next;
68
69 SV *self; /* the perl counterpart of this request, if any */
70 90
71 SV *data, *callback; 91 SV *data, *callback;
72 SV *fh, *fh2; 92 SV *fh, *fh2;
73 void *dataptr, *data2ptr; 93 void *dataptr, *data2ptr;
74 Stat_t *statdata; 94 Stat_t *statdata;
75 off_t offset; 95 off_t offset;
76 size_t length; 96 size_t length;
77 ssize_t result; 97 ssize_t result;
78 98
99 STRLEN dataoffset;
79 int type; 100 int type;
80 int fd, fd2; 101 int fd, fd2;
81 int errorno; 102 int errorno;
82 STRLEN dataoffset;
83 mode_t mode; /* open */ 103 mode_t mode; /* open */
104
84 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;
85} aio_cb; 110} aio_cb;
111
112enum {
113 FLAG_CANCELLED = 0x01,
114};
86 115
87typedef aio_cb *aio_req; 116typedef aio_cb *aio_req;
88typedef 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;
89 129
90static int started, wanted; 130static int started, wanted;
91static volatile int nreqs; 131static volatile int nreqs;
92static int max_outstanding = 1<<30; 132static int max_outstanding = 1<<30;
93static int respipe [2]; 133static int respipe [2];
94 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
95static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 141static pthread_mutex_t reslock = AIO_MUTEX_INIT;
96static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 142static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
97static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 143static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
98 144
99static volatile aio_req reqs, reqe; /* queue start, queue end */ 145/*
100static volatile aio_req ress, rese; /* queue start, queue end */ 146 * a somewhat faster data structure might be nice, but
101 147 * with 8 priorities this actually needs <20 insns
148 * per shift, the most expensive operation.
149 */
102typedef struct aio_grp 150typedef struct {
103{ 151 aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
104 struct aio_cb *first, *last; 152 int size;
105 SV *callback; 153} reqq;
106 int busycount;
107} aio_grp;
108 154
109static void aio_grp_begin (aio_grp *grp) 155static reqq req_queue;
110{ 156static reqq res_queue;
111 ++grp->busycount;
112}
113 157
114static void aio_grp_end (aio_grp *grp) 158int reqq_push (reqq *q, aio_req req)
115{ 159{
116 --grp->busycount; 160 int pri = req->pri;
161 req->next = 0;
117 162
118 if (grp->busycount) 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)
119 return; 179 return 0;
120 180
121 SvREFCNT_dec (grp->callback); 181 --q->size;
122 grp->callback = 0;
123}
124 182
125static aio_grp *aio_grp_new () 183 for (pri = NUM_PRI; pri--; )
126{ 184 {
127 aio_grp *grp; 185 aio_req req = q->qs[pri];
128 186
129 Newz (0, grp, 1, aio_grp); 187 if (req)
130 aio_grp_begin (grp); 188 {
189 if (!(q->qs[pri] = req->next))
190 q->qe[pri] = 0;
131 191
132 return grp; 192 return req;
193 }
194 }
195
196 abort ();
133} 197}
198
199static void req_invoke (aio_req req);
200static void req_free (aio_req req);
134 201
135/* must be called at most once */ 202/* must be called at most once */
136static SV *req_sv (aio_req req) 203static SV *req_sv (aio_req req, const char *klass)
137{ 204{
205 if (!req->self)
206 {
138 req->self = (SV *)newHV (); 207 req->self = (SV *)newHV ();
139 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); 208 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
209 }
140 210
141 return sv_bless (newRV_noinc (req->self), gv_stashpv (AIO_CB_KLASS, 1)); 211 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
142} 212}
143 213
144static aio_req SvAIO_REQ (SV *sv) 214static aio_req SvAIO_REQ (SV *sv)
145{ 215{
216 MAGIC *mg;
217
146 if (!sv_derived_from (sv, AIO_CB_KLASS) || !SvROK (sv)) 218 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
147 croak ("object of class " AIO_CB_KLASS " expected"); 219 croak ("object of class " AIO_REQ_KLASS " expected");
148 220
149 MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 221 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
150 222
151 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 }
152} 405}
153 406
154static void req_free (aio_req req) 407static void req_free (aio_req req)
155{ 408{
156 if (req->self) 409 if (req->self)
157 { 410 {
158 sv_unmagic (req->self, PERL_MAGIC_ext); 411 sv_unmagic (req->self, PERL_MAGIC_ext);
159 SvREFCNT_dec (req->self); 412 SvREFCNT_dec (req->self);
160 } 413 }
161 414
162 if (req->data)
163 SvREFCNT_dec (req->data); 415 SvREFCNT_dec (req->data);
164
165 if (req->fh)
166 SvREFCNT_dec (req->fh); 416 SvREFCNT_dec (req->fh);
167
168 if (req->fh2)
169 SvREFCNT_dec (req->fh2); 417 SvREFCNT_dec (req->fh2);
170
171 if (req->statdata)
172 Safefree (req->statdata);
173
174 if (req->callback)
175 SvREFCNT_dec (req->callback); 418 SvREFCNT_dec (req->callback);
419 Safefree (req->statdata);
176 420
177 if (req->type == REQ_READDIR && req->result >= 0) 421 if (req->type == REQ_READDIR && req->result >= 0)
178 free (req->data2ptr); 422 free (req->data2ptr);
179 423
180 Safefree (req); 424 Safefree (req);
181} 425}
182 426
183static void 427static void req_cancel (aio_req req)
184poll_wait ()
185{ 428{
186 if (nreqs && !ress) 429 req->flags |= FLAG_CANCELLED;
187 {
188 fd_set rfd;
189 FD_ZERO(&rfd);
190 FD_SET(respipe [0], &rfd);
191 430
192 select (respipe [0] + 1, &rfd, 0, 0, 0); 431 if (req->type == REQ_GROUP)
193 } 432 {
194} 433 aio_req sub;
195 434
196static int 435 for (sub = req->grp_first; sub; sub = sub->grp_next)
197poll_cb () 436 req_cancel (sub);
437 }
438}
439
440static int poll_cb ()
198{ 441{
199 dSP; 442 dSP;
200 int count = 0; 443 int count = 0;
201 int do_croak = 0; 444 int do_croak = 0;
202 aio_req req; 445 aio_req req;
203 446
204 for (;;) 447 for (;;)
205 { 448 {
206 pthread_mutex_lock (&reslock); 449 pthread_mutex_lock (&reslock);
207 req = ress; 450 req = reqq_shift (&res_queue);
208 451
209 if (req) 452 if (req)
210 { 453 {
211 ress = req->next;
212
213 if (!ress) 454 if (!res_queue.size)
214 { 455 {
215 /* read any signals sent by the worker threads */ 456 /* read any signals sent by the worker threads */
216 char buf [32]; 457 char buf [32];
217 while (read (respipe [0], buf, 32) == 32) 458 while (read (respipe [0], buf, 32) == 32)
218 ; 459 ;
219
220 rese = 0;
221 } 460 }
222 } 461 }
223 462
224 pthread_mutex_unlock (&reslock); 463 pthread_mutex_unlock (&reslock);
225 464
226 if (!req) 465 if (!req)
227 break; 466 break;
228 467
229 nreqs--; 468 --nreqs;
230 469
231 if (req->type == REQ_QUIT) 470 if (req->type == REQ_QUIT)
232 started--; 471 started--;
472 else if (req->type == REQ_GROUP && req->length)
473 {
474 req->fd = 1; /* mark request as delayed */
475 continue;
476 }
233 else 477 else
234 { 478 {
235 int errorno = errno;
236 errno = req->errorno;
237
238 if (req->type == REQ_READ) 479 if (req->type == REQ_READ)
239 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));
240 481
241 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) 482 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
242 SvREADONLY_off (req->data); 483 SvREADONLY_off (req->data);
246 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 487 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
247 PL_laststatval = req->result; 488 PL_laststatval = req->result;
248 PL_statcache = *(req->statdata); 489 PL_statcache = *(req->statdata);
249 } 490 }
250 491
251 ENTER; 492 req_invoke (req);
252 PUSHMARK (SP);
253 493
254 if (req->type == REQ_READDIR)
255 {
256 SV *rv = &PL_sv_undef;
257
258 if (req->result >= 0)
259 {
260 char *buf = req->data2ptr;
261 AV *av = newAV ();
262
263 while (req->result)
264 {
265 SV *sv = newSVpv (buf, 0);
266
267 av_push (av, sv);
268 buf += SvCUR (sv) + 1;
269 req->result--;
270 }
271
272 rv = sv_2mortal (newRV_noinc ((SV *)av));
273 }
274
275 XPUSHs (rv);
276 }
277 else
278 {
279 XPUSHs (sv_2mortal (newSViv (req->result)));
280
281 if (req->type == REQ_OPEN)
282 {
283 /* convert fd to fh */
284 SV *fh;
285
286 PUTBACK;
287 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
288 SPAGAIN;
289
290 fh = SvREFCNT_inc (POPs);
291
292 PUSHMARK (SP);
293 XPUSHs (sv_2mortal (fh));
294 }
295 }
296
297 if (SvOK (req->callback) && !req->cancelled)
298 {
299 PUTBACK;
300 call_sv (req->callback, G_VOID | G_EVAL);
301 SPAGAIN;
302
303 if (SvTRUE (ERRSV))
304 {
305 req_free (req);
306 croak (0);
307 }
308 }
309
310 LEAVE;
311
312 errno = errorno;
313 count++; 494 count++;
314 } 495 }
315 496
316 req_free (req); 497 req_free (req);
317 } 498 }
319 return count; 500 return count;
320} 501}
321 502
322static void *aio_proc(void *arg); 503static void *aio_proc(void *arg);
323 504
324static void
325start_thread (void) 505static void start_thread (void)
326{ 506{
327 sigset_t fullsigset, oldsigset; 507 sigset_t fullsigset, oldsigset;
328 pthread_t tid; 508 pthread_t tid;
329 pthread_attr_t attr; 509 pthread_attr_t attr;
330 510
339 started++; 519 started++;
340 520
341 sigprocmask (SIG_SETMASK, &oldsigset, 0); 521 sigprocmask (SIG_SETMASK, &oldsigset, 0);
342} 522}
343 523
344static void
345req_send (aio_req req) 524static void req_send (aio_req req)
346{ 525{
347 while (started < wanted && nreqs >= started) 526 while (started < wanted && nreqs >= started)
348 start_thread (); 527 start_thread ();
349 528
350 nreqs++; 529 ++nreqs;
351 530
352 pthread_mutex_lock (&reqlock); 531 pthread_mutex_lock (&reqlock);
353 532 reqq_push (&req_queue, req);
354 req->next = 0;
355
356 if (reqe)
357 {
358 reqe->next = req;
359 reqe = req;
360 }
361 else
362 reqe = reqs = req;
363
364 pthread_cond_signal (&reqwait); 533 pthread_cond_signal (&reqwait);
365 pthread_mutex_unlock (&reqlock); 534 pthread_mutex_unlock (&reqlock);
366 535
367 if (nreqs > max_outstanding) 536 if (nreqs > max_outstanding)
368 for (;;) 537 for (;;)
374 543
375 poll_wait (); 544 poll_wait ();
376 } 545 }
377} 546}
378 547
379static void 548static void end_thread (void)
380end_thread (void)
381{ 549{
382 aio_req req; 550 aio_req req;
551
383 Newz (0, req, 1, aio_cb); 552 Newz (0, req, 1, aio_cb);
553
384 req->type = REQ_QUIT; 554 req->type = REQ_QUIT;
555 req->pri = PRI_MAX + PRI_BIAS;
385 556
386 req_send (req); 557 req_send (req);
387} 558}
388 559
389static void min_parallel (int nthreads) 560static void min_parallel (int nthreads)
436 * 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,
437 * but that's your problem, not mine. 608 * but that's your problem, not mine.
438 */ 609 */
439static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; 610static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
440 611
441static ssize_t 612static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
442pread (int fd, void *buf, size_t count, off_t offset)
443{ 613{
444 ssize_t res; 614 ssize_t res;
445 off_t ooffset; 615 off_t ooffset;
446 616
447 pthread_mutex_lock (&preadwritelock); 617 pthread_mutex_lock (&preadwritelock);
452 pthread_mutex_unlock (&preadwritelock); 622 pthread_mutex_unlock (&preadwritelock);
453 623
454 return res; 624 return res;
455} 625}
456 626
457static ssize_t
458pwrite (int fd, void *buf, size_t count, off_t offset) 627static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
459{ 628{
460 ssize_t res; 629 ssize_t res;
461 off_t ooffset; 630 off_t ooffset;
462 631
463 pthread_mutex_lock (&preadwritelock); 632 pthread_mutex_lock (&preadwritelock);
476#endif 645#endif
477 646
478#if !HAVE_READAHEAD 647#if !HAVE_READAHEAD
479# define readahead aio_readahead 648# define readahead aio_readahead
480 649
481static ssize_t
482readahead (int fd, off_t offset, size_t count) 650static ssize_t readahead (int fd, off_t offset, size_t count)
483{ 651{
484 char readahead_buf[4096]; 652 dBUF;
485 653
486 while (count > 0) 654 while (count > 0)
487 { 655 {
488 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); 656 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
489 657
490 pread (fd, readahead_buf, len, offset); 658 pread (fd, aio_buf, len, offset);
491 offset += len; 659 offset += len;
492 count -= len; 660 count -= len;
493 } 661 }
494 662
663 fBUF;
664
495 errno = 0; 665 errno = 0;
496} 666}
497#endif 667#endif
498 668
499#if !HAVE_READDIR_R 669#if !HAVE_READDIR_R
500# define readdir_r aio_readdir_r 670# define readdir_r aio_readdir_r
501 671
502static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; 672static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
503 673
504static int
505readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) 674static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
506{ 675{
507 struct dirent *e; 676 struct dirent *e;
508 int errorno; 677 int errorno;
509 678
510 pthread_mutex_lock (&readdirlock); 679 pthread_mutex_lock (&readdirlock);
526 return e ? 0 : -1; 695 return e ? 0 : -1;
527} 696}
528#endif 697#endif
529 698
530/* sendfile always needs emulation */ 699/* sendfile always needs emulation */
531static ssize_t
532sendfile_ (int ofd, int ifd, off_t offset, size_t count) 700static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count)
533{ 701{
534 ssize_t res; 702 ssize_t res;
535 703
536 if (!count) 704 if (!count)
537 return 0; 705 return 0;
586#endif 754#endif
587 ) 755 )
588 ) 756 )
589 { 757 {
590 /* emulate sendfile. this is a major pain in the ass */ 758 /* emulate sendfile. this is a major pain in the ass */
591 char buf[4096]; 759 dBUF;
760
592 res = 0; 761 res = 0;
593 762
594 while (count) 763 while (count)
595 { 764 {
596 ssize_t cnt; 765 ssize_t cnt;
597 766
598 cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); 767 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
599 768
600 if (cnt <= 0) 769 if (cnt <= 0)
601 { 770 {
602 if (cnt && !res) res = -1; 771 if (cnt && !res) res = -1;
603 break; 772 break;
604 } 773 }
605 774
606 cnt = write (ofd, buf, cnt); 775 cnt = write (ofd, aio_buf, cnt);
607 776
608 if (cnt <= 0) 777 if (cnt <= 0)
609 { 778 {
610 if (cnt && !res) res = -1; 779 if (cnt && !res) res = -1;
611 break; 780 break;
613 782
614 offset += cnt; 783 offset += cnt;
615 res += cnt; 784 res += cnt;
616 count -= cnt; 785 count -= cnt;
617 } 786 }
787
788 fBUF;
618 } 789 }
619 790
620 return res; 791 return res;
621} 792}
622 793
623/* read a full directory */ 794/* read a full directory */
624static int
625scandir_ (const char *path, void **namesp) 795static int scandir_ (const char *path, void **namesp)
626{ 796{
627 DIR *dirp = opendir (path); 797 DIR *dirp;
628 union 798 union
629 { 799 {
630 struct dirent d; 800 struct dirent d;
631 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; 801 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
632 } u; 802 } *u;
633 struct dirent *entp; 803 struct dirent *entp;
634 char *name, *names; 804 char *name, *names;
635 int memlen = 4096; 805 int memlen = 4096;
636 int memofs = 0; 806 int memofs = 0;
637 int res = 0; 807 int res = 0;
638 int errorno; 808 int errorno;
639 809
810 dirp = opendir (path);
640 if (!dirp) 811 if (!dirp)
641 return -1; 812 return -1;
642 813
814 u = malloc (sizeof (*u));
643 names = malloc (memlen); 815 names = malloc (memlen);
644 816
817 if (u && names)
645 for (;;) 818 for (;;)
646 { 819 {
820 errno = 0;
647 errno = 0, readdir_r (dirp, &u.d, &entp); 821 readdir_r (dirp, &u->d, &entp);
648 822
649 if (!entp) 823 if (!entp)
650 break; 824 break;
651 825
652 name = entp->d_name; 826 name = entp->d_name;
653 827
654 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 828 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
655 { 829 {
656 int len = strlen (name) + 1; 830 int len = strlen (name) + 1;
657 831
658 res++; 832 res++;
659 833
660 while (memofs + len > memlen) 834 while (memofs + len > memlen)
661 { 835 {
662 memlen *= 2; 836 memlen *= 2;
663 names = realloc (names, memlen); 837 names = realloc (names, memlen);
664 if (!names) 838 if (!names)
665 break; 839 break;
666 } 840 }
667 841
668 memcpy (names + memofs, name, len); 842 memcpy (names + memofs, name, len);
669 memofs += len; 843 memofs += len;
670 } 844 }
671 } 845 }
672 846
673 errorno = errno; 847 errorno = errno;
848 free (u);
674 closedir (dirp); 849 closedir (dirp);
675 850
676 if (errorno) 851 if (errorno)
677 { 852 {
678 free (names); 853 free (names);
684 return res; 859 return res;
685} 860}
686 861
687/*****************************************************************************/ 862/*****************************************************************************/
688 863
689static void *
690aio_proc (void *thr_arg) 864static void *aio_proc (void *thr_arg)
691{ 865{
692 aio_req req; 866 aio_req req;
693 int type; 867 int type;
694 868
695 do 869 do
696 { 870 {
697 pthread_mutex_lock (&reqlock); 871 pthread_mutex_lock (&reqlock);
698 872
699 for (;;) 873 for (;;)
700 { 874 {
701 req = reqs; 875 req = reqq_shift (&req_queue);
702
703 if (reqs)
704 {
705 reqs = reqs->next;
706 if (!reqs) reqe = 0;
707 }
708 876
709 if (req) 877 if (req)
710 break; 878 break;
711 879
712 pthread_cond_wait (&reqwait, &reqlock); 880 pthread_cond_wait (&reqwait, &reqlock);
713 } 881 }
714 882
715 pthread_mutex_unlock (&reqlock); 883 pthread_mutex_unlock (&reqlock);
716 884
717 errno = 0; /* strictly unnecessary */ 885 errno = 0; /* strictly unnecessary */
886 type = req->type; /* remember type for QUIT check */
718 887
719 if (!req->cancelled) 888 if (!(req->flags & FLAG_CANCELLED))
720 switch (req->type) 889 switch (type)
721 { 890 {
722 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;
723 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;
724 893
725 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;
739 908
740 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; 909 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
741 case REQ_FSYNC: req->result = fsync (req->fd); break; 910 case REQ_FSYNC: req->result = fsync (req->fd); break;
742 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; 911 case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break;
743 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:
744 case REQ_QUIT: 925 case REQ_QUIT:
745 break; 926 break;
746 927
747 default: 928 default:
748 req->result = ENOSYS; 929 req->result = ENOSYS;
751 932
752 req->errorno = errno; 933 req->errorno = errno;
753 934
754 pthread_mutex_lock (&reslock); 935 pthread_mutex_lock (&reslock);
755 936
756 req->next = 0; 937 if (!reqq_push (&res_queue, req))
757
758 if (rese)
759 {
760 rese->next = req;
761 rese = req;
762 }
763 else
764 {
765 rese = ress = req;
766
767 /* write a dummy byte to the pipe so fh becomes ready */ 938 /* write a dummy byte to the pipe so fh becomes ready */
768 write (respipe [1], &respipe, 1); 939 write (respipe [1], &respipe, 1);
769 }
770 940
771 pthread_mutex_unlock (&reslock); 941 pthread_mutex_unlock (&reslock);
772 } 942 }
773 while (type != REQ_QUIT); 943 while (type != REQ_QUIT);
774 944
805{ 975{
806 aio_req prv; 976 aio_req prv;
807 977
808 started = 0; 978 started = 0;
809 979
810 while (reqs) 980 while (prv = reqq_shift (&req_queue))
811 {
812 prv = reqs;
813 reqs = prv->next;
814 req_free (prv); 981 req_free (prv);
815 }
816 982
817 reqs = reqe = 0; 983 while (prv = reqq_shift (&res_queue))
818
819 while (ress)
820 {
821 prv = ress;
822 ress = prv->next;
823 req_free (prv); 984 req_free (prv);
824 } 985
825
826 ress = rese = 0;
827
828 close (respipe [0]); 986 close (respipe [0]);
829 close (respipe [1]); 987 close (respipe [1]);
830 create_pipe (); 988 create_pipe ();
831 989
832 atfork_parent (); 990 atfork_parent ();
833} 991}
834 992
835#define dREQ \ 993#define dREQ \
836 aio_req req; \ 994 aio_req req; \
995 int req_pri = next_pri; \
996 next_pri = DEFAULT_PRI + PRI_BIAS; \
837 \ 997 \
838 if (SvOK (callback) && !SvROK (callback)) \ 998 if (SvOK (callback) && !SvROK (callback)) \
839 croak ("callback must be undef or of reference type"); \ 999 croak ("callback must be undef or of reference type"); \
840 \ 1000 \
841 Newz (0, req, 1, aio_cb); \ 1001 Newz (0, req, 1, aio_cb); \
842 if (!req) \ 1002 if (!req) \
843 croak ("out of memory during aio_req allocation"); \ 1003 croak ("out of memory during aio_req allocation"); \
844 \ 1004 \
845 req->callback = newSVsv (callback) 1005 req->callback = newSVsv (callback); \
1006 req->pri = req_pri
846 1007
847#define REQ_SEND \ 1008#define REQ_SEND \
848 req_send (req); \ 1009 req_send (req); \
849 \ 1010 \
850 if (GIMME_V != G_VOID) \ 1011 if (GIMME_V != G_VOID) \
851 XPUSHs (req_sv (req)); 1012 XPUSHs (req_sv (req, AIO_REQ_KLASS));
852 1013
853MODULE = IO::AIO PACKAGE = IO::AIO 1014MODULE = IO::AIO PACKAGE = IO::AIO
854 1015
855PROTOTYPES: ENABLE 1016PROTOTYPES: ENABLE
856 1017
1103 req->dataptr = SvPVbyte_nolen (req->data); 1264 req->dataptr = SvPVbyte_nolen (req->data);
1104 1265
1105 REQ_SEND; 1266 REQ_SEND;
1106} 1267}
1107 1268
1108#if 0
1109
1110# undocumented, because it does not cancel active requests
1111void 1269void
1112cancel_most_requests () 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
1285aio_group (callback=&PL_sv_undef)
1286 SV * callback
1113 PROTOTYPE: 1287 PROTOTYPE: ;$
1114 CODE: 1288 PPCODE:
1115{ 1289{
1116 aio_req *req; 1290 dREQ;
1117 1291
1118 pthread_mutex_lock (&reqlock); 1292 req->type = REQ_GROUP;
1119 for (req = reqs; req; req = req->next) 1293 req_send (req);
1120 req->flags |= 1;
1121 pthread_mutex_unlock (&reqlock);
1122 1294
1123 pthread_mutex_lock (&reslock); 1295 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1124 for (req = ress; req; req = req->next)
1125 req->flags |= 1;
1126 pthread_mutex_unlock (&reslock);
1127} 1296}
1128 1297
1129#endif 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;
1130 1324
1131void 1325void
1132flush () 1326flush ()
1133 PROTOTYPE: 1327 PROTOTYPE:
1134 CODE: 1328 CODE:
1177 CODE: 1371 CODE:
1178 RETVAL = nreqs; 1372 RETVAL = nreqs;
1179 OUTPUT: 1373 OUTPUT:
1180 RETVAL 1374 RETVAL
1181 1375
1376PROTOTYPES: DISABLE
1377
1182MODULE = IO::AIO PACKAGE = IO::AIO::CB 1378MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1183 1379
1184void 1380void
1185cancel (aio_req_ornot req) 1381cancel (aio_req_ornot req)
1186 PROTOTYPE:
1187 CODE: 1382 CODE:
1188 req->cancelled = 1; 1383 req_cancel (req);
1189 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