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.125 by root, Thu Jun 19 09:05:43 2008 UTC vs.
Revision 1.163 by root, Sun Jan 10 23:44:02 2010 UTC

3#include <errno.h> 3#include <errno.h>
4 4
5#include "EXTERN.h" 5#include "EXTERN.h"
6#include "perl.h" 6#include "perl.h"
7#include "XSUB.h" 7#include "XSUB.h"
8
9#include "schmorp.h"
8 10
9#include <stddef.h> 11#include <stddef.h>
10#include <stdlib.h> 12#include <stdlib.h>
11#include <errno.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>
16#include <sys/statvfs.h>
14#include <limits.h> 17#include <limits.h>
15#include <fcntl.h> 18#include <fcntl.h>
16#include <sched.h> 19#include <sched.h>
20
21#if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES
22# include <sys/mman.h>
23#endif
17 24
18/* perl namespace pollution */ 25/* perl namespace pollution */
19#undef VERSION 26#undef VERSION
20 27
21#ifdef _WIN32 28#ifdef _WIN32
75 82
76#define EIO_STRUCT_STAT Stat_t 83#define EIO_STRUCT_STAT Stat_t
77 84
78/* use NV for 32 bit perls as it allows larger offsets */ 85/* use NV for 32 bit perls as it allows larger offsets */
79#if IVSIZE >= 8 86#if IVSIZE >= 8
87# define VAL64 IV
80# define SvVAL64 SvIV 88# define SvVAL64 SvIV
89# define newSVval64 newSViv
81#else 90#else
91# define VAL64 NV
82# define SvVAL64 SvNV 92# define SvVAL64 SvNV
93# define newSVval64 newSVnv
83#endif 94#endif
84 95
85static HV *stash; 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
86typedef SV SV8; /* byte-sv, used for argument-checking */ 109typedef SV SV8; /* byte-sv, used for argument-checking */
110typedef int aio_rfd; /* read file desriptor */
111typedef int aio_wfd; /* write file descriptor */
87 112
88#define AIO_REQ_KLASS "IO::AIO::REQ" 113static HV *aio_stash, *aio_req_stash, *aio_grp_stash;
89#define AIO_GRP_KLASS "IO::AIO::GRP"
90 114
91#define EIO_REQ_MEMBERS \ 115#define EIO_REQ_MEMBERS \
92 SV *callback; \ 116 SV *callback; \
93 SV *sv1, *sv2; \ 117 SV *sv1, *sv2; \
94 STRLEN stroffset; \ 118 STRLEN stroffset; \
96 120
97#define EIO_NO_WRAPPERS 1 121#define EIO_NO_WRAPPERS 1
98 122
99#include "libeio/eio.h" 123#include "libeio/eio.h"
100 124
125#ifndef POSIX_FADV_NORMAL
126# define POSIX_FADV_NORMAL 0
127# define NO_FADVISE 1
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#ifndef ST_NODEV
146# define ST_NODEV 0
147#endif
148#ifndef ST_NOEXEC
149# define ST_NOEXEC 0
150#endif
151#ifndef ST_SYNCHRONOUS
152# define ST_SYNCHRONOUS 0
153#endif
154#ifndef ST_MANDLOCK
155# define ST_MANDLOCK 0
156#endif
157#ifndef ST_WRITE
158# define ST_WRITE 0
159#endif
160#ifndef ST_APPEND
161# define ST_APPEND 0
162#endif
163#ifndef ST_IMMUTABLE
164# define ST_IMMUTABLE 0
165#endif
166#ifndef ST_NOATIME
167# define ST_NOATIME 0
168#endif
169#ifndef ST_NODIRATIME
170# define ST_NODIRATIME 0
171#endif
172#ifndef ST_RELATIME
173# define ST_RELATIME 0
174#endif
175
176#ifndef MCL_CURRENT
177# define MCL_CURRENT 0
178#endif
179#ifndef MCL_FUTURE
180# define MCL_FUTURE 0
181#endif
182
183#ifndef MAP_ANONYMOUS
184# ifdef MAP_ANON
185# define MAP_ANONYMOUS MAP_ANON
186# else
187# define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */
188# endif
189#endif
190#ifndef MAP_HUGETLB
191# define MAP_HUGETLB 0
192#endif
193#ifndef MAP_LOCKED
194# define MAP_LOCKED 0
195#endif
196#ifndef MAP_NORESERVE
197# define MAP_NORESERVE 0
198#endif
199#ifndef MAP_POPULATE
200# define MAP_POPULATE 0
201#endif
202#ifndef MAP_NONBLOCK
203# define MAP_NONBLOCK 0
204#endif
205
206#ifndef PAGESIZE
207# define PAGESIZE sysconf (_SC_PAGESIZE)
208#endif
209
101static int req_invoke (eio_req *req); 210static int req_invoke (eio_req *req);
102#define EIO_FINISH(req) req_invoke (req) 211#define EIO_FINISH(req) req_invoke (req)
103static void aio_grp_feed (eio_req *grp);
104#define EIO_FEED(req) aio_grp_feed (req)
105static void req_destroy (eio_req *grp); 212static void req_destroy (eio_req *grp);
106#define EIO_DESTROY(req) req_destroy (req) 213#define EIO_DESTROY(req) req_destroy (req)
107 214
108enum { 215enum {
109 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ 216 FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */
116 223
117static SV *on_next_submit; 224static SV *on_next_submit;
118static int next_pri = EIO_PRI_DEFAULT; 225static int next_pri = EIO_PRI_DEFAULT;
119static int max_outstanding; 226static int max_outstanding;
120 227
121static int respipe_osf [2], respipe [2] = { -1, -1 }; 228static s_epipe respipe;
122 229
123static void req_destroy (aio_req req); 230static void req_destroy (aio_req req);
124static void req_cancel (aio_req req); 231static void req_cancel (aio_req req);
125 232
126static void want_poll (void) 233static void want_poll (void)
127{ 234{
128 /* write a dummy byte to the pipe so fh becomes ready */ 235 /* write a dummy byte to the pipe so fh becomes ready */
129 respipe_write (respipe_osf [1], (const void *)&respipe_osf, 1); 236 s_epipe_signal (&respipe);
130} 237}
131 238
132static void done_poll (void) 239static void done_poll (void)
133{ 240{
134 /* read any signals sent by the worker threads */ 241 /* read any signals sent by the worker threads */
135 char buf [4]; 242 s_epipe_drain (&respipe);
136 while (respipe_read (respipe [0], buf, 4) == 4)
137 ;
138} 243}
139 244
140/* must be called at most once */ 245/* must be called at most once */
141static SV *req_sv (aio_req req, const char *klass) 246static SV *req_sv (aio_req req, HV *stash)
142{ 247{
143 if (!req->self) 248 if (!req->self)
144 { 249 {
145 req->self = (SV *)newHV (); 250 req->self = (SV *)newHV ();
146 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); 251 sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0);
147 } 252 }
148 253
149 return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); 254 return sv_2mortal (sv_bless (newRV_inc (req->self), stash));
150} 255}
151 256
152static aio_req SvAIO_REQ (SV *sv) 257static aio_req SvAIO_REQ (SV *sv)
153{ 258{
154 MAGIC *mg; 259 MAGIC *mg;
155 260
156 if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) 261 if (!SvROK (sv)
262 || (SvSTASH (SvRV (sv)) != aio_grp_stash
263 && SvSTASH (SvRV (sv)) != aio_req_stash
264 && !sv_derived_from (sv, "IO::AIO::REQ")))
157 croak ("object of class " AIO_REQ_KLASS " expected"); 265 croak ("object of class IO::AIO::REQ expected");
158 266
159 mg = mg_find (SvRV (sv), PERL_MAGIC_ext); 267 mg = mg_find (SvRV (sv), PERL_MAGIC_ext);
160 268
161 return mg ? (aio_req)mg->mg_ptr : 0; 269 return mg ? (aio_req)mg->mg_ptr : 0;
162} 270}
168 dSP; 276 dSP;
169 277
170 ENTER; 278 ENTER;
171 SAVETMPS; 279 SAVETMPS;
172 PUSHMARK (SP); 280 PUSHMARK (SP);
173 XPUSHs (req_sv (grp, AIO_GRP_KLASS)); 281 XPUSHs (req_sv (grp, aio_grp_stash));
174 PUTBACK; 282 PUTBACK;
175 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); 283 call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR);
176 SPAGAIN; 284 SPAGAIN;
177 FREETMPS; 285 FREETMPS;
178 LEAVE; 286 LEAVE;
181 289
182static void req_submit (eio_req *req) 290static void req_submit (eio_req *req)
183{ 291{
184 eio_submit (req); 292 eio_submit (req);
185 293
186 if (on_next_submit) 294 if (expect_false (on_next_submit))
187 { 295 {
188 dSP; 296 dSP;
189 SV *cb = sv_2mortal (on_next_submit); 297 SV *cb = sv_2mortal (on_next_submit);
190 298
191 on_next_submit = 0; 299 on_next_submit = 0;
201 dSP; 309 dSP;
202 310
203 if (req->flags & FLAG_SV2_RO_OFF) 311 if (req->flags & FLAG_SV2_RO_OFF)
204 SvREADONLY_off (req->sv2); 312 SvREADONLY_off (req->sv2);
205 313
206 if (!EIO_CANCELLED (req) && SvOK (req->callback)) 314 if (!EIO_CANCELLED (req) && req->callback)
207 { 315 {
208 ENTER; 316 ENTER;
209 SAVETMPS; 317 SAVETMPS;
210 PUSHMARK (SP); 318 PUSHMARK (SP);
211 EXTEND (SP, 1); 319 EXTEND (SP, 1);
217 SV *rv = &PL_sv_undef; 325 SV *rv = &PL_sv_undef;
218 326
219 if (req->result >= 0) 327 if (req->result >= 0)
220 { 328 {
221 int i; 329 int i;
222 char *buf = req->ptr2; 330 char *names = (char *)req->ptr2;
331 eio_dirent *ent = (eio_dirent *)req->ptr1; /* might be 0 */
223 AV *av = newAV (); 332 AV *av = newAV ();
224 333
225 av_extend (av, req->result - 1); 334 av_extend (av, req->result - 1);
226 335
227 for (i = 0; i < req->result; ++i) 336 for (i = 0; i < req->result; ++i)
228 { 337 {
229 SV *sv = newSVpv (buf, 0); 338 if (req->int1 & EIO_READDIR_DENTS)
339 {
340 SV *namesv = newSVpvn (names + ent->nameofs, ent->namelen);
230 341
342 if (req->int1 & EIO_READDIR_CUSTOM2)
343 {
344 static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */
345 AV *avent = newAV ();
346
347 av_extend (avent, 2);
348
349 if (!sv_type [ent->type])
350 {
351 sv_type [ent->type] = newSViv (ent->type);
352 SvREADONLY_on (sv_type [ent->type]);
353 }
354
355 av_store (avent, 0, namesv);
356 av_store (avent, 1, SvREFCNT_inc (sv_type [ent->type]));
357 av_store (avent, 2, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode));
358
359 av_store (av, i, newRV_noinc ((SV *)avent));
360 }
361 else
231 av_store (av, i, sv); 362 av_store (av, i, namesv);
363
364 ++ent;
365 }
366 else
367 {
368 SV *name = newSVpv (names, 0);
369 av_store (av, i, name);
232 buf += SvCUR (sv) + 1; 370 names += SvCUR (name) + 1;
371 }
233 } 372 }
234 373
235 rv = sv_2mortal (newRV_noinc ((SV *)av)); 374 rv = sv_2mortal (newRV_noinc ((SV *)av));
236 } 375 }
237 376
238 PUSHs (rv); 377 PUSHs (rv);
378
379 if (req->int1 & EIO_READDIR_CUSTOM1)
380 XPUSHs (sv_2mortal (newSViv (req->int1 & ~(EIO_READDIR_CUSTOM1 | EIO_READDIR_CUSTOM2))));
239 } 381 }
240 break; 382 break;
241 383
242 case EIO_OPEN: 384 case EIO_OPEN:
243 { 385 {
249 GV *gv = (GV *)sv_newmortal (); 391 GV *gv = (GV *)sv_newmortal ();
250 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); 392 int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR);
251 char sym [64]; 393 char sym [64];
252 int symlen; 394 int symlen;
253 395
254 symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result); 396 symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result);
255 gv_init (gv, stash, sym, symlen, 0); 397 gv_init (gv, aio_stash, sym, symlen, 0);
256 398
257 symlen = snprintf ( 399 symlen = snprintf (
258 sym, 400 sym,
259 sizeof (sym), 401 sizeof (sym),
260 "%s&=%d", 402 "%s&=%d",
261 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", 403 flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<",
262 req->result 404 (int)req->result
263 ); 405 );
264 406
265 if (do_open (gv, sym, symlen, 0, 0, 0, 0)) 407 if (do_open (gv, sym, symlen, 0, 0, 0, 0))
266 fh = (SV *)gv; 408 fh = (SV *)gv;
267 } 409 }
268 410
269 PUSHs (fh); 411 PUSHs (fh);
270 } 412 }
413 break;
414
415 case EIO_STATVFS:
416 case EIO_FSTATVFS:
417 {
418 SV *rv = &PL_sv_undef;
419
420 if (req->result >= 0)
421 {
422 EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req);
423 HV *hv = newHV ();
424
425 rv = sv_2mortal (newRV_noinc ((SV *)hv));
426
427 hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0);
428 hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0);
429 hv_store (hv, "blocks" , sizeof ("blocks" ) - 1, newSVval64 (f->f_blocks ), 0);
430 hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0);
431 hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0);
432 hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0);
433 hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0);
434 hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0);
435 hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0);
436 hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0);
437 hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0);
438 }
439
440 PUSHs (rv);
441 }
442
271 break; 443 break;
272 444
273 case EIO_GROUP: 445 case EIO_GROUP:
274 req->int1 = 2; /* mark group as finished */ 446 req->int1 = 2; /* mark group as finished */
275 447
301 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); 473 PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2);
302 PUSHs (sv_2mortal (newSViv (req->result))); 474 PUSHs (sv_2mortal (newSViv (req->result)));
303 break; 475 break;
304 476
305 case EIO_READ: 477 case EIO_READ:
478 {
306 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); 479 SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0));
307 *SvEND (req->sv2) = 0; 480 *SvEND (req->sv2) = 0;
481 SvPOK_only (req->sv2);
482 SvSETMAGIC (req->sv2);
308 PUSHs (sv_2mortal (newSViv (req->result))); 483 PUSHs (sv_2mortal (newSViv (req->result)));
484 }
309 break; 485 break;
310 486
311 case EIO_DUP2: 487 case EIO_DUP2:
312 if (req->result > 0) 488 if (req->result > 0)
313 req->result = 0; 489 req->result = 0;
359 grp->sv2 = 0; 535 grp->sv2 = 0;
360 536
361 eio_grp_cancel (grp); 537 eio_grp_cancel (grp);
362} 538}
363 539
364#ifdef USE_SOCKETS_AS_HANDLES
365# define TO_SOCKET(x) (win32_get_osfhandle (x))
366#else
367# define TO_SOCKET(x) (x)
368#endif
369
370static void 540static void
371create_respipe (void) 541create_respipe (void)
372{ 542{
373 int old_readfd = respipe [0];
374
375 if (respipe [1] >= 0)
376 respipe_close (TO_SOCKET (respipe [1]));
377
378#ifdef _WIN32
379 if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe))
380#else
381 if (pipe (respipe)) 543 if (s_epipe_renew (&respipe))
382#endif
383 croak ("unable to initialize result pipe"); 544 croak ("IO::AIO: unable to initialize result pipe");
384
385 if (old_readfd >= 0)
386 {
387 if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0)
388 croak ("unable to initialize result pipe(2)");
389
390 respipe_close (respipe [0]);
391 respipe [0] = old_readfd;
392 }
393
394#ifdef _WIN32
395 int arg = 1;
396 if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg)
397 || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg))
398#else
399 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)
400 || fcntl (respipe [1], F_SETFL, O_NONBLOCK))
401#endif
402 croak ("unable to initialize result pipe(3)");
403
404 respipe_osf [0] = TO_SOCKET (respipe [0]);
405 respipe_osf [1] = TO_SOCKET (respipe [1]);
406} 545}
407 546
408static void poll_wait (void) 547static void poll_wait (void)
409{ 548{
410 fd_set rfd;
411
412 while (eio_nreqs ()) 549 while (eio_nreqs ())
413 { 550 {
414 int size; 551 int size;
415 552
416 X_LOCK (reslock); 553 X_LOCK (reslock);
420 if (size) 557 if (size)
421 return; 558 return;
422 559
423 etp_maybe_start_thread (); 560 etp_maybe_start_thread ();
424 561
425 FD_ZERO (&rfd); 562 s_epipe_wait (&respipe);
426 FD_SET (respipe [0], &rfd);
427
428 PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0);
429 } 563 }
430} 564}
431 565
432static int poll_cb (void) 566static int poll_cb (void)
433{ 567{
448static void atfork_child (void) 582static void atfork_child (void)
449{ 583{
450 create_respipe (); 584 create_respipe ();
451} 585}
452 586
587/*****************************************************************************/
588
589#if !_POSIX_MAPPED_FILES
590# define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1)
591# define munmap(addr,length) (errno = ENOSYS, -1)
592#endif
593
594#define MMAP_MAGIC PERL_MAGIC_ext
595
596static int
597mmap_free (pTHX_ SV *sv, MAGIC *mg)
598{
599 int old_errno = errno;
600 munmap (mg->mg_ptr, (size_t)mg->mg_obj);
601 errno = old_errno;
602
603 mg->mg_obj = 0; /* just in case */
604
605 SvREADONLY_off (sv);
606 SvCUR_set (sv, 0);
607 SvLEN_set (sv, 0);
608 SvPVX (sv) = 0;
609 SvOK_off (sv);
610
611 return 0;
612}
613
614static MGVTBL mmap_vtbl = {
615 0, 0, 0, 0, mmap_free
616};
617
618/*****************************************************************************/
619
620static SV *
621get_cb (SV *cb_sv)
622{
623 SvGETMAGIC (cb_sv);
624 return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0;
625}
626
453#define dREQ \ 627#define dREQ \
628 SV *cb_cv; \
454 aio_req req; \ 629 aio_req req; \
455 int req_pri = next_pri; \ 630 int req_pri = next_pri; \
456 next_pri = EIO_PRI_DEFAULT; \ 631 next_pri = EIO_PRI_DEFAULT; \
457 \ 632 \
458 if (SvOK (callback) && !SvROK (callback)) \ 633 cb_cv = get_cb (callback); \
459 croak ("callback must be undef or of reference type"); \
460 \ 634 \
461 Newz (0, req, 1, eio_req); \ 635 Newz (0, req, 1, eio_req); \
462 if (!req) \ 636 if (!req) \
463 croak ("out of memory during eio_req allocation"); \ 637 croak ("out of memory during eio_req allocation"); \
464 \ 638 \
465 req->callback = newSVsv (callback); \ 639 req->callback = SvREFCNT_inc (cb_cv); \
466 req->pri = req_pri 640 req->pri = req_pri
467 641
468#define REQ_SEND \ 642#define REQ_SEND \
643 PUTBACK; \
469 req_submit (req); \ 644 req_submit (req); \
645 SPAGAIN; \
470 \ 646 \
471 if (GIMME_V != G_VOID) \ 647 if (GIMME_V != G_VOID) \
472 XPUSHs (req_sv (req, AIO_REQ_KLASS)); 648 XPUSHs (req_sv (req, aio_req_stash));
473 649
474MODULE = IO::AIO PACKAGE = IO::AIO 650MODULE = IO::AIO PACKAGE = IO::AIO
475 651
476PROTOTYPES: ENABLE 652PROTOTYPES: ENABLE
477 653
478BOOT: 654BOOT:
479{ 655{
480 stash = gv_stashpv ("IO::AIO", 1); 656 static const struct {
481 657 const char *name;
482 newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); 658 IV iv;
483 newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); 659 } *civ, const_iv[] = {
484 newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); 660# define const_niv(name, value) { # name, (IV) value },
485 newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); 661# define const_iv(name) { # name, (IV) name },
486 newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); 662# define const_eio(name) { # name, (IV) EIO_ ## name },
663 const_iv (EXDEV)
664 const_iv (ENOSYS)
665 const_iv (O_RDONLY)
666 const_iv (O_WRONLY)
667 const_iv (O_CREAT)
668 const_iv (O_TRUNC)
487#ifndef _WIN32 669#ifndef _WIN32
488 newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); 670 const_iv (S_IFIFO)
489#endif 671#endif
672 const_niv (FADV_NORMAL , POSIX_FADV_NORMAL)
673 const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL)
674 const_niv (FADV_RANDOM , POSIX_FADV_RANDOM)
675 const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE)
676 const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED)
677 const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED)
490 678
679 const_iv (ST_RDONLY)
680 const_iv (ST_NOSUID)
681 const_iv (ST_NODEV)
682 const_iv (ST_NOEXEC)
683 const_iv (ST_SYNCHRONOUS)
684 const_iv (ST_MANDLOCK)
685 const_iv (ST_WRITE)
686 const_iv (ST_APPEND)
687 const_iv (ST_IMMUTABLE)
688 const_iv (ST_NOATIME)
689 const_iv (ST_NODIRATIME)
690 const_iv (ST_RELATIME)
691
692 const_iv (PROT_EXEC)
693 const_iv (PROT_NONE)
694 const_iv (PROT_READ)
695 const_iv (PROT_WRITE)
696
697 /*const_iv (MAP_FIXED)*/
698 const_iv (MAP_PRIVATE)
699 const_iv (MAP_SHARED)
700 const_iv (MAP_ANONYMOUS)
701
702 /* linuxish */
703 const_iv (MAP_HUGETLB)
704 const_iv (MAP_LOCKED)
705 const_iv (MAP_NORESERVE)
706 const_iv (MAP_POPULATE)
707 const_iv (MAP_NONBLOCK)
708
709 const_iv (MCL_FUTURE)
710 const_iv (MCL_CURRENT)
711
712 const_eio (MS_ASYNC)
713 const_eio (MS_INVALIDATE)
714 const_eio (MS_SYNC)
715
716 const_eio (MT_MODIFY)
717
718 const_eio (SYNC_FILE_RANGE_WAIT_BEFORE)
719 const_eio (SYNC_FILE_RANGE_WRITE)
720 const_eio (SYNC_FILE_RANGE_WAIT_AFTER)
721
722 const_eio (READDIR_DENTS)
723 const_eio (READDIR_DIRS_FIRST)
724 const_eio (READDIR_STAT_ORDER)
725 const_eio (READDIR_FOUND_UNKNOWN)
726
727 const_eio (DT_UNKNOWN)
728 const_eio (DT_FIFO)
729 const_eio (DT_CHR)
730 const_eio (DT_DIR)
731 const_eio (DT_BLK)
732 const_eio (DT_REG)
733 const_eio (DT_LNK)
734 const_eio (DT_SOCK)
735 const_eio (DT_WHT)
736 };
737
738 aio_stash = gv_stashpv ("IO::AIO" , 1);
739 aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1);
740 aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1);
741
742 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
743 newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv));
744
745 newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE));
746
491 create_respipe (); 747 create_respipe ();
492 748
493 if (eio_init (want_poll, done_poll) < 0) 749 if (eio_init (want_poll, done_poll) < 0)
494 croak ("IO::AIO: unable to initialise eio library"); 750 croak ("IO::AIO: unable to initialise eio library");
495 751
496 /* atfork child called in fifo order, so before eio's handler */ 752 /* atfork child called in fifo order, so before eio's handler */
497 X_THREAD_ATFORK (0, 0, atfork_child); 753 X_THREAD_ATFORK (0, 0, atfork_child);
498} 754}
499 755
500void 756void
501max_poll_reqs (int nreqs) 757max_poll_reqs (int nreqs)
502 PROTOTYPE: $ 758 PROTOTYPE: $
555 ALIAS: 811 ALIAS:
556 aio_fsync = EIO_FSYNC 812 aio_fsync = EIO_FSYNC
557 aio_fdatasync = EIO_FDATASYNC 813 aio_fdatasync = EIO_FDATASYNC
558 PPCODE: 814 PPCODE:
559{ 815{
816 int fd = s_fileno_croak (fh, 0);
560 dREQ; 817 dREQ;
561 818
562 req->type = ix; 819 req->type = ix;
563 req->sv1 = newSVsv (fh); 820 req->sv1 = newSVsv (fh);
564 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); 821 req->int1 = fd;
822
823 REQ_SEND (req);
824}
825
826void
827aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef)
828 PROTOTYPE: $$$$;$
829 PPCODE:
830{
831 int fd = s_fileno_croak (fh, 0);
832 dREQ;
833
834 req->type = EIO_SYNC_FILE_RANGE;
835 req->sv1 = newSVsv (fh);
836 req->int1 = fd;
837 req->offs = offset;
838 req->size = nbytes;
839 req->int2 = flags;
565 840
566 REQ_SEND (req); 841 REQ_SEND (req);
567} 842}
568 843
569void 844void
570aio_close (SV *fh, SV *callback=&PL_sv_undef) 845aio_close (SV *fh, SV *callback=&PL_sv_undef)
571 PROTOTYPE: $;$ 846 PROTOTYPE: $;$
572 PPCODE: 847 PPCODE:
573{ 848{
574 static int close_pipe = -1; /* dummy fd to close fds via dup2 */ 849 static int close_pipe = -1; /* dummy fd to close fds via dup2 */
850 int fd = s_fileno_croak (fh, 0);
575 dREQ; 851 dREQ;
576 852
577 if (close_pipe < 0) 853 if (close_pipe < 0)
578 { 854 {
579 int pipefd [2]; 855 int pipefd [2];
587 } 863 }
588 864
589 req->type = EIO_DUP2; 865 req->type = EIO_DUP2;
590 req->int1 = close_pipe; 866 req->int1 = close_pipe;
591 req->sv2 = newSVsv (fh); 867 req->sv2 = newSVsv (fh);
592 req->int2 = PerlIO_fileno (IoIFP (sv_2io (fh))); 868 req->int2 = fd;
593 869
594 REQ_SEND (req); 870 REQ_SEND (req);
595} 871}
596 872
597void 873void
601 aio_write = EIO_WRITE 877 aio_write = EIO_WRITE
602 PROTOTYPE: $$$$$;$ 878 PROTOTYPE: $$$$$;$
603 PPCODE: 879 PPCODE:
604{ 880{
605 STRLEN svlen; 881 STRLEN svlen;
882 int fd = s_fileno_croak (fh, ix == EIO_WRITE);
606 char *svptr = SvPVbyte (data, svlen); 883 char *svptr = SvPVbyte (data, svlen);
607 UV len = SvUV (length); 884 UV len = SvUV (length);
608
609 SvUPGRADE (data, SVt_PV);
610 SvPOK_on (data);
611 885
612 if (dataoffset < 0) 886 if (dataoffset < 0)
613 dataoffset += svlen; 887 dataoffset += svlen;
614 888
615 if (dataoffset < 0 || dataoffset > svlen) 889 if (dataoffset < 0 || dataoffset > svlen)
621 if (!SvOK (length) || len + dataoffset > svlen) 895 if (!SvOK (length) || len + dataoffset > svlen)
622 len = svlen - dataoffset; 896 len = svlen - dataoffset;
623 } 897 }
624 else 898 else
625 { 899 {
626 /* read: grow scalar as necessary */ 900 /* read: check type and grow scalar as necessary */
901 SvUPGRADE (data, SVt_PV);
627 svptr = SvGROW (data, len + dataoffset + 1); 902 svptr = SvGROW (data, len + dataoffset + 1);
628 } 903 }
629 904
630 if (len < 0)
631 croak ("length must not be negative");
632
633 { 905 {
634 dREQ; 906 dREQ;
635 907
636 req->type = ix; 908 req->type = ix;
637 req->sv1 = newSVsv (fh); 909 req->sv1 = newSVsv (fh);
638 req->int1 = PerlIO_fileno (ix == EIO_READ ? IoIFP (sv_2io (fh)) 910 req->int1 = fd;
639 : IoOFP (sv_2io (fh)));
640 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; 911 req->offs = SvOK (offset) ? SvVAL64 (offset) : -1;
641 req->size = len; 912 req->size = len;
642 req->sv2 = SvREFCNT_inc (data); 913 req->sv2 = SvREFCNT_inc (data);
643 req->ptr2 = (char *)svptr + dataoffset; 914 req->ptr2 = (char *)svptr + dataoffset;
644 req->stroffset = dataoffset; 915 req->stroffset = dataoffset;
667 938
668 REQ_SEND; 939 REQ_SEND;
669} 940}
670 941
671void 942void
672aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) 943aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef)
673 PROTOTYPE: $$$$;$ 944 PROTOTYPE: $$$$;$
674 PPCODE: 945 PPCODE:
675{ 946{
947 int ifd = s_fileno_croak (in_fh , 0);
948 int ofd = s_fileno_croak (out_fh, 1);
676 dREQ; 949 dREQ;
677 950
678 req->type = EIO_SENDFILE; 951 req->type = EIO_SENDFILE;
679 req->sv1 = newSVsv (out_fh); 952 req->sv1 = newSVsv (out_fh);
680 req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); 953 req->int1 = ofd;
681 req->sv2 = newSVsv (in_fh); 954 req->sv2 = newSVsv (in_fh);
682 req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); 955 req->int2 = ifd;
683 req->offs = SvVAL64 (in_offset); 956 req->offs = in_offset;
684 req->size = length; 957 req->size = length;
685 958
686 REQ_SEND; 959 REQ_SEND;
687} 960}
688 961
689void 962void
690aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) 963aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef)
691 PROTOTYPE: $$$;$ 964 PROTOTYPE: $$$;$
692 PPCODE: 965 PPCODE:
693{ 966{
967 int fd = s_fileno_croak (fh, 0);
694 dREQ; 968 dREQ;
695 969
696 req->type = EIO_READAHEAD; 970 req->type = EIO_READAHEAD;
697 req->sv1 = newSVsv (fh); 971 req->sv1 = newSVsv (fh);
698 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); 972 req->int1 = fd;
699 req->offs = SvVAL64 (offset); 973 req->offs = offset;
700 req->size = length; 974 req->size = length;
701 975
702 REQ_SEND; 976 REQ_SEND;
703} 977}
704 978
705void 979void
706aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) 980aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef)
707 ALIAS: 981 ALIAS:
708 aio_stat = EIO_STAT 982 aio_stat = EIO_STAT
709 aio_lstat = EIO_LSTAT 983 aio_lstat = EIO_LSTAT
984 aio_statvfs = EIO_STATVFS
710 PPCODE: 985 PPCODE:
711{ 986{
712 dREQ; 987 dREQ;
713 988
714 req->sv1 = newSVsv (fh_or_path); 989 req->sv1 = newSVsv (fh_or_path);
718 req->type = ix; 993 req->type = ix;
719 req->ptr1 = SvPVbyte_nolen (req->sv1); 994 req->ptr1 = SvPVbyte_nolen (req->sv1);
720 } 995 }
721 else 996 else
722 { 997 {
723 req->type = EIO_FSTAT; 998 req->type = ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT;
724 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); 999 req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
725 } 1000 }
726 1001
727 REQ_SEND; 1002 REQ_SEND;
728} 1003}
823 1098
824 REQ_SEND; 1099 REQ_SEND;
825} 1100}
826 1101
827void 1102void
1103aio_readdirx (SV8 *pathname, IV flags, SV *callback=&PL_sv_undef)
1104 PPCODE:
1105{
1106 dREQ;
1107
1108 req->type = EIO_READDIR;
1109 req->sv1 = newSVsv (pathname);
1110 req->ptr1 = SvPVbyte_nolen (req->sv1);
1111 req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1;
1112
1113 if (flags & EIO_READDIR_DENTS)
1114 req->int1 |= EIO_READDIR_CUSTOM2;
1115
1116 REQ_SEND;
1117}
1118
1119void
828aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) 1120aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef)
829 ALIAS: 1121 ALIAS:
830 aio_unlink = EIO_UNLINK 1122 aio_unlink = EIO_UNLINK
831 aio_rmdir = EIO_RMDIR 1123 aio_rmdir = EIO_RMDIR
832 aio_readdir = EIO_READDIR 1124 aio_readdir = EIO_READDIR
874 1166
875 REQ_SEND; 1167 REQ_SEND;
876} 1168}
877 1169
878void 1170void
1171aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef)
1172 ALIAS:
1173 aio_mtouch = EIO_MTOUCH
1174 aio_msync = EIO_MSYNC
1175 PROTOTYPE: $$$$;$
1176 PPCODE:
1177{
1178 STRLEN svlen;
1179 UV len = SvUV (length);
1180 char *svptr = SvPVbyte (data, svlen);
1181
1182 if (offset < 0)
1183 offset += svlen;
1184
1185 if (offset < 0 || offset > svlen)
1186 croak ("offset outside of scalar");
1187
1188 if (!SvOK (length) || len + offset > svlen)
1189 len = svlen - offset;
1190
1191 {
1192 dREQ;
1193
1194 req->type = ix;
1195 req->size = len;
1196 req->sv2 = SvREFCNT_inc (data);
1197 req->ptr2 = (char *)svptr + offset;
1198 req->int1 = flags;
1199
1200 REQ_SEND;
1201 }
1202}
1203
1204void
879aio_busy (double delay, SV *callback=&PL_sv_undef) 1205aio_busy (double delay, SV *callback=&PL_sv_undef)
880 PPCODE: 1206 PPCODE:
881{ 1207{
882 dREQ; 1208 dREQ;
883 1209
894{ 1220{
895 dREQ; 1221 dREQ;
896 1222
897 req->type = EIO_GROUP; 1223 req->type = EIO_GROUP;
898 1224
899 eio_submit (req); 1225 req_submit (req);
900 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1226 XPUSHs (req_sv (req, aio_grp_stash));
901} 1227}
902 1228
903void 1229void
904aio_nop (SV *callback=&PL_sv_undef) 1230aio_nop (SV *callback=&PL_sv_undef)
905 ALIAS: 1231 ALIAS:
957 1283
958int 1284int
959poll_fileno() 1285poll_fileno()
960 PROTOTYPE: 1286 PROTOTYPE:
961 CODE: 1287 CODE:
962 RETVAL = respipe [0]; 1288 RETVAL = s_epipe_fd (&respipe);
963 OUTPUT: 1289 OUTPUT:
964 RETVAL 1290 RETVAL
965 1291
966int 1292int
967poll_cb(...) 1293poll_cb(...)
1007 CODE: 1333 CODE:
1008 RETVAL = eio_nthreads (); 1334 RETVAL = eio_nthreads ();
1009 OUTPUT: 1335 OUTPUT:
1010 RETVAL 1336 RETVAL
1011 1337
1338int
1339fadvise (aio_rfd fh, off_t offset, off_t length, IV advice)
1340 PROTOTYPE: $$$$
1341 CODE:
1342#if _XOPEN_SOURCE >= 600 && !NO_FADVISE
1343 RETVAL = posix_fadvise (fh, offset, length, advice);
1344#else
1345 RETVAL = errno = ENOSYS; /* yes, this is actually correct */
1346#endif
1347 OUTPUT:
1348 RETVAL
1349
1350ssize_t
1351sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count)
1352 PROTOTYPE: $$$$
1353 CODE:
1354 RETVAL = eio_sendfile_sync (ofh, ifh, offset, count);
1355 OUTPUT:
1356 RETVAL
1357
1358void
1359mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0)
1360 PROTOTYPE: $$$$$;$
1361 PPCODE:
1362 sv_unmagic (scalar, MMAP_MAGIC);
1363{
1364 int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1;
1365 void *addr = (void *)mmap (0, length, prot, flags, fd, offset);
1366 if (addr == (void *)-1)
1367 XSRETURN_NO;
1368
1369 /* we store the length in mg_obj, as namlen is I32 :/ */
1370 sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0)
1371 ->mg_obj = (SV *)length;
1372
1373 SvUPGRADE (scalar, SVt_PV); /* nop... */
1374 if (!(prot & PROT_WRITE))
1375 SvREADONLY_on (scalar);
1376
1377 SvPVX (scalar) = (char *)addr;
1378 SvCUR_set (scalar, length);
1379 SvLEN_set (scalar, 0);
1380 SvPOK_only (scalar);
1381
1382 XSRETURN_YES;
1383}
1384
1385void
1386munmap (SV *scalar)
1387 PROTOTYPE: $
1388 CODE:
1389 sv_unmagic (scalar, MMAP_MAGIC);
1390
1391int
1392mlockall (int flags)
1393 PROTOTYPE: $
1394 CODE:
1395#if _POSIX_MEMLOCK
1396#if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1397 extern int mallopt (int, int);
1398 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1399#endif
1400 mlockall (flags);
1401#else
1402 RETVAL = -1;
1403 errno = ENOSYS;
1404#endif
1405 OUTPUT:
1406 RETVAL
1407
1408int
1409munlockall ()
1410 PROTOTYPE:
1411 CODE:
1412#if _POSIX_MEMLOCK
1413 munlockall ();
1414#else
1415 RETVAL = -1;
1416 errno = ENOSYS;
1417#endif
1418 OUTPUT:
1419 RETVAL
1420
1012void _on_next_submit (SV *cb) 1421void _on_next_submit (SV *cb)
1013 CODE: 1422 CODE:
1014 SvREFCNT_dec (on_next_submit); 1423 SvREFCNT_dec (on_next_submit);
1015 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; 1424 on_next_submit = SvOK (cb) ? newSVsv (cb) : 0;
1016 1425
1023 CODE: 1432 CODE:
1024 eio_cancel (req); 1433 eio_cancel (req);
1025 1434
1026void 1435void
1027cb (aio_req_ornot req, SV *callback=&PL_sv_undef) 1436cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1028 CODE: 1437 PPCODE:
1438{
1439 if (GIMME_V != G_VOID)
1440 XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef);
1441
1442 if (items > 1)
1443 {
1444 SV *cb_cv =get_cb (callback);
1445
1029 SvREFCNT_dec (req->callback); 1446 SvREFCNT_dec (req->callback);
1030 req->callback = newSVsv (callback); 1447 req->callback = SvREFCNT_inc (cb_cv);
1448 }
1449}
1031 1450
1032MODULE = IO::AIO PACKAGE = IO::AIO::GRP 1451MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1033 1452
1034void 1453void
1035add (aio_req grp, ...) 1454add (aio_req grp, ...)
1067 AV *av; 1486 AV *av;
1068 1487
1069 grp->errorno = errno; 1488 grp->errorno = errno;
1070 1489
1071 av = newAV (); 1490 av = newAV ();
1491 av_extend (av, items - 1);
1072 1492
1073 for (i = 1; i < items; ++i ) 1493 for (i = 1; i < items; ++i )
1074 av_push (av, newSVsv (ST (i))); 1494 av_push (av, newSVsv (ST (i)));
1075 1495
1076 SvREFCNT_dec (grp->sv1); 1496 SvREFCNT_dec (grp->sv1);
1090void 1510void
1091feed (aio_req grp, SV *callback=&PL_sv_undef) 1511feed (aio_req grp, SV *callback=&PL_sv_undef)
1092 CODE: 1512 CODE:
1093{ 1513{
1094 SvREFCNT_dec (grp->sv2); 1514 SvREFCNT_dec (grp->sv2);
1095 grp->sv2 = newSVsv (callback); 1515 grp->sv2 = newSVsv (callback);
1516 grp->feed = aio_grp_feed;
1096 1517
1097 if (grp->int2 <= 0) 1518 if (grp->int2 <= 0)
1098 grp->int2 = 2; 1519 grp->int2 = 2;
1099 1520
1100 eio_grp_limit (grp, grp->int2); 1521 eio_grp_limit (grp, grp->int2);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines