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