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