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.6 by root, Mon Jul 11 01:03:17 2005 UTC vs.
Revision 1.176 by root, Thu Dec 2 10:04:47 2010 UTC

1#include "libeio/xthread.h"
2
3#include <errno.h>
4
1#include "EXTERN.h" 5#include "EXTERN.h"
2#include "perl.h" 6#include "perl.h"
3#include "XSUB.h" 7#include "XSUB.h"
4 8
9#include "schmorp.h"
10
11#include <stddef.h>
12#include <stdlib.h>
13#include <errno.h>
5#include <sys/types.h> 14#include <sys/types.h>
6#include <sys/stat.h> 15#include <sys/stat.h>
16#include <sys/statvfs.h>
7#include <unistd.h> 17#include <limits.h>
8#include <fcntl.h> 18#include <fcntl.h>
9#include <signal.h>
10#include <sched.h> 19#include <sched.h>
11#include <endian.h>
12 20
13#include <pthread.h> 21#if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES
14#include <sys/syscall.h> 22# include <sys/mman.h>
23#endif
15 24
16typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 25/* perl namespace pollution */
17typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ 26#undef VERSION
18typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
19 27
20#if __ia64 28#ifdef _WIN32
21# define STACKSIZE 65536 29
30# define EIO_STRUCT_DIRENT Direntry_t
31# undef malloc
32# undef free
33
34// perl overrides all those nice win32 functions
35# undef open
36# undef read
37# undef write
38# undef send
39# undef recv
40# undef stat
41# undef fstat
42# define lstat stat
43# undef truncate
44# undef ftruncate
45# undef open
46# undef close
47# undef unlink
48# undef rmdir
49# undef rename
50# undef lseek
51
52# define chown(a,b,c) (errno = ENOSYS, -1)
53# define fchown(a,b,c) (errno = ENOSYS, -1)
54# define fchmod(a,b) (errno = ENOSYS, -1)
55# define symlink(a,b) (errno = ENOSYS, -1)
56# define readlink(a,b,c) (errno = ENOSYS, -1)
57# define mknod(a,b,c) (errno = ENOSYS, -1)
58# define truncate(a,b) (errno = ENOSYS, -1)
59# define ftruncate(fd,o) chsize ((fd), (o))
60# define fsync(fd) _commit (fd)
61# define opendir(fd) (errno = ENOSYS, 0)
62# define readdir(fd) (errno = ENOSYS, -1)
63# define closedir(fd) (errno = ENOSYS, -1)
64# define mkdir(a,b) mkdir (a)
65
22#else 66#else
23# define STACKSIZE 4096 67
68# include <sys/time.h>
69# include <sys/select.h>
70# include <unistd.h>
71# include <utime.h>
72# include <signal.h>
73# define EIO_STRUCT_DIRENT struct dirent
74
75#endif
76
77/* perl stupidly overrides readdir and maybe others */
78/* with thread-unsafe versions, imagine that :( */
79#undef readdir
80#undef opendir
81#undef closedir
82
83#define EIO_STRUCT_STAT Stat_t
84
85/* use NV for 32 bit perls as it allows larger offsets */
86#if IVSIZE >= 8
87# define VAL64 IV
88# define SvVAL64 SvIV
89# define newSVval64 newSViv
90#else
91# define VAL64 NV
92# define SvVAL64 SvNV
93# define newSVval64 newSVnv
94#endif
95
96/*****************************************************************************/
97
98#if __GNUC__ >= 3
99# define expect(expr,value) __builtin_expect ((expr),(value))
100#else
101# define expect(expr,value) (expr)
102#endif
103
104#define expect_false(expr) expect ((expr) != 0, 0)
105#define expect_true(expr) expect ((expr) != 0, 1)
106
107/*****************************************************************************/
108
109typedef SV SV8; /* byte-sv, used for argument-checking */
110typedef int aio_rfd; /* read file desriptor */
111typedef int aio_wfd; /* write file descriptor */
112
113static HV *aio_stash, *aio_req_stash, *aio_grp_stash;
114
115#define EIO_REQ_MEMBERS \
116 SV *callback; \
117 SV *sv1, *sv2; \
118 STRLEN stroffset; \
119 SV *self;
120
121#define EIO_NO_WRAPPERS 1
122
123#include "libeio/config.h"
124#include "libeio/eio.h"
125
126#ifndef POSIX_FADV_NORMAL
127# define POSIX_FADV_NORMAL 0
128#endif
129#ifndef POSIX_FADV_SEQUENTIAL
130# define POSIX_FADV_SEQUENTIAL 0
131#endif
132#ifndef POSIX_FADV_RANDOM
133# define POSIX_FADV_RANDOM 0
134#endif
135#ifndef POSIX_FADV_NOREUSE
136# define POSIX_FADV_NOREUSE 0
137#endif
138#ifndef POSIX_FADV_WILLNEED
139# define POSIX_FADV_WILLNEED 0
140#endif
141#ifndef POSIX_FADV_DONTNEED
142# define POSIX_FADV_DONTNEED 0
143#endif
144
145#if !HAVE_POSIX_FADVISE
146# define posix_fadvise(a,b,c,d) errno = ENOSYS /* also return ENOSYS */
147#endif
148
149#ifndef POSIX_MADV_NORMAL
150# define POSIX_MADV_NORMAL 0
151#endif
152#ifndef POSIX_MADV_SEQUENTIAL
153# define POSIX_MADV_SEQUENTIAL 0
154#endif
155#ifndef POSIX_MADV_RANDOM
156# define POSIX_MADV_RANDOM 0
157#endif
158#ifndef POSIX_MADV_WILLNEED
159# define POSIX_MADV_WILLNEED 0
160#endif
161#ifndef POSIX_MADV_DONTNEED
162# define POSIX_MADV_DONTNEED 0
163#endif
164
165#if !HAVE_POSIX_MADVISE
166# define posix_madvise(a,b,c) errno = ENOSYS /* also return ENOSYS */
167#endif
168
169#ifndef PROT_NONE
170# define PROT_NONE 0
171#endif
172#ifndef PROT_READ
173# define PROT_READ 0
174#endif
175#ifndef PROT_WRITE
176# define PROT_READ 0
177#endif
178#ifndef PROT_EXEC
179# define PROT_EXEC 0
180#endif
181
182#ifndef ST_NODEV
183# define ST_NODEV 0
184#endif
185#ifndef ST_NOEXEC
186# define ST_NOEXEC 0
187#endif
188#ifndef ST_SYNCHRONOUS
189# define ST_SYNCHRONOUS 0
190#endif
191#ifndef ST_MANDLOCK
192# define ST_MANDLOCK 0
193#endif
194#ifndef ST_WRITE
195# define ST_WRITE 0
196#endif
197#ifndef ST_APPEND
198# define ST_APPEND 0
199#endif
200#ifndef ST_IMMUTABLE
201# define ST_IMMUTABLE 0
202#endif
203#ifndef ST_NOATIME
204# define ST_NOATIME 0
205#endif
206#ifndef ST_NODIRATIME
207# define ST_NODIRATIME 0
208#endif
209#ifndef ST_RELATIME
210# define ST_RELATIME 0
211#endif
212
213#ifndef MAP_ANONYMOUS
214# ifdef MAP_ANON
215# define MAP_ANONYMOUS MAP_ANON
216# else
217# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */
24#endif 218# endif
219#endif
220#ifndef MAP_HUGETLB
221# define MAP_HUGETLB 0
222#endif
223#ifndef MAP_LOCKED
224# define MAP_LOCKED 0
225#endif
226#ifndef MAP_NORESERVE
227# define MAP_NORESERVE 0
228#endif
229#ifndef MAP_POPULATE
230# define MAP_POPULATE 0
231#endif
232#ifndef MAP_NONBLOCK
233# define MAP_NONBLOCK 0
234#endif
235
236#ifndef PAGESIZE
237# define PAGESIZE sysconf (_SC_PAGESIZE)
238#endif
239
240static int req_invoke (eio_req *req);
241#define EIO_FINISH(req) req_invoke (req)
242static void req_destroy (eio_req *grp);
243#define EIO_DESTROY(req) req_destroy (req)
25 244
26enum { 245enum {
27 REQ_QUIT, 246 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
28 REQ_OPEN, REQ_CLOSE,
29 REQ_READ, REQ_WRITE, REQ_READAHEAD,
30 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK,
31 REQ_FSYNC, REQ_FDATASYNC,
32}; 247};
33 248
34typedef struct aio_cb { 249#include "libeio/eio.c"
35 struct aio_cb *volatile next;
36 250
37 int type;
38
39 int fd;
40 off_t offset;
41 size_t length;
42 ssize_t result;
43 mode_t mode; /* open */
44 int errorno;
45 SV *data, *callback;
46 void *dataptr;
47 STRLEN dataoffset;
48
49 Stat_t *statdata;
50} aio_cb;
51
52typedef aio_cb *aio_req; 251typedef eio_req *aio_req;
252typedef eio_req *aio_req_ornot;
53 253
54static int started; 254static SV *on_next_submit;
55static int nreqs; 255static int next_pri = EIO_PRI_DEFAULT;
56static int max_outstanding = 1<<30; 256static int max_outstanding;
57static int respipe [2];
58 257
59static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 258static s_epipe respipe;
60static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
61static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
62 259
63static volatile aio_req reqs, reqe; /* queue start, queue end */ 260static void req_destroy (aio_req req);
64static volatile aio_req ress, rese; /* queue start, queue end */ 261static void req_cancel (aio_req req);
65 262
66static void 263static void want_poll (void)
67poll_wait ()
68{ 264{
69 if (!nreqs) 265 /* write a dummy byte to the pipe so fh becomes ready */
70 return; 266 s_epipe_signal (&respipe);
71
72 fd_set rfd;
73 FD_ZERO(&rfd);
74 FD_SET(respipe [0], &rfd);
75
76 select (respipe [0] + 1, &rfd, 0, 0, 0);
77} 267}
78 268
79static int 269static void done_poll (void)
80poll_cb ()
81{ 270{
82 dSP;
83 int count = 0;
84 aio_req req;
85
86 {
87 /* read and signals sent by the worker threads */ 271 /* read any signals sent by the worker threads */
88 char buf [32]; 272 s_epipe_drain (&respipe);
89 while (read (respipe [0], buf, 32) > 0) 273}
90 ;
91 }
92 274
93 for (;;) 275/* must be called at most once */
276static SV *req_sv (aio_req req, HV *stash)
277{
278 if (!req->self)
94 { 279 {
95 pthread_mutex_lock (&reslock); 280 req->self = (SV *)newHV ();
281 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
282 }
96 283
97 req = ress; 284 return sv_2mortal (sv_bless (newRV_inc (req->self), stash));
285}
98 286
99 if (ress) 287static aio_req SvAIO_REQ (SV *sv)
288{
289 MAGIC *mg;
290
291 if (!SvROK (sv)
292 || (SvSTASH (SvRV (sv)) != aio_grp_stash
293 && SvSTASH (SvRV (sv)) != aio_req_stash
294 && !sv_derived_from (sv, "IO::AIO::REQ")))
295 croak ("object of class IO::AIO::REQ expected");
296
297 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
298
299 return mg ? (aio_req)mg->mg_ptr : 0;
300}
301
302static void aio_grp_feed (aio_req grp)
303{
304 if (grp->sv2 && SvOK (grp->sv2))
305 {
306 dSP;
307
308 ENTER;
309 SAVETMPS;
310 PUSHMARK (SP);
311 XPUSHs (req_sv (grp, aio_grp_stash));
312 PUTBACK;
313 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
314 SPAGAIN;
315 FREETMPS;
316 LEAVE;
317 }
318}
319
320static void req_submit (eio_req *req)
321{
322 eio_submit (req);
323
324 if (expect_false (on_next_submit))
325 {
326 dSP;
327 SV *cb = sv_2mortal (on_next_submit);
328
329 on_next_submit = 0;
330
331 PUSHMARK (SP);
332 PUTBACK;
333 call_sv (cb, G_DISCARD | G_EVAL);
334 }
335}
336
337static int req_invoke (eio_req *req)
338{
339 if (req->flags & FLAG_SV2_RO_OFF)
340 SvREADONLY_off (req->sv2);
341
342 if (!EIO_CANCELLED (req) && req->callback)
343 {
344 dSP;
345 static SV *sv_result_cache; /* caches the result integer SV */
346 SV *sv_result;
347
348 ENTER;
349 SAVETMPS;
350 PUSHMARK (SP);
351 EXTEND (SP, 1);
352
353 /* do not recreate the result IV from scratch each time */
354 if (expect_true (sv_result_cache))
100 { 355 {
101 ress = ress->next; 356 sv_result = sv_result_cache; sv_result_cache = 0;
102 if (!ress) rese = 0; 357 SvIV_set (sv_result, req->result);
103 }
104
105 pthread_mutex_unlock (&reslock);
106
107 if (!req)
108 break;
109
110 nreqs--;
111
112 if (req->type == REQ_QUIT)
113 started--;
114 else
115 {
116 int errorno = errno;
117 errno = req->errorno;
118
119 if (req->type == REQ_READ)
120 SvCUR_set (req->data, req->dataoffset
121 + req->result > 0 ? req->result : 0);
122
123 if (req->data)
124 SvREFCNT_dec (req->data);
125
126 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
127 {
128 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
129 PL_laststatval = req->result;
130 PL_statcache = *(req->statdata);
131
132 Safefree (req->statdata);
133 }
134
135 PUSHMARK (SP);
136 XPUSHs (sv_2mortal (newSViv (req->result)));
137
138 if (req->type == REQ_OPEN)
139 {
140 /* convert fd to fh */
141 SV *fh;
142
143 PUTBACK;
144 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
145 SPAGAIN;
146
147 fh = POPs;
148
149 PUSHMARK (SP);
150 XPUSHs (fh);
151 }
152
153 PUTBACK;
154 call_sv (req->callback, G_VOID | G_EVAL);
155 SPAGAIN;
156
157 if (req->callback)
158 SvREFCNT_dec (req->callback);
159
160 errno = errorno;
161 count++;
162 }
163
164 Safefree (req);
165 }
166
167 return count;
168}
169
170static void *aio_proc(void *arg);
171
172static void
173start_thread (void)
174{
175 sigset_t fullsigset, oldsigset;
176 pthread_t tid;
177 pthread_attr_t attr;
178
179 pthread_attr_init (&attr);
180 pthread_attr_setstacksize (&attr, STACKSIZE);
181 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
182
183 sigfillset (&fullsigset);
184 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
185
186 if (pthread_create (&tid, &attr, aio_proc, 0) == 0)
187 started++;
188
189 sigprocmask (SIG_SETMASK, &oldsigset, 0);
190}
191
192static void
193send_req (aio_req req)
194{
195 nreqs++;
196
197 pthread_mutex_lock (&reqlock);
198
199 req->next = 0;
200
201 if (reqe)
202 {
203 reqe->next = req;
204 reqe = req;
205 }
206 else
207 reqe = reqs = req;
208
209 pthread_cond_signal (&reqwait);
210 pthread_mutex_unlock (&reqlock);
211
212 while (nreqs > max_outstanding)
213 {
214 poll_wait ();
215 poll_cb ();
216 }
217}
218
219static void
220end_thread (void)
221{
222 aio_req req;
223 New (0, req, 1, aio_cb);
224 req->type = REQ_QUIT;
225
226 send_req (req);
227}
228
229static void
230read_write (int dowrite, int fd, off_t offset, size_t length,
231 SV *data, STRLEN dataoffset, SV *callback)
232{
233 aio_req req;
234 STRLEN svlen;
235 char *svptr = SvPV (data, svlen);
236
237 SvUPGRADE (data, SVt_PV);
238 SvPOK_on (data);
239
240 if (dataoffset < 0)
241 dataoffset += svlen;
242
243 if (dataoffset < 0 || dataoffset > svlen)
244 croak ("data offset outside of string");
245
246 if (dowrite)
247 {
248 /* write: check length and adjust. */
249 if (length < 0 || length + dataoffset > svlen)
250 length = svlen - dataoffset;
251 }
252 else
253 {
254 /* read: grow scalar as necessary */
255 svptr = SvGROW (data, length + dataoffset);
256 }
257
258 if (length < 0)
259 croak ("length must not be negative");
260
261 Newz (0, req, 1, aio_cb);
262
263 if (!req)
264 croak ("out of memory during aio_req allocation");
265
266 req->type = dowrite ? REQ_WRITE : REQ_READ;
267 req->fd = fd;
268 req->offset = offset;
269 req->length = length;
270 req->data = SvREFCNT_inc (data);
271 req->dataptr = (char *)svptr + dataoffset;
272 req->callback = SvREFCNT_inc (callback);
273
274 send_req (req);
275}
276
277static void *
278aio_proc (void *thr_arg)
279{
280 aio_req req;
281 int type;
282
283 do
284 {
285 pthread_mutex_lock (&reqlock);
286
287 for (;;)
288 {
289 req = reqs;
290
291 if (reqs)
292 {
293 reqs = reqs->next;
294 if (!reqs) reqe = 0;
295 }
296
297 if (req)
298 break;
299
300 pthread_cond_wait (&reqwait, &reqlock);
301 }
302
303 pthread_mutex_unlock (&reqlock);
304
305 errno = 0; /* strictly unnecessary */
306
307 type = req->type;
308
309 switch (type)
310 {
311 case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break;
312 case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break;
313#if SYS_readahead
314 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
315#else
316 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
317#endif
318
319 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
320 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
321 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
322
323 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
324 case REQ_CLOSE: req->result = close (req->fd); break;
325 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
326
327 case REQ_FSYNC: req->result = fsync (req->fd); break;
328 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
329
330 case REQ_QUIT:
331 break;
332
333 default:
334 req->result = ENOSYS;
335 break;
336 }
337
338 req->errorno = errno;
339
340 pthread_mutex_lock (&reslock);
341
342 req->next = 0;
343
344 if (rese)
345 {
346 rese->next = req;
347 rese = req;
348 } 358 }
349 else 359 else
350 { 360 {
351 rese = ress = req; 361 sv_result = newSViv (req->result);
352 362 SvREADONLY_on (sv_result);
353 /* write a dummy byte to the pipe so fh becomes ready */
354 write (respipe [1], &respipe, 1);
355 } 363 }
356 364
357 pthread_mutex_unlock (&reslock); 365 switch (req->type)
366 {
367 case EIO_READDIR:
368 {
369 SV *rv = &PL_sv_undef;
370
371 if (req->result >= 0)
372 {
373 int i;
374 char *names = (char *)req->ptr2;
375 eio_dirent *ent = (eio_dirent *)req->ptr1; /* might be 0 */
376 AV *av = newAV ();
377
378 av_extend (av, req->result - 1);
379
380 for (i = 0; i < req->result; ++i)
381 {
382 if (req->int1 & EIO_READDIR_DENTS)
383 {
384 SV *namesv = newSVpvn (names + ent->nameofs, ent->namelen);
385
386 if (req->int1 & EIO_READDIR_CUSTOM2)
387 {
388 static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */
389 AV *avent = newAV ();
390
391 av_extend (avent, 2);
392
393 if (!sv_type [ent->type])
394 {
395 sv_type [ent->type] = newSViv (ent->type);
396 SvREADONLY_on (sv_type [ent->type]);
397 }
398
399 av_store (avent, 0, namesv);
400 av_store (avent, 1, SvREFCNT_inc (sv_type [ent->type]));
401 av_store (avent, 2, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode));
402
403 av_store (av, i, newRV_noinc ((SV *)avent));
404 }
405 else
406 av_store (av, i, namesv);
407
408 ++ent;
409 }
410 else
411 {
412 SV *name = newSVpv (names, 0);
413 av_store (av, i, name);
414 names += SvCUR (name) + 1;
415 }
416 }
417
418 rv = sv_2mortal (newRV_noinc ((SV *)av));
419 }
420
421 PUSHs (rv);
422
423 if (req->int1 & EIO_READDIR_CUSTOM1)
424 XPUSHs (sv_2mortal (newSViv (req->int1 & ~(EIO_READDIR_CUSTOM1 | EIO_READDIR_CUSTOM2))));
425 }
426 break;
427
428 case EIO_OPEN:
429 {
430 /* convert fd to fh */
431 SV *fh = &PL_sv_undef;
432
433 if (req->result >= 0)
434 {
435 GV *gv = (GV *)sv_newmortal ();
436 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
437 char sym [64];
438 int symlen;
439
440 symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result);
441 gv_init (gv, aio_stash, sym, symlen, 0);
442
443 symlen = snprintf (
444 sym,
445 sizeof (sym),
446 "%s&=%d",
447 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<",
448 (int)req->result
449 );
450
451 if (do_open (gv, sym, symlen, 0, 0, 0, 0))
452 fh = (SV *)gv;
453 }
454
455 PUSHs (fh);
456 }
457 break;
458
459 case EIO_STATVFS:
460 case EIO_FSTATVFS:
461 {
462 SV *rv = &PL_sv_undef;
463
464 if (req->result >= 0)
465 {
466 EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req);
467 HV *hv = newHV ();
468
469 rv = sv_2mortal (newRV_noinc ((SV *)hv));
470
471 hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0);
472 hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0);
473 hv_store (hv, "blocks" , sizeof ("blocks" ) - 1, newSVval64 (f->f_blocks ), 0);
474 hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0);
475 hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0);
476 hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0);
477 hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0);
478 hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0);
479 hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0);
480 hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0);
481 hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0);
482 }
483
484 PUSHs (rv);
485 }
486
487 break;
488
489 case EIO_GROUP:
490 req->int1 = 2; /* mark group as finished */
491
492 if (req->sv1)
493 {
494 int i;
495 AV *av = (AV *)req->sv1;
496
497 EXTEND (SP, AvFILL (av) + 1);
498 for (i = 0; i <= AvFILL (av); ++i)
499 PUSHs (*av_fetch (av, i, 0));
500 }
501 break;
502
503 case EIO_NOP:
504 case EIO_BUSY:
505 break;
506
507 case EIO_READLINK:
508 if (req->result > 0)
509 PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result)));
510 break;
511
512 case EIO_STAT:
513 case EIO_LSTAT:
514 case EIO_FSTAT:
515 PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT;
516 PL_laststatval = req->result;
517 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
518 PUSHs (sv_result);
519 break;
520
521 case EIO_READ:
522 {
523 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
524 *SvEND (req->sv2) = 0;
525 SvPOK_only (req->sv2);
526 SvSETMAGIC (req->sv2);
527 PUSHs (sv_result);
528 }
529 break;
530
531 case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), su fudge result value */
532 if (req->result > 0)
533 SvIV_set (sv_result, 0);
534 /* FALLTHROUGH */
535
536 default:
537 PUSHs (sv_result);
538 break;
539 }
540
541 errno = req->errorno;
542
543 PUTBACK;
544 call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD);
545 SPAGAIN;
546
547 if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache))
548 SvREFCNT_dec (sv_result);
549 else
550 sv_result_cache = sv_result;
551
552 FREETMPS;
553 LEAVE;
554
555 PUTBACK;
358 } 556 }
359 while (type != REQ_QUIT); 557
558 return !!SvTRUE (ERRSV);
559}
560
561static void req_destroy (aio_req req)
562{
563 if (req->self)
564 {
565 sv_unmagic (req->self, PERL_MAGIC_ext);
566 SvREFCNT_dec (req->self);
567 }
568
569 SvREFCNT_dec (req->sv1);
570 SvREFCNT_dec (req->sv2);
571 SvREFCNT_dec (req->callback);
572
573 Safefree (req);
574}
575
576static void req_cancel_subs (aio_req grp)
577{
578 aio_req sub;
579
580 if (grp->type != EIO_GROUP)
581 return;
582
583 SvREFCNT_dec (grp->sv2);
584 grp->sv2 = 0;
585
586 eio_grp_cancel (grp);
587}
588
589static void
590create_respipe (void)
591{
592 if (s_epipe_renew (&respipe))
593 croak ("IO::AIO: unable to initialize result pipe");
594}
595
596static void poll_wait (void)
597{
598 while (eio_nreqs ())
599 {
600 int size;
601
602 X_LOCK (reslock);
603 size = res_queue.size;
604 X_UNLOCK (reslock);
605
606 if (size)
607 return;
608
609 etp_maybe_start_thread ();
610
611 s_epipe_wait (&respipe);
612 }
613}
614
615static int poll_cb (void)
616{
617 for (;;)
618 {
619 int res = eio_poll ();
620
621 if (res > 0)
622 croak (0);
623
624 if (!max_outstanding || max_outstanding > eio_nreqs ())
625 return res;
626
627 poll_wait ();
628 }
629}
630
631static void atfork_child (void)
632{
633 create_respipe ();
634}
635
636/*****************************************************************************/
637
638#if !_POSIX_MAPPED_FILES
639# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1)
640# define munmap(addr,length) (errno = ENOSYS, -1)
641# define mprotect(addr,len,prot) (errno = ENOSYS, -1)
642# define PROT_NONE 0
643# define PROT_WRITE 0
644# define MAP_PRIVATE 0
645# define MAP_SHARED 0
646# define MAP_FIXED 0
647#endif
648
649#define MMAP_MAGIC PERL_MAGIC_ext
650
651static int
652mmap_free (pTHX_ SV *sv, MAGIC *mg)
653{
654 int old_errno = errno;
655 munmap (mg->mg_ptr, (size_t)mg->mg_obj);
656 errno = old_errno;
657
658 mg->mg_obj = 0; /* just in case */
659
660 SvREADONLY_off (sv);
661
662 if (SvPVX (sv) != mg->mg_ptr)
663 croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected");
664
665 SvCUR_set (sv, 0);
666 SvPVX (sv) = 0;
667 SvOK_off (sv);
360 668
361 return 0; 669 return 0;
362} 670}
363 671
672static MGVTBL mmap_vtbl = {
673 0, 0, 0, 0, mmap_free
674};
675
676/*****************************************************************************/
677
678static SV *
679get_cb (SV *cb_sv)
680{
681 SvGETMAGIC (cb_sv);
682 return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0;
683}
684
685#define dREQ \
686 SV *cb_cv; \
687 aio_req req; \
688 int req_pri = next_pri; \
689 next_pri = EIO_PRI_DEFAULT; \
690 \
691 cb_cv = get_cb (callback); \
692 \
693 Newz (0, req, 1, eio_req); \
694 if (!req) \
695 croak ("out of memory during eio_req allocation"); \
696 \
697 req->callback = SvREFCNT_inc (cb_cv); \
698 req->pri = req_pri
699
700#define REQ_SEND \
701 PUTBACK; \
702 req_submit (req); \
703 SPAGAIN; \
704 \
705 if (GIMME_V != G_VOID) \
706 XPUSHs (req_sv (req, aio_req_stash));
707
364MODULE = IO::AIO PACKAGE = IO::AIO 708MODULE = IO::AIO PACKAGE = IO::AIO
365 709
710PROTOTYPES: ENABLE
711
366BOOT: 712BOOT:
367{ 713{
368 if (pipe (respipe)) 714 static const struct {
369 croak ("unable to initialize result pipe"); 715 const char *name;
716 IV iv;
717 } *civ, const_iv[] = {
718# define const_niv(name, value) { # name, (IV) value },
719# define const_iv(name) { # name, (IV) name },
720# define const_eio(name) { # name, (IV) EIO_ ## name },
721 const_iv (EXDEV)
722 const_iv (ENOSYS)
723 const_iv (O_RDONLY)
724 const_iv (O_WRONLY)
725 const_iv (O_RDWR)
726 const_iv (O_CREAT)
727 const_iv (O_TRUNC)
728 const_iv (O_EXCL)
729 const_iv (O_APPEND)
730#ifndef _WIN32
731 const_iv (S_IFIFO)
732#endif
733 const_niv (FADV_NORMAL , POSIX_FADV_NORMAL)
734 const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL)
735 const_niv (FADV_RANDOM , POSIX_FADV_RANDOM)
736 const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE)
737 const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED)
738 const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED)
370 739
371 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) 740 const_niv (MADV_NORMAL , POSIX_MADV_NORMAL)
372 croak ("cannot set result pipe to nonblocking mode"); 741 const_niv (MADV_SEQUENTIAL, POSIX_MADV_SEQUENTIAL)
742 const_niv (MADV_RANDOM , POSIX_MADV_RANDOM)
743 const_niv (MADV_WILLNEED , POSIX_MADV_WILLNEED)
744 const_niv (MADV_DONTNEED , POSIX_MADV_DONTNEED)
373 745
374 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) 746 const_iv (ST_RDONLY)
375 croak ("cannot set result pipe to nonblocking mode"); 747 const_iv (ST_NOSUID)
376} 748 const_iv (ST_NODEV)
749 const_iv (ST_NOEXEC)
750 const_iv (ST_SYNCHRONOUS)
751 const_iv (ST_MANDLOCK)
752 const_iv (ST_WRITE)
753 const_iv (ST_APPEND)
754 const_iv (ST_IMMUTABLE)
755 const_iv (ST_NOATIME)
756 const_iv (ST_NODIRATIME)
757 const_iv (ST_RELATIME)
377 758
759 const_iv (PROT_NONE)
760 const_iv (PROT_EXEC)
761 const_iv (PROT_READ)
762 const_iv (PROT_WRITE)
763
764 /*const_iv (MAP_FIXED)*/
765 const_iv (MAP_PRIVATE)
766 const_iv (MAP_SHARED)
767 const_iv (MAP_ANONYMOUS)
768
769 /* linuxish */
770 const_iv (MAP_HUGETLB)
771 const_iv (MAP_LOCKED)
772 const_iv (MAP_NORESERVE)
773 const_iv (MAP_POPULATE)
774 const_iv (MAP_NONBLOCK)
775
776 const_eio (MCL_FUTURE)
777 const_eio (MCL_CURRENT)
778
779 const_eio (MS_ASYNC)
780 const_eio (MS_INVALIDATE)
781 const_eio (MS_SYNC)
782
783 const_eio (MT_MODIFY)
784
785 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
786 const_eio (SYNC_FILE_RANGE_WRITE)
787 const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
788
789 const_eio (READDIR_DENTS)
790 const_eio (READDIR_DIRS_FIRST)
791 const_eio (READDIR_STAT_ORDER)
792 const_eio (READDIR_FOUND_UNKNOWN)
793
794 const_eio (DT_UNKNOWN)
795 const_eio (DT_FIFO)
796 const_eio (DT_CHR)
797 const_eio (DT_DIR)
798 const_eio (DT_BLK)
799 const_eio (DT_REG)
800 const_eio (DT_LNK)
801 const_eio (DT_SOCK)
802 const_eio (DT_WHT)
803 };
804
805 aio_stash = gv_stashpv ("IO::AIO" , 1);
806 aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1);
807 aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1);
808
809 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
810 newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv));
811
812 newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE));
813
814 create_respipe ();
815
816 if (eio_init (want_poll, done_poll) < 0)
817 croak ("IO::AIO: unable to initialise eio library");
818
819 /* atfork child called in fifo order, so before eio's handler */
820 X_THREAD_ATFORK (0, 0, atfork_child);
821}
822
378void 823void
379min_parallel(nthreads) 824max_poll_reqs (int nreqs)
380 int nthreads
381 PROTOTYPE: $ 825 PROTOTYPE: $
382 CODE: 826 CODE:
383 while (nthreads > started) 827 eio_set_max_poll_reqs (nreqs);
384 start_thread ();
385 828
386void 829void
387max_parallel(nthreads) 830max_poll_time (double nseconds)
388 int nthreads
389 PROTOTYPE: $ 831 PROTOTYPE: $
390 CODE: 832 CODE:
833 eio_set_max_poll_time (nseconds);
834
835void
836min_parallel (int nthreads)
837 PROTOTYPE: $
838 CODE:
839 eio_set_min_parallel (nthreads);
840
841void
842max_parallel (int nthreads)
843 PROTOTYPE: $
844 CODE:
845 eio_set_max_parallel (nthreads);
846
847void
848max_idle (int nthreads)
849 PROTOTYPE: $
850 CODE:
851 eio_set_max_idle (nthreads);
852
853void
854max_outstanding (int maxreqs)
855 PROTOTYPE: $
856 CODE:
857 max_outstanding = maxreqs;
858
859void
860aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef)
861 PPCODE:
391{ 862{
392 int cur = started; 863 dREQ;
393 while (cur > nthreads) 864
394 { 865 req->type = EIO_OPEN;
395 end_thread (); 866 req->sv1 = newSVsv (pathname);
396 cur--; 867 req->ptr1 = SvPVbyte_nolen (req->sv1);
868 req->int1 = flags;
869 req->int2 = mode;
870
871 REQ_SEND;
872}
873
874void
875aio_fsync (SV *fh, SV *callback=&PL_sv_undef)
876 ALIAS:
877 aio_fsync = EIO_FSYNC
878 aio_fdatasync = EIO_FDATASYNC
879 PPCODE:
880{
881 int fd = s_fileno_croak (fh, 0);
882 dREQ;
883
884 req->type = ix;
885 req->sv1 = newSVsv (fh);
886 req->int1 = fd;
887
888 REQ_SEND (req);
889}
890
891void
892aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
893 PPCODE:
894{
895 int fd = s_fileno_croak (fh, 0);
896 dREQ;
897
898 req->type = EIO_SYNC_FILE_RANGE;
899 req->sv1 = newSVsv (fh);
900 req->int1 = fd;
901 req->offs = offset;
902 req->size = nbytes;
903 req->int2 = flags;
904
905 REQ_SEND (req);
906}
907
908void
909aio_close (SV *fh, SV *callback=&PL_sv_undef)
910 PPCODE:
911{
912 static int close_pipe = -1; /* dummy fd to close fds via dup2 */
913 int fd = s_fileno_croak (fh, 0);
914 dREQ;
915
916 if (close_pipe < 0)
917 {
918 int pipefd [2];
919
920 if (pipe (pipefd) < 0
921 || close (pipefd [1]) < 0
922 || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0)
923 abort (); /*D*/
924
925 close_pipe = pipefd [0];
397 } 926 }
398 927
399 while (started > nthreads) 928 req->type = EIO_DUP2;
929 req->int1 = close_pipe;
930 req->sv2 = newSVsv (fh);
931 req->int2 = fd;
932
933 REQ_SEND (req);
934}
935
936void
937aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef)
938 ALIAS:
939 aio_read = EIO_READ
940 aio_write = EIO_WRITE
941 PPCODE:
942{
943 STRLEN svlen;
944 int fd = s_fileno_croak (fh, ix == EIO_WRITE);
945 char *svptr = SvPVbyte (data, svlen);
946 UV len = SvUV (length);
947
948 if (dataoffset < 0)
949 dataoffset += svlen;
950
951 if (dataoffset < 0 || dataoffset > svlen)
952 croak ("dataoffset outside of data scalar");
953
954 if (ix == EIO_WRITE)
955 {
956 /* write: check length and adjust. */
957 if (!SvOK (length) || len + dataoffset > svlen)
958 len = svlen - dataoffset;
959 }
960 else
961 {
962 /* read: check type and grow scalar as necessary */
963 SvUPGRADE (data, SVt_PV);
964 svptr = SvGROW (data, len + dataoffset + 1);
965 }
966
967 {
968 dREQ;
969
970 req->type = ix;
971 req->sv1 = newSVsv (fh);
972 req->int1 = fd;
973 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
974 req->size = len;
975 req->sv2 = SvREFCNT_inc (data);
976 req->ptr2 = (char *)svptr + dataoffset;
977 req->stroffset = dataoffset;
978
979 if (!SvREADONLY (data))
980 {
981 SvREADONLY_on (data);
982 req->flags |= FLAG_SV2_RO_OFF;
983 }
984
985 REQ_SEND;
986 }
987}
988
989void
990aio_readlink (SV8 *path, SV *callback=&PL_sv_undef)
991 PPCODE:
992{
993 SV *data;
994 dREQ;
995
996 req->type = EIO_READLINK;
997 req->sv1 = newSVsv (path);
998 req->ptr1 = SvPVbyte_nolen (req->sv1);
999
1000 REQ_SEND;
1001}
1002
1003void
1004aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef)
1005 PPCODE:
1006{
1007 int ifd = s_fileno_croak (in_fh , 0);
1008 int ofd = s_fileno_croak (out_fh, 1);
1009 dREQ;
1010
1011 req->type = EIO_SENDFILE;
1012 req->sv1 = newSVsv (out_fh);
1013 req->int1 = ofd;
1014 req->sv2 = newSVsv (in_fh);
1015 req->int2 = ifd;
1016 req->offs = in_offset;
1017 req->size = length;
1018
1019 REQ_SEND;
1020}
1021
1022void
1023aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef)
1024 PPCODE:
1025{
1026 int fd = s_fileno_croak (fh, 0);
1027 dREQ;
1028
1029 req->type = EIO_READAHEAD;
1030 req->sv1 = newSVsv (fh);
1031 req->int1 = fd;
1032 req->offs = offset;
1033 req->size = length;
1034
1035 REQ_SEND;
1036}
1037
1038void
1039aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
1040 ALIAS:
1041 aio_stat = EIO_STAT
1042 aio_lstat = EIO_LSTAT
1043 aio_statvfs = EIO_STATVFS
1044 PPCODE:
1045{
1046 dREQ;
1047
1048 req->sv1 = newSVsv (fh_or_path);
1049
1050 if (SvPOK (req->sv1))
1051 {
1052 req->type = ix;
1053 req->ptr1 = SvPVbyte_nolen (req->sv1);
1054 }
1055 else
1056 {
1057 req->type = ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT;
1058 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1059 }
1060
1061 REQ_SEND;
1062}
1063
1064void
1065aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef)
1066 PPCODE:
1067{
1068 dREQ;
1069
1070 req->nv1 = SvOK (atime) ? SvNV (atime) : -1.;
1071 req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.;
1072 req->sv1 = newSVsv (fh_or_path);
1073
1074 if (SvPOK (req->sv1))
1075 {
1076 req->type = EIO_UTIME;
1077 req->ptr1 = SvPVbyte_nolen (req->sv1);
1078 }
1079 else
1080 {
1081 req->type = EIO_FUTIME;
1082 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1083 }
1084
1085 REQ_SEND;
1086}
1087
1088void
1089aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef)
1090 PPCODE:
1091{
1092 dREQ;
1093
1094 req->sv1 = newSVsv (fh_or_path);
1095 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
1096
1097 if (SvPOK (req->sv1))
1098 {
1099 req->type = EIO_TRUNCATE;
1100 req->ptr1 = SvPVbyte_nolen (req->sv1);
1101 }
1102 else
1103 {
1104 req->type = EIO_FTRUNCATE;
1105 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1106 }
1107
1108 REQ_SEND;
1109}
1110
1111void
1112aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef)
1113 ALIAS:
1114 aio_chmod = EIO_CHMOD
1115 aio_mkdir = EIO_MKDIR
1116 PPCODE:
1117{
1118 dREQ;
1119
1120 req->int2 = mode;
1121 req->sv1 = newSVsv (fh_or_path);
1122
1123 if (SvPOK (req->sv1))
1124 {
1125 req->type = ix;
1126 req->ptr1 = SvPVbyte_nolen (req->sv1);
1127 }
1128 else
1129 {
1130 req->type = EIO_FCHMOD;
1131 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1132 }
1133
1134 REQ_SEND;
1135}
1136
1137void
1138aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef)
1139 PPCODE:
1140{
1141 dREQ;
1142
1143 req->int2 = SvOK (uid) ? SvIV (uid) : -1;
1144 req->int3 = SvOK (gid) ? SvIV (gid) : -1;
1145 req->sv1 = newSVsv (fh_or_path);
1146
1147 if (SvPOK (req->sv1))
1148 {
1149 req->type = EIO_CHOWN;
1150 req->ptr1 = SvPVbyte_nolen (req->sv1);
1151 }
1152 else
1153 {
1154 req->type = EIO_FCHOWN;
1155 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
1156 }
1157
1158 REQ_SEND;
1159}
1160
1161void
1162aio_readdirx (SV8 *pathname, IV flags, SV *callback=&PL_sv_undef)
1163 PPCODE:
1164{
1165 dREQ;
1166
1167 req->type = EIO_READDIR;
1168 req->sv1 = newSVsv (pathname);
1169 req->ptr1 = SvPVbyte_nolen (req->sv1);
1170 req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1;
1171
1172 if (flags & EIO_READDIR_DENTS)
1173 req->int1 |= EIO_READDIR_CUSTOM2;
1174
1175 REQ_SEND;
1176}
1177
1178void
1179aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
1180 ALIAS:
1181 aio_unlink = EIO_UNLINK
1182 aio_rmdir = EIO_RMDIR
1183 aio_readdir = EIO_READDIR
1184 PPCODE:
1185{
1186 dREQ;
1187
1188 req->type = ix;
1189 req->sv1 = newSVsv (pathname);
1190 req->ptr1 = SvPVbyte_nolen (req->sv1);
1191
1192 REQ_SEND;
1193}
1194
1195void
1196aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef)
1197 ALIAS:
1198 aio_link = EIO_LINK
1199 aio_symlink = EIO_SYMLINK
1200 aio_rename = EIO_RENAME
1201 PPCODE:
1202{
1203 dREQ;
1204
1205 req->type = ix;
1206 req->sv1 = newSVsv (oldpath);
1207 req->ptr1 = SvPVbyte_nolen (req->sv1);
1208 req->sv2 = newSVsv (newpath);
1209 req->ptr2 = SvPVbyte_nolen (req->sv2);
1210
1211 REQ_SEND;
1212}
1213
1214void
1215aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef)
1216 PPCODE:
1217{
1218 dREQ;
1219
1220 req->type = EIO_MKNOD;
1221 req->sv1 = newSVsv (pathname);
1222 req->ptr1 = SvPVbyte_nolen (req->sv1);
1223 req->int2 = (mode_t)mode;
1224 req->offs = dev;
1225
1226 REQ_SEND;
1227}
1228
1229void
1230aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef)
1231 ALIAS:
1232 aio_mtouch = EIO_MTOUCH
1233 aio_msync = EIO_MSYNC
1234 PPCODE:
1235{
1236 STRLEN svlen;
1237 char *svptr = SvPVbyte (data, svlen);
1238 UV len = SvUV (length);
1239
1240 if (offset < 0)
1241 offset += svlen;
1242
1243 if (offset < 0 || offset > svlen)
1244 croak ("offset outside of scalar");
1245
1246 if (!SvOK (length) || len + offset > svlen)
1247 len = svlen - offset;
1248
1249 {
1250 dREQ;
1251
1252 req->type = ix;
1253 req->sv2 = SvREFCNT_inc (data);
1254 req->ptr2 = (char *)svptr + offset;
1255 req->size = len;
1256 req->int1 = flags;
1257
1258 REQ_SEND;
1259 }
1260}
1261
1262void
1263aio_mlock (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, SV *callback=&PL_sv_undef)
1264 PPCODE:
1265{
1266 STRLEN svlen;
1267 char *svptr = SvPVbyte (data, svlen);
1268 UV len = SvUV (length);
1269
1270 if (offset < 0)
1271 offset += svlen;
1272
1273 if (offset < 0 || offset > svlen)
1274 croak ("offset outside of scalar");
1275
1276 if (!SvOK (length) || len + offset > svlen)
1277 len = svlen - offset;
1278
1279 {
1280 dREQ;
1281
1282 req->type = EIO_MLOCK;
1283 req->sv2 = SvREFCNT_inc (data);
1284 req->ptr2 = (char *)svptr + offset;
1285 req->size = len;
1286
1287 REQ_SEND;
1288 }
1289}
1290
1291void
1292aio_mlockall (IV flags, SV *callback=&PL_sv_undef)
1293 PPCODE:
1294{
1295 dREQ;
1296
1297 req->type = EIO_MLOCKALL;
1298 req->int1 = flags;
1299
1300 REQ_SEND;
1301}
1302
1303void
1304aio_busy (double delay, SV *callback=&PL_sv_undef)
1305 PPCODE:
1306{
1307 dREQ;
1308
1309 req->type = EIO_BUSY;
1310 req->nv1 = delay < 0. ? 0. : delay;
1311
1312 REQ_SEND;
1313}
1314
1315void
1316aio_group (SV *callback=&PL_sv_undef)
1317 PPCODE:
1318{
1319 dREQ;
1320
1321 req->type = EIO_GROUP;
1322
1323 req_submit (req);
1324 XPUSHs (req_sv (req, aio_grp_stash));
1325}
1326
1327void
1328aio_nop (SV *callback=&PL_sv_undef)
1329 ALIAS:
1330 aio_nop = EIO_NOP
1331 aio_sync = EIO_SYNC
1332 PPCODE:
1333{
1334 dREQ;
1335
1336 req->type = ix;
1337
1338 REQ_SEND;
1339}
1340
1341int
1342aioreq_pri (int pri = 0)
1343 CODE:
1344 RETVAL = next_pri;
1345 if (items > 0)
1346 {
1347 if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN;
1348 if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX;
1349 next_pri = pri;
1350 }
1351 OUTPUT:
1352 RETVAL
1353
1354void
1355aioreq_nice (int nice = 0)
1356 CODE:
1357 nice = next_pri - nice;
1358 if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN;
1359 if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX;
1360 next_pri = nice;
1361
1362void
1363flush ()
1364 CODE:
1365 while (eio_nreqs ())
400 { 1366 {
401 poll_wait (); 1367 poll_wait ();
402 poll_cb (); 1368 poll_cb ();
403 } 1369 }
404}
405 1370
406int 1371int
407max_outstanding(nreqs) 1372poll ()
408 int nreqs
409 PROTOTYPE: $
410 CODE:
411 RETVAL = max_outstanding;
412 max_outstanding = nreqs;
413
414void
415aio_open(pathname,flags,mode,callback)
416 SV * pathname
417 int flags
418 int mode
419 SV * callback
420 PROTOTYPE: $$$$
421 CODE: 1373 CODE:
422{
423 aio_req req;
424
425 Newz (0, req, 1, aio_cb);
426
427 if (!req)
428 croak ("out of memory during aio_req allocation");
429
430 req->type = REQ_OPEN;
431 req->data = newSVsv (pathname);
432 req->dataptr = SvPV_nolen (req->data);
433 req->fd = flags;
434 req->mode = mode;
435 req->callback = SvREFCNT_inc (callback);
436
437 send_req (req);
438}
439
440void
441aio_close(fh,callback)
442 InputStream fh
443 SV * callback
444 PROTOTYPE: $$
445 ALIAS:
446 aio_close = REQ_CLOSE
447 aio_fsync = REQ_FSYNC
448 aio_fdatasync = REQ_FDATASYNC
449 CODE:
450{
451 aio_req req;
452
453 Newz (0, req, 1, aio_cb);
454
455 if (!req)
456 croak ("out of memory during aio_req allocation");
457
458 req->type = ix;
459 req->fd = PerlIO_fileno (fh);
460 req->callback = SvREFCNT_inc (callback);
461
462 send_req (req);
463}
464
465void
466aio_read(fh,offset,length,data,dataoffset,callback)
467 InputStream fh
468 UV offset
469 IV length
470 SV * data
471 IV dataoffset
472 SV * callback
473 PROTOTYPE: $$$$$$
474 CODE:
475 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
476
477void
478aio_write(fh,offset,length,data,dataoffset,callback)
479 OutputStream fh
480 UV offset
481 IV length
482 SV * data
483 IV dataoffset
484 SV * callback
485 PROTOTYPE: $$$$$$
486 CODE:
487 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
488
489void
490aio_readahead(fh,offset,length,callback)
491 InputStream fh
492 UV offset
493 IV length
494 SV * callback
495 PROTOTYPE: $$$$
496 CODE:
497{
498 aio_req req;
499
500 if (length < 0)
501 croak ("length must not be negative");
502
503 Newz (0, req, 1, aio_cb);
504
505 if (!req)
506 croak ("out of memory during aio_req allocation");
507
508 req->type = REQ_READAHEAD;
509 req->fd = PerlIO_fileno (fh);
510 req->offset = offset;
511 req->length = length;
512 req->callback = SvREFCNT_inc (callback);
513
514 send_req (req);
515}
516
517void
518aio_stat(fh_or_path,callback)
519 SV * fh_or_path
520 SV * callback
521 PROTOTYPE: $$
522 ALIAS:
523 aio_lstat = 1
524 CODE:
525{
526 aio_req req;
527
528 Newz (0, req, 1, aio_cb);
529
530 if (!req)
531 croak ("out of memory during aio_req allocation");
532
533 New (0, req->statdata, 1, Stat_t);
534
535 if (!req->statdata)
536 croak ("out of memory during aio_req->statdata allocation");
537
538 if (SvPOK (fh_or_path))
539 {
540 req->type = ix ? REQ_LSTAT : REQ_STAT;
541 req->data = newSVsv (fh_or_path);
542 req->dataptr = SvPV_nolen (req->data);
543 }
544 else
545 {
546 req->type = REQ_FSTAT;
547 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
548 }
549
550 req->callback = SvREFCNT_inc (callback);
551
552 send_req (req);
553}
554
555void
556aio_unlink(pathname,callback)
557 SV * pathname
558 SV * callback
559 PROTOTYPE: $$
560 CODE:
561{
562 aio_req req;
563
564 Newz (0, req, 1, aio_cb);
565
566 if (!req)
567 croak ("out of memory during aio_req allocation");
568
569 req->type = REQ_UNLINK;
570 req->data = newSVsv (pathname);
571 req->dataptr = SvPV_nolen (req->data);
572 req->callback = SvREFCNT_inc (callback);
573
574 send_req (req);
575}
576
577void
578flush()
579 PROTOTYPE:
580 CODE:
581 while (nreqs)
582 {
583 poll_wait (); 1374 poll_wait ();
584 poll_cb (); 1375 RETVAL = poll_cb ();
585 }
586
587int
588poll_fileno()
589 PROTOTYPE:
590 CODE:
591 RETVAL = respipe [0];
592 OUTPUT: 1376 OUTPUT:
593 RETVAL 1377 RETVAL
594 1378
595int 1379int
1380poll_fileno ()
1381 CODE:
1382 RETVAL = s_epipe_fd (&respipe);
1383 OUTPUT:
1384 RETVAL
1385
1386int
596poll_cb(...) 1387poll_cb (...)
597 PROTOTYPE: 1388 PROTOTYPE:
598 CODE: 1389 CODE:
599 RETVAL = poll_cb (); 1390 RETVAL = poll_cb ();
600 OUTPUT: 1391 OUTPUT:
601 RETVAL 1392 RETVAL
602 1393
603void 1394void
604poll_wait() 1395poll_wait ()
605 PROTOTYPE:
606 CODE: 1396 CODE:
607 if (nreqs)
608 poll_wait (); 1397 poll_wait ();
609 1398
610int 1399int
611nreqs() 1400nreqs ()
612 PROTOTYPE:
613 CODE: 1401 CODE:
614 RETVAL = nreqs; 1402 RETVAL = eio_nreqs ();
615 OUTPUT: 1403 OUTPUT:
616 RETVAL 1404 RETVAL
617 1405
1406int
1407nready ()
1408 CODE:
1409 RETVAL = eio_nready ();
1410 OUTPUT:
1411 RETVAL
1412
1413int
1414npending ()
1415 CODE:
1416 RETVAL = eio_npending ();
1417 OUTPUT:
1418 RETVAL
1419
1420int
1421nthreads ()
1422 CODE:
1423 RETVAL = eio_nthreads ();
1424 OUTPUT:
1425 RETVAL
1426
1427int
1428fadvise (aio_rfd fh, off_t offset, off_t length, IV advice)
1429 CODE:
1430 RETVAL = posix_fadvise (fh, offset, length, advice);
1431 OUTPUT:
1432 RETVAL
1433
1434ssize_t
1435sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1436 CODE:
1437 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count);
1438 OUTPUT:
1439 RETVAL
1440
1441void
1442mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0)
1443 PPCODE:
1444 sv_unmagic (scalar, MMAP_MAGIC);
1445{
1446 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1;
1447 void *addr = (void *)mmap (0, length, prot, flags, fd, offset);
1448 if (addr == (void *)-1)
1449 XSRETURN_NO;
1450
1451 sv_force_normal (scalar);
1452
1453 /* we store the length in mg_obj, as namlen is I32 :/ */
1454 sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0)
1455 ->mg_obj = (SV *)length;
1456
1457 SvUPGRADE (scalar, SVt_PV); /* nop... */
1458
1459 if (!(prot & PROT_WRITE))
1460 SvREADONLY_on (scalar);
1461
1462 if (SvLEN (scalar))
1463 Safefree (SvPVX (scalar));
1464
1465 SvPVX (scalar) = (char *)addr;
1466 SvCUR_set (scalar, length);
1467 SvLEN_set (scalar, 0);
1468 SvPOK_only (scalar);
1469
1470 XSRETURN_YES;
1471}
1472
1473void
1474munmap (SV *scalar)
1475 CODE:
1476 sv_unmagic (scalar, MMAP_MAGIC);
1477
1478int
1479madvise (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef, IV advice_or_prot)
1480 ALIAS:
1481 mprotect = 1
1482 CODE:
1483{
1484 STRLEN svlen;
1485 void *addr = SvPVbyte (scalar, svlen);
1486 size_t len = SvUV (length);
1487
1488 if (offset < 0)
1489 offset += svlen;
1490
1491 if (offset < 0 || offset > svlen)
1492 croak ("offset outside of scalar");
1493
1494 if (!SvOK (length) || len + offset > svlen)
1495 len = svlen - offset;
1496
1497 addr = (void *)(((intptr_t)addr) + offset);
1498 eio_page_align (&addr, &len);
1499
1500 switch (ix)
1501 {
1502 case 0: RETVAL = posix_madvise (addr, len, advice_or_prot); break;
1503 case 1: RETVAL = mprotect (addr, len, advice_or_prot); break;
1504 }
1505}
1506 OUTPUT:
1507 RETVAL
1508
1509int
1510munlock (SV *scalar, off_t offset = 0, SV *length = &PL_sv_undef)
1511 CODE:
1512{
1513 STRLEN svlen;
1514 void *addr = SvPVbyte (scalar, svlen);
1515 size_t len = SvUV (length);
1516
1517 if (offset < 0)
1518 offset += svlen;
1519
1520 if (offset < 0 || offset > svlen)
1521 croak ("offset outside of scalar");
1522
1523 if (!SvOK (length) || len + offset > svlen)
1524 len = svlen - offset;
1525
1526 addr = (void *)(((intptr_t)addr) + offset);
1527 eio_page_align (&addr, &len);
1528#if _POSIX_MEMLOCK
1529 RETVAL = munlock (addr, len);
1530#else
1531 RETVAL = ((errno = ENOSYS), -1);
1532#endif
1533}
1534 OUTPUT:
1535 RETVAL
1536
1537int
1538munlockall ()
1539 CODE:
1540#if _POSIX_MEMLOCK
1541 munlockall ();
1542#else
1543 RETVAL = -1;
1544 errno = ENOSYS;
1545#endif
1546 OUTPUT:
1547 RETVAL
1548
1549void _on_next_submit (SV *cb)
1550 CODE:
1551 SvREFCNT_dec (on_next_submit);
1552 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1553
1554PROTOTYPES: DISABLE
1555
1556MODULE = IO::AIO PACKAGE = IO::AIO::REQ
1557
1558void
1559cancel (aio_req_ornot req)
1560 CODE:
1561 eio_cancel (req);
1562
1563void
1564cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1565 PPCODE:
1566{
1567 if (GIMME_V != G_VOID)
1568 XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef);
1569
1570 if (items > 1)
1571 {
1572 SV *cb_cv =get_cb (callback);
1573
1574 SvREFCNT_dec (req->callback);
1575 req->callback = SvREFCNT_inc (cb_cv);
1576 }
1577}
1578
1579MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1580
1581void
1582add (aio_req grp, ...)
1583 PPCODE:
1584{
1585 int i;
1586
1587 if (grp->int1 == 2)
1588 croak ("cannot add requests to IO::AIO::GRP after the group finished");
1589
1590 for (i = 1; i < items; ++i )
1591 {
1592 aio_req req;
1593
1594 if (GIMME_V != G_VOID)
1595 XPUSHs (sv_2mortal (newSVsv (ST (i))));
1596
1597 req = SvAIO_REQ (ST (i));
1598
1599 if (req)
1600 eio_grp_add (grp, req);
1601 }
1602}
1603
1604void
1605cancel_subs (aio_req_ornot req)
1606 CODE:
1607 req_cancel_subs (req);
1608
1609void
1610result (aio_req grp, ...)
1611 CODE:
1612{
1613 int i;
1614 AV *av;
1615
1616 grp->errorno = errno;
1617
1618 av = newAV ();
1619 av_extend (av, items - 1);
1620
1621 for (i = 1; i < items; ++i )
1622 av_push (av, newSVsv (ST (i)));
1623
1624 SvREFCNT_dec (grp->sv1);
1625 grp->sv1 = (SV *)av;
1626}
1627
1628void
1629errno (aio_req grp, int errorno = errno)
1630 CODE:
1631 grp->errorno = errorno;
1632
1633void
1634limit (aio_req grp, int limit)
1635 CODE:
1636 eio_grp_limit (grp, limit);
1637
1638void
1639feed (aio_req grp, SV *callback=&PL_sv_undef)
1640 CODE:
1641{
1642 SvREFCNT_dec (grp->sv2);
1643 grp->sv2 = newSVsv (callback);
1644 grp->feed = aio_grp_feed;
1645
1646 if (grp->int2 <= 0)
1647 grp->int2 = 2;
1648
1649 eio_grp_limit (grp, grp->int2);
1650}
1651

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines