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.53 by root, Mon Oct 23 00:17:07 2006 UTC vs.
Revision 1.63 by root, Mon Oct 23 23:48:31 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"
55 REQ_STAT, REQ_LSTAT, REQ_FSTAT, 60 REQ_STAT, REQ_LSTAT, REQ_FSTAT,
56 REQ_FSYNC, REQ_FDATASYNC, 61 REQ_FSYNC, REQ_FDATASYNC,
57 REQ_UNLINK, REQ_RMDIR, REQ_RENAME, 62 REQ_UNLINK, REQ_RMDIR, REQ_RENAME,
58 REQ_READDIR, 63 REQ_READDIR,
59 REQ_LINK, REQ_SYMLINK, 64 REQ_LINK, REQ_SYMLINK,
65 REQ_GROUP, REQ_NOP,
60 REQ_SLEEP, 66 REQ_SLEEP,
61 REQ_GROUP,
62}; 67};
63 68
64#define AIO_REQ_KLASS "IO::AIO::REQ" 69#define AIO_REQ_KLASS "IO::AIO::REQ"
65#define AIO_GRP_KLASS "IO::AIO::GRP" 70#define AIO_GRP_KLASS "IO::AIO::GRP"
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
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
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
88 unsigned char cancelled; 90 unsigned char flags;
91 unsigned char pri;
92
93 SV *self; /* the perl counterpart of this request, if any */
94 struct aio_cb *grp, *grp_prev, *grp_next, *grp_first;
89} aio_cb; 95} aio_cb;
96
97enum {
98 FLAG_CANCELLED = 0x01,
99};
90 100
91typedef aio_cb *aio_req; 101typedef aio_cb *aio_req;
92typedef 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;
93 113
94static int started, wanted; 114static int started, wanted;
95static volatile int nreqs; 115static volatile int nreqs;
96static int max_outstanding = 1<<30; 116static int max_outstanding = 1<<30;
97static int respipe [2]; 117static int respipe [2];
98 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
99static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 125static pthread_mutex_t reslock = AIO_MUTEX_INIT;
100static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 126static pthread_mutex_t reqlock = AIO_MUTEX_INIT;
101static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 127static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
102 128
103static volatile aio_req reqs, reqe; /* queue start, queue end */ 129static volatile aio_req reqs, reqe; /* queue start, queue end */
104static volatile aio_req ress, rese; /* queue start, queue end */ 130static volatile aio_req ress, rese; /* queue start, queue end */
105 131
130 return mg ? (aio_req)mg->mg_ptr : 0; 156 return mg ? (aio_req)mg->mg_ptr : 0;
131} 157}
132 158
133static void aio_grp_feed (aio_req grp) 159static void aio_grp_feed (aio_req grp)
134{ 160{
135 while (grp->length < grp->fd2) 161 while (grp->length < grp->fd2 && !(grp->flags & FLAG_CANCELLED))
136 { 162 {
137 int old_len = grp->length; 163 int old_len = grp->length;
138 164
139 if (grp->fh2 && SvOK (grp->fh2)) 165 if (grp->fh2 && SvOK (grp->fh2))
140 { 166 {
176 } 202 }
177} 203}
178 204
179static void poll_wait () 205static void poll_wait ()
180{ 206{
181 if (nreqs && !ress)
182 {
183 fd_set rfd; 207 fd_set rfd;
208
209 while (nreqs)
210 {
211 aio_req req;
212 pthread_mutex_lock (&reslock);
213 req = ress;
214 pthread_mutex_unlock (&reslock);
215
216 if (req)
217 return;
218
184 FD_ZERO(&rfd); 219 FD_ZERO(&rfd);
185 FD_SET(respipe [0], &rfd); 220 FD_SET(respipe [0], &rfd);
186 221
187 select (respipe [0] + 1, &rfd, 0, 0, 0); 222 select (respipe [0] + 1, &rfd, 0, 0, 0);
188 } 223 }
191static void req_invoke (aio_req req) 226static void req_invoke (aio_req req)
192{ 227{
193 dSP; 228 dSP;
194 int errorno = errno; 229 int errorno = errno;
195 230
196 if (req->cancelled || !SvOK (req->callback)) 231 if (req->flags & FLAG_CANCELLED || !SvOK (req->callback))
197 return; 232 return;
198 233
199 errno = req->errorno; 234 errno = req->errorno;
200 235
201 ENTER; 236 ENTER;
259 for (i = 0; i <= AvFILL (av); ++i) 294 for (i = 0; i <= AvFILL (av); ++i)
260 PUSHs (*av_fetch (av, i, 0)); 295 PUSHs (*av_fetch (av, i, 0));
261 } 296 }
262 break; 297 break;
263 298
299 case REQ_NOP:
264 case REQ_SLEEP: 300 case REQ_SLEEP:
265 break; 301 break;
266 302
267 default: 303 default:
268 PUSHs (sv_2mortal (newSViv (req->result))); 304 PUSHs (sv_2mortal (newSViv (req->result)));
320 Safefree (req); 356 Safefree (req);
321} 357}
322 358
323static void req_cancel (aio_req req) 359static void req_cancel (aio_req req)
324{ 360{
325 req->cancelled = 1; 361 req->flags |= FLAG_CANCELLED;
326 362
327 if (req->type == REQ_GROUP) 363 if (req->type == REQ_GROUP)
328 { 364 {
329 aio_req sub; 365 aio_req sub;
330 366
784 } 820 }
785 821
786 pthread_mutex_unlock (&reqlock); 822 pthread_mutex_unlock (&reqlock);
787 823
788 errno = 0; /* strictly unnecessary */ 824 errno = 0; /* strictly unnecessary */
789
790 if (!req->cancelled)
791 switch (type = req->type) /* remember type for QUIT check */ 825 type = req->type; /* remember type for QUIT check */
826
827 if (!(req->flags & FLAG_CANCELLED))
828 switch (type)
792 { 829 {
793 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break; 830 case REQ_READ: req->result = pread (req->fd, req->dataptr, req->length, req->offset); break;
794 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break; 831 case REQ_WRITE: req->result = pwrite (req->fd, req->dataptr, req->length, req->offset); break;
795 832
796 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break; 833 case REQ_READAHEAD: req->result = readahead (req->fd, req->offset, req->length); break;
820 tv.tv_usec = req->fd2; 857 tv.tv_usec = req->fd2;
821 858
822 req->result = select (0, 0, 0, 0, &tv); 859 req->result = select (0, 0, 0, 0, &tv);
823 } 860 }
824 861
862 case REQ_GROUP:
863 case REQ_NOP:
825 case REQ_QUIT: 864 case REQ_QUIT:
826 break; 865 break;
827 866
828 default: 867 default:
829 req->result = ENOSYS; 868 req->result = ENOSYS;
913 atfork_parent (); 952 atfork_parent ();
914} 953}
915 954
916#define dREQ \ 955#define dREQ \
917 aio_req req; \ 956 aio_req req; \
957 int req_pri = next_pri; \
958 next_pri = DEFAULT_PRI + PRI_BIAS; \
918 \ 959 \
919 if (SvOK (callback) && !SvROK (callback)) \ 960 if (SvOK (callback) && !SvROK (callback)) \
920 croak ("callback must be undef or of reference type"); \ 961 croak ("callback must be undef or of reference type"); \
921 \ 962 \
922 Newz (0, req, 1, aio_cb); \ 963 Newz (0, req, 1, aio_cb); \
923 if (!req) \ 964 if (!req) \
924 croak ("out of memory during aio_req allocation"); \ 965 croak ("out of memory during aio_req allocation"); \
925 \ 966 \
926 req->callback = newSVsv (callback) 967 req->callback = newSVsv (callback); \
968 req->pri = req_pri
927 969
928#define REQ_SEND \ 970#define REQ_SEND \
929 req_send (req); \ 971 req_send (req); \
930 \ 972 \
931 if (GIMME_V != G_VOID) \ 973 if (GIMME_V != G_VOID) \
1206 SV * callback 1248 SV * callback
1207 PROTOTYPE: ;$ 1249 PROTOTYPE: ;$
1208 PPCODE: 1250 PPCODE:
1209{ 1251{
1210 dREQ; 1252 dREQ;
1253
1211 req->type = REQ_GROUP; 1254 req->type = REQ_GROUP;
1212 req_send (req); 1255 req_send (req);
1256
1213 XPUSHs (req_sv (req, AIO_GRP_KLASS)); 1257 XPUSHs (req_sv (req, AIO_GRP_KLASS));
1214} 1258}
1259
1260void
1261aio_nop (callback=&PL_sv_undef)
1262 SV * callback
1263 PPCODE:
1264{
1265 dREQ;
1266
1267 req->type = REQ_NOP;
1268
1269 REQ_SEND;
1270}
1271
1272#if 0
1273
1274void
1275aio_pri (int pri = DEFAULT_PRI)
1276 CODE:
1277 if (pri < PRI_MIN) pri = PRI_MIN;
1278 if (pri > PRI_MAX) pri = PRI_MAX;
1279 next_pri = pri + PRI_BIAS;
1280
1281#endif
1215 1282
1216void 1283void
1217flush () 1284flush ()
1218 PROTOTYPE: 1285 PROTOTYPE:
1219 CODE: 1286 CODE:
1272cancel (aio_req_ornot req) 1339cancel (aio_req_ornot req)
1273 PROTOTYPE: 1340 PROTOTYPE:
1274 CODE: 1341 CODE:
1275 req_cancel (req); 1342 req_cancel (req);
1276 1343
1344void
1345cb (aio_req_ornot req, SV *callback=&PL_sv_undef)
1346 CODE:
1347 SvREFCNT_dec (req->callback);
1348 req->callback = newSVsv (callback);
1349
1277MODULE = IO::AIO PACKAGE = IO::AIO::GRP 1350MODULE = IO::AIO PACKAGE = IO::AIO::GRP
1278 1351
1279void 1352void
1280add (aio_req grp, ...) 1353add (aio_req grp, ...)
1281 PPCODE: 1354 PPCODE:
1322 SvREFCNT_dec (grp->data); 1395 SvREFCNT_dec (grp->data);
1323 grp->data = (SV *)av; 1396 grp->data = (SV *)av;
1324} 1397}
1325 1398
1326void 1399void
1327lock (aio_req grp)
1328 CODE:
1329 ++grp->length;
1330
1331void
1332unlock (aio_req grp)
1333 CODE:
1334 aio_grp_dec (grp);
1335
1336void
1337feeder_limit (aio_req grp, int limit) 1400feed_limit (aio_req grp, int limit)
1338 CODE: 1401 CODE:
1339 grp->fd2 = limit; 1402 grp->fd2 = limit;
1340 aio_grp_feed (grp); 1403 aio_grp_feed (grp);
1341 1404
1342void 1405void
1343set_feeder (aio_req grp, SV *callback=&PL_sv_undef) 1406feed (aio_req grp, SV *callback=&PL_sv_undef)
1344 CODE: 1407 CODE:
1345{ 1408{
1346 SvREFCNT_dec (grp->fh2); 1409 SvREFCNT_dec (grp->fh2);
1347 grp->fh2 = newSVsv (callback); 1410 grp->fh2 = newSVsv (callback);
1348 1411

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines