ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.12
Committed: Wed Jul 20 21:57:04 2005 UTC (18 years, 10 months ago) by root
Branch: MAIN
Changes since 1.11: +0 -5 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #define _XOPEN_SOURCE 500
2
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <sched.h>
14 #if __linux
15 #include <sys/syscall.h>
16 #endif
17
18 #include <pthread.h>
19
20 typedef void *InputStream; /* hack, but 5.6.1 is simply toooo old ;) */
21 typedef void *OutputStream; /* hack, but 5.6.1 is simply toooo old ;) */
22 typedef void *InOutStream; /* hack, but 5.6.1 is simply toooo old ;) */
23
24 #if __ia64
25 # define STACKSIZE 65536
26 #else
27 # define STACKSIZE 4096
28 #endif
29
30 enum {
31 REQ_QUIT,
32 REQ_OPEN, REQ_CLOSE,
33 REQ_READ, REQ_WRITE, REQ_READAHEAD,
34 REQ_STAT, REQ_LSTAT, REQ_FSTAT, REQ_UNLINK,
35 REQ_FSYNC, REQ_FDATASYNC,
36 };
37
38 typedef struct aio_cb {
39 struct aio_cb *volatile next;
40
41 int type;
42
43 int fd;
44 off_t offset;
45 size_t length;
46 ssize_t result;
47 mode_t mode; /* open */
48 int errorno;
49 SV *data, *callback;
50 void *dataptr;
51 STRLEN dataoffset;
52
53 Stat_t *statdata;
54 } aio_cb;
55
56 typedef aio_cb *aio_req;
57
58 static int started;
59 static volatile int nreqs;
60 static int max_outstanding = 1<<30;
61 static int respipe [2];
62
63 static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
64 static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
65 static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
66
67 static volatile aio_req reqs, reqe; /* queue start, queue end */
68 static volatile aio_req ress, rese; /* queue start, queue end */
69
70 static void
71 poll_wait ()
72 {
73 if (nreqs && !ress)
74 {
75 fd_set rfd;
76 FD_ZERO(&rfd);
77 FD_SET(respipe [0], &rfd);
78
79 select (respipe [0] + 1, &rfd, 0, 0, 0);
80 }
81 }
82
83 static int
84 poll_cb ()
85 {
86 dSP;
87 int count = 0;
88 aio_req req, prv;
89
90 pthread_mutex_lock (&reslock);
91
92 {
93 /* read any signals sent by the worker threads */
94 char buf [32];
95 while (read (respipe [0], buf, 32) > 0)
96 ;
97 }
98
99 req = ress;
100 ress = rese = 0;
101
102 pthread_mutex_unlock (&reslock);
103
104 while (req)
105 {
106 nreqs--;
107
108 if (req->type == REQ_QUIT)
109 started--;
110 else
111 {
112 int errorno = errno;
113 errno = req->errorno;
114
115 if (req->type == REQ_READ)
116 SvCUR_set (req->data, req->dataoffset
117 + req->result > 0 ? req->result : 0);
118
119 if (req->data)
120 SvREFCNT_dec (req->data);
121
122 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
123 {
124 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
125 PL_laststatval = req->result;
126 PL_statcache = *(req->statdata);
127
128 Safefree (req->statdata);
129 }
130
131 PUSHMARK (SP);
132 XPUSHs (sv_2mortal (newSViv (req->result)));
133
134 if (req->type == REQ_OPEN)
135 {
136 /* convert fd to fh */
137 SV *fh;
138
139 PUTBACK;
140 call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
141 SPAGAIN;
142
143 fh = POPs;
144
145 PUSHMARK (SP);
146 XPUSHs (fh);
147 }
148
149 if (SvOK (req->callback))
150 {
151 PUTBACK;
152 call_sv (req->callback, G_VOID | G_EVAL);
153 SPAGAIN;
154 }
155
156 if (req->callback)
157 SvREFCNT_dec (req->callback);
158
159 errno = errorno;
160 count++;
161 }
162
163 prv = req;
164 req = req->next;
165 Safefree (prv);
166
167 /* TODO: croak on errors? */
168 }
169
170 return count;
171 }
172
173 static void *aio_proc(void *arg);
174
175 static void
176 start_thread (void)
177 {
178 sigset_t fullsigset, oldsigset;
179 pthread_t tid;
180 pthread_attr_t attr;
181
182 pthread_attr_init (&attr);
183 pthread_attr_setstacksize (&attr, STACKSIZE);
184 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
185
186 sigfillset (&fullsigset);
187 sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
188
189 if (pthread_create (&tid, &attr, aio_proc, 0) == 0)
190 started++;
191
192 sigprocmask (SIG_SETMASK, &oldsigset, 0);
193 }
194
195 static void
196 send_req (aio_req req)
197 {
198 nreqs++;
199
200 pthread_mutex_lock (&reqlock);
201
202 req->next = 0;
203
204 if (reqe)
205 {
206 reqe->next = req;
207 reqe = req;
208 }
209 else
210 reqe = reqs = req;
211
212 pthread_cond_signal (&reqwait);
213 pthread_mutex_unlock (&reqlock);
214
215 while (nreqs > max_outstanding)
216 {
217 poll_wait ();
218 poll_cb ();
219 }
220 }
221
222 static void
223 end_thread (void)
224 {
225 aio_req req;
226 New (0, req, 1, aio_cb);
227 req->type = REQ_QUIT;
228
229 send_req (req);
230 }
231
232 static void
233 read_write (int dowrite, int fd, off_t offset, size_t length,
234 SV *data, STRLEN dataoffset, SV *callback)
235 {
236 aio_req req;
237 STRLEN svlen;
238 char *svptr = SvPV (data, svlen);
239
240 SvUPGRADE (data, SVt_PV);
241 SvPOK_on (data);
242
243 if (dataoffset < 0)
244 dataoffset += svlen;
245
246 if (dataoffset < 0 || dataoffset > svlen)
247 croak ("data offset outside of string");
248
249 if (dowrite)
250 {
251 /* write: check length and adjust. */
252 if (length < 0 || length + dataoffset > svlen)
253 length = svlen - dataoffset;
254 }
255 else
256 {
257 /* read: grow scalar as necessary */
258 svptr = SvGROW (data, length + dataoffset);
259 }
260
261 if (length < 0)
262 croak ("length must not be negative");
263
264 Newz (0, req, 1, aio_cb);
265
266 if (!req)
267 croak ("out of memory during aio_req allocation");
268
269 req->type = dowrite ? REQ_WRITE : REQ_READ;
270 req->fd = fd;
271 req->offset = offset;
272 req->length = length;
273 req->data = SvREFCNT_inc (data);
274 req->dataptr = (char *)svptr + dataoffset;
275 req->callback = SvREFCNT_inc (callback);
276
277 send_req (req);
278 }
279
280 static void *
281 aio_proc (void *thr_arg)
282 {
283 aio_req req;
284 int type;
285
286 do
287 {
288 pthread_mutex_lock (&reqlock);
289
290 for (;;)
291 {
292 req = reqs;
293
294 if (reqs)
295 {
296 reqs = reqs->next;
297 if (!reqs) reqe = 0;
298 }
299
300 if (req)
301 break;
302
303 pthread_cond_wait (&reqwait, &reqlock);
304 }
305
306 pthread_mutex_unlock (&reqlock);
307
308 errno = 0; /* strictly unnecessary */
309
310 type = req->type;
311
312 switch (type)
313 {
314 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
315 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
316 #if SYS_readahead
317 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
318 #else
319 case REQ_READAHEAD: req->result = -1; errno = ENOSYS; break;
320 #endif
321
322 case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
323 case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
324 case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
325
326 case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
327 case REQ_CLOSE: req->result = close (req->fd); break;
328 case REQ_UNLINK: req->result = unlink (req->dataptr); break;
329
330 case REQ_FSYNC: req->result = fsync (req->fd); break;
331 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
332
333 case REQ_QUIT:
334 break;
335
336 default:
337 req->result = ENOSYS;
338 break;
339 }
340
341 req->errorno = errno;
342
343 pthread_mutex_lock (&reslock);
344
345 req->next = 0;
346
347 if (rese)
348 {
349 rese->next = req;
350 rese = req;
351 }
352 else
353 {
354 rese = ress = req;
355
356 /* write a dummy byte to the pipe so fh becomes ready */
357 write (respipe [1], &respipe, 1);
358 }
359
360 pthread_mutex_unlock (&reslock);
361 }
362 while (type != REQ_QUIT);
363
364 return 0;
365 }
366
367 MODULE = IO::AIO PACKAGE = IO::AIO
368
369 PROTOTYPES: ENABLE
370
371 BOOT:
372 {
373 if (pipe (respipe))
374 croak ("unable to initialize result pipe");
375
376 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
377 croak ("cannot set result pipe to nonblocking mode");
378
379 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
380 croak ("cannot set result pipe to nonblocking mode");
381 }
382
383 void
384 min_parallel(nthreads)
385 int nthreads
386 PROTOTYPE: $
387 CODE:
388 while (nthreads > started)
389 start_thread ();
390
391 void
392 max_parallel(nthreads)
393 int nthreads
394 PROTOTYPE: $
395 CODE:
396 {
397 int cur = started;
398 while (cur > nthreads)
399 {
400 end_thread ();
401 cur--;
402 }
403
404 while (started > nthreads)
405 {
406 poll_wait ();
407 poll_cb ();
408 }
409 }
410
411 int
412 max_outstanding(nreqs)
413 int nreqs
414 PROTOTYPE: $
415 CODE:
416 RETVAL = max_outstanding;
417 max_outstanding = nreqs;
418
419 void
420 aio_open(pathname,flags,mode,callback=&PL_sv_undef)
421 SV * pathname
422 int flags
423 int mode
424 SV * callback
425 PROTOTYPE: $$$;$
426 CODE:
427 {
428 aio_req req;
429
430 Newz (0, req, 1, aio_cb);
431
432 if (!req)
433 croak ("out of memory during aio_req allocation");
434
435 req->type = REQ_OPEN;
436 req->data = newSVsv (pathname);
437 req->dataptr = SvPV_nolen (req->data);
438 req->fd = flags;
439 req->mode = mode;
440 req->callback = SvREFCNT_inc (callback);
441
442 send_req (req);
443 }
444
445 void
446 aio_close(fh,callback=&PL_sv_undef)
447 InputStream fh
448 SV * callback
449 PROTOTYPE: $;$
450 ALIAS:
451 aio_close = REQ_CLOSE
452 aio_fsync = REQ_FSYNC
453 aio_fdatasync = REQ_FDATASYNC
454 CODE:
455 {
456 aio_req req;
457
458 Newz (0, req, 1, aio_cb);
459
460 if (!req)
461 croak ("out of memory during aio_req allocation");
462
463 req->type = ix;
464 req->fd = PerlIO_fileno (fh);
465 req->callback = SvREFCNT_inc (callback);
466
467 send_req (req);
468 }
469
470 void
471 aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
472 InputStream fh
473 UV offset
474 IV length
475 SV * data
476 IV dataoffset
477 SV * callback
478 PROTOTYPE: $$$$$;$
479 CODE:
480 read_write (0, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
481
482 void
483 aio_write(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
484 OutputStream fh
485 UV offset
486 IV length
487 SV * data
488 IV dataoffset
489 SV * callback
490 PROTOTYPE: $$$$$;$
491 CODE:
492 read_write (1, PerlIO_fileno (fh), offset, length, data, dataoffset, callback);
493
494 void
495 aio_readahead(fh,offset,length,callback=&PL_sv_undef)
496 InputStream fh
497 UV offset
498 IV length
499 SV * callback
500 PROTOTYPE: $$$;$
501 CODE:
502 {
503 aio_req req;
504
505 if (length < 0)
506 croak ("length must not be negative");
507
508 Newz (0, req, 1, aio_cb);
509
510 if (!req)
511 croak ("out of memory during aio_req allocation");
512
513 req->type = REQ_READAHEAD;
514 req->fd = PerlIO_fileno (fh);
515 req->offset = offset;
516 req->length = length;
517 req->callback = SvREFCNT_inc (callback);
518
519 send_req (req);
520 }
521
522 void
523 aio_stat(fh_or_path,callback=&PL_sv_undef)
524 SV * fh_or_path
525 SV * callback
526 ALIAS:
527 aio_stat = REQ_STAT
528 aio_lstat = REQ_LSTAT
529 CODE:
530 {
531 aio_req req;
532
533 Newz (0, req, 1, aio_cb);
534
535 if (!req)
536 croak ("out of memory during aio_req allocation");
537
538 New (0, req->statdata, 1, Stat_t);
539
540 if (!req->statdata)
541 croak ("out of memory during aio_req->statdata allocation");
542
543 if (SvPOK (fh_or_path))
544 {
545 req->type = ix;
546 req->data = newSVsv (fh_or_path);
547 req->dataptr = SvPV_nolen (req->data);
548 }
549 else
550 {
551 req->type = REQ_FSTAT;
552 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
553 }
554
555 req->callback = SvREFCNT_inc (callback);
556
557 send_req (req);
558 }
559
560 void
561 aio_unlink(pathname,callback=&PL_sv_undef)
562 SV * pathname
563 SV * callback
564 CODE:
565 {
566 aio_req req;
567
568 Newz (0, req, 1, aio_cb);
569
570 if (!req)
571 croak ("out of memory during aio_req allocation");
572
573 req->type = REQ_UNLINK;
574 req->data = newSVsv (pathname);
575 req->dataptr = SvPV_nolen (req->data);
576 req->callback = SvREFCNT_inc (callback);
577
578 send_req (req);
579 }
580
581 void
582 flush()
583 PROTOTYPE:
584 CODE:
585 while (nreqs)
586 {
587 poll_wait ();
588 poll_cb ();
589 }
590
591 void
592 poll()
593 PROTOTYPE:
594 CODE:
595 if (nreqs)
596 {
597 poll_wait ();
598 poll_cb ();
599 }
600
601 int
602 poll_fileno()
603 PROTOTYPE:
604 CODE:
605 RETVAL = respipe [0];
606 OUTPUT:
607 RETVAL
608
609 int
610 poll_cb(...)
611 PROTOTYPE:
612 CODE:
613 RETVAL = poll_cb ();
614 OUTPUT:
615 RETVAL
616
617 void
618 poll_wait()
619 PROTOTYPE:
620 CODE:
621 if (nreqs)
622 poll_wait ();
623
624 int
625 nreqs()
626 PROTOTYPE:
627 CODE:
628 RETVAL = nreqs;
629 OUTPUT:
630 RETVAL
631