ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/IO-AIO/AIO.xs
(Generate patch)

Comparing IO-AIO/AIO.xs (file contents):
Revision 1.58 by root, Mon Oct 23 18:48:08 2006 UTC vs.
Revision 1.64 by root, Mon Oct 23 23:54:41 2006 UTC

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"
66 71
67typedef struct aio_cb 72typedef struct aio_cb
68{ 73{
69 struct aio_cb *volatile next; 74 struct aio_cb *volatile next;
70 75
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; 76 SV *data, *callback;
76 SV *fh, *fh2; 77 SV *fh, *fh2;
77 void *dataptr, *data2ptr; 78 void *dataptr, *data2ptr;
78 Stat_t *statdata; 79 Stat_t *statdata;
79 off_t offset; 80 off_t offset;
80 size_t length; 81 size_t length;
81 ssize_t result; 82 ssize_t result;
82 83
84 STRLEN dataoffset;
83 int type; 85 int type;
84 int fd, fd2; 86 int fd, fd2;
85 int errorno; 87 int errorno;
86 STRLEN dataoffset;
87 mode_t mode; /* open */ 88 mode_t mode; /* open */
89
90 unsigned char flags;
88 unsigned char pri; 91 unsigned char pri;
89 unsigned char flags; 92
93 SV *self; /* the perl counterpart of this request, if any */
94 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
90} aio_cb; 95} aio_cb;
91 96
92enum { 97enum {
93 FLAG_CANCELLED = 0x01, 98 FLAG_CANCELLED = 0x01,
94}; 99};
95 100
96typedef aio_cb *aio_req; 101typedef aio_cb *aio_req;
97typedef aio_cb *aio_req_ornot; 102typedef aio_cb *aio_req_ornot;
103
104enum {
105 PRI_MIN = -4,
106 PRI_MAX = 4,
107
108 DEFAULT_PRI = 0,
109 PRI_BIAS = -PRI_MIN,
110};
111
112static int next_pri = DEFAULT_PRI + PRI_BIAS;
98 113
99static int started, wanted; 114static int started, wanted;
100static volatile int nreqs; 115static volatile int nreqs;
101static int max_outstanding = 1<<30; 116static int max_outstanding = 1<<30;
102static int respipe [2]; 117static int respipe [2];
103 118
119#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
120# define AIO_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
121#else
122# define AIO_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
123#endif
124
104static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 125static pthread_mutex_t reslock = AIO_MUTEX_INIT;
105static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 126static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
106static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 127static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
107 128
108static volatile aio_req reqs, reqe; /* queue start, queue end */ 129static volatile aio_req reqs, reqe; /* queue start, queue end */
109static volatile aio_req ress, rese; /* queue start, queue end */ 130static volatile aio_req ress, rese; /* queue start, queue end */
110 131
181 } 202 }
182} 203}
183 204
184static void poll_wait () 205static void poll_wait ()
185{ 206{
186 if (nreqs && !ress)
187 {
188 fd_set rfd; 207 fd_set rfd;
208
209 while (nreqs)
210 {
211 aio_req req;
212#if !(__x86 || __x86_64) /* safe without sempahore on this archs */
213 pthread_mutex_lock (&reslock);
214#endif
215 req = ress;
216#if !(__x86 || __x86_64) /* safe without sempahore on this archs */
217 pthread_mutex_unlock (&reslock);
218#endif
219
220 if (req)
221 return;
222
189 FD_ZERO(&rfd); 223 FD_ZERO(&rfd);
190 FD_SET(respipe [0], &rfd); 224 FD_SET(respipe [0], &rfd);
191 225
192 select (respipe [0] + 1, &rfd, 0, 0, 0); 226 select (respipe [0] + 1, &rfd, 0, 0, 0);
193 } 227 }
790 } 824 }
791 825
792 pthread_mutex_unlock (&reqlock); 826 pthread_mutex_unlock (&reqlock);
793 827
794 errno = 0; /* strictly unnecessary */ 828 errno = 0; /* strictly unnecessary */
829 type = req->type; /* remember type for QUIT check */
795 830
796 if (!(req->flags & FLAG_CANCELLED)) 831 if (!(req->flags & FLAG_CANCELLED))
797 switch (type = req->type) /* remember type for QUIT check */ 832 switch (type)
798 { 833 {
799 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 834 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; 835 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
801 836
802 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 837 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
921 atfork_parent (); 956 atfork_parent ();
922} 957}
923 958
924#define dREQ \ 959#define dREQ \
925 aio_req req; \ 960 aio_req req; \
961 int req_pri = next_pri; \
962 next_pri = DEFAULT_PRI + PRI_BIAS; \
926 \ 963 \
927 if (SvOK (callback) && !SvROK (callback)) \ 964 if (SvOK (callback) && !SvROK (callback)) \
928 croak ("callback must be undef or of reference type"); \ 965 croak ("callback must be undef or of reference type"); \
929 \ 966 \
930 Newz (0, req, 1, aio_cb); \ 967 Newz (0, req, 1, aio_cb); \
931 if (!req) \ 968 if (!req) \
932 croak ("out of memory during aio_req allocation"); \ 969 croak ("out of memory during aio_req allocation"); \
933 \ 970 \
934 req->callback = newSVsv (callback) 971 req->callback = newSVsv (callback); \
972 req->pri = req_pri
935 973
936#define REQ_SEND \ 974#define REQ_SEND \
937 req_send (req); \ 975 req_send (req); \
938 \ 976 \
939 if (GIMME_V != G_VOID) \ 977 if (GIMME_V != G_VOID) \
1214 SV * callback 1252 SV * callback
1215 PROTOTYPE: ;$ 1253 PROTOTYPE: ;$
1216 PPCODE: 1254 PPCODE:
1217{ 1255{
1218 dREQ; 1256 dREQ;
1257
1219 req->type = REQ_GROUP; 1258 req->type = REQ_GROUP;
1220 req_send (req); 1259 req_send (req);
1260
1221 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1261 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1222} 1262}
1223 1263
1224void 1264void
1225aio_nop (callback=&PL_sv_undef) 1265aio_nop (callback=&PL_sv_undef)
1230 1270
1231 req->type = REQ_NOP; 1271 req->type = REQ_NOP;
1232 1272
1233 REQ_SEND; 1273 REQ_SEND;
1234} 1274}
1275
1276#if 0
1277
1278void
1279aio_pri (int pri = DEFAULT_PRI)
1280 CODE:
1281 if (pri < PRI_MIN) pri = PRI_MIN;
1282 if (pri > PRI_MAX) pri = PRI_MAX;
1283 next_pri = pri + PRI_BIAS;
1284
1285#endif
1235 1286
1236void 1287void
1237flush () 1288flush ()
1238 PROTOTYPE: 1289 PROTOTYPE:
1239 CODE: 1290 CODE:
1293 PROTOTYPE: 1344 PROTOTYPE:
1294 CODE: 1345 CODE:
1295 req_cancel (req); 1346 req_cancel (req);
1296 1347
1297void 1348void
1298cb (aio_req req, SV *callback=&PL_sv_undef) 1349cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1299 CODE: 1350 CODE:
1300 SvREFCNT_dec (req->callback); 1351 SvREFCNT_dec (req->callback);
1301 req->callback = newSVsv (callback); 1352 req->callback = newSVsv (callback);
1302 1353
1303MODULE = IO::AIO PACKAGE = IO::AIO::GRP 1354MODULE = IO::AIO PACKAGE = IO::AIO::GRP

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines