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.34 by root, Tue Aug 23 00:03:14 2005 UTC vs.
Revision 1.152 by root, Tue Jul 14 00:32:27 2009 UTC

1#define _REENTRANT 1 1#include "libeio/xthread.h"
2
2#include <errno.h> 3#include <errno.h>
3 4
4#include "EXTERN.h" 5#include "EXTERN.h"
5#include "perl.h" 6#include "perl.h"
6#include "XSUB.h" 7#include "XSUB.h"
7 8
8#include "autoconf/config.h" 9#include "schmorp.h"
9 10
10#include <pthread.h> 11#include <stddef.h>
11 12#include <stdlib.h>
13#include <errno.h>
12#include <sys/types.h> 14#include <sys/types.h>
13#include <sys/stat.h> 15#include <sys/stat.h>
14
15#include <unistd.h> 16#include <limits.h>
16#include <fcntl.h> 17#include <fcntl.h>
17#include <signal.h>
18#include <sched.h> 18#include <sched.h>
19 19
20#if HAVE_SENDFILE 20/* perl namespace pollution */
21# if __linux 21#undef VERSION
22# include <sys/sendfile.h>
23# elif __freebsd
24# include <sys/socket.h>
25# include <sys/uio.h>
26# elif __hpux
27# include <sys/socket.h>
28# else
29# error sendfile support requested but not available
30# endif
31#endif
32 22
33#if __ia64 23#ifdef _WIN32
34# define STACKSIZE 65536 24
25# define EIO_STRUCT_DIRENT Direntry_t
26# undef malloc
27# undef free
28
29// perl overrides all those nice win32 functions
30# undef open
31# undef read
32# undef write
33# undef send
34# undef recv
35# undef stat
36# undef fstat
37# define lstat stat
38# undef truncate
39# undef ftruncate
40# undef open
41# undef close
42# undef unlink
43# undef rmdir
44# undef rename
45# undef lseek
46
47# define chown(a,b,c) (errno = ENOSYS, -1)
48# define fchown(a,b,c) (errno = ENOSYS, -1)
49# define fchmod(a,b) (errno = ENOSYS, -1)
50# define symlink(a,b) (errno = ENOSYS, -1)
51# define readlink(a,b,c) (errno = ENOSYS, -1)
52# define mknod(a,b,c) (errno = ENOSYS, -1)
53# define truncate(a,b) (errno = ENOSYS, -1)
54# define ftruncate(fd,o) chsize ((fd), (o))
55# define fsync(fd) _commit (fd)
56# define opendir(fd) (errno = ENOSYS, 0)
57# define readdir(fd) (errno = ENOSYS, -1)
58# define closedir(fd) (errno = ENOSYS, -1)
59# define mkdir(a,b) mkdir (a)
60
35#else 61#else
36# define STACKSIZE 4096 62
63# include <sys/time.h>
64# include <sys/select.h>
65# include <unistd.h>
66# include <utime.h>
67# include <signal.h>
68# define EIO_STRUCT_DIRENT struct dirent
69
37#endif 70#endif
71
72/* perl stupidly overrides readdir and maybe others */
73/* with thread-unsafe versions, imagine that :( */
74#undef readdir
75#undef opendir
76#undef closedir
77
78#define EIO_STRUCT_STAT Stat_t
79
80/* use NV for 32 bit perls as it allows larger offsets */
81#if IVSIZE >= 8
82# define VAL64 IV
83# define SvVAL64 SvIV
84# define newSVval64 newSViv
85#else
86# define VAL64 NV
87# define SvVAL64 SvNV
88# define newSVval64 newSVnv
89#endif
90
91/*****************************************************************************/
92
93#if __GNUC__ >= 3
94# define expect(expr,value) __builtin_expect ((expr),(value))
95#else
96# define expect(expr,value) (expr)
97#endif
98
99#define expect_false(expr) expect ((expr) != 0, 0)
100#define expect_true(expr) expect ((expr) != 0, 1)
101
102/*****************************************************************************/
103
104static HV *stash;
105typedef SV SV8; /* byte-sv, used for argument-checking */
106typedef int aio_rfd; /* read file desriptor */
107typedef int aio_wfd; /* write file descriptor */
108
109#define AIO_REQ_KLASS "IO::AIO::REQ"
110#define AIO_GRP_KLASS "IO::AIO::GRP"
111
112#define EIO_REQ_MEMBERS \
113 SV *callback; \
114 SV *sv1, *sv2; \
115 STRLEN stroffset; \
116 SV *self;
117
118#define EIO_NO_WRAPPERS 1
119
120#include "libeio/eio.h"
121
122#ifndef POSIX_FADV_NORMAL
123# define POSIX_FADV_NORMAL 0
124# define NO_FADVISE 1
125#endif
126
127#ifndef POSIX_FADV_SEQUENTIAL
128# define POSIX_FADV_SEQUENTIAL 0
129#endif
130
131#ifndef POSIX_FADV_RANDOM
132# define POSIX_FADV_RANDOM 0
133#endif
134
135#ifndef POSIX_FADV_NOREUSE
136# define POSIX_FADV_NOREUSE 0
137#endif
138
139#ifndef POSIX_FADV_WILLNEED
140# define POSIX_FADV_WILLNEED 0
141#endif
142
143#ifndef POSIX_FADV_DONTNEED
144# define POSIX_FADV_DONTNEED 0
145#endif
146
147static int req_invoke (eio_req *req);
148#define EIO_FINISH(req) req_invoke (req)
149static void req_destroy (eio_req *grp);
150#define EIO_DESTROY(req) req_destroy (req)
38 151
39enum { 152enum {
40 REQ_QUIT, 153 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
41 REQ_OPEN, REQ_CLOSE,
42 REQ_READ, REQ_WRITE, REQ_READAHEAD,
43 REQ_SENDFILE,
44 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
45 REQ_FSYNC, REQ_FDATASYNC,
46 REQ_UNLINK, REQ_RMDIR,
47 REQ_SYMLINK,
48}; 154};
49 155
50typedef struct aio_cb { 156#include "libeio/eio.c"
51 struct aio_cb *volatile next;
52 157
53 int type;
54
55 int fd, fd2;
56 off_t offset;
57 size_t length;
58 ssize_t result;
59 mode_t mode; /* open */
60 int errorno;
61 SV *data, *callback;
62 SV *fh, *fh2;
63 void *dataptr, *data2ptr;
64 STRLEN dataoffset;
65
66 Stat_t *statdata;
67} aio_cb;
68
69typedef aio_cb *aio_req; 158typedef eio_req *aio_req;
159typedef eio_req *aio_req_ornot;
70 160
71static int started, wanted; 161static SV *on_next_submit;
72static volatile int nreqs; 162static int next_pri = EIO_PRI_DEFAULT;
73static int max_outstanding = 1<<30; 163static int max_outstanding;
74static int respipe [2];
75 164
76static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 165static int respipe_osf [2], respipe [2] = { -1, -1 };
77static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
78static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
79 166
80static volatile aio_req reqs, reqe; /* queue start, queue end */ 167static void req_destroy (aio_req req);
81static volatile aio_req ress, rese; /* queue start, queue end */
82
83static void free_req (aio_req req) 168static void req_cancel (aio_req req);
84{
85 if (req->data)
86 SvREFCNT_dec (req->data);
87 169
170static void want_poll (void)
171{
172 /* write a dummy byte to the pipe so fh becomes ready */
173 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1);
174}
175
176static void done_poll (void)
177{
178 /* read any signals sent by the worker threads */
179 char buf [4];
180 while (respipe_read (respipe [0], buf, 4) == 4)
181 ;
182}
183
184/* must be called at most once */
185static SV *req_sv (aio_req req, const char *klass)
186{
187 if (!req->self)
188 {
189 req->self = (SV *)newHV ();
190 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
191 }
192
193 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1)));
194}
195
196static aio_req SvAIO_REQ (SV *sv)
197{
198 MAGIC *mg;
199
200 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv))
201 croak ("object of class " AIO_REQ_KLASS " expected");
202
203 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
204
205 return mg ? (aio_req)mg->mg_ptr : 0;
206}
207
208static void aio_grp_feed (aio_req grp)
209{
210 if (grp->sv2 && SvOK (grp->sv2))
211 {
212 dSP;
213
214 ENTER;
215 SAVETMPS;
216 PUSHMARK (SP);
217 XPUSHs (req_sv (grp, AIO_GRP_KLASS));
218 PUTBACK;
219 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
220 SPAGAIN;
221 FREETMPS;
222 LEAVE;
223 }
224}
225
226static void req_submit (eio_req *req)
227{
228 eio_submit (req);
229
230 if (expect_false (on_next_submit))
231 {
232 dSP;
233 SV *cb = sv_2mortal (on_next_submit);
234
235 on_next_submit = 0;
236
237 PUSHMARK (SP);
238 PUTBACK;
239 call_sv (cb, G_DISCARD | G_EVAL);
240 }
241}
242
243static int req_invoke (eio_req *req)
244{
245 dSP;
246
247 if (req->flags & FLAG_SV2_RO_OFF)
248 SvREADONLY_off (req->sv2);
249
250 if (!EIO_CANCELLED (req) && req->callback)
251 {
252 ENTER;
253 SAVETMPS;
254 PUSHMARK (SP);
255 EXTEND (SP, 1);
256
257 switch (req->type)
258 {
259 case EIO_READDIR:
260 {
261 SV *rv = &PL_sv_undef;
262
263 if (req->result >= 0)
264 {
265 int i;
266 char *names = (char *)req->ptr2;
267 eio_dirent *ent = (eio_dirent *)req->ptr1; /* might be 0 */
268 AV *av = newAV ();
269
270 av_extend (av, req->result - 1);
271
272 for (i = 0; i < req->result; ++i)
273 {
274 if (req->int1 & EIO_READDIR_DENTS)
275 {
276 SV *namesv = newSVpvn (names + ent->nameofs, ent->namelen);
277
278 if (req->int1 & EIO_READDIR_CUSTOM2)
279 {
280 static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */
281 AV *avent = newAV ();
282
283 av_extend (avent, 2);
284
285 if (!sv_type [ent->type])
286 {
287 sv_type [ent->type] = newSViv (ent->type);
288 SvREADONLY_on (sv_type [ent->type]);
289 }
290
291 av_store (avent, 0, namesv);
292 av_store (avent, 1, SvREFCNT_inc (sv_type [ent->type]));
293 av_store (avent, 2, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode));
294
295 av_store (av, i, newRV_noinc ((SV *)avent));
296 }
297 else
298 av_store (av, i, namesv);
299
300 ++ent;
301 }
302 else
303 {
304 SV *name = newSVpv (names, 0);
305 av_store (av, i, name);
306 names += SvCUR (name) + 1;
307 }
308 }
309
310 rv = sv_2mortal (newRV_noinc ((SV *)av));
311 }
312
313 PUSHs (rv);
314
315 if (req->int1 & EIO_READDIR_CUSTOM1)
316 XPUSHs (sv_2mortal (newSViv (req->int1 & ~(EIO_READDIR_CUSTOM1 | EIO_READDIR_CUSTOM2))));
317 }
318 break;
319
320 case EIO_OPEN:
321 {
322 /* convert fd to fh */
323 SV *fh = &PL_sv_undef;
324
325 if (req->result >= 0)
326 {
327 GV *gv = (GV *)sv_newmortal ();
328 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
329 char sym [64];
330 int symlen;
331
332 symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result);
333 gv_init (gv, stash, sym, symlen, 0);
334
335 symlen = snprintf (
336 sym,
337 sizeof (sym),
338 "%s&=%d",
339 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<",
340 (int)req->result
341 );
342
343 if (do_open (gv, sym, symlen, 0, 0, 0, 0))
344 fh = (SV *)gv;
345 }
346
347 PUSHs (fh);
348 }
349 break;
350
351 case EIO_GROUP:
352 req->int1 = 2; /* mark group as finished */
353
354 if (req->sv1)
355 {
356 int i;
357 AV *av = (AV *)req->sv1;
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 EIO_NOP:
366 case EIO_BUSY:
367 break;
368
369 case EIO_READLINK:
370 if (req->result > 0)
371 PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result)));
372 break;
373
374 case EIO_STAT:
375 case EIO_LSTAT:
376 case EIO_FSTAT:
377 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
378 PL_laststatval = req->result;
379 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
380 PUSHs (sv_2mortal (newSViv (req->result)));
381 break;
382
383 case EIO_READ:
384 {
385 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
386 *SvEND (req->sv2) = 0;
387 SvPOK_only (req->sv2);
388 SvSETMAGIC (req->sv2);
389 PUSHs (sv_2mortal (newSViv (req->result)));
390 }
391 break;
392
393 case EIO_DUP2:
394 if (req->result > 0)
395 req->result = 0;
396 /* FALLTHROUGH */
397
398 default:
399 PUSHs (sv_2mortal (newSViv (req->result)));
400 break;
401 }
402
403 errno = req->errorno;
404
405 PUTBACK;
406 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
407 SPAGAIN;
408
409 FREETMPS;
410 LEAVE;
411
412 PUTBACK;
413 }
414
415 return !!SvTRUE (ERRSV);
416}
417
418static void req_destroy (aio_req req)
419{
88 if (req->fh) 420 if (req->self)
421 {
422 sv_unmagic (req->self, PERL_MAGIC_ext);
89 SvREFCNT_dec (req->fh); 423 SvREFCNT_dec (req->self);
424 }
90 425
91 if (req->fh2) 426 SvREFCNT_dec (req->sv1);
92 SvREFCNT_dec (req->fh2); 427 SvREFCNT_dec (req->sv2);
93
94 if (req->statdata)
95 Safefree (req->statdata);
96
97 if (req->callback)
98 SvREFCNT_dec (req->callback); 428 SvREFCNT_dec (req->callback);
99 429
100 Safefree (req); 430 Safefree (req);
101} 431}
102 432
433static void req_cancel_subs (aio_req grp)
434{
435 aio_req sub;
436
437 if (grp->type != EIO_GROUP)
438 return;
439
440 SvREFCNT_dec (grp->sv2);
441 grp->sv2 = 0;
442
443 eio_grp_cancel (grp);
444}
445
446#ifdef USE_SOCKETS_AS_HANDLES
447# define TO_SOCKET(x) (win32_get_osfhandle (x))
448#else
449# define TO_SOCKET(x) (x)
450#endif
451
103static void 452static void
104poll_wait () 453create_respipe (void)
105{ 454{
106 if (nreqs && !ress) 455 int old_readfd = respipe [0];
456
457 if (respipe [1] >= 0)
458 respipe_close (TO_SOCKET (respipe [1]));
459
460#ifdef _WIN32
461 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
462#else
463 if (pipe (respipe))
464#endif
465 croak ("unable to initialize result pipe");
466
467 if (old_readfd >= 0)
107 { 468 {
108 fd_set rfd; 469 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
109 FD_ZERO(&rfd); 470 croak ("unable to initialize result pipe(2)");
110 FD_SET(respipe [0], &rfd);
111 471
112 select (respipe [0] + 1, &rfd, 0, 0, 0); 472 respipe_close (respipe [0]);
473 respipe [0] = old_readfd;
113 } 474 }
114}
115 475
116static int 476#ifdef _WIN32
117poll_cb () 477 int arg = 1;
118{ 478 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
119 dSP; 479 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
120 int count = 0; 480#else
121 int do_croak = 0; 481 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
122 aio_req req; 482 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
483#endif
484 croak ("unable to initialize result pipe(3)");
123 485
486 respipe_osf [0] = TO_SOCKET (respipe [0]);
487 respipe_osf [1] = TO_SOCKET (respipe [1]);
488}
489
490static void poll_wait (void)
491{
492 fd_set rfd;
493
494 while (eio_nreqs ())
495 {
496 int size;
497
498 X_LOCK (reslock);
499 size = res_queue.size;
500 X_UNLOCK (reslock);
501
502 if (size)
503 return;
504
505 etp_maybe_start_thread ();
506
507 FD_ZERO (&rfd);
508 FD_SET (respipe [0], &rfd);
509
510 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
511 }
512}
513
514static int poll_cb (void)
515{
124 for (;;) 516 for (;;)
125 { 517 {
126 pthread_mutex_lock (&reslock); 518 int res = eio_poll ();
127 req = ress;
128 519
129 if (req) 520 if (res > 0)
130 {
131 ress = req->next;
132
133 if (!ress)
134 {
135 /* read any signals sent by the worker threads */
136 char buf [32];
137 while (read (respipe [0], buf, 32) == 32)
138 ;
139
140 rese = 0;
141 }
142 }
143
144 pthread_mutex_unlock (&reslock);
145
146 if (!req)
147 break;
148
149 nreqs--;
150
151 if (req->type == REQ_QUIT)
152 started--;
153 else
154 {
155 int errorno = errno;
156 errno = req->errorno;
157
158 if (req->type == REQ_READ)
159 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
160
161 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
162 SvREADONLY_off (req->data);
163
164 if (req->statdata)
165 {
166 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
167 PL_laststatval = req->result;
168 PL_statcache = *(req->statdata);
169 }
170
171 ENTER;
172 PUSHMARK (SP);
173 XPUSHs (sv_2mortal (newSViv (req->result)));
174
175 if (req->type == REQ_OPEN)
176 {
177 /* convert fd to fh */
178 SV *fh;
179
180 PUTBACK;
181 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
182 SPAGAIN;
183
184 fh = SvREFCNT_inc (POPs);
185
186 PUSHMARK (SP);
187 XPUSHs (sv_2mortal (fh));
188 }
189
190 if (SvOK (req->callback))
191 {
192 PUTBACK;
193 call_sv (req->callback, G_VOID | G_EVAL);
194 SPAGAIN;
195
196 if (SvTRUE (ERRSV))
197 {
198 free_req (req);
199 croak (0); 521 croak (0);
200 }
201 }
202 522
203 LEAVE; 523 if (!max_outstanding || max_outstanding > eio_nreqs ())
524 return res;
204 525
205 errno = errorno; 526 poll_wait ();
206 count++;
207 }
208
209 free_req (req);
210 } 527 }
211
212 return count;
213}
214
215static void *aio_proc(void *arg);
216
217static void
218start_thread (void)
219{
220 sigset_t fullsigset, oldsigset;
221 pthread_t tid;
222 pthread_attr_t attr;
223
224 pthread_attr_init (&attr);
225 pthread_attr_setstacksize (&attr, STACKSIZE);
226 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
227
228 sigfillset (&fullsigset);
229 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
230
231 if (pthread_create (&tid, &attr, aio_proc, 0) == 0)
232 started++;
233
234 sigprocmask (SIG_SETMASK, &oldsigset, 0);
235}
236
237static void
238send_req (aio_req req)
239{
240 while (started < wanted && nreqs >= started)
241 start_thread ();
242
243 nreqs++;
244
245 pthread_mutex_lock (&reqlock);
246
247 req->next = 0;
248
249 if (reqe)
250 {
251 reqe->next = req;
252 reqe = req;
253 }
254 else
255 reqe = reqs = req;
256
257 pthread_cond_signal (&reqwait);
258 pthread_mutex_unlock (&reqlock);
259
260 if (nreqs > max_outstanding)
261 for (;;)
262 {
263 poll_cb ();
264
265 if (nreqs <= max_outstanding)
266 break;
267
268 poll_wait ();
269 }
270}
271
272static void
273end_thread (void)
274{
275 aio_req req;
276 Newz (0, req, 1, aio_cb);
277 req->type = REQ_QUIT;
278
279 send_req (req);
280}
281
282static void min_parallel (int nthreads)
283{
284 if (wanted < nthreads)
285 wanted = nthreads;
286}
287
288static void max_parallel (int nthreads)
289{
290 int cur = started;
291
292 if (wanted > nthreads)
293 wanted = nthreads;
294
295 while (cur > wanted)
296 {
297 end_thread ();
298 cur--;
299 }
300
301 while (started > wanted)
302 {
303 poll_wait ();
304 poll_cb ();
305 }
306}
307
308static void create_pipe ()
309{
310 if (pipe (respipe))
311 croak ("unable to initialize result pipe");
312
313 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
314 croak ("cannot set result pipe to nonblocking mode");
315
316 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
317 croak ("cannot set result pipe to nonblocking mode");
318}
319
320static void atfork_prepare (void)
321{
322 pthread_mutex_lock (&reqlock);
323 pthread_mutex_lock (&reslock);
324}
325
326static void atfork_parent (void)
327{
328 pthread_mutex_unlock (&reslock);
329 pthread_mutex_unlock (&reqlock);
330} 528}
331 529
332static void atfork_child (void) 530static void atfork_child (void)
333{ 531{
334 aio_req prv;
335
336 started = 0;
337
338 while (reqs)
339 {
340 prv = reqs;
341 reqs = prv->next;
342 free_req (prv);
343 }
344
345 reqs = reqe = 0;
346
347 while (ress)
348 {
349 prv = ress;
350 ress = prv->next;
351 free_req (prv);
352 }
353
354 ress = rese = 0;
355
356 close (respipe [0]);
357 close (respipe [1]);
358 create_pipe (); 532 create_respipe ();
359
360 atfork_parent ();
361} 533}
362 534
363/*****************************************************************************/
364/* work around various missing functions */
365
366#if !HAVE_PREADWRITE
367# define pread aio_pread
368# define pwrite aio_pwrite
369
370/*
371 * make our pread/pwrite safe against themselves, but not against
372 * normal read/write by using a mutex. slows down execution a lot,
373 * but that's your problem, not mine.
374 */
375static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER;
376
377static ssize_t
378pread (int fd, void *buf, size_t count, off_t offset)
379{
380 ssize_t res;
381 off_t ooffset;
382
383 pthread_mutex_lock (&iolock);
384 ooffset = lseek (fd, 0, SEEK_CUR);
385 lseek (fd, offset, SEEK_SET);
386 res = read (fd, buf, count);
387 lseek (fd, ooffset, SEEK_SET);
388 pthread_mutex_unlock (&iolock);
389
390 return res;
391}
392
393static ssize_t
394pwrite (int fd, void *buf, size_t count, off_t offset)
395{
396 ssize_t res;
397 off_t ooffset;
398
399 pthread_mutex_lock (&iolock);
400 ooffset = lseek (fd, 0, SEEK_CUR);
401 lseek (fd, offset, SEEK_SET);
402 res = write (fd, buf, count);
403 lseek (fd, offset, SEEK_SET);
404 pthread_mutex_unlock (&iolock);
405
406 return res;
407}
408#endif
409
410#if !HAVE_FDATASYNC
411# define fdatasync fsync
412#endif
413
414#if !HAVE_READAHEAD
415# define readahead aio_readahead
416
417static char readahead_buf[4096];
418
419static ssize_t
420readahead (int fd, off_t offset, size_t count)
421{
422 while (count > 0)
423 {
424 size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
425
426 pread (fd, readahead_buf, len, offset);
427 offset += len;
428 count -= len;
429 }
430
431 errno = 0;
432}
433#endif
434
435/* sendfile always needs emulation */
436static ssize_t
437sendfile_ (int ofd, int ifd, off_t offset, size_t count)
438{
439 ssize_t res;
440
441 if (!count)
442 return 0;
443
444#if __linux
445 res = sendfile (ofd, ifd, &offset, count);
446
447#elif __freebsd
448 /*
449 * Of course, the freebsd sendfile is a dire hack with no thoughts
450 * wasted on making it similar to other i/o functions.
451 */
452 {
453 off_t sbytes;
454 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
455
456 if (!res && errno == EAGAIN)
457 /* maybe on others, too, as usual, the manpage leaves you guessing */
458 res = sbytes;
459 }
460
461#elif __hpux
462 res = sendfile (ofd, ifd, offset, count, 0, 0);
463
464#else
465 res = -1;
466 errno = ENOSYS;
467#endif
468
469 if (res < 0 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK))
470 {
471 /* emulate sendfile. this is a major pain in the ass */
472 char *buf = malloc (4096);
473 res = 0;
474
475 for (;;)
476 {
477 ssize_t cnt;
478
479 cnt = pread (ifd, buf, 4096, offset);
480
481 if (cnt <= 0)
482 {
483 if (cnt && !res) res = -1;
484 break;
485 }
486
487 cnt = write (ofd, buf, cnt);
488
489 if (cnt <= 0)
490 {
491 if (cnt && !res) res = -1;
492 break;
493 }
494
495 offset += cnt;
496 res += cnt;
497 }
498
499 {
500 int errorno = errno;
501 free (buf);
502 errno = errorno;
503 }
504 }
505
506 return res;
507}
508
509/*****************************************************************************/
510
511static void * 535static SV *
512aio_proc (void *thr_arg) 536get_cb (SV *cb_sv)
513{ 537{
514 aio_req req; 538 SvGETMAGIC (cb_sv);
515 int type; 539 return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0;
516
517 do
518 {
519 pthread_mutex_lock (&reqlock);
520
521 for (;;)
522 {
523 req = reqs;
524
525 if (reqs)
526 {
527 reqs = reqs->next;
528 if (!reqs) reqe = 0;
529 }
530
531 if (req)
532 break;
533
534 pthread_cond_wait (&reqwait, &reqlock);
535 }
536
537 pthread_mutex_unlock (&reqlock);
538
539 errno = 0; /* strictly unnecessary */
540
541 type = req->type;
542
543 switch (type)
544 {
545 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
546 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
547
548 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
549 case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length); break;
550
551 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
552 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
553 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
554
555 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
556 case REQ_CLOSE: req->result = close (req->fd); break;
557 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
558 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
559 case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
560
561 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
562 case REQ_FSYNC: req->result = fsync (req->fd); break;
563
564 case REQ_QUIT:
565 break;
566
567 default:
568 req->result = ENOSYS;
569 break;
570 }
571
572 req->errorno = errno;
573
574 pthread_mutex_lock (&reslock);
575
576 req->next = 0;
577
578 if (rese)
579 {
580 rese->next = req;
581 rese = req;
582 }
583 else
584 {
585 rese = ress = req;
586
587 /* write a dummy byte to the pipe so fh becomes ready */
588 write (respipe [1], &respipe, 1);
589 }
590
591 pthread_mutex_unlock (&reslock);
592 }
593 while (type != REQ_QUIT);
594
595 return 0;
596} 540}
597 541
598#define dREQ \ 542#define dREQ \
543 SV *cb_cv; \
599 aio_req req; \ 544 aio_req req; \
545 int req_pri = next_pri; \
546 next_pri = EIO_PRI_DEFAULT; \
600 \ 547 \
601 if (SvOK (callback) && !SvROK (callback)) \ 548 cb_cv = get_cb (callback); \
602 croak ("clalback must be undef or of reference type"); \
603 \ 549 \
604 Newz (0, req, 1, aio_cb); \ 550 Newz (0, req, 1, eio_req); \
605 if (!req) \ 551 if (!req) \
606 croak ("out of memory during aio_req allocation"); \ 552 croak ("out of memory during eio_req allocation"); \
607 \ 553 \
608 req->callback = newSVsv (callback); 554 req->callback = SvREFCNT_inc (cb_cv); \
609 555 req->pri = req_pri
556
557#define REQ_SEND \
558 PUTBACK; \
559 req_submit (req); \
560 SPAGAIN; \
561 \
562 if (GIMME_V != G_VOID) \
563 XPUSHs (req_sv (req, AIO_REQ_KLASS));
564
610MODULE = IO::AIO PACKAGE = IO::AIO 565MODULE = IO::AIO PACKAGE = IO::AIO
611 566
612PROTOTYPES: ENABLE 567PROTOTYPES: ENABLE
613 568
614BOOT: 569BOOT:
615{ 570{
571 static const struct {
572 const char *name;
573 IV iv;
574 } *civ, const_iv[] = {
575# define const_iv(name, value) { # name, (IV) value },
576# define const_eio(name) { # name, (IV) EIO_ ## name },
577 const_iv (EXDEV , EXDEV)
578 const_iv (ENOSYS , ENOSYS)
579 const_iv (O_RDONLY, O_RDONLY)
580 const_iv (O_WRONLY, O_WRONLY)
581 const_iv (O_CREAT , O_CREAT)
582 const_iv (O_TRUNC , O_TRUNC)
583#ifndef _WIN32
584 const_iv (S_IFIFO , S_IFIFO)
585#endif
586 const_iv (FADV_NORMAL , POSIX_FADV_NORMAL)
587 const_iv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL)
588 const_iv (FADV_RANDOM , POSIX_FADV_RANDOM)
589 const_iv (FADV_NOREUSE , POSIX_FADV_NOREUSE)
590 const_iv (FADV_WILLNEED , POSIX_FADV_WILLNEED)
591 const_iv (FADV_DONTNEED , POSIX_FADV_DONTNEED)
592
593 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
594 const_eio (SYNC_FILE_RANGE_WRITE)
595 const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
596
597 const_eio (READDIR_DENTS)
598 const_eio (READDIR_DIRS_FIRST)
599 const_eio (READDIR_STAT_ORDER)
600 const_eio (READDIR_FOUND_UNKNOWN)
601
602 const_eio (DT_UNKNOWN)
603 const_eio (DT_FIFO)
604 const_eio (DT_CHR)
605 const_eio (DT_DIR)
606 const_eio (DT_BLK)
607 const_eio (DT_REG)
608 const_eio (DT_LNK)
609 const_eio (DT_SOCK)
610 const_eio (DT_WHT)
611 };
612
613 stash = gv_stashpv ("IO::AIO", 1);
614
615 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
616 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
617
616 create_pipe (); 618 create_respipe ();
617 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
618}
619 619
620 if (eio_init (want_poll, done_poll) < 0)
621 croak ("IO::AIO: unable to initialise eio library");
622
623 /* atfork child called in fifo order, so before eio's handler */
624 X_THREAD_ATFORK (0, 0, atfork_child);
625}
626
620void 627void
621min_parallel(nthreads) 628max_poll_reqs (int nreqs)
622 int nthreads
623 PROTOTYPE: $ 629 PROTOTYPE: $
630 CODE:
631 eio_set_max_poll_reqs (nreqs);
624 632
625void 633void
626max_parallel(nthreads) 634max_poll_time (double nseconds)
627 int nthreads
628 PROTOTYPE: $ 635 PROTOTYPE: $
629
630int
631max_outstanding(nreqs)
632 int nreqs
633 PROTOTYPE: $
634 CODE: 636 CODE:
635 RETVAL = max_outstanding; 637 eio_set_max_poll_time (nseconds);
638
639void
640min_parallel (int nthreads)
641 PROTOTYPE: $
642 CODE:
643 eio_set_min_parallel (nthreads);
644
645void
646max_parallel (int nthreads)
647 PROTOTYPE: $
648 CODE:
649 eio_set_max_parallel (nthreads);
650
651void
652max_idle (int nthreads)
653 PROTOTYPE: $
654 CODE:
655 eio_set_max_idle (nthreads);
656
657void
658max_outstanding (int maxreqs)
659 PROTOTYPE: $
660 CODE:
636 max_outstanding = nreqs; 661 max_outstanding = maxreqs;
637 662
638void 663void
639aio_open(pathname,flags,mode,callback=&PL_sv_undef) 664aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
640 SV * pathname
641 int flags
642 int mode
643 SV * callback
644 PROTOTYPE: $$$;$ 665 PROTOTYPE: $$$;$
645 CODE: 666 PPCODE:
646{ 667{
647 dREQ; 668 dREQ;
648 669
649 req->type = REQ_OPEN; 670 req->type = EIO_OPEN;
650 req->data = newSVsv (pathname); 671 req->sv1 = newSVsv (pathname);
651 req->dataptr = SvPVbyte_nolen (req->data); 672 req->ptr1 = SvPVbyte_nolen (req->sv1);
652 req->fd = flags; 673 req->int1 = flags;
653 req->mode = mode; 674 req->int2 = mode;
654 675
655 send_req (req); 676 REQ_SEND;
656} 677}
657 678
658void 679void
659aio_close(fh,callback=&PL_sv_undef) 680aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
660 SV * fh
661 SV * callback
662 PROTOTYPE: $;$ 681 PROTOTYPE: $;$
663 ALIAS: 682 ALIAS:
664 aio_close = REQ_CLOSE
665 aio_fsync = REQ_FSYNC 683 aio_fsync = EIO_FSYNC
666 aio_fdatasync = REQ_FDATASYNC 684 aio_fdatasync = EIO_FDATASYNC
667 CODE: 685 PPCODE:
668{ 686{
687 int fd = s_fileno_croak (fh, 0);
669 dREQ; 688 dREQ;
670 689
671 req->type = ix; 690 req->type = ix;
672 req->fh = newSVsv (fh); 691 req->sv1 = newSVsv (fh);
673 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 692 req->int1 = fd;
674 693
675 send_req (req); 694 REQ_SEND (req);
676} 695}
677 696
678void 697void
698aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
699 PROTOTYPE: $$$$;$
700 PPCODE:
701{
702 int fd = s_fileno_croak (fh, 0);
703 dREQ;
704
705 req->type = EIO_SYNC_FILE_RANGE;
706 req->sv1 = newSVsv (fh);
707 req->int1 = fd;
708 req->offs = offset;
709 req->size = nbytes;
710 req->int2 = flags;
711
712 REQ_SEND (req);
713}
714
715void
716aio_close (SV *fh, SV *callback=&PL_sv_undef)
717 PROTOTYPE: $;$
718 PPCODE:
719{
720 static int close_pipe = -1; /* dummy fd to close fds via dup2 */
721 int fd = s_fileno_croak (fh, 0);
722 dREQ;
723
724 if (close_pipe < 0)
725 {
726 int pipefd [2];
727
728 if (pipe (pipefd) < 0
729 || close (pipefd [1]) < 0
730 || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0)
731 abort (); /*D*/
732
733 close_pipe = pipefd [0];
734 }
735
736 req->type = EIO_DUP2;
737 req->int1 = close_pipe;
738 req->sv2 = newSVsv (fh);
739 req->int2 = fd;
740
741 REQ_SEND (req);
742}
743
744void
679aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef) 745aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
680 SV * fh
681 UV offset
682 UV length
683 SV * data
684 UV dataoffset
685 SV * callback
686 ALIAS: 746 ALIAS:
687 aio_read = REQ_READ 747 aio_read = EIO_READ
688 aio_write = REQ_WRITE 748 aio_write = EIO_WRITE
689 PROTOTYPE: $$$$$;$ 749 PROTOTYPE: $$$$$;$
690 CODE: 750 PPCODE:
691{ 751{
692 aio_req req;
693 STRLEN svlen; 752 STRLEN svlen;
753 int fd = s_fileno_croak (fh, ix == EIO_WRITE);
694 char *svptr = SvPVbyte (data, svlen); 754 char *svptr = SvPVbyte (data, svlen);
695 755 UV len = SvUV (length);
696 SvUPGRADE (data, SVt_PV);
697 SvPOK_on (data);
698 756
699 if (dataoffset < 0) 757 if (dataoffset < 0)
700 dataoffset += svlen; 758 dataoffset += svlen;
701 759
702 if (dataoffset < 0 || dataoffset > svlen) 760 if (dataoffset < 0 || dataoffset > svlen)
703 croak ("data offset outside of string"); 761 croak ("dataoffset outside of data scalar");
704 762
705 if (ix == REQ_WRITE) 763 if (ix == EIO_WRITE)
706 { 764 {
707 /* write: check length and adjust. */ 765 /* write: check length and adjust. */
708 if (length < 0 || length + dataoffset > svlen) 766 if (!SvOK (length) || len + dataoffset > svlen)
709 length = svlen - dataoffset; 767 len = svlen - dataoffset;
710 } 768 }
711 else 769 else
712 { 770 {
713 /* read: grow scalar as necessary */ 771 /* read: check type and grow scalar as necessary */
772 SvUPGRADE (data, SVt_PV);
714 svptr = SvGROW (data, length + dataoffset); 773 svptr = SvGROW (data, len + dataoffset + 1);
715 } 774 }
716
717 if (length < 0)
718 croak ("length must not be negative");
719 775
720 { 776 {
721 dREQ; 777 dREQ;
722 778
723 req->type = ix; 779 req->type = ix;
724 req->fh = newSVsv (fh); 780 req->sv1 = newSVsv (fh);
725 req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) 781 req->int1 = fd;
726 : IoOFP (sv_2io (fh))); 782 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
727 req->offset = offset; 783 req->size = len;
728 req->length = length;
729 req->data = SvREFCNT_inc (data); 784 req->sv2 = SvREFCNT_inc (data);
730 req->dataptr = (char *)svptr + dataoffset; 785 req->ptr2 = (char *)svptr + dataoffset;
786 req->stroffset = dataoffset;
731 787
732 if (!SvREADONLY (data)) 788 if (!SvREADONLY (data))
733 { 789 {
734 SvREADONLY_on (data); 790 SvREADONLY_on (data);
735 req->data2ptr = (void *)data; 791 req->flags |= FLAG_SV2_RO_OFF;
736 } 792 }
737 793
738 send_req (req); 794 REQ_SEND;
739 } 795 }
740} 796}
741 797
742void 798void
799aio_readlink (SV8 *path, SV *callback=&PL_sv_undef)
800 PROTOTYPE: $$;$
801 PPCODE:
802{
803 SV *data;
804 dREQ;
805
806 req->type = EIO_READLINK;
807 req->sv1 = newSVsv (path);
808 req->ptr1 = SvPVbyte_nolen (req->sv1);
809
810 REQ_SEND;
811}
812
813void
743aio_sendfile(out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) 814aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef)
744 SV * out_fh
745 SV * in_fh
746 UV in_offset
747 UV length
748 SV * callback
749 PROTOTYPE: $$$$;$ 815 PROTOTYPE: $$$$;$
750 CODE: 816 PPCODE:
751{ 817{
818 int ifd = s_fileno_croak (in_fh , 0);
819 int ofd = s_fileno_croak (out_fh, 1);
752 dREQ; 820 dREQ;
753 821
754 req->type = REQ_SENDFILE; 822 req->type = EIO_SENDFILE;
755 req->fh = newSVsv (out_fh); 823 req->sv1 = newSVsv (out_fh);
756 req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); 824 req->int1 = ofd;
757 req->fh2 = newSVsv (in_fh); 825 req->sv2 = newSVsv (in_fh);
758 req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 826 req->int2 = ifd;
759 req->offset = in_offset; 827 req->offs = in_offset;
760 req->length = length; 828 req->size = length;
761 829
762 send_req (req); 830 REQ_SEND;
763} 831}
764 832
765void 833void
766aio_readahead(fh,offset,length,callback=&PL_sv_undef) 834aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef)
767 SV * fh
768 UV offset
769 IV length
770 SV * callback
771 PROTOTYPE: $$$;$ 835 PROTOTYPE: $$$;$
772 CODE: 836 PPCODE:
773{ 837{
838 int fd = s_fileno_croak (fh, 0);
774 dREQ; 839 dREQ;
775 840
776 req->type = REQ_READAHEAD; 841 req->type = EIO_READAHEAD;
777 req->fh = newSVsv (fh); 842 req->sv1 = newSVsv (fh);
778 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); 843 req->int1 = fd;
779 req->offset = offset; 844 req->offs = offset;
780 req->length = length; 845 req->size = length;
781 846
782 send_req (req); 847 REQ_SEND;
783} 848}
784 849
785void 850void
786aio_stat(fh_or_path,callback=&PL_sv_undef) 851aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
787 SV * fh_or_path
788 SV * callback
789 ALIAS: 852 ALIAS:
790 aio_stat = REQ_STAT 853 aio_stat = EIO_STAT
791 aio_lstat = REQ_LSTAT 854 aio_lstat = EIO_LSTAT
792 CODE: 855 PPCODE:
793{ 856{
794 dREQ; 857 dREQ;
795 858
796 New (0, req->statdata, 1, Stat_t); 859 req->sv1 = newSVsv (fh_or_path);
797 if (!req->statdata)
798 {
799 free_req (req);
800 croak ("out of memory during aio_req->statdata allocation");
801 }
802 860
803 if (SvPOK (fh_or_path)) 861 if (SvPOK (req->sv1))
804 { 862 {
805 req->type = ix; 863 req->type = ix;
806 req->data = newSVsv (fh_or_path);
807 req->dataptr = SvPVbyte_nolen (req->data); 864 req->ptr1 = SvPVbyte_nolen (req->sv1);
808 } 865 }
809 else 866 else
810 { 867 {
811 req->type = REQ_FSTAT; 868 req->type = EIO_FSTAT;
812 req->fh = newSVsv (fh_or_path);
813 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 869 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
870 }
871
872 REQ_SEND;
873}
874
875void
876aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
877 PPCODE:
878{
879 dREQ;
880
881 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
882 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
883 req->sv1 = newSVsv (fh_or_path);
884
885 if (SvPOK (req->sv1))
814 } 886 {
887 req->type = EIO_UTIME;
888 req->ptr1 = SvPVbyte_nolen (req->sv1);
889 }
890 else
891 {
892 req->type = EIO_FUTIME;
893 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
894 }
815 895
816 send_req (req); 896 REQ_SEND;
817} 897}
818 898
819void 899void
900aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
901 PPCODE:
902{
903 dREQ;
904
905 req->sv1 = newSVsv (fh_or_path);
906 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
907
908 if (SvPOK (req->sv1))
909 {
910 req->type = EIO_TRUNCATE;
911 req->ptr1 = SvPVbyte_nolen (req->sv1);
912 }
913 else
914 {
915 req->type = EIO_FTRUNCATE;
916 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
917 }
918
919 REQ_SEND;
920}
921
922void
923aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
924 ALIAS:
925 aio_chmod = EIO_CHMOD
926 aio_mkdir = EIO_MKDIR
927 PPCODE:
928{
929 dREQ;
930
931 req->int2 = mode;
932 req->sv1 = newSVsv (fh_or_path);
933
934 if (SvPOK (req->sv1))
935 {
936 req->type = ix;
937 req->ptr1 = SvPVbyte_nolen (req->sv1);
938 }
939 else
940 {
941 req->type = EIO_FCHMOD;
942 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
943 }
944
945 REQ_SEND;
946}
947
948void
949aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef)
950 PPCODE:
951{
952 dREQ;
953
954 req->int2 = SvOK (uid) ? SvIV (uid) : -1;
955 req->int3 = SvOK (gid) ? SvIV (gid) : -1;
956 req->sv1 = newSVsv (fh_or_path);
957
958 if (SvPOK (req->sv1))
959 {
960 req->type = EIO_CHOWN;
961 req->ptr1 = SvPVbyte_nolen (req->sv1);
962 }
963 else
964 {
965 req->type = EIO_FCHOWN;
966 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
967 }
968
969 REQ_SEND;
970}
971
972void
973aio_readdirx (SV8 *pathname, IV flags, SV *callback=&PL_sv_undef)
974 PPCODE:
975{
976 dREQ;
977
978 req->type = EIO_READDIR;
979 req->sv1 = newSVsv (pathname);
980 req->ptr1 = SvPVbyte_nolen (req->sv1);
981 req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1;
982
983 if (flags & EIO_READDIR_DENTS)
984 req->int1 |= EIO_READDIR_CUSTOM2;
985
986 REQ_SEND;
987}
988
989void
820aio_unlink(pathname,callback=&PL_sv_undef) 990aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
821 SV * pathname
822 SV * callback
823 ALIAS: 991 ALIAS:
824 aio_unlink = REQ_UNLINK 992 aio_unlink = EIO_UNLINK
825 aio_rmdir = REQ_RMDIR 993 aio_rmdir = EIO_RMDIR
994 aio_readdir = EIO_READDIR
826 CODE: 995 PPCODE:
827{ 996{
828 dREQ; 997 dREQ;
829 998
830 req->type = ix; 999 req->type = ix;
831 req->data = newSVsv (pathname); 1000 req->sv1 = newSVsv (pathname);
832 req->dataptr = SvPVbyte_nolen (req->data); 1001 req->ptr1 = SvPVbyte_nolen (req->sv1);
1002
1003 REQ_SEND;
1004}
1005
1006void
1007aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
1008 ALIAS:
1009 aio_link = EIO_LINK
1010 aio_symlink = EIO_SYMLINK
1011 aio_rename = EIO_RENAME
1012 PPCODE:
1013{
1014 dREQ;
833 1015
834 send_req (req); 1016 req->type = ix;
835} 1017 req->sv1 = newSVsv (oldpath);
836 1018 req->ptr1 = SvPVbyte_nolen (req->sv1);
837void 1019 req->sv2 = newSVsv (newpath);
838aio_symlink(oldpath,newpath,callback=&PL_sv_undef) 1020 req->ptr2 = SvPVbyte_nolen (req->sv2);
839 SV * oldpath
840 SV * newpath
841 SV * callback
842 CODE:
843{
844 dREQ;
845 1021
846 req->type = REQ_SYMLINK; 1022 REQ_SEND;
847 req->fh = newSVsv (oldpath); 1023}
848 req->data2ptr = SvPVbyte_nolen (req->fh); 1024
849 req->data = newSVsv (newpath); 1025void
850 req->dataptr = SvPVbyte_nolen (req->data); 1026aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
1027 PPCODE:
1028{
1029 dREQ;
851 1030
852 send_req (req); 1031 req->type = EIO_MKNOD;
1032 req->sv1 = newSVsv (pathname);
1033 req->ptr1 = SvPVbyte_nolen (req->sv1);
1034 req->int2 = (mode_t)mode;
1035 req->offs = dev;
1036
1037 REQ_SEND;
853} 1038}
854 1039
855void 1040void
1041aio_busy (double delay, SV *callback=&PL_sv_undef)
1042 PPCODE:
1043{
1044 dREQ;
1045
1046 req->type = EIO_BUSY;
1047 req->nv1 = delay < 0. ? 0. : delay;
1048
1049 REQ_SEND;
1050}
1051
1052void
1053aio_group (SV *callback=&PL_sv_undef)
1054 PROTOTYPE: ;$
1055 PPCODE:
1056{
1057 dREQ;
1058
1059 req->type = EIO_GROUP;
1060
1061 req_submit (req);
1062 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1063}
1064
1065void
1066aio_nop (SV *callback=&PL_sv_undef)
1067 ALIAS:
1068 aio_nop = EIO_NOP
1069 aio_sync = EIO_SYNC
1070 PPCODE:
1071{
1072 dREQ;
1073
1074 req->type = ix;
1075
1076 REQ_SEND;
1077}
1078
1079int
1080aioreq_pri (int pri = 0)
1081 PROTOTYPE: ;$
1082 CODE:
1083 RETVAL = next_pri;
1084 if (items > 0)
1085 {
1086 if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN;
1087 if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX;
1088 next_pri = pri;
1089 }
1090 OUTPUT:
1091 RETVAL
1092
1093void
1094aioreq_nice (int nice = 0)
1095 CODE:
1096 nice = next_pri - nice;
1097 if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN;
1098 if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX;
1099 next_pri = nice;
1100
1101void
856flush() 1102flush ()
857 PROTOTYPE: 1103 PROTOTYPE:
858 CODE: 1104 CODE:
859 while (nreqs) 1105 while (eio_nreqs ())
860 { 1106 {
861 poll_wait (); 1107 poll_wait ();
862 poll_cb (); 1108 poll_cb ();
863 } 1109 }
864 1110
865void 1111int
866poll() 1112poll()
867 PROTOTYPE: 1113 PROTOTYPE:
868 CODE: 1114 CODE:
869 if (nreqs)
870 {
871 poll_wait (); 1115 poll_wait ();
872 poll_cb (); 1116 RETVAL = poll_cb ();
873 } 1117 OUTPUT:
1118 RETVAL
874 1119
875int 1120int
876poll_fileno() 1121poll_fileno()
877 PROTOTYPE: 1122 PROTOTYPE:
878 CODE: 1123 CODE:
890 1135
891void 1136void
892poll_wait() 1137poll_wait()
893 PROTOTYPE: 1138 PROTOTYPE:
894 CODE: 1139 CODE:
895 if (nreqs)
896 poll_wait (); 1140 poll_wait ();
897 1141
898int 1142int
899nreqs() 1143nreqs()
900 PROTOTYPE: 1144 PROTOTYPE:
901 CODE: 1145 CODE:
902 RETVAL = nreqs; 1146 RETVAL = eio_nreqs ();
903 OUTPUT: 1147 OUTPUT:
904 RETVAL 1148 RETVAL
905 1149
1150int
1151nready()
1152 PROTOTYPE:
1153 CODE:
1154 RETVAL = eio_nready ();
1155 OUTPUT:
1156 RETVAL
1157
1158int
1159npending()
1160 PROTOTYPE:
1161 CODE:
1162 RETVAL = eio_npending ();
1163 OUTPUT:
1164 RETVAL
1165
1166int
1167nthreads()
1168 PROTOTYPE:
1169 CODE:
1170 RETVAL = eio_nthreads ();
1171 OUTPUT:
1172 RETVAL
1173
1174int
1175fadvise (aio_rfd fh, off_t offset, off_t length, IV advice)
1176 PROTOTYPE: $$$$
1177 CODE:
1178#if _XOPEN_SOURCE >= 600 && !NO_FADVISE
1179 RETVAL = posix_fadvise (fh, offset, length, advice);
1180#else
1181 RETVAL = errno = ENOSYS;
1182#endif
1183 OUTPUT:
1184 RETVAL
1185
1186ssize_t
1187sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1188 PROTOTYPE: $$$$
1189 CODE:
1190 eio_sendfile_sync (ofh, ifh, offset, count);
1191
1192void _on_next_submit (SV *cb)
1193 CODE:
1194 SvREFCNT_dec (on_next_submit);
1195 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1196
1197PROTOTYPES: DISABLE
1198
1199MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1200
1201void
1202cancel (aio_req_ornot req)
1203 CODE:
1204 eio_cancel (req);
1205
1206void
1207cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1208 PPCODE:
1209{
1210 if (GIMME_V != G_VOID)
1211 XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef);
1212
1213 if (items > 1)
1214 {
1215 SV *cb_cv =get_cb (callback);
1216
1217 SvREFCNT_dec (req->callback);
1218 req->callback = SvREFCNT_inc (cb_cv);
1219 }
1220}
1221
1222MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1223
1224void
1225add (aio_req grp, ...)
1226 PPCODE:
1227{
1228 int i;
1229
1230 if (grp->int1 == 2)
1231 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1232
1233 for (i = 1; i < items; ++i )
1234 {
1235 aio_req req;
1236
1237 if (GIMME_V != G_VOID)
1238 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1239
1240 req = SvAIO_REQ (ST (i));
1241
1242 if (req)
1243 eio_grp_add (grp, req);
1244 }
1245}
1246
1247void
1248cancel_subs (aio_req_ornot req)
1249 CODE:
1250 req_cancel_subs (req);
1251
1252void
1253result (aio_req grp, ...)
1254 CODE:
1255{
1256 int i;
1257 AV *av;
1258
1259 grp->errorno = errno;
1260
1261 av = newAV ();
1262 av_extend (av, items - 1);
1263
1264 for (i = 1; i < items; ++i )
1265 av_push (av, newSVsv (ST (i)));
1266
1267 SvREFCNT_dec (grp->sv1);
1268 grp->sv1 = (SV *)av;
1269}
1270
1271void
1272errno (aio_req grp, int errorno = errno)
1273 CODE:
1274 grp->errorno = errorno;
1275
1276void
1277limit (aio_req grp, int limit)
1278 CODE:
1279 eio_grp_limit (grp, limit);
1280
1281void
1282feed (aio_req grp, SV *callback=&PL_sv_undef)
1283 CODE:
1284{
1285 SvREFCNT_dec (grp->sv2);
1286 grp->sv2 = newSVsv (callback);
1287 grp->feed = aio_grp_feed;
1288
1289 if (grp->int2 <= 0)
1290 grp->int2 = 2;
1291
1292 eio_grp_limit (grp, grp->int2);
1293}
1294

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines