ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
(Generate patch)

Comparing libeio/eio.c (file contents):
Revision 1.64 by root, Thu May 26 04:05:18 2011 UTC vs.
Revision 1.72 by root, Fri Jun 10 12:45:20 2011 UTC

35 * and other provisions required by the GPL. If you do not delete the 35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under 36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL. 37 * either the BSD or the GPL.
38 */ 38 */
39 39
40#ifndef _WIN32
41# include "config.h"
42#endif
43
40#include "eio.h" 44#include "eio.h"
45#include "ecb.h"
41 46
42#ifdef EIO_STACKSIZE 47#ifdef EIO_STACKSIZE
43# define XTHREAD_STACKSIZE EIO_STACKSIZE 48# define XTHREAD_STACKSIZE EIO_STACKSIZE
44#endif 49#endif
45#include "xthread.h" 50#include "xthread.h"
54#include <sys/statvfs.h> 59#include <sys/statvfs.h>
55#include <limits.h> 60#include <limits.h>
56#include <fcntl.h> 61#include <fcntl.h>
57#include <assert.h> 62#include <assert.h>
58 63
64/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
65/* intptr_t only comes form stdint.h, says idiot openbsd coder */
66#if HAVE_STDINT_H
67# include <stdint.h>
68#endif
69
59#ifndef EIO_FINISH 70#ifndef EIO_FINISH
60# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 71# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
61#endif 72#endif
62 73
63#ifndef EIO_DESTROY 74#ifndef EIO_DESTROY
71#ifdef _WIN32 82#ifdef _WIN32
72 83
73 /*doh*/ 84 /*doh*/
74#else 85#else
75 86
76# include "config.h"
77# include <sys/time.h> 87# include <sys/time.h>
78# include <sys/select.h> 88# include <sys/select.h>
79# include <unistd.h> 89# include <unistd.h>
80# include <utime.h> 90# include <utime.h>
81# include <signal.h> 91# include <signal.h>
156 if (!eio_buf) \ 166 if (!eio_buf) \
157 return -1; 167 return -1;
158 168
159#define EIO_TICKS ((1000000 + 1023) >> 10) 169#define EIO_TICKS ((1000000 + 1023) >> 10)
160 170
161/*****************************************************************************/
162
163#if __GNUC__ >= 3
164# define expect(expr,value) __builtin_expect ((expr),(value))
165#else
166# define expect(expr,value) (expr)
167#endif
168
169#define expect_false(expr) expect ((expr) != 0, 0)
170#define expect_true(expr) expect ((expr) != 0, 1)
171
172/*****************************************************************************/
173
174#define ETP_PRI_MIN EIO_PRI_MIN 171#define ETP_PRI_MIN EIO_PRI_MIN
175#define ETP_PRI_MAX EIO_PRI_MAX 172#define ETP_PRI_MAX EIO_PRI_MAX
176 173
177struct etp_worker; 174struct etp_worker;
178 175
203/*****************************************************************************/ 200/*****************************************************************************/
204 201
205#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 202#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
206 203
207/* calculate time difference in ~1/EIO_TICKS of a second */ 204/* calculate time difference in ~1/EIO_TICKS of a second */
205ECB_INLINE int
208static int tvdiff (struct timeval *tv1, struct timeval *tv2) 206tvdiff (struct timeval *tv1, struct timeval *tv2)
209{ 207{
210 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS 208 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
211 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 209 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
212} 210}
213 211
257#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 255#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
258#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 256#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
259 257
260/* worker threads management */ 258/* worker threads management */
261 259
260static void ecb_cold
262static void etp_worker_clear (etp_worker *wrk) 261etp_worker_clear (etp_worker *wrk)
263{ 262{
264 ETP_WORKER_CLEAR (wrk); 263 ETP_WORKER_CLEAR (wrk);
265} 264}
266 265
266static void ecb_cold
267static void etp_worker_free (etp_worker *wrk) 267etp_worker_free (etp_worker *wrk)
268{ 268{
269 wrk->next->prev = wrk->prev; 269 wrk->next->prev = wrk->prev;
270 wrk->prev->next = wrk->next; 270 wrk->prev->next = wrk->next;
271 271
272 free (wrk); 272 free (wrk);
273} 273}
274 274
275static unsigned int etp_nreqs (void) 275static unsigned int
276etp_nreqs (void)
276{ 277{
277 int retval; 278 int retval;
278 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 279 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
279 retval = nreqs; 280 retval = nreqs;
280 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 281 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
281 return retval; 282 return retval;
282} 283}
283 284
284static unsigned int etp_nready (void) 285static unsigned int
286etp_nready (void)
285{ 287{
286 unsigned int retval; 288 unsigned int retval;
287 289
288 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 290 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
289 retval = nready; 291 retval = nready;
290 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 292 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
291 293
292 return retval; 294 return retval;
293} 295}
294 296
295static unsigned int etp_npending (void) 297static unsigned int
298etp_npending (void)
296{ 299{
297 unsigned int retval; 300 unsigned int retval;
298 301
299 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 302 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
300 retval = npending; 303 retval = npending;
301 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 304 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
302 305
303 return retval; 306 return retval;
304} 307}
305 308
306static unsigned int etp_nthreads (void) 309static unsigned int
310etp_nthreads (void)
307{ 311{
308 unsigned int retval; 312 unsigned int retval;
309 313
310 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 314 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
311 retval = started; 315 retval = started;
325} etp_reqq; 329} etp_reqq;
326 330
327static etp_reqq req_queue; 331static etp_reqq req_queue;
328static etp_reqq res_queue; 332static etp_reqq res_queue;
329 333
334static int ecb_noinline
330static int reqq_push (etp_reqq *q, ETP_REQ *req) 335reqq_push (etp_reqq *q, ETP_REQ *req)
331{ 336{
332 int pri = req->pri; 337 int pri = req->pri;
333 req->next = 0; 338 req->next = 0;
334 339
335 if (q->qe[pri]) 340 if (q->qe[pri])
341 q->qe[pri] = q->qs[pri] = req; 346 q->qe[pri] = q->qs[pri] = req;
342 347
343 return q->size++; 348 return q->size++;
344} 349}
345 350
351static ETP_REQ * ecb_noinline
346static ETP_REQ *reqq_shift (etp_reqq *q) 352reqq_shift (etp_reqq *q)
347{ 353{
348 int pri; 354 int pri;
349 355
350 if (!q->size) 356 if (!q->size)
351 return 0; 357 return 0;
366 } 372 }
367 373
368 abort (); 374 abort ();
369} 375}
370 376
377static void ecb_cold
371static void etp_thread_init (void) 378etp_thread_init (void)
372{ 379{
373 X_MUTEX_CREATE (wrklock); 380 X_MUTEX_CREATE (wrklock);
374 X_MUTEX_CREATE (reslock); 381 X_MUTEX_CREATE (reslock);
375 X_MUTEX_CREATE (reqlock); 382 X_MUTEX_CREATE (reqlock);
376 X_COND_CREATE (reqwait); 383 X_COND_CREATE (reqwait);
377} 384}
378 385
386static void ecb_cold
379static void etp_atfork_prepare (void) 387etp_atfork_prepare (void)
380{ 388{
381 X_LOCK (wrklock); 389 X_LOCK (wrklock);
382 X_LOCK (reqlock); 390 X_LOCK (reqlock);
383 X_LOCK (reslock); 391 X_LOCK (reslock);
384#if !HAVE_PREADWRITE 392#if !HAVE_PREADWRITE
385 X_LOCK (preadwritelock); 393 X_LOCK (preadwritelock);
386#endif 394#endif
387} 395}
388 396
397static void ecb_cold
389static void etp_atfork_parent (void) 398etp_atfork_parent (void)
390{ 399{
391#if !HAVE_PREADWRITE 400#if !HAVE_PREADWRITE
392 X_UNLOCK (preadwritelock); 401 X_UNLOCK (preadwritelock);
393#endif 402#endif
394 X_UNLOCK (reslock); 403 X_UNLOCK (reslock);
395 X_UNLOCK (reqlock); 404 X_UNLOCK (reqlock);
396 X_UNLOCK (wrklock); 405 X_UNLOCK (wrklock);
397} 406}
398 407
408static void ecb_cold
399static void etp_atfork_child (void) 409etp_atfork_child (void)
400{ 410{
401 ETP_REQ *prv; 411 ETP_REQ *prv;
402 412
403 while ((prv = reqq_shift (&req_queue))) 413 while ((prv = reqq_shift (&req_queue)))
404 ETP_DESTROY (prv); 414 ETP_DESTROY (prv);
424 npending = 0; 434 npending = 0;
425 435
426 etp_thread_init (); 436 etp_thread_init ();
427} 437}
428 438
429static void 439static void ecb_cold
430etp_once_init (void) 440etp_once_init (void)
431{ 441{
432 etp_thread_init (); 442 etp_thread_init ();
433 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); 443 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
434} 444}
435 445
436static int 446static int ecb_cold
437etp_init (void (*want_poll)(void), void (*done_poll)(void)) 447etp_init (void (*want_poll)(void), void (*done_poll)(void))
438{ 448{
439 static pthread_once_t doinit = PTHREAD_ONCE_INIT; 449 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
440 450
441 pthread_once (&doinit, etp_once_init); 451 pthread_once (&doinit, etp_once_init);
446 return 0; 456 return 0;
447} 457}
448 458
449X_THREAD_PROC (etp_proc); 459X_THREAD_PROC (etp_proc);
450 460
461static void ecb_cold
451static void etp_start_thread (void) 462etp_start_thread (void)
452{ 463{
453 etp_worker *wrk = calloc (1, sizeof (etp_worker)); 464 etp_worker *wrk = calloc (1, sizeof (etp_worker));
454 465
455 /*TODO*/ 466 /*TODO*/
456 assert (("unable to allocate worker thread data", wrk)); 467 assert (("unable to allocate worker thread data", wrk));
469 free (wrk); 480 free (wrk);
470 481
471 X_UNLOCK (wrklock); 482 X_UNLOCK (wrklock);
472} 483}
473 484
485static void
474static void etp_maybe_start_thread (void) 486etp_maybe_start_thread (void)
475{ 487{
476 if (expect_true (etp_nthreads () >= wanted)) 488 if (ecb_expect_true (etp_nthreads () >= wanted))
477 return; 489 return;
478 490
479 /* todo: maybe use idle here, but might be less exact */ 491 /* todo: maybe use idle here, but might be less exact */
480 if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) 492 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
481 return; 493 return;
482 494
483 etp_start_thread (); 495 etp_start_thread ();
484} 496}
485 497
498static void ecb_cold
486static void etp_end_thread (void) 499etp_end_thread (void)
487{ 500{
488 eio_req *req = calloc (1, sizeof (eio_req)); 501 eio_req *req = calloc (1, sizeof (eio_req));
489 502
490 req->type = -1; 503 req->type = -1;
491 req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 504 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
498 X_LOCK (wrklock); 511 X_LOCK (wrklock);
499 --started; 512 --started;
500 X_UNLOCK (wrklock); 513 X_UNLOCK (wrklock);
501} 514}
502 515
503static int etp_poll (void) 516static int
517etp_poll (void)
504{ 518{
505 unsigned int maxreqs; 519 unsigned int maxreqs;
506 unsigned int maxtime; 520 unsigned int maxtime;
507 struct timeval tv_start, tv_now; 521 struct timeval tv_start, tv_now;
508 522
538 552
539 X_LOCK (reqlock); 553 X_LOCK (reqlock);
540 --nreqs; 554 --nreqs;
541 X_UNLOCK (reqlock); 555 X_UNLOCK (reqlock);
542 556
543 if (expect_false (req->type == EIO_GROUP && req->size)) 557 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
544 { 558 {
545 req->int1 = 1; /* mark request as delayed */ 559 req->int1 = 1; /* mark request as delayed */
546 continue; 560 continue;
547 } 561 }
548 else 562 else
549 { 563 {
550 int res = ETP_FINISH (req); 564 int res = ETP_FINISH (req);
551 if (expect_false (res)) 565 if (ecb_expect_false (res))
552 return res; 566 return res;
553 } 567 }
554 568
555 if (expect_false (maxreqs && !--maxreqs)) 569 if (ecb_expect_false (maxreqs && !--maxreqs))
556 break; 570 break;
557 571
558 if (maxtime) 572 if (maxtime)
559 { 573 {
560 gettimeofday (&tv_now, 0); 574 gettimeofday (&tv_now, 0);
566 580
567 errno = EAGAIN; 581 errno = EAGAIN;
568 return -1; 582 return -1;
569} 583}
570 584
585static void
571static void etp_cancel (ETP_REQ *req) 586etp_cancel (ETP_REQ *req)
572{ 587{
573 X_LOCK (wrklock); 588 X_LOCK (wrklock);
574 req->flags |= EIO_FLAG_CANCELLED; 589 req->flags |= EIO_FLAG_CANCELLED;
575 X_UNLOCK (wrklock); 590 X_UNLOCK (wrklock);
576 591
577 eio_grp_cancel (req); 592 eio_grp_cancel (req);
578} 593}
579 594
595static void
580static void etp_submit (ETP_REQ *req) 596etp_submit (ETP_REQ *req)
581{ 597{
582 req->pri -= ETP_PRI_MIN; 598 req->pri -= ETP_PRI_MIN;
583 599
584 if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; 600 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
585 if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 601 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
586 602
587 if (expect_false (req->type == EIO_GROUP)) 603 if (ecb_expect_false (req->type == EIO_GROUP))
588 { 604 {
589 /* I hope this is worth it :/ */ 605 /* I hope this is worth it :/ */
590 X_LOCK (reqlock); 606 X_LOCK (reqlock);
591 ++nreqs; 607 ++nreqs;
592 X_UNLOCK (reqlock); 608 X_UNLOCK (reqlock);
611 627
612 etp_maybe_start_thread (); 628 etp_maybe_start_thread ();
613 } 629 }
614} 630}
615 631
632static void ecb_cold
616static void etp_set_max_poll_time (double nseconds) 633etp_set_max_poll_time (double nseconds)
617{ 634{
618 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 635 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
619 max_poll_time = nseconds * EIO_TICKS; 636 max_poll_time = nseconds * EIO_TICKS;
620 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 637 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
621} 638}
622 639
640static void ecb_cold
623static void etp_set_max_poll_reqs (unsigned int maxreqs) 641etp_set_max_poll_reqs (unsigned int maxreqs)
624{ 642{
625 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 643 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
626 max_poll_reqs = maxreqs; 644 max_poll_reqs = maxreqs;
627 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 645 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
628} 646}
629 647
648static void ecb_cold
630static void etp_set_max_idle (unsigned int nthreads) 649etp_set_max_idle (unsigned int nthreads)
631{ 650{
632 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 651 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
633 max_idle = nthreads; 652 max_idle = nthreads;
634 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 653 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
635} 654}
636 655
656static void ecb_cold
637static void etp_set_idle_timeout (unsigned int seconds) 657etp_set_idle_timeout (unsigned int seconds)
638{ 658{
639 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 659 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
640 idle_timeout = seconds; 660 idle_timeout = seconds;
641 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 661 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
642} 662}
643 663
664static void ecb_cold
644static void etp_set_min_parallel (unsigned int nthreads) 665etp_set_min_parallel (unsigned int nthreads)
645{ 666{
646 if (wanted < nthreads) 667 if (wanted < nthreads)
647 wanted = nthreads; 668 wanted = nthreads;
648} 669}
649 670
671static void ecb_cold
650static void etp_set_max_parallel (unsigned int nthreads) 672etp_set_max_parallel (unsigned int nthreads)
651{ 673{
652 if (wanted > nthreads) 674 if (wanted > nthreads)
653 wanted = nthreads; 675 wanted = nthreads;
654 676
655 while (started > wanted) 677 while (started > wanted)
656 etp_end_thread (); 678 etp_end_thread ();
657} 679}
658 680
659/*****************************************************************************/ 681/*****************************************************************************/
660 682
683static void
661static void grp_try_feed (eio_req *grp) 684grp_try_feed (eio_req *grp)
662{ 685{
663 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 686 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
664 { 687 {
665 grp->flags &= ~EIO_FLAG_GROUPADD; 688 grp->flags &= ~EIO_FLAG_GROUPADD;
666 689
673 break; 696 break;
674 } 697 }
675 } 698 }
676} 699}
677 700
701static int
678static int grp_dec (eio_req *grp) 702grp_dec (eio_req *grp)
679{ 703{
680 --grp->size; 704 --grp->size;
681 705
682 /* call feeder, if applicable */ 706 /* call feeder, if applicable */
683 grp_try_feed (grp); 707 grp_try_feed (grp);
687 return eio_finish (grp); 711 return eio_finish (grp);
688 else 712 else
689 return 0; 713 return 0;
690} 714}
691 715
716void
692void eio_destroy (eio_req *req) 717eio_destroy (eio_req *req)
693{ 718{
694 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); 719 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
695 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); 720 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
696 721
697 EIO_DESTROY (req); 722 EIO_DESTROY (req);
698} 723}
699 724
725static int
700static int eio_finish (eio_req *req) 726eio_finish (eio_req *req)
701{ 727{
702 int res = EIO_FINISH (req); 728 int res = EIO_FINISH (req);
703 729
704 if (req->grp) 730 if (req->grp)
705 { 731 {
722 eio_destroy (req); 748 eio_destroy (req);
723 749
724 return res; 750 return res;
725} 751}
726 752
753void
727void eio_grp_cancel (eio_req *grp) 754eio_grp_cancel (eio_req *grp)
728{ 755{
729 for (grp = grp->grp_first; grp; grp = grp->grp_next) 756 for (grp = grp->grp_first; grp; grp = grp->grp_next)
730 eio_cancel (grp); 757 eio_cancel (grp);
731} 758}
732 759
760void
733void eio_cancel (eio_req *req) 761eio_cancel (eio_req *req)
734{ 762{
735 etp_cancel (req); 763 etp_cancel (req);
736} 764}
737 765
766void
738void eio_submit (eio_req *req) 767eio_submit (eio_req *req)
739{ 768{
740 etp_submit (req); 769 etp_submit (req);
741} 770}
742 771
743unsigned int eio_nreqs (void) 772unsigned int
773eio_nreqs (void)
744{ 774{
745 return etp_nreqs (); 775 return etp_nreqs ();
746} 776}
747 777
748unsigned int eio_nready (void) 778unsigned int
779eio_nready (void)
749{ 780{
750 return etp_nready (); 781 return etp_nready ();
751} 782}
752 783
753unsigned int eio_npending (void) 784unsigned int
785eio_npending (void)
754{ 786{
755 return etp_npending (); 787 return etp_npending ();
756} 788}
757 789
758unsigned int eio_nthreads (void) 790unsigned int ecb_cold
791eio_nthreads (void)
759{ 792{
760 return etp_nthreads (); 793 return etp_nthreads ();
761} 794}
762 795
796void ecb_cold
763void eio_set_max_poll_time (double nseconds) 797eio_set_max_poll_time (double nseconds)
764{ 798{
765 etp_set_max_poll_time (nseconds); 799 etp_set_max_poll_time (nseconds);
766} 800}
767 801
802void ecb_cold
768void eio_set_max_poll_reqs (unsigned int maxreqs) 803eio_set_max_poll_reqs (unsigned int maxreqs)
769{ 804{
770 etp_set_max_poll_reqs (maxreqs); 805 etp_set_max_poll_reqs (maxreqs);
771} 806}
772 807
808void ecb_cold
773void eio_set_max_idle (unsigned int nthreads) 809eio_set_max_idle (unsigned int nthreads)
774{ 810{
775 etp_set_max_idle (nthreads); 811 etp_set_max_idle (nthreads);
776} 812}
777 813
814void ecb_cold
778void eio_set_idle_timeout (unsigned int seconds) 815eio_set_idle_timeout (unsigned int seconds)
779{ 816{
780 etp_set_idle_timeout (seconds); 817 etp_set_idle_timeout (seconds);
781} 818}
782 819
820void ecb_cold
783void eio_set_min_parallel (unsigned int nthreads) 821eio_set_min_parallel (unsigned int nthreads)
784{ 822{
785 etp_set_min_parallel (nthreads); 823 etp_set_min_parallel (nthreads);
786} 824}
787 825
826void ecb_cold
788void eio_set_max_parallel (unsigned int nthreads) 827eio_set_max_parallel (unsigned int nthreads)
789{ 828{
790 etp_set_max_parallel (nthreads); 829 etp_set_max_parallel (nthreads);
791} 830}
792 831
793int eio_poll (void) 832int eio_poll (void)
863#ifndef HAVE_FUTIMES 902#ifndef HAVE_FUTIMES
864 903
865# undef futimes 904# undef futimes
866# define futimes(fd,times) eio__futimes (fd, times) 905# define futimes(fd,times) eio__futimes (fd, times)
867 906
907static int
868static int eio__futimes (int fd, const struct timeval tv[2]) 908eio__futimes (int fd, const struct timeval tv[2])
869{ 909{
870 errno = ENOSYS; 910 errno = ENOSYS;
871 return -1; 911 return -1;
872} 912}
873 913
877# undef fdatasync 917# undef fdatasync
878# define fdatasync(fd) fsync (fd) 918# define fdatasync(fd) fsync (fd)
879#endif 919#endif
880 920
881/* sync_file_range always needs emulation */ 921/* sync_file_range always needs emulation */
882int 922static int
883eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) 923eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags)
884{ 924{
885#if HAVE_SYNC_FILE_RANGE 925#if HAVE_SYNC_FILE_RANGE
886 int res; 926 int res;
887 927
933 973
934/* sendfile always needs emulation */ 974/* sendfile always needs emulation */
935static ssize_t 975static ssize_t
936eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 976eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
937{ 977{
978 ssize_t written = 0;
938 ssize_t res; 979 ssize_t res;
939 980
940 if (!count) 981 if (!count)
941 return 0; 982 return 0;
942 983
984 for (;;)
985 {
943#if HAVE_SENDFILE 986#if HAVE_SENDFILE
944# if __linux 987# if __linux
988 off_t soffset = offset;
945 res = sendfile (ofd, ifd, &offset, count); 989 res = sendfile (ofd, ifd, &soffset, count);
946 990
947# elif __FreeBSD__ 991# elif __FreeBSD__
948 /* 992 /*
949 * Of course, the freebsd sendfile is a dire hack with no thoughts 993 * Of course, the freebsd sendfile is a dire hack with no thoughts
950 * wasted on making it similar to other I/O functions. 994 * wasted on making it similar to other I/O functions.
951 */ 995 */
952 {
953 off_t sbytes; 996 off_t sbytes;
954 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 997 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
955 998
956 #if 0 /* according to the manpage, this is correct, but broken behaviour */ 999 #if 0 /* according to the manpage, this is correct, but broken behaviour */
957 /* freebsd' sendfile will return 0 on success */ 1000 /* freebsd' sendfile will return 0 on success */
958 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */ 1001 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */
959 /* not on e.g. EIO or EPIPE - sounds broken */ 1002 /* not on e.g. EIO or EPIPE - sounds broken */
960 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) 1003 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0)
961 res = sbytes; 1004 res = sbytes;
962 #endif 1005 #endif
963 1006
964 /* according to source inspection, this is correct, and useful behaviour */ 1007 /* according to source inspection, this is correct, and useful behaviour */
965 if (sbytes) 1008 if (sbytes)
966 res = sbytes; 1009 res = sbytes;
967 }
968 1010
969# elif defined (__APPLE__) 1011# elif defined (__APPLE__)
970
971 {
972 off_t sbytes = count; 1012 off_t sbytes = count;
973 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 1013 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
974 1014
975 /* according to the manpage, sbytes is always valid */ 1015 /* according to the manpage, sbytes is always valid */
976 if (sbytes) 1016 if (sbytes)
977 res = sbytes; 1017 res = sbytes;
978 }
979 1018
980# elif __hpux 1019# elif __hpux
981 res = sendfile (ofd, ifd, offset, count, 0, 0); 1020 res = sendfile (ofd, ifd, offset, count, 0, 0);
982 1021
983# elif __solaris 1022# elif __solaris
984 {
985 struct sendfilevec vec; 1023 struct sendfilevec vec;
986 size_t sbytes; 1024 size_t sbytes;
987 1025
988 vec.sfv_fd = ifd; 1026 vec.sfv_fd = ifd;
989 vec.sfv_flag = 0; 1027 vec.sfv_flag = 0;
990 vec.sfv_off = offset; 1028 vec.sfv_off = offset;
991 vec.sfv_len = count; 1029 vec.sfv_len = count;
992 1030
993 res = sendfilev (ofd, &vec, 1, &sbytes); 1031 res = sendfilev (ofd, &vec, 1, &sbytes);
994 1032
995 if (res < 0 && sbytes) 1033 if (res < 0 && sbytes)
996 res = sbytes; 1034 res = sbytes;
997 }
998 1035
999# endif 1036# endif
1000 1037
1001#elif defined (_WIN32) 1038#elif defined (_WIN32)
1002
1003 /* does not work, just for documentation of what would need to be done */ 1039 /* does not work, just for documentation of what would need to be done */
1004 { 1040 /* actually, cannot be done like this, as TransmitFile changes the file offset, */
1041 /* libeio guarantees that the file offset does not change, and windows */
1042 /* has no way to get an independent handle to the same file description */
1005 HANDLE h = TO_SOCKET (ifd); 1043 HANDLE h = TO_SOCKET (ifd);
1006 SetFilePointer (h, offset, 0, FILE_BEGIN); 1044 SetFilePointer (h, offset, 0, FILE_BEGIN);
1007 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 1045 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1008 }
1009 1046
1010#else 1047#else
1011 res = -1; 1048 res = -1;
1012 errno = ENOSYS; 1049 errno = ENOSYS;
1013#endif 1050#endif
1014 1051
1052 /* we assume sendfile can copy at least 128mb in one go */
1053 if (res <= 128 * 1024 * 1024)
1054 {
1055 if (res > 0)
1056 written += res;
1057
1058 if (written)
1059 return written;
1060
1061 break;
1062 }
1063 else
1064 {
1065 /* if we requested more, then probably the kernel was lazy */
1066 written += res;
1067 offset += res;
1068 count -= res;
1069
1070 if (!count)
1071 return written;
1072 }
1073 }
1074
1015 if (res < 0 1075 if (res < 0
1016 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1076 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1017 /* BSDs */ 1077 /* BSDs */
1018#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1078#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1019 || errno == ENOTSUP 1079 || errno == ENOTSUP
1020#endif 1080#endif
1306 /* skip . and .. entries */ 1366 /* skip . and .. entries */
1307 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1367 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1308 { 1368 {
1309 int len = D_NAMLEN (entp) + 1; 1369 int len = D_NAMLEN (entp) + 1;
1310 1370
1311 while (expect_false (namesoffs + len > namesalloc)) 1371 while (ecb_expect_false (namesoffs + len > namesalloc))
1312 { 1372 {
1313 namesalloc *= 2; 1373 namesalloc *= 2;
1314 X_LOCK (wrklock); 1374 X_LOCK (wrklock);
1315 req->ptr2 = names = realloc (names, namesalloc); 1375 req->ptr2 = names = realloc (names, namesalloc);
1316 X_UNLOCK (wrklock); 1376 X_UNLOCK (wrklock);
1323 1383
1324 if (dents) 1384 if (dents)
1325 { 1385 {
1326 struct eio_dirent *ent; 1386 struct eio_dirent *ent;
1327 1387
1328 if (expect_false (dentoffs == dentalloc)) 1388 if (ecb_expect_false (dentoffs == dentalloc))
1329 { 1389 {
1330 dentalloc *= 2; 1390 dentalloc *= 2;
1331 X_LOCK (wrklock); 1391 X_LOCK (wrklock);
1332 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1392 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1333 X_UNLOCK (wrklock); 1393 X_UNLOCK (wrklock);
1493 1553
1494#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) 1554#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1495# define eio__msync(a,b,c) ((errno = ENOSYS), -1) 1555# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1496#else 1556#else
1497 1557
1498int 1558static int
1499eio__msync (void *mem, size_t len, int flags) 1559eio__msync (void *mem, size_t len, int flags)
1500{ 1560{
1501 eio_page_align (&mem, &len); 1561 eio_page_align (&mem, &len);
1502 1562
1503 if (EIO_MS_ASYNC != MS_SYNC 1563 if (EIO_MS_ASYNC != MS_SYNC
1513 return msync (mem, len, flags); 1573 return msync (mem, len, flags);
1514} 1574}
1515 1575
1516#endif 1576#endif
1517 1577
1518int 1578static int
1519eio__mtouch (void *mem, size_t len, int flags) 1579eio__mtouch (eio_req *req)
1520{ 1580{
1581 void *mem = req->ptr2;
1582 size_t len = req->size;
1583 int flags = req->int1;
1584
1521 eio_page_align (&mem, &len); 1585 eio_page_align (&mem, &len);
1522 1586
1523 { 1587 {
1524 intptr_t addr = (intptr_t)mem; 1588 intptr_t addr = (intptr_t)mem;
1525 intptr_t end = addr + len; 1589 intptr_t end = addr + len;
1526 intptr_t page = eio_pagesize (); 1590 intptr_t page = eio_pagesize ();
1527 1591
1528 if (addr < end) 1592 if (addr < end)
1529 if (flags & EIO_MT_MODIFY) /* modify */ 1593 if (flags & EIO_MT_MODIFY) /* modify */
1530 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len); 1594 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1531 else 1595 else
1532 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len); 1596 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1533 } 1597 }
1534 1598
1535 return 0; 1599 return 0;
1536} 1600}
1537 1601
1625 return 0; 1689 return 0;
1626} 1690}
1627 1691
1628/*****************************************************************************/ 1692/*****************************************************************************/
1629 1693
1694int ecb_cold
1630int eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1695eio_init (void (*want_poll)(void), void (*done_poll)(void))
1631{ 1696{
1632 return etp_init (want_poll, done_poll); 1697 return etp_init (want_poll, done_poll);
1633} 1698}
1634 1699
1700ECB_INLINE void
1635static void eio_api_destroy (eio_req *req) 1701eio_api_destroy (eio_req *req)
1636{ 1702{
1637 free (req); 1703 free (req);
1638} 1704}
1639 1705
1640#define REQ(rtype) \ 1706#define REQ(rtype) \
1659 { \ 1725 { \
1660 eio_api_destroy (req); \ 1726 eio_api_destroy (req); \
1661 return 0; \ 1727 return 0; \
1662 } 1728 }
1663 1729
1730static void
1664static void eio_execute (etp_worker *self, eio_req *req) 1731eio_execute (etp_worker *self, eio_req *req)
1665{ 1732{
1666 switch (req->type) 1733 switch (req->type)
1667 { 1734 {
1668 case EIO_READ: ALLOC (req->size); 1735 case EIO_READ: ALLOC (req->size);
1669 req->result = req->offs >= 0 1736 req->result = req->offs >= 0
1711 1778
1712 case EIO_SYNC: req->result = 0; sync (); break; 1779 case EIO_SYNC: req->result = 0; sync (); break;
1713 case EIO_FSYNC: req->result = fsync (req->int1); break; 1780 case EIO_FSYNC: req->result = fsync (req->int1); break;
1714 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 1781 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1715 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 1782 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1716 case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; 1783 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1717 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 1784 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1718 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 1785 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
1719 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; 1786 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
1720 1787
1721 case EIO_READDIR: eio__scandir (req, self); break; 1788 case EIO_READDIR: eio__scandir (req, self); break;
1765 case EIO_NOP: 1832 case EIO_NOP:
1766 req->result = 0; 1833 req->result = 0;
1767 break; 1834 break;
1768 1835
1769 case EIO_CUSTOM: 1836 case EIO_CUSTOM:
1770 ((void (*)(eio_req *))req->feed) (req); 1837 req->feed (req);
1771 break; 1838 break;
1772 1839
1773 default: 1840 default:
1774 errno = ENOSYS; 1841 errno = ENOSYS;
1775 req->result = -1; 1842 req->result = -1;
1996eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2063eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
1997{ 2064{
1998 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2065 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
1999} 2066}
2000 2067
2001eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) 2068eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
2002{ 2069{
2003 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; 2070 REQ (EIO_CUSTOM); req->feed = execute; SEND;
2004} 2071}
2005 2072
2006#endif 2073#endif
2007 2074
2008eio_req *eio_grp (eio_cb cb, void *data) 2075eio_req *eio_grp (eio_cb cb, void *data)
2017#undef SEND 2084#undef SEND
2018 2085
2019/*****************************************************************************/ 2086/*****************************************************************************/
2020/* grp functions */ 2087/* grp functions */
2021 2088
2089void
2022void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) 2090eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
2023{ 2091{
2024 grp->int2 = limit; 2092 grp->int2 = limit;
2025 grp->feed = feed; 2093 grp->feed = feed;
2026 2094
2027 grp_try_feed (grp); 2095 grp_try_feed (grp);
2028} 2096}
2029 2097
2098void
2030void eio_grp_limit (eio_req *grp, int limit) 2099eio_grp_limit (eio_req *grp, int limit)
2031{ 2100{
2032 grp->int2 = limit; 2101 grp->int2 = limit;
2033 2102
2034 grp_try_feed (grp); 2103 grp_try_feed (grp);
2035} 2104}
2036 2105
2106void
2037void eio_grp_add (eio_req *grp, eio_req *req) 2107eio_grp_add (eio_req *grp, eio_req *req)
2038{ 2108{
2039 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2109 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2040 2110
2041 grp->flags |= EIO_FLAG_GROUPADD; 2111 grp->flags |= EIO_FLAG_GROUPADD;
2042 2112
2053} 2123}
2054 2124
2055/*****************************************************************************/ 2125/*****************************************************************************/
2056/* misc garbage */ 2126/* misc garbage */
2057 2127
2128ssize_t
2058ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2129eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2059{ 2130{
2060 etp_worker wrk; 2131 etp_worker wrk;
2061 ssize_t ret; 2132 ssize_t ret;
2062 2133
2063 wrk.dbuf = 0; 2134 wrk.dbuf = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines