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