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