ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-AIO/AIO.xs
(Generate patch)

Comparing Linux-AIO/AIO.xs (file contents):
Revision 1.1 by root, Tue Aug 14 03:18:53 2001 UTC vs.
Revision 1.12 by root, Tue Dec 25 02:33:48 2001 UTC

2#include "perl.h" 2#include "perl.h"
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>
8#include <signal.h>
7#include <sched.h> 9#include <sched.h>
8 10
11typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
12
9#define STACKSIZE 128 /* yeah */ 13#define STACKSIZE 1024 /* yeah */
10 14
11#define REQ_EXIT 0 15enum { REQ_QUIT, REQ_READ, REQ_WRITE, REQ_OPEN, REQ_CLOSE };
12#define REQ_READ 1
13#define REQ_WRITE 2
14 16
15typedef struct { 17typedef struct {
16 char stack[STACKSIZE]; 18 char stack[STACKSIZE];
17} aio_thread; 19} aio_thread;
18 20
22 24
23/* read/write */ 25/* read/write */
24 int fd; 26 int fd;
25 off_t offset; 27 off_t offset;
26 size_t length; 28 size_t length;
27 ssize_t done; 29 ssize_t result;
30 mode_t mode; /* open */
28 int errorno; 31 int errorno;
29 32 SV *data, *callback;
30 SV *data;
31 void *dataptr; 33 void *dataptr;
32 STRLEN offset; 34 STRLEN dataoffset;
33} aio_cb; 35} aio_cb;
34 36
35typedef aio_cb *aio_req; 37typedef aio_cb *aio_req;
36 38
37static int started; 39static int started;
40static int nreqs;
38static int reqpipe[2], respipe[2]; 41static int reqpipe[2], respipe[2];
42
43static int aio_proc(void *arg);
39 44
40static void 45static void
41start_thread(void) 46start_thread(void)
42{ 47{
43 aio_thread *thr = NEW ( 48 aio_thread *thr;
49
50 New (0, thr, 1, aio_thread);
51
44 __clone (aio_proc, 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);
45} 59}
46 60
47static void 61static void
48end_thread(void) 62end_thread(void)
49{ 63{
50 aio_req req = 0; 64 aio_req req;
65 New (0, req, 1, aio_cb);
66 req->type = REQ_QUIT;
51 write (reqpipe[1], &req, sizeof (aio_req)); 67 write (reqpipe[1], &req, sizeof (aio_req));
52 nthreads--; 68}
69
70static void
71send_req (aio_req req)
72{
73 nreqs++;
74 write (reqpipe[1], &req, sizeof (aio_req));
75}
76
77static void
78read_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
122static int
123poll_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
165static sigset_t fullsigset;
166
167#undef errno
168#include <asm/unistd.h>
169
170static int
171aio_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;
53} 221}
54 222
55MODULE = Linux::AIO PACKAGE = Linux::AIO 223MODULE = Linux::AIO PACKAGE = Linux::AIO
56 224
57BOOT: 225BOOT:
58{ 226{
227 sigfillset (&fullsigset);
228 sigdelset (&fullsigset, SIGTERM);
229 sigdelset (&fullsigset, SIGQUIT);
230 sigdelset (&fullsigset, SIGABRT);
231 sigdelset (&fullsigset, SIGINT);
232
59 if (pipe (reqpipe) || pipe (respipe)) 233 if (pipe (reqpipe) || pipe (respipe))
60 croak ("unable to initialize request or result pipe"); 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");
61} 238}
62 239
63void 240void
64min_parallel(nthreads) 241min_parallel(nthreads)
65 int nthreads 242 int nthreads
243 PROTOTYPE: $
66 CODE: 244 CODE:
67 while (nthreads > started) 245 while (nthreads > started)
68 start_thread (); 246 start_thread ();
69 247
70void 248void
71max_parallel(nthreads) 249max_parallel(nthreads)
72 int nthreads 250 int nthreads
251 PROTOTYPE: $
73 CODE: 252 CODE:
253 int cur = started;
254 while (cur > nthreads)
255 {
256 end_thread ();
257 cur--;
258 }
259
260 poll_cb ();
74 while (started > nthreads) 261 while (started > nthreads)
75 end_thread (); 262 {
263 sched_yield ();
264 fcntl (respipe[0], F_SETFL, 0);
265 poll_cb ();
266 fcntl (respipe[0], F_SETFL, O_NONBLOCK);
267 }
76 268
77void 269void
78read(fh,offset,length,data,dataoffset,callback) 270aio_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
79 CODE: 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
285void
286aio_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
308void
309aio_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
327int
328poll_fileno()
329 PROTOTYPE:
330 CODE:
331 RETVAL = respipe[0];
332 OUTPUT:
333 RETVAL
334
335int
336poll_cb(...)
337 PROTOTYPE:
338 CODE:
339 RETVAL = poll_cb (aTHX);
340 OUTPUT:
341 RETVAL
342
343int
344nreqs()
345 PROTOTYPE:
346 CODE:
347 RETVAL = nreqs;
348 OUTPUT:
349 RETVAL
350

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines