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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines