|
|
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, |
|
|
65 | REQ_GROUP, REQ_NOP, |
|
|
66 | REQ_SLEEP, |
58 | }; |
67 | }; |
59 | |
68 | |
60 | #define AIO_CB_KLASS "IO::AIO::CB" |
69 | #define AIO_REQ_KLASS "IO::AIO::REQ" |
|
|
70 | #define AIO_GRP_KLASS "IO::AIO::GRP" |
61 | |
71 | |
62 | typedef struct aio_cb |
72 | typedef struct aio_cb |
63 | { |
73 | { |
64 | struct aio_cb *grp_prev, *grp_next; |
|
|
65 | struct aio_grp *grp; |
|
|
66 | |
|
|
67 | struct aio_cb *volatile next; |
74 | struct aio_cb *volatile next; |
68 | |
|
|
69 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
70 | |
75 | |
71 | SV *data, *callback; |
76 | SV *data, *callback; |
72 | SV *fh, *fh2; |
77 | SV *fh, *fh2; |
73 | void *dataptr, *data2ptr; |
78 | void *dataptr, *data2ptr; |
74 | Stat_t *statdata; |
79 | Stat_t *statdata; |
75 | off_t offset; |
80 | off_t offset; |
76 | size_t length; |
81 | size_t length; |
77 | ssize_t result; |
82 | ssize_t result; |
78 | |
83 | |
|
|
84 | STRLEN dataoffset; |
79 | int type; |
85 | int type; |
80 | int fd, fd2; |
86 | int fd, fd2; |
81 | int errorno; |
87 | int errorno; |
82 | STRLEN dataoffset; |
|
|
83 | mode_t mode; /* open */ |
88 | mode_t mode; /* open */ |
|
|
89 | |
84 | 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; |
85 | } aio_cb; |
95 | } aio_cb; |
|
|
96 | |
|
|
97 | enum { |
|
|
98 | FLAG_CANCELLED = 0x01, |
|
|
99 | }; |
86 | |
100 | |
87 | typedef aio_cb *aio_req; |
101 | typedef aio_cb *aio_req; |
88 | typedef aio_cb *aio_req_ornot; |
102 | typedef aio_cb *aio_req_ornot; |
|
|
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; |
89 | |
113 | |
90 | static int started, wanted; |
114 | static int started, wanted; |
91 | static volatile int nreqs; |
115 | static volatile int nreqs; |
92 | static int max_outstanding = 1<<30; |
116 | static int max_outstanding = 1<<30; |
93 | static int respipe [2]; |
117 | static int respipe [2]; |
94 | |
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 | |
95 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
125 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
96 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
126 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
97 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
127 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
98 | |
128 | |
99 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
129 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
100 | static volatile aio_req ress, rese; /* queue start, queue end */ |
130 | static volatile aio_req ress, rese; /* queue start, queue end */ |
101 | |
131 | |
102 | typedef struct aio_grp |
132 | static void req_invoke (aio_req req); |
103 | { |
133 | static void req_free (aio_req req); |
104 | struct aio_cb *first, *last; |
|
|
105 | SV *callback; |
|
|
106 | int busycount; |
|
|
107 | } aio_grp; |
|
|
108 | |
134 | |
109 | static void aio_grp_begin (aio_grp *grp) |
135 | /* must be called at most once */ |
|
|
136 | static SV *req_sv (aio_req req, const char *klass) |
110 | { |
137 | { |
111 | ++grp->busycount; |
138 | if (!req->self) |
112 | } |
139 | { |
|
|
140 | req->self = (SV *)newHV (); |
|
|
141 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
142 | } |
113 | |
143 | |
|
|
144 | return sv_2mortal (sv_bless (newRV_inc (req->self), gv_stashpv (klass, 1))); |
|
|
145 | } |
|
|
146 | |
|
|
147 | static aio_req SvAIO_REQ (SV *sv) |
|
|
148 | { |
|
|
149 | MAGIC *mg; |
|
|
150 | |
|
|
151 | if (!sv_derived_from (sv, AIO_REQ_KLASS) || !SvROK (sv)) |
|
|
152 | croak ("object of class " AIO_REQ_KLASS " expected"); |
|
|
153 | |
|
|
154 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
|
|
155 | |
|
|
156 | return mg ? (aio_req)mg->mg_ptr : 0; |
|
|
157 | } |
|
|
158 | |
114 | static void aio_grp_end (aio_grp *grp) |
159 | static void aio_grp_feed (aio_req grp) |
115 | { |
160 | { |
116 | --grp->busycount; |
161 | while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED)) |
|
|
162 | { |
|
|
163 | int old_len = grp->length; |
117 | |
164 | |
118 | if (grp->busycount) |
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)) |
119 | return; |
236 | return; |
120 | |
237 | |
121 | SvREFCNT_dec (grp->callback); |
238 | errno = req->errorno; |
122 | grp->callback = 0; |
|
|
123 | } |
|
|
124 | |
239 | |
125 | static aio_grp *aio_grp_new () |
240 | ENTER; |
126 | { |
241 | SAVETMPS; |
127 | aio_grp *grp; |
242 | PUSHMARK (SP); |
|
|
243 | EXTEND (SP, 1); |
128 | |
244 | |
129 | Newz (0, grp, 1, aio_grp); |
245 | switch (req->type) |
130 | aio_grp_begin (grp); |
246 | { |
|
|
247 | case REQ_READDIR: |
|
|
248 | { |
|
|
249 | SV *rv = &PL_sv_undef; |
131 | |
250 | |
132 | return grp; |
251 | if (req->result >= 0) |
133 | } |
252 | { |
|
|
253 | char *buf = req->data2ptr; |
|
|
254 | AV *av = newAV (); |
134 | |
255 | |
135 | /* must be called at most once */ |
256 | while (req->result) |
136 | static SV *req_sv (aio_req req) |
257 | { |
137 | { |
258 | SV *sv = newSVpv (buf, 0); |
138 | req->self = (SV *)newHV (); |
|
|
139 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
140 | |
259 | |
141 | return sv_bless (newRV_noinc (req->self), gv_stashpv (AIO_CB_KLASS, 1)); |
260 | av_push (av, sv); |
142 | } |
261 | buf += SvCUR (sv) + 1; |
|
|
262 | req->result--; |
|
|
263 | } |
143 | |
264 | |
144 | static aio_req SvAIO_REQ (SV *sv) |
265 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
145 | { |
266 | } |
146 | if (!sv_derived_from (sv, AIO_CB_KLASS) || !SvROK (sv)) |
|
|
147 | croak ("object of class " AIO_CB_KLASS " expected"); |
|
|
148 | |
267 | |
149 | MAGIC *mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
268 | PUSHs (rv); |
|
|
269 | } |
|
|
270 | break; |
150 | |
271 | |
151 | return mg ? (aio_req)mg->mg_ptr : 0; |
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 | } |
152 | } |
327 | } |
153 | |
328 | |
154 | static void req_free (aio_req req) |
329 | static void req_free (aio_req req) |
155 | { |
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 | |
156 | if (req->self) |
345 | if (req->self) |
157 | { |
346 | { |
158 | sv_unmagic (req->self, PERL_MAGIC_ext); |
347 | sv_unmagic (req->self, PERL_MAGIC_ext); |
159 | SvREFCNT_dec (req->self); |
348 | SvREFCNT_dec (req->self); |
160 | } |
349 | } |
161 | |
350 | |
162 | if (req->data) |
|
|
163 | SvREFCNT_dec (req->data); |
351 | SvREFCNT_dec (req->data); |
164 | |
|
|
165 | if (req->fh) |
|
|
166 | SvREFCNT_dec (req->fh); |
352 | SvREFCNT_dec (req->fh); |
167 | |
|
|
168 | if (req->fh2) |
|
|
169 | SvREFCNT_dec (req->fh2); |
353 | SvREFCNT_dec (req->fh2); |
170 | |
|
|
171 | if (req->statdata) |
|
|
172 | Safefree (req->statdata); |
|
|
173 | |
|
|
174 | if (req->callback) |
|
|
175 | SvREFCNT_dec (req->callback); |
354 | SvREFCNT_dec (req->callback); |
|
|
355 | Safefree (req->statdata); |
176 | |
356 | |
177 | if (req->type == REQ_READDIR && req->result >= 0) |
357 | if (req->type == REQ_READDIR && req->result >= 0) |
178 | free (req->data2ptr); |
358 | free (req->data2ptr); |
179 | |
359 | |
180 | Safefree (req); |
360 | Safefree (req); |
181 | } |
361 | } |
182 | |
362 | |
183 | static void |
363 | static void req_cancel (aio_req req) |
184 | poll_wait () |
|
|
185 | { |
364 | { |
186 | if (nreqs && !ress) |
365 | req->flags |= FLAG_CANCELLED; |
187 | { |
|
|
188 | fd_set rfd; |
|
|
189 | FD_ZERO(&rfd); |
|
|
190 | FD_SET(respipe [0], &rfd); |
|
|
191 | |
366 | |
192 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
367 | if (req->type == REQ_GROUP) |
193 | } |
368 | { |
194 | } |
369 | aio_req sub; |
195 | |
370 | |
196 | static int |
371 | for (sub = req->grp_first; sub; sub = sub->grp_next) |
197 | poll_cb () |
372 | req_cancel (sub); |
|
|
373 | } |
|
|
374 | } |
|
|
375 | |
|
|
376 | static int poll_cb () |
198 | { |
377 | { |
199 | dSP; |
378 | dSP; |
200 | int count = 0; |
379 | int count = 0; |
201 | int do_croak = 0; |
380 | int do_croak = 0; |
202 | aio_req req; |
381 | aio_req req; |
… | |
… | |
224 | pthread_mutex_unlock (&reslock); |
403 | pthread_mutex_unlock (&reslock); |
225 | |
404 | |
226 | if (!req) |
405 | if (!req) |
227 | break; |
406 | break; |
228 | |
407 | |
229 | nreqs--; |
408 | --nreqs; |
230 | |
409 | |
231 | if (req->type == REQ_QUIT) |
410 | if (req->type == REQ_QUIT) |
232 | started--; |
411 | started--; |
|
|
412 | else if (req->type == REQ_GROUP && req->length) |
|
|
413 | { |
|
|
414 | req->fd = 1; /* mark request as delayed */ |
|
|
415 | continue; |
|
|
416 | } |
233 | else |
417 | else |
234 | { |
418 | { |
235 | int errorno = errno; |
|
|
236 | errno = req->errorno; |
|
|
237 | |
|
|
238 | if (req->type == REQ_READ) |
419 | if (req->type == REQ_READ) |
239 | 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)); |
240 | |
421 | |
241 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
422 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
242 | SvREADONLY_off (req->data); |
423 | SvREADONLY_off (req->data); |
… | |
… | |
246 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
427 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
247 | PL_laststatval = req->result; |
428 | PL_laststatval = req->result; |
248 | PL_statcache = *(req->statdata); |
429 | PL_statcache = *(req->statdata); |
249 | } |
430 | } |
250 | |
431 | |
251 | ENTER; |
432 | req_invoke (req); |
252 | PUSHMARK (SP); |
|
|
253 | |
433 | |
254 | if (req->type == REQ_READDIR) |
|
|
255 | { |
|
|
256 | SV *rv = &PL_sv_undef; |
|
|
257 | |
|
|
258 | if (req->result >= 0) |
|
|
259 | { |
|
|
260 | char *buf = req->data2ptr; |
|
|
261 | AV *av = newAV (); |
|
|
262 | |
|
|
263 | while (req->result) |
|
|
264 | { |
|
|
265 | SV *sv = newSVpv (buf, 0); |
|
|
266 | |
|
|
267 | av_push (av, sv); |
|
|
268 | buf += SvCUR (sv) + 1; |
|
|
269 | req->result--; |
|
|
270 | } |
|
|
271 | |
|
|
272 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
273 | } |
|
|
274 | |
|
|
275 | XPUSHs (rv); |
|
|
276 | } |
|
|
277 | else |
|
|
278 | { |
|
|
279 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
280 | |
|
|
281 | if (req->type == REQ_OPEN) |
|
|
282 | { |
|
|
283 | /* convert fd to fh */ |
|
|
284 | SV *fh; |
|
|
285 | |
|
|
286 | PUTBACK; |
|
|
287 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
288 | SPAGAIN; |
|
|
289 | |
|
|
290 | fh = SvREFCNT_inc (POPs); |
|
|
291 | |
|
|
292 | PUSHMARK (SP); |
|
|
293 | XPUSHs (sv_2mortal (fh)); |
|
|
294 | } |
|
|
295 | } |
|
|
296 | |
|
|
297 | if (SvOK (req->callback) && !req->cancelled) |
|
|
298 | { |
|
|
299 | PUTBACK; |
|
|
300 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
301 | SPAGAIN; |
|
|
302 | |
|
|
303 | if (SvTRUE (ERRSV)) |
|
|
304 | { |
|
|
305 | req_free (req); |
|
|
306 | croak (0); |
|
|
307 | } |
|
|
308 | } |
|
|
309 | |
|
|
310 | LEAVE; |
|
|
311 | |
|
|
312 | errno = errorno; |
|
|
313 | count++; |
434 | count++; |
314 | } |
435 | } |
315 | |
436 | |
316 | req_free (req); |
437 | req_free (req); |
317 | } |
438 | } |
… | |
… | |
319 | return count; |
440 | return count; |
320 | } |
441 | } |
321 | |
442 | |
322 | static void *aio_proc(void *arg); |
443 | static void *aio_proc(void *arg); |
323 | |
444 | |
324 | static void |
|
|
325 | start_thread (void) |
445 | static void start_thread (void) |
326 | { |
446 | { |
327 | sigset_t fullsigset, oldsigset; |
447 | sigset_t fullsigset, oldsigset; |
328 | pthread_t tid; |
448 | pthread_t tid; |
329 | pthread_attr_t attr; |
449 | pthread_attr_t attr; |
330 | |
450 | |
… | |
… | |
339 | started++; |
459 | started++; |
340 | |
460 | |
341 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
461 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
342 | } |
462 | } |
343 | |
463 | |
344 | static void |
|
|
345 | req_send (aio_req req) |
464 | static void req_send (aio_req req) |
346 | { |
465 | { |
347 | while (started < wanted && nreqs >= started) |
466 | while (started < wanted && nreqs >= started) |
348 | start_thread (); |
467 | start_thread (); |
349 | |
468 | |
350 | nreqs++; |
469 | ++nreqs; |
351 | |
470 | |
352 | pthread_mutex_lock (&reqlock); |
471 | pthread_mutex_lock (&reqlock); |
353 | |
472 | |
354 | req->next = 0; |
473 | req->next = 0; |
355 | |
474 | |
… | |
… | |
374 | |
493 | |
375 | poll_wait (); |
494 | poll_wait (); |
376 | } |
495 | } |
377 | } |
496 | } |
378 | |
497 | |
379 | static void |
498 | static void end_thread (void) |
380 | end_thread (void) |
|
|
381 | { |
499 | { |
382 | aio_req req; |
500 | aio_req req; |
383 | Newz (0, req, 1, aio_cb); |
501 | Newz (0, req, 1, aio_cb); |
384 | req->type = REQ_QUIT; |
502 | req->type = REQ_QUIT; |
385 | |
503 | |
… | |
… | |
436 | * 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, |
437 | * but that's your problem, not mine. |
555 | * but that's your problem, not mine. |
438 | */ |
556 | */ |
439 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
557 | static pthread_mutex_t preadwritelock = PTHREAD_MUTEX_INITIALIZER; |
440 | |
558 | |
441 | static ssize_t |
559 | static ssize_t pread (int fd, void *buf, size_t count, off_t offset) |
442 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
443 | { |
560 | { |
444 | ssize_t res; |
561 | ssize_t res; |
445 | off_t ooffset; |
562 | off_t ooffset; |
446 | |
563 | |
447 | pthread_mutex_lock (&preadwritelock); |
564 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
452 | pthread_mutex_unlock (&preadwritelock); |
569 | pthread_mutex_unlock (&preadwritelock); |
453 | |
570 | |
454 | return res; |
571 | return res; |
455 | } |
572 | } |
456 | |
573 | |
457 | static ssize_t |
|
|
458 | 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) |
459 | { |
575 | { |
460 | ssize_t res; |
576 | ssize_t res; |
461 | off_t ooffset; |
577 | off_t ooffset; |
462 | |
578 | |
463 | pthread_mutex_lock (&preadwritelock); |
579 | pthread_mutex_lock (&preadwritelock); |
… | |
… | |
476 | #endif |
592 | #endif |
477 | |
593 | |
478 | #if !HAVE_READAHEAD |
594 | #if !HAVE_READAHEAD |
479 | # define readahead aio_readahead |
595 | # define readahead aio_readahead |
480 | |
596 | |
481 | static ssize_t |
|
|
482 | readahead (int fd, off_t offset, size_t count) |
597 | static ssize_t readahead (int fd, off_t offset, size_t count) |
483 | { |
598 | { |
484 | char readahead_buf[4096]; |
599 | char readahead_buf[4096]; |
485 | |
600 | |
486 | while (count > 0) |
601 | while (count > 0) |
487 | { |
602 | { |
… | |
… | |
499 | #if !HAVE_READDIR_R |
614 | #if !HAVE_READDIR_R |
500 | # define readdir_r aio_readdir_r |
615 | # define readdir_r aio_readdir_r |
501 | |
616 | |
502 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
617 | static pthread_mutex_t readdirlock = PTHREAD_MUTEX_INITIALIZER; |
503 | |
618 | |
504 | static int |
|
|
505 | readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
619 | static int readdir_r (DIR *dirp, struct dirent *ent, struct dirent **res) |
506 | { |
620 | { |
507 | struct dirent *e; |
621 | struct dirent *e; |
508 | int errorno; |
622 | int errorno; |
509 | |
623 | |
510 | pthread_mutex_lock (&readdirlock); |
624 | pthread_mutex_lock (&readdirlock); |
… | |
… | |
526 | return e ? 0 : -1; |
640 | return e ? 0 : -1; |
527 | } |
641 | } |
528 | #endif |
642 | #endif |
529 | |
643 | |
530 | /* sendfile always needs emulation */ |
644 | /* sendfile always needs emulation */ |
531 | static ssize_t |
|
|
532 | 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) |
533 | { |
646 | { |
534 | ssize_t res; |
647 | ssize_t res; |
535 | |
648 | |
536 | if (!count) |
649 | if (!count) |
537 | return 0; |
650 | return 0; |
… | |
… | |
619 | |
732 | |
620 | return res; |
733 | return res; |
621 | } |
734 | } |
622 | |
735 | |
623 | /* read a full directory */ |
736 | /* read a full directory */ |
624 | static int |
|
|
625 | scandir_ (const char *path, void **namesp) |
737 | static int scandir_ (const char *path, void **namesp) |
626 | { |
738 | { |
627 | DIR *dirp = opendir (path); |
739 | DIR *dirp = opendir (path); |
628 | union |
740 | union |
629 | { |
741 | { |
630 | struct dirent d; |
742 | struct dirent d; |
… | |
… | |
684 | return res; |
796 | return res; |
685 | } |
797 | } |
686 | |
798 | |
687 | /*****************************************************************************/ |
799 | /*****************************************************************************/ |
688 | |
800 | |
689 | static void * |
|
|
690 | aio_proc (void *thr_arg) |
801 | static void *aio_proc (void *thr_arg) |
691 | { |
802 | { |
692 | aio_req req; |
803 | aio_req req; |
693 | int type; |
804 | int type; |
694 | |
805 | |
695 | do |
806 | do |
… | |
… | |
713 | } |
824 | } |
714 | |
825 | |
715 | pthread_mutex_unlock (&reqlock); |
826 | pthread_mutex_unlock (&reqlock); |
716 | |
827 | |
717 | errno = 0; /* strictly unnecessary */ |
828 | errno = 0; /* strictly unnecessary */ |
|
|
829 | type = req->type; /* remember type for QUIT check */ |
718 | |
830 | |
719 | if (!req->cancelled) |
831 | if (!(req->flags & FLAG_CANCELLED)) |
720 | switch (req->type) |
832 | switch (type) |
721 | { |
833 | { |
722 | 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; |
723 | 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; |
724 | |
836 | |
725 | 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; |
… | |
… | |
739 | |
851 | |
740 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
852 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
741 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
853 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
742 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
854 | case REQ_READDIR: req->result = scandir_ (req->dataptr, &req->data2ptr); break; |
743 | |
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: |
744 | case REQ_QUIT: |
868 | case REQ_QUIT: |
745 | break; |
869 | break; |
746 | |
870 | |
747 | default: |
871 | default: |
748 | req->result = ENOSYS; |
872 | req->result = ENOSYS; |
… | |
… | |
832 | atfork_parent (); |
956 | atfork_parent (); |
833 | } |
957 | } |
834 | |
958 | |
835 | #define dREQ \ |
959 | #define dREQ \ |
836 | aio_req req; \ |
960 | aio_req req; \ |
|
|
961 | int req_pri = next_pri; \ |
|
|
962 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
837 | \ |
963 | \ |
838 | if (SvOK (callback) && !SvROK (callback)) \ |
964 | if (SvOK (callback) && !SvROK (callback)) \ |
839 | croak ("callback must be undef or of reference type"); \ |
965 | croak ("callback must be undef or of reference type"); \ |
840 | \ |
966 | \ |
841 | Newz (0, req, 1, aio_cb); \ |
967 | Newz (0, req, 1, aio_cb); \ |
842 | if (!req) \ |
968 | if (!req) \ |
843 | croak ("out of memory during aio_req allocation"); \ |
969 | croak ("out of memory during aio_req allocation"); \ |
844 | \ |
970 | \ |
845 | req->callback = newSVsv (callback) |
971 | req->callback = newSVsv (callback); \ |
|
|
972 | req->pri = req_pri |
846 | |
973 | |
847 | #define REQ_SEND \ |
974 | #define REQ_SEND \ |
848 | req_send (req); \ |
975 | req_send (req); \ |
849 | \ |
976 | \ |
850 | if (GIMME_V != G_VOID) \ |
977 | if (GIMME_V != G_VOID) \ |
851 | XPUSHs (req_sv (req)); |
978 | XPUSHs (req_sv (req, AIO_REQ_KLASS)); |
852 | |
979 | |
853 | MODULE = IO::AIO PACKAGE = IO::AIO |
980 | MODULE = IO::AIO PACKAGE = IO::AIO |
854 | |
981 | |
855 | PROTOTYPES: ENABLE |
982 | PROTOTYPES: ENABLE |
856 | |
983 | |
… | |
… | |
1103 | req->dataptr = SvPVbyte_nolen (req->data); |
1230 | req->dataptr = SvPVbyte_nolen (req->data); |
1104 | |
1231 | |
1105 | REQ_SEND; |
1232 | REQ_SEND; |
1106 | } |
1233 | } |
1107 | |
1234 | |
|
|
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 |
|
|
1251 | aio_group (callback=&PL_sv_undef) |
|
|
1252 | SV * callback |
|
|
1253 | PROTOTYPE: ;$ |
|
|
1254 | PPCODE: |
|
|
1255 | { |
|
|
1256 | dREQ; |
|
|
1257 | |
|
|
1258 | req->type = REQ_GROUP; |
|
|
1259 | req_send (req); |
|
|
1260 | |
|
|
1261 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
|
|
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 | |
1108 | #if 0 |
1276 | #if 0 |
1109 | |
1277 | |
1110 | # undocumented, because it does not cancel active requests |
|
|
1111 | void |
1278 | void |
1112 | cancel_most_requests () |
1279 | aio_pri (int pri = DEFAULT_PRI) |
1113 | PROTOTYPE: |
1280 | CODE: |
1114 | CODE: |
1281 | if (pri < PRI_MIN) pri = PRI_MIN; |
1115 | { |
1282 | if (pri > PRI_MAX) pri = PRI_MAX; |
1116 | aio_req *req; |
1283 | next_pri = pri + PRI_BIAS; |
1117 | |
|
|
1118 | pthread_mutex_lock (&reqlock); |
|
|
1119 | for (req = reqs; req; req = req->next) |
|
|
1120 | req->flags |= 1; |
|
|
1121 | pthread_mutex_unlock (&reqlock); |
|
|
1122 | |
|
|
1123 | pthread_mutex_lock (&reslock); |
|
|
1124 | for (req = ress; req; req = req->next) |
|
|
1125 | req->flags |= 1; |
|
|
1126 | pthread_mutex_unlock (&reslock); |
|
|
1127 | } |
|
|
1128 | |
1284 | |
1129 | #endif |
1285 | #endif |
1130 | |
1286 | |
1131 | void |
1287 | void |
1132 | flush () |
1288 | flush () |
… | |
… | |
1177 | CODE: |
1333 | CODE: |
1178 | RETVAL = nreqs; |
1334 | RETVAL = nreqs; |
1179 | OUTPUT: |
1335 | OUTPUT: |
1180 | RETVAL |
1336 | RETVAL |
1181 | |
1337 | |
|
|
1338 | PROTOTYPES: DISABLE |
|
|
1339 | |
1182 | MODULE = IO::AIO PACKAGE = IO::AIO::CB |
1340 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1183 | |
1341 | |
1184 | void |
1342 | void |
1185 | cancel (aio_req_ornot req) |
1343 | cancel (aio_req_ornot req) |
1186 | PROTOTYPE: |
1344 | PROTOTYPE: |
1187 | CODE: |
1345 | CODE: |
1188 | req->cancelled = 1; |
1346 | req_cancel (req); |
1189 | |
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 | |