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.4 by root, Tue Aug 14 14:56:22 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_QUIT 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
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 result; 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 dataoffset; 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];
39 42
40static int aio_proc(void *arg); 43static int aio_proc(void *arg);
41 44
42static void 45static void
56} 59}
57 60
58static void 61static void
59end_thread(void) 62end_thread(void)
60{ 63{
61 aio_req req = 0; 64 aio_req req;
65 New (0, req, 1, aio_cb);
66 req->type = REQ_QUIT;
62 write (reqpipe[1], &req, sizeof (aio_req)); 67 write (reqpipe[1], &req, sizeof (aio_req));
63 started--;
64} 68}
65 69
66static void 70static void
67set_errno(int errorno) 71send_req (aio_req req)
68{ 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
69 errno = errorno; 154 errno = errorno;
155 nreqs--;
156 count++;
157 }
158
159 Safefree (req);
160 }
161
162 return count;
70} 163}
164
165static sigset_t fullsigset;
71 166
72#undef errno 167#undef errno
73#include <asm/unistd.h> 168#include <asm/unistd.h>
74 169
75static int 170static int
76aio_proc(void *thr_arg) 171aio_proc(void *thr_arg)
77{ 172{
78 aio_thread *thr = thr_arg; 173 aio_thread *thr = thr_arg;
79 int sig; 174 aio_req req;
80 int errno; 175 int errno;
81 aio_req req;
82 176
83 /* we rely on gcc's ability to create closures. */ 177 /* we rely on gcc's ability to create closures. */
84 _syscall3(int,lseek,int,fd,off_t,offset,int,whence); 178 _syscall3(int,lseek,int,fd,off_t,offset,int,whence)
85 _syscall3(int,read,int,fd,char *,buf,off_t,count); 179 _syscall3(int,read,int,fd,char *,buf,off_t,count)
86 _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)
87 183
88 /* first get rid of any signals */ 184 sigprocmask (SIG_SETMASK, &fullsigset, 0);
89 for (sig = 1; sig < _NSIG; sig++)
90 signal (sig, SIG_IGN);
91 185
92 signal (SIGTERM, SIG_DFL);
93
94 /* then loop */ 186 /* then loop */
95 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 187 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
96 { 188 {
97 req->thread = thr; 189 req->thread = thr;
190 errno = 0;
98 191
99 if (req->type == REQ_READ || req->type == REQ_WRITE) 192 if (req->type == REQ_READ || req->type == REQ_WRITE)
100 { 193 {
101 errno = 0;
102
103 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset) 194 if (lseek (req->fd, req->offset, SEEK_SET) == req->offset)
104 { 195 {
105 if (req->type == REQ_READ) 196 if (req->type == REQ_READ)
106 req->result = read (req->fd, req->dataptr, req->length); 197 req->result = read (req->fd, req->dataptr, req->length);
107 else 198 else
108 req->result = write(req->fd, req->dataptr, req->length); 199 req->result = write(req->fd, req->dataptr, req->length);
109 } 200 }
110 201 }
111 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);
112 } 209 }
113 else 210 else
114 { 211 {
115 write (respipe[1], (void *)&req, sizeof (req)); 212 write (respipe[1], (void *)&req, sizeof (req));
116 break; 213 break;
117 } 214 }
118 215
216 req->errorno = errno;
119 write (respipe[1], (void *)&req, sizeof (req)); 217 write (respipe[1], (void *)&req, sizeof (req));
120 } 218 }
121 219
122 return 0; 220 return 0;
123} 221}
124 222
125MODULE = Linux::AIO PACKAGE = Linux::AIO 223MODULE = Linux::AIO PACKAGE = Linux::AIO
126 224
127BOOT: 225BOOT:
128{ 226{
227 sigfillset (&fullsigset);
228 sigdelset (&fullsigset, SIGTERM);
229 sigdelset (&fullsigset, SIGQUIT);
230 sigdelset (&fullsigset, SIGABRT);
231 sigdelset (&fullsigset, SIGINT);
232
129 if (pipe (reqpipe) || pipe (respipe)) 233 if (pipe (reqpipe) || pipe (respipe))
130 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");
131} 238}
132 239
133void 240void
134min_parallel(nthreads) 241min_parallel(nthreads)
135 int nthreads 242 int nthreads
243 PROTOTYPE: $
136 CODE: 244 CODE:
137 while (nthreads > started) 245 while (nthreads > started)
138 start_thread (); 246 start_thread ();
139 247
140void 248void
141max_parallel(nthreads) 249max_parallel(nthreads)
142 int nthreads 250 int nthreads
251 PROTOTYPE: $
143 CODE: 252 CODE:
253 int cur = started;
254 while (cur > nthreads)
255 {
256 end_thread ();
257 cur--;
258 }
259
260 poll_cb ();
144 while (started > nthreads) 261 while (started > nthreads)
145 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 }
146 268
147void 269void
148read(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: $$$$$$
149 ALIAS: 278 ALIAS:
150 write = 1 279 aio_write = 1
151 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