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.28 by root, Wed Aug 17 05:06:59 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->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
148 SvREADONLY_off (req->data);
149
150 if (req->statdata)
141 { 151 {
142 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 152 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
143 PL_laststatval = req->result; 153 PL_laststatval = req->result;
144 PL_statcache = *(req->statdata); 154 PL_statcache = *(req->statdata);
145 } 155 }
166 if (SvOK (req->callback)) 176 if (SvOK (req->callback))
167 { 177 {
168 PUTBACK; 178 PUTBACK;
169 call_sv (req->callback, G_VOID | G_EVAL); 179 call_sv (req->callback, G_VOID | G_EVAL);
170 SPAGAIN; 180 SPAGAIN;
181
182 if (SvTRUE (ERRSV))
183 {
184 free_req (req);
185 croak (0);
186 }
171 } 187 }
172 188
173 LEAVE; 189 LEAVE;
174 190
175 do_croak = SvTRUE (ERRSV);
176
177 errno = errorno; 191 errno = errorno;
178 count++; 192 count++;
179 } 193 }
180 194
181 prv = req;
182 req = req->next;
183 free_req (prv); 195 free_req (req);
184
185 if (do_croak)
186 croak (0);
187 } 196 }
188 197
189 return count; 198 return count;
190} 199}
191 200
229 reqe = reqs = req; 238 reqe = reqs = req;
230 239
231 pthread_cond_signal (&reqwait); 240 pthread_cond_signal (&reqwait);
232 pthread_mutex_unlock (&reqlock); 241 pthread_mutex_unlock (&reqlock);
233 242
234 while (nreqs > max_outstanding) 243 if (nreqs > max_outstanding)
244 for (;;)
235 { 245 {
246 poll_cb ();
247
248 if (nreqs <= max_outstanding)
249 break;
250
236 poll_wait (); 251 poll_wait ();
237 poll_cb ();
238 } 252 }
239} 253}
240 254
241static void 255static void
242end_thread (void) 256end_thread (void)
243{ 257{
246 req->type = REQ_QUIT; 260 req->type = REQ_QUIT;
247 261
248 send_req (req); 262 send_req (req);
249} 263}
250 264
251
252static void min_parallel (int nthreads) 265static void min_parallel (int nthreads)
253{ 266{
254 while (nthreads > started) 267 while (nthreads > started)
255 start_thread (); 268 start_thread ();
256} 269}
257 270
258static void max_parallel (int nthreads) 271static void max_parallel (int nthreads)
259{ 272{
260 int cur = started; 273 int cur = started;
274
261 while (cur > nthreads) 275 while (cur > nthreads)
262 { 276 {
263 end_thread (); 277 end_thread ();
264 cur--; 278 cur--;
265 } 279 }
283 croak ("cannot set result pipe to nonblocking mode"); 297 croak ("cannot set result pipe to nonblocking mode");
284} 298}
285 299
286static void atfork_prepare (void) 300static void atfork_prepare (void)
287{ 301{
288 pthread_mutex_lock (&frklock);
289 pthread_mutex_lock (&reqlock); 302 pthread_mutex_lock (&reqlock);
290 pthread_mutex_lock (&reslock); 303 pthread_mutex_lock (&reslock);
291} 304}
292 305
293static void atfork_parent (void) 306static void atfork_parent (void)
294{ 307{
295 pthread_mutex_unlock (&reslock); 308 pthread_mutex_unlock (&reslock);
296 pthread_mutex_unlock (&reqlock); 309 pthread_mutex_unlock (&reqlock);
297 pthread_mutex_unlock (&frklock);
298} 310}
299 311
300static void atfork_child (void) 312static void atfork_child (void)
301{ 313{
314 aio_req prv;
315
302 int restart = started; 316 int restart = started;
303 started = 0; 317 started = 0;
304 318
305 while (reqs) 319 while (reqs)
306 { 320 {
307 free_req (reqs); 321 prv = reqs;
308 reqs = reqs->next; 322 reqs = prv->next;
323 free_req (prv);
309 } 324 }
310 325
311 reqs = reqe = 0; 326 reqs = reqe = 0;
312 327
313 while (ress) 328 while (ress)
314 { 329 {
315 free_req (ress); 330 prv = ress;
316 ress = ress->next; 331 ress = prv->next;
332 free_req (prv);
317 } 333 }
318 334
319 ress = rese = 0; 335 ress = rese = 0;
320
321 close (respipe [0]);
322 close (respipe [1]);
323
324 create_pipe ();
325 336
326 atfork_parent (); 337 atfork_parent ();
327 338
328 min_parallel (restart); 339 min_parallel (restart);
329} 340}
496 \ 507 \
497 Newz (0, req, 1, aio_cb); \ 508 Newz (0, req, 1, aio_cb); \
498 if (!req) \ 509 if (!req) \
499 croak ("out of memory during aio_req allocation"); \ 510 croak ("out of memory during aio_req allocation"); \
500 \ 511 \
501 req->callback = SvREFCNT_inc (callback); 512 req->callback = newSVsv (callback);
502 513
503MODULE = IO::AIO PACKAGE = IO::AIO 514MODULE = IO::AIO PACKAGE = IO::AIO
504 515
505PROTOTYPES: ENABLE 516PROTOTYPES: ENABLE
506 517
619 : IoOFP (sv_2io (fh))); 630 : IoOFP (sv_2io (fh)));
620 req->offset = offset; 631 req->offset = offset;
621 req->length = length; 632 req->length = length;
622 req->data = SvREFCNT_inc (data); 633 req->data = SvREFCNT_inc (data);
623 req->dataptr = (char *)svptr + dataoffset; 634 req->dataptr = (char *)svptr + dataoffset;
624 req->callback = SvREFCNT_inc (callback); 635
636 if (!SvREADONLY (data))
637 {
638 SvREADONLY_on (data);
639 req->data2ptr = (void *)data;
640 }
625 641
626 send_req (req); 642 send_req (req);
627 } 643 }
628} 644}
629 645
658{ 674{
659 dREQ; 675 dREQ;
660 676
661 New (0, req->statdata, 1, Stat_t); 677 New (0, req->statdata, 1, Stat_t);
662 if (!req->statdata) 678 if (!req->statdata)
679 {
680 free_req (req);
663 croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); 681 croak ("out of memory during aio_req->statdata allocation");
682 }
664 683
665 if (SvPOK (fh_or_path)) 684 if (SvPOK (fh_or_path))
666 { 685 {
667 req->type = ix; 686 req->type = ix;
668 req->data = newSVsv (fh_or_path); 687 req->data = newSVsv (fh_or_path);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines