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