|
|
1 | #include "libeio/xthread.h" |
|
|
2 | |
|
|
3 | #include <errno.h> |
|
|
4 | |
1 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
2 | #include "perl.h" |
6 | #include "perl.h" |
3 | #include "XSUB.h" |
7 | #include "XSUB.h" |
4 | |
8 | |
|
|
9 | #include "schmorp.h" |
|
|
10 | |
|
|
11 | #include <stddef.h> |
|
|
12 | #include <stdlib.h> |
|
|
13 | #include <errno.h> |
5 | #include <sys/types.h> |
14 | #include <sys/types.h> |
6 | #include <sys/stat.h> |
15 | #include <sys/stat.h> |
|
|
16 | #include <sys/statvfs.h> |
7 | #include <unistd.h> |
17 | #include <limits.h> |
8 | #include <fcntl.h> |
18 | #include <fcntl.h> |
9 | #include <signal.h> |
|
|
10 | #include <sched.h> |
19 | #include <sched.h> |
11 | #include <endian.h> |
|
|
12 | |
20 | |
13 | #include <pthread.h> |
21 | #if _POSIX_MEMLOCK || _POSIX_MAPPED_FILES |
14 | #include <sys/syscall.h> |
22 | # include <sys/mman.h> |
|
|
23 | #endif |
15 | |
24 | |
16 | typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
25 | /* perl namespace pollution */ |
17 | typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
26 | #undef VERSION |
18 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
19 | |
27 | |
20 | #if __ia64 |
28 | #ifdef _WIN32 |
21 | # define STACKSIZE 65536 |
29 | |
|
|
30 | # define EIO_STRUCT_DIRENT Direntry_t |
|
|
31 | # undef malloc |
|
|
32 | # undef free |
|
|
33 | |
|
|
34 | // perl overrides all those nice win32 functions |
|
|
35 | # undef open |
|
|
36 | # undef read |
|
|
37 | # undef write |
|
|
38 | # undef send |
|
|
39 | # undef recv |
|
|
40 | # undef stat |
|
|
41 | # undef fstat |
|
|
42 | # define lstat stat |
|
|
43 | # undef truncate |
|
|
44 | # undef ftruncate |
|
|
45 | # undef open |
|
|
46 | # undef close |
|
|
47 | # undef unlink |
|
|
48 | # undef rmdir |
|
|
49 | # undef rename |
|
|
50 | # undef lseek |
|
|
51 | |
|
|
52 | # define chown(a,b,c) (errno = ENOSYS, -1) |
|
|
53 | # define fchown(a,b,c) (errno = ENOSYS, -1) |
|
|
54 | # define fchmod(a,b) (errno = ENOSYS, -1) |
|
|
55 | # define symlink(a,b) (errno = ENOSYS, -1) |
|
|
56 | # define readlink(a,b,c) (errno = ENOSYS, -1) |
|
|
57 | # define mknod(a,b,c) (errno = ENOSYS, -1) |
|
|
58 | # define truncate(a,b) (errno = ENOSYS, -1) |
|
|
59 | # define ftruncate(fd,o) chsize ((fd), (o)) |
|
|
60 | # define fsync(fd) _commit (fd) |
|
|
61 | # define opendir(fd) (errno = ENOSYS, 0) |
|
|
62 | # define readdir(fd) (errno = ENOSYS, -1) |
|
|
63 | # define closedir(fd) (errno = ENOSYS, -1) |
|
|
64 | # define mkdir(a,b) mkdir (a) |
|
|
65 | |
22 | #else |
66 | #else |
23 | # define STACKSIZE 4096 |
67 | |
|
|
68 | # include <sys/time.h> |
|
|
69 | # include <sys/select.h> |
|
|
70 | # include <unistd.h> |
|
|
71 | # include <utime.h> |
|
|
72 | # include <signal.h> |
|
|
73 | # define EIO_STRUCT_DIRENT struct dirent |
|
|
74 | |
|
|
75 | #endif |
|
|
76 | |
|
|
77 | /* perl stupidly overrides readdir and maybe others */ |
|
|
78 | /* with thread-unsafe versions, imagine that :( */ |
|
|
79 | #undef readdir |
|
|
80 | #undef opendir |
|
|
81 | #undef closedir |
|
|
82 | |
|
|
83 | #define EIO_STRUCT_STAT Stat_t |
|
|
84 | |
|
|
85 | /* use NV for 32 bit perls as it allows larger offsets */ |
|
|
86 | #if IVSIZE >= 8 |
|
|
87 | # define VAL64 IV |
|
|
88 | # define SvVAL64 SvIV |
|
|
89 | # define newSVval64 newSViv |
|
|
90 | #else |
|
|
91 | # define VAL64 NV |
|
|
92 | # define SvVAL64 SvNV |
|
|
93 | # define newSVval64 newSVnv |
|
|
94 | #endif |
|
|
95 | |
|
|
96 | /*****************************************************************************/ |
|
|
97 | |
|
|
98 | #if __GNUC__ >= 3 |
|
|
99 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
100 | #else |
|
|
101 | # define expect(expr,value) (expr) |
|
|
102 | #endif |
|
|
103 | |
|
|
104 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
105 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
106 | |
|
|
107 | /*****************************************************************************/ |
|
|
108 | |
|
|
109 | typedef SV SV8; /* byte-sv, used for argument-checking */ |
|
|
110 | typedef int aio_rfd; /* read file desriptor */ |
|
|
111 | typedef int aio_wfd; /* write file descriptor */ |
|
|
112 | |
|
|
113 | static HV *aio_stash, *aio_req_stash, *aio_grp_stash; |
|
|
114 | |
|
|
115 | #define EIO_REQ_MEMBERS \ |
|
|
116 | SV *callback; \ |
|
|
117 | SV *sv1, *sv2; \ |
|
|
118 | STRLEN stroffset; \ |
|
|
119 | SV *self; |
|
|
120 | |
|
|
121 | #define EIO_NO_WRAPPERS 1 |
|
|
122 | |
|
|
123 | #include "libeio/eio.h" |
|
|
124 | |
|
|
125 | #ifndef POSIX_FADV_NORMAL |
|
|
126 | # define POSIX_FADV_NORMAL 0 |
|
|
127 | # define NO_FADVISE 1 |
|
|
128 | #endif |
|
|
129 | #ifndef POSIX_FADV_SEQUENTIAL |
|
|
130 | # define POSIX_FADV_SEQUENTIAL 0 |
|
|
131 | #endif |
|
|
132 | #ifndef POSIX_FADV_RANDOM |
|
|
133 | # define POSIX_FADV_RANDOM 0 |
|
|
134 | #endif |
|
|
135 | #ifndef POSIX_FADV_NOREUSE |
|
|
136 | # define POSIX_FADV_NOREUSE 0 |
|
|
137 | #endif |
|
|
138 | #ifndef POSIX_FADV_WILLNEED |
|
|
139 | # define POSIX_FADV_WILLNEED 0 |
|
|
140 | #endif |
|
|
141 | #ifndef POSIX_FADV_DONTNEED |
|
|
142 | # define POSIX_FADV_DONTNEED 0 |
|
|
143 | #endif |
|
|
144 | |
|
|
145 | #ifndef ST_NODEV |
|
|
146 | # define ST_NODEV 0 |
|
|
147 | #endif |
|
|
148 | #ifndef ST_NOEXEC |
|
|
149 | # define ST_NOEXEC 0 |
|
|
150 | #endif |
|
|
151 | #ifndef ST_SYNCHRONOUS |
|
|
152 | # define ST_SYNCHRONOUS 0 |
|
|
153 | #endif |
|
|
154 | #ifndef ST_MANDLOCK |
|
|
155 | # define ST_MANDLOCK 0 |
|
|
156 | #endif |
|
|
157 | #ifndef ST_WRITE |
|
|
158 | # define ST_WRITE 0 |
|
|
159 | #endif |
|
|
160 | #ifndef ST_APPEND |
|
|
161 | # define ST_APPEND 0 |
|
|
162 | #endif |
|
|
163 | #ifndef ST_IMMUTABLE |
|
|
164 | # define ST_IMMUTABLE 0 |
|
|
165 | #endif |
|
|
166 | #ifndef ST_NOATIME |
|
|
167 | # define ST_NOATIME 0 |
|
|
168 | #endif |
|
|
169 | #ifndef ST_NODIRATIME |
|
|
170 | # define ST_NODIRATIME 0 |
|
|
171 | #endif |
|
|
172 | #ifndef ST_RELATIME |
|
|
173 | # define ST_RELATIME 0 |
|
|
174 | #endif |
|
|
175 | |
|
|
176 | #ifndef MCL_CURRENT |
|
|
177 | # define MCL_CURRENT 0 |
|
|
178 | #endif |
|
|
179 | #ifndef MCL_FUTURE |
|
|
180 | # define MCL_FUTURE 0 |
|
|
181 | #endif |
|
|
182 | |
|
|
183 | #ifndef MAP_ANONYMOUS |
|
|
184 | # ifdef MAP_ANON |
|
|
185 | # define MAP_ANONYMOUS MAP_ANON |
|
|
186 | # else |
|
|
187 | # define MAP_ANONYMOUS MAP_FIXED /* and hope this fails */ |
24 | #endif |
188 | # endif |
|
|
189 | #endif |
|
|
190 | #ifndef MAP_HUGETLB |
|
|
191 | # define MAP_HUGETLB 0 |
|
|
192 | #endif |
|
|
193 | #ifndef MAP_LOCKED |
|
|
194 | # define MAP_LOCKED 0 |
|
|
195 | #endif |
|
|
196 | #ifndef MAP_NORESERVE |
|
|
197 | # define MAP_NORESERVE 0 |
|
|
198 | #endif |
|
|
199 | #ifndef MAP_POPULATE |
|
|
200 | # define MAP_POPULATE 0 |
|
|
201 | #endif |
|
|
202 | #ifndef MAP_NONBLOCK |
|
|
203 | # define MAP_NONBLOCK 0 |
|
|
204 | #endif |
|
|
205 | |
|
|
206 | #ifndef PAGESIZE |
|
|
207 | # define PAGESIZE sysconf (_SC_PAGESIZE) |
|
|
208 | #endif |
|
|
209 | |
|
|
210 | static int req_invoke (eio_req *req); |
|
|
211 | #define EIO_FINISH(req) req_invoke (req) |
|
|
212 | static void req_destroy (eio_req *grp); |
|
|
213 | #define EIO_DESTROY(req) req_destroy (req) |
25 | |
214 | |
26 | enum { |
215 | enum { |
27 | REQ_QUIT, |
216 | FLAG_SV2_RO_OFF = 0x40, /* data was set readonly */ |
28 | REQ_OPEN, REQ_CLOSE, |
|
|
29 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
|
|
30 | REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK, |
|
|
31 | REQ_FSYNC, REQ_FDATASYNC, |
|
|
32 | }; |
217 | }; |
33 | |
218 | |
34 | typedef struct aio_cb { |
219 | #include "libeio/eio.c" |
35 | struct aio_cb *volatile next; |
|
|
36 | |
220 | |
37 | int type; |
|
|
38 | |
|
|
39 | int fd; |
|
|
40 | off_t offset; |
|
|
41 | size_t length; |
|
|
42 | ssize_t result; |
|
|
43 | mode_t mode; /* open */ |
|
|
44 | int errorno; |
|
|
45 | SV *data, *callback; |
|
|
46 | void *dataptr; |
|
|
47 | STRLEN dataoffset; |
|
|
48 | |
|
|
49 | Stat_t *statdata; |
|
|
50 | } aio_cb; |
|
|
51 | |
|
|
52 | typedef aio_cb *aio_req; |
221 | typedef eio_req *aio_req; |
|
|
222 | typedef eio_req *aio_req_ornot; |
53 | |
223 | |
54 | static int started; |
224 | static SV *on_next_submit; |
55 | static int nreqs; |
225 | static int next_pri = EIO_PRI_DEFAULT; |
56 | static int max_outstanding = 1<<30; |
226 | static int max_outstanding; |
57 | static int respipe [2]; |
|
|
58 | |
227 | |
59 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
228 | static s_epipe respipe; |
60 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
|
|
61 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
|
|
62 | |
229 | |
63 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
230 | static void req_destroy (aio_req req); |
64 | static volatile aio_req ress, rese; /* queue start, queue end */ |
231 | static void req_cancel (aio_req req); |
65 | |
232 | |
66 | static void |
233 | static void want_poll (void) |
67 | poll_wait () |
|
|
68 | { |
234 | { |
69 | if (!nreqs) |
235 | /* write a dummy byte to the pipe so fh becomes ready */ |
70 | return; |
236 | s_epipe_signal (&respipe); |
71 | |
|
|
72 | fd_set rfd; |
|
|
73 | FD_ZERO(&rfd); |
|
|
74 | FD_SET(respipe [0], &rfd); |
|
|
75 | |
|
|
76 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
|
|
77 | } |
237 | } |
78 | |
238 | |
79 | static int |
239 | static void done_poll (void) |
80 | poll_cb () |
|
|
81 | { |
240 | { |
82 | dSP; |
|
|
83 | int count = 0; |
|
|
84 | aio_req req; |
|
|
85 | |
|
|
86 | { |
|
|
87 | /* read and signals sent by the worker threads */ |
241 | /* read any signals sent by the worker threads */ |
88 | char buf [32]; |
242 | s_epipe_drain (&respipe); |
89 | while (read (respipe [0], buf, 32) > 0) |
243 | } |
90 | ; |
|
|
91 | } |
|
|
92 | |
244 | |
93 | for (;;) |
245 | /* must be called at most once */ |
|
|
246 | static SV *req_sv (aio_req req, HV *stash) |
|
|
247 | { |
|
|
248 | if (!req->self) |
94 | { |
249 | { |
95 | pthread_mutex_lock (&reslock); |
250 | req->self = (SV *)newHV (); |
|
|
251 | sv_magic (req->self, 0, PERL_MAGIC_ext, (char *)req, 0); |
|
|
252 | } |
96 | |
253 | |
97 | req = ress; |
254 | return sv_2mortal (sv_bless (newRV_inc (req->self), stash)); |
|
|
255 | } |
98 | |
256 | |
99 | if (ress) |
257 | static aio_req SvAIO_REQ (SV *sv) |
|
|
258 | { |
|
|
259 | MAGIC *mg; |
|
|
260 | |
|
|
261 | if (!SvROK (sv) |
|
|
262 | || (SvSTASH (SvRV (sv)) != aio_grp_stash |
|
|
263 | && SvSTASH (SvRV (sv)) != aio_req_stash |
|
|
264 | && !sv_derived_from (sv, "IO::AIO::REQ"))) |
|
|
265 | croak ("object of class IO::AIO::REQ expected"); |
|
|
266 | |
|
|
267 | mg = mg_find (SvRV (sv), PERL_MAGIC_ext); |
|
|
268 | |
|
|
269 | return mg ? (aio_req)mg->mg_ptr : 0; |
|
|
270 | } |
|
|
271 | |
|
|
272 | static void aio_grp_feed (aio_req grp) |
|
|
273 | { |
|
|
274 | if (grp->sv2 && SvOK (grp->sv2)) |
|
|
275 | { |
|
|
276 | dSP; |
|
|
277 | |
|
|
278 | ENTER; |
|
|
279 | SAVETMPS; |
|
|
280 | PUSHMARK (SP); |
|
|
281 | XPUSHs (req_sv (grp, aio_grp_stash)); |
|
|
282 | PUTBACK; |
|
|
283 | call_sv (grp->sv2, G_VOID | G_EVAL | G_KEEPERR); |
|
|
284 | SPAGAIN; |
|
|
285 | FREETMPS; |
|
|
286 | LEAVE; |
|
|
287 | } |
|
|
288 | } |
|
|
289 | |
|
|
290 | static void req_submit (eio_req *req) |
|
|
291 | { |
|
|
292 | eio_submit (req); |
|
|
293 | |
|
|
294 | if (expect_false (on_next_submit)) |
|
|
295 | { |
|
|
296 | dSP; |
|
|
297 | SV *cb = sv_2mortal (on_next_submit); |
|
|
298 | |
|
|
299 | on_next_submit = 0; |
|
|
300 | |
|
|
301 | PUSHMARK (SP); |
|
|
302 | PUTBACK; |
|
|
303 | call_sv (cb, G_DISCARD | G_EVAL); |
|
|
304 | } |
|
|
305 | } |
|
|
306 | |
|
|
307 | static int req_invoke (eio_req *req) |
|
|
308 | { |
|
|
309 | if (req->flags & FLAG_SV2_RO_OFF) |
|
|
310 | SvREADONLY_off (req->sv2); |
|
|
311 | |
|
|
312 | if (!EIO_CANCELLED (req) && req->callback) |
|
|
313 | { |
|
|
314 | dSP; |
|
|
315 | static SV *sv_result_cache; /* caches the result integer SV */ |
|
|
316 | SV *sv_result; |
|
|
317 | |
|
|
318 | ENTER; |
|
|
319 | SAVETMPS; |
|
|
320 | PUSHMARK (SP); |
|
|
321 | EXTEND (SP, 1); |
|
|
322 | |
|
|
323 | /* do not recreate the result IV from scratch each time */ |
|
|
324 | if (expect_true (sv_result_cache)) |
100 | { |
325 | { |
101 | ress = ress->next; |
326 | sv_result = sv_result_cache; sv_result_cache = 0; |
102 | if (!ress) rese = 0; |
327 | SvIV_set (sv_result, req->result); |
103 | } |
|
|
104 | |
|
|
105 | pthread_mutex_unlock (&reslock); |
|
|
106 | |
|
|
107 | if (!req) |
|
|
108 | break; |
|
|
109 | |
|
|
110 | nreqs--; |
|
|
111 | |
|
|
112 | if (req->type == REQ_QUIT) |
|
|
113 | started--; |
|
|
114 | else |
|
|
115 | { |
|
|
116 | int errorno = errno; |
|
|
117 | errno = req->errorno; |
|
|
118 | |
|
|
119 | if (req->type == REQ_READ) |
|
|
120 | SvCUR_set (req->data, req->dataoffset |
|
|
121 | + req->result > 0 ? req->result : 0); |
|
|
122 | |
|
|
123 | if (req->data) |
|
|
124 | SvREFCNT_dec (req->data); |
|
|
125 | |
|
|
126 | if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) |
|
|
127 | { |
|
|
128 | PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
129 | PL_laststatval = req->result; |
|
|
130 | PL_statcache = *(req->statdata); |
|
|
131 | |
|
|
132 | Safefree (req->statdata); |
|
|
133 | } |
|
|
134 | |
|
|
135 | PUSHMARK (SP); |
|
|
136 | XPUSHs (sv_2mortal (newSViv (req->result))); |
|
|
137 | |
|
|
138 | if (req->type == REQ_OPEN) |
|
|
139 | { |
|
|
140 | /* convert fd to fh */ |
|
|
141 | SV *fh; |
|
|
142 | |
|
|
143 | PUTBACK; |
|
|
144 | call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL); |
|
|
145 | SPAGAIN; |
|
|
146 | |
|
|
147 | fh = POPs; |
|
|
148 | |
|
|
149 | PUSHMARK (SP); |
|
|
150 | XPUSHs (fh); |
|
|
151 | } |
|
|
152 | |
|
|
153 | PUTBACK; |
|
|
154 | call_sv (req->callback, G_VOID | G_EVAL); |
|
|
155 | SPAGAIN; |
|
|
156 | |
|
|
157 | if (req->callback) |
|
|
158 | SvREFCNT_dec (req->callback); |
|
|
159 | |
|
|
160 | errno = errorno; |
|
|
161 | count++; |
|
|
162 | } |
|
|
163 | |
|
|
164 | Safefree (req); |
|
|
165 | } |
|
|
166 | |
|
|
167 | return count; |
|
|
168 | } |
|
|
169 | |
|
|
170 | static void *aio_proc(void *arg); |
|
|
171 | |
|
|
172 | static void |
|
|
173 | start_thread (void) |
|
|
174 | { |
|
|
175 | sigset_t fullsigset, oldsigset; |
|
|
176 | pthread_t tid; |
|
|
177 | pthread_attr_t attr; |
|
|
178 | |
|
|
179 | pthread_attr_init (&attr); |
|
|
180 | pthread_attr_setstacksize (&attr, STACKSIZE); |
|
|
181 | pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); |
|
|
182 | |
|
|
183 | sigfillset (&fullsigset); |
|
|
184 | sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset); |
|
|
185 | |
|
|
186 | if (pthread_create (&tid, &attr, aio_proc, 0) == 0) |
|
|
187 | started++; |
|
|
188 | |
|
|
189 | sigprocmask (SIG_SETMASK, &oldsigset, 0); |
|
|
190 | } |
|
|
191 | |
|
|
192 | static void |
|
|
193 | send_req (aio_req req) |
|
|
194 | { |
|
|
195 | nreqs++; |
|
|
196 | |
|
|
197 | pthread_mutex_lock (&reqlock); |
|
|
198 | |
|
|
199 | req->next = 0; |
|
|
200 | |
|
|
201 | if (reqe) |
|
|
202 | { |
|
|
203 | reqe->next = req; |
|
|
204 | reqe = req; |
|
|
205 | } |
|
|
206 | else |
|
|
207 | reqe = reqs = req; |
|
|
208 | |
|
|
209 | pthread_cond_signal (&reqwait); |
|
|
210 | pthread_mutex_unlock (&reqlock); |
|
|
211 | |
|
|
212 | while (nreqs > max_outstanding) |
|
|
213 | { |
|
|
214 | poll_wait (); |
|
|
215 | poll_cb (); |
|
|
216 | } |
|
|
217 | } |
|
|
218 | |
|
|
219 | static void |
|
|
220 | end_thread (void) |
|
|
221 | { |
|
|
222 | aio_req req; |
|
|
223 | New (0, req, 1, aio_cb); |
|
|
224 | req->type = REQ_QUIT; |
|
|
225 | |
|
|
226 | send_req (req); |
|
|
227 | } |
|
|
228 | |
|
|
229 | static void |
|
|
230 | read_write (int dowrite, int fd, off_t offset, size_t length, |
|
|
231 | SV *data, STRLEN dataoffset, SV *callback) |
|
|
232 | { |
|
|
233 | aio_req req; |
|
|
234 | STRLEN svlen; |
|
|
235 | char *svptr = SvPV (data, svlen); |
|
|
236 | |
|
|
237 | SvUPGRADE (data, SVt_PV); |
|
|
238 | SvPOK_on (data); |
|
|
239 | |
|
|
240 | if (dataoffset < 0) |
|
|
241 | dataoffset += svlen; |
|
|
242 | |
|
|
243 | if (dataoffset < 0 || dataoffset > svlen) |
|
|
244 | croak ("data offset outside of string"); |
|
|
245 | |
|
|
246 | if (dowrite) |
|
|
247 | { |
|
|
248 | /* write: check length and adjust. */ |
|
|
249 | if (length < 0 || length + dataoffset > svlen) |
|
|
250 | length = svlen - dataoffset; |
|
|
251 | } |
|
|
252 | else |
|
|
253 | { |
|
|
254 | /* read: grow scalar as necessary */ |
|
|
255 | svptr = SvGROW (data, length + dataoffset); |
|
|
256 | } |
|
|
257 | |
|
|
258 | if (length < 0) |
|
|
259 | croak ("length must not be negative"); |
|
|
260 | |
|
|
261 | Newz (0, req, 1, aio_cb); |
|
|
262 | |
|
|
263 | if (!req) |
|
|
264 | croak ("out of memory during aio_req allocation"); |
|
|
265 | |
|
|
266 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
|
|
267 | req->fd = fd; |
|
|
268 | req->offset = offset; |
|
|
269 | req->length = length; |
|
|
270 | req->data = SvREFCNT_inc (data); |
|
|
271 | req->dataptr = (char *)svptr + dataoffset; |
|
|
272 | req->callback = SvREFCNT_inc (callback); |
|
|
273 | |
|
|
274 | send_req (req); |
|
|
275 | } |
|
|
276 | |
|
|
277 | static void * |
|
|
278 | aio_proc (void *thr_arg) |
|
|
279 | { |
|
|
280 | aio_req req; |
|
|
281 | int type; |
|
|
282 | |
|
|
283 | do |
|
|
284 | { |
|
|
285 | pthread_mutex_lock (&reqlock); |
|
|
286 | |
|
|
287 | for (;;) |
|
|
288 | { |
|
|
289 | req = reqs; |
|
|
290 | |
|
|
291 | if (reqs) |
|
|
292 | { |
|
|
293 | reqs = reqs->next; |
|
|
294 | if (!reqs) reqe = 0; |
|
|
295 | } |
|
|
296 | |
|
|
297 | if (req) |
|
|
298 | break; |
|
|
299 | |
|
|
300 | pthread_cond_wait (&reqwait, &reqlock); |
|
|
301 | } |
|
|
302 | |
|
|
303 | pthread_mutex_unlock (&reqlock); |
|
|
304 | |
|
|
305 | errno = 0; /* strictly unnecessary */ |
|
|
306 | |
|
|
307 | type = req->type; |
|
|
308 | |
|
|
309 | switch (type) |
|
|
310 | { |
|
|
311 | case REQ_READ: req->result = pread64 (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
312 | case REQ_WRITE: req->result = pwrite64 (req->fd, req->dataptr, req->length, req->offset); break; |
|
|
313 | #if SYS_readahead |
|
|
314 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
|
|
315 | #else |
|
|
316 | case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break; |
|
|
317 | #endif |
|
|
318 | |
|
|
319 | case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break; |
|
|
320 | case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break; |
|
|
321 | case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break; |
|
|
322 | |
|
|
323 | case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break; |
|
|
324 | case REQ_CLOSE: req->result = close (req->fd); break; |
|
|
325 | case REQ_UNLINK: req->result = unlink (req->dataptr); break; |
|
|
326 | |
|
|
327 | case REQ_FSYNC: req->result = fsync (req->fd); break; |
|
|
328 | case REQ_FDATASYNC: req->result = fdatasync (req->fd); break; |
|
|
329 | |
|
|
330 | case REQ_QUIT: |
|
|
331 | break; |
|
|
332 | |
|
|
333 | default: |
|
|
334 | req->result = ENOSYS; |
|
|
335 | break; |
|
|
336 | } |
|
|
337 | |
|
|
338 | req->errorno = errno; |
|
|
339 | |
|
|
340 | pthread_mutex_lock (&reslock); |
|
|
341 | |
|
|
342 | req->next = 0; |
|
|
343 | |
|
|
344 | if (rese) |
|
|
345 | { |
|
|
346 | rese->next = req; |
|
|
347 | rese = req; |
|
|
348 | } |
328 | } |
349 | else |
329 | else |
350 | { |
330 | { |
351 | rese = ress = req; |
331 | sv_result = newSViv (req->result); |
352 | |
332 | SvREADONLY_on (sv_result); |
353 | /* write a dummy byte to the pipe so fh becomes ready */ |
|
|
354 | write (respipe [1], &respipe, 1); |
|
|
355 | } |
333 | } |
356 | |
334 | |
357 | pthread_mutex_unlock (&reslock); |
335 | switch (req->type) |
|
|
336 | { |
|
|
337 | case EIO_READDIR: |
|
|
338 | { |
|
|
339 | SV *rv = &PL_sv_undef; |
|
|
340 | |
|
|
341 | if (req->result >= 0) |
|
|
342 | { |
|
|
343 | int i; |
|
|
344 | char *names = (char *)req->ptr2; |
|
|
345 | eio_dirent *ent = (eio_dirent *)req->ptr1; /* might be 0 */ |
|
|
346 | AV *av = newAV (); |
|
|
347 | |
|
|
348 | av_extend (av, req->result - 1); |
|
|
349 | |
|
|
350 | for (i = 0; i < req->result; ++i) |
|
|
351 | { |
|
|
352 | if (req->int1 & EIO_READDIR_DENTS) |
|
|
353 | { |
|
|
354 | SV *namesv = newSVpvn (names + ent->nameofs, ent->namelen); |
|
|
355 | |
|
|
356 | if (req->int1 & EIO_READDIR_CUSTOM2) |
|
|
357 | { |
|
|
358 | static SV *sv_type [EIO_DT_MAX + 1]; /* type sv cache */ |
|
|
359 | AV *avent = newAV (); |
|
|
360 | |
|
|
361 | av_extend (avent, 2); |
|
|
362 | |
|
|
363 | if (!sv_type [ent->type]) |
|
|
364 | { |
|
|
365 | sv_type [ent->type] = newSViv (ent->type); |
|
|
366 | SvREADONLY_on (sv_type [ent->type]); |
|
|
367 | } |
|
|
368 | |
|
|
369 | av_store (avent, 0, namesv); |
|
|
370 | av_store (avent, 1, SvREFCNT_inc (sv_type [ent->type])); |
|
|
371 | av_store (avent, 2, IVSIZE >= 8 ? newSVuv (ent->inode) : newSVnv (ent->inode)); |
|
|
372 | |
|
|
373 | av_store (av, i, newRV_noinc ((SV *)avent)); |
|
|
374 | } |
|
|
375 | else |
|
|
376 | av_store (av, i, namesv); |
|
|
377 | |
|
|
378 | ++ent; |
|
|
379 | } |
|
|
380 | else |
|
|
381 | { |
|
|
382 | SV *name = newSVpv (names, 0); |
|
|
383 | av_store (av, i, name); |
|
|
384 | names += SvCUR (name) + 1; |
|
|
385 | } |
|
|
386 | } |
|
|
387 | |
|
|
388 | rv = sv_2mortal (newRV_noinc ((SV *)av)); |
|
|
389 | } |
|
|
390 | |
|
|
391 | PUSHs (rv); |
|
|
392 | |
|
|
393 | if (req->int1 & EIO_READDIR_CUSTOM1) |
|
|
394 | XPUSHs (sv_2mortal (newSViv (req->int1 & ~(EIO_READDIR_CUSTOM1 | EIO_READDIR_CUSTOM2)))); |
|
|
395 | } |
|
|
396 | break; |
|
|
397 | |
|
|
398 | case EIO_OPEN: |
|
|
399 | { |
|
|
400 | /* convert fd to fh */ |
|
|
401 | SV *fh = &PL_sv_undef; |
|
|
402 | |
|
|
403 | if (req->result >= 0) |
|
|
404 | { |
|
|
405 | GV *gv = (GV *)sv_newmortal (); |
|
|
406 | int flags = req->int1 & (O_RDONLY | O_WRONLY | O_RDWR); |
|
|
407 | char sym [64]; |
|
|
408 | int symlen; |
|
|
409 | |
|
|
410 | symlen = snprintf (sym, sizeof (sym), "fd#%d", (int)req->result); |
|
|
411 | gv_init (gv, aio_stash, sym, symlen, 0); |
|
|
412 | |
|
|
413 | symlen = snprintf ( |
|
|
414 | sym, |
|
|
415 | sizeof (sym), |
|
|
416 | "%s&=%d", |
|
|
417 | flags == O_RDONLY ? "<" : flags == O_WRONLY ? ">" : "+<", |
|
|
418 | (int)req->result |
|
|
419 | ); |
|
|
420 | |
|
|
421 | if (do_open (gv, sym, symlen, 0, 0, 0, 0)) |
|
|
422 | fh = (SV *)gv; |
|
|
423 | } |
|
|
424 | |
|
|
425 | PUSHs (fh); |
|
|
426 | } |
|
|
427 | break; |
|
|
428 | |
|
|
429 | case EIO_STATVFS: |
|
|
430 | case EIO_FSTATVFS: |
|
|
431 | { |
|
|
432 | SV *rv = &PL_sv_undef; |
|
|
433 | |
|
|
434 | if (req->result >= 0) |
|
|
435 | { |
|
|
436 | EIO_STRUCT_STATVFS *f = EIO_STATVFS_BUF (req); |
|
|
437 | HV *hv = newHV (); |
|
|
438 | |
|
|
439 | rv = sv_2mortal (newRV_noinc ((SV *)hv)); |
|
|
440 | |
|
|
441 | hv_store (hv, "bsize" , sizeof ("bsize" ) - 1, newSVval64 (f->f_bsize ), 0); |
|
|
442 | hv_store (hv, "frsize" , sizeof ("frsize" ) - 1, newSVval64 (f->f_frsize ), 0); |
|
|
443 | hv_store (hv, "blocks" , sizeof ("blocks" ) - 1, newSVval64 (f->f_blocks ), 0); |
|
|
444 | hv_store (hv, "bfree" , sizeof ("bfree" ) - 1, newSVval64 (f->f_bfree ), 0); |
|
|
445 | hv_store (hv, "bavail" , sizeof ("bavail" ) - 1, newSVval64 (f->f_bavail ), 0); |
|
|
446 | hv_store (hv, "files" , sizeof ("files" ) - 1, newSVval64 (f->f_files ), 0); |
|
|
447 | hv_store (hv, "ffree" , sizeof ("ffree" ) - 1, newSVval64 (f->f_ffree ), 0); |
|
|
448 | hv_store (hv, "favail" , sizeof ("favail" ) - 1, newSVval64 (f->f_favail ), 0); |
|
|
449 | hv_store (hv, "fsid" , sizeof ("fsid" ) - 1, newSVval64 (f->f_fsid ), 0); |
|
|
450 | hv_store (hv, "flag" , sizeof ("flag" ) - 1, newSVval64 (f->f_flag ), 0); |
|
|
451 | hv_store (hv, "namemax", sizeof ("namemax") - 1, newSVval64 (f->f_namemax), 0); |
|
|
452 | } |
|
|
453 | |
|
|
454 | PUSHs (rv); |
|
|
455 | } |
|
|
456 | |
|
|
457 | break; |
|
|
458 | |
|
|
459 | case EIO_GROUP: |
|
|
460 | req->int1 = 2; /* mark group as finished */ |
|
|
461 | |
|
|
462 | if (req->sv1) |
|
|
463 | { |
|
|
464 | int i; |
|
|
465 | AV *av = (AV *)req->sv1; |
|
|
466 | |
|
|
467 | EXTEND (SP, AvFILL (av) + 1); |
|
|
468 | for (i = 0; i <= AvFILL (av); ++i) |
|
|
469 | PUSHs (*av_fetch (av, i, 0)); |
|
|
470 | } |
|
|
471 | break; |
|
|
472 | |
|
|
473 | case EIO_NOP: |
|
|
474 | case EIO_BUSY: |
|
|
475 | break; |
|
|
476 | |
|
|
477 | case EIO_READLINK: |
|
|
478 | if (req->result > 0) |
|
|
479 | PUSHs (sv_2mortal (newSVpvn (req->ptr2, req->result))); |
|
|
480 | break; |
|
|
481 | |
|
|
482 | case EIO_STAT: |
|
|
483 | case EIO_LSTAT: |
|
|
484 | case EIO_FSTAT: |
|
|
485 | PL_laststype = req->type == EIO_LSTAT ? OP_LSTAT : OP_STAT; |
|
|
486 | PL_laststatval = req->result; |
|
|
487 | PL_statcache = *(EIO_STRUCT_STAT *)(req->ptr2); |
|
|
488 | PUSHs (sv_result); |
|
|
489 | break; |
|
|
490 | |
|
|
491 | case EIO_READ: |
|
|
492 | { |
|
|
493 | SvCUR_set (req->sv2, req->stroffset + (req->result > 0 ? req->result : 0)); |
|
|
494 | *SvEND (req->sv2) = 0; |
|
|
495 | SvPOK_only (req->sv2); |
|
|
496 | SvSETMAGIC (req->sv2); |
|
|
497 | PUSHs (sv_result); |
|
|
498 | } |
|
|
499 | break; |
|
|
500 | |
|
|
501 | case EIO_DUP2: /* EIO_DUP2 actually means aio_close(), su fudge result value */ |
|
|
502 | if (req->result > 0) |
|
|
503 | SvIV_set (sv_result, 0); |
|
|
504 | /* FALLTHROUGH */ |
|
|
505 | |
|
|
506 | default: |
|
|
507 | PUSHs (sv_result); |
|
|
508 | break; |
|
|
509 | } |
|
|
510 | |
|
|
511 | errno = req->errorno; |
|
|
512 | |
|
|
513 | PUTBACK; |
|
|
514 | call_sv (req->callback, G_VOID | G_EVAL | G_DISCARD); |
|
|
515 | SPAGAIN; |
|
|
516 | |
|
|
517 | if (expect_false (SvREFCNT (sv_result) != 1 || sv_result_cache)) |
|
|
518 | SvREFCNT_dec (sv_result); |
|
|
519 | else |
|
|
520 | sv_result_cache = sv_result; |
|
|
521 | |
|
|
522 | FREETMPS; |
|
|
523 | LEAVE; |
|
|
524 | |
|
|
525 | PUTBACK; |
358 | } |
526 | } |
359 | while (type != REQ_QUIT); |
527 | |
|
|
528 | return !!SvTRUE (ERRSV); |
|
|
529 | } |
|
|
530 | |
|
|
531 | static void req_destroy (aio_req req) |
|
|
532 | { |
|
|
533 | if (req->self) |
|
|
534 | { |
|
|
535 | sv_unmagic (req->self, PERL_MAGIC_ext); |
|
|
536 | SvREFCNT_dec (req->self); |
|
|
537 | } |
|
|
538 | |
|
|
539 | SvREFCNT_dec (req->sv1); |
|
|
540 | SvREFCNT_dec (req->sv2); |
|
|
541 | SvREFCNT_dec (req->callback); |
|
|
542 | |
|
|
543 | Safefree (req); |
|
|
544 | } |
|
|
545 | |
|
|
546 | static void req_cancel_subs (aio_req grp) |
|
|
547 | { |
|
|
548 | aio_req sub; |
|
|
549 | |
|
|
550 | if (grp->type != EIO_GROUP) |
|
|
551 | return; |
|
|
552 | |
|
|
553 | SvREFCNT_dec (grp->sv2); |
|
|
554 | grp->sv2 = 0; |
|
|
555 | |
|
|
556 | eio_grp_cancel (grp); |
|
|
557 | } |
|
|
558 | |
|
|
559 | static void |
|
|
560 | create_respipe (void) |
|
|
561 | { |
|
|
562 | if (s_epipe_renew (&respipe)) |
|
|
563 | croak ("IO::AIO: unable to initialize result pipe"); |
|
|
564 | } |
|
|
565 | |
|
|
566 | static void poll_wait (void) |
|
|
567 | { |
|
|
568 | while (eio_nreqs ()) |
|
|
569 | { |
|
|
570 | int size; |
|
|
571 | |
|
|
572 | X_LOCK (reslock); |
|
|
573 | size = res_queue.size; |
|
|
574 | X_UNLOCK (reslock); |
|
|
575 | |
|
|
576 | if (size) |
|
|
577 | return; |
|
|
578 | |
|
|
579 | etp_maybe_start_thread (); |
|
|
580 | |
|
|
581 | s_epipe_wait (&respipe); |
|
|
582 | } |
|
|
583 | } |
|
|
584 | |
|
|
585 | static int poll_cb (void) |
|
|
586 | { |
|
|
587 | for (;;) |
|
|
588 | { |
|
|
589 | int res = eio_poll (); |
|
|
590 | |
|
|
591 | if (res > 0) |
|
|
592 | croak (0); |
|
|
593 | |
|
|
594 | if (!max_outstanding || max_outstanding > eio_nreqs ()) |
|
|
595 | return res; |
|
|
596 | |
|
|
597 | poll_wait (); |
|
|
598 | } |
|
|
599 | } |
|
|
600 | |
|
|
601 | static void atfork_child (void) |
|
|
602 | { |
|
|
603 | create_respipe (); |
|
|
604 | } |
|
|
605 | |
|
|
606 | /*****************************************************************************/ |
|
|
607 | |
|
|
608 | #if !_POSIX_MAPPED_FILES |
|
|
609 | # define mmap(addr,length,prot,flags,fd,offs) (errno = ENOSYS, -1) |
|
|
610 | # define munmap(addr,length) (errno = ENOSYS, -1) |
|
|
611 | #endif |
|
|
612 | |
|
|
613 | #define MMAP_MAGIC PERL_MAGIC_ext |
|
|
614 | |
|
|
615 | static int |
|
|
616 | mmap_free (pTHX_ SV *sv, MAGIC *mg) |
|
|
617 | { |
|
|
618 | int old_errno = errno; |
|
|
619 | munmap (mg->mg_ptr, (size_t)mg->mg_obj); |
|
|
620 | errno = old_errno; |
|
|
621 | |
|
|
622 | mg->mg_obj = 0; /* just in case */ |
|
|
623 | |
|
|
624 | SvREADONLY_off (sv); |
|
|
625 | |
|
|
626 | if (SvPVX (sv) != mg->mg_ptr) |
|
|
627 | croak ("ERROR: IO::AIO::mmap-mapped scalar changed location, detected"); |
|
|
628 | |
|
|
629 | SvCUR_set (sv, 0); |
|
|
630 | SvPVX (sv) = 0; |
|
|
631 | SvOK_off (sv); |
360 | |
632 | |
361 | return 0; |
633 | return 0; |
362 | } |
634 | } |
363 | |
635 | |
|
|
636 | static MGVTBL mmap_vtbl = { |
|
|
637 | 0, 0, 0, 0, mmap_free |
|
|
638 | }; |
|
|
639 | |
|
|
640 | /*****************************************************************************/ |
|
|
641 | |
|
|
642 | static SV * |
|
|
643 | get_cb (SV *cb_sv) |
|
|
644 | { |
|
|
645 | SvGETMAGIC (cb_sv); |
|
|
646 | return SvOK (cb_sv) ? s_get_cv_croak (cb_sv) : 0; |
|
|
647 | } |
|
|
648 | |
|
|
649 | #define dREQ \ |
|
|
650 | SV *cb_cv; \ |
|
|
651 | aio_req req; \ |
|
|
652 | int req_pri = next_pri; \ |
|
|
653 | next_pri = EIO_PRI_DEFAULT; \ |
|
|
654 | \ |
|
|
655 | cb_cv = get_cb (callback); \ |
|
|
656 | \ |
|
|
657 | Newz (0, req, 1, eio_req); \ |
|
|
658 | if (!req) \ |
|
|
659 | croak ("out of memory during eio_req allocation"); \ |
|
|
660 | \ |
|
|
661 | req->callback = SvREFCNT_inc (cb_cv); \ |
|
|
662 | req->pri = req_pri |
|
|
663 | |
|
|
664 | #define REQ_SEND \ |
|
|
665 | PUTBACK; \ |
|
|
666 | req_submit (req); \ |
|
|
667 | SPAGAIN; \ |
|
|
668 | \ |
|
|
669 | if (GIMME_V != G_VOID) \ |
|
|
670 | XPUSHs (req_sv (req, aio_req_stash)); |
|
|
671 | |
364 | MODULE = IO::AIO PACKAGE = IO::AIO |
672 | MODULE = IO::AIO PACKAGE = IO::AIO |
365 | |
673 | |
|
|
674 | PROTOTYPES: ENABLE |
|
|
675 | |
366 | BOOT: |
676 | BOOT: |
367 | { |
677 | { |
368 | if (pipe (respipe)) |
678 | static const struct { |
369 | croak ("unable to initialize result pipe"); |
679 | const char *name; |
|
|
680 | IV iv; |
|
|
681 | } *civ, const_iv[] = { |
|
|
682 | # define const_niv(name, value) { # name, (IV) value }, |
|
|
683 | # define const_iv(name) { # name, (IV) name }, |
|
|
684 | # define const_eio(name) { # name, (IV) EIO_ ## name }, |
|
|
685 | const_iv (EXDEV) |
|
|
686 | const_iv (ENOSYS) |
|
|
687 | const_iv (O_RDONLY) |
|
|
688 | const_iv (O_WRONLY) |
|
|
689 | const_iv (O_RDWR) |
|
|
690 | const_iv (O_CREAT) |
|
|
691 | const_iv (O_TRUNC) |
|
|
692 | const_iv (O_EXCL) |
|
|
693 | const_iv (O_APPEND) |
|
|
694 | #ifndef _WIN32 |
|
|
695 | const_iv (S_IFIFO) |
|
|
696 | #endif |
|
|
697 | const_niv (FADV_NORMAL , POSIX_FADV_NORMAL) |
|
|
698 | const_niv (FADV_SEQUENTIAL, POSIX_FADV_SEQUENTIAL) |
|
|
699 | const_niv (FADV_RANDOM , POSIX_FADV_RANDOM) |
|
|
700 | const_niv (FADV_NOREUSE , POSIX_FADV_NOREUSE) |
|
|
701 | const_niv (FADV_WILLNEED , POSIX_FADV_WILLNEED) |
|
|
702 | const_niv (FADV_DONTNEED , POSIX_FADV_DONTNEED) |
370 | |
703 | |
371 | if (fcntl (respipe [0], F_SETFL, O_NONBLOCK)) |
704 | const_iv (ST_RDONLY) |
372 | croak ("cannot set result pipe to nonblocking mode"); |
705 | const_iv (ST_NOSUID) |
|
|
706 | const_iv (ST_NODEV) |
|
|
707 | const_iv (ST_NOEXEC) |
|
|
708 | const_iv (ST_SYNCHRONOUS) |
|
|
709 | const_iv (ST_MANDLOCK) |
|
|
710 | const_iv (ST_WRITE) |
|
|
711 | const_iv (ST_APPEND) |
|
|
712 | const_iv (ST_IMMUTABLE) |
|
|
713 | const_iv (ST_NOATIME) |
|
|
714 | const_iv (ST_NODIRATIME) |
|
|
715 | const_iv (ST_RELATIME) |
373 | |
716 | |
374 | if (fcntl (respipe [1], F_SETFL, O_NONBLOCK)) |
717 | const_iv (PROT_EXEC) |
375 | croak ("cannot set result pipe to nonblocking mode"); |
718 | const_iv (PROT_NONE) |
376 | } |
719 | const_iv (PROT_READ) |
|
|
720 | const_iv (PROT_WRITE) |
377 | |
721 | |
|
|
722 | /*const_iv (MAP_FIXED)*/ |
|
|
723 | const_iv (MAP_PRIVATE) |
|
|
724 | const_iv (MAP_SHARED) |
|
|
725 | const_iv (MAP_ANONYMOUS) |
|
|
726 | |
|
|
727 | /* linuxish */ |
|
|
728 | const_iv (MAP_HUGETLB) |
|
|
729 | const_iv (MAP_LOCKED) |
|
|
730 | const_iv (MAP_NORESERVE) |
|
|
731 | const_iv (MAP_POPULATE) |
|
|
732 | const_iv (MAP_NONBLOCK) |
|
|
733 | |
|
|
734 | const_iv (MCL_FUTURE) |
|
|
735 | const_iv (MCL_CURRENT) |
|
|
736 | |
|
|
737 | const_eio (MS_ASYNC) |
|
|
738 | const_eio (MS_INVALIDATE) |
|
|
739 | const_eio (MS_SYNC) |
|
|
740 | |
|
|
741 | const_eio (MT_MODIFY) |
|
|
742 | |
|
|
743 | const_eio (SYNC_FILE_RANGE_WAIT_BEFORE) |
|
|
744 | const_eio (SYNC_FILE_RANGE_WRITE) |
|
|
745 | const_eio (SYNC_FILE_RANGE_WAIT_AFTER) |
|
|
746 | |
|
|
747 | const_eio (READDIR_DENTS) |
|
|
748 | const_eio (READDIR_DIRS_FIRST) |
|
|
749 | const_eio (READDIR_STAT_ORDER) |
|
|
750 | const_eio (READDIR_FOUND_UNKNOWN) |
|
|
751 | |
|
|
752 | const_eio (DT_UNKNOWN) |
|
|
753 | const_eio (DT_FIFO) |
|
|
754 | const_eio (DT_CHR) |
|
|
755 | const_eio (DT_DIR) |
|
|
756 | const_eio (DT_BLK) |
|
|
757 | const_eio (DT_REG) |
|
|
758 | const_eio (DT_LNK) |
|
|
759 | const_eio (DT_SOCK) |
|
|
760 | const_eio (DT_WHT) |
|
|
761 | }; |
|
|
762 | |
|
|
763 | aio_stash = gv_stashpv ("IO::AIO" , 1); |
|
|
764 | aio_req_stash = gv_stashpv ("IO::AIO::REQ", 1); |
|
|
765 | aio_grp_stash = gv_stashpv ("IO::AIO::GRP", 1); |
|
|
766 | |
|
|
767 | for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) |
|
|
768 | newCONSTSUB (aio_stash, (char *)civ->name, newSViv (civ->iv)); |
|
|
769 | |
|
|
770 | newCONSTSUB (aio_stash, "PAGESIZE", newSViv (PAGESIZE)); |
|
|
771 | |
|
|
772 | create_respipe (); |
|
|
773 | |
|
|
774 | if (eio_init (want_poll, done_poll) < 0) |
|
|
775 | croak ("IO::AIO: unable to initialise eio library"); |
|
|
776 | |
|
|
777 | /* atfork child called in fifo order, so before eio's handler */ |
|
|
778 | X_THREAD_ATFORK (0, 0, atfork_child); |
|
|
779 | } |
|
|
780 | |
378 | void |
781 | void |
379 | min_parallel(nthreads) |
782 | max_poll_reqs (int nreqs) |
380 | int nthreads |
|
|
381 | PROTOTYPE: $ |
783 | PROTOTYPE: $ |
382 | CODE: |
784 | CODE: |
383 | while (nthreads > started) |
785 | eio_set_max_poll_reqs (nreqs); |
384 | start_thread (); |
|
|
385 | |
786 | |
386 | void |
787 | void |
387 | max_parallel(nthreads) |
788 | max_poll_time (double nseconds) |
388 | int nthreads |
|
|
389 | PROTOTYPE: $ |
789 | PROTOTYPE: $ |
390 | CODE: |
790 | CODE: |
|
|
791 | eio_set_max_poll_time (nseconds); |
|
|
792 | |
|
|
793 | void |
|
|
794 | min_parallel (int nthreads) |
|
|
795 | PROTOTYPE: $ |
|
|
796 | CODE: |
|
|
797 | eio_set_min_parallel (nthreads); |
|
|
798 | |
|
|
799 | void |
|
|
800 | max_parallel (int nthreads) |
|
|
801 | PROTOTYPE: $ |
|
|
802 | CODE: |
|
|
803 | eio_set_max_parallel (nthreads); |
|
|
804 | |
|
|
805 | void |
|
|
806 | max_idle (int nthreads) |
|
|
807 | PROTOTYPE: $ |
|
|
808 | CODE: |
|
|
809 | eio_set_max_idle (nthreads); |
|
|
810 | |
|
|
811 | void |
|
|
812 | max_outstanding (int maxreqs) |
|
|
813 | PROTOTYPE: $ |
|
|
814 | CODE: |
|
|
815 | max_outstanding = maxreqs; |
|
|
816 | |
|
|
817 | void |
|
|
818 | aio_open (SV8 *pathname, int flags, int mode, SV *callback=&PL_sv_undef) |
|
|
819 | PROTOTYPE: $$$;$ |
|
|
820 | PPCODE: |
391 | { |
821 | { |
392 | int cur = started; |
822 | dREQ; |
393 | while (cur > nthreads) |
823 | |
394 | { |
824 | req->type = EIO_OPEN; |
395 | end_thread (); |
825 | req->sv1 = newSVsv (pathname); |
396 | cur--; |
826 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
827 | req->int1 = flags; |
|
|
828 | req->int2 = mode; |
|
|
829 | |
|
|
830 | REQ_SEND; |
|
|
831 | } |
|
|
832 | |
|
|
833 | void |
|
|
834 | aio_fsync (SV *fh, SV *callback=&PL_sv_undef) |
|
|
835 | PROTOTYPE: $;$ |
|
|
836 | ALIAS: |
|
|
837 | aio_fsync = EIO_FSYNC |
|
|
838 | aio_fdatasync = EIO_FDATASYNC |
|
|
839 | PPCODE: |
|
|
840 | { |
|
|
841 | int fd = s_fileno_croak (fh, 0); |
|
|
842 | dREQ; |
|
|
843 | |
|
|
844 | req->type = ix; |
|
|
845 | req->sv1 = newSVsv (fh); |
|
|
846 | req->int1 = fd; |
|
|
847 | |
|
|
848 | REQ_SEND (req); |
|
|
849 | } |
|
|
850 | |
|
|
851 | void |
|
|
852 | aio_sync_file_range (SV *fh, off_t offset, size_t nbytes, UV flags, SV *callback=&PL_sv_undef) |
|
|
853 | PROTOTYPE: $$$$;$ |
|
|
854 | PPCODE: |
|
|
855 | { |
|
|
856 | int fd = s_fileno_croak (fh, 0); |
|
|
857 | dREQ; |
|
|
858 | |
|
|
859 | req->type = EIO_SYNC_FILE_RANGE; |
|
|
860 | req->sv1 = newSVsv (fh); |
|
|
861 | req->int1 = fd; |
|
|
862 | req->offs = offset; |
|
|
863 | req->size = nbytes; |
|
|
864 | req->int2 = flags; |
|
|
865 | |
|
|
866 | REQ_SEND (req); |
|
|
867 | } |
|
|
868 | |
|
|
869 | void |
|
|
870 | aio_close (SV *fh, SV *callback=&PL_sv_undef) |
|
|
871 | PROTOTYPE: $;$ |
|
|
872 | PPCODE: |
|
|
873 | { |
|
|
874 | static int close_pipe = -1; /* dummy fd to close fds via dup2 */ |
|
|
875 | int fd = s_fileno_croak (fh, 0); |
|
|
876 | dREQ; |
|
|
877 | |
|
|
878 | if (close_pipe < 0) |
|
|
879 | { |
|
|
880 | int pipefd [2]; |
|
|
881 | |
|
|
882 | if (pipe (pipefd) < 0 |
|
|
883 | || close (pipefd [1]) < 0 |
|
|
884 | || fcntl (pipefd [0], F_SETFD, FD_CLOEXEC) < 0) |
|
|
885 | abort (); /*D*/ |
|
|
886 | |
|
|
887 | close_pipe = pipefd [0]; |
397 | } |
888 | } |
398 | |
889 | |
399 | while (started > nthreads) |
890 | req->type = EIO_DUP2; |
|
|
891 | req->int1 = close_pipe; |
|
|
892 | req->sv2 = newSVsv (fh); |
|
|
893 | req->int2 = fd; |
|
|
894 | |
|
|
895 | REQ_SEND (req); |
|
|
896 | } |
|
|
897 | |
|
|
898 | void |
|
|
899 | aio_read (SV *fh, SV *offset, SV *length, SV8 *data, IV dataoffset, SV *callback=&PL_sv_undef) |
|
|
900 | ALIAS: |
|
|
901 | aio_read = EIO_READ |
|
|
902 | aio_write = EIO_WRITE |
|
|
903 | PROTOTYPE: $$$$$;$ |
|
|
904 | PPCODE: |
|
|
905 | { |
|
|
906 | STRLEN svlen; |
|
|
907 | int fd = s_fileno_croak (fh, ix == EIO_WRITE); |
|
|
908 | char *svptr = SvPVbyte (data, svlen); |
|
|
909 | UV len = SvUV (length); |
|
|
910 | |
|
|
911 | if (dataoffset < 0) |
|
|
912 | dataoffset += svlen; |
|
|
913 | |
|
|
914 | if (dataoffset < 0 || dataoffset > svlen) |
|
|
915 | croak ("dataoffset outside of data scalar"); |
|
|
916 | |
|
|
917 | if (ix == EIO_WRITE) |
|
|
918 | { |
|
|
919 | /* write: check length and adjust. */ |
|
|
920 | if (!SvOK (length) || len + dataoffset > svlen) |
|
|
921 | len = svlen - dataoffset; |
|
|
922 | } |
|
|
923 | else |
|
|
924 | { |
|
|
925 | /* read: check type and grow scalar as necessary */ |
|
|
926 | SvUPGRADE (data, SVt_PV); |
|
|
927 | svptr = SvGROW (data, len + dataoffset + 1); |
|
|
928 | } |
|
|
929 | |
|
|
930 | { |
|
|
931 | dREQ; |
|
|
932 | |
|
|
933 | req->type = ix; |
|
|
934 | req->sv1 = newSVsv (fh); |
|
|
935 | req->int1 = fd; |
|
|
936 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
|
|
937 | req->size = len; |
|
|
938 | req->sv2 = SvREFCNT_inc (data); |
|
|
939 | req->ptr2 = (char *)svptr + dataoffset; |
|
|
940 | req->stroffset = dataoffset; |
|
|
941 | |
|
|
942 | if (!SvREADONLY (data)) |
|
|
943 | { |
|
|
944 | SvREADONLY_on (data); |
|
|
945 | req->flags |= FLAG_SV2_RO_OFF; |
|
|
946 | } |
|
|
947 | |
|
|
948 | REQ_SEND; |
|
|
949 | } |
|
|
950 | } |
|
|
951 | |
|
|
952 | void |
|
|
953 | aio_readlink (SV8 *path, SV *callback=&PL_sv_undef) |
|
|
954 | PROTOTYPE: $$;$ |
|
|
955 | PPCODE: |
|
|
956 | { |
|
|
957 | SV *data; |
|
|
958 | dREQ; |
|
|
959 | |
|
|
960 | req->type = EIO_READLINK; |
|
|
961 | req->sv1 = newSVsv (path); |
|
|
962 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
963 | |
|
|
964 | REQ_SEND; |
|
|
965 | } |
|
|
966 | |
|
|
967 | void |
|
|
968 | aio_sendfile (SV *out_fh, SV *in_fh, off_t in_offset, size_t length, SV *callback=&PL_sv_undef) |
|
|
969 | PROTOTYPE: $$$$;$ |
|
|
970 | PPCODE: |
|
|
971 | { |
|
|
972 | int ifd = s_fileno_croak (in_fh , 0); |
|
|
973 | int ofd = s_fileno_croak (out_fh, 1); |
|
|
974 | dREQ; |
|
|
975 | |
|
|
976 | req->type = EIO_SENDFILE; |
|
|
977 | req->sv1 = newSVsv (out_fh); |
|
|
978 | req->int1 = ofd; |
|
|
979 | req->sv2 = newSVsv (in_fh); |
|
|
980 | req->int2 = ifd; |
|
|
981 | req->offs = in_offset; |
|
|
982 | req->size = length; |
|
|
983 | |
|
|
984 | REQ_SEND; |
|
|
985 | } |
|
|
986 | |
|
|
987 | void |
|
|
988 | aio_readahead (SV *fh, off_t offset, size_t length, SV *callback=&PL_sv_undef) |
|
|
989 | PROTOTYPE: $$$;$ |
|
|
990 | PPCODE: |
|
|
991 | { |
|
|
992 | int fd = s_fileno_croak (fh, 0); |
|
|
993 | dREQ; |
|
|
994 | |
|
|
995 | req->type = EIO_READAHEAD; |
|
|
996 | req->sv1 = newSVsv (fh); |
|
|
997 | req->int1 = fd; |
|
|
998 | req->offs = offset; |
|
|
999 | req->size = length; |
|
|
1000 | |
|
|
1001 | REQ_SEND; |
|
|
1002 | } |
|
|
1003 | |
|
|
1004 | void |
|
|
1005 | aio_stat (SV8 *fh_or_path, SV *callback=&PL_sv_undef) |
|
|
1006 | ALIAS: |
|
|
1007 | aio_stat = EIO_STAT |
|
|
1008 | aio_lstat = EIO_LSTAT |
|
|
1009 | aio_statvfs = EIO_STATVFS |
|
|
1010 | PPCODE: |
|
|
1011 | { |
|
|
1012 | dREQ; |
|
|
1013 | |
|
|
1014 | req->sv1 = newSVsv (fh_or_path); |
|
|
1015 | |
|
|
1016 | if (SvPOK (req->sv1)) |
|
|
1017 | { |
|
|
1018 | req->type = ix; |
|
|
1019 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1020 | } |
|
|
1021 | else |
|
|
1022 | { |
|
|
1023 | req->type = ix == EIO_STATVFS ? EIO_FSTATVFS : EIO_FSTAT; |
|
|
1024 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1025 | } |
|
|
1026 | |
|
|
1027 | REQ_SEND; |
|
|
1028 | } |
|
|
1029 | |
|
|
1030 | void |
|
|
1031 | aio_utime (SV8 *fh_or_path, SV *atime, SV *mtime, SV *callback=&PL_sv_undef) |
|
|
1032 | PPCODE: |
|
|
1033 | { |
|
|
1034 | dREQ; |
|
|
1035 | |
|
|
1036 | req->nv1 = SvOK (atime) ? SvNV (atime) : -1.; |
|
|
1037 | req->nv2 = SvOK (mtime) ? SvNV (mtime) : -1.; |
|
|
1038 | req->sv1 = newSVsv (fh_or_path); |
|
|
1039 | |
|
|
1040 | if (SvPOK (req->sv1)) |
|
|
1041 | { |
|
|
1042 | req->type = EIO_UTIME; |
|
|
1043 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1044 | } |
|
|
1045 | else |
|
|
1046 | { |
|
|
1047 | req->type = EIO_FUTIME; |
|
|
1048 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1049 | } |
|
|
1050 | |
|
|
1051 | REQ_SEND; |
|
|
1052 | } |
|
|
1053 | |
|
|
1054 | void |
|
|
1055 | aio_truncate (SV8 *fh_or_path, SV *offset, SV *callback=&PL_sv_undef) |
|
|
1056 | PPCODE: |
|
|
1057 | { |
|
|
1058 | dREQ; |
|
|
1059 | |
|
|
1060 | req->sv1 = newSVsv (fh_or_path); |
|
|
1061 | req->offs = SvOK (offset) ? SvVAL64 (offset) : -1; |
|
|
1062 | |
|
|
1063 | if (SvPOK (req->sv1)) |
|
|
1064 | { |
|
|
1065 | req->type = EIO_TRUNCATE; |
|
|
1066 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1067 | } |
|
|
1068 | else |
|
|
1069 | { |
|
|
1070 | req->type = EIO_FTRUNCATE; |
|
|
1071 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1072 | } |
|
|
1073 | |
|
|
1074 | REQ_SEND; |
|
|
1075 | } |
|
|
1076 | |
|
|
1077 | void |
|
|
1078 | aio_chmod (SV8 *fh_or_path, int mode, SV *callback=&PL_sv_undef) |
|
|
1079 | ALIAS: |
|
|
1080 | aio_chmod = EIO_CHMOD |
|
|
1081 | aio_mkdir = EIO_MKDIR |
|
|
1082 | PPCODE: |
|
|
1083 | { |
|
|
1084 | dREQ; |
|
|
1085 | |
|
|
1086 | req->int2 = mode; |
|
|
1087 | req->sv1 = newSVsv (fh_or_path); |
|
|
1088 | |
|
|
1089 | if (SvPOK (req->sv1)) |
|
|
1090 | { |
|
|
1091 | req->type = ix; |
|
|
1092 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1093 | } |
|
|
1094 | else |
|
|
1095 | { |
|
|
1096 | req->type = EIO_FCHMOD; |
|
|
1097 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1098 | } |
|
|
1099 | |
|
|
1100 | REQ_SEND; |
|
|
1101 | } |
|
|
1102 | |
|
|
1103 | void |
|
|
1104 | aio_chown (SV8 *fh_or_path, SV *uid, SV *gid, SV *callback=&PL_sv_undef) |
|
|
1105 | PPCODE: |
|
|
1106 | { |
|
|
1107 | dREQ; |
|
|
1108 | |
|
|
1109 | req->int2 = SvOK (uid) ? SvIV (uid) : -1; |
|
|
1110 | req->int3 = SvOK (gid) ? SvIV (gid) : -1; |
|
|
1111 | req->sv1 = newSVsv (fh_or_path); |
|
|
1112 | |
|
|
1113 | if (SvPOK (req->sv1)) |
|
|
1114 | { |
|
|
1115 | req->type = EIO_CHOWN; |
|
|
1116 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1117 | } |
|
|
1118 | else |
|
|
1119 | { |
|
|
1120 | req->type = EIO_FCHOWN; |
|
|
1121 | req->int1 = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
1122 | } |
|
|
1123 | |
|
|
1124 | REQ_SEND; |
|
|
1125 | } |
|
|
1126 | |
|
|
1127 | void |
|
|
1128 | aio_readdirx (SV8 *pathname, IV flags, SV *callback=&PL_sv_undef) |
|
|
1129 | PPCODE: |
|
|
1130 | { |
|
|
1131 | dREQ; |
|
|
1132 | |
|
|
1133 | req->type = EIO_READDIR; |
|
|
1134 | req->sv1 = newSVsv (pathname); |
|
|
1135 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1136 | req->int1 = flags | EIO_READDIR_DENTS | EIO_READDIR_CUSTOM1; |
|
|
1137 | |
|
|
1138 | if (flags & EIO_READDIR_DENTS) |
|
|
1139 | req->int1 |= EIO_READDIR_CUSTOM2; |
|
|
1140 | |
|
|
1141 | REQ_SEND; |
|
|
1142 | } |
|
|
1143 | |
|
|
1144 | void |
|
|
1145 | aio_unlink (SV8 *pathname, SV *callback=&PL_sv_undef) |
|
|
1146 | ALIAS: |
|
|
1147 | aio_unlink = EIO_UNLINK |
|
|
1148 | aio_rmdir = EIO_RMDIR |
|
|
1149 | aio_readdir = EIO_READDIR |
|
|
1150 | PPCODE: |
|
|
1151 | { |
|
|
1152 | dREQ; |
|
|
1153 | |
|
|
1154 | req->type = ix; |
|
|
1155 | req->sv1 = newSVsv (pathname); |
|
|
1156 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1157 | |
|
|
1158 | REQ_SEND; |
|
|
1159 | } |
|
|
1160 | |
|
|
1161 | void |
|
|
1162 | aio_link (SV8 *oldpath, SV8 *newpath, SV *callback=&PL_sv_undef) |
|
|
1163 | ALIAS: |
|
|
1164 | aio_link = EIO_LINK |
|
|
1165 | aio_symlink = EIO_SYMLINK |
|
|
1166 | aio_rename = EIO_RENAME |
|
|
1167 | PPCODE: |
|
|
1168 | { |
|
|
1169 | dREQ; |
|
|
1170 | |
|
|
1171 | req->type = ix; |
|
|
1172 | req->sv1 = newSVsv (oldpath); |
|
|
1173 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1174 | req->sv2 = newSVsv (newpath); |
|
|
1175 | req->ptr2 = SvPVbyte_nolen (req->sv2); |
|
|
1176 | |
|
|
1177 | REQ_SEND; |
|
|
1178 | } |
|
|
1179 | |
|
|
1180 | void |
|
|
1181 | aio_mknod (SV8 *pathname, int mode, UV dev, SV *callback=&PL_sv_undef) |
|
|
1182 | PPCODE: |
|
|
1183 | { |
|
|
1184 | dREQ; |
|
|
1185 | |
|
|
1186 | req->type = EIO_MKNOD; |
|
|
1187 | req->sv1 = newSVsv (pathname); |
|
|
1188 | req->ptr1 = SvPVbyte_nolen (req->sv1); |
|
|
1189 | req->int2 = (mode_t)mode; |
|
|
1190 | req->offs = dev; |
|
|
1191 | |
|
|
1192 | REQ_SEND; |
|
|
1193 | } |
|
|
1194 | |
|
|
1195 | void |
|
|
1196 | aio_mtouch (SV8 *data, IV offset = 0, SV *length = &PL_sv_undef, int flags = 0, SV *callback=&PL_sv_undef) |
|
|
1197 | ALIAS: |
|
|
1198 | aio_mtouch = EIO_MTOUCH |
|
|
1199 | aio_msync = EIO_MSYNC |
|
|
1200 | PROTOTYPE: $$$$;$ |
|
|
1201 | PPCODE: |
|
|
1202 | { |
|
|
1203 | STRLEN svlen; |
|
|
1204 | UV len = SvUV (length); |
|
|
1205 | char *svptr = SvPVbyte (data, svlen); |
|
|
1206 | |
|
|
1207 | if (offset < 0) |
|
|
1208 | offset += svlen; |
|
|
1209 | |
|
|
1210 | if (offset < 0 || offset > svlen) |
|
|
1211 | croak ("offset outside of scalar"); |
|
|
1212 | |
|
|
1213 | if (!SvOK (length) || len + offset > svlen) |
|
|
1214 | len = svlen - offset; |
|
|
1215 | |
|
|
1216 | { |
|
|
1217 | dREQ; |
|
|
1218 | |
|
|
1219 | req->type = ix; |
|
|
1220 | req->size = len; |
|
|
1221 | req->sv2 = SvREFCNT_inc (data); |
|
|
1222 | req->ptr2 = (char *)svptr + offset; |
|
|
1223 | req->int1 = flags; |
|
|
1224 | |
|
|
1225 | REQ_SEND; |
|
|
1226 | } |
|
|
1227 | } |
|
|
1228 | |
|
|
1229 | void |
|
|
1230 | aio_busy (double delay, SV *callback=&PL_sv_undef) |
|
|
1231 | PPCODE: |
|
|
1232 | { |
|
|
1233 | dREQ; |
|
|
1234 | |
|
|
1235 | req->type = EIO_BUSY; |
|
|
1236 | req->nv1 = delay < 0. ? 0. : delay; |
|
|
1237 | |
|
|
1238 | REQ_SEND; |
|
|
1239 | } |
|
|
1240 | |
|
|
1241 | void |
|
|
1242 | aio_group (SV *callback=&PL_sv_undef) |
|
|
1243 | PROTOTYPE: ;$ |
|
|
1244 | PPCODE: |
|
|
1245 | { |
|
|
1246 | dREQ; |
|
|
1247 | |
|
|
1248 | req->type = EIO_GROUP; |
|
|
1249 | |
|
|
1250 | req_submit (req); |
|
|
1251 | XPUSHs (req_sv (req, aio_grp_stash)); |
|
|
1252 | } |
|
|
1253 | |
|
|
1254 | void |
|
|
1255 | aio_nop (SV *callback=&PL_sv_undef) |
|
|
1256 | ALIAS: |
|
|
1257 | aio_nop = EIO_NOP |
|
|
1258 | aio_sync = EIO_SYNC |
|
|
1259 | PPCODE: |
|
|
1260 | { |
|
|
1261 | dREQ; |
|
|
1262 | |
|
|
1263 | req->type = ix; |
|
|
1264 | |
|
|
1265 | REQ_SEND; |
|
|
1266 | } |
|
|
1267 | |
|
|
1268 | int |
|
|
1269 | aioreq_pri (int pri = 0) |
|
|
1270 | PROTOTYPE: ;$ |
|
|
1271 | CODE: |
|
|
1272 | RETVAL = next_pri; |
|
|
1273 | if (items > 0) |
|
|
1274 | { |
|
|
1275 | if (pri < EIO_PRI_MIN) pri = EIO_PRI_MIN; |
|
|
1276 | if (pri > EIO_PRI_MAX) pri = EIO_PRI_MAX; |
|
|
1277 | next_pri = pri; |
|
|
1278 | } |
|
|
1279 | OUTPUT: |
|
|
1280 | RETVAL |
|
|
1281 | |
|
|
1282 | void |
|
|
1283 | aioreq_nice (int nice = 0) |
|
|
1284 | CODE: |
|
|
1285 | nice = next_pri - nice; |
|
|
1286 | if (nice < EIO_PRI_MIN) nice = EIO_PRI_MIN; |
|
|
1287 | if (nice > EIO_PRI_MAX) nice = EIO_PRI_MAX; |
|
|
1288 | next_pri = nice; |
|
|
1289 | |
|
|
1290 | void |
|
|
1291 | flush () |
|
|
1292 | PROTOTYPE: |
|
|
1293 | CODE: |
|
|
1294 | while (eio_nreqs ()) |
400 | { |
1295 | { |
401 | poll_wait (); |
1296 | poll_wait (); |
402 | poll_cb (); |
1297 | poll_cb (); |
403 | } |
1298 | } |
404 | } |
|
|
405 | |
1299 | |
406 | int |
1300 | int |
407 | max_outstanding(nreqs) |
1301 | poll() |
408 | int nreqs |
|
|
409 | PROTOTYPE: $ |
|
|
410 | CODE: |
|
|
411 | RETVAL = max_outstanding; |
|
|
412 | max_outstanding = nreqs; |
|
|
413 | |
|
|
414 | void |
|
|
415 | aio_open(pathname,flags,mode,callback) |
|
|
416 | SV * pathname |
|
|
417 | int flags |
|
|
418 | int mode |
|
|
419 | SV * callback |
|
|
420 | PROTOTYPE: $$$$ |
|
|
421 | CODE: |
|
|
422 | { |
|
|
423 | aio_req req; |
|
|
424 | |
|
|
425 | Newz (0, req, 1, aio_cb); |
|
|
426 | |
|
|
427 | if (!req) |
|
|
428 | croak ("out of memory during aio_req allocation"); |
|
|
429 | |
|
|
430 | req->type = REQ_OPEN; |
|
|
431 | req->data = newSVsv (pathname); |
|
|
432 | req->dataptr = SvPV_nolen (req->data); |
|
|
433 | req->fd = flags; |
|
|
434 | req->mode = mode; |
|
|
435 | req->callback = SvREFCNT_inc (callback); |
|
|
436 | |
|
|
437 | send_req (req); |
|
|
438 | } |
|
|
439 | |
|
|
440 | void |
|
|
441 | aio_close(fh,callback) |
|
|
442 | InputStream fh |
|
|
443 | SV * callback |
|
|
444 | PROTOTYPE: $$ |
|
|
445 | ALIAS: |
|
|
446 | aio_close = REQ_CLOSE |
|
|
447 | aio_fsync = REQ_FSYNC |
|
|
448 | aio_fdatasync = REQ_FDATASYNC |
|
|
449 | CODE: |
|
|
450 | { |
|
|
451 | aio_req req; |
|
|
452 | |
|
|
453 | Newz (0, req, 1, aio_cb); |
|
|
454 | |
|
|
455 | if (!req) |
|
|
456 | croak ("out of memory during aio_req allocation"); |
|
|
457 | |
|
|
458 | req->type = ix; |
|
|
459 | req->fd = PerlIO_fileno (fh); |
|
|
460 | req->callback = SvREFCNT_inc (callback); |
|
|
461 | |
|
|
462 | send_req (req); |
|
|
463 | } |
|
|
464 | |
|
|
465 | void |
|
|
466 | aio_read(fh,offset,length,data,dataoffset,callback) |
|
|
467 | InputStream fh |
|
|
468 | UV offset |
|
|
469 | IV length |
|
|
470 | SV * data |
|
|
471 | IV dataoffset |
|
|
472 | SV * callback |
|
|
473 | PROTOTYPE: $$$$$$ |
|
|
474 | CODE: |
|
|
475 | read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
476 | |
|
|
477 | void |
|
|
478 | aio_write(fh,offset,length,data,dataoffset,callback) |
|
|
479 | OutputStream fh |
|
|
480 | UV offset |
|
|
481 | IV length |
|
|
482 | SV * data |
|
|
483 | IV dataoffset |
|
|
484 | SV * callback |
|
|
485 | PROTOTYPE: $$$$$$ |
|
|
486 | CODE: |
|
|
487 | read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
488 | |
|
|
489 | void |
|
|
490 | aio_readahead(fh,offset,length,callback) |
|
|
491 | InputStream fh |
|
|
492 | UV offset |
|
|
493 | IV length |
|
|
494 | SV * callback |
|
|
495 | PROTOTYPE: $$$$ |
|
|
496 | CODE: |
|
|
497 | { |
|
|
498 | aio_req req; |
|
|
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 | |
|
|
508 | req->type = REQ_READAHEAD; |
|
|
509 | req->fd = PerlIO_fileno (fh); |
|
|
510 | req->offset = offset; |
|
|
511 | req->length = length; |
|
|
512 | req->callback = SvREFCNT_inc (callback); |
|
|
513 | |
|
|
514 | send_req (req); |
|
|
515 | } |
|
|
516 | |
|
|
517 | void |
|
|
518 | aio_stat(fh_or_path,callback) |
|
|
519 | SV * fh_or_path |
|
|
520 | SV * callback |
|
|
521 | PROTOTYPE: $$ |
|
|
522 | ALIAS: |
|
|
523 | aio_lstat = 1 |
|
|
524 | CODE: |
|
|
525 | { |
|
|
526 | aio_req req; |
|
|
527 | |
|
|
528 | Newz (0, req, 1, aio_cb); |
|
|
529 | |
|
|
530 | if (!req) |
|
|
531 | croak ("out of memory during aio_req allocation"); |
|
|
532 | |
|
|
533 | New (0, req->statdata, 1, Stat_t); |
|
|
534 | |
|
|
535 | if (!req->statdata) |
|
|
536 | croak ("out of memory during aio_req->statdata allocation"); |
|
|
537 | |
|
|
538 | if (SvPOK (fh_or_path)) |
|
|
539 | { |
|
|
540 | req->type = ix ? REQ_LSTAT : REQ_STAT; |
|
|
541 | req->data = newSVsv (fh_or_path); |
|
|
542 | req->dataptr = SvPV_nolen (req->data); |
|
|
543 | } |
|
|
544 | else |
|
|
545 | { |
|
|
546 | req->type = REQ_FSTAT; |
|
|
547 | req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path))); |
|
|
548 | } |
|
|
549 | |
|
|
550 | req->callback = SvREFCNT_inc (callback); |
|
|
551 | |
|
|
552 | send_req (req); |
|
|
553 | } |
|
|
554 | |
|
|
555 | void |
|
|
556 | aio_unlink(pathname,callback) |
|
|
557 | SV * pathname |
|
|
558 | SV * callback |
|
|
559 | PROTOTYPE: $$ |
|
|
560 | CODE: |
|
|
561 | { |
|
|
562 | aio_req req; |
|
|
563 | |
|
|
564 | Newz (0, req, 1, aio_cb); |
|
|
565 | |
|
|
566 | if (!req) |
|
|
567 | croak ("out of memory during aio_req allocation"); |
|
|
568 | |
|
|
569 | req->type = REQ_UNLINK; |
|
|
570 | req->data = newSVsv (pathname); |
|
|
571 | req->dataptr = SvPV_nolen (req->data); |
|
|
572 | req->callback = SvREFCNT_inc (callback); |
|
|
573 | |
|
|
574 | send_req (req); |
|
|
575 | } |
|
|
576 | |
|
|
577 | void |
|
|
578 | flush() |
|
|
579 | PROTOTYPE: |
1302 | PROTOTYPE: |
580 | CODE: |
1303 | CODE: |
581 | while (nreqs) |
|
|
582 | { |
|
|
583 | poll_wait (); |
1304 | poll_wait (); |
584 | poll_cb (); |
1305 | RETVAL = poll_cb (); |
585 | } |
1306 | OUTPUT: |
|
|
1307 | RETVAL |
586 | |
1308 | |
587 | int |
1309 | int |
588 | poll_fileno() |
1310 | poll_fileno() |
589 | PROTOTYPE: |
1311 | PROTOTYPE: |
590 | CODE: |
1312 | CODE: |
591 | RETVAL = respipe [0]; |
1313 | RETVAL = s_epipe_fd (&respipe); |
592 | OUTPUT: |
1314 | OUTPUT: |
593 | RETVAL |
1315 | RETVAL |
594 | |
1316 | |
595 | int |
1317 | int |
596 | poll_cb(...) |
1318 | poll_cb(...) |
… | |
… | |
602 | |
1324 | |
603 | void |
1325 | void |
604 | poll_wait() |
1326 | poll_wait() |
605 | PROTOTYPE: |
1327 | PROTOTYPE: |
606 | CODE: |
1328 | CODE: |
607 | if (nreqs) |
|
|
608 | poll_wait (); |
1329 | poll_wait (); |
609 | |
1330 | |
610 | int |
1331 | int |
611 | nreqs() |
1332 | nreqs() |
612 | PROTOTYPE: |
1333 | PROTOTYPE: |
613 | CODE: |
1334 | CODE: |
614 | RETVAL = nreqs; |
1335 | RETVAL = eio_nreqs (); |
615 | OUTPUT: |
1336 | OUTPUT: |
616 | RETVAL |
1337 | RETVAL |
617 | |
1338 | |
|
|
1339 | int |
|
|
1340 | nready() |
|
|
1341 | PROTOTYPE: |
|
|
1342 | CODE: |
|
|
1343 | RETVAL = eio_nready (); |
|
|
1344 | OUTPUT: |
|
|
1345 | RETVAL |
|
|
1346 | |
|
|
1347 | int |
|
|
1348 | npending() |
|
|
1349 | PROTOTYPE: |
|
|
1350 | CODE: |
|
|
1351 | RETVAL = eio_npending (); |
|
|
1352 | OUTPUT: |
|
|
1353 | RETVAL |
|
|
1354 | |
|
|
1355 | int |
|
|
1356 | nthreads() |
|
|
1357 | PROTOTYPE: |
|
|
1358 | CODE: |
|
|
1359 | RETVAL = eio_nthreads (); |
|
|
1360 | OUTPUT: |
|
|
1361 | RETVAL |
|
|
1362 | |
|
|
1363 | int |
|
|
1364 | fadvise (aio_rfd fh, off_t offset, off_t length, IV advice) |
|
|
1365 | PROTOTYPE: $$$$ |
|
|
1366 | CODE: |
|
|
1367 | #if _XOPEN_SOURCE >= 600 && !NO_FADVISE |
|
|
1368 | RETVAL = posix_fadvise (fh, offset, length, advice); |
|
|
1369 | #else |
|
|
1370 | RETVAL = errno = ENOSYS; /* yes, this is actually correct */ |
|
|
1371 | #endif |
|
|
1372 | OUTPUT: |
|
|
1373 | RETVAL |
|
|
1374 | |
|
|
1375 | ssize_t |
|
|
1376 | sendfile (aio_wfd ofh, aio_rfd ifh, off_t offset, size_t count) |
|
|
1377 | PROTOTYPE: $$$$ |
|
|
1378 | CODE: |
|
|
1379 | RETVAL = eio_sendfile_sync (ofh, ifh, offset, count); |
|
|
1380 | OUTPUT: |
|
|
1381 | RETVAL |
|
|
1382 | |
|
|
1383 | void |
|
|
1384 | mmap (SV *scalar, size_t length, int prot, int flags, SV *fh, off_t offset = 0) |
|
|
1385 | PROTOTYPE: $$$$$;$ |
|
|
1386 | PPCODE: |
|
|
1387 | sv_unmagic (scalar, MMAP_MAGIC); |
|
|
1388 | { |
|
|
1389 | int fd = SvOK (fh) ? s_fileno_croak (fh, flags & PROT_WRITE) : -1; |
|
|
1390 | void *addr = (void *)mmap (0, length, prot, flags, fd, offset); |
|
|
1391 | if (addr == (void *)-1) |
|
|
1392 | XSRETURN_NO; |
|
|
1393 | |
|
|
1394 | sv_force_normal (scalar); |
|
|
1395 | |
|
|
1396 | /* we store the length in mg_obj, as namlen is I32 :/ */ |
|
|
1397 | sv_magicext (scalar, 0, MMAP_MAGIC, &mmap_vtbl, (char *)addr, 0) |
|
|
1398 | ->mg_obj = (SV *)length; |
|
|
1399 | |
|
|
1400 | SvUPGRADE (scalar, SVt_PV); /* nop... */ |
|
|
1401 | |
|
|
1402 | if (!(prot & PROT_WRITE)) |
|
|
1403 | SvREADONLY_on (scalar); |
|
|
1404 | |
|
|
1405 | if (SvLEN (scalar)) |
|
|
1406 | Safefree (SvPVX (scalar)); |
|
|
1407 | |
|
|
1408 | SvPVX (scalar) = (char *)addr; |
|
|
1409 | SvCUR_set (scalar, length); |
|
|
1410 | SvLEN_set (scalar, 0); |
|
|
1411 | SvPOK_only (scalar); |
|
|
1412 | |
|
|
1413 | XSRETURN_YES; |
|
|
1414 | } |
|
|
1415 | |
|
|
1416 | void |
|
|
1417 | munmap (SV *scalar) |
|
|
1418 | PROTOTYPE: $ |
|
|
1419 | CODE: |
|
|
1420 | sv_unmagic (scalar, MMAP_MAGIC); |
|
|
1421 | |
|
|
1422 | int |
|
|
1423 | mlockall (int flags) |
|
|
1424 | PROTOTYPE: $ |
|
|
1425 | CODE: |
|
|
1426 | #if _POSIX_MEMLOCK |
|
|
1427 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 |
|
|
1428 | extern int mallopt (int, int); |
|
|
1429 | mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ |
|
|
1430 | #endif |
|
|
1431 | mlockall (flags); |
|
|
1432 | #else |
|
|
1433 | RETVAL = -1; |
|
|
1434 | errno = ENOSYS; |
|
|
1435 | #endif |
|
|
1436 | OUTPUT: |
|
|
1437 | RETVAL |
|
|
1438 | |
|
|
1439 | int |
|
|
1440 | munlockall () |
|
|
1441 | PROTOTYPE: |
|
|
1442 | CODE: |
|
|
1443 | #if _POSIX_MEMLOCK |
|
|
1444 | munlockall (); |
|
|
1445 | #else |
|
|
1446 | RETVAL = -1; |
|
|
1447 | errno = ENOSYS; |
|
|
1448 | #endif |
|
|
1449 | OUTPUT: |
|
|
1450 | RETVAL |
|
|
1451 | |
|
|
1452 | void _on_next_submit (SV *cb) |
|
|
1453 | CODE: |
|
|
1454 | SvREFCNT_dec (on_next_submit); |
|
|
1455 | on_next_submit = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1456 | |
|
|
1457 | PROTOTYPES: DISABLE |
|
|
1458 | |
|
|
1459 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
|
|
1460 | |
|
|
1461 | void |
|
|
1462 | cancel (aio_req_ornot req) |
|
|
1463 | CODE: |
|
|
1464 | eio_cancel (req); |
|
|
1465 | |
|
|
1466 | void |
|
|
1467 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
|
|
1468 | PPCODE: |
|
|
1469 | { |
|
|
1470 | if (GIMME_V != G_VOID) |
|
|
1471 | XPUSHs (req->callback ? sv_2mortal (newRV_inc (req->callback)) : &PL_sv_undef); |
|
|
1472 | |
|
|
1473 | if (items > 1) |
|
|
1474 | { |
|
|
1475 | SV *cb_cv =get_cb (callback); |
|
|
1476 | |
|
|
1477 | SvREFCNT_dec (req->callback); |
|
|
1478 | req->callback = SvREFCNT_inc (cb_cv); |
|
|
1479 | } |
|
|
1480 | } |
|
|
1481 | |
|
|
1482 | MODULE = IO::AIO PACKAGE = IO::AIO::GRP |
|
|
1483 | |
|
|
1484 | void |
|
|
1485 | add (aio_req grp, ...) |
|
|
1486 | PPCODE: |
|
|
1487 | { |
|
|
1488 | int i; |
|
|
1489 | |
|
|
1490 | if (grp->int1 == 2) |
|
|
1491 | croak ("cannot add requests to IO::AIO::GRP after the group finished"); |
|
|
1492 | |
|
|
1493 | for (i = 1; i < items; ++i ) |
|
|
1494 | { |
|
|
1495 | aio_req req; |
|
|
1496 | |
|
|
1497 | if (GIMME_V != G_VOID) |
|
|
1498 | XPUSHs (sv_2mortal (newSVsv (ST (i)))); |
|
|
1499 | |
|
|
1500 | req = SvAIO_REQ (ST (i)); |
|
|
1501 | |
|
|
1502 | if (req) |
|
|
1503 | eio_grp_add (grp, req); |
|
|
1504 | } |
|
|
1505 | } |
|
|
1506 | |
|
|
1507 | void |
|
|
1508 | cancel_subs (aio_req_ornot req) |
|
|
1509 | CODE: |
|
|
1510 | req_cancel_subs (req); |
|
|
1511 | |
|
|
1512 | void |
|
|
1513 | result (aio_req grp, ...) |
|
|
1514 | CODE: |
|
|
1515 | { |
|
|
1516 | int i; |
|
|
1517 | AV *av; |
|
|
1518 | |
|
|
1519 | grp->errorno = errno; |
|
|
1520 | |
|
|
1521 | av = newAV (); |
|
|
1522 | av_extend (av, items - 1); |
|
|
1523 | |
|
|
1524 | for (i = 1; i < items; ++i ) |
|
|
1525 | av_push (av, newSVsv (ST (i))); |
|
|
1526 | |
|
|
1527 | SvREFCNT_dec (grp->sv1); |
|
|
1528 | grp->sv1 = (SV *)av; |
|
|
1529 | } |
|
|
1530 | |
|
|
1531 | void |
|
|
1532 | errno (aio_req grp, int errorno = errno) |
|
|
1533 | CODE: |
|
|
1534 | grp->errorno = errorno; |
|
|
1535 | |
|
|
1536 | void |
|
|
1537 | limit (aio_req grp, int limit) |
|
|
1538 | CODE: |
|
|
1539 | eio_grp_limit (grp, limit); |
|
|
1540 | |
|
|
1541 | void |
|
|
1542 | feed (aio_req grp, SV *callback=&PL_sv_undef) |
|
|
1543 | CODE: |
|
|
1544 | { |
|
|
1545 | SvREFCNT_dec (grp->sv2); |
|
|
1546 | grp->sv2 = newSVsv (callback); |
|
|
1547 | grp->feed = aio_grp_feed; |
|
|
1548 | |
|
|
1549 | if (grp->int2 <= 0) |
|
|
1550 | grp->int2 = 2; |
|
|
1551 | |
|
|
1552 | eio_grp_limit (grp, grp->int2); |
|
|
1553 | } |
|
|
1554 | |