|
|
1 | #if __linux |
|
|
2 | # define _GNU_SOURCE |
|
|
3 | #endif |
|
|
4 | |
1 | #define _REENTRANT 1 |
5 | #define _REENTRANT 1 |
|
|
6 | |
2 | #include <errno.h> |
7 | #include <errno.h> |
3 | |
8 | |
4 | #include "EXTERN.h" |
9 | #include "EXTERN.h" |
5 | #include "perl.h" |
10 | #include "perl.h" |
6 | #include "XSUB.h" |
11 | #include "XSUB.h" |
… | |
… | |
9 | |
14 | |
10 | #include <pthread.h> |
15 | #include <pthread.h> |
11 | |
16 | |
12 | #include <stddef.h> |
17 | #include <stddef.h> |
13 | #include <errno.h> |
18 | #include <errno.h> |
|
|
19 | #include <sys/time.h> |
|
|
20 | #include <sys/select.h> |
14 | #include <sys/types.h> |
21 | #include <sys/types.h> |
15 | #include <sys/stat.h> |
22 | #include <sys/stat.h> |
16 | #include <limits.h> |
23 | #include <limits.h> |
17 | #include <unistd.h> |
24 | #include <unistd.h> |
18 | #include <fcntl.h> |
25 | #include <fcntl.h> |
… | |
… | |
53 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
60 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
54 | REQ_FSYNC, REQ_FDATASYNC, |
61 | REQ_FSYNC, REQ_FDATASYNC, |
55 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
62 | REQ_UNLINK, REQ_RMDIR, REQ_RENAME, |
56 | REQ_READDIR, |
63 | REQ_READDIR, |
57 | REQ_LINK, REQ_SYMLINK, |
64 | REQ_LINK, REQ_SYMLINK, |
58 | REQ_GROUP, |
65 | REQ_GROUP, REQ_NOP, |
|
|
66 | REQ_SLEEP, |
59 | }; |
67 | }; |
60 | |
68 | |
61 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
69 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
62 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
70 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
63 | |
71 | |
64 | typedef struct aio_cb |
72 | typedef struct aio_cb |
65 | { |
73 | { |
66 | struct aio_cb *grp, *grp_prev, *grp_next; |
|
|
67 | |
|
|
68 | struct aio_cb *volatile next; |
74 | struct aio_cb *volatile next; |
69 | |
|
|
70 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
71 | |
75 | |
72 | SV *data, *callback; |
76 | SV *data, *callback; |
73 | SV *fh, *fh2; |
77 | SV *fh, *fh2; |
74 | void *dataptr, *data2ptr; |
78 | void *dataptr, *data2ptr; |
75 | Stat_t *statdata; |
79 | Stat_t *statdata; |
76 | off_t offset; |
80 | off_t offset; |
77 | size_t length; |
81 | size_t length; |
78 | ssize_t result; |
82 | ssize_t result; |
79 | |
83 | |
|
|
84 | STRLEN dataoffset; |
80 | int type; |
85 | int type; |
81 | int fd, fd2; |
86 | int fd, fd2; |
82 | int errorno; |
87 | int errorno; |
83 | STRLEN dataoffset; |
|
|
84 | mode_t mode; /* open */ |
88 | mode_t mode; /* open */ |
|
|
89 | |
85 | unsigned char cancelled; |
90 | unsigned char flags; |
|
|
91 | unsigned char pri; |
|
|
92 | |
|
|
93 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
94 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
86 | } aio_cb; |
95 | } aio_cb; |
|
|
96 | |
|
|
97 | enum { |
|
|
98 | FLAG_CANCELLED = 0x01, |
|
|
99 | }; |
87 | |
100 | |
88 | typedef aio_cb *aio_req; |
101 | typedef aio_cb *aio_req; |
89 | typedef aio_cb *aio_req_ornot; |
102 | typedef aio_cb *aio_req_ornot; |
90 | typedef aio_cb *aio_group; |
103 | |
|
|
104 | enum { |
|
|
105 | PRI_MIN = -4, |
|
|
106 | PRI_MAX = 4, |
|
|
107 | |
|
|
108 | DEFAULT_PRI = 0, |
|
|
109 | PRI_BIAS = -PRI_MIN, |
|
|
110 | }; |
|
|
111 | |
|
|
112 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
91 | |
113 | |
92 | static int started, wanted; |
114 | static int started, wanted; |
93 | static volatile int nreqs; |
115 | static volatile int nreqs; |
94 | static int max_outstanding = 1<<30; |
116 | static int max_outstanding = 1<<30; |
95 | static int respipe [2]; |
117 | static int respipe [2]; |
96 | |
118 | |
|
|
119 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
|
|
120 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
|
|
121 | #else |
|
|
122 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
123 | #endif |
|
|
124 | |
97 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
125 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
98 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
126 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
99 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
127 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
100 | |
128 | |
101 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
129 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
102 | static volatile aio_req ress, rese; /* queue start, queue end */ |
130 | static volatile aio_req ress, rese; /* queue start, queue end */ |
103 | |
131 | |
|
|
132 | static void req_invoke (aio_req req); |
|
|
133 | static void req_free (aio_req req); |
|
|
134 | |
104 | /* must be called at most once */ |
135 | /* must be called at most once */ |
105 | static SV *req_sv (aio_req req, const char *klass) |
136 | static SV *req_sv (aio_req req, const char *klass) |
106 | { |
137 | { |
|
|
138 | if (!req->self) |
|
|
139 | { |
107 | req->self = (SV *)newHV (); |
140 | req->self = (SV *)newHV (); |
108 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
141 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
142 | } |
109 | |
143 | |
110 | return sv_2mortal (sv_bless (newRV_noinc (req->self), gv_stashpv (klass, 1))); |
144 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
111 | } |
145 | } |
112 | |
146 | |
113 | static aio_req SvAIO_REQ (SV *sv, const char *klass) |
147 | static aio_req SvAIO_REQ (SV *sv) |
114 | { |
148 | { |
|
|
149 | MAGIC *mg; |
|
|
150 | |
115 | if (!sv_derived_from (sv, klass) || !SvROK (sv)) |
151 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
116 | croak ("object of class %s expected", klass); |
152 | croak ("object of class " AIO_REQ_KLASS " expected"); |
117 | |
153 | |
118 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
154 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
119 | |
155 | |
120 | return mg ? (aio_req)mg->mg_ptr : 0; |
156 | return mg ? (aio_req)mg->mg_ptr : 0; |
121 | } |
157 | } |
122 | |
158 | |
|
|
159 | static void aio_grp_feed (aio_req grp) |
|
|
160 | { |
|
|
161 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
|
|
162 | { |
|
|
163 | int old_len = grp->length; |
|
|
164 | |
|
|
165 | if (grp->fh2 && SvOK (grp->fh2)) |
|
|
166 | { |
|
|
167 | dSP; |
|
|
168 | |
|
|
169 | ENTER; |
|
|
170 | SAVETMPS; |
|
|
171 | PUSHMARK (SP); |
|
|
172 | XPUSHs (req_sv (grp, AIO_GRP_KLASS)); |
|
|
173 | PUTBACK; |
|
|
174 | call_sv (grp->fh2, G_VOID | G_EVAL); |
|
|
175 | SPAGAIN; |
|
|
176 | FREETMPS; |
|
|
177 | LEAVE; |
|
|
178 | } |
|
|
179 | |
|
|
180 | /* stop if no progress has been made */ |
|
|
181 | if (old_len == grp->length) |
|
|
182 | { |
|
|
183 | SvREFCNT_dec (grp->fh2); |
|
|
184 | grp->fh2 = 0; |
|
|
185 | break; |
|
|
186 | } |
|
|
187 | } |
|
|
188 | } |
|
|
189 | |
|
|
190 | static void aio_grp_dec (aio_req grp) |
|
|
191 | { |
|
|
192 | --grp->length; |
|
|
193 | |
|
|
194 | /* call feeder, if applicable */ |
|
|
195 | aio_grp_feed (grp); |
|
|
196 | |
|
|
197 | /* finish, if done */ |
|
|
198 | if (!grp->length && grp->fd) |
|
|
199 | { |
|
|
200 | req_invoke (grp); |
|
|
201 | req_free (grp); |
|
|
202 | } |
|
|
203 | } |
|
|
204 | |
|
|
205 | static void poll_wait () |
|
|
206 | { |
|
|
207 | fd_set rfd; |
|
|
208 | |
|
|
209 | while (nreqs) |
|
|
210 | { |
|
|
211 | aio_req req; |
|
|
212 | #if !(__x86 || __x86_64) /* safe without sempahore on this archs */ |
|
|
213 | pthread_mutex_lock (&reslock); |
|
|
214 | #endif |
|
|
215 | req = ress; |
|
|
216 | #if !(__x86 || __x86_64) /* safe without sempahore on this archs */ |
|
|
217 | pthread_mutex_unlock (&reslock); |
|
|
218 | #endif |
|
|
219 | |
|
|
220 | if (req) |
|
|
221 | return; |
|
|
222 | |
|
|
223 | FD_ZERO(&rfd); |
|
|
224 | FD_SET(respipe [0], &rfd); |
|
|
225 | |
|
|
226 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
227 | } |
|
|
228 | } |
|
|
229 | |
|
|
230 | static void req_invoke (aio_req req) |
|
|
231 | { |
|
|
232 | dSP; |
|
|
233 | int errorno = errno; |
|
|
234 | |
|
|
235 | if (req->flags & FLAG_CANCELLED || !SvOK (req->callback)) |
|
|
236 | return; |
|
|
237 | |
|
|
238 | errno = req->errorno; |
|
|
239 | |
|
|
240 | ENTER; |
|
|
241 | SAVETMPS; |
|
|
242 | PUSHMARK (SP); |
|
|
243 | EXTEND (SP, 1); |
|
|
244 | |
|
|
245 | switch (req->type) |
|
|
246 | { |
|
|
247 | case REQ_READDIR: |
|
|
248 | { |
|
|
249 | SV *rv = &PL_sv_undef; |
|
|
250 | |
|
|
251 | if (req->result >= 0) |
|
|
252 | { |
|
|
253 | char *buf = req->data2ptr; |
|
|
254 | AV *av = newAV (); |
|
|
255 | |
|
|
256 | while (req->result) |
|
|
257 | { |
|
|
258 | SV *sv = newSVpv (buf, 0); |
|
|
259 | |
|
|
260 | av_push (av, sv); |
|
|
261 | buf += SvCUR (sv) + 1; |
|
|
262 | req->result--; |
|
|
263 | } |
|
|
264 | |
|
|
265 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
266 | } |
|
|
267 | |
|
|
268 | PUSHs (rv); |
|
|
269 | } |
|
|
270 | break; |
|
|
271 | |
|
|
272 | case REQ_OPEN: |
|
|
273 | { |
|
|
274 | /* convert fd to fh */ |
|
|
275 | SV *fh; |
|
|
276 | |
|
|
277 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
278 | PUTBACK; |
|
|
279 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
280 | SPAGAIN; |
|
|
281 | |
|
|
282 | fh = SvREFCNT_inc (POPs); |
|
|
283 | |
|
|
284 | PUSHMARK (SP); |
|
|
285 | XPUSHs (sv_2mortal (fh)); |
|
|
286 | } |
|
|
287 | break; |
|
|
288 | |
|
|
289 | case REQ_GROUP: |
|
|
290 | req->fd = 2; /* mark group as finished */ |
|
|
291 | |
|
|
292 | if (req->data) |
|
|
293 | { |
|
|
294 | int i; |
|
|
295 | AV *av = (AV *)req->data; |
|
|
296 | |
|
|
297 | EXTEND (SP, AvFILL (av) + 1); |
|
|
298 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
299 | PUSHs (*av_fetch (av, i, 0)); |
|
|
300 | } |
|
|
301 | break; |
|
|
302 | |
|
|
303 | case REQ_NOP: |
|
|
304 | case REQ_SLEEP: |
|
|
305 | break; |
|
|
306 | |
|
|
307 | default: |
|
|
308 | PUSHs (sv_2mortal (newSViv (req->result))); |
|
|
309 | break; |
|
|
310 | } |
|
|
311 | |
|
|
312 | |
|
|
313 | PUTBACK; |
|
|
314 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
315 | SPAGAIN; |
|
|
316 | |
|
|
317 | FREETMPS; |
|
|
318 | LEAVE; |
|
|
319 | |
|
|
320 | errno = errorno; |
|
|
321 | |
|
|
322 | if (SvTRUE (ERRSV)) |
|
|
323 | { |
|
|
324 | req_free (req); |
|
|
325 | croak (0); |
|
|
326 | } |
|
|
327 | } |
|
|
328 | |
123 | static void req_free (aio_req req) |
329 | static void req_free (aio_req req) |
124 | { |
330 | { |
|
|
331 | if (req->grp) |
|
|
332 | { |
|
|
333 | aio_req grp = req->grp; |
|
|
334 | |
|
|
335 | /* unlink request */ |
|
|
336 | if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; |
|
|
337 | if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; |
|
|
338 | |
|
|
339 | if (grp->grp_first == req) |
|
|
340 | grp->grp_first = req->grp_next; |
|
|
341 | |
|
|
342 | aio_grp_dec (grp); |
|
|
343 | } |
|
|
344 | |
125 | if (req->self) |
345 | if (req->self) |
126 | { |
346 | { |
127 | sv_unmagic (req->self, PERL_MAGIC_ext); |
347 | sv_unmagic (req->self, PERL_MAGIC_ext); |
128 | SvREFCNT_dec (req->self); |
348 | SvREFCNT_dec (req->self); |
129 | } |
349 | } |
130 | |
350 | |
131 | if (req->data) |
|
|
132 | SvREFCNT_dec (req->data); |
351 | SvREFCNT_dec (req->data); |
133 | |
|
|
134 | if (req->fh) |
|
|
135 | SvREFCNT_dec (req->fh); |
352 | SvREFCNT_dec (req->fh); |
136 | |
|
|
137 | if (req->fh2) |
|
|
138 | SvREFCNT_dec (req->fh2); |
353 | SvREFCNT_dec (req->fh2); |
139 | |
|
|
140 | if (req->statdata) |
|
|
141 | Safefree (req->statdata); |
|
|
142 | |
|
|
143 | if (req->callback) |
|
|
144 | SvREFCNT_dec (req->callback); |
354 | SvREFCNT_dec (req->callback); |
|
|
355 | Safefree (req->statdata); |
145 | |
356 | |
146 | if (req->type == REQ_READDIR && req->result >= 0) |
357 | if (req->type == REQ_READDIR && req->result >= 0) |
147 | free (req->data2ptr); |
358 | free (req->data2ptr); |
148 | |
359 | |
149 | Safefree (req); |
360 | Safefree (req); |
150 | } |
361 | } |
151 | |
362 | |
152 | static void |
363 | static void req_cancel (aio_req req) |
153 | poll_wait () |
|
|
154 | { |
364 | { |
155 | if (nreqs && !ress) |
365 | req->flags |= FLAG_CANCELLED; |
156 | { |
|
|
157 | fd_set rfd; |
|
|
158 | FD_ZERO(&rfd); |
|
|
159 | FD_SET(respipe [0], &rfd); |
|
|
160 | |
366 | |
161 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
367 | if (req->type == REQ_GROUP) |
162 | } |
368 | { |
163 | } |
369 | aio_req sub; |
164 | |
370 | |
165 | static int |
371 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
166 | poll_cb () |
372 | req_cancel (sub); |
|
|
373 | } |
|
|
374 | } |
|
|
375 | |
|
|
376 | static int poll_cb () |
167 | { |
377 | { |
168 | dSP; |
378 | dSP; |
169 | int count = 0; |
379 | int count = 0; |
170 | int do_croak = 0; |
380 | int do_croak = 0; |
171 | aio_req req; |
381 | aio_req req; |
… | |
… | |
193 | pthread_mutex_unlock (&reslock); |
403 | pthread_mutex_unlock (&reslock); |
194 | |
404 | |
195 | if (!req) |
405 | if (!req) |
196 | break; |
406 | break; |
197 | |
407 | |
198 | nreqs--; |
408 | --nreqs; |
199 | |
409 | |
200 | if (req->type == REQ_QUIT) |
410 | if (req->type == REQ_QUIT) |
201 | started--; |
411 | started--; |
|
|
412 | else if (req->type == REQ_GROUP && req->length) |
|
|
413 | { |
|
|
414 | req->fd = 1; /* mark request as delayed */ |
|
|
415 | continue; |
|
|
416 | } |
202 | else |
417 | else |
203 | { |
418 | { |
204 | int errorno = errno; |
|
|
205 | errno = req->errorno; |
|
|
206 | |
|
|
207 | if (req->type == REQ_READ) |
419 | if (req->type == REQ_READ) |
208 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
420 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
209 | |
421 | |
210 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
422 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
211 | SvREADONLY_off (req->data); |
423 | SvREADONLY_off (req->data); |
… | |
… | |
215 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
427 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
216 | PL_laststatval = req->result; |
428 | PL_laststatval = req->result; |
217 | PL_statcache = *(req->statdata); |
429 | PL_statcache = *(req->statdata); |
218 | } |
430 | } |
219 | |
431 | |
220 | ENTER; |
432 | req_invoke (req); |
221 | PUSHMARK (SP); |
|
|
222 | |
433 | |
223 | if (req->type == REQ_READDIR) |
|
|
224 | { |
|
|
225 | SV *rv = &PL_sv_undef; |
|
|
226 | |
|
|
227 | if (req->result >= 0) |
|
|
228 | { |
|
|
229 | char *buf = req->data2ptr; |
|
|
230 | AV *av = newAV (); |
|
|
231 | |
|
|
232 | while (req->result) |
|
|
233 | { |
|
|
234 | SV *sv = newSVpv (buf, 0); |
|
|
235 | |
|
|
236 | av_push (av, sv); |
|
|
237 | buf += SvCUR (sv) + 1; |
|
|
238 | req->result--; |
|
|
239 | } |
|
|
240 | |
|
|
241 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
242 | } |
|
|
243 | |
|
|
244 | XPUSHs (rv); |
|
|
245 | } |
|
|
246 | else |
|
|
247 | { |
|
|
248 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
249 | |
|
|
250 | if (req->type == REQ_OPEN) |
|
|
251 | { |
|
|
252 | /* convert fd to fh */ |
|
|
253 | SV *fh; |
|
|
254 | |
|
|
255 | PUTBACK; |
|
|
256 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
257 | SPAGAIN; |
|
|
258 | |
|
|
259 | fh = SvREFCNT_inc (POPs); |
|
|
260 | |
|
|
261 | PUSHMARK (SP); |
|
|
262 | XPUSHs (sv_2mortal (fh)); |
|
|
263 | } |
|
|
264 | } |
|
|
265 | |
|
|
266 | if (SvOK (req->callback) && !req->cancelled) |
|
|
267 | { |
|
|
268 | PUTBACK; |
|
|
269 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
270 | SPAGAIN; |
|
|
271 | |
|
|
272 | if (SvTRUE (ERRSV)) |
|
|
273 | { |
|
|
274 | req_free (req); |
|
|
275 | croak (0); |
|
|
276 | } |
|
|
277 | } |
|
|
278 | |
|
|
279 | LEAVE; |
|
|
280 | |
|
|
281 | errno = errorno; |
|
|
282 | count++; |
434 | count++; |
283 | } |
435 | } |
284 | |
436 | |
285 | req_free (req); |
437 | req_free (req); |
286 | } |
438 | } |
… | |
… | |
288 | return count; |
440 | return count; |
289 | } |
441 | } |
290 | |
442 | |
291 | static void *aio_proc(void *arg); |
443 | static void *aio_proc(void *arg); |
292 | |
444 | |
293 | static void |
|
|
294 | start_thread (void) |
445 | static void start_thread (void) |
295 | { |
446 | { |
296 | sigset_t fullsigset, oldsigset; |
447 | sigset_t fullsigset, oldsigset; |
297 | pthread_t tid; |
448 | pthread_t tid; |
298 | pthread_attr_t attr; |
449 | pthread_attr_t attr; |
299 | |
450 | |
… | |
… | |
308 | started++; |
459 | started++; |
309 | |
460 | |
310 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
461 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
311 | } |
462 | } |
312 | |
463 | |
313 | static void |
|
|
314 | req_send (aio_req req) |
464 | static void req_send (aio_req req) |
315 | { |
465 | { |
316 | while (started < wanted && nreqs >= started) |
466 | while (started < wanted && nreqs >= started) |
317 | start_thread (); |
467 | start_thread (); |
318 | |
468 | |
319 | nreqs++; |
469 | ++nreqs; |
320 | |
470 | |
321 | pthread_mutex_lock (&reqlock); |
471 | pthread_mutex_lock (&reqlock); |
322 | |
472 | |
323 | req->next = 0; |
473 | req->next = 0; |
324 | |
474 | |
… | |
… | |
343 | |
493 | |
344 | poll_wait (); |
494 | poll_wait (); |
345 | } |
495 | } |
346 | } |
496 | } |
347 | |
497 | |
348 | static void |
498 | static void end_thread (void) |
349 | end_thread (void) |
|
|
350 | { |
499 | { |
351 | aio_req req; |
500 | aio_req req; |
352 | Newz (0, req, 1, aio_cb); |
501 | Newz (0, req, 1, aio_cb); |
353 | req->type = REQ_QUIT; |
502 | req->type = REQ_QUIT; |
354 | |
503 | |
… | |
… | |
405 | * normal read/write by using a mutex. slows down execution a lot, |
554 | * normal read/write by using a mutex. slows down execution a lot, |
406 | * but that's your problem, not mine. |
555 | * but that's your problem, not mine. |
407 | */ |
556 | */ |
408 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
557 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
409 | |
558 | |
410 | static ssize_t |
559 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
411 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
412 | { |
560 | { |
413 | ssize_t res; |
561 | ssize_t res; |
414 | off_t ooffset; |
562 | off_t ooffset; |
415 | |
563 | |
416 | pthread_mutex_lock (&preadwritelock); |
564 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
421 | pthread_mutex_unlock (&preadwritelock); |
569 | pthread_mutex_unlock (&preadwritelock); |
422 | |
570 | |
423 | return res; |
571 | return res; |
424 | } |
572 | } |
425 | |
573 | |
426 | static ssize_t |
|
|
427 | pwrite (int fd, void *buf, size_t count, off_t offset) |
574 | static ssize_t pwrite (int fd, void *buf, size_t count, off_t offset) |
428 | { |
575 | { |
429 | ssize_t res; |
576 | ssize_t res; |
430 | off_t ooffset; |
577 | off_t ooffset; |
431 | |
578 | |
432 | pthread_mutex_lock (&preadwritelock); |
579 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
445 | #endif |
592 | #endif |
446 | |
593 | |
447 | #if !HAVE_READAHEAD |
594 | #if !HAVE_READAHEAD |
448 | # define readahead aio_readahead |
595 | # define readahead aio_readahead |
449 | |
596 | |
450 | static ssize_t |
|
|
451 | readahead (int fd, off_t offset, size_t count) |
597 | static ssize_t readahead (int fd, off_t offset, size_t count) |
452 | { |
598 | { |
453 | char readahead_buf[4096]; |
599 | char readahead_buf[4096]; |
454 | |
600 | |
455 | while (count > 0) |
601 | while (count > 0) |
456 | { |
602 | { |
… | |
… | |
468 | #if !HAVE_READDIR_R |
614 | #if !HAVE_READDIR_R |
469 | # define readdir_r aio_readdir_r |
615 | # define readdir_r aio_readdir_r |
470 | |
616 | |
471 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
617 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
472 | |
618 | |
473 | static int |
|
|
474 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
619 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
475 | { |
620 | { |
476 | struct dirent *e; |
621 | struct dirent *e; |
477 | int errorno; |
622 | int errorno; |
478 | |
623 | |
479 | pthread_mutex_lock (&readdirlock); |
624 | pthread_mutex_lock (&readdirlock); |
… | |
… | |
495 | return e ? 0 : -1; |
640 | return e ? 0 : -1; |
496 | } |
641 | } |
497 | #endif |
642 | #endif |
498 | |
643 | |
499 | /* sendfile always needs emulation */ |
644 | /* sendfile always needs emulation */ |
500 | static ssize_t |
|
|
501 | sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
645 | static ssize_t sendfile_ (int ofd, int ifd, off_t offset, size_t count) |
502 | { |
646 | { |
503 | ssize_t res; |
647 | ssize_t res; |
504 | |
648 | |
505 | if (!count) |
649 | if (!count) |
506 | return 0; |
650 | return 0; |
… | |
… | |
588 | |
732 | |
589 | return res; |
733 | return res; |
590 | } |
734 | } |
591 | |
735 | |
592 | /* read a full directory */ |
736 | /* read a full directory */ |
593 | static int |
|
|
594 | scandir_ (const char *path, void **namesp) |
737 | static int scandir_ (const char *path, void **namesp) |
595 | { |
738 | { |
596 | DIR *dirp = opendir (path); |
739 | DIR *dirp = opendir (path); |
597 | union |
740 | union |
598 | { |
741 | { |
599 | struct dirent d; |
742 | struct dirent d; |
… | |
… | |
653 | return res; |
796 | return res; |
654 | } |
797 | } |
655 | |
798 | |
656 | /*****************************************************************************/ |
799 | /*****************************************************************************/ |
657 | |
800 | |
658 | static void * |
|
|
659 | aio_proc (void *thr_arg) |
801 | static void *aio_proc (void *thr_arg) |
660 | { |
802 | { |
661 | aio_req req; |
803 | aio_req req; |
662 | int type; |
804 | int type; |
663 | |
805 | |
664 | do |
806 | do |
… | |
… | |
682 | } |
824 | } |
683 | |
825 | |
684 | pthread_mutex_unlock (&reqlock); |
826 | pthread_mutex_unlock (&reqlock); |
685 | |
827 | |
686 | errno = 0; /* strictly unnecessary */ |
828 | errno = 0; /* strictly unnecessary */ |
|
|
829 | type = req->type; /* remember type for QUIT check */ |
687 | |
830 | |
688 | if (!req->cancelled) |
831 | if (!(req->flags & FLAG_CANCELLED)) |
689 | switch (req->type) |
832 | switch (type) |
690 | { |
833 | { |
691 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
834 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
692 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
835 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
693 | |
836 | |
694 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
837 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
… | |
… | |
708 | |
851 | |
709 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
852 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
710 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
853 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
711 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
854 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
712 | |
855 | |
|
|
856 | case REQ_SLEEP: |
|
|
857 | { |
|
|
858 | struct timeval tv; |
|
|
859 | |
|
|
860 | tv.tv_sec = req->fd; |
|
|
861 | tv.tv_usec = req->fd2; |
|
|
862 | |
|
|
863 | req->result = select (0, 0, 0, 0, &tv); |
|
|
864 | } |
|
|
865 | |
|
|
866 | case REQ_GROUP: |
|
|
867 | case REQ_NOP: |
713 | case REQ_QUIT: |
868 | case REQ_QUIT: |
714 | break; |
869 | break; |
715 | |
870 | |
716 | default: |
871 | default: |
717 | req->result = ENOSYS; |
872 | req->result = ENOSYS; |
… | |
… | |
801 | atfork_parent (); |
956 | atfork_parent (); |
802 | } |
957 | } |
803 | |
958 | |
804 | #define dREQ \ |
959 | #define dREQ \ |
805 | aio_req req; \ |
960 | aio_req req; \ |
|
|
961 | int req_pri = next_pri; \ |
|
|
962 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
806 | \ |
963 | \ |
807 | if (SvOK (callback) && !SvROK (callback)) \ |
964 | if (SvOK (callback) && !SvROK (callback)) \ |
808 | croak ("callback must be undef or of reference type"); \ |
965 | croak ("callback must be undef or of reference type"); \ |
809 | \ |
966 | \ |
810 | Newz (0, req, 1, aio_cb); \ |
967 | Newz (0, req, 1, aio_cb); \ |
811 | if (!req) \ |
968 | if (!req) \ |
812 | croak ("out of memory during aio_req allocation"); \ |
969 | croak ("out of memory during aio_req allocation"); \ |
813 | \ |
970 | \ |
814 | req->callback = newSVsv (callback) |
971 | req->callback = newSVsv (callback); \ |
|
|
972 | req->pri = req_pri |
815 | |
973 | |
816 | #define REQ_SEND \ |
974 | #define REQ_SEND \ |
817 | req_send (req); \ |
975 | req_send (req); \ |
818 | \ |
976 | \ |
819 | if (GIMME_V != G_VOID) \ |
977 | if (GIMME_V != G_VOID) \ |
… | |
… | |
1073 | |
1231 | |
1074 | REQ_SEND; |
1232 | REQ_SEND; |
1075 | } |
1233 | } |
1076 | |
1234 | |
1077 | void |
1235 | void |
|
|
1236 | aio_sleep (delay,callback=&PL_sv_undef) |
|
|
1237 | double delay |
|
|
1238 | SV * callback |
|
|
1239 | PPCODE: |
|
|
1240 | { |
|
|
1241 | dREQ; |
|
|
1242 | |
|
|
1243 | req->type = REQ_SLEEP; |
|
|
1244 | req->fd = delay < 0. ? 0 : delay; |
|
|
1245 | req->fd2 = delay < 0. ? 0 : 1000. * (delay - req->fd); |
|
|
1246 | |
|
|
1247 | REQ_SEND; |
|
|
1248 | } |
|
|
1249 | |
|
|
1250 | void |
1078 | aio_group (callback=&PL_sv_undef) |
1251 | aio_group (callback=&PL_sv_undef) |
1079 | SV * callback |
1252 | SV * callback |
1080 | PROTOTYPE: ;& |
1253 | PROTOTYPE: ;$ |
1081 | PPCODE: |
1254 | PPCODE: |
1082 | { |
1255 | { |
1083 | dREQ; |
1256 | dREQ; |
|
|
1257 | |
1084 | req->type = REQ_GROUP; |
1258 | req->type = REQ_GROUP; |
|
|
1259 | req_send (req); |
|
|
1260 | |
1085 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1261 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1086 | } |
1262 | } |
|
|
1263 | |
|
|
1264 | void |
|
|
1265 | aio_nop (callback=&PL_sv_undef) |
|
|
1266 | SV * callback |
|
|
1267 | PPCODE: |
|
|
1268 | { |
|
|
1269 | dREQ; |
|
|
1270 | |
|
|
1271 | req->type = REQ_NOP; |
|
|
1272 | |
|
|
1273 | REQ_SEND; |
|
|
1274 | } |
|
|
1275 | |
|
|
1276 | #if 0 |
|
|
1277 | |
|
|
1278 | void |
|
|
1279 | aio_pri (int pri = DEFAULT_PRI) |
|
|
1280 | CODE: |
|
|
1281 | if (pri < PRI_MIN) pri = PRI_MIN; |
|
|
1282 | if (pri > PRI_MAX) pri = PRI_MAX; |
|
|
1283 | next_pri = pri + PRI_BIAS; |
|
|
1284 | |
|
|
1285 | #endif |
1087 | |
1286 | |
1088 | void |
1287 | void |
1089 | flush () |
1288 | flush () |
1090 | PROTOTYPE: |
1289 | PROTOTYPE: |
1091 | CODE: |
1290 | CODE: |
… | |
… | |
1134 | CODE: |
1333 | CODE: |
1135 | RETVAL = nreqs; |
1334 | RETVAL = nreqs; |
1136 | OUTPUT: |
1335 | OUTPUT: |
1137 | RETVAL |
1336 | RETVAL |
1138 | |
1337 | |
|
|
1338 | PROTOTYPES: DISABLE |
|
|
1339 | |
1139 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1340 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1140 | |
1341 | |
1141 | void |
1342 | void |
1142 | cancel (aio_req_ornot req) |
1343 | cancel (aio_req_ornot req) |
1143 | PROTOTYPE: |
1344 | PROTOTYPE: |
1144 | CODE: |
1345 | CODE: |
1145 | req->cancelled = 1; |
1346 | req_cancel (req); |
1146 | |
1347 | |
|
|
1348 | void |
|
|
1349 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
|
|
1350 | CODE: |
|
|
1351 | SvREFCNT_dec (req->callback); |
|
|
1352 | req->callback = newSVsv (callback); |
|
|
1353 | |
|
|
1354 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
|
|
1355 | |
|
|
1356 | void |
|
|
1357 | add (aio_req grp, ...) |
|
|
1358 | PPCODE: |
|
|
1359 | { |
|
|
1360 | int i; |
|
|
1361 | aio_req req; |
|
|
1362 | |
|
|
1363 | if (grp->fd == 2) |
|
|
1364 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
|
|
1365 | |
|
|
1366 | for (i = 1; i < items; ++i ) |
|
|
1367 | { |
|
|
1368 | if (GIMME_V != G_VOID) |
|
|
1369 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
|
|
1370 | |
|
|
1371 | req = SvAIO_REQ (ST (i)); |
|
|
1372 | |
|
|
1373 | if (req) |
|
|
1374 | { |
|
|
1375 | ++grp->length; |
|
|
1376 | req->grp = grp; |
|
|
1377 | |
|
|
1378 | req->grp_prev = 0; |
|
|
1379 | req->grp_next = grp->grp_first; |
|
|
1380 | |
|
|
1381 | if (grp->grp_first) |
|
|
1382 | grp->grp_first->grp_prev = req; |
|
|
1383 | |
|
|
1384 | grp->grp_first = req; |
|
|
1385 | } |
|
|
1386 | } |
|
|
1387 | } |
|
|
1388 | |
|
|
1389 | void |
|
|
1390 | result (aio_req grp, ...) |
|
|
1391 | CODE: |
|
|
1392 | { |
|
|
1393 | int i; |
|
|
1394 | AV *av = newAV (); |
|
|
1395 | |
|
|
1396 | for (i = 1; i < items; ++i ) |
|
|
1397 | av_push (av, newSVsv (ST (i))); |
|
|
1398 | |
|
|
1399 | SvREFCNT_dec (grp->data); |
|
|
1400 | grp->data = (SV *)av; |
|
|
1401 | } |
|
|
1402 | |
|
|
1403 | void |
|
|
1404 | feed_limit (aio_req grp, int limit) |
|
|
1405 | CODE: |
|
|
1406 | grp->fd2 = limit; |
|
|
1407 | aio_grp_feed (grp); |
|
|
1408 | |
|
|
1409 | void |
|
|
1410 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
|
|
1411 | CODE: |
|
|
1412 | { |
|
|
1413 | SvREFCNT_dec (grp->fh2); |
|
|
1414 | grp->fh2 = newSVsv (callback); |
|
|
1415 | |
|
|
1416 | if (grp->fd2 <= 0) |
|
|
1417 | grp->fd2 = 2; |
|
|
1418 | |
|
|
1419 | aio_grp_feed (grp); |
|
|
1420 | } |
|
|
1421 | |