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