ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.28
Committed: Wed Aug 17 05:06:59 2005 UTC (18 years, 9 months ago) by root
Branch: MAIN
CVS Tags: rel-1_3
Changes since 1.27: +30 -22 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.20 #define _REENTRANT 1
2 root 1.19 #include <errno.h>
3    
4 root 1.15 #include "EXTERN.h"
5 root 1.1 #include "perl.h"
6     #include "XSUB.h"
7    
8 root 1.16 #include "autoconf/config.h"
9    
10 root 1.1 #include <sys/types.h>
11     #include <sys/stat.h>
12 root 1.10
13 root 1.1 #include <unistd.h>
14     #include <fcntl.h>
15     #include <signal.h>
16     #include <sched.h>
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 root 1.4 #if __ia64
25     # define STACKSIZE 65536
26 root 1.1 #else
27 root 1.4 # define STACKSIZE 4096
28 root 1.1 #endif
29    
30     enum {
31     REQ_QUIT,
32     REQ_OPEN, REQ_CLOSE,
33     REQ_READ, REQ_WRITE, REQ_READAHEAD,
34 root 1.22 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
35 root 1.1 REQ_FSYNC, REQ_FDATASYNC,
36 root 1.23 REQ_UNLINK, REQ_RMDIR,
37 root 1.22 REQ_SYMLINK,
38 root 1.1 };
39    
40     typedef struct aio_cb {
41 root 1.3 struct aio_cb *volatile next;
42 root 1.1
43     int type;
44    
45     int fd;
46     off_t offset;
47     size_t length;
48     ssize_t result;
49     mode_t mode; /* open */
50     int errorno;
51 root 1.13 SV *data, *callback, *fh;
52 root 1.22 void *dataptr, *data2ptr;
53 root 1.1 STRLEN dataoffset;
54    
55     Stat_t *statdata;
56     } aio_cb;
57    
58     typedef aio_cb *aio_req;
59    
60     static int started;
61 root 1.11 static volatile int nreqs;
62 root 1.4 static int max_outstanding = 1<<30;
63 root 1.3 static int respipe [2];
64 root 1.1
65 root 1.3 static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
66     static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
67     static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
68    
69     static volatile aio_req reqs, reqe; /* queue start, queue end */
70     static volatile aio_req ress, rese; /* queue start, queue end */
71 root 1.1
72 root 1.26 static void free_req (aio_req req)
73     {
74     if (req->data)
75     SvREFCNT_dec (req->data);
76    
77     if (req->fh)
78     SvREFCNT_dec (req->fh);
79    
80 root 1.27 if (req->statdata)
81 root 1.26 Safefree (req->statdata);
82    
83     if (req->callback)
84     SvREFCNT_dec (req->callback);
85    
86     Safefree (req);
87     }
88    
89 root 1.1 static void
90     poll_wait ()
91     {
92 root 1.11 if (nreqs && !ress)
93     {
94     fd_set rfd;
95     FD_ZERO(&rfd);
96     FD_SET(respipe [0], &rfd);
97 root 1.3
98 root 1.11 select (respipe [0] + 1, &rfd, 0, 0, 0);
99     }
100 root 1.1 }
101    
102     static int
103 root 1.5 poll_cb ()
104 root 1.1 {
105     dSP;
106     int count = 0;
107 root 1.24 int do_croak = 0;
108 root 1.27 aio_req req;
109    
110     for (;;)
111     {
112     pthread_mutex_lock (&reslock);
113     req = ress;
114    
115     if (req)
116     {
117     ress = req->next;
118 root 1.11
119 root 1.27 if (!ress)
120     {
121     rese = 0;
122 root 1.11
123 root 1.27 /* read any signals sent by the worker threads */
124     char buf [32];
125     while (read (respipe [0], buf, 32) == 32)
126     ;
127     }
128     }
129 root 1.1
130 root 1.27 pthread_mutex_unlock (&reslock);
131 root 1.3
132 root 1.27 if (!req)
133     break;
134 root 1.3
135 root 1.1 nreqs--;
136    
137     if (req->type == REQ_QUIT)
138     started--;
139     else
140     {
141     int errorno = errno;
142     errno = req->errorno;
143    
144     if (req->type == REQ_READ)
145 root 1.27 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
146 root 1.1
147 root 1.28 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
148     SvREADONLY_off (req->data);
149    
150 root 1.27 if (req->statdata)
151 root 1.1 {
152     PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
153     PL_laststatval = req->result;
154     PL_statcache = *(req->statdata);
155     }
156    
157 root 1.13 ENTER;
158 root 1.1 PUSHMARK (SP);
159     XPUSHs (sv_2mortal (newSViv (req->result)));
160 root 1.2
161     if (req->type == REQ_OPEN)
162     {
163     /* convert fd to fh */
164     SV *fh;
165    
166     PUTBACK;
167     call_pv ("IO::AIO::_fd2fh", G_SCALAR | G_EVAL);
168     SPAGAIN;
169    
170 root 1.13 fh = SvREFCNT_inc (POPs);
171 root 1.2
172     PUSHMARK (SP);
173 root 1.13 XPUSHs (sv_2mortal (fh));
174 root 1.2 }
175    
176 root 1.8 if (SvOK (req->callback))
177     {
178     PUTBACK;
179     call_sv (req->callback, G_VOID | G_EVAL);
180     SPAGAIN;
181 root 1.27
182     if (SvTRUE (ERRSV))
183     {
184     free_req (req);
185     croak (0);
186     }
187 root 1.8 }
188 root 1.13
189 root 1.26 LEAVE;
190    
191 root 1.1 errno = errorno;
192     count++;
193     }
194    
195 root 1.27 free_req (req);
196 root 1.1 }
197    
198     return count;
199     }
200    
201 root 1.4 static void *aio_proc(void *arg);
202    
203     static void
204     start_thread (void)
205     {
206     sigset_t fullsigset, oldsigset;
207     pthread_t tid;
208     pthread_attr_t attr;
209    
210     pthread_attr_init (&attr);
211     pthread_attr_setstacksize (&attr, STACKSIZE);
212     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
213    
214     sigfillset (&fullsigset);
215     sigprocmask (SIG_SETMASK, &fullsigset, &oldsigset);
216    
217     if (pthread_create (&tid, &attr, aio_proc, 0) == 0)
218     started++;
219    
220     sigprocmask (SIG_SETMASK, &oldsigset, 0);
221     }
222    
223     static void
224     send_req (aio_req req)
225     {
226     nreqs++;
227    
228     pthread_mutex_lock (&reqlock);
229    
230     req->next = 0;
231    
232     if (reqe)
233     {
234     reqe->next = req;
235     reqe = req;
236     }
237     else
238     reqe = reqs = req;
239    
240     pthread_cond_signal (&reqwait);
241     pthread_mutex_unlock (&reqlock);
242    
243 root 1.27 if (nreqs > max_outstanding)
244     for (;;)
245     {
246     poll_cb ();
247    
248     if (nreqs <= max_outstanding)
249     break;
250    
251     poll_wait ();
252     }
253 root 1.4 }
254    
255     static void
256     end_thread (void)
257     {
258     aio_req req;
259 root 1.26 Newz (0, req, 1, aio_cb);
260 root 1.4 req->type = REQ_QUIT;
261    
262     send_req (req);
263     }
264    
265 root 1.22 static void min_parallel (int nthreads)
266     {
267     while (nthreads > started)
268     start_thread ();
269     }
270    
271     static void max_parallel (int nthreads)
272     {
273     int cur = started;
274 root 1.27
275 root 1.22 while (cur > nthreads)
276     {
277     end_thread ();
278     cur--;
279     }
280    
281     while (started > nthreads)
282     {
283     poll_wait ();
284     poll_cb ();
285     }
286     }
287    
288 root 1.26 static void create_pipe ()
289 root 1.22 {
290 root 1.26 if (pipe (respipe))
291     croak ("unable to initialize result pipe");
292 root 1.22
293 root 1.26 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
294     croak ("cannot set result pipe to nonblocking mode");
295 root 1.22
296 root 1.26 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
297     croak ("cannot set result pipe to nonblocking mode");
298     }
299 root 1.22
300 root 1.26 static void atfork_prepare (void)
301     {
302 root 1.28 pthread_mutex_lock (&reqlock);
303 root 1.22 pthread_mutex_lock (&reslock);
304     }
305    
306     static void atfork_parent (void)
307     {
308     pthread_mutex_unlock (&reslock);
309 root 1.26 pthread_mutex_unlock (&reqlock);
310     }
311    
312     static void atfork_child (void)
313     {
314 root 1.28 aio_req prv;
315    
316 root 1.26 int restart = started;
317     started = 0;
318    
319 root 1.28 while (reqs)
320     {
321     prv = reqs;
322     reqs = prv->next;
323     free_req (prv);
324     }
325    
326     reqs = reqe = 0;
327    
328     while (ress)
329     {
330     prv = ress;
331     ress = prv->next;
332     free_req (prv);
333     }
334    
335     ress = rese = 0;
336    
337 root 1.26 atfork_parent ();
338 root 1.25
339 root 1.26 min_parallel (restart);
340 root 1.22 }
341    
342     /*****************************************************************************/
343 root 1.17 /* work around various missing functions */
344    
345     #if !HAVE_PREADWRITE
346     # define pread aio_pread
347     # define pwrite aio_pwrite
348    
349     /*
350     * make our pread/pwrite safe against themselves, but not against
351     * normal read/write by using a mutex. slows down execution a lot,
352     * but that's your problem, not mine.
353     */
354     static pthread_mutex_t iolock = PTHREAD_MUTEX_INITIALIZER;
355    
356     static ssize_t
357     pread (int fd, void *buf, size_t count, off_t offset)
358     {
359     ssize_t res;
360     off_t ooffset;
361    
362     pthread_mutex_lock (&iolock);
363     ooffset = lseek (fd, 0, SEEK_CUR);
364     lseek (fd, offset, SEEK_SET);
365     res = read (fd, buf, count);
366     lseek (fd, ooffset, SEEK_SET);
367     pthread_mutex_unlock (&iolock);
368    
369     return res;
370     }
371    
372     static ssize_t
373     pwrite (int fd, void *buf, size_t count, off_t offset)
374     {
375     ssize_t res;
376     off_t ooffset;
377    
378     pthread_mutex_lock (&iolock);
379     ooffset = lseek (fd, 0, SEEK_CUR);
380     lseek (fd, offset, SEEK_SET);
381     res = write (fd, buf, count);
382     lseek (fd, offset, SEEK_SET);
383     pthread_mutex_unlock (&iolock);
384    
385     return res;
386     }
387     #endif
388    
389     #if !HAVE_FDATASYNC
390     # define fdatasync fsync
391     #endif
392    
393     #if !HAVE_READAHEAD
394     # define readahead aio_readahead
395    
396     static char readahead_buf[4096];
397    
398     static ssize_t
399     readahead (int fd, off_t offset, size_t count)
400     {
401     while (count > 0)
402     {
403     size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf);
404    
405     pread (fd, readahead_buf, len, offset);
406     offset += len;
407     count -= len;
408     }
409    
410     errno = 0;
411     }
412     #endif
413    
414 root 1.22 /*****************************************************************************/
415    
416 root 1.1 static void *
417     aio_proc (void *thr_arg)
418     {
419     aio_req req;
420 root 1.3 int type;
421 root 1.1
422 root 1.3 do
423 root 1.1 {
424 root 1.3 pthread_mutex_lock (&reqlock);
425    
426     for (;;)
427     {
428     req = reqs;
429    
430     if (reqs)
431     {
432     reqs = reqs->next;
433     if (!reqs) reqe = 0;
434     }
435    
436     if (req)
437     break;
438    
439     pthread_cond_wait (&reqwait, &reqlock);
440     }
441    
442     pthread_mutex_unlock (&reqlock);
443    
444 root 1.1 errno = 0; /* strictly unnecessary */
445    
446 root 1.3 type = req->type;
447    
448     switch (type)
449 root 1.1 {
450 root 1.10 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
451     case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
452 root 1.16
453 root 1.1 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
454    
455     case REQ_STAT: req->result = stat (req->dataptr, req->statdata); break;
456     case REQ_LSTAT: req->result = lstat (req->dataptr, req->statdata); break;
457     case REQ_FSTAT: req->result = fstat (req->fd , req->statdata); break;
458    
459     case REQ_OPEN: req->result = open (req->dataptr, req->fd, req->mode); break;
460     case REQ_CLOSE: req->result = close (req->fd); break;
461     case REQ_UNLINK: req->result = unlink (req->dataptr); break;
462 root 1.22 case REQ_RMDIR: req->result = rmdir (req->dataptr); break;
463     case REQ_SYMLINK: req->result = symlink (req->data2ptr, req->dataptr); break;
464 root 1.1
465 root 1.17 case REQ_FDATASYNC: req->result = fdatasync (req->fd); break;
466 root 1.1 case REQ_FSYNC: req->result = fsync (req->fd); break;
467    
468     case REQ_QUIT:
469 root 1.3 break;
470 root 1.1
471     default:
472     req->result = ENOSYS;
473     break;
474     }
475    
476     req->errorno = errno;
477 root 1.3
478     pthread_mutex_lock (&reslock);
479    
480     req->next = 0;
481    
482     if (rese)
483     {
484     rese->next = req;
485     rese = req;
486     }
487     else
488     {
489     rese = ress = req;
490    
491     /* write a dummy byte to the pipe so fh becomes ready */
492     write (respipe [1], &respipe, 1);
493     }
494    
495     pthread_mutex_unlock (&reslock);
496 root 1.1 }
497 root 1.3 while (type != REQ_QUIT);
498 root 1.1
499     return 0;
500     }
501    
502 root 1.22 #define dREQ \
503     aio_req req; \
504     \
505     if (SvOK (callback) && !SvROK (callback)) \
506     croak ("clalback must be undef or of reference type"); \
507     \
508     Newz (0, req, 1, aio_cb); \
509     if (!req) \
510     croak ("out of memory during aio_req allocation"); \
511     \
512 root 1.27 req->callback = newSVsv (callback);
513 root 1.22
514 root 1.1 MODULE = IO::AIO PACKAGE = IO::AIO
515    
516 root 1.8 PROTOTYPES: ENABLE
517    
518 root 1.1 BOOT:
519     {
520 root 1.26 create_pipe ();
521 root 1.22 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
522 root 1.1 }
523    
524     void
525     min_parallel(nthreads)
526     int nthreads
527     PROTOTYPE: $
528    
529     void
530     max_parallel(nthreads)
531     int nthreads
532     PROTOTYPE: $
533    
534 root 1.4 int
535     max_outstanding(nreqs)
536     int nreqs
537     PROTOTYPE: $
538     CODE:
539     RETVAL = max_outstanding;
540     max_outstanding = nreqs;
541    
542 root 1.1 void
543 root 1.8 aio_open(pathname,flags,mode,callback=&PL_sv_undef)
544 root 1.1 SV * pathname
545     int flags
546     int mode
547     SV * callback
548 root 1.8 PROTOTYPE: $$$;$
549 root 1.1 CODE:
550     {
551 root 1.22 dREQ;
552 root 1.1
553     req->type = REQ_OPEN;
554     req->data = newSVsv (pathname);
555 root 1.22 req->dataptr = SvPVbyte_nolen (req->data);
556 root 1.1 req->fd = flags;
557     req->mode = mode;
558    
559     send_req (req);
560     }
561    
562     void
563 root 1.8 aio_close(fh,callback=&PL_sv_undef)
564 root 1.13 SV * fh
565     SV * callback
566 root 1.8 PROTOTYPE: $;$
567 root 1.1 ALIAS:
568     aio_close = REQ_CLOSE
569     aio_fsync = REQ_FSYNC
570     aio_fdatasync = REQ_FDATASYNC
571     CODE:
572     {
573 root 1.22 dREQ;
574 root 1.1
575     req->type = ix;
576 root 1.13 req->fh = newSVsv (fh);
577     req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
578 root 1.1
579     send_req (req);
580     }
581    
582     void
583 root 1.8 aio_read(fh,offset,length,data,dataoffset,callback=&PL_sv_undef)
584 root 1.13 SV * fh
585     UV offset
586     IV length
587     SV * data
588     IV dataoffset
589     SV * callback
590     ALIAS:
591     aio_read = REQ_READ
592     aio_write = REQ_WRITE
593 root 1.8 PROTOTYPE: $$$$$;$
594 root 1.1 CODE:
595 root 1.13 {
596     aio_req req;
597     STRLEN svlen;
598 root 1.21 char *svptr = SvPVbyte (data, svlen);
599 root 1.13
600     SvUPGRADE (data, SVt_PV);
601     SvPOK_on (data);
602 root 1.1
603 root 1.13 if (dataoffset < 0)
604     dataoffset += svlen;
605    
606     if (dataoffset < 0 || dataoffset > svlen)
607     croak ("data offset outside of string");
608    
609     if (ix == REQ_WRITE)
610     {
611     /* write: check length and adjust. */
612     if (length < 0 || length + dataoffset > svlen)
613     length = svlen - dataoffset;
614     }
615     else
616     {
617     /* read: grow scalar as necessary */
618     svptr = SvGROW (data, length + dataoffset);
619     }
620    
621     if (length < 0)
622     croak ("length must not be negative");
623    
624 root 1.22 {
625     dREQ;
626 root 1.13
627 root 1.22 req->type = ix;
628     req->fh = newSVsv (fh);
629     req->fd = PerlIO_fileno (ix == REQ_READ ? IoIFP (sv_2io (fh))
630     : IoOFP (sv_2io (fh)));
631     req->offset = offset;
632     req->length = length;
633     req->data = SvREFCNT_inc (data);
634     req->dataptr = (char *)svptr + dataoffset;
635 root 1.13
636 root 1.28 if (!SvREADONLY (data))
637     {
638     SvREADONLY_on (data);
639     req->data2ptr = (void *)data;
640     }
641    
642 root 1.22 send_req (req);
643     }
644 root 1.13 }
645 root 1.1
646     void
647 root 1.8 aio_readahead(fh,offset,length,callback=&PL_sv_undef)
648 root 1.13 SV * fh
649     UV offset
650     IV length
651     SV * callback
652 root 1.8 PROTOTYPE: $$$;$
653 root 1.1 CODE:
654     {
655 root 1.22 dREQ;
656 root 1.1
657     req->type = REQ_READAHEAD;
658 root 1.13 req->fh = newSVsv (fh);
659     req->fd = PerlIO_fileno (IoIFP (sv_2io (fh)));
660 root 1.1 req->offset = offset;
661     req->length = length;
662    
663     send_req (req);
664     }
665    
666     void
667 root 1.8 aio_stat(fh_or_path,callback=&PL_sv_undef)
668 root 1.1 SV * fh_or_path
669     SV * callback
670     ALIAS:
671 root 1.8 aio_stat = REQ_STAT
672     aio_lstat = REQ_LSTAT
673 root 1.1 CODE:
674     {
675 root 1.22 dREQ;
676 root 1.1
677     New (0, req->statdata, 1, Stat_t);
678     if (!req->statdata)
679 root 1.27 {
680     free_req (req);
681     croak ("out of memory during aio_req->statdata allocation");
682     }
683 root 1.1
684     if (SvPOK (fh_or_path))
685     {
686 root 1.8 req->type = ix;
687 root 1.1 req->data = newSVsv (fh_or_path);
688 root 1.22 req->dataptr = SvPVbyte_nolen (req->data);
689 root 1.1 }
690     else
691     {
692     req->type = REQ_FSTAT;
693 root 1.13 req->fh = newSVsv (fh_or_path);
694 root 1.1 req->fd = PerlIO_fileno (IoIFP (sv_2io (fh_or_path)));
695     }
696    
697     send_req (req);
698     }
699    
700     void
701 root 1.8 aio_unlink(pathname,callback=&PL_sv_undef)
702 root 1.1 SV * pathname
703     SV * callback
704 root 1.22 ALIAS:
705     aio_unlink = REQ_UNLINK
706     aio_rmdir = REQ_RMDIR
707 root 1.1 CODE:
708     {
709 root 1.22 dREQ;
710 root 1.1
711 root 1.22 req->type = ix;
712     req->data = newSVsv (pathname);
713     req->dataptr = SvPVbyte_nolen (req->data);
714 root 1.1
715 root 1.22 send_req (req);
716     }
717    
718     void
719     aio_symlink(oldpath,newpath,callback=&PL_sv_undef)
720     SV * oldpath
721     SV * newpath
722     SV * callback
723     CODE:
724     {
725     dREQ;
726 root 1.1
727 root 1.22 req->type = REQ_SYMLINK;
728     req->fh = newSVsv (oldpath);
729     req->data2ptr = SvPVbyte_nolen (req->fh);
730     req->data = newSVsv (newpath);
731     req->dataptr = SvPVbyte_nolen (req->data);
732 root 1.1
733     send_req (req);
734     }
735    
736 root 1.6 void
737     flush()
738     PROTOTYPE:
739     CODE:
740     while (nreqs)
741     {
742     poll_wait ();
743     poll_cb ();
744     }
745    
746 root 1.7 void
747     poll()
748     PROTOTYPE:
749     CODE:
750     if (nreqs)
751     {
752     poll_wait ();
753     poll_cb ();
754     }
755    
756 root 1.1 int
757     poll_fileno()
758     PROTOTYPE:
759     CODE:
760 root 1.3 RETVAL = respipe [0];
761 root 1.1 OUTPUT:
762     RETVAL
763    
764     int
765     poll_cb(...)
766     PROTOTYPE:
767     CODE:
768 root 1.5 RETVAL = poll_cb ();
769 root 1.1 OUTPUT:
770     RETVAL
771    
772     void
773     poll_wait()
774     PROTOTYPE:
775     CODE:
776 root 1.3 if (nreqs)
777     poll_wait ();
778 root 1.1
779     int
780     nreqs()
781     PROTOTYPE:
782     CODE:
783     RETVAL = nreqs;
784     OUTPUT:
785     RETVAL
786