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.79 by root, Thu Oct 26 16:28:33 2006 UTC vs.
Revision 1.117 by root, Sat May 10 20:02:09 2008 UTC

1/* solaris */ 1#include "libeio/xthread.h"
2#define _POSIX_PTHREAD_SEMANTICS 1
3
4#if __linux && !defined(_GNU_SOURCE)
5# define _GNU_SOURCE
6#endif
7
8/* just in case */
9#define _REENTRANT 1
10 2
11#include <errno.h> 3#include <errno.h>
12 4
13#include "EXTERN.h" 5#include "EXTERN.h"
14#include "perl.h" 6#include "perl.h"
15#include "XSUB.h" 7#include "XSUB.h"
16 8
17#include "autoconf/config.h"
18
19#include <pthread.h>
20
21#include <stddef.h> 9#include <stddef.h>
10#include <stdlib.h>
22#include <errno.h> 11#include <errno.h>
23#include <sys/time.h>
24#include <sys/select.h>
25#include <sys/types.h> 12#include <sys/types.h>
26#include <sys/stat.h> 13#include <sys/stat.h>
27#include <limits.h> 14#include <limits.h>
28#include <unistd.h>
29#include <fcntl.h> 15#include <fcntl.h>
30#include <signal.h>
31#include <sched.h> 16#include <sched.h>
32 17
33#if HAVE_SENDFILE 18#ifdef _WIN32
34# if __linux 19
35# include <sys/sendfile.h> 20# define EIO_STRUCT_DIRENT Direntry_t
36# elif __freebsd 21# undef malloc
37# include <sys/socket.h> 22# undef free
38# include <sys/uio.h> 23
39# elif __hpux 24// perl overrides all those nice win32 functions
40# include <sys/socket.h> 25# undef open
41# elif __solaris /* not yet */ 26# undef read
42# include <sys/sendfile.h> 27# undef write
28# undef send
29# undef recv
30# undef stat
31# undef fstat
32# define lstat stat
33# undef truncate
34# undef ftruncate
35# undef open
36# undef close
37# undef unlink
38# undef rmdir
39# undef rename
40# undef lseek
41
42# define chown(a,b,c) (errno = ENOSYS, -1)
43# define fchown(a,b,c) (errno = ENOSYS, -1)
44# define fchmod(a,b) (errno = ENOSYS, -1)
45# define symlink(a,b) (errno = ENOSYS, -1)
46# define readlink(a,b,c) (errno = ENOSYS, -1)
47# define mknod(a,b,c) (errno = ENOSYS, -1)
48# define truncate(a,b) (errno = ENOSYS, -1)
49# define ftruncate(fd,o) chsize ((fd), (o))
50# define fsync(fd) _commit (fd)
51# define opendir(fd) (errno = ENOSYS, 0)
52# define readdir(fd) (errno = ENOSYS, -1)
53# define closedir(fd) (errno = ENOSYS, -1)
54# define mkdir(a,b) mkdir (a)
55
43# else 56#else
44# error sendfile support requested but not available 57
45# endif 58# include <sys/time.h>
59# include <sys/select.h>
60# include <unistd.h>
61# include <utime.h>
62# include <signal.h>
63# define EIO_STRUCT_DIRENT struct dirent
64
46#endif 65#endif
47 66
48/* used for struct dirent, AIX doesn't provide it */ 67#define EIO_STRUCT_STAT Stat_t
49#ifndef NAME_MAX 68
50# define NAME_MAX 4096 69/* use NV for 32 bit perls as it allows larger offsets */
70#if IVSIZE >= 8
71# define SvVAL64 SvIV
72#else
73# define SvVAL64 SvNV
51#endif 74#endif
52 75
53#ifndef PTHREAD_STACK_MIN 76static HV *stash;
54/* care for broken platforms, e.g. windows */ 77typedef SV SV8; /* byte-sv, used for argument-checking */
55# define PTHREAD_STACK_MIN 16384
56#endif
57
58#if __ia64
59# define STACKSIZE 65536
60#elif __i386 || __x86_64 /* 16k is unreasonably high :( */
61# define STACKSIZE PTHREAD_STACK_MIN
62#else
63# define STACKSIZE 16384
64#endif
65
66/* wether word reads are potentially non-atomic.
67 * this is conservatice, likely most arches this runs
68 * on have atomic word read/writes.
69 */
70#ifndef WORDREAD_UNSAFE
71# if __i386 || __x86_64
72# define WORDREAD_UNSAFE 0
73# else
74# define WORDREAD_UNSAFE 1
75# endif
76#endif
77
78/* buffer size for various temporary buffers */
79#define AIO_BUFSIZE 65536
80
81#define dBUF \
82 char *aio_buf; \
83 LOCK (wrklock); \
84 self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \
85 UNLOCK (wrklock); \
86 if (!aio_buf) \
87 return -1;
88
89enum {
90 REQ_QUIT,
91 REQ_OPEN, REQ_CLOSE,
92 REQ_READ, REQ_WRITE, REQ_READAHEAD,
93 REQ_SENDFILE,
94 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
95 REQ_FSYNC, REQ_FDATASYNC,
96 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
97 REQ_READDIR,
98 REQ_LINK, REQ_SYMLINK,
99 REQ_GROUP, REQ_NOP,
100 REQ_BUSY,
101};
102 78
103#define AIO_REQ_KLASS "IO::AIO::REQ" 79#define AIO_REQ_KLASS "IO::AIO::REQ"
104#define AIO_GRP_KLASS "IO::AIO::GRP" 80#define AIO_GRP_KLASS "IO::AIO::GRP"
105 81
106typedef struct aio_cb 82#define EIO_COMMON \
107{
108 struct aio_cb *volatile next;
109
110 SV *data, *callback; 83 SV *callback; \
111 SV *fh, *fh2; 84 SV *sv1, *sv2; \
112 void *dataptr, *data2ptr;
113 Stat_t *statdata;
114 off_t offset;
115 size_t length;
116 ssize_t result;
117
118 STRLEN dataoffset; 85 STRLEN stroffset; \
119 int type; 86 SV *self
120 int fd, fd2;
121 int errorno;
122 mode_t mode; /* open */
123 87
124 unsigned char flags; 88#include "libeio/eio.h"
125 unsigned char pri;
126 89
127 SV *self; /* the perl counterpart of this request, if any */ 90static int req_invoke (eio_req *req);
128 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; 91#define EIO_FINISH(req) req_invoke (req)
129} aio_cb; 92static void aio_grp_feed (eio_req *grp);
93#define EIO_FEED(req) aio_grp_feed (req)
94static void req_destroy (eio_req *grp);
95#define EIO_DESTROY(req) req_destroy (req)
130 96
131enum { 97enum {
132 FLAG_CANCELLED = 0x01, 98 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
133}; 99};
134 100
101#include "libeio/eio.c"
102
135typedef aio_cb *aio_req; 103typedef eio_req *aio_req;
136typedef aio_cb *aio_req_ornot; 104typedef eio_req *aio_req_ornot;
137 105
138enum { 106static SV *on_next_submit;
139 PRI_MIN = -4,
140 PRI_MAX = 4,
141
142 DEFAULT_PRI = 0,
143 PRI_BIAS = -PRI_MIN,
144 NUM_PRI = PRI_MAX + PRI_BIAS + 1,
145};
146
147static int next_pri = DEFAULT_PRI + PRI_BIAS; 107static int next_pri = EIO_DEFAULT_PRI + EIO_PRI_BIAS;
108static int max_outstanding;
148 109
149static unsigned int started, wanted; 110static int respipe_osf [2], respipe [2] = { -1, -1 };
150static volatile unsigned int nreqs, nready, npending;
151static volatile unsigned int max_outstanding = 0xffffffff;
152static int respipe [2];
153 111
154#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
155# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
156#else
157# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
158#endif
159
160#define LOCK(mutex) pthread_mutex_lock (&(mutex))
161#define UNLOCK(mutex) pthread_mutex_unlock (&(mutex))
162
163/* worker threads management */
164static pthread_mutex_t wrklock = AIO_MUTEX_INIT;
165
166typedef struct worker {
167 /* locked by wrklock */
168 struct worker *prev, *next;
169
170 pthread_t tid;
171
172 /* locked by reslock, reqlock or wrklock */
173 aio_req req; /* currently processed request */
174 void *dbuf;
175 DIR *dirp;
176} worker;
177
178static worker wrk_first = { &wrk_first, &wrk_first, 0 };
179
180static void worker_clear (worker *wrk)
181{
182 if (wrk->dirp)
183 {
184 closedir (wrk->dirp);
185 wrk->dirp = 0;
186 }
187
188 if (wrk->dbuf)
189 {
190 free (wrk->dbuf);
191 wrk->dbuf = 0;
192 }
193}
194
195static void worker_free (worker *wrk)
196{
197 wrk->next->prev = wrk->prev;
198 wrk->prev->next = wrk->next;
199
200 free (wrk);
201}
202
203static pthread_mutex_t reslock = AIO_MUTEX_INIT;
204static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
205static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
206
207/*
208 * a somewhat faster data structure might be nice, but
209 * with 8 priorities this actually needs <20 insns
210 * per shift, the most expensive operation.
211 */
212typedef struct {
213 aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */
214 int size;
215} reqq;
216
217static reqq req_queue;
218static reqq res_queue;
219
220int reqq_push (reqq *q, aio_req req)
221{
222 int pri = req->pri;
223 req->next = 0;
224
225 if (q->qe[pri])
226 {
227 q->qe[pri]->next = req;
228 q->qe[pri] = req;
229 }
230 else
231 q->qe[pri] = q->qs[pri] = req;
232
233 return q->size++;
234}
235
236aio_req reqq_shift (reqq *q)
237{
238 int pri;
239
240 if (!q->size)
241 return 0;
242
243 --q->size;
244
245 for (pri = NUM_PRI; pri--; )
246 {
247 aio_req req = q->qs[pri];
248
249 if (req)
250 {
251 if (!(q->qs[pri] = req->next))
252 q->qe[pri] = 0;
253
254 return req;
255 }
256 }
257
258 abort ();
259}
260
261static int poll_cb (int max);
262static void req_invoke (aio_req req);
263static void req_free (aio_req req); 112static void req_destroy (aio_req req);
264static void req_cancel (aio_req req); 113static void req_cancel (aio_req req);
114
115static void want_poll (void)
116{
117 /* write a dummy byte to the pipe so fh becomes ready */
118 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
119}
120
121static void done_poll (void)
122{
123 /* read any signals sent by the worker threads */
124 char buf [4];
125 while (respipe_read (respipe [0], buf, 4) == 4)
126 ;
127}
265 128
266/* must be called at most once */ 129/* must be called at most once */
267static SV *req_sv (aio_req req, const char *klass) 130static SV *req_sv (aio_req req, const char *klass)
268{ 131{
269 if (!req->self) 132 if (!req->self)
287 return mg ? (aio_req)mg->mg_ptr : 0; 150 return mg ? (aio_req)mg->mg_ptr : 0;
288} 151}
289 152
290static void aio_grp_feed (aio_req grp) 153static void aio_grp_feed (aio_req grp)
291{ 154{
292 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) 155 if (grp->sv2 && SvOK (grp->sv2))
293 { 156 {
294 int old_len = grp->length;
295
296 if (grp->fh2 && SvOK (grp->fh2))
297 {
298 dSP; 157 dSP;
299 158
300 ENTER; 159 ENTER;
301 SAVETMPS; 160 SAVETMPS;
302 PUSHMARK (SP); 161 PUSHMARK (SP);
303 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 162 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
304 PUTBACK; 163 PUTBACK;
305 call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR); 164 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
306 SPAGAIN; 165 SPAGAIN;
307 FREETMPS; 166 FREETMPS;
308 LEAVE; 167 LEAVE;
309 }
310
311 /* stop if no progress has been made */
312 if (old_len == grp->length)
313 {
314 SvREFCNT_dec (grp->fh2);
315 grp->fh2 = 0;
316 break;
317 }
318 } 168 }
319} 169}
320 170
321static void aio_grp_dec (aio_req grp) 171static void req_submit (eio_req *req)
322{ 172{
323 --grp->length; 173 eio_submit (req);
324 174
325 /* call feeder, if applicable */ 175 if (on_next_submit)
326 aio_grp_feed (grp);
327
328 /* finish, if done */
329 if (!grp->length && grp->fd)
330 { 176 {
331 req_invoke (grp); 177 dSP;
332 req_free (grp); 178 SV *cb = sv_2mortal (on_next_submit);
179
180 on_next_submit = 0;
181
182 PUSHMARK (SP);
183 PUTBACK;
184 call_sv (cb, G_DISCARD | G_EVAL);
333 } 185 }
334} 186}
335 187
336static void poll_wait ()
337{
338 fd_set rfd;
339
340 while (nreqs)
341 {
342 int size;
343 if (WORDREAD_UNSAFE) LOCK (reslock);
344 size = res_queue.size;
345 if (WORDREAD_UNSAFE) UNLOCK (reslock);
346
347 if (size)
348 return;
349
350 FD_ZERO(&rfd);
351 FD_SET(respipe [0], &rfd);
352
353 select (respipe [0] + 1, &rfd, 0, 0, 0);
354 }
355}
356
357static void req_invoke (aio_req req) 188static int req_invoke (eio_req *req)
358{ 189{
359 dSP; 190 dSP;
360 191
192 if (req->flags & FLAG_SV2_RO_OFF)
193 SvREADONLY_off (req->sv2);
194
361 if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) 195 if (!EIO_CANCELLED (req) && SvOK (req->callback))
362 { 196 {
363 ENTER; 197 ENTER;
364 SAVETMPS; 198 SAVETMPS;
365 PUSHMARK (SP); 199 PUSHMARK (SP);
366 EXTEND (SP, 1); 200 EXTEND (SP, 1);
367 201
368 switch (req->type) 202 switch (req->type)
369 { 203 {
370 case REQ_READDIR: 204 case EIO_READDIR:
371 { 205 {
372 SV *rv = &PL_sv_undef; 206 SV *rv = &PL_sv_undef;
373 207
374 if (req->result >= 0) 208 if (req->result >= 0)
375 { 209 {
376 int i; 210 int i;
377 char *buf = req->data2ptr; 211 char *buf = req->ptr2;
378 AV *av = newAV (); 212 AV *av = newAV ();
379 213
380 av_extend (av, req->result - 1); 214 av_extend (av, req->result - 1);
381 215
382 for (i = 0; i < req->result; ++i) 216 for (i = 0; i < req->result; ++i)
392 226
393 PUSHs (rv); 227 PUSHs (rv);
394 } 228 }
395 break; 229 break;
396 230
397 case REQ_OPEN: 231 case EIO_OPEN:
398 { 232 {
399 /* convert fd to fh */ 233 /* convert fd to fh */
400 SV *fh; 234 SV *fh = &PL_sv_undef;
401 235
402 PUSHs (sv_2mortal (newSViv (req->result))); 236 if (req->result >= 0)
403 PUTBACK; 237 {
404 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); 238 GV *gv = (GV *)sv_newmortal ();
405 SPAGAIN; 239 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
240 char sym [64];
241 int symlen;
242
243 symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result);
244 gv_init (gv, stash, sym, symlen, 0);
406 245
407 fh = SvREFCNT_inc (POPs); 246 symlen = snprintf (
247 sym,
248 sizeof (sym),
249 "%s&=%d",
250 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<",
251 req->result
252 );
408 253
254 if (do_open (gv, sym, symlen, 0, 0, 0, 0))
255 fh = (SV *)gv;
256 }
257
409 PUSHMARK (SP); 258 PUSHs (fh);
410 XPUSHs (sv_2mortal (fh));
411 } 259 }
412 break; 260 break;
413 261
414 case REQ_GROUP: 262 case EIO_GROUP:
415 req->fd = 2; /* mark group as finished */ 263 req->int1 = 2; /* mark group as finished */
416 264
417 if (req->data) 265 if (req->sv1)
418 { 266 {
419 int i; 267 int i;
420 AV *av = (AV *)req->data; 268 AV *av = (AV *)req->sv1;
421 269
422 EXTEND (SP, AvFILL (av) + 1); 270 EXTEND (SP, AvFILL (av) + 1);
423 for (i = 0; i <= AvFILL (av); ++i) 271 for (i = 0; i <= AvFILL (av); ++i)
424 PUSHs (*av_fetch (av, i, 0)); 272 PUSHs (*av_fetch (av, i, 0));
425 } 273 }
426 break; 274 break;
427 275
428 case REQ_NOP: 276 case EIO_NOP:
429 case REQ_BUSY: 277 case EIO_BUSY:
430 break; 278 break;
279
280 case EIO_READLINK:
281 if (req->result > 0)
282 {
283 SvCUR_set (req->sv2, req->result);
284 *SvEND (req->sv2) = 0;
285 PUSHs (req->sv2);
286 }
287 break;
288
289 case EIO_STAT:
290 case EIO_LSTAT:
291 case EIO_FSTAT:
292 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
293 PL_laststatval = req->result;
294 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
295 PUSHs (sv_2mortal (newSViv (req->result)));
296 break;
297
298 case EIO_READ:
299 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
300 *SvEND (req->sv2) = 0;
301 PUSHs (sv_2mortal (newSViv (req->result)));
302 break;
303
304 case EIO_DUP2:
305 if (req->result > 0)
306 req->result = 0;
307 /* FALLTHROUGH */
431 308
432 default: 309 default:
433 PUSHs (sv_2mortal (newSViv (req->result))); 310 PUSHs (sv_2mortal (newSViv (req->result)));
434 break; 311 break;
435 } 312 }
436 313
437 errno = req->errorno; 314 errno = req->errorno;
438 315
439 PUTBACK; 316 PUTBACK;
440 call_sv (req->callback, G_VOID | G_EVAL); 317 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
441 SPAGAIN; 318 SPAGAIN;
442 319
443 FREETMPS; 320 FREETMPS;
444 LEAVE; 321 LEAVE;
322
323 PUTBACK;
445 } 324 }
446 325
447 if (req->grp) 326 return !!SvTRUE (ERRSV);
448 {
449 aio_req grp = req->grp;
450
451 /* unlink request */
452 if (req->grp_next) req->grp_next->grp_prev = req->grp_prev;
453 if (req->grp_prev) req->grp_prev->grp_next = req->grp_next;
454
455 if (grp->grp_first == req)
456 grp->grp_first = req->grp_next;
457
458 aio_grp_dec (grp);
459 }
460
461 if (SvTRUE (ERRSV))
462 {
463 req_free (req);
464 croak (0);
465 }
466} 327}
467 328
468static void req_free (aio_req req) 329static void req_destroy (aio_req req)
469{ 330{
470 if (req->self) 331 if (req->self)
471 { 332 {
472 sv_unmagic (req->self, PERL_MAGIC_ext); 333 sv_unmagic (req->self, PERL_MAGIC_ext);
473 SvREFCNT_dec (req->self); 334 SvREFCNT_dec (req->self);
474 } 335 }
475 336
476 SvREFCNT_dec (req->data);
477 SvREFCNT_dec (req->fh); 337 SvREFCNT_dec (req->sv1);
478 SvREFCNT_dec (req->fh2); 338 SvREFCNT_dec (req->sv2);
479 SvREFCNT_dec (req->callback); 339 SvREFCNT_dec (req->callback);
480 Safefree (req->statdata);
481
482 if (req->type == REQ_READDIR)
483 free (req->data2ptr);
484 340
485 Safefree (req); 341 Safefree (req);
486} 342}
487 343
488static void req_cancel_subs (aio_req grp) 344static void req_cancel_subs (aio_req grp)
489{ 345{
490 aio_req sub; 346 aio_req sub;
491 347
492 if (grp->type != REQ_GROUP) 348 if (grp->type != EIO_GROUP)
493 return; 349 return;
494 350
495 SvREFCNT_dec (grp->fh2); 351 SvREFCNT_dec (grp->sv2);
496 grp->fh2 = 0; 352 grp->sv2 = 0;
497 353
498 for (sub = grp->grp_first; sub; sub = sub->grp_next) 354 eio_grp_cancel (grp);
499 req_cancel (sub);
500} 355}
501 356
502static void req_cancel (aio_req req) 357#ifdef USE_SOCKETS_AS_HANDLES
503{ 358# define TO_SOCKET(x) (win32_get_osfhandle (x))
504 req->flags |= FLAG_CANCELLED; 359#else
360# define TO_SOCKET(x) (x)
361#endif
505 362
506 req_cancel_subs (req); 363static void
507} 364create_respipe (void)
508
509static int poll_cb (int max)
510{ 365{
511 dSP; 366 int old_readfd = respipe [0];
512 int count = 0;
513 int do_croak = 0;
514 aio_req req;
515 367
516 for (;;) 368 if (respipe [1] >= 0)
369 respipe_close (TO_SOCKET (respipe [1]));
370
371#ifdef _WIN32
372 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
373#else
374 if (pipe (respipe))
375#endif
376 croak ("unable to initialize result pipe");
377
378 if (old_readfd >= 0)
517 { 379 {
518 while (max <= 0 || count < max) 380 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
519 { 381 croak ("unable to initialize result pipe(2)");
520 LOCK (reslock);
521 req = reqq_shift (&res_queue);
522 382
523 if (req) 383 respipe_close (respipe [0]);
524 { 384 respipe [0] = old_readfd;
525 --npending;
526
527 if (!res_queue.size)
528 {
529 /* read any signals sent by the worker threads */
530 char buf [32];
531 while (read (respipe [0], buf, 32) == 32)
532 ;
533 }
534 }
535
536 UNLOCK (reslock);
537
538 if (!req)
539 break;
540
541 --nreqs;
542
543 if (req->type == REQ_QUIT)
544 --started;
545 else if (req->type == REQ_GROUP && req->length)
546 {
547 req->fd = 1; /* mark request as delayed */
548 continue;
549 }
550 else
551 {
552 if (req->type == REQ_READ)
553 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
554
555 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
556 SvREADONLY_off (req->data);
557
558 if (req->statdata)
559 {
560 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
561 PL_laststatval = req->result;
562 PL_statcache = *(req->statdata);
563 }
564
565 req_invoke (req);
566
567 count++;
568 }
569
570 req_free (req);
571 }
572
573 if (nreqs <= max_outstanding)
574 break;
575
576 poll_wait ();
577
578 max = 0;
579 } 385 }
580 386
581 return count; 387#ifdef _WIN32
582} 388 int arg = 1;
389 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
390 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
391#else
392 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
393 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
394#endif
395 croak ("unable to initialize result pipe(3)");
583 396
584static void *aio_proc(void *arg); 397 respipe_osf [0] = TO_SOCKET (respipe [0]);
398 respipe_osf [1] = TO_SOCKET (respipe [1]);
399}
585 400
586static void start_thread (void) 401static void poll_wait (void)
587{ 402{
588 sigset_t fullsigset, oldsigset; 403 fd_set rfd;
589 pthread_attr_t attr;
590 404
591 worker *wrk = calloc (1, sizeof (worker)); 405 while (eio_nreqs ())
592
593 if (!wrk)
594 croak ("unable to allocate worker thread data");
595
596 pthread_attr_init (&attr);
597 pthread_attr_setstacksize (&attr, STACKSIZE);
598 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
599
600 sigfillset (&fullsigset);
601
602 LOCK (wrklock);
603 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
604
605 if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0)
606 { 406 {
607 wrk->prev = &wrk_first; 407 int size;
608 wrk->next = wrk_first.next; 408
609 wrk_first.next->prev = wrk; 409 X_LOCK (reslock);
610 wrk_first.next = wrk; 410 size = res_queue.size;
611 ++started; 411 X_UNLOCK (reslock);
412
413 if (size)
414 return;
415
416 maybe_start_thread ();
417
418 FD_ZERO (&rfd);
419 FD_SET (respipe [0], &rfd);
420
421 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
612 } 422 }
613 else
614 free (wrk);
615
616 sigprocmask (SIG_SETMASK, &oldsigset, 0);
617 UNLOCK (wrklock);
618} 423}
619 424
620static void req_send (aio_req req) 425static int poll_cb (void)
621{ 426{
622 while (started < wanted && nreqs >= started) 427 int res;
623 start_thread ();
624 428
625 ++nreqs; 429 do
626
627 LOCK (reqlock);
628 ++nready;
629 reqq_push (&req_queue, req);
630 pthread_cond_signal (&reqwait);
631 UNLOCK (reqlock);
632}
633
634static void end_thread (void)
635{
636 aio_req req;
637
638 Newz (0, req, 1, aio_cb);
639
640 req->type = REQ_QUIT;
641 req->pri = PRI_MAX + PRI_BIAS;
642
643 req_send (req);
644}
645
646static void min_parallel (int nthreads)
647{
648 if (wanted < nthreads)
649 wanted = nthreads;
650}
651
652static void max_parallel (int nthreads)
653{
654 int cur = started;
655
656 if (wanted > nthreads)
657 wanted = nthreads;
658
659 while (cur > wanted)
660 { 430 {
661 end_thread (); 431 res = eio_poll ();
662 cur--; 432
433 if (res > 0)
434 croak (0);
663 } 435 }
664 436 while (max_outstanding && max_outstanding <= eio_nreqs ());
665 while (started > wanted)
666 {
667 poll_wait ();
668 poll_cb (0);
669 }
670}
671
672static void create_pipe ()
673{
674 if (pipe (respipe))
675 croak ("unable to initialize result pipe");
676
677 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
678 croak ("cannot set result pipe to nonblocking mode");
679
680 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
681 croak ("cannot set result pipe to nonblocking mode");
682}
683
684/*****************************************************************************/
685/* work around various missing functions */
686
687#if !HAVE_PREADWRITE
688# define pread aio_pread
689# define pwrite aio_pwrite
690
691/*
692 * make our pread/pwrite safe against themselves, but not against
693 * normal read/write by using a mutex. slows down execution a lot,
694 * but that's your problem, not mine.
695 */
696static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER;
697
698static ssize_t pread (int fd, void *buf, size_t count, off_t offset)
699{
700 ssize_t res;
701 off_t ooffset;
702
703 LOCK (preadwritelock);
704 ooffset = lseek (fd, 0, SEEK_CUR);
705 lseek (fd, offset, SEEK_SET);
706 res = read (fd, buf, count);
707 lseek (fd, ooffset, SEEK_SET);
708 UNLOCK (preadwritelock);
709 437
710 return res; 438 return res;
711} 439}
712 440
713static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset)
714{
715 ssize_t res;
716 off_t ooffset;
717
718 LOCK (preadwritelock);
719 ooffset = lseek (fd, 0, SEEK_CUR);
720 lseek (fd, offset, SEEK_SET);
721 res = write (fd, buf, count);
722 lseek (fd, offset, SEEK_SET);
723 UNLOCK (preadwritelock);
724
725 return res;
726}
727#endif
728
729#if !HAVE_FDATASYNC
730# define fdatasync fsync
731#endif
732
733#if !HAVE_READAHEAD
734# define readahead(fd,offset,count) aio_readahead (fd, offset, count, self)
735
736static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self)
737{
738 dBUF;
739
740 while (count > 0)
741 {
742 size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE;
743
744 pread (fd, aio_buf, len, offset);
745 offset += len;
746 count -= len;
747 }
748
749 errno = 0;
750}
751
752#endif
753
754#if !HAVE_READDIR_R
755# define readdir_r aio_readdir_r
756
757static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER;
758
759static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res)
760{
761 struct dirent *e;
762 int errorno;
763
764 LOCK (readdirlock);
765
766 e = readdir (dirp);
767 errorno = errno;
768
769 if (e)
770 {
771 *res = ent;
772 strcpy (ent->d_name, e->d_name);
773 }
774 else
775 *res = 0;
776
777 UNLOCK (readdirlock);
778
779 errno = errorno;
780 return e ? 0 : -1;
781}
782#endif
783
784/* sendfile always needs emulation */
785static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self)
786{
787 ssize_t res;
788
789 if (!count)
790 return 0;
791
792#if HAVE_SENDFILE
793# if __linux
794 res = sendfile (ofd, ifd, &offset, count);
795
796# elif __freebsd
797 /*
798 * Of course, the freebsd sendfile is a dire hack with no thoughts
799 * wasted on making it similar to other I/O functions.
800 */
801 {
802 off_t sbytes;
803 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
804
805 if (res < 0 && sbytes)
806 /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */
807 res = sbytes;
808 }
809
810# elif __hpux
811 res = sendfile (ofd, ifd, offset, count, 0, 0);
812
813# elif __solaris
814 {
815 struct sendfilevec vec;
816 size_t sbytes;
817
818 vec.sfv_fd = ifd;
819 vec.sfv_flag = 0;
820 vec.sfv_off = offset;
821 vec.sfv_len = count;
822
823 res = sendfilev (ofd, &vec, 1, &sbytes);
824
825 if (res < 0 && sbytes)
826 res = sbytes;
827 }
828
829# endif
830#else
831 res = -1;
832 errno = ENOSYS;
833#endif
834
835 if (res < 0
836 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
837#if __solaris
838 || errno == EAFNOSUPPORT || errno == EPROTOTYPE
839#endif
840 )
841 )
842 {
843 /* emulate sendfile. this is a major pain in the ass */
844 dBUF;
845
846 res = 0;
847
848 while (count)
849 {
850 ssize_t cnt;
851
852 cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset);
853
854 if (cnt <= 0)
855 {
856 if (cnt && !res) res = -1;
857 break;
858 }
859
860 cnt = write (ofd, aio_buf, cnt);
861
862 if (cnt <= 0)
863 {
864 if (cnt && !res) res = -1;
865 break;
866 }
867
868 offset += cnt;
869 res += cnt;
870 count -= cnt;
871 }
872 }
873
874 return res;
875}
876
877/* read a full directory */
878static void scandir_ (aio_req req, worker *self)
879{
880 DIR *dirp;
881 union
882 {
883 struct dirent d;
884 char b [offsetof (struct dirent, d_name) + NAME_MAX + 1];
885 } *u;
886 struct dirent *entp;
887 char *name, *names;
888 int memlen = 4096;
889 int memofs = 0;
890 int res = 0;
891 int errorno;
892
893 LOCK (wrklock);
894 self->dirp = dirp = opendir (req->dataptr);
895 self->dbuf = u = malloc (sizeof (*u));
896 req->data2ptr = names = malloc (memlen);
897 UNLOCK (wrklock);
898
899 if (dirp && u && names)
900 for (;;)
901 {
902 errno = 0;
903 readdir_r (dirp, &u->d, &entp);
904
905 if (!entp)
906 break;
907
908 name = entp->d_name;
909
910 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
911 {
912 int len = strlen (name) + 1;
913
914 res++;
915
916 while (memofs + len > memlen)
917 {
918 memlen *= 2;
919 LOCK (wrklock);
920 req->data2ptr = names = realloc (names, memlen);
921 UNLOCK (wrklock);
922
923 if (!names)
924 break;
925 }
926
927 memcpy (names + memofs, name, len);
928 memofs += len;
929 }
930 }
931
932 if (errno)
933 res = -1;
934
935 req->result = res;
936}
937
938/*****************************************************************************/
939
940static void *aio_proc (void *thr_arg)
941{
942 aio_req req;
943 int type;
944 worker *self = (worker *)thr_arg;
945
946 do
947 {
948 LOCK (reqlock);
949
950 for (;;)
951 {
952 self->req = req = reqq_shift (&req_queue);
953
954 if (req)
955 break;
956
957 pthread_cond_wait (&reqwait, &reqlock);
958 }
959
960 --nready;
961
962 UNLOCK (reqlock);
963
964 errno = 0; /* strictly unnecessary */
965 type = req->type; /* remember type for QUIT check */
966
967 if (!(req->flags & FLAG_CANCELLED))
968 switch (type)
969 {
970 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
971 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
972
973 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
974 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length, self); break;
975
976 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
977 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
978 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
979
980 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
981 case REQ_CLOSE: req->result = close (req->fd); break;
982 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
983 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
984 case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break;
985 case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break;
986 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
987
988 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
989 case REQ_FSYNC: req->result = fsync (req->fd); break;
990 case REQ_READDIR: scandir_ (req, self); break;
991
992 case REQ_BUSY:
993 {
994 struct timeval tv;
995
996 tv.tv_sec = req->fd;
997 tv.tv_usec = req->fd2;
998
999 req->result = select (0, 0, 0, 0, &tv);
1000 }
1001
1002 case REQ_GROUP:
1003 case REQ_NOP:
1004 case REQ_QUIT:
1005 break;
1006
1007 default:
1008 req->result = ENOSYS;
1009 break;
1010 }
1011
1012 req->errorno = errno;
1013
1014 LOCK (reslock);
1015
1016 ++npending;
1017
1018 if (!reqq_push (&res_queue, req))
1019 /* write a dummy byte to the pipe so fh becomes ready */
1020 write (respipe [1], &respipe, 1);
1021
1022 self->req = 0;
1023 worker_clear (self);
1024
1025 UNLOCK (reslock);
1026 }
1027 while (type != REQ_QUIT);
1028
1029 LOCK (wrklock);
1030 worker_free (self);
1031 UNLOCK (wrklock);
1032
1033 return 0;
1034}
1035
1036/*****************************************************************************/
1037
1038static void atfork_prepare (void)
1039{
1040 LOCK (wrklock);
1041 LOCK (reqlock);
1042 LOCK (reslock);
1043#if !HAVE_PREADWRITE
1044 LOCK (preadwritelock);
1045#endif
1046#if !HAVE_READDIR_R
1047 LOCK (readdirlock);
1048#endif
1049}
1050
1051static void atfork_parent (void)
1052{
1053#if !HAVE_READDIR_R
1054 UNLOCK (readdirlock);
1055#endif
1056#if !HAVE_PREADWRITE
1057 UNLOCK (preadwritelock);
1058#endif
1059 UNLOCK (reslock);
1060 UNLOCK (reqlock);
1061 UNLOCK (wrklock);
1062}
1063
1064static void atfork_child (void) 441static void atfork_child (void)
1065{ 442{
1066 aio_req prv;
1067
1068 while (prv = reqq_shift (&req_queue))
1069 req_free (prv);
1070
1071 while (prv = reqq_shift (&res_queue))
1072 req_free (prv);
1073
1074 while (wrk_first.next != &wrk_first)
1075 {
1076 worker *wrk = wrk_first.next;
1077
1078 if (wrk->req)
1079 req_free (wrk->req);
1080
1081 worker_clear (wrk);
1082 worker_free (wrk);
1083 }
1084
1085 started = 0;
1086 nreqs = 0;
1087
1088 close (respipe [0]);
1089 close (respipe [1]);
1090 create_pipe (); 443 create_respipe ();
1091
1092 atfork_parent ();
1093} 444}
1094 445
1095#define dREQ \ 446#define dREQ \
1096 aio_req req; \ 447 aio_req req; \
1097 int req_pri = next_pri; \ 448 int req_pri = next_pri; \
1098 next_pri = DEFAULT_PRI + PRI_BIAS; \ 449 next_pri = EIO_DEFAULT_PRI + EIO_PRI_BIAS; \
1099 \ 450 \
1100 if (SvOK (callback) && !SvROK (callback)) \ 451 if (SvOK (callback) && !SvROK (callback)) \
1101 croak ("callback must be undef or of reference type"); \ 452 croak ("callback must be undef or of reference type"); \
1102 \ 453 \
1103 Newz (0, req, 1, aio_cb); \ 454 Newz (0, req, 1, eio_req); \
1104 if (!req) \ 455 if (!req) \
1105 croak ("out of memory during aio_req allocation"); \ 456 croak ("out of memory during eio_req allocation"); \
1106 \ 457 \
1107 req->callback = newSVsv (callback); \ 458 req->callback = newSVsv (callback); \
1108 req->pri = req_pri 459 req->pri = req_pri
1109 460
1110#define REQ_SEND \ 461#define REQ_SEND \
1111 req_send (req); \ 462 req_submit (req); \
1112 \ 463 \
1113 if (GIMME_V != G_VOID) \ 464 if (GIMME_V != G_VOID) \
1114 XPUSHs (req_sv (req, AIO_REQ_KLASS)); 465 XPUSHs (req_sv (req, AIO_REQ_KLASS));
1115 466
1116MODULE = IO::AIO PACKAGE = IO::AIO 467MODULE = IO::AIO PACKAGE = IO::AIO
1117 468
1118PROTOTYPES: ENABLE 469PROTOTYPES: ENABLE
1119 470
1120BOOT: 471BOOT:
1121{ 472{
1122 HV *stash = gv_stashpv ("IO::AIO", 1); 473 stash = gv_stashpv ("IO::AIO", 1);
474
1123 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); 475 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV));
1124 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 476 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY));
1125 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 477 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY));
478 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT));
479 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC));
480#ifdef _WIN32
481 X_MUTEX_CHECK (wrklock);
482 X_MUTEX_CHECK (reslock);
483 X_MUTEX_CHECK (reqlock);
484 X_MUTEX_CHECK (reqwait);
485 X_MUTEX_CHECK (preadwritelock);
486 X_MUTEX_CHECK (readdirlock);
1126 487
1127 create_pipe (); 488 X_COND_CHECK (reqwait);
1128 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 489#else
490 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO));
491#endif
492
493 create_respipe ();
494
495 if (eio_init (want_poll, done_poll) < 0)
496 croak ("IO::AIO: unable to initialise eio library");
497
498 /* atfork child called in fifo order, so before eio's handler */
499 X_THREAD_ATFORK (0, 0, atfork_child);
1129} 500}
501
502void
503max_poll_reqs (int nreqs)
504 PROTOTYPE: $
505 CODE:
506 eio_set_max_poll_reqs (nreqs);
507
508void
509max_poll_time (double nseconds)
510 PROTOTYPE: $
511 CODE:
512 eio_set_max_poll_time (nseconds);
1130 513
1131void 514void
1132min_parallel (int nthreads) 515min_parallel (int nthreads)
1133 PROTOTYPE: $ 516 PROTOTYPE: $
517 CODE:
518 eio_set_min_parallel (nthreads);
1134 519
1135void 520void
1136max_parallel (int nthreads) 521max_parallel (int nthreads)
1137 PROTOTYPE: $ 522 PROTOTYPE: $
523 CODE:
524 eio_set_max_parallel (nthreads);
1138 525
1139int 526void
527max_idle (int nthreads)
528 PROTOTYPE: $
529 CODE:
530 eio_set_max_idle (nthreads);
531
532void
1140max_outstanding (int maxreqs) 533max_outstanding (int maxreqs)
1141 PROTOTYPE: $ 534 PROTOTYPE: $
1142 CODE: 535 CODE:
1143 RETVAL = max_outstanding;
1144 max_outstanding = maxreqs; 536 max_outstanding = maxreqs;
1145 OUTPUT:
1146 RETVAL
1147 537
1148void 538void
1149aio_open (pathname,flags,mode,callback=&PL_sv_undef) 539aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
1150 SV * pathname
1151 int flags
1152 int mode
1153 SV * callback
1154 PROTOTYPE: $$$;$ 540 PROTOTYPE: $$$;$
1155 PPCODE: 541 PPCODE:
1156{ 542{
1157 dREQ; 543 dREQ;
1158 544
1159 req->type = REQ_OPEN; 545 req->type = EIO_OPEN;
1160 req->data = newSVsv (pathname); 546 req->sv1 = newSVsv (pathname);
1161 req->dataptr = SvPVbyte_nolen (req->data); 547 req->ptr1 = SvPVbyte_nolen (req->sv1);
1162 req->fd = flags; 548 req->int1 = flags;
1163 req->mode = mode; 549 req->int2 = mode;
1164 550
1165 REQ_SEND; 551 REQ_SEND;
1166} 552}
1167 553
1168void 554void
1169aio_close (fh,callback=&PL_sv_undef) 555aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
1170 SV * fh
1171 SV * callback
1172 PROTOTYPE: $;$ 556 PROTOTYPE: $;$
1173 ALIAS: 557 ALIAS:
1174 aio_close = REQ_CLOSE
1175 aio_fsync = REQ_FSYNC 558 aio_fsync = EIO_FSYNC
1176 aio_fdatasync = REQ_FDATASYNC 559 aio_fdatasync = EIO_FDATASYNC
1177 PPCODE: 560 PPCODE:
1178{ 561{
1179 dREQ; 562 dREQ;
1180 563
1181 req->type = ix; 564 req->type = ix;
1182 req->fh = newSVsv (fh); 565 req->sv1 = newSVsv (fh);
1183 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 566 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1184 567
1185 REQ_SEND (req); 568 REQ_SEND (req);
1186} 569}
1187 570
1188void 571void
572aio_close (SV *fh, SV *callback=&PL_sv_undef)
573 PROTOTYPE: $;$
574 PPCODE:
575{
576 static int close_pipe = -1; /* dummy fd to close fds via dup2 */
577 dREQ;
578
579 if (close_pipe < 0)
580 {
581 int pipefd [2];
582
583 if (pipe (pipefd) < 0
584 || close (pipefd [1]) < 0
585 || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0)
586 abort (); /*D*/
587
588 close_pipe = pipefd [0];
589 }
590
591 req->type = EIO_DUP2;
592 req->int1 = close_pipe;
593 req->sv2 = newSVsv (fh);
594 req->int2 = PerlIO_fileno (IoIFP (sv_2io (fh)));
595
596 REQ_SEND (req);
597}
598
599void
1189aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 600aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
1190 SV * fh
1191 UV offset
1192 UV length
1193 SV * data
1194 UV dataoffset
1195 SV * callback
1196 ALIAS: 601 ALIAS:
1197 aio_read = REQ_READ 602 aio_read = EIO_READ
1198 aio_write = REQ_WRITE 603 aio_write = EIO_WRITE
1199 PROTOTYPE: $$$$$;$ 604 PROTOTYPE: $$$$$;$
1200 PPCODE: 605 PPCODE:
1201{ 606{
1202 aio_req req;
1203 STRLEN svlen; 607 STRLEN svlen;
1204 char *svptr = SvPVbyte (data, svlen); 608 char *svptr = SvPVbyte (data, svlen);
609 UV len = SvUV (length);
1205 610
1206 SvUPGRADE (data, SVt_PV); 611 SvUPGRADE (data, SVt_PV);
1207 SvPOK_on (data); 612 SvPOK_on (data);
1208 613
1209 if (dataoffset < 0) 614 if (dataoffset < 0)
1210 dataoffset += svlen; 615 dataoffset += svlen;
1211 616
1212 if (dataoffset < 0 || dataoffset > svlen) 617 if (dataoffset < 0 || dataoffset > svlen)
1213 croak ("data offset outside of string"); 618 croak ("dataoffset outside of data scalar");
1214 619
1215 if (ix == REQ_WRITE) 620 if (ix == EIO_WRITE)
1216 { 621 {
1217 /* write: check length and adjust. */ 622 /* write: check length and adjust. */
1218 if (length < 0 || length + dataoffset > svlen) 623 if (!SvOK (length) || len + dataoffset > svlen)
1219 length = svlen - dataoffset; 624 len = svlen - dataoffset;
1220 } 625 }
1221 else 626 else
1222 { 627 {
1223 /* read: grow scalar as necessary */ 628 /* read: grow scalar as necessary */
1224 svptr = SvGROW (data, length + dataoffset); 629 svptr = SvGROW (data, len + dataoffset + 1);
1225 } 630 }
1226 631
1227 if (length < 0) 632 if (len < 0)
1228 croak ("length must not be negative"); 633 croak ("length must not be negative");
1229 634
1230 { 635 {
1231 dREQ; 636 dREQ;
1232 637
1233 req->type = ix; 638 req->type = ix;
1234 req->fh = newSVsv (fh); 639 req->sv1 = newSVsv (fh);
1235 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 640 req->int1 = PerlIO_fileno (ix == EIO_READ ? IoIFP (sv_2io (fh))
1236 : IoOFP (sv_2io (fh))); 641 : IoOFP (sv_2io (fh)));
642 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1237 req->offset = offset; 643 req->size = len;
1238 req->length = length;
1239 req->data = SvREFCNT_inc (data); 644 req->sv2 = SvREFCNT_inc (data);
1240 req->dataptr = (char *)svptr + dataoffset; 645 req->ptr2 = (char *)svptr + dataoffset;
646 req->stroffset = dataoffset;
1241 647
1242 if (!SvREADONLY (data)) 648 if (!SvREADONLY (data))
1243 { 649 {
1244 SvREADONLY_on (data); 650 SvREADONLY_on (data);
1245 req->data2ptr = (void *)data; 651 req->flags |= FLAG_SV2_RO_OFF;
1246 } 652 }
1247 653
1248 REQ_SEND; 654 REQ_SEND;
1249 } 655 }
1250} 656}
1251 657
1252void 658void
659aio_readlink (SV8 *path, SV *callback=&PL_sv_undef)
660 PROTOTYPE: $$;$
661 PPCODE:
662{
663 SV *data;
664 dREQ;
665
666 data = newSV (NAME_MAX);
667 SvPOK_on (data);
668
669 req->type = EIO_READLINK;
670 req->sv1 = newSVsv (path);
671 req->ptr1 = SvPVbyte_nolen (req->sv1);
672 req->sv2 = data;
673 req->ptr2 = SvPVbyte_nolen (data);
674
675 REQ_SEND;
676}
677
678void
1253aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) 679aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef)
1254 SV * out_fh
1255 SV * in_fh
1256 UV in_offset
1257 UV length
1258 SV * callback
1259 PROTOTYPE: $$$$;$ 680 PROTOTYPE: $$$$;$
1260 PPCODE: 681 PPCODE:
1261{ 682{
1262 dREQ; 683 dREQ;
1263 684
1264 req->type = REQ_SENDFILE; 685 req->type = EIO_SENDFILE;
1265 req->fh = newSVsv (out_fh); 686 req->sv1 = newSVsv (out_fh);
1266 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); 687 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh)));
1267 req->fh2 = newSVsv (in_fh); 688 req->sv2 = newSVsv (in_fh);
1268 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 689 req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh)));
1269 req->offset = in_offset; 690 req->offs = SvVAL64 (in_offset);
1270 req->length = length; 691 req->size = length;
1271 692
1272 REQ_SEND; 693 REQ_SEND;
1273} 694}
1274 695
1275void 696void
1276aio_readahead (fh,offset,length,callback=&PL_sv_undef) 697aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef)
1277 SV * fh
1278 UV offset
1279 IV length
1280 SV * callback
1281 PROTOTYPE: $$$;$ 698 PROTOTYPE: $$$;$
1282 PPCODE: 699 PPCODE:
1283{ 700{
1284 dREQ; 701 dREQ;
1285 702
1286 req->type = REQ_READAHEAD; 703 req->type = EIO_READAHEAD;
1287 req->fh = newSVsv (fh); 704 req->sv1 = newSVsv (fh);
1288 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 705 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh)));
1289 req->offset = offset; 706 req->offs = SvVAL64 (offset);
1290 req->length = length; 707 req->size = length;
1291 708
1292 REQ_SEND; 709 REQ_SEND;
1293} 710}
1294 711
1295void 712void
1296aio_stat (fh_or_path,callback=&PL_sv_undef) 713aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
1297 SV * fh_or_path
1298 SV * callback
1299 ALIAS: 714 ALIAS:
1300 aio_stat = REQ_STAT 715 aio_stat = EIO_STAT
1301 aio_lstat = REQ_LSTAT 716 aio_lstat = EIO_LSTAT
1302 PPCODE: 717 PPCODE:
1303{ 718{
1304 dREQ; 719 dREQ;
1305 720
1306 New (0, req->statdata, 1, Stat_t); 721 req->ptr2 = malloc (sizeof (EIO_STRUCT_STAT));
1307 if (!req->statdata) 722 if (!req->ptr2)
1308 { 723 {
1309 req_free (req); 724 req_destroy (req);
1310 croak ("out of memory during aio_req->statdata allocation"); 725 croak ("out of memory during aio_stat statdata allocation");
1311 } 726 }
727
728 req->flags |= EIO_FLAG_PTR2_FREE;
729 req->sv1 = newSVsv (fh_or_path);
1312 730
1313 if (SvPOK (fh_or_path)) 731 if (SvPOK (fh_or_path))
1314 { 732 {
1315 req->type = ix; 733 req->type = ix;
1316 req->data = newSVsv (fh_or_path);
1317 req->dataptr = SvPVbyte_nolen (req->data); 734 req->ptr1 = SvPVbyte_nolen (req->sv1);
1318 } 735 }
1319 else 736 else
1320 { 737 {
1321 req->type = REQ_FSTAT; 738 req->type = EIO_FSTAT;
1322 req->fh = newSVsv (fh_or_path);
1323 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 739 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1324 } 740 }
1325 741
1326 REQ_SEND; 742 REQ_SEND;
1327} 743}
1328 744
1329void 745void
746aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
747 PPCODE:
748{
749 dREQ;
750
751 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
752 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
753 req->sv1 = newSVsv (fh_or_path);
754
755 if (SvPOK (fh_or_path))
756 {
757 req->type = EIO_UTIME;
758 req->ptr1 = SvPVbyte_nolen (req->sv1);
759 }
760 else
761 {
762 req->type = EIO_FUTIME;
763 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
764 }
765
766 REQ_SEND;
767}
768
769void
770aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
771 PPCODE:
772{
773 dREQ;
774
775 req->sv1 = newSVsv (fh_or_path);
776 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
777
778 if (SvPOK (fh_or_path))
779 {
780 req->type = EIO_TRUNCATE;
781 req->ptr1 = SvPVbyte_nolen (req->sv1);
782 }
783 else
784 {
785 req->type = EIO_FTRUNCATE;
786 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
787 }
788
789 REQ_SEND;
790}
791
792void
793aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
794 ALIAS:
795 aio_chmod = EIO_CHMOD
796 aio_fchmod = EIO_FCHMOD
797 aio_mkdir = EIO_MKDIR
798 PPCODE:
799{
800 dREQ;
801
802 req->type = ix;
803 req->int2 = mode;
804 req->sv1 = newSVsv (fh_or_path);
805
806 if (ix == EIO_FCHMOD)
807 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
808 else
809 req->ptr1 = SvPVbyte_nolen (req->sv1);
810
811 REQ_SEND;
812}
813
814void
815aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef)
816 PPCODE:
817{
818 dREQ;
819
820 req->int2 = SvOK (uid) ? SvIV (uid) : -1;
821 req->int3 = SvOK (gid) ? SvIV (gid) : -1;
822 req->sv1 = newSVsv (fh_or_path);
823
824 if (SvPOK (fh_or_path))
825 {
826 req->type = EIO_CHOWN;
827 req->ptr1 = SvPVbyte_nolen (req->sv1);
828 }
829 else
830 {
831 req->type = EIO_FCHOWN;
832 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
833 }
834
835 REQ_SEND;
836}
837
838void
1330aio_unlink (pathname,callback=&PL_sv_undef) 839aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1331 SV * pathname
1332 SV * callback
1333 ALIAS: 840 ALIAS:
1334 aio_unlink = REQ_UNLINK 841 aio_unlink = EIO_UNLINK
1335 aio_rmdir = REQ_RMDIR 842 aio_rmdir = EIO_RMDIR
1336 aio_readdir = REQ_READDIR 843 aio_readdir = EIO_READDIR
1337 PPCODE: 844 PPCODE:
1338{ 845{
1339 dREQ; 846 dREQ;
1340 847
1341 req->type = ix; 848 req->type = ix;
1342 req->data = newSVsv (pathname); 849 req->sv1 = newSVsv (pathname);
1343 req->dataptr = SvPVbyte_nolen (req->data); 850 req->ptr1 = SvPVbyte_nolen (req->sv1);
851
852 REQ_SEND;
853}
854
855void
856aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
857 ALIAS:
858 aio_link = EIO_LINK
859 aio_symlink = EIO_SYMLINK
860 aio_rename = EIO_RENAME
861 PPCODE:
862{
863 dREQ;
864
865 req->type = ix;
866 req->sv1 = newSVsv (oldpath);
867 req->ptr1 = SvPVbyte_nolen (req->sv1);
868 req->sv2 = newSVsv (newpath);
869 req->ptr2 = SvPVbyte_nolen (req->sv2);
1344 870
1345 REQ_SEND; 871 REQ_SEND;
1346} 872}
1347 873
1348void 874void
1349aio_link (oldpath,newpath,callback=&PL_sv_undef) 875aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
1350 SV * oldpath
1351 SV * newpath
1352 SV * callback
1353 ALIAS:
1354 aio_link = REQ_LINK
1355 aio_symlink = REQ_SYMLINK
1356 aio_rename = REQ_RENAME
1357 PPCODE: 876 PPCODE:
1358{ 877{
1359 dREQ; 878 dREQ;
1360 879
1361 req->type = ix; 880 req->type = EIO_MKNOD;
1362 req->fh = newSVsv (oldpath); 881 req->sv1 = newSVsv (pathname);
1363 req->data2ptr = SvPVbyte_nolen (req->fh); 882 req->ptr1 = SvPVbyte_nolen (req->sv1);
1364 req->data = newSVsv (newpath); 883 req->int2 = (mode_t)mode;
1365 req->dataptr = SvPVbyte_nolen (req->data); 884 req->offs = dev;
1366 885
1367 REQ_SEND; 886 REQ_SEND;
1368} 887}
1369 888
1370void 889void
1371aio_busy (delay,callback=&PL_sv_undef) 890aio_busy (double delay, SV *callback=&PL_sv_undef)
1372 double delay
1373 SV * callback
1374 PPCODE: 891 PPCODE:
1375{ 892{
1376 dREQ; 893 dREQ;
1377 894
1378 req->type = REQ_BUSY; 895 req->type = EIO_BUSY;
1379 req->fd = delay < 0. ? 0 : delay; 896 req->nv1 = delay < 0. ? 0. : delay;
1380 req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd);
1381 897
1382 REQ_SEND; 898 REQ_SEND;
1383} 899}
1384 900
1385void 901void
1386aio_group (callback=&PL_sv_undef) 902aio_group (SV *callback=&PL_sv_undef)
1387 SV * callback
1388 PROTOTYPE: ;$ 903 PROTOTYPE: ;$
1389 PPCODE: 904 PPCODE:
1390{ 905{
1391 dREQ; 906 dREQ;
1392 907
1393 req->type = REQ_GROUP; 908 req->type = EIO_GROUP;
909
1394 req_send (req); 910 eio_submit (req);
1395
1396 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 911 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1397} 912}
1398 913
1399void 914void
1400aio_nop (callback=&PL_sv_undef) 915aio_nop (SV *callback=&PL_sv_undef)
1401 SV * callback 916 ALIAS:
917 aio_nop = EIO_NOP
918 aio_sync = EIO_SYNC
1402 PPCODE: 919 PPCODE:
1403{ 920{
1404 dREQ; 921 dREQ;
1405 922
1406 req->type = REQ_NOP; 923 req->type = ix;
1407 924
1408 REQ_SEND; 925 REQ_SEND;
1409} 926}
1410 927
1411int 928int
1412aioreq_pri (int pri = 0) 929aioreq_pri (int pri = 0)
1413 PROTOTYPE: ;$ 930 PROTOTYPE: ;$
1414 CODE: 931 CODE:
1415 RETVAL = next_pri - PRI_BIAS; 932 RETVAL = next_pri - EIO_PRI_BIAS;
1416 if (items > 0) 933 if (items > 0)
1417 { 934 {
1418 if (pri < PRI_MIN) pri = PRI_MIN; 935 if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN;
1419 if (pri > PRI_MAX) pri = PRI_MAX; 936 if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX;
1420 next_pri = pri + PRI_BIAS; 937 next_pri = pri + EIO_PRI_BIAS;
1421 } 938 }
1422 OUTPUT: 939 OUTPUT:
1423 RETVAL 940 RETVAL
1424 941
1425void 942void
1426aioreq_nice (int nice = 0) 943aioreq_nice (int nice = 0)
1427 CODE: 944 CODE:
1428 nice = next_pri - nice; 945 nice = next_pri - nice;
1429 if (nice < PRI_MIN) nice = PRI_MIN; 946 if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN;
1430 if (nice > PRI_MAX) nice = PRI_MAX; 947 if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX;
1431 next_pri = nice + PRI_BIAS; 948 next_pri = nice + EIO_PRI_BIAS;
1432 949
1433void 950void
1434flush () 951flush ()
1435 PROTOTYPE: 952 PROTOTYPE:
1436 CODE: 953 CODE:
1437 while (nreqs) 954 while (eio_nreqs ())
1438 { 955 {
1439 poll_wait (); 956 poll_wait ();
1440 poll_cb (0); 957 poll_cb ();
1441 } 958 }
1442 959
1443void 960int
1444poll() 961poll()
1445 PROTOTYPE: 962 PROTOTYPE:
1446 CODE: 963 CODE:
1447 if (nreqs)
1448 {
1449 poll_wait (); 964 poll_wait ();
1450 poll_cb (0); 965 RETVAL = poll_cb ();
1451 } 966 OUTPUT:
967 RETVAL
1452 968
1453int 969int
1454poll_fileno() 970poll_fileno()
1455 PROTOTYPE: 971 PROTOTYPE:
1456 CODE: 972 CODE:
1460 976
1461int 977int
1462poll_cb(...) 978poll_cb(...)
1463 PROTOTYPE: 979 PROTOTYPE:
1464 CODE: 980 CODE:
1465 RETVAL = poll_cb (0); 981 RETVAL = poll_cb ();
1466 OUTPUT: 982 OUTPUT:
1467 RETVAL 983 RETVAL
1468 984
1469int
1470poll_some(int max = 0)
1471 PROTOTYPE: $
1472 CODE:
1473 RETVAL = poll_cb (max);
1474 OUTPUT:
1475 RETVAL
1476
1477void 985void
1478poll_wait() 986poll_wait()
1479 PROTOTYPE: 987 PROTOTYPE:
1480 CODE: 988 CODE:
1481 if (nreqs)
1482 poll_wait (); 989 poll_wait ();
1483 990
1484int 991int
1485nreqs() 992nreqs()
1486 PROTOTYPE: 993 PROTOTYPE:
1487 CODE: 994 CODE:
1488 RETVAL = nreqs; 995 RETVAL = eio_nreqs ();
1489 OUTPUT: 996 OUTPUT:
1490 RETVAL 997 RETVAL
1491 998
1492int 999int
1493nready() 1000nready()
1494 PROTOTYPE: 1001 PROTOTYPE:
1495 CODE: 1002 CODE:
1496 if (WORDREAD_UNSAFE) LOCK (reqlock);
1497 RETVAL = nready; 1003 RETVAL = eio_nready ();
1498 if (WORDREAD_UNSAFE) UNLOCK (reqlock);
1499 OUTPUT: 1004 OUTPUT:
1500 RETVAL 1005 RETVAL
1501 1006
1502int 1007int
1503npending() 1008npending()
1504 PROTOTYPE: 1009 PROTOTYPE:
1505 CODE: 1010 CODE:
1506 if (WORDREAD_UNSAFE) LOCK (reslock);
1507 RETVAL = npending; 1011 RETVAL = eio_npending ();
1508 if (WORDREAD_UNSAFE) UNLOCK (reslock);
1509 OUTPUT: 1012 OUTPUT:
1510 RETVAL 1013 RETVAL
1511 1014
1015int
1016nthreads()
1017 PROTOTYPE:
1018 CODE:
1019 if (WORDACCESS_UNSAFE) X_LOCK (wrklock);
1020 RETVAL = started;
1021 if (WORDACCESS_UNSAFE) X_UNLOCK (wrklock);
1022 OUTPUT:
1023 RETVAL
1024
1025void _on_next_submit (SV *cb)
1026 CODE:
1027 SvREFCNT_dec (on_next_submit);
1028 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1029
1512PROTOTYPES: DISABLE 1030PROTOTYPES: DISABLE
1513 1031
1514MODULE = IO::AIO PACKAGE = IO::AIO::REQ 1032MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1515 1033
1516void 1034void
1517cancel (aio_req_ornot req) 1035cancel (aio_req_ornot req)
1518 CODE: 1036 CODE:
1519 req_cancel (req); 1037 eio_cancel (req);
1520 1038
1521void 1039void
1522cb (aio_req_ornot req, SV *callback=&PL_sv_undef) 1040cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1523 CODE: 1041 CODE:
1524 SvREFCNT_dec (req->callback); 1042 SvREFCNT_dec (req->callback);
1529void 1047void
1530add (aio_req grp, ...) 1048add (aio_req grp, ...)
1531 PPCODE: 1049 PPCODE:
1532{ 1050{
1533 int i; 1051 int i;
1534 aio_req req;
1535 1052
1536 if (grp->fd == 2) 1053 if (grp->int1 == 2)
1537 croak ("cannot add requests to IO::AIO::GRP after the group finished"); 1054 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1538 1055
1539 for (i = 1; i < items; ++i ) 1056 for (i = 1; i < items; ++i )
1540 { 1057 {
1058 aio_req req;
1059
1541 if (GIMME_V != G_VOID) 1060 if (GIMME_V != G_VOID)
1542 XPUSHs (sv_2mortal (newSVsv (ST (i)))); 1061 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1543 1062
1544 req = SvAIO_REQ (ST (i)); 1063 req = SvAIO_REQ (ST (i));
1545 1064
1546 if (req) 1065 if (req)
1547 { 1066 eio_grp_add (grp, req);
1548 ++grp->length;
1549 req->grp = grp;
1550
1551 req->grp_prev = 0;
1552 req->grp_next = grp->grp_first;
1553
1554 if (grp->grp_first)
1555 grp->grp_first->grp_prev = req;
1556
1557 grp->grp_first = req;
1558 }
1559 } 1067 }
1560} 1068}
1561 1069
1562void 1070void
1563cancel_subs (aio_req_ornot req) 1071cancel_subs (aio_req_ornot req)
1576 av = newAV (); 1084 av = newAV ();
1577 1085
1578 for (i = 1; i < items; ++i ) 1086 for (i = 1; i < items; ++i )
1579 av_push (av, newSVsv (ST (i))); 1087 av_push (av, newSVsv (ST (i)));
1580 1088
1581 SvREFCNT_dec (grp->data); 1089 SvREFCNT_dec (grp->sv1);
1582 grp->data = (SV *)av; 1090 grp->sv1 = (SV *)av;
1583} 1091}
1584 1092
1585void 1093void
1586errno (aio_req grp, int errorno = errno) 1094errno (aio_req grp, int errorno = errno)
1587 CODE: 1095 CODE:
1588 grp->errorno = errorno; 1096 grp->errorno = errorno;
1589 1097
1590void 1098void
1591limit (aio_req grp, int limit) 1099limit (aio_req grp, int limit)
1592 CODE: 1100 CODE:
1593 grp->fd2 = limit; 1101 eio_grp_limit (grp, limit);
1594 aio_grp_feed (grp);
1595 1102
1596void 1103void
1597feed (aio_req grp, SV *callback=&PL_sv_undef) 1104feed (aio_req grp, SV *callback=&PL_sv_undef)
1598 CODE: 1105 CODE:
1599{ 1106{
1600 SvREFCNT_dec (grp->fh2); 1107 SvREFCNT_dec (grp->sv2);
1601 grp->fh2 = newSVsv (callback); 1108 grp->sv2 = newSVsv (callback);
1602 1109
1603 if (grp->fd2 <= 0) 1110 if (grp->int2 <= 0)
1604 grp->fd2 = 2; 1111 grp->int2 = 2;
1605 1112
1606 aio_grp_feed (grp); 1113 eio_grp_limit (grp, grp->int2);
1607} 1114}
1608 1115

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines