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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines