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.3 by root, Tue Aug 14 04:33:50 2001 UTC vs.
Revision 1.7 by root, Wed Aug 15 03:24:08 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>
7#include <sched.h> 8#include <sched.h>
8 9
9#define STACKSIZE 128 /* yeah */ 10#define STACKSIZE 4096 /* yeah */
10 11
11#define REQ_QUIT 0 12#define REQ_QUIT 0
12#define REQ_READ 1 13#define REQ_READ 1
13#define REQ_WRITE 2 14#define REQ_WRITE 2
14 15
25 off_t offset; 26 off_t offset;
26 size_t length; 27 size_t length;
27 ssize_t result; 28 ssize_t result;
28 int errorno; 29 int errorno;
29 30
30 SV *data; 31 SV *data, *callback;
31 void *dataptr; 32 void *dataptr;
32 STRLEN dataoffset; 33 STRLEN dataoffset;
33} aio_cb; 34} aio_cb;
34 35
35typedef aio_cb *aio_req; 36typedef aio_cb *aio_req;
36 37
37static int started; 38static int started;
39static int nreqs;
38static int reqpipe[2], respipe[2]; 40static int reqpipe[2], respipe[2];
39 41
40static int aio_proc(void *arg); 42static int aio_proc(void *arg);
41 43
42static void 44static void
56} 58}
57 59
58static void 60static void
59end_thread(void) 61end_thread(void)
60{ 62{
61 aio_req req = 0; 63 aio_req req;
64 New (0, req, 1, aio_cb);
65 req->type = REQ_QUIT;
62 write (reqpipe[1], &req, sizeof (aio_req)); 66 write (reqpipe[1], &req, sizeof (aio_req));
63 started--;
64} 67}
65 68
66static void 69static void
67set_errno(int errorno) 70read_write (pTHX_ int dowrite, int fd, off_t offset, size_t length,
71 SV *data, STRLEN dataoffset, SV*callback)
68{ 72{
73 aio_req req;
74 STRLEN svlen;
75 char *svptr = SvPV (data, svlen);
76
77 if (dataoffset < 0)
78 dataoffset += svlen;
79
80 if (dataoffset < 0 || dataoffset > svlen)
81 croak ("data offset outside of string");
82
83 if (dowrite)
84 {
85 /* write: check length and adjust. */
86 if (length < 0 || length + dataoffset > svlen)
87 length = svlen - dataoffset;
88 }
89 else
90 {
91 /* read: grow scalar as necessary */
92 svptr = SvGROW (data, length + dataoffset);
93 }
94
95 if (length < 0)
96 croak ("length must not be negative");
97
98 New (0, req, 1, aio_cb);
99
100 if (!req)
101 croak ("out of memory during aio_req allocation");
102
103 req->type = dowrite ? REQ_WRITE : REQ_READ;
104 req->fd = fd;
105 req->offset = offset;
106 req->length = length;
107 req->data = SvREFCNT_inc (data);
108 req->dataptr = (char *)svptr + dataoffset;
109 req->callback = SvREFCNT_inc (callback);
110
111 nreqs++;
112 write (reqpipe[1], &req, sizeof (aio_req));
113}
114
115static int
116poll_cb (pTHX)
117{
118 dSP;
119 int count = 0;
120 aio_req req;
121
122 while (read (respipe[0], (void *)&req, sizeof (req)) == sizeof (req))
123 {
124 if (req->type == REQ_QUIT)
125 {
126 Safefree (req->thread);
127 started--;
128 }
129 else
130 {
131 int errorno = errno;
132 errno = req->errorno;
133
134 if (req->type == REQ_READ)
135 SvCUR_set (req->data, req->dataoffset
136 + req->result > 0 ? req->result : 0);
137
138 PUSHMARK (SP);
139 XPUSHs (sv_2mortal (newSViv (req->result)));
140 PUTBACK;
141 call_sv (req->callback, G_VOID);
142 SPAGAIN;
143
144 SvREFCNT_dec (req->data);
145 SvREFCNT_dec (req->callback);
146
69 errno = errorno; 147 errno = errorno;
148 nreqs--;
149 count++;
150 }
151
152 Safefree (req);
153 }
154
155 return count;
70} 156}
71 157
72#undef errno 158#undef errno
73#include <asm/unistd.h> 159#include <asm/unistd.h>
74 160
85 _syscall3(int,read,int,fd,char *,buf,off_t,count); 171 _syscall3(int,read,int,fd,char *,buf,off_t,count);
86 _syscall3(int,write,int,fd,char *,buf,off_t,count); 172 _syscall3(int,write,int,fd,char *,buf,off_t,count);
87 173
88 /* first get rid of any signals */ 174 /* first get rid of any signals */
89 for (sig = 1; sig < _NSIG; sig++) 175 for (sig = 1; sig < _NSIG; sig++)
90 if (sig != SIGTERM)
91 signal (sig, SIG_IGN); 176 signal (sig, SIG_DFL);
177
178 signal (SIGPIPE, SIG_IGN);
92 179
93 /* then loop */ 180 /* then loop */
94 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req)) 181 while (read (reqpipe[0], (void *)&req, sizeof (req)) == sizeof (req))
95 { 182 {
96 req->thread = thr; 183 req->thread = thr;
125 212
126BOOT: 213BOOT:
127{ 214{
128 if (pipe (reqpipe) || pipe (respipe)) 215 if (pipe (reqpipe) || pipe (respipe))
129 croak ("unable to initialize request or result pipe"); 216 croak ("unable to initialize request or result pipe");
217
218 if (fcntl (respipe[0], F_SETFL, O_NONBLOCK))
219 croak ("cannot set result pipe to nonblocking mode");
130} 220}
131 221
132void 222void
133min_parallel(nthreads) 223min_parallel(nthreads)
134 int nthreads 224 int nthreads
225 PROTOTYPE: $
135 CODE: 226 CODE:
136 while (nthreads > started) 227 while (nthreads > started)
137 start_thread (); 228 start_thread ();
138 229
139void 230void
140max_parallel(nthreads) 231max_parallel(nthreads)
141 int nthreads 232 int nthreads
233 PROTOTYPE: $
142 CODE: 234 CODE:
235 int cur = started;
236 while (cur > nthreads)
237 {
238 end_thread ();
239 cur--;
240 }
241
242 poll_cb ();
143 while (started > nthreads) 243 while (started > nthreads)
144 end_thread (); 244 {
245 sched_yield ();
246 poll_cb ();
247 }
145 248
146void 249void
147read(fh,offset,length,data,dataoffset,callback) 250aio_read(fh,offset,length,data,dataoffset,callback)
251 PerlIO * fh
252 UV offset
253 STRLEN length
254 SV * data
255 STRLEN dataoffset
256 SV * callback
257 PROTOTYPE: $$$$$$
148 ALIAS: 258 ALIAS:
149 write = 1 259 aio_write = 1
150 CODE: 260 CODE:
261 SvUPGRADE (data, SVt_PV);
262 SvPOK_on (data);
263 read_write (aTHX_ ix, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
264
265int
266poll_fileno()
267 PROTOTYPE:
268 CODE:
269 RETVAL = respipe[0];
270 OUTPUT:
271 RETVAL
272
273int
274poll_cb(...)
275 PROTOTYPE:
276 CODE:
277 RETVAL = poll_cb (aTHX);
278 OUTPUT:
279 RETVAL
280
281int
282nreqs()
283 PROTOTYPE:
284 CODE:
285 RETVAL = nreqs;
286 OUTPUT:
287 RETVAL
288

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines