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