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