ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-AIO/AIO.xs
Revision: 1.12
Committed: Tue Dec 25 02:33:48 2001 UTC (22 years, 4 months ago) by root
Branch: MAIN
Changes since 1.11: +8 -4 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <signal.h>
9 #include <sched.h>
10
11 typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
12
13 #define STACKSIZE 1024 /* yeah */
14
15 enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN, REQ_CLOSE };
16
17 typedef struct {
18 char stack[STACKSIZE];
19 } aio_thread;
20
21 typedef struct {
22 int type;
23 aio_thread *thread;
24
25 /* read/write */
26 int fd;
27 off_t offset;
28 size_t length;
29 ssize_t result;
30 mode_t mode; /* open */
31 int errorno;
32 SV *data, *callback;
33 void *dataptr;
34 STRLEN dataoffset;
35 } aio_cb;
36
37 typedef aio_cb *aio_req;
38
39 static int started;
40 static int nreqs;
41 static int reqpipe[2], respipe[2];
42
43 static int aio_proc(void *arg);
44
45 static void
46 start_thread(void)
47 {
48 aio_thread *thr;
49
50 New (0, thr, 1, aio_thread);
51
52 if (clone (aio_proc,
53 &(thr->stack[STACKSIZE]),
54 CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND,
55 thr) >= 0)
56 started++;
57 else
58 Safefree (thr);
59 }
60
61 static void
62 end_thread(void)
63 {
64 aio_req req;
65 New (0, req, 1, aio_cb);
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++;
74 write (reqpipe[1], &req, sizeof (aio_req));
75 }
76
77 static void
78 read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length,
79 SV *data, STRLEN dataoffset, SV*callback)
80 {
81 aio_req req;
82 STRLEN svlen;
83 char *svptr = SvPV (data, svlen);
84
85 if (dataoffset < 0)
86 dataoffset += svlen;
87
88 if (dataoffset < 0 || dataoffset > svlen)
89 croak ("data offset outside of string");
90
91 if (dowrite)
92 {
93 /* write: check length and adjust. */
94 if (length < 0 || length + dataoffset > svlen)
95 length = svlen - dataoffset;
96 }
97 else
98 {
99 /* read: grow scalar as necessary */
100 svptr = SvGROW (data, length + dataoffset);
101 }
102
103 if (length < 0)
104 croak ("length must not be negative");
105
106 New (0, req, 1, aio_cb);
107
108 if (!req)
109 croak ("out of memory during aio_req allocation");
110
111 req->type = dowrite ? REQ_WRITE : REQ_READ;
112 req->fd = fd;
113 req->offset = offset;
114 req->length = length;
115 req->data = SvREFCNT_inc (data);
116 req->dataptr = (char *)svptr + dataoffset;
117 req->callback = SvREFCNT_inc (callback);
118
119 send_req (req);
120 }
121
122 static int
123 poll_cb (pTHX)
124 {
125 dSP;
126 int count = 0;
127 aio_req req;
128
129 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
130 {
131 if (req->type == REQ_QUIT)
132 {
133 Safefree (req->thread);
134 started--;
135 }
136 else
137 {
138 int errorno = errno;
139 errno = req->errorno;
140
141 if (req->type == REQ_READ)
142 SvCUR_set (req->data, req->dataoffset
143 + req->result > 0 ? req->result : 0);
144
145 PUSHMARK (SP);
146 XPUSHs (sv_2mortal (newSViv (req->result)));
147 PUTBACK;
148 call_sv (req->callback, G_VOID);
149 SPAGAIN;
150
151 SvREFCNT_dec (req->data);
152 SvREFCNT_dec (req->callback);
153
154 errno = errorno;
155 nreqs--;
156 count++;
157 }
158
159 Safefree (req);
160 }
161
162 return count;
163 }
164
165 static sigset_t fullsigset;
166
167 #undef errno
168 #include <asm/unistd.h>
169
170 static int
171 aio_proc(void *thr_arg)
172 {
173 aio_thread *thr = thr_arg;
174 aio_req req;
175 int errno;
176
177 /* we rely on gcc's ability to create closures. */
178 _syscall3(int,lseek,int,fd,off_t,offset,int,whence)
179 _syscall3(int,read,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)
183
184 sigprocmask (SIG_SETMASK, &fullsigset, 0);
185
186 /* then loop */
187 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
188 {
189 req->thread = thr;
190 errno = 0;
191
192 if (req->type == REQ_READ || req->type == REQ_WRITE)
193 {
194 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset)
195 {
196 if (req->type == REQ_READ)
197 req->result = read (req->fd, req->dataptr, req->length);
198 else
199 req->result = write(req->fd, req->dataptr, req->length);
200 }
201 }
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);
209 }
210 else
211 {
212 write (respipe[1], (void *)&req, sizeof (req));
213 break;
214 }
215
216 req->errorno = errno;
217 write (respipe[1], (void *)&req, sizeof (req));
218 }
219
220 return 0;
221 }
222
223 MODULE = Linux::AIO PACKAGE = Linux::AIO
224
225 BOOT:
226 {
227 sigfillset (&fullsigset);
228 sigdelset (&fullsigset, SIGTERM);
229 sigdelset (&fullsigset, SIGQUIT);
230 sigdelset (&fullsigset, SIGABRT);
231 sigdelset (&fullsigset, SIGINT);
232
233 if (pipe (reqpipe) || pipe (respipe))
234 croak ("unable to initialize request or result pipe");
235
236 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
237 croak ("cannot set result pipe to nonblocking mode");
238 }
239
240 void
241 min_parallel(nthreads)
242 int nthreads
243 PROTOTYPE: $
244 CODE:
245 while (nthreads > started)
246 start_thread ();
247
248 void
249 max_parallel(nthreads)
250 int nthreads
251 PROTOTYPE: $
252 CODE:
253 int cur = started;
254 while (cur > nthreads)
255 {
256 end_thread ();
257 cur--;
258 }
259
260 poll_cb ();
261 while (started > nthreads)
262 {
263 sched_yield ();
264 fcntl (respipe[0], F_SETFL, 0);
265 poll_cb ();
266 fcntl (respipe[0], F_SETFL, O_NONBLOCK);
267 }
268
269 void
270 aio_read(fh,offset,length,data,dataoffset,callback)
271 InOutStream fh
272 UV offset
273 IV length
274 SV * data
275 IV dataoffset
276 SV * callback
277 PROTOTYPE: $$$$$$
278 ALIAS:
279 aio_write = 1
280 CODE:
281 SvUPGRADE (data, SVt_PV);
282 SvPOK_on (data);
283 read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
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
327 int
328 poll_fileno()
329 PROTOTYPE:
330 CODE:
331 RETVAL = respipe[0];
332 OUTPUT:
333 RETVAL
334
335 int
336 poll_cb(...)
337 PROTOTYPE:
338 CODE:
339 RETVAL = poll_cb (aTHX);
340 OUTPUT:
341 RETVAL
342
343 int
344 nreqs()
345 PROTOTYPE:
346 CODE:
347 RETVAL = nreqs;
348 OUTPUT:
349 RETVAL
350