… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include <sys/types.h> |
5 | #include <sys/types.h> |
6 | #include <unistd.h> |
6 | #include <unistd.h> |
7 | #include <fcntl.h> |
7 | #include <fcntl.h> |
|
|
8 | #include <signal.h> |
8 | #include <sched.h> |
9 | #include <sched.h> |
9 | |
10 | |
|
|
11 | typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */ |
|
|
12 | |
10 | #define STACKSIZE 4096 /* yeah */ |
13 | #define STACKSIZE 1024 /* yeah */ |
11 | |
14 | |
12 | #define REQ_QUIT 0 |
15 | enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN, REQ_CLOSE }; |
13 | #define REQ_READ 1 |
|
|
14 | #define REQ_WRITE 2 |
|
|
15 | |
16 | |
16 | typedef struct { |
17 | typedef struct { |
17 | char stack[STACKSIZE]; |
18 | char stack[STACKSIZE]; |
18 | } aio_thread; |
19 | } aio_thread; |
19 | |
20 | |
… | |
… | |
24 | /* read/write */ |
25 | /* read/write */ |
25 | int fd; |
26 | int fd; |
26 | off_t offset; |
27 | off_t offset; |
27 | size_t length; |
28 | size_t length; |
28 | ssize_t result; |
29 | ssize_t result; |
|
|
30 | mode_t mode; /* open */ |
29 | int errorno; |
31 | int errorno; |
30 | |
|
|
31 | SV *data, *callback; |
32 | SV *data, *callback; |
32 | void *dataptr; |
33 | void *dataptr; |
33 | STRLEN dataoffset; |
34 | STRLEN dataoffset; |
34 | } aio_cb; |
35 | } aio_cb; |
35 | |
36 | |
… | |
… | |
61 | end_thread(void) |
62 | end_thread(void) |
62 | { |
63 | { |
63 | aio_req req; |
64 | aio_req req; |
64 | New (0, req, 1, aio_cb); |
65 | New (0, req, 1, aio_cb); |
65 | req->type = REQ_QUIT; |
66 | req->type = REQ_QUIT; |
|
|
67 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
68 | } |
|
|
69 | |
|
|
70 | static void |
|
|
71 | send_req (aio_req req) |
|
|
72 | { |
|
|
73 | nreqs++; |
66 | write (reqpipe[1], &req, sizeof (aio_req)); |
74 | write (reqpipe[1], &req, sizeof (aio_req)); |
67 | } |
75 | } |
68 | |
76 | |
69 | static void |
77 | static void |
70 | read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, |
78 | read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, |
… | |
… | |
106 | req->length = length; |
114 | req->length = length; |
107 | req->data = SvREFCNT_inc (data); |
115 | req->data = SvREFCNT_inc (data); |
108 | req->dataptr = (char *)svptr + dataoffset; |
116 | req->dataptr = (char *)svptr + dataoffset; |
109 | req->callback = SvREFCNT_inc (callback); |
117 | req->callback = SvREFCNT_inc (callback); |
110 | |
118 | |
111 | nreqs++; |
119 | send_req (req); |
112 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
113 | } |
120 | } |
114 | |
121 | |
115 | static int |
122 | static int |
116 | poll_cb (pTHX) |
123 | poll_cb (pTHX) |
117 | { |
124 | { |
… | |
… | |
153 | } |
160 | } |
154 | |
161 | |
155 | return count; |
162 | return count; |
156 | } |
163 | } |
157 | |
164 | |
|
|
165 | static sigset_t fullsigset; |
|
|
166 | |
158 | #undef errno |
167 | #undef errno |
159 | #include <asm/unistd.h> |
168 | #include <asm/unistd.h> |
160 | |
169 | |
161 | static int |
170 | static int |
162 | aio_proc(void *thr_arg) |
171 | aio_proc(void *thr_arg) |
163 | { |
172 | { |
164 | aio_thread *thr = thr_arg; |
173 | aio_thread *thr = thr_arg; |
165 | int sig; |
174 | aio_req req; |
166 | int errno; |
175 | int errno; |
167 | aio_req req; |
|
|
168 | |
176 | |
169 | /* we rely on gcc's ability to create closures. */ |
177 | /* we rely on gcc's ability to create closures. */ |
170 | _syscall3(int,lseek,int,fd,off_t,offset,int,whence); |
178 | _syscall3(int,lseek,int,fd,off_t,offset,int,whence) |
171 | _syscall3(int,read,int,fd,char *,buf,off_t,count); |
179 | _syscall3(int,read,int,fd,char *,buf,off_t,count) |
172 | _syscall3(int,write,int,fd,char *,buf,off_t,count); |
180 | _syscall3(int,write,int,fd,char *,buf,off_t,count) |
|
|
181 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode) |
|
|
182 | _syscall1(int,close,int,fd) |
173 | |
183 | |
174 | /* first get rid of any signals */ |
184 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
175 | for (sig = 1; sig < _NSIG; sig++) |
|
|
176 | signal (sig, SIG_DFL); |
|
|
177 | |
185 | |
178 | signal (SIGPIPE, SIG_IGN); |
|
|
179 | |
|
|
180 | /* then loop */ |
186 | /* then loop */ |
181 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
187 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
182 | { |
188 | { |
183 | req->thread = thr; |
189 | req->thread = thr; |
|
|
190 | errno = 0; |
184 | |
191 | |
185 | if (req->type == REQ_READ || req->type == REQ_WRITE) |
192 | if (req->type == REQ_READ || req->type == REQ_WRITE) |
186 | { |
193 | { |
187 | errno = 0; |
|
|
188 | |
|
|
189 | if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) |
194 | if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) |
190 | { |
195 | { |
191 | if (req->type == REQ_READ) |
196 | if (req->type == REQ_READ) |
192 | req->result = read (req->fd, req->dataptr, req->length); |
197 | req->result = read (req->fd, req->dataptr, req->length); |
193 | else |
198 | else |
194 | req->result = write(req->fd, req->dataptr, req->length); |
199 | req->result = write(req->fd, req->dataptr, req->length); |
195 | } |
200 | } |
196 | |
201 | } |
197 | req->errorno = errno; |
202 | else if (req->type == REQ_OPEN) |
|
|
203 | { |
|
|
204 | req->result = open (req->dataptr, req->fd, req->mode); |
|
|
205 | } |
|
|
206 | else if (req->type == REQ_CLOSE) |
|
|
207 | { |
|
|
208 | req->result = close (req->fd); |
198 | } |
209 | } |
199 | else |
210 | else |
200 | { |
211 | { |
201 | write (respipe[1], (void *)&req, sizeof (req)); |
212 | write (respipe[1], (void *)&req, sizeof (req)); |
202 | break; |
213 | break; |
203 | } |
214 | } |
204 | |
215 | |
|
|
216 | req->errorno = errno; |
205 | write (respipe[1], (void *)&req, sizeof (req)); |
217 | write (respipe[1], (void *)&req, sizeof (req)); |
206 | } |
218 | } |
207 | |
219 | |
208 | return 0; |
220 | return 0; |
209 | } |
221 | } |
210 | |
222 | |
211 | MODULE = Linux::AIO PACKAGE = Linux::AIO |
223 | MODULE = Linux::AIO PACKAGE = Linux::AIO |
212 | |
224 | |
213 | BOOT: |
225 | BOOT: |
214 | { |
226 | { |
|
|
227 | sigfillset (&fullsigset); |
|
|
228 | sigdelset (&fullsigset, SIGTERM); |
|
|
229 | sigdelset (&fullsigset, SIGQUIT); |
|
|
230 | sigdelset (&fullsigset, SIGABRT); |
|
|
231 | sigdelset (&fullsigset, SIGINT); |
|
|
232 | |
215 | if (pipe (reqpipe) || pipe (respipe)) |
233 | if (pipe (reqpipe) || pipe (respipe)) |
216 | croak ("unable to initialize request or result pipe"); |
234 | croak ("unable to initialize request or result pipe"); |
217 | |
235 | |
218 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
236 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
219 | croak ("cannot set result pipe to nonblocking mode"); |
237 | croak ("cannot set result pipe to nonblocking mode"); |
… | |
… | |
241 | |
259 | |
242 | poll_cb (); |
260 | poll_cb (); |
243 | while (started > nthreads) |
261 | while (started > nthreads) |
244 | { |
262 | { |
245 | sched_yield (); |
263 | sched_yield (); |
|
|
264 | fcntl (respipe[0], F_SETFL, 0); |
246 | poll_cb (); |
265 | poll_cb (); |
|
|
266 | fcntl (respipe[0], F_SETFL, O_NONBLOCK); |
247 | } |
267 | } |
248 | |
268 | |
249 | void |
269 | void |
250 | aio_read(fh,offset,length,data,dataoffset,callback) |
270 | aio_read(fh,offset,length,data,dataoffset,callback) |
251 | PerlIO * fh |
271 | InOutStream fh |
252 | UV offset |
272 | UV offset |
253 | STRLEN length |
273 | IV length |
254 | SV * data |
274 | SV * data |
255 | STRLEN dataoffset |
275 | IV dataoffset |
256 | SV * callback |
276 | SV * callback |
257 | PROTOTYPE: $$$$$$ |
277 | PROTOTYPE: $$$$$$ |
258 | ALIAS: |
278 | ALIAS: |
259 | aio_write = 1 |
279 | aio_write = 1 |
260 | CODE: |
280 | CODE: |
261 | SvUPGRADE (data, SVt_PV); |
281 | SvUPGRADE (data, SVt_PV); |
262 | SvPOK_on (data); |
282 | SvPOK_on (data); |
263 | read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
283 | read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
264 | |
284 | |
|
|
285 | void |
|
|
286 | aio_open(pathname,flags,mode,callback) |
|
|
287 | char * pathname |
|
|
288 | int flags |
|
|
289 | int mode |
|
|
290 | SV * callback |
|
|
291 | PROTOTYPE: $$$$ |
|
|
292 | CODE: |
|
|
293 | aio_req req; |
|
|
294 | |
|
|
295 | New (0, req, 1, aio_cb); |
|
|
296 | |
|
|
297 | if (!req) |
|
|
298 | croak ("out of memory during aio_req allocation"); |
|
|
299 | |
|
|
300 | req->type = REQ_OPEN; |
|
|
301 | req->dataptr = pathname; |
|
|
302 | req->fd = flags; |
|
|
303 | req->mode = mode; |
|
|
304 | req->callback = SvREFCNT_inc (callback); |
|
|
305 | |
|
|
306 | send_req (req); |
|
|
307 | |
|
|
308 | void |
|
|
309 | aio_close(fh,callback) |
|
|
310 | InOutStream fh |
|
|
311 | SV * callback |
|
|
312 | PROTOTYPE: $ |
|
|
313 | CODE: |
|
|
314 | aio_req req; |
|
|
315 | |
|
|
316 | New (0, req, 1, aio_cb); |
|
|
317 | |
|
|
318 | if (!req) |
|
|
319 | croak ("out of memory during aio_req allocation"); |
|
|
320 | |
|
|
321 | req->type = REQ_CLOSE; |
|
|
322 | req->fd = PerlIO_fileno (fh); |
|
|
323 | req->callback = SvREFCNT_inc (callback); |
|
|
324 | |
|
|
325 | send_req (req); |
|
|
326 | |
265 | int |
327 | int |
266 | poll_fileno() |
328 | poll_fileno() |
267 | PROTOTYPE: |
329 | PROTOTYPE: |
268 | CODE: |
330 | CODE: |
269 | RETVAL = respipe[0]; |
331 | RETVAL = respipe[0]; |