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