ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
Revision: 1.16
Committed: Sun Jul 31 18:20:07 2005 UTC (18 years, 9 months ago) by root
Branch: MAIN
Changes since 1.15: +13 -5 lines
Log Message:
*** empty log message ***

File Contents

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