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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines