1 | /* solaris */ |
1 | #include "libeio/xthread.h" |
2 | #define _POSIX_PTHREAD_SEMANTICS 1 |
|
|
3 | |
|
|
4 | #if __linux && !defined(_GNU_SOURCE) |
|
|
5 | # define _GNU_SOURCE |
|
|
6 | #endif |
|
|
7 | |
|
|
8 | /* just in case */ |
|
|
9 | #define _REENTRANT 1 |
|
|
10 | |
2 | |
11 | #include <errno.h> |
3 | #include <errno.h> |
12 | |
4 | |
13 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
14 | #include "perl.h" |
6 | #include "perl.h" |
15 | #include "XSUB.h" |
7 | #include "XSUB.h" |
16 | |
8 | |
17 | #include "autoconf/config.h" |
|
|
18 | |
|
|
19 | #include <pthread.h> |
|
|
20 | |
|
|
21 | #include <stddef.h> |
9 | #include <stddef.h> |
|
|
10 | #include <stdlib.h> |
22 | #include <errno.h> |
11 | #include <errno.h> |
23 | #include <sys/time.h> |
|
|
24 | #include <sys/select.h> |
|
|
25 | #include <sys/types.h> |
12 | #include <sys/types.h> |
26 | #include <sys/stat.h> |
13 | #include <sys/stat.h> |
27 | #include <limits.h> |
14 | #include <limits.h> |
28 | #include <unistd.h> |
|
|
29 | #include <fcntl.h> |
15 | #include <fcntl.h> |
30 | #include <signal.h> |
|
|
31 | #include <sched.h> |
16 | #include <sched.h> |
32 | |
17 | |
33 | #if HAVE_SENDFILE |
18 | /* perl namespace pollution */ |
34 | # if __linux |
19 | #undef VERSION |
35 | # include <sys/sendfile.h> |
20 | |
36 | # elif __freebsd |
21 | #ifdef _WIN32 |
37 | # include <sys/socket.h> |
22 | |
38 | # include <sys/uio.h> |
23 | # define EIO_STRUCT_DIRENT Direntry_t |
39 | # elif __hpux |
24 | # undef malloc |
40 | # include <sys/socket.h> |
25 | # undef free |
41 | # elif __solaris /* not yet */ |
26 | |
42 | # include <sys/sendfile.h> |
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 | |
43 | # else |
59 | #else |
44 | # error sendfile support requested but not available |
60 | |
45 | # endif |
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 | |
46 | #endif |
68 | #endif |
47 | |
69 | |
48 | /* used for struct dirent, AIX doesn't provide it */ |
70 | /* perl stupidly overrides readdir and maybe others */ |
49 | #ifndef NAME_MAX |
71 | /* with thread-unsafe versions, imagine that :( */ |
50 | # define NAME_MAX 4096 |
72 | #undef readdir |
|
|
73 | #undef opendir |
|
|
74 | #undef closedir |
|
|
75 | |
|
|
76 | #define EIO_STRUCT_STAT Stat_t |
|
|
77 | |
|
|
78 | /* use NV for 32 bit perls as it allows larger offsets */ |
|
|
79 | #if IVSIZE >= 8 |
|
|
80 | # define SvVAL64 SvIV |
|
|
81 | #else |
|
|
82 | # define SvVAL64 SvNV |
51 | #endif |
83 | #endif |
52 | |
84 | |
53 | #ifndef PTHREAD_STACK_MIN |
85 | /*****************************************************************************/ |
54 | /* care for broken platforms, e.g. windows */ |
86 | |
55 | # define PTHREAD_STACK_MIN 16384 |
87 | #if __GNUC__ >= 3 |
|
|
88 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
89 | #else |
|
|
90 | # define expect(expr,value) (expr) |
56 | #endif |
91 | #endif |
57 | |
92 | |
58 | #if __ia64 |
93 | #define expect_false(expr) expect ((expr) != 0, 0) |
59 | # define STACKSIZE 65536 |
94 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
|
|
61 | # define STACKSIZE PTHREAD_STACK_MIN |
|
|
62 | #else |
|
|
63 | # define STACKSIZE 16384 |
|
|
64 | #endif |
|
|
65 | |
95 | |
66 | /* wether word reads are potentially non-atomic. |
96 | /*****************************************************************************/ |
67 | * this is conservatice, likely most arches this runs |
|
|
68 | * on have atomic word read/writes. |
|
|
69 | */ |
|
|
70 | #ifndef WORDREAD_UNSAFE |
|
|
71 | # if __i386 || __x86_64 |
|
|
72 | # define WORDREAD_UNSAFE 0 |
|
|
73 | # else |
|
|
74 | # define WORDREAD_UNSAFE 1 |
|
|
75 | # endif |
|
|
76 | #endif |
|
|
77 | |
97 | |
78 | /* buffer size for various temporary buffers */ |
98 | static HV *stash; |
79 | #define AIO_BUFSIZE 65536 |
99 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
80 | |
|
|
81 | #define dBUF \ |
|
|
82 | char *aio_buf; \ |
|
|
83 | LOCK (wrklock); \ |
|
|
84 | self->dbuf = aio_buf = malloc (AIO_BUFSIZE); \ |
|
|
85 | UNLOCK (wrklock); \ |
|
|
86 | if (!aio_buf) \ |
|
|
87 | return -1; |
|
|
88 | |
|
|
89 | enum { |
|
|
90 | REQ_QUIT, |
|
|
91 | REQ_OPEN, REQ_CLOSE, |
|
|
92 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
93 | REQ_SENDFILE, |
|
|
94 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
|
|
95 | REQ_FSYNC, REQ_FDATASYNC, |
|
|
96 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
|
|
97 | REQ_MKNOD, REQ_READDIR, |
|
|
98 | REQ_LINK, REQ_SYMLINK, |
|
|
99 | REQ_GROUP, REQ_NOP, |
|
|
100 | REQ_BUSY, |
|
|
101 | }; |
|
|
102 | |
100 | |
103 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
101 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
104 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
102 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
105 | |
103 | |
106 | typedef struct aio_cb |
104 | #define EIO_REQ_MEMBERS \ |
107 | { |
|
|
108 | struct aio_cb *volatile next; |
|
|
109 | |
|
|
110 | SV *data, *callback; |
105 | SV *callback; \ |
111 | SV *fh, *fh2; |
106 | SV *sv1, *sv2; \ |
112 | void *dataptr, *data2ptr; |
|
|
113 | Stat_t *statdata; |
|
|
114 | off_t offset; |
|
|
115 | size_t length; |
|
|
116 | ssize_t result; |
|
|
117 | |
|
|
118 | STRLEN dataoffset; |
107 | STRLEN stroffset; \ |
119 | int type; |
108 | SV *self; |
120 | int fd, fd2; |
|
|
121 | int errorno; |
|
|
122 | mode_t mode; /* open */ |
|
|
123 | |
109 | |
124 | unsigned char flags; |
110 | #define EIO_NO_WRAPPERS 1 |
125 | unsigned char pri; |
|
|
126 | |
111 | |
127 | SV *self; /* the perl counterpart of this request, if any */ |
112 | #include "libeio/eio.h" |
128 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
113 | |
129 | } aio_cb; |
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) |
130 | |
118 | |
131 | enum { |
119 | enum { |
132 | FLAG_CANCELLED = 0x01, |
120 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
133 | }; |
121 | }; |
134 | |
122 | |
|
|
123 | #include "libeio/eio.c" |
|
|
124 | |
135 | typedef aio_cb *aio_req; |
125 | typedef eio_req *aio_req; |
136 | typedef aio_cb *aio_req_ornot; |
126 | typedef eio_req *aio_req_ornot; |
137 | |
127 | |
138 | enum { |
128 | static SV *on_next_submit; |
139 | PRI_MIN = -4, |
129 | static int next_pri = EIO_PRI_DEFAULT; |
140 | PRI_MAX = 4, |
130 | static int max_outstanding; |
141 | |
131 | |
142 | DEFAULT_PRI = 0, |
132 | static int respipe_osf [2], respipe [2] = { -1, -1 }; |
143 | PRI_BIAS = -PRI_MIN, |
|
|
144 | NUM_PRI = PRI_MAX + PRI_BIAS + 1, |
|
|
145 | }; |
|
|
146 | |
133 | |
147 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
|
|
148 | |
|
|
149 | static unsigned int started, wanted; |
|
|
150 | |
|
|
151 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
|
|
152 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
|
|
153 | #else |
|
|
154 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
155 | #endif |
|
|
156 | |
|
|
157 | #define LOCK(mutex) pthread_mutex_lock (&(mutex)) |
|
|
158 | #define UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) |
|
|
159 | |
|
|
160 | /* worker threads management */ |
|
|
161 | static pthread_mutex_t wrklock = AIO_MUTEX_INIT; |
|
|
162 | |
|
|
163 | typedef struct worker { |
|
|
164 | /* locked by wrklock */ |
|
|
165 | struct worker *prev, *next; |
|
|
166 | |
|
|
167 | pthread_t tid; |
|
|
168 | |
|
|
169 | /* locked by reslock, reqlock or wrklock */ |
|
|
170 | aio_req req; /* currently processed request */ |
|
|
171 | void *dbuf; |
|
|
172 | DIR *dirp; |
|
|
173 | } worker; |
|
|
174 | |
|
|
175 | static worker wrk_first = { &wrk_first, &wrk_first, 0 }; |
|
|
176 | |
|
|
177 | static void worker_clear (worker *wrk) |
|
|
178 | { |
|
|
179 | if (wrk->dirp) |
|
|
180 | { |
|
|
181 | closedir (wrk->dirp); |
|
|
182 | wrk->dirp = 0; |
|
|
183 | } |
|
|
184 | |
|
|
185 | if (wrk->dbuf) |
|
|
186 | { |
|
|
187 | free (wrk->dbuf); |
|
|
188 | wrk->dbuf = 0; |
|
|
189 | } |
|
|
190 | } |
|
|
191 | |
|
|
192 | static void worker_free (worker *wrk) |
|
|
193 | { |
|
|
194 | wrk->next->prev = wrk->prev; |
|
|
195 | wrk->prev->next = wrk->next; |
|
|
196 | |
|
|
197 | free (wrk); |
|
|
198 | } |
|
|
199 | |
|
|
200 | static volatile unsigned int nreqs, nready, npending; |
|
|
201 | static volatile unsigned int max_outstanding = 0xffffffff; |
|
|
202 | static int respipe [2]; |
|
|
203 | |
|
|
204 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
|
|
205 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
|
|
206 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
|
|
207 | |
|
|
208 | #if WORDREAD_UNSAFE |
|
|
209 | |
|
|
210 | static unsigned int get_nready () |
|
|
211 | { |
|
|
212 | unsigned int retval; |
|
|
213 | |
|
|
214 | LOCK (reqlock); |
|
|
215 | retval = nready; |
|
|
216 | UNLOCK (reqlock); |
|
|
217 | |
|
|
218 | return retval; |
|
|
219 | } |
|
|
220 | |
|
|
221 | static unsigned int get_npending () |
|
|
222 | { |
|
|
223 | unsigned int retval; |
|
|
224 | |
|
|
225 | LOCK (reslock); |
|
|
226 | retval = npending; |
|
|
227 | UNLOCK (reslock); |
|
|
228 | |
|
|
229 | return retval; |
|
|
230 | } |
|
|
231 | |
|
|
232 | #else |
|
|
233 | |
|
|
234 | # define get_nready() nready |
|
|
235 | # define get_npending() npending |
|
|
236 | |
|
|
237 | #endif |
|
|
238 | |
|
|
239 | /* |
|
|
240 | * a somewhat faster data structure might be nice, but |
|
|
241 | * with 8 priorities this actually needs <20 insns |
|
|
242 | * per shift, the most expensive operation. |
|
|
243 | */ |
|
|
244 | typedef struct { |
|
|
245 | aio_req qs[NUM_PRI], qe[NUM_PRI]; /* qstart, qend */ |
|
|
246 | int size; |
|
|
247 | } reqq; |
|
|
248 | |
|
|
249 | static reqq req_queue; |
|
|
250 | static reqq res_queue; |
|
|
251 | |
|
|
252 | int reqq_push (reqq *q, aio_req req) |
|
|
253 | { |
|
|
254 | int pri = req->pri; |
|
|
255 | req->next = 0; |
|
|
256 | |
|
|
257 | if (q->qe[pri]) |
|
|
258 | { |
|
|
259 | q->qe[pri]->next = req; |
|
|
260 | q->qe[pri] = req; |
|
|
261 | } |
|
|
262 | else |
|
|
263 | q->qe[pri] = q->qs[pri] = req; |
|
|
264 | |
|
|
265 | return q->size++; |
|
|
266 | } |
|
|
267 | |
|
|
268 | aio_req reqq_shift (reqq *q) |
|
|
269 | { |
|
|
270 | int pri; |
|
|
271 | |
|
|
272 | if (!q->size) |
|
|
273 | return 0; |
|
|
274 | |
|
|
275 | --q->size; |
|
|
276 | |
|
|
277 | for (pri = NUM_PRI; pri--; ) |
|
|
278 | { |
|
|
279 | aio_req req = q->qs[pri]; |
|
|
280 | |
|
|
281 | if (req) |
|
|
282 | { |
|
|
283 | if (!(q->qs[pri] = req->next)) |
|
|
284 | q->qe[pri] = 0; |
|
|
285 | |
|
|
286 | return req; |
|
|
287 | } |
|
|
288 | } |
|
|
289 | |
|
|
290 | abort (); |
|
|
291 | } |
|
|
292 | |
|
|
293 | static int poll_cb (int max); |
|
|
294 | static void req_invoke (aio_req req); |
|
|
295 | static void req_free (aio_req req); |
134 | static void req_destroy (aio_req req); |
296 | static void req_cancel (aio_req req); |
135 | static void req_cancel (aio_req req); |
|
|
136 | |
|
|
137 | static void want_poll (void) |
|
|
138 | { |
|
|
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 | } |
297 | |
150 | |
298 | /* must be called at most once */ |
151 | /* must be called at most once */ |
299 | static SV *req_sv (aio_req req, const char *klass) |
152 | static SV *req_sv (aio_req req, const char *klass) |
300 | { |
153 | { |
301 | if (!req->self) |
154 | if (!req->self) |
… | |
… | |
319 | return mg ? (aio_req)mg->mg_ptr : 0; |
172 | return mg ? (aio_req)mg->mg_ptr : 0; |
320 | } |
173 | } |
321 | |
174 | |
322 | static void aio_grp_feed (aio_req grp) |
175 | static void aio_grp_feed (aio_req grp) |
323 | { |
176 | { |
324 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
177 | if (grp->sv2 && SvOK (grp->sv2)) |
325 | { |
178 | { |
326 | int old_len = grp->length; |
|
|
327 | |
|
|
328 | if (grp->fh2 && SvOK (grp->fh2)) |
|
|
329 | { |
|
|
330 | dSP; |
179 | dSP; |
331 | |
180 | |
332 | ENTER; |
181 | ENTER; |
333 | SAVETMPS; |
182 | SAVETMPS; |
334 | PUSHMARK (SP); |
183 | PUSHMARK (SP); |
335 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
184 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
336 | PUTBACK; |
185 | PUTBACK; |
337 | call_sv (grp->fh2, G_VOID | G_EVAL | G_KEEPERR); |
186 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
338 | SPAGAIN; |
187 | SPAGAIN; |
339 | FREETMPS; |
188 | FREETMPS; |
340 | LEAVE; |
189 | LEAVE; |
341 | } |
|
|
342 | |
|
|
343 | /* stop if no progress has been made */ |
|
|
344 | if (old_len == grp->length) |
|
|
345 | { |
|
|
346 | SvREFCNT_dec (grp->fh2); |
|
|
347 | grp->fh2 = 0; |
|
|
348 | break; |
|
|
349 | } |
|
|
350 | } |
190 | } |
351 | } |
191 | } |
352 | |
192 | |
353 | static void aio_grp_dec (aio_req grp) |
193 | static void req_submit (eio_req *req) |
354 | { |
194 | { |
355 | --grp->length; |
195 | eio_submit (req); |
356 | |
196 | |
357 | /* call feeder, if applicable */ |
197 | if (expect_false (on_next_submit)) |
358 | aio_grp_feed (grp); |
|
|
359 | |
|
|
360 | /* finish, if done */ |
|
|
361 | if (!grp->length && grp->fd) |
|
|
362 | { |
198 | { |
363 | req_invoke (grp); |
199 | dSP; |
364 | req_free (grp); |
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); |
365 | } |
207 | } |
366 | } |
208 | } |
367 | |
209 | |
368 | static void req_invoke (aio_req req) |
210 | static int req_invoke (eio_req *req) |
369 | { |
211 | { |
370 | dSP; |
212 | dSP; |
371 | |
213 | |
372 | if (!(req->flags & FLAG_CANCELLED) && SvOK (req->callback)) |
214 | if (req->flags & FLAG_SV2_RO_OFF) |
|
|
215 | SvREADONLY_off (req->sv2); |
|
|
216 | |
|
|
217 | if (!EIO_CANCELLED (req) && req->callback) |
373 | { |
218 | { |
374 | ENTER; |
219 | ENTER; |
375 | SAVETMPS; |
220 | SAVETMPS; |
376 | PUSHMARK (SP); |
221 | PUSHMARK (SP); |
377 | EXTEND (SP, 1); |
222 | EXTEND (SP, 1); |
378 | |
223 | |
379 | switch (req->type) |
224 | switch (req->type) |
380 | { |
225 | { |
381 | case REQ_READDIR: |
226 | case EIO_READDIR: |
382 | { |
227 | { |
383 | SV *rv = &PL_sv_undef; |
228 | SV *rv = &PL_sv_undef; |
384 | |
229 | |
385 | if (req->result >= 0) |
230 | if (req->result >= 0) |
386 | { |
231 | { |
387 | int i; |
232 | int i; |
388 | char *buf = req->data2ptr; |
233 | char *buf = req->ptr2; |
389 | AV *av = newAV (); |
234 | AV *av = newAV (); |
390 | |
235 | |
391 | av_extend (av, req->result - 1); |
236 | av_extend (av, req->result - 1); |
392 | |
237 | |
393 | for (i = 0; i < req->result; ++i) |
238 | for (i = 0; i < req->result; ++i) |
… | |
… | |
403 | |
248 | |
404 | PUSHs (rv); |
249 | PUSHs (rv); |
405 | } |
250 | } |
406 | break; |
251 | break; |
407 | |
252 | |
408 | case REQ_OPEN: |
253 | case EIO_OPEN: |
409 | { |
254 | { |
410 | /* convert fd to fh */ |
255 | /* convert fd to fh */ |
411 | SV *fh; |
256 | SV *fh = &PL_sv_undef; |
412 | |
257 | |
413 | PUSHs (sv_2mortal (newSViv (req->result))); |
258 | if (req->result >= 0) |
414 | PUTBACK; |
259 | { |
415 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
260 | GV *gv = (GV *)sv_newmortal (); |
416 | SPAGAIN; |
261 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
|
|
262 | char sym [64]; |
|
|
263 | int symlen; |
|
|
264 | |
|
|
265 | symlen = snprintf (sym, sizeof (sym), "fd#%d", req->result); |
|
|
266 | gv_init (gv, stash, sym, symlen, 0); |
417 | |
267 | |
418 | fh = SvREFCNT_inc (POPs); |
268 | symlen = snprintf ( |
|
|
269 | sym, |
|
|
270 | sizeof (sym), |
|
|
271 | "%s&=%d", |
|
|
272 | flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", |
|
|
273 | req->result |
|
|
274 | ); |
419 | |
275 | |
|
|
276 | if (do_open (gv, sym, symlen, 0, 0, 0, 0)) |
|
|
277 | fh = (SV *)gv; |
|
|
278 | } |
|
|
279 | |
420 | PUSHMARK (SP); |
280 | PUSHs (fh); |
421 | XPUSHs (sv_2mortal (fh)); |
|
|
422 | } |
281 | } |
423 | break; |
282 | break; |
424 | |
283 | |
425 | case REQ_GROUP: |
284 | case EIO_GROUP: |
426 | req->fd = 2; /* mark group as finished */ |
285 | req->int1 = 2; /* mark group as finished */ |
427 | |
286 | |
428 | if (req->data) |
287 | if (req->sv1) |
429 | { |
288 | { |
430 | int i; |
289 | int i; |
431 | AV *av = (AV *)req->data; |
290 | AV *av = (AV *)req->sv1; |
432 | |
291 | |
433 | EXTEND (SP, AvFILL (av) + 1); |
292 | EXTEND (SP, AvFILL (av) + 1); |
434 | for (i = 0; i <= AvFILL (av); ++i) |
293 | for (i = 0; i <= AvFILL (av); ++i) |
435 | PUSHs (*av_fetch (av, i, 0)); |
294 | PUSHs (*av_fetch (av, i, 0)); |
436 | } |
295 | } |
437 | break; |
296 | break; |
438 | |
297 | |
439 | case REQ_NOP: |
298 | case EIO_NOP: |
440 | case REQ_BUSY: |
299 | case EIO_BUSY: |
441 | break; |
300 | break; |
|
|
301 | |
|
|
302 | case EIO_READLINK: |
|
|
303 | if (req->result > 0) |
|
|
304 | PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result))); |
|
|
305 | break; |
|
|
306 | |
|
|
307 | case EIO_STAT: |
|
|
308 | case EIO_LSTAT: |
|
|
309 | case EIO_FSTAT: |
|
|
310 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
311 | PL_laststatval = req->result; |
|
|
312 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
|
|
313 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
314 | break; |
|
|
315 | |
|
|
316 | case EIO_READ: |
|
|
317 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
|
|
318 | *SvEND (req->sv2) = 0; |
|
|
319 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
320 | break; |
|
|
321 | |
|
|
322 | case EIO_DUP2: |
|
|
323 | if (req->result > 0) |
|
|
324 | req->result = 0; |
|
|
325 | /* FALLTHROUGH */ |
442 | |
326 | |
443 | default: |
327 | default: |
444 | PUSHs (sv_2mortal (newSViv (req->result))); |
328 | PUSHs (sv_2mortal (newSViv (req->result))); |
445 | break; |
329 | break; |
446 | } |
330 | } |
447 | |
331 | |
448 | errno = req->errorno; |
332 | errno = req->errorno; |
449 | |
333 | |
450 | PUTBACK; |
334 | PUTBACK; |
451 | call_sv (req->callback, G_VOID | G_EVAL); |
335 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
452 | SPAGAIN; |
336 | SPAGAIN; |
453 | |
337 | |
454 | FREETMPS; |
338 | FREETMPS; |
455 | LEAVE; |
339 | LEAVE; |
|
|
340 | |
|
|
341 | PUTBACK; |
456 | } |
342 | } |
457 | |
343 | |
458 | if (req->grp) |
344 | return !!SvTRUE (ERRSV); |
459 | { |
|
|
460 | aio_req grp = req->grp; |
|
|
461 | |
|
|
462 | /* unlink request */ |
|
|
463 | if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; |
|
|
464 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
|
|
465 | |
|
|
466 | if (grp->grp_first == req) |
|
|
467 | grp->grp_first = req->grp_next; |
|
|
468 | |
|
|
469 | aio_grp_dec (grp); |
|
|
470 | } |
|
|
471 | |
|
|
472 | if (SvTRUE (ERRSV)) |
|
|
473 | { |
|
|
474 | req_free (req); |
|
|
475 | croak (0); |
|
|
476 | } |
|
|
477 | } |
345 | } |
478 | |
346 | |
479 | static void req_free (aio_req req) |
347 | static void req_destroy (aio_req req) |
480 | { |
348 | { |
481 | if (req->self) |
349 | if (req->self) |
482 | { |
350 | { |
483 | sv_unmagic (req->self, PERL_MAGIC_ext); |
351 | sv_unmagic (req->self, PERL_MAGIC_ext); |
484 | SvREFCNT_dec (req->self); |
352 | SvREFCNT_dec (req->self); |
485 | } |
353 | } |
486 | |
354 | |
487 | SvREFCNT_dec (req->data); |
|
|
488 | SvREFCNT_dec (req->fh); |
355 | SvREFCNT_dec (req->sv1); |
489 | SvREFCNT_dec (req->fh2); |
356 | SvREFCNT_dec (req->sv2); |
490 | SvREFCNT_dec (req->callback); |
357 | SvREFCNT_dec (req->callback); |
491 | Safefree (req->statdata); |
|
|
492 | |
|
|
493 | if (req->type == REQ_READDIR) |
|
|
494 | free (req->data2ptr); |
|
|
495 | |
358 | |
496 | Safefree (req); |
359 | Safefree (req); |
497 | } |
360 | } |
498 | |
361 | |
499 | static void req_cancel_subs (aio_req grp) |
362 | static void req_cancel_subs (aio_req grp) |
500 | { |
363 | { |
501 | aio_req sub; |
364 | aio_req sub; |
502 | |
365 | |
503 | if (grp->type != REQ_GROUP) |
366 | if (grp->type != EIO_GROUP) |
504 | return; |
367 | return; |
505 | |
368 | |
506 | SvREFCNT_dec (grp->fh2); |
369 | SvREFCNT_dec (grp->sv2); |
507 | grp->fh2 = 0; |
370 | grp->sv2 = 0; |
508 | |
371 | |
509 | for (sub = grp->grp_first; sub; sub = sub->grp_next) |
372 | eio_grp_cancel (grp); |
510 | req_cancel (sub); |
|
|
511 | } |
373 | } |
512 | |
374 | |
513 | static void req_cancel (aio_req req) |
375 | #ifdef USE_SOCKETS_AS_HANDLES |
514 | { |
376 | # define TO_SOCKET(x) (win32_get_osfhandle (x)) |
515 | req->flags |= FLAG_CANCELLED; |
377 | #else |
516 | |
378 | # define TO_SOCKET(x) (x) |
517 | req_cancel_subs (req); |
|
|
518 | } |
|
|
519 | |
|
|
520 | static void *aio_proc(void *arg); |
|
|
521 | |
|
|
522 | static void start_thread (void) |
|
|
523 | { |
|
|
524 | sigset_t fullsigset, oldsigset; |
|
|
525 | pthread_attr_t attr; |
|
|
526 | |
|
|
527 | worker *wrk = calloc (1, sizeof (worker)); |
|
|
528 | |
|
|
529 | if (!wrk) |
|
|
530 | croak ("unable to allocate worker thread data"); |
|
|
531 | |
|
|
532 | pthread_attr_init (&attr); |
|
|
533 | pthread_attr_setstacksize (&attr, STACKSIZE); |
|
|
534 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
|
|
535 | #ifdef PTHREAD_SCOPE_PROCESS |
|
|
536 | pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); |
|
|
537 | #endif |
379 | #endif |
538 | |
380 | |
539 | sigfillset (&fullsigset); |
381 | static void |
|
|
382 | create_respipe (void) |
|
|
383 | { |
|
|
384 | int old_readfd = respipe [0]; |
540 | |
385 | |
541 | LOCK (wrklock); |
386 | if (respipe [1] >= 0) |
542 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
387 | respipe_close (TO_SOCKET (respipe [1])); |
543 | |
388 | |
544 | if (pthread_create (&wrk->tid, &attr, aio_proc, (void *)wrk) == 0) |
389 | #ifdef _WIN32 |
|
|
390 | if (PerlSock_socketpair (AF_UNIX, SOCK_STREAM, 0, respipe)) |
|
|
391 | #else |
|
|
392 | if (pipe (respipe)) |
|
|
393 | #endif |
|
|
394 | croak ("unable to initialize result pipe"); |
|
|
395 | |
|
|
396 | if (old_readfd >= 0) |
545 | { |
397 | { |
546 | wrk->prev = &wrk_first; |
398 | if (dup2 (TO_SOCKET (respipe [0]), TO_SOCKET (old_readfd)) < 0) |
547 | wrk->next = wrk_first.next; |
399 | croak ("unable to initialize result pipe(2)"); |
548 | wrk_first.next->prev = wrk; |
400 | |
549 | wrk_first.next = wrk; |
401 | respipe_close (respipe [0]); |
550 | ++started; |
402 | respipe [0] = old_readfd; |
551 | } |
403 | } |
552 | else |
|
|
553 | free (wrk); |
|
|
554 | |
404 | |
555 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
405 | #ifdef _WIN32 |
556 | UNLOCK (wrklock); |
406 | int arg = 1; |
557 | } |
407 | if (ioctlsocket (TO_SOCKET (respipe [0]), FIONBIO, &arg) |
558 | |
408 | || ioctlsocket (TO_SOCKET (respipe [1]), FIONBIO, &arg)) |
559 | static void maybe_start_thread () |
409 | #else |
560 | { |
410 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK) |
561 | #if 0 |
411 | || fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
562 | static struct timeval last; |
|
|
563 | struct timeval diff, now; |
|
|
564 | #endif |
412 | #endif |
|
|
413 | croak ("unable to initialize result pipe(3)"); |
565 | |
414 | |
566 | if (started >= wanted) |
415 | respipe_osf [0] = TO_SOCKET (respipe [0]); |
567 | return; |
416 | respipe_osf [1] = TO_SOCKET (respipe [1]); |
568 | |
|
|
569 | if (nready <= nreqs - get_nready () - get_npending ()) |
|
|
570 | return; |
|
|
571 | |
|
|
572 | #if 0 |
|
|
573 | gettimeofday (&now, 0); |
|
|
574 | |
|
|
575 | diff.tv_sec = now.tv_sec - last.tv_sec; |
|
|
576 | diff.tv_usec = now.tv_usec - last.tv_usec; |
|
|
577 | |
|
|
578 | if (diff.tv_usec < 0) |
|
|
579 | { |
|
|
580 | --diff.tv_sec; |
|
|
581 | diff.tv_usec += 1000000; |
|
|
582 | } |
|
|
583 | |
|
|
584 | if (!diff.tv_sec && diff.tv_usec < 10000) |
|
|
585 | return; |
|
|
586 | |
|
|
587 | last = now; |
|
|
588 | #endif |
|
|
589 | |
|
|
590 | start_thread (); |
|
|
591 | } |
417 | } |
592 | |
418 | |
593 | static void req_send (aio_req req) |
|
|
594 | { |
|
|
595 | ++nreqs; |
|
|
596 | |
|
|
597 | LOCK (reqlock); |
|
|
598 | ++nready; |
|
|
599 | reqq_push (&req_queue, req); |
|
|
600 | pthread_cond_signal (&reqwait); |
|
|
601 | UNLOCK (reqlock); |
|
|
602 | |
|
|
603 | maybe_start_thread (); |
|
|
604 | } |
|
|
605 | |
|
|
606 | static void end_thread (void) |
|
|
607 | { |
|
|
608 | aio_req req; |
|
|
609 | |
|
|
610 | Newz (0, req, 1, aio_cb); |
|
|
611 | |
|
|
612 | req->type = REQ_QUIT; |
|
|
613 | req->pri = PRI_MAX + PRI_BIAS; |
|
|
614 | |
|
|
615 | req_send (req); |
|
|
616 | |
|
|
617 | LOCK (wrklock); |
|
|
618 | --started; |
|
|
619 | UNLOCK (wrklock); |
|
|
620 | } |
|
|
621 | |
|
|
622 | static void min_parallel (int nthreads) |
|
|
623 | { |
|
|
624 | if (wanted < nthreads) |
|
|
625 | wanted = nthreads; |
|
|
626 | } |
|
|
627 | |
|
|
628 | static void max_parallel (int nthreads) |
|
|
629 | { |
|
|
630 | if (wanted > nthreads) |
|
|
631 | wanted = nthreads; |
|
|
632 | |
|
|
633 | while (started > wanted) |
|
|
634 | end_thread (); |
|
|
635 | } |
|
|
636 | |
|
|
637 | static void poll_wait () |
419 | static void poll_wait (void) |
638 | { |
420 | { |
639 | fd_set rfd; |
421 | fd_set rfd; |
640 | |
422 | |
641 | while (nreqs) |
423 | while (eio_nreqs ()) |
642 | { |
424 | { |
643 | int size; |
425 | int size; |
644 | if (WORDREAD_UNSAFE) LOCK (reslock); |
426 | |
|
|
427 | X_LOCK (reslock); |
645 | size = res_queue.size; |
428 | size = res_queue.size; |
646 | if (WORDREAD_UNSAFE) UNLOCK (reslock); |
429 | X_UNLOCK (reslock); |
647 | |
430 | |
648 | if (size) |
431 | if (size) |
649 | return; |
432 | return; |
650 | |
433 | |
651 | maybe_start_thread (); |
434 | etp_maybe_start_thread (); |
652 | |
435 | |
653 | FD_ZERO(&rfd); |
436 | FD_ZERO (&rfd); |
654 | FD_SET(respipe [0], &rfd); |
437 | FD_SET (respipe [0], &rfd); |
655 | |
438 | |
656 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
439 | PerlSock_select (respipe [0] + 1, &rfd, 0, 0, 0); |
657 | } |
440 | } |
658 | } |
441 | } |
659 | |
442 | |
660 | static int poll_cb (int max) |
443 | static int poll_cb (void) |
661 | { |
444 | { |
662 | dSP; |
|
|
663 | int count = 0; |
|
|
664 | int do_croak = 0; |
|
|
665 | aio_req req; |
|
|
666 | |
|
|
667 | for (;;) |
445 | for (;;) |
668 | { |
446 | { |
669 | while (max <= 0 || count < max) |
447 | int res = eio_poll (); |
670 | { |
|
|
671 | maybe_start_thread (); |
|
|
672 | |
448 | |
673 | LOCK (reslock); |
449 | if (res > 0) |
674 | req = reqq_shift (&res_queue); |
|
|
675 | |
|
|
676 | if (req) |
|
|
677 | { |
|
|
678 | --npending; |
|
|
679 | |
|
|
680 | if (!res_queue.size) |
|
|
681 | { |
|
|
682 | /* read any signals sent by the worker threads */ |
|
|
683 | char buf [32]; |
|
|
684 | while (read (respipe [0], buf, 32) == 32) |
|
|
685 | ; |
|
|
686 | } |
|
|
687 | } |
|
|
688 | |
|
|
689 | UNLOCK (reslock); |
|
|
690 | |
|
|
691 | if (!req) |
|
|
692 | break; |
|
|
693 | |
|
|
694 | --nreqs; |
|
|
695 | |
|
|
696 | if (req->type == REQ_GROUP && req->length) |
|
|
697 | { |
|
|
698 | req->fd = 1; /* mark request as delayed */ |
|
|
699 | continue; |
|
|
700 | } |
|
|
701 | else |
|
|
702 | { |
|
|
703 | if (req->type == REQ_READ) |
|
|
704 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
|
|
705 | |
|
|
706 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
707 | SvREADONLY_off (req->data); |
|
|
708 | |
|
|
709 | if (req->statdata) |
|
|
710 | { |
|
|
711 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
712 | PL_laststatval = req->result; |
|
|
713 | PL_statcache = *(req->statdata); |
|
|
714 | } |
|
|
715 | |
|
|
716 | req_invoke (req); |
|
|
717 | |
|
|
718 | count++; |
|
|
719 | } |
|
|
720 | |
|
|
721 | req_free (req); |
|
|
722 | } |
|
|
723 | |
|
|
724 | if (nreqs <= max_outstanding) |
|
|
725 | break; |
450 | croak (0); |
|
|
451 | |
|
|
452 | if (!max_outstanding || max_outstanding > eio_nreqs ()) |
|
|
453 | return res; |
726 | |
454 | |
727 | poll_wait (); |
455 | poll_wait (); |
728 | |
|
|
729 | max = 0; |
|
|
730 | } |
456 | } |
731 | |
|
|
732 | return count; |
|
|
733 | } |
457 | } |
734 | |
458 | |
735 | static void create_pipe () |
459 | static void atfork_child (void) |
736 | { |
460 | { |
737 | if (pipe (respipe)) |
461 | create_respipe (); |
738 | croak ("unable to initialize result pipe"); |
|
|
739 | |
|
|
740 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
741 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
742 | |
|
|
743 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
744 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
745 | } |
462 | } |
746 | |
463 | |
747 | /*****************************************************************************/ |
464 | static SV * |
748 | /* work around various missing functions */ |
465 | get_cb (SV *cb_sv) |
749 | |
|
|
750 | #if !HAVE_PREADWRITE |
|
|
751 | # define pread aio_pread |
|
|
752 | # define pwrite aio_pwrite |
|
|
753 | |
|
|
754 | /* |
|
|
755 | * make our pread/pwrite safe against themselves, but not against |
|
|
756 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
757 | * but that's your problem, not mine. |
|
|
758 | */ |
|
|
759 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
|
|
760 | |
|
|
761 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
|
|
762 | { |
466 | { |
763 | ssize_t res; |
467 | HV *st; |
764 | off_t ooffset; |
468 | GV *gvp; |
|
|
469 | CV *cv; |
765 | |
470 | |
766 | LOCK (preadwritelock); |
471 | if (!SvOK (cb_sv)) |
767 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
768 | lseek (fd, offset, SEEK_SET); |
|
|
769 | res = read (fd, buf, count); |
|
|
770 | lseek (fd, ooffset, SEEK_SET); |
|
|
771 | UNLOCK (preadwritelock); |
|
|
772 | |
|
|
773 | return res; |
|
|
774 | } |
|
|
775 | |
|
|
776 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
777 | { |
|
|
778 | ssize_t res; |
|
|
779 | off_t ooffset; |
|
|
780 | |
|
|
781 | LOCK (preadwritelock); |
|
|
782 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
783 | lseek (fd, offset, SEEK_SET); |
|
|
784 | res = write (fd, buf, count); |
|
|
785 | lseek (fd, offset, SEEK_SET); |
|
|
786 | UNLOCK (preadwritelock); |
|
|
787 | |
|
|
788 | return res; |
|
|
789 | } |
|
|
790 | #endif |
|
|
791 | |
|
|
792 | #if !HAVE_FDATASYNC |
|
|
793 | # define fdatasync fsync |
|
|
794 | #endif |
|
|
795 | |
|
|
796 | #if !HAVE_READAHEAD |
|
|
797 | # define readahead(fd,offset,count) aio_readahead (fd, offset, count, self) |
|
|
798 | |
|
|
799 | static ssize_t aio_readahead (int fd, off_t offset, size_t count, worker *self) |
|
|
800 | { |
|
|
801 | dBUF; |
|
|
802 | |
|
|
803 | while (count > 0) |
|
|
804 | { |
|
|
805 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
|
|
806 | |
|
|
807 | pread (fd, aio_buf, len, offset); |
|
|
808 | offset += len; |
|
|
809 | count -= len; |
|
|
810 | } |
|
|
811 | |
|
|
812 | errno = 0; |
|
|
813 | } |
|
|
814 | |
|
|
815 | #endif |
|
|
816 | |
|
|
817 | #if !HAVE_READDIR_R |
|
|
818 | # define readdir_r aio_readdir_r |
|
|
819 | |
|
|
820 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
821 | |
|
|
822 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
|
|
823 | { |
|
|
824 | struct dirent *e; |
|
|
825 | int errorno; |
|
|
826 | |
|
|
827 | LOCK (readdirlock); |
|
|
828 | |
|
|
829 | e = readdir (dirp); |
|
|
830 | errorno = errno; |
|
|
831 | |
|
|
832 | if (e) |
|
|
833 | { |
|
|
834 | *res = ent; |
|
|
835 | strcpy (ent->d_name, e->d_name); |
|
|
836 | } |
|
|
837 | else |
|
|
838 | *res = 0; |
|
|
839 | |
|
|
840 | UNLOCK (readdirlock); |
|
|
841 | |
|
|
842 | errno = errorno; |
|
|
843 | return e ? 0 : -1; |
|
|
844 | } |
|
|
845 | #endif |
|
|
846 | |
|
|
847 | /* sendfile always needs emulation */ |
|
|
848 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count, worker *self) |
|
|
849 | { |
|
|
850 | ssize_t res; |
|
|
851 | |
|
|
852 | if (!count) |
|
|
853 | return 0; |
472 | return 0; |
854 | |
473 | |
855 | #if HAVE_SENDFILE |
474 | cv = sv_2cv (cb_sv, &st, &gvp, 0); |
856 | # if __linux |
|
|
857 | res = sendfile (ofd, ifd, &offset, count); |
|
|
858 | |
475 | |
859 | # elif __freebsd |
476 | if (!cv) |
860 | /* |
477 | croak ("IO::AIO callback must be undef or a CODE reference"); |
861 | * Of course, the freebsd sendfile is a dire hack with no thoughts |
|
|
862 | * wasted on making it similar to other I/O functions. |
|
|
863 | */ |
|
|
864 | { |
|
|
865 | off_t sbytes; |
|
|
866 | res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); |
|
|
867 | |
478 | |
868 | if (res < 0 && sbytes) |
479 | return (SV *)cv; |
869 | /* maybe only on EAGAIN: as usual, the manpage leaves you guessing */ |
|
|
870 | res = sbytes; |
|
|
871 | } |
|
|
872 | |
|
|
873 | # elif __hpux |
|
|
874 | res = sendfile (ofd, ifd, offset, count, 0, 0); |
|
|
875 | |
|
|
876 | # elif __solaris |
|
|
877 | { |
|
|
878 | struct sendfilevec vec; |
|
|
879 | size_t sbytes; |
|
|
880 | |
|
|
881 | vec.sfv_fd = ifd; |
|
|
882 | vec.sfv_flag = 0; |
|
|
883 | vec.sfv_off = offset; |
|
|
884 | vec.sfv_len = count; |
|
|
885 | |
|
|
886 | res = sendfilev (ofd, &vec, 1, &sbytes); |
|
|
887 | |
|
|
888 | if (res < 0 && sbytes) |
|
|
889 | res = sbytes; |
|
|
890 | } |
|
|
891 | |
|
|
892 | # endif |
|
|
893 | #else |
|
|
894 | res = -1; |
|
|
895 | errno = ENOSYS; |
|
|
896 | #endif |
|
|
897 | |
|
|
898 | if (res < 0 |
|
|
899 | && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK |
|
|
900 | #if __solaris |
|
|
901 | || errno == EAFNOSUPPORT || errno == EPROTOTYPE |
|
|
902 | #endif |
|
|
903 | ) |
|
|
904 | ) |
|
|
905 | { |
|
|
906 | /* emulate sendfile. this is a major pain in the ass */ |
|
|
907 | dBUF; |
|
|
908 | |
|
|
909 | res = 0; |
|
|
910 | |
|
|
911 | while (count) |
|
|
912 | { |
|
|
913 | ssize_t cnt; |
|
|
914 | |
|
|
915 | cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); |
|
|
916 | |
|
|
917 | if (cnt <= 0) |
|
|
918 | { |
|
|
919 | if (cnt && !res) res = -1; |
|
|
920 | break; |
|
|
921 | } |
|
|
922 | |
|
|
923 | cnt = write (ofd, aio_buf, cnt); |
|
|
924 | |
|
|
925 | if (cnt <= 0) |
|
|
926 | { |
|
|
927 | if (cnt && !res) res = -1; |
|
|
928 | break; |
|
|
929 | } |
|
|
930 | |
|
|
931 | offset += cnt; |
|
|
932 | res += cnt; |
|
|
933 | count -= cnt; |
|
|
934 | } |
|
|
935 | } |
|
|
936 | |
|
|
937 | return res; |
|
|
938 | } |
|
|
939 | |
|
|
940 | /* read a full directory */ |
|
|
941 | static void scandir_ (aio_req req, worker *self) |
|
|
942 | { |
|
|
943 | DIR *dirp; |
|
|
944 | union |
|
|
945 | { |
|
|
946 | struct dirent d; |
|
|
947 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
|
|
948 | } *u; |
|
|
949 | struct dirent *entp; |
|
|
950 | char *name, *names; |
|
|
951 | int memlen = 4096; |
|
|
952 | int memofs = 0; |
|
|
953 | int res = 0; |
|
|
954 | int errorno; |
|
|
955 | |
|
|
956 | LOCK (wrklock); |
|
|
957 | self->dirp = dirp = opendir (req->dataptr); |
|
|
958 | self->dbuf = u = malloc (sizeof (*u)); |
|
|
959 | req->data2ptr = names = malloc (memlen); |
|
|
960 | UNLOCK (wrklock); |
|
|
961 | |
|
|
962 | if (dirp && u && names) |
|
|
963 | for (;;) |
|
|
964 | { |
|
|
965 | errno = 0; |
|
|
966 | readdir_r (dirp, &u->d, &entp); |
|
|
967 | |
|
|
968 | if (!entp) |
|
|
969 | break; |
|
|
970 | |
|
|
971 | name = entp->d_name; |
|
|
972 | |
|
|
973 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
|
|
974 | { |
|
|
975 | int len = strlen (name) + 1; |
|
|
976 | |
|
|
977 | res++; |
|
|
978 | |
|
|
979 | while (memofs + len > memlen) |
|
|
980 | { |
|
|
981 | memlen *= 2; |
|
|
982 | LOCK (wrklock); |
|
|
983 | req->data2ptr = names = realloc (names, memlen); |
|
|
984 | UNLOCK (wrklock); |
|
|
985 | |
|
|
986 | if (!names) |
|
|
987 | break; |
|
|
988 | } |
|
|
989 | |
|
|
990 | memcpy (names + memofs, name, len); |
|
|
991 | memofs += len; |
|
|
992 | } |
|
|
993 | } |
|
|
994 | |
|
|
995 | if (errno) |
|
|
996 | res = -1; |
|
|
997 | |
|
|
998 | req->result = res; |
|
|
999 | } |
|
|
1000 | |
|
|
1001 | /*****************************************************************************/ |
|
|
1002 | |
|
|
1003 | static void *aio_proc (void *thr_arg) |
|
|
1004 | { |
|
|
1005 | aio_req req; |
|
|
1006 | worker *self = (worker *)thr_arg; |
|
|
1007 | |
|
|
1008 | for (;;) |
|
|
1009 | { |
|
|
1010 | LOCK (reqlock); |
|
|
1011 | |
|
|
1012 | for (;;) |
|
|
1013 | { |
|
|
1014 | self->req = req = reqq_shift (&req_queue); |
|
|
1015 | |
|
|
1016 | if (req) |
|
|
1017 | break; |
|
|
1018 | |
|
|
1019 | pthread_cond_wait (&reqwait, &reqlock); |
|
|
1020 | } |
|
|
1021 | |
|
|
1022 | --nready; |
|
|
1023 | |
|
|
1024 | UNLOCK (reqlock); |
|
|
1025 | |
|
|
1026 | errno = 0; /* strictly unnecessary */ |
|
|
1027 | |
|
|
1028 | if (!(req->flags & FLAG_CANCELLED)) |
|
|
1029 | switch (req->type) |
|
|
1030 | { |
|
|
1031 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
1032 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
1033 | |
|
|
1034 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
1035 | case REQ_SENDFILE: req->result = sendfile_ (req->fd, req->fd2, req->offset, req->length, self); break; |
|
|
1036 | |
|
|
1037 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
|
|
1038 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
|
|
1039 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
|
|
1040 | |
|
|
1041 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
|
|
1042 | case REQ_CLOSE: req->result = close (req->fd); break; |
|
|
1043 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
1044 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
1045 | case REQ_RENAME: req->result = rename (req->data2ptr, req->dataptr); break; |
|
|
1046 | case REQ_LINK: req->result = link (req->data2ptr, req->dataptr); break; |
|
|
1047 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
|
|
1048 | case REQ_MKNOD: req->result = mknod (req->data2ptr, req->mode, (dev_t)req->offset); break; |
|
|
1049 | |
|
|
1050 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
1051 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
|
|
1052 | case REQ_READDIR: scandir_ (req, self); break; |
|
|
1053 | |
|
|
1054 | case REQ_BUSY: |
|
|
1055 | { |
|
|
1056 | struct timeval tv; |
|
|
1057 | |
|
|
1058 | tv.tv_sec = req->fd; |
|
|
1059 | tv.tv_usec = req->fd2; |
|
|
1060 | |
|
|
1061 | req->result = select (0, 0, 0, 0, &tv); |
|
|
1062 | } |
|
|
1063 | |
|
|
1064 | case REQ_GROUP: |
|
|
1065 | case REQ_NOP: |
|
|
1066 | break; |
|
|
1067 | |
|
|
1068 | case REQ_QUIT: |
|
|
1069 | LOCK (wrklock); |
|
|
1070 | worker_free (self); |
|
|
1071 | --started; |
|
|
1072 | UNLOCK (wrklock); |
|
|
1073 | return 0; |
|
|
1074 | |
|
|
1075 | default: |
|
|
1076 | req->result = ENOSYS; |
|
|
1077 | break; |
|
|
1078 | } |
|
|
1079 | |
|
|
1080 | req->errorno = errno; |
|
|
1081 | |
|
|
1082 | LOCK (reslock); |
|
|
1083 | |
|
|
1084 | ++npending; |
|
|
1085 | |
|
|
1086 | if (!reqq_push (&res_queue, req)) |
|
|
1087 | /* write a dummy byte to the pipe so fh becomes ready */ |
|
|
1088 | write (respipe [1], &respipe, 1); |
|
|
1089 | |
|
|
1090 | self->req = 0; |
|
|
1091 | worker_clear (self); |
|
|
1092 | |
|
|
1093 | UNLOCK (reslock); |
|
|
1094 | } |
|
|
1095 | } |
|
|
1096 | |
|
|
1097 | /*****************************************************************************/ |
|
|
1098 | |
|
|
1099 | static void atfork_prepare (void) |
|
|
1100 | { |
|
|
1101 | LOCK (wrklock); |
|
|
1102 | LOCK (reqlock); |
|
|
1103 | LOCK (reslock); |
|
|
1104 | #if !HAVE_PREADWRITE |
|
|
1105 | LOCK (preadwritelock); |
|
|
1106 | #endif |
|
|
1107 | #if !HAVE_READDIR_R |
|
|
1108 | LOCK (readdirlock); |
|
|
1109 | #endif |
|
|
1110 | } |
|
|
1111 | |
|
|
1112 | static void atfork_parent (void) |
|
|
1113 | { |
|
|
1114 | #if !HAVE_READDIR_R |
|
|
1115 | UNLOCK (readdirlock); |
|
|
1116 | #endif |
|
|
1117 | #if !HAVE_PREADWRITE |
|
|
1118 | UNLOCK (preadwritelock); |
|
|
1119 | #endif |
|
|
1120 | UNLOCK (reslock); |
|
|
1121 | UNLOCK (reqlock); |
|
|
1122 | UNLOCK (wrklock); |
|
|
1123 | } |
|
|
1124 | |
|
|
1125 | static void atfork_child (void) |
|
|
1126 | { |
|
|
1127 | aio_req prv; |
|
|
1128 | |
|
|
1129 | while (prv = reqq_shift (&req_queue)) |
|
|
1130 | req_free (prv); |
|
|
1131 | |
|
|
1132 | while (prv = reqq_shift (&res_queue)) |
|
|
1133 | req_free (prv); |
|
|
1134 | |
|
|
1135 | while (wrk_first.next != &wrk_first) |
|
|
1136 | { |
|
|
1137 | worker *wrk = wrk_first.next; |
|
|
1138 | |
|
|
1139 | if (wrk->req) |
|
|
1140 | req_free (wrk->req); |
|
|
1141 | |
|
|
1142 | worker_clear (wrk); |
|
|
1143 | worker_free (wrk); |
|
|
1144 | } |
|
|
1145 | |
|
|
1146 | started = 0; |
|
|
1147 | nreqs = 0; |
|
|
1148 | |
|
|
1149 | close (respipe [0]); |
|
|
1150 | close (respipe [1]); |
|
|
1151 | create_pipe (); |
|
|
1152 | |
|
|
1153 | atfork_parent (); |
|
|
1154 | } |
480 | } |
1155 | |
481 | |
1156 | #define dREQ \ |
482 | #define dREQ \ |
|
|
483 | SV *cb_cv; \ |
1157 | aio_req req; \ |
484 | aio_req req; \ |
1158 | int req_pri = next_pri; \ |
485 | int req_pri = next_pri; \ |
1159 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
486 | next_pri = EIO_PRI_DEFAULT; \ |
1160 | \ |
487 | \ |
1161 | if (SvOK (callback) && !SvROK (callback)) \ |
488 | cb_cv = get_cb (callback); \ |
1162 | croak ("callback must be undef or of reference type"); \ |
|
|
1163 | \ |
489 | \ |
1164 | Newz (0, req, 1, aio_cb); \ |
490 | Newz (0, req, 1, eio_req); \ |
1165 | if (!req) \ |
491 | if (!req) \ |
1166 | croak ("out of memory during aio_req allocation"); \ |
492 | croak ("out of memory during eio_req allocation"); \ |
1167 | \ |
493 | \ |
1168 | req->callback = newSVsv (callback); \ |
494 | req->callback = SvREFCNT_inc (cb_cv); \ |
1169 | req->pri = req_pri |
495 | req->pri = req_pri |
1170 | |
496 | |
1171 | #define REQ_SEND \ |
497 | #define REQ_SEND \ |
|
|
498 | PUTBACK; \ |
1172 | req_send (req); \ |
499 | req_submit (req); \ |
|
|
500 | SPAGAIN; \ |
1173 | \ |
501 | \ |
1174 | if (GIMME_V != G_VOID) \ |
502 | if (GIMME_V != G_VOID) \ |
1175 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
503 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
1176 | |
504 | |
1177 | MODULE = IO::AIO PACKAGE = IO::AIO |
505 | MODULE = IO::AIO PACKAGE = IO::AIO |
1178 | |
506 | |
1179 | PROTOTYPES: ENABLE |
507 | PROTOTYPES: ENABLE |
1180 | |
508 | |
1181 | BOOT: |
509 | BOOT: |
1182 | { |
510 | { |
1183 | HV *stash = gv_stashpv ("IO::AIO", 1); |
511 | stash = gv_stashpv ("IO::AIO", 1); |
1184 | |
512 | |
1185 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
513 | newCONSTSUB (stash, "EXDEV", newSViv (EXDEV)); |
1186 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
514 | newCONSTSUB (stash, "O_RDONLY", newSViv (O_RDONLY)); |
1187 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
515 | newCONSTSUB (stash, "O_WRONLY", newSViv (O_WRONLY)); |
1188 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
516 | newCONSTSUB (stash, "O_CREAT", newSViv (O_CREAT)); |
1189 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
517 | newCONSTSUB (stash, "O_TRUNC", newSViv (O_TRUNC)); |
|
|
518 | #ifndef _WIN32 |
1190 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
519 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
|
|
520 | #endif |
|
|
521 | newCONSTSUB (stash, "S_IFIFO", newSViv (S_IFIFO)); |
|
|
522 | newCONSTSUB (stash, "SYNC_FILE_RANGE_WAIT_BEFORE", newSViv (EIO_SYNC_FILE_RANGE_WAIT_BEFORE)); |
|
|
523 | newCONSTSUB (stash, "SYNC_FILE_RANGE_WRITE" , newSViv (EIO_SYNC_FILE_RANGE_WRITE)); |
|
|
524 | newCONSTSUB (stash, "SYNC_FILE_RANGE_WAIT_AFTER" , newSViv (EIO_SYNC_FILE_RANGE_WAIT_AFTER)); |
1191 | |
525 | |
1192 | create_pipe (); |
526 | create_respipe (); |
1193 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
|
|
1194 | |
527 | |
1195 | start_thread (); |
528 | if (eio_init (want_poll, done_poll) < 0) |
|
|
529 | croak ("IO::AIO: unable to initialise eio library"); |
|
|
530 | |
|
|
531 | /* atfork child called in fifo order, so before eio's handler */ |
|
|
532 | X_THREAD_ATFORK (0, 0, atfork_child); |
1196 | } |
533 | } |
|
|
534 | |
|
|
535 | void |
|
|
536 | max_poll_reqs (int nreqs) |
|
|
537 | PROTOTYPE: $ |
|
|
538 | CODE: |
|
|
539 | eio_set_max_poll_reqs (nreqs); |
|
|
540 | |
|
|
541 | void |
|
|
542 | max_poll_time (double nseconds) |
|
|
543 | PROTOTYPE: $ |
|
|
544 | CODE: |
|
|
545 | eio_set_max_poll_time (nseconds); |
1197 | |
546 | |
1198 | void |
547 | void |
1199 | min_parallel (int nthreads) |
548 | min_parallel (int nthreads) |
1200 | PROTOTYPE: $ |
549 | PROTOTYPE: $ |
|
|
550 | CODE: |
|
|
551 | eio_set_min_parallel (nthreads); |
1201 | |
552 | |
1202 | void |
553 | void |
1203 | max_parallel (int nthreads) |
554 | max_parallel (int nthreads) |
1204 | PROTOTYPE: $ |
555 | PROTOTYPE: $ |
|
|
556 | CODE: |
|
|
557 | eio_set_max_parallel (nthreads); |
1205 | |
558 | |
1206 | int |
559 | void |
|
|
560 | max_idle (int nthreads) |
|
|
561 | PROTOTYPE: $ |
|
|
562 | CODE: |
|
|
563 | eio_set_max_idle (nthreads); |
|
|
564 | |
|
|
565 | void |
1207 | max_outstanding (int maxreqs) |
566 | max_outstanding (int maxreqs) |
1208 | PROTOTYPE: $ |
567 | PROTOTYPE: $ |
1209 | CODE: |
568 | CODE: |
1210 | RETVAL = max_outstanding; |
|
|
1211 | max_outstanding = maxreqs; |
569 | max_outstanding = maxreqs; |
1212 | OUTPUT: |
|
|
1213 | RETVAL |
|
|
1214 | |
570 | |
1215 | void |
571 | void |
1216 | aio_open (pathname,flags,mode,callback=&PL_sv_undef) |
572 | aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) |
1217 | SV * pathname |
|
|
1218 | int flags |
|
|
1219 | int mode |
|
|
1220 | SV * callback |
|
|
1221 | PROTOTYPE: $$$;$ |
573 | PROTOTYPE: $$$;$ |
1222 | PPCODE: |
574 | PPCODE: |
1223 | { |
575 | { |
1224 | dREQ; |
576 | dREQ; |
1225 | |
577 | |
1226 | req->type = REQ_OPEN; |
578 | req->type = EIO_OPEN; |
1227 | req->data = newSVsv (pathname); |
579 | req->sv1 = newSVsv (pathname); |
1228 | req->dataptr = SvPVbyte_nolen (req->data); |
580 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1229 | req->fd = flags; |
581 | req->int1 = flags; |
1230 | req->mode = mode; |
582 | req->int2 = mode; |
1231 | |
583 | |
1232 | REQ_SEND; |
584 | REQ_SEND; |
1233 | } |
585 | } |
1234 | |
586 | |
1235 | void |
587 | void |
1236 | aio_close (fh,callback=&PL_sv_undef) |
588 | aio_fsync (SV *fh, SV *callback=&PL_sv_undef) |
1237 | SV * fh |
|
|
1238 | SV * callback |
|
|
1239 | PROTOTYPE: $;$ |
589 | PROTOTYPE: $;$ |
1240 | ALIAS: |
590 | ALIAS: |
1241 | aio_close = REQ_CLOSE |
|
|
1242 | aio_fsync = REQ_FSYNC |
591 | aio_fsync = EIO_FSYNC |
1243 | aio_fdatasync = REQ_FDATASYNC |
592 | aio_fdatasync = EIO_FDATASYNC |
1244 | PPCODE: |
593 | PPCODE: |
1245 | { |
594 | { |
1246 | dREQ; |
595 | dREQ; |
1247 | |
596 | |
1248 | req->type = ix; |
597 | req->type = ix; |
1249 | req->fh = newSVsv (fh); |
598 | req->sv1 = newSVsv (fh); |
1250 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
599 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1251 | |
600 | |
1252 | REQ_SEND (req); |
601 | REQ_SEND (req); |
1253 | } |
602 | } |
1254 | |
603 | |
1255 | void |
604 | void |
|
|
605 | aio_sync_file_range (SV *fh, SV *offset, SV *nbytes, IV flags, SV *callback=&PL_sv_undef) |
|
|
606 | PROTOTYPE: $$$$;$ |
|
|
607 | PPCODE: |
|
|
608 | { |
|
|
609 | dREQ; |
|
|
610 | |
|
|
611 | req->type = EIO_SYNC_FILE_RANGE; |
|
|
612 | req->sv1 = newSVsv (fh); |
|
|
613 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
|
|
614 | req->offs = SvVAL64 (offset); |
|
|
615 | req->size = SvVAL64 (nbytes); |
|
|
616 | req->int2 = flags; |
|
|
617 | |
|
|
618 | REQ_SEND (req); |
|
|
619 | } |
|
|
620 | |
|
|
621 | void |
|
|
622 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
|
|
623 | PROTOTYPE: $;$ |
|
|
624 | PPCODE: |
|
|
625 | { |
|
|
626 | static int close_pipe = -1; /* dummy fd to close fds via dup2 */ |
|
|
627 | dREQ; |
|
|
628 | |
|
|
629 | if (close_pipe < 0) |
|
|
630 | { |
|
|
631 | int pipefd [2]; |
|
|
632 | |
|
|
633 | if (pipe (pipefd) < 0 |
|
|
634 | || close (pipefd [1]) < 0 |
|
|
635 | || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0) |
|
|
636 | abort (); /*D*/ |
|
|
637 | |
|
|
638 | close_pipe = pipefd [0]; |
|
|
639 | } |
|
|
640 | |
|
|
641 | req->type = EIO_DUP2; |
|
|
642 | req->int1 = close_pipe; |
|
|
643 | req->sv2 = newSVsv (fh); |
|
|
644 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
|
|
645 | |
|
|
646 | REQ_SEND (req); |
|
|
647 | } |
|
|
648 | |
|
|
649 | void |
1256 | aio_read (fh,offset,length,data,dataoffset,callback=&PL_sv_undef) |
650 | aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) |
1257 | SV * fh |
|
|
1258 | UV offset |
|
|
1259 | UV length |
|
|
1260 | SV * data |
|
|
1261 | UV dataoffset |
|
|
1262 | SV * callback |
|
|
1263 | ALIAS: |
651 | ALIAS: |
1264 | aio_read = REQ_READ |
652 | aio_read = EIO_READ |
1265 | aio_write = REQ_WRITE |
653 | aio_write = EIO_WRITE |
1266 | PROTOTYPE: $$$$$;$ |
654 | PROTOTYPE: $$$$$;$ |
1267 | PPCODE: |
655 | PPCODE: |
1268 | { |
656 | { |
1269 | aio_req req; |
|
|
1270 | STRLEN svlen; |
657 | STRLEN svlen; |
1271 | char *svptr = SvPVbyte (data, svlen); |
658 | char *svptr = SvPVbyte (data, svlen); |
|
|
659 | UV len = SvUV (length); |
|
|
660 | int fd; |
|
|
661 | |
|
|
662 | if (SvTYPE (data) > SVt_PVMG) |
|
|
663 | croak ("illegal data argument '%s', must be plain scalar string", SvPV_nolen (data)); |
1272 | |
664 | |
1273 | SvUPGRADE (data, SVt_PV); |
665 | SvUPGRADE (data, SVt_PV); |
1274 | SvPOK_on (data); |
666 | SvPOK_only (data); |
1275 | |
667 | |
1276 | if (dataoffset < 0) |
668 | if (dataoffset < 0) |
1277 | dataoffset += svlen; |
669 | dataoffset += svlen; |
1278 | |
670 | |
1279 | if (dataoffset < 0 || dataoffset > svlen) |
671 | if (dataoffset < 0 || dataoffset > svlen) |
1280 | croak ("data offset outside of string"); |
672 | croak ("dataoffset outside of data scalar"); |
1281 | |
673 | |
1282 | if (ix == REQ_WRITE) |
674 | if (ix == EIO_WRITE) |
1283 | { |
675 | { |
1284 | /* write: check length and adjust. */ |
676 | /* write: check length and adjust. */ |
1285 | if (length < 0 || length + dataoffset > svlen) |
677 | if (!SvOK (length) || len + dataoffset > svlen) |
1286 | length = svlen - dataoffset; |
678 | len = svlen - dataoffset; |
1287 | } |
679 | } |
1288 | else |
680 | else |
1289 | { |
681 | { |
1290 | /* read: grow scalar as necessary */ |
682 | /* read: grow scalar as necessary */ |
1291 | svptr = SvGROW (data, length + dataoffset); |
683 | svptr = SvGROW (data, len + dataoffset + 1); |
1292 | } |
684 | } |
1293 | |
685 | |
1294 | if (length < 0) |
686 | if (len < 0) |
1295 | croak ("length must not be negative"); |
687 | croak ("length must not be negative"); |
|
|
688 | |
|
|
689 | fd = PerlIO_fileno (ix == EIO_READ ? IoIFP (sv_2io (fh)) |
|
|
690 | : IoOFP (sv_2io (fh))); |
|
|
691 | |
|
|
692 | if (fd < 0) |
|
|
693 | croak ("illegal fh argument, either not an OS file or read/write mode mismatch"); |
1296 | |
694 | |
1297 | { |
695 | { |
1298 | dREQ; |
696 | dREQ; |
1299 | |
697 | |
1300 | req->type = ix; |
698 | req->type = ix; |
1301 | req->fh = newSVsv (fh); |
699 | req->sv1 = newSVsv (fh); |
1302 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
700 | req->int1 = df; |
1303 | : IoOFP (sv_2io (fh))); |
701 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
1304 | req->offset = offset; |
702 | req->size = len; |
1305 | req->length = length; |
|
|
1306 | req->data = SvREFCNT_inc (data); |
703 | req->sv2 = SvREFCNT_inc (data); |
1307 | req->dataptr = (char *)svptr + dataoffset; |
704 | req->ptr2 = (char *)svptr + dataoffset; |
|
|
705 | req->stroffset = dataoffset; |
1308 | |
706 | |
1309 | if (!SvREADONLY (data)) |
707 | if (!SvREADONLY (data)) |
1310 | { |
708 | { |
1311 | SvREADONLY_on (data); |
709 | SvREADONLY_on (data); |
1312 | req->data2ptr = (void *)data; |
710 | req->flags |= FLAG_SV2_RO_OFF; |
1313 | } |
711 | } |
1314 | |
712 | |
1315 | REQ_SEND; |
713 | REQ_SEND; |
1316 | } |
714 | } |
1317 | } |
715 | } |
1318 | |
716 | |
1319 | void |
717 | void |
|
|
718 | aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) |
|
|
719 | PROTOTYPE: $$;$ |
|
|
720 | PPCODE: |
|
|
721 | { |
|
|
722 | SV *data; |
|
|
723 | dREQ; |
|
|
724 | |
|
|
725 | req->type = EIO_READLINK; |
|
|
726 | req->sv1 = newSVsv (path); |
|
|
727 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
728 | |
|
|
729 | REQ_SEND; |
|
|
730 | } |
|
|
731 | |
|
|
732 | void |
1320 | aio_sendfile (out_fh,in_fh,in_offset,length,callback=&PL_sv_undef) |
733 | aio_sendfile (SV *out_fh, SV *in_fh, SV *in_offset, UV length, SV *callback=&PL_sv_undef) |
1321 | SV * out_fh |
|
|
1322 | SV * in_fh |
|
|
1323 | UV in_offset |
|
|
1324 | UV length |
|
|
1325 | SV * callback |
|
|
1326 | PROTOTYPE: $$$$;$ |
734 | PROTOTYPE: $$$$;$ |
1327 | PPCODE: |
735 | PPCODE: |
1328 | { |
736 | { |
1329 | dREQ; |
737 | dREQ; |
1330 | |
738 | |
1331 | req->type = REQ_SENDFILE; |
739 | req->type = EIO_SENDFILE; |
1332 | req->fh = newSVsv (out_fh); |
740 | req->sv1 = newSVsv (out_fh); |
1333 | req->fd = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
741 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (out_fh))); |
1334 | req->fh2 = newSVsv (in_fh); |
742 | req->sv2 = newSVsv (in_fh); |
1335 | req->fd2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
743 | req->int2 = PerlIO_fileno (IoIFP (sv_2io (in_fh))); |
1336 | req->offset = in_offset; |
744 | req->offs = SvVAL64 (in_offset); |
1337 | req->length = length; |
745 | req->size = length; |
1338 | |
746 | |
1339 | REQ_SEND; |
747 | REQ_SEND; |
1340 | } |
748 | } |
1341 | |
749 | |
1342 | void |
750 | void |
1343 | aio_readahead (fh,offset,length,callback=&PL_sv_undef) |
751 | aio_readahead (SV *fh, SV *offset, IV length, SV *callback=&PL_sv_undef) |
1344 | SV * fh |
|
|
1345 | UV offset |
|
|
1346 | IV length |
|
|
1347 | SV * callback |
|
|
1348 | PROTOTYPE: $$$;$ |
752 | PROTOTYPE: $$$;$ |
1349 | PPCODE: |
753 | PPCODE: |
1350 | { |
754 | { |
1351 | dREQ; |
755 | dREQ; |
1352 | |
756 | |
1353 | req->type = REQ_READAHEAD; |
757 | req->type = EIO_READAHEAD; |
1354 | req->fh = newSVsv (fh); |
758 | req->sv1 = newSVsv (fh); |
1355 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
759 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh))); |
1356 | req->offset = offset; |
760 | req->offs = SvVAL64 (offset); |
1357 | req->length = length; |
761 | req->size = length; |
1358 | |
762 | |
1359 | REQ_SEND; |
763 | REQ_SEND; |
1360 | } |
764 | } |
1361 | |
765 | |
1362 | void |
766 | void |
1363 | aio_stat (fh_or_path,callback=&PL_sv_undef) |
767 | aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) |
1364 | SV * fh_or_path |
|
|
1365 | SV * callback |
|
|
1366 | ALIAS: |
768 | ALIAS: |
1367 | aio_stat = REQ_STAT |
769 | aio_stat = EIO_STAT |
1368 | aio_lstat = REQ_LSTAT |
770 | aio_lstat = EIO_LSTAT |
1369 | PPCODE: |
771 | PPCODE: |
1370 | { |
772 | { |
1371 | dREQ; |
773 | dREQ; |
1372 | |
774 | |
1373 | New (0, req->statdata, 1, Stat_t); |
775 | req->sv1 = newSVsv (fh_or_path); |
1374 | if (!req->statdata) |
|
|
1375 | { |
|
|
1376 | req_free (req); |
|
|
1377 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
1378 | } |
|
|
1379 | |
776 | |
1380 | if (SvPOK (fh_or_path)) |
777 | if (SvPOK (req->sv1)) |
1381 | { |
778 | { |
1382 | req->type = ix; |
779 | req->type = ix; |
1383 | req->data = newSVsv (fh_or_path); |
|
|
1384 | req->dataptr = SvPVbyte_nolen (req->data); |
780 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1385 | } |
781 | } |
1386 | else |
782 | else |
1387 | { |
783 | { |
1388 | req->type = REQ_FSTAT; |
784 | req->type = EIO_FSTAT; |
1389 | req->fh = newSVsv (fh_or_path); |
|
|
1390 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
785 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
1391 | } |
786 | } |
1392 | |
787 | |
1393 | REQ_SEND; |
788 | REQ_SEND; |
1394 | } |
789 | } |
1395 | |
790 | |
1396 | void |
791 | void |
|
|
792 | aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef) |
|
|
793 | PPCODE: |
|
|
794 | { |
|
|
795 | dREQ; |
|
|
796 | |
|
|
797 | req->nv1 = SvOK (atime) ? SvNV (atime) : -1.; |
|
|
798 | req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; |
|
|
799 | req->sv1 = newSVsv (fh_or_path); |
|
|
800 | |
|
|
801 | if (SvPOK (req->sv1)) |
|
|
802 | { |
|
|
803 | req->type = EIO_UTIME; |
|
|
804 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
805 | } |
|
|
806 | else |
|
|
807 | { |
|
|
808 | req->type = EIO_FUTIME; |
|
|
809 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
810 | } |
|
|
811 | |
|
|
812 | REQ_SEND; |
|
|
813 | } |
|
|
814 | |
|
|
815 | void |
|
|
816 | aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) |
|
|
817 | PPCODE: |
|
|
818 | { |
|
|
819 | dREQ; |
|
|
820 | |
|
|
821 | req->sv1 = newSVsv (fh_or_path); |
|
|
822 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
|
|
823 | |
|
|
824 | if (SvPOK (req->sv1)) |
|
|
825 | { |
|
|
826 | req->type = EIO_TRUNCATE; |
|
|
827 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
828 | } |
|
|
829 | else |
|
|
830 | { |
|
|
831 | req->type = EIO_FTRUNCATE; |
|
|
832 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
833 | } |
|
|
834 | |
|
|
835 | REQ_SEND; |
|
|
836 | } |
|
|
837 | |
|
|
838 | void |
|
|
839 | aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) |
|
|
840 | ALIAS: |
|
|
841 | aio_chmod = EIO_CHMOD |
|
|
842 | aio_mkdir = EIO_MKDIR |
|
|
843 | PPCODE: |
|
|
844 | { |
|
|
845 | dREQ; |
|
|
846 | |
|
|
847 | req->int2 = mode; |
|
|
848 | req->sv1 = newSVsv (fh_or_path); |
|
|
849 | |
|
|
850 | if (SvPOK (req->sv1)) |
|
|
851 | { |
|
|
852 | req->type = ix; |
|
|
853 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
854 | } |
|
|
855 | else |
|
|
856 | { |
|
|
857 | req->type = EIO_FCHMOD; |
|
|
858 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
859 | } |
|
|
860 | |
|
|
861 | REQ_SEND; |
|
|
862 | } |
|
|
863 | |
|
|
864 | void |
|
|
865 | aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef) |
|
|
866 | PPCODE: |
|
|
867 | { |
|
|
868 | dREQ; |
|
|
869 | |
|
|
870 | req->int2 = SvOK (uid) ? SvIV (uid) : -1; |
|
|
871 | req->int3 = SvOK (gid) ? SvIV (gid) : -1; |
|
|
872 | req->sv1 = newSVsv (fh_or_path); |
|
|
873 | |
|
|
874 | if (SvPOK (req->sv1)) |
|
|
875 | { |
|
|
876 | req->type = EIO_CHOWN; |
|
|
877 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
878 | } |
|
|
879 | else |
|
|
880 | { |
|
|
881 | req->type = EIO_FCHOWN; |
|
|
882 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
883 | } |
|
|
884 | |
|
|
885 | REQ_SEND; |
|
|
886 | } |
|
|
887 | |
|
|
888 | void |
1397 | aio_unlink (pathname,callback=&PL_sv_undef) |
889 | aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) |
1398 | SV * pathname |
|
|
1399 | SV * callback |
|
|
1400 | ALIAS: |
890 | ALIAS: |
1401 | aio_unlink = REQ_UNLINK |
891 | aio_unlink = EIO_UNLINK |
1402 | aio_rmdir = REQ_RMDIR |
892 | aio_rmdir = EIO_RMDIR |
1403 | aio_readdir = REQ_READDIR |
893 | aio_readdir = EIO_READDIR |
1404 | PPCODE: |
894 | PPCODE: |
1405 | { |
895 | { |
1406 | dREQ; |
896 | dREQ; |
1407 | |
897 | |
1408 | req->type = ix; |
898 | req->type = ix; |
1409 | req->data = newSVsv (pathname); |
899 | req->sv1 = newSVsv (pathname); |
1410 | req->dataptr = SvPVbyte_nolen (req->data); |
900 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
901 | |
|
|
902 | REQ_SEND; |
|
|
903 | } |
|
|
904 | |
|
|
905 | void |
|
|
906 | aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef) |
|
|
907 | ALIAS: |
|
|
908 | aio_link = EIO_LINK |
|
|
909 | aio_symlink = EIO_SYMLINK |
|
|
910 | aio_rename = EIO_RENAME |
|
|
911 | PPCODE: |
|
|
912 | { |
|
|
913 | dREQ; |
|
|
914 | |
|
|
915 | req->type = ix; |
|
|
916 | req->sv1 = newSVsv (oldpath); |
|
|
917 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
918 | req->sv2 = newSVsv (newpath); |
|
|
919 | req->ptr2 = SvPVbyte_nolen (req->sv2); |
1411 | |
920 | |
1412 | REQ_SEND; |
921 | REQ_SEND; |
1413 | } |
922 | } |
1414 | |
923 | |
1415 | void |
924 | void |
1416 | aio_link (oldpath,newpath,callback=&PL_sv_undef) |
925 | aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef) |
1417 | SV * oldpath |
|
|
1418 | SV * newpath |
|
|
1419 | SV * callback |
|
|
1420 | ALIAS: |
|
|
1421 | aio_link = REQ_LINK |
|
|
1422 | aio_symlink = REQ_SYMLINK |
|
|
1423 | aio_rename = REQ_RENAME |
|
|
1424 | PPCODE: |
926 | PPCODE: |
1425 | { |
927 | { |
1426 | dREQ; |
928 | dREQ; |
1427 | |
929 | |
1428 | req->type = ix; |
930 | req->type = EIO_MKNOD; |
1429 | req->fh = newSVsv (oldpath); |
931 | req->sv1 = newSVsv (pathname); |
1430 | req->data2ptr = SvPVbyte_nolen (req->fh); |
932 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
1431 | req->data = newSVsv (newpath); |
933 | req->int2 = (mode_t)mode; |
1432 | req->dataptr = SvPVbyte_nolen (req->data); |
934 | req->offs = dev; |
1433 | |
935 | |
1434 | REQ_SEND; |
936 | REQ_SEND; |
1435 | } |
937 | } |
1436 | |
938 | |
1437 | void |
939 | void |
1438 | aio_mknod (pathname,mode,dev,callback=&PL_sv_undef) |
940 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
1439 | SV * pathname |
|
|
1440 | SV * callback |
|
|
1441 | UV mode |
|
|
1442 | UV dev |
|
|
1443 | PPCODE: |
941 | PPCODE: |
1444 | { |
942 | { |
1445 | dREQ; |
943 | dREQ; |
1446 | |
944 | |
1447 | req->type = REQ_MKNOD; |
945 | req->type = EIO_BUSY; |
1448 | req->data = newSVsv (pathname); |
946 | req->nv1 = delay < 0. ? 0. : delay; |
1449 | req->dataptr = SvPVbyte_nolen (req->data); |
947 | |
1450 | req->mode = (mode_t)mode; |
|
|
1451 | req->offset = dev; |
|
|
1452 | |
|
|
1453 | REQ_SEND; |
948 | REQ_SEND; |
1454 | } |
949 | } |
1455 | |
950 | |
1456 | void |
951 | void |
1457 | aio_busy (delay,callback=&PL_sv_undef) |
|
|
1458 | double delay |
|
|
1459 | SV * callback |
|
|
1460 | PPCODE: |
|
|
1461 | { |
|
|
1462 | dREQ; |
|
|
1463 | |
|
|
1464 | req->type = REQ_BUSY; |
|
|
1465 | req->fd = delay < 0. ? 0 : delay; |
|
|
1466 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
|
|
1467 | |
|
|
1468 | REQ_SEND; |
|
|
1469 | } |
|
|
1470 | |
|
|
1471 | void |
|
|
1472 | aio_group (callback=&PL_sv_undef) |
952 | aio_group (SV *callback=&PL_sv_undef) |
1473 | SV * callback |
|
|
1474 | PROTOTYPE: ;$ |
953 | PROTOTYPE: ;$ |
1475 | PPCODE: |
954 | PPCODE: |
1476 | { |
955 | { |
1477 | dREQ; |
956 | dREQ; |
1478 | |
957 | |
1479 | req->type = REQ_GROUP; |
958 | req->type = EIO_GROUP; |
|
|
959 | |
1480 | req_send (req); |
960 | req_submit (req); |
1481 | |
|
|
1482 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
961 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1483 | } |
962 | } |
1484 | |
963 | |
1485 | void |
964 | void |
1486 | aio_nop (callback=&PL_sv_undef) |
965 | aio_nop (SV *callback=&PL_sv_undef) |
1487 | SV * callback |
966 | ALIAS: |
|
|
967 | aio_nop = EIO_NOP |
|
|
968 | aio_sync = EIO_SYNC |
1488 | PPCODE: |
969 | PPCODE: |
1489 | { |
970 | { |
1490 | dREQ; |
971 | dREQ; |
1491 | |
972 | |
1492 | req->type = REQ_NOP; |
973 | req->type = ix; |
1493 | |
974 | |
1494 | REQ_SEND; |
975 | REQ_SEND; |
1495 | } |
976 | } |
1496 | |
977 | |
1497 | int |
978 | int |
1498 | aioreq_pri (int pri = 0) |
979 | aioreq_pri (int pri = 0) |
1499 | PROTOTYPE: ;$ |
980 | PROTOTYPE: ;$ |
1500 | CODE: |
981 | CODE: |
1501 | RETVAL = next_pri - PRI_BIAS; |
982 | RETVAL = next_pri; |
1502 | if (items > 0) |
983 | if (items > 0) |
1503 | { |
984 | { |
1504 | if (pri < PRI_MIN) pri = PRI_MIN; |
985 | if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN; |
1505 | if (pri > PRI_MAX) pri = PRI_MAX; |
986 | if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX; |
1506 | next_pri = pri + PRI_BIAS; |
987 | next_pri = pri; |
1507 | } |
988 | } |
1508 | OUTPUT: |
989 | OUTPUT: |
1509 | RETVAL |
990 | RETVAL |
1510 | |
991 | |
1511 | void |
992 | void |
1512 | aioreq_nice (int nice = 0) |
993 | aioreq_nice (int nice = 0) |
1513 | CODE: |
994 | CODE: |
1514 | nice = next_pri - nice; |
995 | nice = next_pri - nice; |
1515 | if (nice < PRI_MIN) nice = PRI_MIN; |
996 | if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN; |
1516 | if (nice > PRI_MAX) nice = PRI_MAX; |
997 | if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX; |
1517 | next_pri = nice + PRI_BIAS; |
998 | next_pri = nice; |
1518 | |
999 | |
1519 | void |
1000 | void |
1520 | flush () |
1001 | flush () |
1521 | PROTOTYPE: |
1002 | PROTOTYPE: |
1522 | CODE: |
1003 | CODE: |
1523 | while (nreqs) |
1004 | while (eio_nreqs ()) |
1524 | { |
1005 | { |
1525 | poll_wait (); |
1006 | poll_wait (); |
1526 | poll_cb (0); |
1007 | poll_cb (); |
1527 | } |
1008 | } |
1528 | |
1009 | |
1529 | void |
1010 | int |
1530 | poll() |
1011 | poll() |
1531 | PROTOTYPE: |
1012 | PROTOTYPE: |
1532 | CODE: |
1013 | CODE: |
1533 | if (nreqs) |
|
|
1534 | { |
|
|
1535 | poll_wait (); |
1014 | poll_wait (); |
1536 | poll_cb (0); |
1015 | RETVAL = poll_cb (); |
1537 | } |
1016 | OUTPUT: |
|
|
1017 | RETVAL |
1538 | |
1018 | |
1539 | int |
1019 | int |
1540 | poll_fileno() |
1020 | poll_fileno() |
1541 | PROTOTYPE: |
1021 | PROTOTYPE: |
1542 | CODE: |
1022 | CODE: |
… | |
… | |
1546 | |
1026 | |
1547 | int |
1027 | int |
1548 | poll_cb(...) |
1028 | poll_cb(...) |
1549 | PROTOTYPE: |
1029 | PROTOTYPE: |
1550 | CODE: |
1030 | CODE: |
1551 | RETVAL = poll_cb (0); |
1031 | RETVAL = poll_cb (); |
1552 | OUTPUT: |
1032 | OUTPUT: |
1553 | RETVAL |
1033 | RETVAL |
1554 | |
1034 | |
1555 | int |
|
|
1556 | poll_some(int max = 0) |
|
|
1557 | PROTOTYPE: $ |
|
|
1558 | CODE: |
|
|
1559 | RETVAL = poll_cb (max); |
|
|
1560 | OUTPUT: |
|
|
1561 | RETVAL |
|
|
1562 | |
|
|
1563 | void |
1035 | void |
1564 | poll_wait() |
1036 | poll_wait() |
1565 | PROTOTYPE: |
1037 | PROTOTYPE: |
1566 | CODE: |
1038 | CODE: |
1567 | if (nreqs) |
|
|
1568 | poll_wait (); |
1039 | poll_wait (); |
1569 | |
1040 | |
1570 | int |
1041 | int |
1571 | nreqs() |
1042 | nreqs() |
1572 | PROTOTYPE: |
1043 | PROTOTYPE: |
1573 | CODE: |
1044 | CODE: |
1574 | RETVAL = nreqs; |
1045 | RETVAL = eio_nreqs (); |
1575 | OUTPUT: |
1046 | OUTPUT: |
1576 | RETVAL |
1047 | RETVAL |
1577 | |
1048 | |
1578 | int |
1049 | int |
1579 | nready() |
1050 | nready() |
1580 | PROTOTYPE: |
1051 | PROTOTYPE: |
1581 | CODE: |
1052 | CODE: |
1582 | RETVAL = get_nready (); |
1053 | RETVAL = eio_nready (); |
1583 | OUTPUT: |
1054 | OUTPUT: |
1584 | RETVAL |
1055 | RETVAL |
1585 | |
1056 | |
1586 | int |
1057 | int |
1587 | npending() |
1058 | npending() |
1588 | PROTOTYPE: |
1059 | PROTOTYPE: |
1589 | CODE: |
1060 | CODE: |
1590 | RETVAL = get_npending (); |
1061 | RETVAL = eio_npending (); |
1591 | OUTPUT: |
1062 | OUTPUT: |
1592 | RETVAL |
1063 | RETVAL |
1593 | |
1064 | |
|
|
1065 | int |
|
|
1066 | nthreads() |
|
|
1067 | PROTOTYPE: |
|
|
1068 | CODE: |
|
|
1069 | RETVAL = eio_nthreads (); |
|
|
1070 | OUTPUT: |
|
|
1071 | RETVAL |
|
|
1072 | |
|
|
1073 | void _on_next_submit (SV *cb) |
|
|
1074 | CODE: |
|
|
1075 | SvREFCNT_dec (on_next_submit); |
|
|
1076 | on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1077 | |
1594 | PROTOTYPES: DISABLE |
1078 | PROTOTYPES: DISABLE |
1595 | |
1079 | |
1596 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1080 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1597 | |
1081 | |
1598 | void |
1082 | void |
1599 | cancel (aio_req_ornot req) |
1083 | cancel (aio_req_ornot req) |
1600 | CODE: |
1084 | CODE: |
1601 | req_cancel (req); |
1085 | eio_cancel (req); |
1602 | |
1086 | |
1603 | void |
1087 | void |
1604 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
1088 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
1605 | CODE: |
1089 | PPCODE: |
|
|
1090 | { |
|
|
1091 | if (GIMME_V != G_VOID) |
|
|
1092 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
|
|
1093 | |
|
|
1094 | if (items > 1) |
|
|
1095 | { |
|
|
1096 | SV *cb_cv = get_cb (callback); |
|
|
1097 | |
1606 | SvREFCNT_dec (req->callback); |
1098 | SvREFCNT_dec (req->callback); |
1607 | req->callback = newSVsv (callback); |
1099 | req->callback = SvREFCNT_inc (cb_cv); |
|
|
1100 | } |
|
|
1101 | } |
1608 | |
1102 | |
1609 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1103 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
1610 | |
1104 | |
1611 | void |
1105 | void |
1612 | add (aio_req grp, ...) |
1106 | add (aio_req grp, ...) |
1613 | PPCODE: |
1107 | PPCODE: |
1614 | { |
1108 | { |
1615 | int i; |
1109 | int i; |
1616 | aio_req req; |
|
|
1617 | |
1110 | |
1618 | if (grp->fd == 2) |
1111 | if (grp->int1 == 2) |
1619 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1112 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
1620 | |
1113 | |
1621 | for (i = 1; i < items; ++i ) |
1114 | for (i = 1; i < items; ++i ) |
1622 | { |
1115 | { |
|
|
1116 | aio_req req; |
|
|
1117 | |
1623 | if (GIMME_V != G_VOID) |
1118 | if (GIMME_V != G_VOID) |
1624 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1119 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
1625 | |
1120 | |
1626 | req = SvAIO_REQ (ST (i)); |
1121 | req = SvAIO_REQ (ST (i)); |
1627 | |
1122 | |
1628 | if (req) |
1123 | if (req) |
1629 | { |
1124 | eio_grp_add (grp, req); |
1630 | ++grp->length; |
|
|
1631 | req->grp = grp; |
|
|
1632 | |
|
|
1633 | req->grp_prev = 0; |
|
|
1634 | req->grp_next = grp->grp_first; |
|
|
1635 | |
|
|
1636 | if (grp->grp_first) |
|
|
1637 | grp->grp_first->grp_prev = req; |
|
|
1638 | |
|
|
1639 | grp->grp_first = req; |
|
|
1640 | } |
|
|
1641 | } |
1125 | } |
1642 | } |
1126 | } |
1643 | |
1127 | |
1644 | void |
1128 | void |
1645 | cancel_subs (aio_req_ornot req) |
1129 | cancel_subs (aio_req_ornot req) |
… | |
… | |
1658 | av = newAV (); |
1142 | av = newAV (); |
1659 | |
1143 | |
1660 | for (i = 1; i < items; ++i ) |
1144 | for (i = 1; i < items; ++i ) |
1661 | av_push (av, newSVsv (ST (i))); |
1145 | av_push (av, newSVsv (ST (i))); |
1662 | |
1146 | |
1663 | SvREFCNT_dec (grp->data); |
1147 | SvREFCNT_dec (grp->sv1); |
1664 | grp->data = (SV *)av; |
1148 | grp->sv1 = (SV *)av; |
1665 | } |
1149 | } |
1666 | |
1150 | |
1667 | void |
1151 | void |
1668 | errno (aio_req grp, int errorno = errno) |
1152 | errno (aio_req grp, int errorno = errno) |
1669 | CODE: |
1153 | CODE: |
1670 | grp->errorno = errorno; |
1154 | grp->errorno = errorno; |
1671 | |
1155 | |
1672 | void |
1156 | void |
1673 | limit (aio_req grp, int limit) |
1157 | limit (aio_req grp, int limit) |
1674 | CODE: |
1158 | CODE: |
1675 | grp->fd2 = limit; |
1159 | eio_grp_limit (grp, limit); |
1676 | aio_grp_feed (grp); |
|
|
1677 | |
1160 | |
1678 | void |
1161 | void |
1679 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1162 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
1680 | CODE: |
1163 | CODE: |
1681 | { |
1164 | { |
1682 | SvREFCNT_dec (grp->fh2); |
1165 | SvREFCNT_dec (grp->sv2); |
1683 | grp->fh2 = newSVsv (callback); |
1166 | grp->sv2 = newSVsv (callback); |
|
|
1167 | grp->feed = aio_grp_feed; |
1684 | |
1168 | |
1685 | if (grp->fd2 <= 0) |
1169 | if (grp->int2 <= 0) |
1686 | grp->fd2 = 2; |
1170 | grp->int2 = 2; |
1687 | |
1171 | |
1688 | aio_grp_feed (grp); |
1172 | eio_grp_limit (grp, grp->int2); |
1689 | } |
1173 | } |
1690 | |
1174 | |