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.24 by root, Wed Aug 17 03:01:56 2005 UTC vs.
Revision 1.31 by root, Thu Aug 18 16:34:53 2005 UTC

55 Stat_t *statdata; 55 Stat_t *statdata;
56} aio_cb; 56} aio_cb;
57 57
58typedef aio_cb *aio_req; 58typedef aio_cb *aio_req;
59 59
60static int started; 60static int started, wanted;
61static volatile int nreqs; 61static volatile int nreqs;
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 */
71
72static void free_req (aio_req req)
73{
74 if (req->data)
75 SvREFCNT_dec (req->data);
76
77 if (req->fh)
78 SvREFCNT_dec (req->fh);
79
80 if (req->statdata)
81 Safefree (req->statdata);
82
83 if (req->callback)
84 SvREFCNT_dec (req->callback);
85
86 Safefree (req);
87}
72 88
73static void 89static void
74poll_wait () 90poll_wait ()
75{ 91{
76 if (nreqs && !ress) 92 if (nreqs && !ress)
87poll_cb () 103poll_cb ()
88{ 104{
89 dSP; 105 dSP;
90 int count = 0; 106 int count = 0;
91 int do_croak = 0; 107 int do_croak = 0;
92 aio_req req, prv; 108 aio_req req;
93 109
110 for (;;)
111 {
94 pthread_mutex_lock (&reslock); 112 pthread_mutex_lock (&reslock);
113 req = ress;
95 114
96 { 115 if (req)
116 {
117 ress = req->next;
118
119 if (!ress)
120 {
97 /* read any signals sent by the worker threads */ 121 /* read any signals sent by the worker threads */
98 char buf [32]; 122 char buf [32];
99 while (read (respipe [0], buf, 32) == 32) 123 while (read (respipe [0], buf, 32) == 32)
124 ;
125
126 rese = 0;
127 }
100 ; 128 }
101 }
102 129
103 req = ress;
104 ress = rese = 0;
105
106 pthread_mutex_unlock (&reslock); 130 pthread_mutex_unlock (&reslock);
107 131
108 while (req) 132 if (!req)
109 { 133 break;
134
110 nreqs--; 135 nreqs--;
111 136
112 if (req->type == REQ_QUIT) 137 if (req->type == REQ_QUIT)
113 started--; 138 started--;
114 else 139 else
115 { 140 {
116 int errorno = errno; 141 int errorno = errno;
117 errno = req->errorno; 142 errno = req->errorno;
118 143
119 if (req->type == REQ_READ) 144 if (req->type == REQ_READ)
120 SvCUR_set (req->data, req->dataoffset 145 SvCUR_set (req->data, req->dataoffset + (req->result > 0 ? req->result : 0));
121 + req->result > 0 ? req->result : 0);
122 146
147 if (req->data2ptr && (req->type == REQ_READ || req->type == REQ_WRITE))
148 SvREADONLY_off (req->data);
149
123 if (req->data) 150 if (req->statdata)
124 SvREFCNT_dec (req->data);
125
126 if (req->fh)
127 SvREFCNT_dec (req->fh);
128
129 if (req->type == REQ_STAT || req->type == REQ_LSTAT || req->type == REQ_FSTAT)
130 { 151 {
131 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT; 152 PL_laststype = req->type == REQ_LSTAT ? OP_LSTAT : OP_STAT;
132 PL_laststatval = req->result; 153 PL_laststatval = req->result;
133 PL_statcache = *(req->statdata); 154 PL_statcache = *(req->statdata);
134
135 Safefree (req->statdata);
136 } 155 }
137 156
138 ENTER; 157 ENTER;
139 PUSHMARK (SP); 158 PUSHMARK (SP);
140 XPUSHs (sv_2mortal (newSViv (req->result))); 159 XPUSHs (sv_2mortal (newSViv (req->result)));
157 if (SvOK (req->callback)) 176 if (SvOK (req->callback))
158 { 177 {
159 PUTBACK; 178 PUTBACK;
160 call_sv (req->callback, G_VOID | G_EVAL); 179 call_sv (req->callback, G_VOID | G_EVAL);
161 SPAGAIN; 180 SPAGAIN;
181
182 if (SvTRUE (ERRSV))
183 {
184 free_req (req);
185 croak (0);
186 }
162 } 187 }
163 188
164 do_croak = SvTRUE (ERRSV);
165
166 LEAVE; 189 LEAVE;
167
168 if (req->callback)
169 SvREFCNT_dec (req->callback);
170 190
171 errno = errorno; 191 errno = errorno;
172 count++; 192 count++;
173 } 193 }
174 194
175 prv = req; 195 free_req (req);
176 req = req->next;
177 Safefree (prv);
178
179 if (do_croak)
180 croak (0);
181 } 196 }
182 197
183 return count; 198 return count;
184} 199}
185 200
206} 221}
207 222
208static void 223static void
209send_req (aio_req req) 224send_req (aio_req req)
210{ 225{
226 while (started < wanted && nreqs >= started)
227 start_thread ();
228
211 nreqs++; 229 nreqs++;
212 230
213 pthread_mutex_lock (&reqlock); 231 pthread_mutex_lock (&reqlock);
214 232
215 req->next = 0; 233 req->next = 0;
223 reqe = reqs = req; 241 reqe = reqs = req;
224 242
225 pthread_cond_signal (&reqwait); 243 pthread_cond_signal (&reqwait);
226 pthread_mutex_unlock (&reqlock); 244 pthread_mutex_unlock (&reqlock);
227 245
228 while (nreqs > max_outstanding) 246 if (nreqs > max_outstanding)
247 for (;;)
248 {
249 poll_cb ();
250
251 if (nreqs <= max_outstanding)
252 break;
253
254 poll_wait ();
255 }
256}
257
258static void
259end_thread (void)
260{
261 aio_req req;
262 Newz (0, req, 1, aio_cb);
263 req->type = REQ_QUIT;
264
265 send_req (req);
266}
267
268static void min_parallel (int nthreads)
269{
270 if (wanted < nthreads)
271 wanted = nthreads;
272}
273
274static void max_parallel (int nthreads)
275{
276 int cur = started;
277
278 if (wanted > nthreads)
279 wanted = nthreads;
280
281 while (cur > wanted)
282 {
283 end_thread ();
284 cur--;
285 }
286
287 while (started > wanted)
229 { 288 {
230 poll_wait (); 289 poll_wait ();
231 poll_cb (); 290 poll_cb ();
232 } 291 }
233} 292}
234 293
235static void 294static void create_pipe ()
236end_thread (void)
237{ 295{
238 aio_req req; 296 if (pipe (respipe))
239 New (0, req, 1, aio_cb); 297 croak ("unable to initialize result pipe");
240 req->type = REQ_QUIT;
241 298
242 send_req (req); 299 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
243} 300 croak ("cannot set result pipe to nonblocking mode");
244 301
245 302 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
246static void min_parallel (int nthreads) 303 croak ("cannot set result pipe to nonblocking mode");
247{
248 while (nthreads > started)
249 start_thread ();
250} 304}
251
252static void max_parallel (int nthreads)
253{
254 int cur = started;
255 while (cur > nthreads)
256 {
257 end_thread ();
258 cur--;
259 }
260
261 while (started > nthreads)
262 {
263 poll_wait ();
264 poll_cb ();
265 }
266}
267
268static int fork_started;
269 305
270static void atfork_prepare (void) 306static void atfork_prepare (void)
271{ 307{
272 pthread_mutex_lock (&frklock);
273
274 fork_started = started;
275
276 for (;;) {
277 while (nreqs)
278 {
279 poll_wait ();
280 poll_cb ();
281 }
282
283 max_parallel (0);
284
285 pthread_mutex_lock (&reqlock); 308 pthread_mutex_lock (&reqlock);
286
287 if (!nreqs && !started)
288 break;
289
290 pthread_mutex_unlock (&reqlock);
291
292 min_parallel (fork_started);
293 }
294
295 pthread_mutex_lock (&reslock); 309 pthread_mutex_lock (&reslock);
296
297 assert (!started);
298 assert (!nreqs);
299 assert (!reqs && !reqe);
300 assert (!ress && !rese);
301} 310}
302 311
303static void atfork_parent (void) 312static void atfork_parent (void)
304{ 313{
305 pthread_mutex_unlock (&reslock); 314 pthread_mutex_unlock (&reslock);
306 min_parallel (fork_started);
307 pthread_mutex_unlock (&reqlock); 315 pthread_mutex_unlock (&reqlock);
308 pthread_mutex_unlock (&frklock);
309} 316}
310 317
311static void atfork_child (void) 318static void atfork_child (void)
312{ 319{
320 aio_req prv;
321
322 started = 0;
323
324 while (reqs)
325 {
326 prv = reqs;
327 reqs = prv->next;
328 free_req (prv);
329 }
330
313 reqs = reqe = 0; 331 reqs = reqe = 0;
332
333 while (ress)
334 {
335 prv = ress;
336 ress = prv->next;
337 free_req (prv);
338 }
339
340 ress = rese = 0;
341
342 close (respipe [0]);
343 close (respipe [1]);
344 create_pipe ();
314 345
315 atfork_parent (); 346 atfork_parent ();
316} 347}
317 348
318/*****************************************************************************/ 349/*****************************************************************************/
483 \ 514 \
484 Newz (0, req, 1, aio_cb); \ 515 Newz (0, req, 1, aio_cb); \
485 if (!req) \ 516 if (!req) \
486 croak ("out of memory during aio_req allocation"); \ 517 croak ("out of memory during aio_req allocation"); \
487 \ 518 \
488 req->callback = SvREFCNT_inc (callback); 519 req->callback = newSVsv (callback);
489 520
490MODULE = IO::AIO PACKAGE = IO::AIO 521MODULE = IO::AIO PACKAGE = IO::AIO
491 522
492PROTOTYPES: ENABLE 523PROTOTYPES: ENABLE
493 524
494BOOT: 525BOOT:
495{ 526{
496 if (pipe (respipe)) 527 create_pipe ();
497 croak ("unable to initialize result pipe");
498
499 if (fcntl (respipe [0], F_SETFL, O_NONBLOCK))
500 croak ("cannot set result pipe to nonblocking mode");
501
502 if (fcntl (respipe [1], F_SETFL, O_NONBLOCK))
503 croak ("cannot set result pipe to nonblocking mode");
504
505 pthread_atfork (atfork_prepare, atfork_parent, atfork_child); 528 pthread_atfork (atfork_prepare, atfork_parent, atfork_child);
506} 529}
507 530
508void 531void
509min_parallel(nthreads) 532min_parallel(nthreads)
614 : IoOFP (sv_2io (fh))); 637 : IoOFP (sv_2io (fh)));
615 req->offset = offset; 638 req->offset = offset;
616 req->length = length; 639 req->length = length;
617 req->data = SvREFCNT_inc (data); 640 req->data = SvREFCNT_inc (data);
618 req->dataptr = (char *)svptr + dataoffset; 641 req->dataptr = (char *)svptr + dataoffset;
619 req->callback = SvREFCNT_inc (callback); 642
643 if (!SvREADONLY (data))
644 {
645 SvREADONLY_on (data);
646 req->data2ptr = (void *)data;
647 }
620 648
621 send_req (req); 649 send_req (req);
622 } 650 }
623} 651}
624 652
653{ 681{
654 dREQ; 682 dREQ;
655 683
656 New (0, req->statdata, 1, Stat_t); 684 New (0, req->statdata, 1, Stat_t);
657 if (!req->statdata) 685 if (!req->statdata)
686 {
687 free_req (req);
658 croak ("out of memory during aio_req->statdata allocation (sorry, i just leaked memory, too)"); 688 croak ("out of memory during aio_req->statdata allocation");
689 }
659 690
660 if (SvPOK (fh_or_path)) 691 if (SvPOK (fh_or_path))
661 { 692 {
662 req->type = ix; 693 req->type = ix;
663 req->data = newSVsv (fh_or_path); 694 req->data = newSVsv (fh_or_path);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines