|
|
1 | #if __linux |
|
|
2 | # define _GNU_SOURCE |
|
|
3 | #endif |
|
|
4 | |
1 | #define _REENTRANT 1 |
5 | #define _REENTRANT 1 |
|
|
6 | |
2 | #include <errno.h> |
7 | #include <errno.h> |
3 | |
8 | |
4 | #include "EXTERN.h" |
9 | #include "EXTERN.h" |
5 | #include "perl.h" |
10 | #include "perl.h" |
6 | #include "XSUB.h" |
11 | #include "XSUB.h" |
… | |
… | |
41 | # define NAME_MAX 4096 |
46 | # define NAME_MAX 4096 |
42 | #endif |
47 | #endif |
43 | |
48 | |
44 | #if __ia64 |
49 | #if __ia64 |
45 | # define STACKSIZE 65536 |
50 | # define STACKSIZE 65536 |
|
|
51 | #elif __i386 || __x86_64 /* 16k is unreasonably high :( */ |
|
|
52 | # define STACKSIZE PTHREAD_STACK_MIN |
46 | #else |
53 | #else |
47 | # define STACKSIZE 8192 |
54 | # define STACKSIZE 16384 |
48 | #endif |
55 | #endif |
|
|
56 | |
|
|
57 | /* buffer size for various temporary buffers */ |
|
|
58 | #define AIO_BUFSIZE 65536 |
|
|
59 | |
|
|
60 | #define dBUF \ |
|
|
61 | char *aio_buf = malloc (AIO_BUFSIZE); \ |
|
|
62 | if (!aio_buf) \ |
|
|
63 | return -1; |
|
|
64 | |
|
|
65 | #define fBUF free (aio_buf) |
49 | |
66 | |
50 | enum { |
67 | enum { |
51 | REQ_QUIT, |
68 | REQ_QUIT, |
52 | REQ_OPEN, REQ_CLOSE, |
69 | REQ_OPEN, REQ_CLOSE, |
53 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
70 | REQ_READ, REQ_WRITE, REQ_READAHEAD, |
… | |
… | |
109 | static int started, wanted; |
126 | static int started, wanted; |
110 | static volatile int nreqs; |
127 | static volatile int nreqs; |
111 | static int max_outstanding = 1<<30; |
128 | static int max_outstanding = 1<<30; |
112 | static int respipe [2]; |
129 | static int respipe [2]; |
113 | |
130 | |
|
|
131 | #if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) |
|
|
132 | # define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
|
|
133 | #else |
|
|
134 | # define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER |
|
|
135 | #endif |
|
|
136 | |
114 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
137 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
115 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
138 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
116 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
139 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
117 | |
140 | |
118 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
141 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
119 | static volatile aio_req ress, rese; /* queue start, queue end */ |
142 | static volatile aio_req ress, rese; /* queue start, queue end */ |
120 | |
143 | |
… | |
… | |
191 | } |
214 | } |
192 | } |
215 | } |
193 | |
216 | |
194 | static void poll_wait () |
217 | static void poll_wait () |
195 | { |
218 | { |
|
|
219 | fd_set rfd; |
|
|
220 | |
196 | while (nreqs) |
221 | while (nreqs) |
197 | { |
222 | { |
198 | aio_req req; |
223 | aio_req req; |
|
|
224 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
199 | pthread_mutex_lock (&reslock); |
225 | pthread_mutex_lock (&reslock); |
|
|
226 | #endif |
200 | req = ress; |
227 | req = ress; |
|
|
228 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
201 | pthread_mutex_unlock (&reslock); |
229 | pthread_mutex_unlock (&reslock); |
|
|
230 | #endif |
202 | |
231 | |
203 | if (req) |
232 | if (req) |
204 | return; |
233 | return; |
205 | |
234 | |
206 | fd_set rfd; |
|
|
207 | FD_ZERO(&rfd); |
235 | FD_ZERO(&rfd); |
208 | FD_SET(respipe [0], &rfd); |
236 | FD_SET(respipe [0], &rfd); |
209 | |
237 | |
210 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
238 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
211 | } |
239 | } |
… | |
… | |
578 | #if !HAVE_READAHEAD |
606 | #if !HAVE_READAHEAD |
579 | # define readahead aio_readahead |
607 | # define readahead aio_readahead |
580 | |
608 | |
581 | static ssize_t readahead (int fd, off_t offset, size_t count) |
609 | static ssize_t readahead (int fd, off_t offset, size_t count) |
582 | { |
610 | { |
583 | char readahead_buf[4096]; |
611 | dBUF; |
584 | |
612 | |
585 | while (count > 0) |
613 | while (count > 0) |
586 | { |
614 | { |
587 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
615 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
588 | |
616 | |
589 | pread (fd, readahead_buf, len, offset); |
617 | pread (fd, aio_buf, len, offset); |
590 | offset += len; |
618 | offset += len; |
591 | count -= len; |
619 | count -= len; |
592 | } |
620 | } |
|
|
621 | |
|
|
622 | fBUF; |
593 | |
623 | |
594 | errno = 0; |
624 | errno = 0; |
595 | } |
625 | } |
596 | #endif |
626 | #endif |
597 | |
627 | |
… | |
… | |
683 | #endif |
713 | #endif |
684 | ) |
714 | ) |
685 | ) |
715 | ) |
686 | { |
716 | { |
687 | /* emulate sendfile. this is a major pain in the ass */ |
717 | /* emulate sendfile. this is a major pain in the ass */ |
688 | char buf[4096]; |
718 | dBUF; |
|
|
719 | |
689 | res = 0; |
720 | res = 0; |
690 | |
721 | |
691 | while (count) |
722 | while (count) |
692 | { |
723 | { |
693 | ssize_t cnt; |
724 | ssize_t cnt; |
694 | |
725 | |
695 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
726 | cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); |
696 | |
727 | |
697 | if (cnt <= 0) |
728 | if (cnt <= 0) |
698 | { |
729 | { |
699 | if (cnt && !res) res = -1; |
730 | if (cnt && !res) res = -1; |
700 | break; |
731 | break; |
701 | } |
732 | } |
702 | |
733 | |
703 | cnt = write (ofd, buf, cnt); |
734 | cnt = write (ofd, aio_buf, cnt); |
704 | |
735 | |
705 | if (cnt <= 0) |
736 | if (cnt <= 0) |
706 | { |
737 | { |
707 | if (cnt && !res) res = -1; |
738 | if (cnt && !res) res = -1; |
708 | break; |
739 | break; |
… | |
… | |
710 | |
741 | |
711 | offset += cnt; |
742 | offset += cnt; |
712 | res += cnt; |
743 | res += cnt; |
713 | count -= cnt; |
744 | count -= cnt; |
714 | } |
745 | } |
|
|
746 | |
|
|
747 | fBUF; |
715 | } |
748 | } |
716 | |
749 | |
717 | return res; |
750 | return res; |
718 | } |
751 | } |
719 | |
752 | |
720 | /* read a full directory */ |
753 | /* read a full directory */ |
721 | static int scandir_ (const char *path, void **namesp) |
754 | static int scandir_ (const char *path, void **namesp) |
722 | { |
755 | { |
723 | DIR *dirp = opendir (path); |
756 | DIR *dirp; |
724 | union |
757 | union |
725 | { |
758 | { |
726 | struct dirent d; |
759 | struct dirent d; |
727 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
760 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
728 | } u; |
761 | } *u; |
729 | struct dirent *entp; |
762 | struct dirent *entp; |
730 | char *name, *names; |
763 | char *name, *names; |
731 | int memlen = 4096; |
764 | int memlen = 4096; |
732 | int memofs = 0; |
765 | int memofs = 0; |
733 | int res = 0; |
766 | int res = 0; |
734 | int errorno; |
767 | int errorno; |
735 | |
768 | |
|
|
769 | dirp = opendir (path); |
736 | if (!dirp) |
770 | if (!dirp) |
737 | return -1; |
771 | return -1; |
738 | |
772 | |
|
|
773 | u = malloc (sizeof (*u)); |
739 | names = malloc (memlen); |
774 | names = malloc (memlen); |
740 | |
775 | |
|
|
776 | if (u && names) |
741 | for (;;) |
777 | for (;;) |
742 | { |
778 | { |
|
|
779 | errno = 0; |
743 | errno = 0, readdir_r (dirp, &u.d, &entp); |
780 | readdir_r (dirp, &u->d, &entp); |
744 | |
781 | |
745 | if (!entp) |
782 | if (!entp) |
746 | break; |
783 | break; |
747 | |
784 | |
748 | name = entp->d_name; |
785 | name = entp->d_name; |
749 | |
786 | |
750 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
787 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
751 | { |
788 | { |
752 | int len = strlen (name) + 1; |
789 | int len = strlen (name) + 1; |
753 | |
790 | |
754 | res++; |
791 | res++; |
755 | |
792 | |
756 | while (memofs + len > memlen) |
793 | while (memofs + len > memlen) |
757 | { |
794 | { |
758 | memlen *= 2; |
795 | memlen *= 2; |
759 | names = realloc (names, memlen); |
796 | names = realloc (names, memlen); |
760 | if (!names) |
797 | if (!names) |
761 | break; |
798 | break; |
762 | } |
799 | } |
763 | |
800 | |
764 | memcpy (names + memofs, name, len); |
801 | memcpy (names + memofs, name, len); |
765 | memofs += len; |
802 | memofs += len; |
766 | } |
803 | } |
767 | } |
804 | } |
768 | |
805 | |
769 | errorno = errno; |
806 | errorno = errno; |
|
|
807 | free (u); |
770 | closedir (dirp); |
808 | closedir (dirp); |
771 | |
809 | |
772 | if (errorno) |
810 | if (errorno) |
773 | { |
811 | { |
774 | free (names); |
812 | free (names); |
… | |
… | |
1323 | |
1361 | |
1324 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1362 | MODULE = IO::AIO PACKAGE = IO::AIO::REQ |
1325 | |
1363 | |
1326 | void |
1364 | void |
1327 | cancel (aio_req_ornot req) |
1365 | cancel (aio_req_ornot req) |
1328 | PROTOTYPE: |
|
|
1329 | CODE: |
1366 | CODE: |
1330 | req_cancel (req); |
1367 | req_cancel (req); |
1331 | |
1368 | |
1332 | void |
1369 | void |
1333 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |
1370 | cb (aio_req_ornot req, SV *callback=&PL_sv_undef) |