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

Comparing libeio/eio.c (file contents):
Revision 1.65 by root, Mon May 30 12:56:50 2011 UTC vs.
Revision 1.72 by root, Fri Jun 10 12:45:20 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines