|
|
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, |
… | |
… | |
66 | |
83 | |
67 | typedef struct aio_cb |
84 | typedef struct aio_cb |
68 | { |
85 | { |
69 | struct aio_cb *volatile next; |
86 | struct aio_cb *volatile next; |
70 | |
87 | |
71 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
|
|
72 | |
|
|
73 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
74 | |
|
|
75 | SV *data, *callback; |
88 | SV *data, *callback; |
76 | SV *fh, *fh2; |
89 | SV *fh, *fh2; |
77 | void *dataptr, *data2ptr; |
90 | void *dataptr, *data2ptr; |
78 | Stat_t *statdata; |
91 | Stat_t *statdata; |
79 | off_t offset; |
92 | off_t offset; |
80 | size_t length; |
93 | size_t length; |
81 | ssize_t result; |
94 | ssize_t result; |
82 | |
95 | |
|
|
96 | STRLEN dataoffset; |
83 | int type; |
97 | int type; |
84 | int fd, fd2; |
98 | int fd, fd2; |
85 | int errorno; |
99 | int errorno; |
86 | STRLEN dataoffset; |
|
|
87 | mode_t mode; /* open */ |
100 | mode_t mode; /* open */ |
|
|
101 | |
|
|
102 | unsigned char flags; |
88 | unsigned char pri; |
103 | unsigned char pri; |
89 | unsigned char flags; |
104 | |
|
|
105 | SV *self; /* the perl counterpart of this request, if any */ |
|
|
106 | struct aio_cb *grp, *grp_prev, *grp_next, *grp_first; |
90 | } aio_cb; |
107 | } aio_cb; |
91 | |
108 | |
92 | enum { |
109 | enum { |
93 | FLAG_CANCELLED = 0x01, |
110 | FLAG_CANCELLED = 0x01, |
94 | }; |
111 | }; |
95 | |
112 | |
96 | typedef aio_cb *aio_req; |
113 | typedef aio_cb *aio_req; |
97 | typedef aio_cb *aio_req_ornot; |
114 | typedef aio_cb *aio_req_ornot; |
|
|
115 | |
|
|
116 | enum { |
|
|
117 | PRI_MIN = -4, |
|
|
118 | PRI_MAX = 4, |
|
|
119 | |
|
|
120 | DEFAULT_PRI = 0, |
|
|
121 | PRI_BIAS = -PRI_MIN, |
|
|
122 | }; |
|
|
123 | |
|
|
124 | static int next_pri = DEFAULT_PRI + PRI_BIAS; |
98 | |
125 | |
99 | static int started, wanted; |
126 | static int started, wanted; |
100 | static volatile int nreqs; |
127 | static volatile int nreqs; |
101 | static int max_outstanding = 1<<30; |
128 | static int max_outstanding = 1<<30; |
102 | static int respipe [2]; |
129 | static int respipe [2]; |
103 | |
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 | |
104 | static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; |
137 | static pthread_mutex_t reslock = AIO_MUTEX_INIT; |
105 | static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; |
138 | static pthread_mutex_t reqlock = AIO_MUTEX_INIT; |
106 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
139 | static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; |
107 | |
140 | |
108 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
141 | static volatile aio_req reqs, reqe; /* queue start, queue end */ |
109 | static volatile aio_req ress, rese; /* queue start, queue end */ |
142 | static volatile aio_req ress, rese; /* queue start, queue end */ |
110 | |
143 | |
… | |
… | |
181 | } |
214 | } |
182 | } |
215 | } |
183 | |
216 | |
184 | static void poll_wait () |
217 | static void poll_wait () |
185 | { |
218 | { |
186 | if (nreqs && !ress) |
|
|
187 | { |
|
|
188 | fd_set rfd; |
219 | fd_set rfd; |
|
|
220 | |
|
|
221 | while (nreqs) |
|
|
222 | { |
|
|
223 | aio_req req; |
|
|
224 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
|
|
225 | pthread_mutex_lock (&reslock); |
|
|
226 | #endif |
|
|
227 | req = ress; |
|
|
228 | #if !(__i386 || __x86_64) /* safe without sempahore on this archs */ |
|
|
229 | pthread_mutex_unlock (&reslock); |
|
|
230 | #endif |
|
|
231 | |
|
|
232 | if (req) |
|
|
233 | return; |
|
|
234 | |
189 | FD_ZERO(&rfd); |
235 | FD_ZERO(&rfd); |
190 | FD_SET(respipe [0], &rfd); |
236 | FD_SET(respipe [0], &rfd); |
191 | |
237 | |
192 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
238 | select (respipe [0] + 1, &rfd, 0, 0, 0); |
193 | } |
239 | } |
… | |
… | |
560 | #if !HAVE_READAHEAD |
606 | #if !HAVE_READAHEAD |
561 | # define readahead aio_readahead |
607 | # define readahead aio_readahead |
562 | |
608 | |
563 | 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) |
564 | { |
610 | { |
565 | char readahead_buf[4096]; |
611 | dBUF; |
566 | |
612 | |
567 | while (count > 0) |
613 | while (count > 0) |
568 | { |
614 | { |
569 | size_t len = count < sizeof (readahead_buf) ? count : sizeof (readahead_buf); |
615 | size_t len = count < AIO_BUFSIZE ? count : AIO_BUFSIZE; |
570 | |
616 | |
571 | pread (fd, readahead_buf, len, offset); |
617 | pread (fd, aio_buf, len, offset); |
572 | offset += len; |
618 | offset += len; |
573 | count -= len; |
619 | count -= len; |
574 | } |
620 | } |
|
|
621 | |
|
|
622 | fBUF; |
575 | |
623 | |
576 | errno = 0; |
624 | errno = 0; |
577 | } |
625 | } |
578 | #endif |
626 | #endif |
579 | |
627 | |
… | |
… | |
665 | #endif |
713 | #endif |
666 | ) |
714 | ) |
667 | ) |
715 | ) |
668 | { |
716 | { |
669 | /* emulate sendfile. this is a major pain in the ass */ |
717 | /* emulate sendfile. this is a major pain in the ass */ |
670 | char buf[4096]; |
718 | dBUF; |
|
|
719 | |
671 | res = 0; |
720 | res = 0; |
672 | |
721 | |
673 | while (count) |
722 | while (count) |
674 | { |
723 | { |
675 | ssize_t cnt; |
724 | ssize_t cnt; |
676 | |
725 | |
677 | cnt = pread (ifd, buf, count > 4096 ? 4096 : count, offset); |
726 | cnt = pread (ifd, aio_buf, count > AIO_BUFSIZE ? AIO_BUFSIZE : count, offset); |
678 | |
727 | |
679 | if (cnt <= 0) |
728 | if (cnt <= 0) |
680 | { |
729 | { |
681 | if (cnt && !res) res = -1; |
730 | if (cnt && !res) res = -1; |
682 | break; |
731 | break; |
683 | } |
732 | } |
684 | |
733 | |
685 | cnt = write (ofd, buf, cnt); |
734 | cnt = write (ofd, aio_buf, cnt); |
686 | |
735 | |
687 | if (cnt <= 0) |
736 | if (cnt <= 0) |
688 | { |
737 | { |
689 | if (cnt && !res) res = -1; |
738 | if (cnt && !res) res = -1; |
690 | break; |
739 | break; |
… | |
… | |
692 | |
741 | |
693 | offset += cnt; |
742 | offset += cnt; |
694 | res += cnt; |
743 | res += cnt; |
695 | count -= cnt; |
744 | count -= cnt; |
696 | } |
745 | } |
|
|
746 | |
|
|
747 | fBUF; |
697 | } |
748 | } |
698 | |
749 | |
699 | return res; |
750 | return res; |
700 | } |
751 | } |
701 | |
752 | |
702 | /* read a full directory */ |
753 | /* read a full directory */ |
703 | static int scandir_ (const char *path, void **namesp) |
754 | static int scandir_ (const char *path, void **namesp) |
704 | { |
755 | { |
705 | DIR *dirp = opendir (path); |
756 | DIR *dirp; |
706 | union |
757 | union |
707 | { |
758 | { |
708 | struct dirent d; |
759 | struct dirent d; |
709 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
760 | char b [offsetof (struct dirent, d_name) + NAME_MAX + 1]; |
710 | } u; |
761 | } *u; |
711 | struct dirent *entp; |
762 | struct dirent *entp; |
712 | char *name, *names; |
763 | char *name, *names; |
713 | int memlen = 4096; |
764 | int memlen = 4096; |
714 | int memofs = 0; |
765 | int memofs = 0; |
715 | int res = 0; |
766 | int res = 0; |
716 | int errorno; |
767 | int errorno; |
717 | |
768 | |
|
|
769 | dirp = opendir (path); |
718 | if (!dirp) |
770 | if (!dirp) |
719 | return -1; |
771 | return -1; |
720 | |
772 | |
|
|
773 | u = malloc (sizeof (*u)); |
721 | names = malloc (memlen); |
774 | names = malloc (memlen); |
722 | |
775 | |
|
|
776 | if (u && names) |
723 | for (;;) |
777 | for (;;) |
724 | { |
778 | { |
|
|
779 | errno = 0; |
725 | errno = 0, readdir_r (dirp, &u.d, &entp); |
780 | readdir_r (dirp, &u->d, &entp); |
726 | |
781 | |
727 | if (!entp) |
782 | if (!entp) |
728 | break; |
783 | break; |
729 | |
784 | |
730 | name = entp->d_name; |
785 | name = entp->d_name; |
731 | |
786 | |
732 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
787 | if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) |
733 | { |
788 | { |
734 | int len = strlen (name) + 1; |
789 | int len = strlen (name) + 1; |
735 | |
790 | |
736 | res++; |
791 | res++; |
737 | |
792 | |
738 | while (memofs + len > memlen) |
793 | while (memofs + len > memlen) |
739 | { |
794 | { |
740 | memlen *= 2; |
795 | memlen *= 2; |
741 | names = realloc (names, memlen); |
796 | names = realloc (names, memlen); |
742 | if (!names) |
797 | if (!names) |
743 | break; |
798 | break; |
744 | } |
799 | } |
745 | |
800 | |
746 | memcpy (names + memofs, name, len); |
801 | memcpy (names + memofs, name, len); |
747 | memofs += len; |
802 | memofs += len; |
748 | } |
803 | } |
749 | } |
804 | } |
750 | |
805 | |
751 | errorno = errno; |
806 | errorno = errno; |
|
|
807 | free (u); |
752 | closedir (dirp); |
808 | closedir (dirp); |
753 | |
809 | |
754 | if (errorno) |
810 | if (errorno) |
755 | { |
811 | { |
756 | free (names); |
812 | free (names); |
… | |
… | |
790 | } |
846 | } |
791 | |
847 | |
792 | pthread_mutex_unlock (&reqlock); |
848 | pthread_mutex_unlock (&reqlock); |
793 | |
849 | |
794 | errno = 0; /* strictly unnecessary */ |
850 | errno = 0; /* strictly unnecessary */ |
|
|
851 | type = req->type; /* remember type for QUIT check */ |
795 | |
852 | |
796 | if (!(req->flags & FLAG_CANCELLED)) |
853 | if (!(req->flags & FLAG_CANCELLED)) |
797 | switch (type = req->type) /* remember type for QUIT check */ |
854 | switch (type) |
798 | { |
855 | { |
799 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
856 | case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; |
800 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
857 | case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; |
801 | |
858 | |
802 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
859 | case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; |
… | |
… | |
921 | atfork_parent (); |
978 | atfork_parent (); |
922 | } |
979 | } |
923 | |
980 | |
924 | #define dREQ \ |
981 | #define dREQ \ |
925 | aio_req req; \ |
982 | aio_req req; \ |
|
|
983 | int req_pri = next_pri; \ |
|
|
984 | next_pri = DEFAULT_PRI + PRI_BIAS; \ |
926 | \ |
985 | \ |
927 | if (SvOK (callback) && !SvROK (callback)) \ |
986 | if (SvOK (callback) && !SvROK (callback)) \ |
928 | croak ("callback must be undef or of reference type"); \ |
987 | croak ("callback must be undef or of reference type"); \ |
929 | \ |
988 | \ |
930 | Newz (0, req, 1, aio_cb); \ |
989 | Newz (0, req, 1, aio_cb); \ |
931 | if (!req) \ |
990 | if (!req) \ |
932 | croak ("out of memory during aio_req allocation"); \ |
991 | croak ("out of memory during aio_req allocation"); \ |
933 | \ |
992 | \ |
934 | req->callback = newSVsv (callback) |
993 | req->callback = newSVsv (callback); \ |
|
|
994 | req->pri = req_pri |
935 | |
995 | |
936 | #define REQ_SEND \ |
996 | #define REQ_SEND \ |
937 | req_send (req); \ |
997 | req_send (req); \ |
938 | \ |
998 | \ |
939 | if (GIMME_V != G_VOID) \ |
999 | if (GIMME_V != G_VOID) \ |
… | |
… | |
1214 | SV * callback |
1274 | SV * callback |
1215 | PROTOTYPE: ;$ |
1275 | PROTOTYPE: ;$ |
1216 | PPCODE: |
1276 | PPCODE: |
1217 | { |
1277 | { |
1218 | dREQ; |
1278 | dREQ; |
|
|
1279 | |
1219 | req->type = REQ_GROUP; |
1280 | req->type = REQ_GROUP; |
1220 | req_send (req); |
1281 | req_send (req); |
|
|
1282 | |
1221 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1283 | XPUSHs (req_sv (req, AIO_GRP_KLASS)); |
1222 | } |
1284 | } |
1223 | |
1285 | |
1224 | void |
1286 | void |
1225 | aio_nop (callback=&PL_sv_undef) |
1287 | aio_nop (callback=&PL_sv_undef) |
… | |
… | |
1230 | |
1292 | |
1231 | req->type = REQ_NOP; |
1293 | req->type = REQ_NOP; |
1232 | |
1294 | |
1233 | REQ_SEND; |
1295 | REQ_SEND; |
1234 | } |
1296 | } |
|
|
1297 | |
|
|
1298 | #if 0 |
|
|
1299 | |
|
|
1300 | void |
|
|
1301 | aio_pri (int pri = DEFAULT_PRI) |
|
|
1302 | CODE: |
|
|
1303 | if (pri < PRI_MIN) pri = PRI_MIN; |
|
|
1304 | if (pri > PRI_MAX) pri = PRI_MAX; |
|
|
1305 | next_pri = pri + PRI_BIAS; |
|
|
1306 | |
|
|
1307 | #endif |
1235 | |
1308 | |
1236 | void |
1309 | void |
1237 | flush () |
1310 | flush () |
1238 | PROTOTYPE: |
1311 | PROTOTYPE: |
1239 | CODE: |
1312 | CODE: |