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.26 by root, Wed Aug 17 03:52:20 2005 UTC vs.
Revision 1.27 by root, Wed Aug 17 04:47:02 2005 UTC

62static int max_outstanding = 1<<30; 62static int max_outstanding = 1<<30;
63static int respipe [2]; 63static int respipe [2];
64 64
65static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER; 65static pthread_mutex_t reslock = PTHREAD_MUTEX_INITIALIZER;
66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER; 66static pthread_mutex_t reqlock = PTHREAD_MUTEX_INITIALIZER;
67static pthread_mutex_t frklock = PTHREAD_MUTEX_INITIALIZER;
68static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER; 67static pthread_cond_t reqwait = PTHREAD_COND_INITIALIZER;
69 68
70static volatile aio_req reqs, reqe; /* queue start, queue end */ 69static volatile aio_req reqs, reqe; /* queue start, queue end */
71static volatile aio_req ress, rese; /* queue start, queue end */ 70static volatile aio_req ress, rese; /* queue start, queue end */
72 71
76 SvREFCNT_dec (req->data); 75 SvREFCNT_dec (req->data);
77 76
78 if (req->fh) 77 if (req->fh)
79 SvREFCNT_dec (req->fh); 78 SvREFCNT_dec (req->fh);
80 79
81 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 80 if (req->statdata)
82 Safefree (req->statdata); 81 Safefree (req->statdata);
83 82
84 if (req->callback) 83 if (req->callback)
85 SvREFCNT_dec (req->callback); 84 SvREFCNT_dec (req->callback);
86 85
104poll_cb () 103poll_cb ()
105{ 104{
106 dSP; 105 dSP;
107 int count = 0; 106 int count = 0;
108 int do_croak = 0; 107 int do_croak = 0;
109 aio_req req, prv; 108 aio_req req;
110 109
111 pthread_mutex_lock (&reslock); 110 for (;;)
112
113 {
114 /* read any signals sent by the worker threads */
115 char buf [32];
116 while (read (respipe [0], buf, 32) == 32)
117 ;
118 }
119
120 req = ress;
121 ress = rese = 0;
122
123 pthread_mutex_unlock (&reslock);
124
125 while (req)
126 { 111 {
112 pthread_mutex_lock (&reslock);
113 req = ress;
114
115 if (req)
116 {
117 ress = req->next;
118
119 if (!ress)
120 {
121 rese = 0;
122
123 /* read any signals sent by the worker threads */
124 char buf [32];
125 while (read (respipe [0], buf, 32) == 32)
126 ;
127 }
128 }
129
130 pthread_mutex_unlock (&reslock);
131
132 if (!req)
133 break;
134
127 nreqs--; 135 nreqs--;
128 136
129 if (req->type == REQ_QUIT) 137 if (req->type == REQ_QUIT)
130 started--; 138 started--;
131 else 139 else
132 { 140 {
133 int errorno = errno; 141 int errorno = errno;
134 errno = req->errorno; 142 errno = req->errorno;
135 143
136 if (req->type == REQ_READ) 144 if (req->type == REQ_READ)
137 SvCUR_set (req->data, req->dataoffset 145 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
138 + req->result > 0 ? req->result : 0);
139 146
140 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT) 147 if (req->statdata)
141 { 148 {
142 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 149 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
143 PL_laststatval = req->result; 150 PL_laststatval = req->result;
144 PL_statcache = *(req->statdata); 151 PL_statcache = *(req->statdata);
145 } 152 }
166 if (SvOK (req->callback)) 173 if (SvOK (req->callback))
167 { 174 {
168 PUTBACK; 175 PUTBACK;
169 call_sv (req->callback, G_VOID | G_EVAL); 176 call_sv (req->callback, G_VOID | G_EVAL);
170 SPAGAIN; 177 SPAGAIN;
178
179 if (SvTRUE (ERRSV))
180 {
181 free_req (req);
182 croak (0);
183 }
171 } 184 }
172 185
173 LEAVE; 186 LEAVE;
174 187
175 do_croak = SvTRUE (ERRSV);
176
177 errno = errorno; 188 errno = errorno;
178 count++; 189 count++;
179 } 190 }
180 191
181 prv = req;
182 req = req->next;
183 free_req (prv); 192 free_req (req);
184
185 if (do_croak)
186 croak (0);
187 } 193 }
188 194
189 return count; 195 return count;
190} 196}
191 197
229 reqe = reqs = req; 235 reqe = reqs = req;
230 236
231 pthread_cond_signal (&reqwait); 237 pthread_cond_signal (&reqwait);
232 pthread_mutex_unlock (&reqlock); 238 pthread_mutex_unlock (&reqlock);
233 239
234 while (nreqs > max_outstanding) 240 if (nreqs > max_outstanding)
241 for (;;)
235 { 242 {
243 poll_cb ();
244
245 if (nreqs <= max_outstanding)
246 break;
247
236 poll_wait (); 248 poll_wait ();
237 poll_cb ();
238 } 249 }
239} 250}
240 251
241static void 252static void
242end_thread (void) 253end_thread (void)
243{ 254{
246 req->type = REQ_QUIT; 257 req->type = REQ_QUIT;
247 258
248 send_req (req); 259 send_req (req);
249} 260}
250 261
251
252static void min_parallel (int nthreads) 262static void min_parallel (int nthreads)
253{ 263{
254 while (nthreads > started) 264 while (nthreads > started)
255 start_thread (); 265 start_thread ();
256} 266}
257 267
258static void max_parallel (int nthreads) 268static void max_parallel (int nthreads)
259{ 269{
260 int cur = started; 270 int cur = started;
271
261 while (cur > nthreads) 272 while (cur > nthreads)
262 { 273 {
263 end_thread (); 274 end_thread ();
264 cur--; 275 cur--;
265 } 276 }
283 croak ("cannot set result pipe to nonblocking mode"); 294 croak ("cannot set result pipe to nonblocking mode");
284} 295}
285 296
286static void atfork_prepare (void) 297static void atfork_prepare (void)
287{ 298{
288 pthread_mutex_lock (&frklock); 299 for (;;) {
300
301 for (;;)
302 {
303 poll_cb ();
304
305 if (!nreqs)
306 break;
307
308 poll_wait ();
309 }
310
289 pthread_mutex_lock (&reqlock); 311 pthread_mutex_lock (&reqlock);
312
313 if (!nreqs)
314 break;
315
316 pthread_mutex_unlock (&reqlock);
317 }
318
290 pthread_mutex_lock (&reslock); 319 pthread_mutex_lock (&reslock);
320
321 assert (!nreqs && !reqs && !ress);
291} 322}
292 323
293static void atfork_parent (void) 324static void atfork_parent (void)
294{ 325{
295 pthread_mutex_unlock (&reslock); 326 pthread_mutex_unlock (&reslock);
296 pthread_mutex_unlock (&reqlock); 327 pthread_mutex_unlock (&reqlock);
297 pthread_mutex_unlock (&frklock);
298} 328}
299 329
300static void atfork_child (void) 330static void atfork_child (void)
301{ 331{
302 int restart = started; 332 int restart = started;
303 started = 0; 333 started = 0;
304
305 while (reqs)
306 {
307 free_req (reqs);
308 reqs = reqs->next;
309 }
310
311 reqs = reqe = 0;
312
313 while (ress)
314 {
315 free_req (ress);
316 ress = ress->next;
317 }
318
319 ress = rese = 0;
320
321 close (respipe [0]);
322 close (respipe [1]);
323
324 create_pipe ();
325 334
326 atfork_parent (); 335 atfork_parent ();
327 336
328 min_parallel (restart); 337 min_parallel (restart);
329} 338}
496 \ 505 \
497 Newz (0, req, 1, aio_cb); \ 506 Newz (0, req, 1, aio_cb); \
498 if (!req) \ 507 if (!req) \
499 croak ("out of memory during aio_req allocation"); \ 508 croak ("out of memory during aio_req allocation"); \
500 \ 509 \
501 req->callback = SvREFCNT_inc (callback); 510 req->callback = newSVsv (callback);
502 511
503MODULE = IO::AIO PACKAGE = IO::AIO 512MODULE = IO::AIO PACKAGE = IO::AIO
504 513
505PROTOTYPES: ENABLE 514PROTOTYPES: ENABLE
506 515
619 : IoOFP (sv_2io (fh))); 628 : IoOFP (sv_2io (fh)));
620 req->offset = offset; 629 req->offset = offset;
621 req->length = length; 630 req->length = length;
622 req->data = SvREFCNT_inc (data); 631 req->data = SvREFCNT_inc (data);
623 req->dataptr = (char *)svptr + dataoffset; 632 req->dataptr = (char *)svptr + dataoffset;
624 req->callback = SvREFCNT_inc (callback);
625 633
626 send_req (req); 634 send_req (req);
627 } 635 }
628} 636}
629 637
658{ 666{
659 dREQ; 667 dREQ;
660 668
661 New (0, req->statdata, 1, Stat_t); 669 New (0, req->statdata, 1, Stat_t);
662 if (!req->statdata) 670 if (!req->statdata)
671 {
672 free_req (req);
663 croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); 673 croak ("out of memory during aio_req->statdata allocation");
674 }
664 675
665 if (SvPOK (fh_or_path)) 676 if (SvPOK (fh_or_path))
666 { 677 {
667 req->type = ix; 678 req->type = ix;
668 req->data = newSVsv (fh_or_path); 679 req->data = newSVsv (fh_or_path);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines