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