… | |
… | |
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 | |
10 | #define STACKSIZE 1024 /* yeah */ |
11 | #define STACKSIZE 1024 /* yeah */ |
11 | |
12 | |
12 | #define REQ_QUIT 0 |
13 | enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN }; |
13 | #define REQ_READ 1 |
|
|
14 | #define REQ_WRITE 2 |
|
|
15 | |
14 | |
16 | typedef struct { |
15 | typedef struct { |
17 | char stack[STACKSIZE]; |
16 | char stack[STACKSIZE]; |
18 | } aio_thread; |
17 | } aio_thread; |
19 | |
18 | |
… | |
… | |
24 | /* read/write */ |
23 | /* read/write */ |
25 | int fd; |
24 | int fd; |
26 | off_t offset; |
25 | off_t offset; |
27 | size_t length; |
26 | size_t length; |
28 | ssize_t result; |
27 | ssize_t result; |
|
|
28 | mode_t mode; /* open */ |
29 | int errorno; |
29 | int errorno; |
30 | |
|
|
31 | SV *data, *callback; |
30 | SV *data, *callback; |
32 | void *dataptr; |
31 | void *dataptr; |
33 | STRLEN dataoffset; |
32 | STRLEN dataoffset; |
34 | } aio_cb; |
33 | } aio_cb; |
35 | |
34 | |
… | |
… | |
61 | end_thread(void) |
60 | end_thread(void) |
62 | { |
61 | { |
63 | aio_req req; |
62 | aio_req req; |
64 | New (0, req, 1, aio_cb); |
63 | New (0, req, 1, aio_cb); |
65 | req->type = REQ_QUIT; |
64 | req->type = REQ_QUIT; |
|
|
65 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
66 | } |
|
|
67 | |
|
|
68 | static void |
|
|
69 | send_req (aio_req req) |
|
|
70 | { |
|
|
71 | nreqs++; |
66 | write (reqpipe[1], &req, sizeof (aio_req)); |
72 | write (reqpipe[1], &req, sizeof (aio_req)); |
67 | } |
73 | } |
68 | |
74 | |
69 | static void |
75 | static void |
70 | read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, |
76 | read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length, |
… | |
… | |
103 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
109 | req->type = dowrite ? REQ_WRITE : REQ_READ; |
104 | req->fd = fd; |
110 | req->fd = fd; |
105 | req->offset = offset; |
111 | req->offset = offset; |
106 | req->length = length; |
112 | req->length = length; |
107 | req->data = SvREFCNT_inc (data); |
113 | req->data = SvREFCNT_inc (data); |
108 | req->dataptr = svptr + dataoffset; |
114 | req->dataptr = (char *)svptr + dataoffset; |
109 | req->callback = SvREFCNT_inc (callback); |
115 | req->callback = SvREFCNT_inc (callback); |
110 | |
116 | |
111 | nreqs++; |
117 | send_req (req); |
112 | write (reqpipe[1], &req, sizeof (aio_req)); |
|
|
113 | } |
118 | } |
114 | |
119 | |
115 | static int |
120 | static int |
116 | poll_cb (pTHX) |
121 | poll_cb (pTHX) |
117 | { |
122 | { |
… | |
… | |
130 | { |
135 | { |
131 | int errorno = errno; |
136 | int errorno = errno; |
132 | errno = req->errorno; |
137 | errno = req->errorno; |
133 | |
138 | |
134 | if (req->type == REQ_READ) |
139 | if (req->type == REQ_READ) |
135 | SvCUR_set (req->data, req->result > 0 |
140 | SvCUR_set (req->data, req->dataoffset |
136 | ? req->dataoffset + req->result |
141 | + req->result > 0 ? req->result : 0); |
137 | : req->dataoffset); |
|
|
138 | |
142 | |
139 | PUSHMARK (SP); |
143 | PUSHMARK (SP); |
140 | XPUSHs (sv_2mortal (newSViv (req->result))); |
144 | XPUSHs (sv_2mortal (newSViv (req->result))); |
141 | PUTBACK; |
145 | PUTBACK; |
142 | call_sv (req->callback, G_VOID); |
146 | call_sv (req->callback, G_VOID); |
… | |
… | |
154 | } |
158 | } |
155 | |
159 | |
156 | return count; |
160 | return count; |
157 | } |
161 | } |
158 | |
162 | |
|
|
163 | static sigset_t fullsigset; |
|
|
164 | |
159 | #undef errno |
165 | #undef errno |
160 | #include <asm/unistd.h> |
166 | #include <asm/unistd.h> |
161 | |
167 | |
162 | static int |
168 | static int |
163 | aio_proc(void *thr_arg) |
169 | aio_proc(void *thr_arg) |
… | |
… | |
165 | aio_thread *thr = thr_arg; |
171 | aio_thread *thr = thr_arg; |
166 | int sig; |
172 | int sig; |
167 | int errno; |
173 | int errno; |
168 | aio_req req; |
174 | aio_req req; |
169 | |
175 | |
|
|
176 | sigprocmask (SIG_SETMASK, &fullsigset, 0); |
|
|
177 | |
170 | /* we rely on gcc's ability to create closures. */ |
178 | /* we rely on gcc's ability to create closures. */ |
171 | _syscall3(int,lseek,int,fd,off_t,offset,int,whence); |
179 | _syscall3(int,lseek,int,fd,off_t,offset,int,whence); |
172 | _syscall3(int,read,int,fd,char *,buf,off_t,count); |
180 | _syscall3(int,read,int,fd,char *,buf,off_t,count); |
173 | _syscall3(int,write,int,fd,char *,buf,off_t,count); |
181 | _syscall3(int,write,int,fd,char *,buf,off_t,count); |
|
|
182 | _syscall3(int,open,char *,pathname,int,flags,mode_t,mode); |
174 | |
183 | |
175 | /* first get rid of any signals */ |
|
|
176 | for (sig = 1; sig < _NSIG; sig++) |
|
|
177 | signal (sig, SIG_DFL); |
|
|
178 | |
|
|
179 | signal (SIGPIPE, SIG_IGN); |
|
|
180 | |
|
|
181 | /* then loop */ |
184 | /* then loop */ |
182 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
185 | while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) |
183 | { |
186 | { |
184 | req->thread = thr; |
187 | req->thread = thr; |
|
|
188 | errno = 0; |
185 | |
189 | |
186 | if (req->type == REQ_READ || req->type == REQ_WRITE) |
190 | if (req->type == REQ_READ || req->type == REQ_WRITE) |
187 | { |
191 | { |
188 | errno = 0; |
|
|
189 | |
|
|
190 | if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) |
192 | if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) |
191 | { |
193 | { |
192 | if (req->type == REQ_READ) |
194 | if (req->type == REQ_READ) |
193 | req->result = read (req->fd, req->dataptr, req->length); |
195 | req->result = read (req->fd, req->dataptr, req->length); |
194 | else |
196 | else |
195 | req->result = write(req->fd, req->dataptr, req->length); |
197 | req->result = write(req->fd, req->dataptr, req->length); |
196 | } |
198 | } |
197 | |
199 | } |
198 | req->errorno = errno; |
200 | else if (req->type == REQ_OPEN) |
|
|
201 | { |
|
|
202 | req->result = open (req->dataptr, req->fd, req->mode); |
199 | } |
203 | } |
200 | else |
204 | else |
201 | { |
205 | { |
202 | write (respipe[1], (void *)&req, sizeof (req)); |
206 | write (respipe[1], (void *)&req, sizeof (req)); |
203 | break; |
207 | break; |
204 | } |
208 | } |
205 | |
209 | |
|
|
210 | req->errorno = errno; |
206 | write (respipe[1], (void *)&req, sizeof (req)); |
211 | write (respipe[1], (void *)&req, sizeof (req)); |
207 | } |
212 | } |
208 | |
213 | |
209 | return 0; |
214 | return 0; |
210 | } |
215 | } |
211 | |
216 | |
212 | MODULE = Linux::AIO PACKAGE = Linux::AIO |
217 | MODULE = Linux::AIO PACKAGE = Linux::AIO |
213 | |
218 | |
214 | BOOT: |
219 | BOOT: |
215 | { |
220 | { |
|
|
221 | sigfillset (&fullsigset); |
|
|
222 | |
216 | if (pipe (reqpipe) || pipe (respipe)) |
223 | if (pipe (reqpipe) || pipe (respipe)) |
217 | croak ("unable to initialize request or result pipe"); |
224 | croak ("unable to initialize request or result pipe"); |
218 | |
225 | |
219 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
226 | if (fcntl (respipe[0], F_SETFL, O_NONBLOCK)) |
220 | croak ("cannot set result pipe to nonblocking mode"); |
227 | croak ("cannot set result pipe to nonblocking mode"); |
… | |
… | |
248 | } |
255 | } |
249 | |
256 | |
250 | void |
257 | void |
251 | aio_read(fh,offset,length,data,dataoffset,callback) |
258 | aio_read(fh,offset,length,data,dataoffset,callback) |
252 | PerlIO * fh |
259 | PerlIO * fh |
253 | unsigned long offset |
260 | UV offset |
254 | long length |
261 | STRLEN length |
255 | SV * data |
262 | SV * data |
256 | STRLEN dataoffset |
263 | STRLEN dataoffset |
257 | SV * callback |
264 | SV * callback |
258 | PROTOTYPE: $$$$$$ |
265 | PROTOTYPE: $$$$$$ |
259 | ALIAS: |
266 | ALIAS: |
260 | aio_write = 1 |
267 | aio_write = 1 |
261 | CODE: |
268 | CODE: |
262 | sv_upgrade (data, SVt_PV); |
269 | SvUPGRADE (data, SVt_PV); |
|
|
270 | SvPOK_on (data); |
263 | read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
271 | read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback); |
|
|
272 | |
|
|
273 | void |
|
|
274 | aio_open(pathname,flags,mode,callback) |
|
|
275 | char * pathname |
|
|
276 | int flags |
|
|
277 | int mode |
|
|
278 | SV * callback |
|
|
279 | PROTOTYPE: $$$$ |
|
|
280 | CODE: |
|
|
281 | aio_req req; |
|
|
282 | |
|
|
283 | New (0, req, 1, aio_cb); |
|
|
284 | |
|
|
285 | if (!req) |
|
|
286 | croak ("out of memory during aio_req allocation"); |
|
|
287 | |
|
|
288 | req->type = REQ_OPEN; |
|
|
289 | req->dataptr = pathname; |
|
|
290 | req->fd = flags; |
|
|
291 | req->mode = mode; |
|
|
292 | req->callback = SvREFCNT_inc (callback); |
|
|
293 | |
|
|
294 | send_req (req); |
264 | |
295 | |
265 | int |
296 | int |
266 | poll_fileno() |
297 | poll_fileno() |
267 | PROTOTYPE: |
298 | PROTOTYPE: |
268 | CODE: |
299 | CODE: |
269 | RETVAL = respipe[0]; |
300 | RETVAL = respipe[0]; |
270 | OUTPUT: |
301 | OUTPUT: |
271 | RETVAL |
302 | RETVAL |
272 | |
303 | |
273 | int |
304 | int |
274 | poll_cb() |
305 | poll_cb(...) |
275 | PROTOTYPE: |
306 | PROTOTYPE: |
276 | CODE: |
307 | CODE: |
277 | RETVAL = poll_cb (aTHX); |
308 | RETVAL = poll_cb (aTHX); |
278 | OUTPUT: |
309 | OUTPUT: |
279 | RETVAL |
310 | RETVAL |