|
|
1 | #define _REENTRANT 1 |
|
|
2 | #include <errno.h> |
|
|
3 | |
1 | #include "EXTERN.h" |
4 | #include "EXTERN.h" |
2 | #include "perl.h" |
5 | #include "perl.h" |
3 | #include "XSUB.h" |
6 | #include "XSUB.h" |
|
|
7 | |
|
|
8 | #include "autoconf/config.h" |
4 | |
9 | |
5 | #include <sys/types.h> |
10 | #include <sys/types.h> |
6 | #include <sys/stat.h> |
11 | #include <sys/stat.h> |
7 | |
12 | |
8 | #include <unistd.h> |
13 | #include <unistd.h> |
9 | #include <fcntl.h> |
14 | #include <fcntl.h> |
10 | #include <signal.h> |
15 | #include <signal.h> |
11 | #include <sched.h> |
16 | #include <sched.h> |
12 | #if __linux |
|
|
13 | #include <sys/syscall.h> |
|
|
14 | #endif |
|
|
15 | |
17 | |
16 | #include <pthread.h> |
18 | #include <pthread.h> |
17 | |
19 | |
18 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
20 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
19 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
21 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
… | |
… | |
27 | |
29 | |
28 | enum { |
30 | enum { |
29 | REQ_QUIT, |
31 | REQ_QUIT, |
30 | REQ_OPEN, REQ_CLOSE, |
32 | REQ_OPEN, REQ_CLOSE, |
31 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
33 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
32 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, |
34 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, |
33 | REQ_FSYNC, REQ_FDATASYNC, |
35 | REQ_FSYNC, REQ_FDATASYNC, |
|
|
36 | REQ_UNLINK, REQ_RMDIR, |
|
|
37 | REQ_SYMLINK, |
34 | }; |
38 | }; |
35 | |
39 | |
36 | typedef struct aio_cb { |
40 | typedef struct aio_cb { |
37 | struct aio_cb *volatile next; |
41 | struct aio_cb *volatile next; |
38 | |
42 | |
… | |
… | |
43 | size_t length; |
47 | size_t length; |
44 | ssize_t result; |
48 | ssize_t result; |
45 | mode_t mode; /* open */ |
49 | mode_t mode; /* open */ |
46 | int errorno; |
50 | int errorno; |
47 | SV *data, *callback, *fh; |
51 | SV *data, *callback, *fh; |
48 | void *dataptr; |
52 | void *dataptr, *data2ptr; |
49 | STRLEN dataoffset; |
53 | STRLEN dataoffset; |
50 | |
54 | |
51 | Stat_t *statdata; |
55 | Stat_t *statdata; |
52 | } aio_cb; |
56 | } aio_cb; |
53 | |
57 | |
… | |
… | |
62 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
66 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
63 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
67 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
64 | |
68 | |
65 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
69 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
66 | static volatile aio_req ress, rese; /* queue start, queue end */ |
70 | static volatile aio_req ress, rese; /* queue start, queue end */ |
|
|
71 | |
|
|
72 | static void free_req (aio_req req) |
|
|
73 | { |
|
|
74 | if (req->data) |
|
|
75 | SvREFCNT_dec (req->data); |
|
|
76 | |
|
|
77 | if (req->fh) |
|
|
78 | SvREFCNT_dec (req->fh); |
|
|
79 | |
|
|
80 | if (req->statdata) |
|
|
81 | Safefree (req->statdata); |
|
|
82 | |
|
|
83 | if (req->callback) |
|
|
84 | SvREFCNT_dec (req->callback); |
|
|
85 | |
|
|
86 | Safefree (req); |
|
|
87 | } |
67 | |
88 | |
68 | static void |
89 | static void |
69 | poll_wait () |
90 | poll_wait () |
70 | { |
91 | { |
71 | if (nreqs && !ress) |
92 | if (nreqs && !ress) |
… | |
… | |
81 | static int |
102 | static int |
82 | poll_cb () |
103 | poll_cb () |
83 | { |
104 | { |
84 | dSP; |
105 | dSP; |
85 | int count = 0; |
106 | int count = 0; |
|
|
107 | int do_croak = 0; |
86 | aio_req req, prv; |
108 | aio_req req; |
87 | |
109 | |
88 | pthread_mutex_lock (&reslock); |
110 | for (;;) |
89 | |
|
|
90 | { |
|
|
91 | /* read any signals sent by the worker threads */ |
|
|
92 | char buf [32]; |
|
|
93 | while (read (respipe [0], buf, 32) > 0) |
|
|
94 | ; |
|
|
95 | } |
|
|
96 | |
|
|
97 | req = ress; |
|
|
98 | ress = rese = 0; |
|
|
99 | |
|
|
100 | pthread_mutex_unlock (&reslock); |
|
|
101 | |
|
|
102 | while (req) |
|
|
103 | { |
111 | { |
|
|
112 | pthread_mutex_lock (&reslock); |
|
|
113 | req = ress; |
|
|
114 | |
|
|
115 | if (req) |
|
|
116 | { |
|
|
117 | ress = req->next; |
|
|
118 | |
|
|
119 | if (!ress) |
|
|
120 | { |
|
|
121 | rese = 0; |
|
|
122 | |
|
|
123 | /* read any signals sent by the worker threads */ |
|
|
124 | char buf [32]; |
|
|
125 | while (read (respipe [0], buf, 32) == 32) |
|
|
126 | ; |
|
|
127 | } |
|
|
128 | } |
|
|
129 | |
|
|
130 | pthread_mutex_unlock (&reslock); |
|
|
131 | |
|
|
132 | if (!req) |
|
|
133 | break; |
|
|
134 | |
104 | nreqs--; |
135 | nreqs--; |
105 | |
136 | |
106 | if (req->type == REQ_QUIT) |
137 | if (req->type == REQ_QUIT) |
107 | started--; |
138 | started--; |
108 | else |
139 | else |
109 | { |
140 | { |
110 | int errorno = errno; |
141 | int errorno = errno; |
111 | errno = req->errorno; |
142 | errno = req->errorno; |
112 | |
143 | |
113 | if (req->type == REQ_READ) |
144 | if (req->type == REQ_READ) |
114 | SvCUR_set (req->data, req->dataoffset |
145 | SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0)); |
115 | + req->result > 0 ? req->result : 0); |
|
|
116 | |
146 | |
|
|
147 | if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE)) |
|
|
148 | SvREADONLY_off (req->data); |
|
|
149 | |
117 | if (req->data) |
150 | if (req->statdata) |
118 | SvREFCNT_dec (req->data); |
|
|
119 | |
|
|
120 | if (req->fh) |
|
|
121 | SvREFCNT_dec (req->fh); |
|
|
122 | |
|
|
123 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
124 | { |
151 | { |
125 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
152 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
126 | PL_laststatval = req->result; |
153 | PL_laststatval = req->result; |
127 | PL_statcache = *(req->statdata); |
154 | PL_statcache = *(req->statdata); |
128 | |
|
|
129 | Safefree (req->statdata); |
|
|
130 | } |
155 | } |
131 | |
156 | |
132 | ENTER; |
157 | ENTER; |
133 | PUSHMARK (SP); |
158 | PUSHMARK (SP); |
134 | XPUSHs (sv_2mortal (newSViv (req->result))); |
159 | XPUSHs (sv_2mortal (newSViv (req->result))); |
… | |
… | |
151 | if (SvOK (req->callback)) |
176 | if (SvOK (req->callback)) |
152 | { |
177 | { |
153 | PUTBACK; |
178 | PUTBACK; |
154 | call_sv (req->callback, G_VOID | G_EVAL); |
179 | call_sv (req->callback, G_VOID | G_EVAL); |
155 | SPAGAIN; |
180 | SPAGAIN; |
|
|
181 | |
|
|
182 | if (SvTRUE (ERRSV)) |
|
|
183 | { |
|
|
184 | free_req (req); |
|
|
185 | croak (0); |
|
|
186 | } |
156 | } |
187 | } |
157 | |
188 | |
158 | LEAVE; |
189 | LEAVE; |
159 | |
|
|
160 | if (req->callback) |
|
|
161 | SvREFCNT_dec (req->callback); |
|
|
162 | |
190 | |
163 | errno = errorno; |
191 | errno = errorno; |
164 | count++; |
192 | count++; |
165 | } |
193 | } |
166 | |
194 | |
167 | prv = req; |
195 | free_req (req); |
168 | req = req->next; |
|
|
169 | Safefree (prv); |
|
|
170 | |
|
|
171 | /* TODO: croak on errors? */ |
|
|
172 | } |
196 | } |
173 | |
197 | |
174 | return count; |
198 | return count; |
175 | } |
199 | } |
176 | |
200 | |
… | |
… | |
214 | reqe = reqs = req; |
238 | reqe = reqs = req; |
215 | |
239 | |
216 | pthread_cond_signal (&reqwait); |
240 | pthread_cond_signal (&reqwait); |
217 | pthread_mutex_unlock (&reqlock); |
241 | pthread_mutex_unlock (&reqlock); |
218 | |
242 | |
219 | while (nreqs > max_outstanding) |
243 | if (nreqs > max_outstanding) |
|
|
244 | for (;;) |
|
|
245 | { |
|
|
246 | poll_cb (); |
|
|
247 | |
|
|
248 | if (nreqs <= max_outstanding) |
|
|
249 | break; |
|
|
250 | |
|
|
251 | poll_wait (); |
|
|
252 | } |
|
|
253 | } |
|
|
254 | |
|
|
255 | static void |
|
|
256 | end_thread (void) |
|
|
257 | { |
|
|
258 | aio_req req; |
|
|
259 | Newz (0, req, 1, aio_cb); |
|
|
260 | req->type = REQ_QUIT; |
|
|
261 | |
|
|
262 | send_req (req); |
|
|
263 | } |
|
|
264 | |
|
|
265 | static void min_parallel (int nthreads) |
|
|
266 | { |
|
|
267 | while (nthreads > started) |
|
|
268 | start_thread (); |
|
|
269 | } |
|
|
270 | |
|
|
271 | static void max_parallel (int nthreads) |
|
|
272 | { |
|
|
273 | int cur = started; |
|
|
274 | |
|
|
275 | while (cur > nthreads) |
|
|
276 | { |
|
|
277 | end_thread (); |
|
|
278 | cur--; |
|
|
279 | } |
|
|
280 | |
|
|
281 | while (started > nthreads) |
220 | { |
282 | { |
221 | poll_wait (); |
283 | poll_wait (); |
222 | poll_cb (); |
284 | poll_cb (); |
223 | } |
285 | } |
224 | } |
286 | } |
225 | |
287 | |
226 | static void |
288 | static void create_pipe () |
227 | end_thread (void) |
|
|
228 | { |
289 | { |
|
|
290 | if (pipe (respipe)) |
|
|
291 | croak ("unable to initialize result pipe"); |
|
|
292 | |
|
|
293 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
294 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
295 | |
|
|
296 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
297 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
298 | } |
|
|
299 | |
|
|
300 | static void atfork_prepare (void) |
|
|
301 | { |
|
|
302 | pthread_mutex_lock (&reqlock); |
|
|
303 | pthread_mutex_lock (&reslock); |
|
|
304 | } |
|
|
305 | |
|
|
306 | static void atfork_parent (void) |
|
|
307 | { |
|
|
308 | pthread_mutex_unlock (&reslock); |
|
|
309 | pthread_mutex_unlock (&reqlock); |
|
|
310 | } |
|
|
311 | |
|
|
312 | static void atfork_child (void) |
|
|
313 | { |
229 | aio_req req; |
314 | aio_req prv; |
230 | New (0, req, 1, aio_cb); |
|
|
231 | req->type = REQ_QUIT; |
|
|
232 | |
315 | |
233 | send_req (req); |
316 | int restart = started; |
|
|
317 | started = 0; |
|
|
318 | |
|
|
319 | while (reqs) |
|
|
320 | { |
|
|
321 | prv = reqs; |
|
|
322 | reqs = prv->next; |
|
|
323 | free_req (prv); |
|
|
324 | } |
|
|
325 | |
|
|
326 | reqs = reqe = 0; |
|
|
327 | |
|
|
328 | while (ress) |
|
|
329 | { |
|
|
330 | prv = ress; |
|
|
331 | ress = prv->next; |
|
|
332 | free_req (prv); |
|
|
333 | } |
|
|
334 | |
|
|
335 | ress = rese = 0; |
|
|
336 | |
|
|
337 | close (respipe [0]); |
|
|
338 | close (respipe [1]); |
|
|
339 | create_pipe (); |
|
|
340 | |
|
|
341 | atfork_parent (); |
|
|
342 | |
|
|
343 | min_parallel (restart); |
234 | } |
344 | } |
|
|
345 | |
|
|
346 | /*****************************************************************************/ |
|
|
347 | /* work around various missing functions */ |
|
|
348 | |
|
|
349 | #if !HAVE_PREADWRITE |
|
|
350 | # define pread aio_pread |
|
|
351 | # define pwrite aio_pwrite |
|
|
352 | |
|
|
353 | /* |
|
|
354 | * make our pread/pwrite safe against themselves, but not against |
|
|
355 | * normal read/write by using a mutex. slows down execution a lot, |
|
|
356 | * but that's your problem, not mine. |
|
|
357 | */ |
|
|
358 | static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER; |
|
|
359 | |
|
|
360 | static ssize_t |
|
|
361 | pread (int fd, void *buf, size_t count, off_t offset) |
|
|
362 | { |
|
|
363 | ssize_t res; |
|
|
364 | off_t ooffset; |
|
|
365 | |
|
|
366 | pthread_mutex_lock (&iolock); |
|
|
367 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
368 | lseek (fd, offset, SEEK_SET); |
|
|
369 | res = read (fd, buf, count); |
|
|
370 | lseek (fd, ooffset, SEEK_SET); |
|
|
371 | pthread_mutex_unlock (&iolock); |
|
|
372 | |
|
|
373 | return res; |
|
|
374 | } |
|
|
375 | |
|
|
376 | static ssize_t |
|
|
377 | pwrite (int fd, void *buf, size_t count, off_t offset) |
|
|
378 | { |
|
|
379 | ssize_t res; |
|
|
380 | off_t ooffset; |
|
|
381 | |
|
|
382 | pthread_mutex_lock (&iolock); |
|
|
383 | ooffset = lseek (fd, 0, SEEK_CUR); |
|
|
384 | lseek (fd, offset, SEEK_SET); |
|
|
385 | res = write (fd, buf, count); |
|
|
386 | lseek (fd, offset, SEEK_SET); |
|
|
387 | pthread_mutex_unlock (&iolock); |
|
|
388 | |
|
|
389 | return res; |
|
|
390 | } |
|
|
391 | #endif |
|
|
392 | |
|
|
393 | #if !HAVE_FDATASYNC |
|
|
394 | # define fdatasync fsync |
|
|
395 | #endif |
|
|
396 | |
|
|
397 | #if !HAVE_READAHEAD |
|
|
398 | # define readahead aio_readahead |
|
|
399 | |
|
|
400 | static char readahead_buf[4096]; |
|
|
401 | |
|
|
402 | static ssize_t |
|
|
403 | readahead (int fd, off_t offset, size_t count) |
|
|
404 | { |
|
|
405 | while (count > 0) |
|
|
406 | { |
|
|
407 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
|
|
408 | |
|
|
409 | pread (fd, readahead_buf, len, offset); |
|
|
410 | offset += len; |
|
|
411 | count -= len; |
|
|
412 | } |
|
|
413 | |
|
|
414 | errno = 0; |
|
|
415 | } |
|
|
416 | #endif |
|
|
417 | |
|
|
418 | /*****************************************************************************/ |
235 | |
419 | |
236 | static void * |
420 | static void * |
237 | aio_proc (void *thr_arg) |
421 | aio_proc (void *thr_arg) |
238 | { |
422 | { |
239 | aio_req req; |
423 | aio_req req; |
… | |
… | |
267 | |
451 | |
268 | switch (type) |
452 | switch (type) |
269 | { |
453 | { |
270 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
454 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
271 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
455 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
272 | #if SYS_readahead |
456 | |
273 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
457 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
274 | #else |
|
|
275 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
|
|
276 | #endif |
|
|
277 | |
458 | |
278 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
459 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
279 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
460 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
280 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
461 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
281 | |
462 | |
282 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
463 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
283 | case REQ_CLOSE: req->result = close (req->fd); break; |
464 | case REQ_CLOSE: req->result = close (req->fd); break; |
284 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
465 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
466 | case REQ_RMDIR: req->result = rmdir (req->dataptr); break; |
|
|
467 | case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break; |
285 | |
468 | |
|
|
469 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
286 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
470 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
287 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
288 | |
471 | |
289 | case REQ_QUIT: |
472 | case REQ_QUIT: |
290 | break; |
473 | break; |
291 | |
474 | |
292 | default: |
475 | default: |
… | |
… | |
318 | while (type != REQ_QUIT); |
501 | while (type != REQ_QUIT); |
319 | |
502 | |
320 | return 0; |
503 | return 0; |
321 | } |
504 | } |
322 | |
505 | |
|
|
506 | #define dREQ \ |
|
|
507 | aio_req req; \ |
|
|
508 | \ |
|
|
509 | if (SvOK (callback) && !SvROK (callback)) \ |
|
|
510 | croak ("clalback must be undef or of reference type"); \ |
|
|
511 | \ |
|
|
512 | Newz (0, req, 1, aio_cb); \ |
|
|
513 | if (!req) \ |
|
|
514 | croak ("out of memory during aio_req allocation"); \ |
|
|
515 | \ |
|
|
516 | req->callback = newSVsv (callback); |
|
|
517 | |
323 | MODULE = IO::AIO PACKAGE = IO::AIO |
518 | MODULE = IO::AIO PACKAGE = IO::AIO |
324 | |
519 | |
325 | PROTOTYPES: ENABLE |
520 | PROTOTYPES: ENABLE |
326 | |
521 | |
327 | BOOT: |
522 | BOOT: |
328 | { |
523 | { |
329 | if (pipe (respipe)) |
524 | create_pipe (); |
330 | croak ("unable to initialize result pipe"); |
525 | pthread_atfork (atfork_prepare, atfork_parent, atfork_child); |
331 | |
|
|
332 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
|
|
333 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
334 | |
|
|
335 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
|
|
336 | croak ("cannot set result pipe to nonblocking mode"); |
|
|
337 | } |
526 | } |
338 | |
527 | |
339 | void |
528 | void |
340 | min_parallel(nthreads) |
529 | min_parallel(nthreads) |
341 | int nthreads |
530 | int nthreads |
342 | PROTOTYPE: $ |
531 | PROTOTYPE: $ |
343 | CODE: |
|
|
344 | while (nthreads > started) |
|
|
345 | start_thread (); |
|
|
346 | |
532 | |
347 | void |
533 | void |
348 | max_parallel(nthreads) |
534 | max_parallel(nthreads) |
349 | int nthreads |
535 | int nthreads |
350 | PROTOTYPE: $ |
536 | PROTOTYPE: $ |
351 | CODE: |
|
|
352 | { |
|
|
353 | int cur = started; |
|
|
354 | while (cur > nthreads) |
|
|
355 | { |
|
|
356 | end_thread (); |
|
|
357 | cur--; |
|
|
358 | } |
|
|
359 | |
|
|
360 | while (started > nthreads) |
|
|
361 | { |
|
|
362 | poll_wait (); |
|
|
363 | poll_cb (); |
|
|
364 | } |
|
|
365 | } |
|
|
366 | |
537 | |
367 | int |
538 | int |
368 | max_outstanding(nreqs) |
539 | max_outstanding(nreqs) |
369 | int nreqs |
540 | int nreqs |
370 | PROTOTYPE: $ |
541 | PROTOTYPE: $ |
… | |
… | |
379 | int mode |
550 | int mode |
380 | SV * callback |
551 | SV * callback |
381 | PROTOTYPE: $$$;$ |
552 | PROTOTYPE: $$$;$ |
382 | CODE: |
553 | CODE: |
383 | { |
554 | { |
384 | aio_req req; |
555 | dREQ; |
385 | |
|
|
386 | Newz (0, req, 1, aio_cb); |
|
|
387 | |
|
|
388 | if (!req) |
|
|
389 | croak ("out of memory during aio_req allocation"); |
|
|
390 | |
556 | |
391 | req->type = REQ_OPEN; |
557 | req->type = REQ_OPEN; |
392 | req->data = newSVsv (pathname); |
558 | req->data = newSVsv (pathname); |
393 | req->dataptr = SvPV_nolen (req->data); |
559 | req->dataptr = SvPVbyte_nolen (req->data); |
394 | req->fd = flags; |
560 | req->fd = flags; |
395 | req->mode = mode; |
561 | req->mode = mode; |
396 | req->callback = SvREFCNT_inc (callback); |
|
|
397 | |
562 | |
398 | send_req (req); |
563 | send_req (req); |
399 | } |
564 | } |
400 | |
565 | |
401 | void |
566 | void |
… | |
… | |
407 | aio_close = REQ_CLOSE |
572 | aio_close = REQ_CLOSE |
408 | aio_fsync = REQ_FSYNC |
573 | aio_fsync = REQ_FSYNC |
409 | aio_fdatasync = REQ_FDATASYNC |
574 | aio_fdatasync = REQ_FDATASYNC |
410 | CODE: |
575 | CODE: |
411 | { |
576 | { |
412 | aio_req req; |
577 | dREQ; |
413 | |
|
|
414 | Newz (0, req, 1, aio_cb); |
|
|
415 | |
|
|
416 | if (!req) |
|
|
417 | croak ("out of memory during aio_req allocation"); |
|
|
418 | |
578 | |
419 | req->type = ix; |
579 | req->type = ix; |
420 | req->fh = newSVsv (fh); |
580 | req->fh = newSVsv (fh); |
421 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
581 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
422 | req->callback = SvREFCNT_inc (callback); |
|
|
423 | |
582 | |
424 | send_req (req); |
583 | send_req (req); |
425 | } |
584 | } |
426 | |
585 | |
427 | void |
586 | void |
… | |
… | |
438 | PROTOTYPE: $$$$$;$ |
597 | PROTOTYPE: $$$$$;$ |
439 | CODE: |
598 | CODE: |
440 | { |
599 | { |
441 | aio_req req; |
600 | aio_req req; |
442 | STRLEN svlen; |
601 | STRLEN svlen; |
443 | char *svptr = SvPV (data, svlen); |
602 | char *svptr = SvPVbyte (data, svlen); |
444 | |
603 | |
445 | SvUPGRADE (data, SVt_PV); |
604 | SvUPGRADE (data, SVt_PV); |
446 | SvPOK_on (data); |
605 | SvPOK_on (data); |
447 | |
606 | |
448 | if (dataoffset < 0) |
607 | if (dataoffset < 0) |
… | |
… | |
464 | } |
623 | } |
465 | |
624 | |
466 | if (length < 0) |
625 | if (length < 0) |
467 | croak ("length must not be negative"); |
626 | croak ("length must not be negative"); |
468 | |
627 | |
469 | Newz (0, req, 1, aio_cb); |
628 | { |
|
|
629 | dREQ; |
470 | |
630 | |
471 | if (!req) |
|
|
472 | croak ("out of memory during aio_req allocation"); |
|
|
473 | |
|
|
474 | req->type = ix; |
631 | req->type = ix; |
475 | req->fh = newSVsv (fh); |
632 | req->fh = newSVsv (fh); |
476 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
633 | req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh)) |
477 | : IoOFP (sv_2io (fh))); |
634 | : IoOFP (sv_2io (fh))); |
478 | req->offset = offset; |
635 | req->offset = offset; |
479 | req->length = length; |
636 | req->length = length; |
480 | req->data = SvREFCNT_inc (data); |
637 | req->data = SvREFCNT_inc (data); |
481 | req->dataptr = (char *)svptr + dataoffset; |
638 | req->dataptr = (char *)svptr + dataoffset; |
482 | req->callback = SvREFCNT_inc (callback); |
|
|
483 | |
639 | |
|
|
640 | if (!SvREADONLY (data)) |
|
|
641 | { |
|
|
642 | SvREADONLY_on (data); |
|
|
643 | req->data2ptr = (void *)data; |
|
|
644 | } |
|
|
645 | |
484 | send_req (req); |
646 | send_req (req); |
|
|
647 | } |
485 | } |
648 | } |
486 | |
649 | |
487 | void |
650 | void |
488 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
651 | aio_readahead(fh,offset,length,callback=&PL_sv_undef) |
489 | SV * fh |
652 | SV * fh |
… | |
… | |
491 | IV length |
654 | IV length |
492 | SV * callback |
655 | SV * callback |
493 | PROTOTYPE: $$$;$ |
656 | PROTOTYPE: $$$;$ |
494 | CODE: |
657 | CODE: |
495 | { |
658 | { |
496 | aio_req req; |
659 | dREQ; |
497 | |
|
|
498 | if (length < 0) |
|
|
499 | croak ("length must not be negative"); |
|
|
500 | |
|
|
501 | Newz (0, req, 1, aio_cb); |
|
|
502 | |
|
|
503 | if (!req) |
|
|
504 | croak ("out of memory during aio_req allocation"); |
|
|
505 | |
660 | |
506 | req->type = REQ_READAHEAD; |
661 | req->type = REQ_READAHEAD; |
507 | req->fh = newSVsv (fh); |
662 | req->fh = newSVsv (fh); |
508 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
663 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh))); |
509 | req->offset = offset; |
664 | req->offset = offset; |
510 | req->length = length; |
665 | req->length = length; |
511 | req->callback = SvREFCNT_inc (callback); |
|
|
512 | |
666 | |
513 | send_req (req); |
667 | send_req (req); |
514 | } |
668 | } |
515 | |
669 | |
516 | void |
670 | void |
… | |
… | |
520 | ALIAS: |
674 | ALIAS: |
521 | aio_stat = REQ_STAT |
675 | aio_stat = REQ_STAT |
522 | aio_lstat = REQ_LSTAT |
676 | aio_lstat = REQ_LSTAT |
523 | CODE: |
677 | CODE: |
524 | { |
678 | { |
525 | aio_req req; |
679 | dREQ; |
526 | |
|
|
527 | Newz (0, req, 1, aio_cb); |
|
|
528 | |
|
|
529 | if (!req) |
|
|
530 | croak ("out of memory during aio_req allocation"); |
|
|
531 | |
680 | |
532 | New (0, req->statdata, 1, Stat_t); |
681 | New (0, req->statdata, 1, Stat_t); |
533 | |
|
|
534 | if (!req->statdata) |
682 | if (!req->statdata) |
|
|
683 | { |
|
|
684 | free_req (req); |
535 | croak ("out of memory during aio_req->statdata allocation"); |
685 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
686 | } |
536 | |
687 | |
537 | if (SvPOK (fh_or_path)) |
688 | if (SvPOK (fh_or_path)) |
538 | { |
689 | { |
539 | req->type = ix; |
690 | req->type = ix; |
540 | req->data = newSVsv (fh_or_path); |
691 | req->data = newSVsv (fh_or_path); |
541 | req->dataptr = SvPV_nolen (req->data); |
692 | req->dataptr = SvPVbyte_nolen (req->data); |
542 | } |
693 | } |
543 | else |
694 | else |
544 | { |
695 | { |
545 | req->type = REQ_FSTAT; |
696 | req->type = REQ_FSTAT; |
546 | req->fh = newSVsv (fh_or_path); |
697 | req->fh = newSVsv (fh_or_path); |
547 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
698 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
548 | } |
699 | } |
549 | |
700 | |
550 | req->callback = SvREFCNT_inc (callback); |
|
|
551 | |
|
|
552 | send_req (req); |
701 | send_req (req); |
553 | } |
702 | } |
554 | |
703 | |
555 | void |
704 | void |
556 | aio_unlink(pathname,callback=&PL_sv_undef) |
705 | aio_unlink(pathname,callback=&PL_sv_undef) |
557 | SV * pathname |
706 | SV * pathname |
558 | SV * callback |
707 | SV * callback |
|
|
708 | ALIAS: |
|
|
709 | aio_unlink = REQ_UNLINK |
|
|
710 | aio_rmdir = REQ_RMDIR |
559 | CODE: |
711 | CODE: |
560 | { |
712 | { |
561 | aio_req req; |
713 | dREQ; |
562 | |
714 | |
563 | Newz (0, req, 1, aio_cb); |
715 | req->type = ix; |
|
|
716 | req->data = newSVsv (pathname); |
|
|
717 | req->dataptr = SvPVbyte_nolen (req->data); |
564 | |
718 | |
565 | if (!req) |
719 | send_req (req); |
566 | croak ("out of memory during aio_req allocation"); |
720 | } |
|
|
721 | |
|
|
722 | void |
|
|
723 | aio_symlink(oldpath,newpath,callback=&PL_sv_undef) |
|
|
724 | SV * oldpath |
|
|
725 | SV * newpath |
|
|
726 | SV * callback |
|
|
727 | CODE: |
|
|
728 | { |
|
|
729 | dREQ; |
567 | |
730 | |
568 | req->type = REQ_UNLINK; |
731 | req->type = REQ_SYMLINK; |
|
|
732 | req->fh = newSVsv (oldpath); |
|
|
733 | req->data2ptr = SvPVbyte_nolen (req->fh); |
569 | req->data = newSVsv (pathname); |
734 | req->data = newSVsv (newpath); |
570 | req->dataptr = SvPV_nolen (req->data); |
735 | req->dataptr = SvPVbyte_nolen (req->data); |
571 | req->callback = SvREFCNT_inc (callback); |
|
|
572 | |
736 | |
573 | send_req (req); |
737 | send_req (req); |
574 | } |
738 | } |
575 | |
739 | |
576 | void |
740 | void |