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

Comparing cvsroot/libeio/eio.c (file contents):
Revision 1.66 by root, Sun Jun 5 19:58:37 2011 UTC vs.
Revision 1.85 by root, Wed Jul 13 00:53:03 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"
59#include <limits.h> 60#include <limits.h>
60#include <fcntl.h> 61#include <fcntl.h>
61#include <assert.h> 62#include <assert.h>
62 63
63/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ 64/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */
64/* intptr_t only comes form stdint.h, says idiot openbsd coder */ 65/* intptr_t only comes from stdint.h, says idiot openbsd coder */
65#if HAVE_STDINT_H 66#if HAVE_STDINT_H
66# include <stdint.h> 67# include <stdint.h>
67#endif 68#endif
69
70#ifndef ECANCELED
71# define ECANCELED EDOM
72#endif
73
74static void eio_destroy (eio_req *req);
68 75
69#ifndef EIO_FINISH 76#ifndef EIO_FINISH
70# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 77# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0
71#endif 78#endif
72 79
165 if (!eio_buf) \ 172 if (!eio_buf) \
166 return -1; 173 return -1;
167 174
168#define EIO_TICKS ((1000000 + 1023) >> 10) 175#define EIO_TICKS ((1000000 + 1023) >> 10)
169 176
170/*****************************************************************************/
171
172#if __GNUC__ >= 3
173# define expect(expr,value) __builtin_expect ((expr),(value))
174#else
175# define expect(expr,value) (expr)
176#endif
177
178#define expect_false(expr) expect ((expr) != 0, 0)
179#define expect_true(expr) expect ((expr) != 0, 1)
180
181/*****************************************************************************/
182
183#define ETP_PRI_MIN EIO_PRI_MIN 177#define ETP_PRI_MIN EIO_PRI_MIN
184#define ETP_PRI_MAX EIO_PRI_MAX 178#define ETP_PRI_MAX EIO_PRI_MAX
185 179
186struct etp_worker; 180struct etp_worker;
187 181
212/*****************************************************************************/ 206/*****************************************************************************/
213 207
214#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) 208#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1)
215 209
216/* calculate time difference in ~1/EIO_TICKS of a second */ 210/* calculate time difference in ~1/EIO_TICKS of a second */
211ecb_inline int
217static int tvdiff (struct timeval *tv1, struct timeval *tv2) 212tvdiff (struct timeval *tv1, struct timeval *tv2)
218{ 213{
219 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS 214 return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS
220 + ((tv2->tv_usec - tv1->tv_usec) >> 10); 215 + ((tv2->tv_usec - tv1->tv_usec) >> 10);
221} 216}
222 217
266#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) 261#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock)
267#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) 262#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock)
268 263
269/* worker threads management */ 264/* worker threads management */
270 265
266static void ecb_cold
271static void etp_worker_clear (etp_worker *wrk) 267etp_worker_clear (etp_worker *wrk)
272{ 268{
273 ETP_WORKER_CLEAR (wrk); 269 ETP_WORKER_CLEAR (wrk);
274} 270}
275 271
272static void ecb_cold
276static void etp_worker_free (etp_worker *wrk) 273etp_worker_free (etp_worker *wrk)
277{ 274{
278 wrk->next->prev = wrk->prev; 275 wrk->next->prev = wrk->prev;
279 wrk->prev->next = wrk->next; 276 wrk->prev->next = wrk->next;
280 277
281 free (wrk); 278 free (wrk);
282} 279}
283 280
284static unsigned int etp_nreqs (void) 281static unsigned int
282etp_nreqs (void)
285{ 283{
286 int retval; 284 int retval;
287 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 285 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
288 retval = nreqs; 286 retval = nreqs;
289 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 287 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
290 return retval; 288 return retval;
291} 289}
292 290
293static unsigned int etp_nready (void) 291static unsigned int
292etp_nready (void)
294{ 293{
295 unsigned int retval; 294 unsigned int retval;
296 295
297 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 296 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
298 retval = nready; 297 retval = nready;
299 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 298 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
300 299
301 return retval; 300 return retval;
302} 301}
303 302
304static unsigned int etp_npending (void) 303static unsigned int
304etp_npending (void)
305{ 305{
306 unsigned int retval; 306 unsigned int retval;
307 307
308 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 308 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
309 retval = npending; 309 retval = npending;
310 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 310 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
311 311
312 return retval; 312 return retval;
313} 313}
314 314
315static unsigned int etp_nthreads (void) 315static unsigned int
316etp_nthreads (void)
316{ 317{
317 unsigned int retval; 318 unsigned int retval;
318 319
319 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 320 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
320 retval = started; 321 retval = started;
334} etp_reqq; 335} etp_reqq;
335 336
336static etp_reqq req_queue; 337static etp_reqq req_queue;
337static etp_reqq res_queue; 338static etp_reqq res_queue;
338 339
340static int ecb_noinline
339static int reqq_push (etp_reqq *q, ETP_REQ *req) 341reqq_push (etp_reqq *q, ETP_REQ *req)
340{ 342{
341 int pri = req->pri; 343 int pri = req->pri;
342 req->next = 0; 344 req->next = 0;
343 345
344 if (q->qe[pri]) 346 if (q->qe[pri])
350 q->qe[pri] = q->qs[pri] = req; 352 q->qe[pri] = q->qs[pri] = req;
351 353
352 return q->size++; 354 return q->size++;
353} 355}
354 356
357static ETP_REQ * ecb_noinline
355static ETP_REQ *reqq_shift (etp_reqq *q) 358reqq_shift (etp_reqq *q)
356{ 359{
357 int pri; 360 int pri;
358 361
359 if (!q->size) 362 if (!q->size)
360 return 0; 363 return 0;
375 } 378 }
376 379
377 abort (); 380 abort ();
378} 381}
379 382
383static void ecb_cold
380static void etp_thread_init (void) 384etp_thread_init (void)
381{ 385{
386#if !HAVE_PREADWRITE
387 X_MUTEX_CREATE (preadwritelock);
388#endif
382 X_MUTEX_CREATE (wrklock); 389 X_MUTEX_CREATE (wrklock);
383 X_MUTEX_CREATE (reslock); 390 X_MUTEX_CREATE (reslock);
384 X_MUTEX_CREATE (reqlock); 391 X_MUTEX_CREATE (reqlock);
385 X_COND_CREATE (reqwait); 392 X_COND_CREATE (reqwait);
386} 393}
387 394
395static void ecb_cold
388static void etp_atfork_prepare (void) 396etp_atfork_prepare (void)
389{ 397{
390 X_LOCK (wrklock);
391 X_LOCK (reqlock);
392 X_LOCK (reslock);
393#if !HAVE_PREADWRITE
394 X_LOCK (preadwritelock);
395#endif
396} 398}
397 399
400static void ecb_cold
398static void etp_atfork_parent (void) 401etp_atfork_parent (void)
399{ 402{
400#if !HAVE_PREADWRITE
401 X_UNLOCK (preadwritelock);
402#endif
403 X_UNLOCK (reslock);
404 X_UNLOCK (reqlock);
405 X_UNLOCK (wrklock);
406} 403}
407 404
405static void ecb_cold
408static void etp_atfork_child (void) 406etp_atfork_child (void)
409{ 407{
410 ETP_REQ *prv; 408 ETP_REQ *prv;
411 409
412 while ((prv = reqq_shift (&req_queue))) 410 while ((prv = reqq_shift (&req_queue)))
413 ETP_DESTROY (prv); 411 ETP_DESTROY (prv);
433 npending = 0; 431 npending = 0;
434 432
435 etp_thread_init (); 433 etp_thread_init ();
436} 434}
437 435
438static void 436static void ecb_cold
439etp_once_init (void) 437etp_once_init (void)
440{ 438{
441 etp_thread_init (); 439 etp_thread_init ();
442 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); 440 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
443} 441}
444 442
445static int 443static int ecb_cold
446etp_init (void (*want_poll)(void), void (*done_poll)(void)) 444etp_init (void (*want_poll)(void), void (*done_poll)(void))
447{ 445{
448 static pthread_once_t doinit = PTHREAD_ONCE_INIT; 446 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
449 447
450 pthread_once (&doinit, etp_once_init); 448 pthread_once (&doinit, etp_once_init);
455 return 0; 453 return 0;
456} 454}
457 455
458X_THREAD_PROC (etp_proc); 456X_THREAD_PROC (etp_proc);
459 457
458static void ecb_cold
460static void etp_start_thread (void) 459etp_start_thread (void)
461{ 460{
462 etp_worker *wrk = calloc (1, sizeof (etp_worker)); 461 etp_worker *wrk = calloc (1, sizeof (etp_worker));
463 462
464 /*TODO*/ 463 /*TODO*/
465 assert (("unable to allocate worker thread data", wrk)); 464 assert (("unable to allocate worker thread data", wrk));
478 free (wrk); 477 free (wrk);
479 478
480 X_UNLOCK (wrklock); 479 X_UNLOCK (wrklock);
481} 480}
482 481
482static void
483static void etp_maybe_start_thread (void) 483etp_maybe_start_thread (void)
484{ 484{
485 if (expect_true (etp_nthreads () >= wanted)) 485 if (ecb_expect_true (etp_nthreads () >= wanted))
486 return; 486 return;
487 487
488 /* todo: maybe use idle here, but might be less exact */ 488 /* todo: maybe use idle here, but might be less exact */
489 if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) 489 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
490 return; 490 return;
491 491
492 etp_start_thread (); 492 etp_start_thread ();
493} 493}
494 494
495static void ecb_cold
495static void etp_end_thread (void) 496etp_end_thread (void)
496{ 497{
497 eio_req *req = calloc (1, sizeof (eio_req)); 498 eio_req *req = calloc (1, sizeof (eio_req));
498 499
499 req->type = -1; 500 req->type = -1;
500 req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 501 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
507 X_LOCK (wrklock); 508 X_LOCK (wrklock);
508 --started; 509 --started;
509 X_UNLOCK (wrklock); 510 X_UNLOCK (wrklock);
510} 511}
511 512
512static int etp_poll (void) 513static int
514etp_poll (void)
513{ 515{
514 unsigned int maxreqs; 516 unsigned int maxreqs;
515 unsigned int maxtime; 517 unsigned int maxtime;
516 struct timeval tv_start, tv_now; 518 struct timeval tv_start, tv_now;
517 519
547 549
548 X_LOCK (reqlock); 550 X_LOCK (reqlock);
549 --nreqs; 551 --nreqs;
550 X_UNLOCK (reqlock); 552 X_UNLOCK (reqlock);
551 553
552 if (expect_false (req->type == EIO_GROUP && req->size)) 554 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
553 { 555 {
554 req->int1 = 1; /* mark request as delayed */ 556 req->int1 = 1; /* mark request as delayed */
555 continue; 557 continue;
556 } 558 }
557 else 559 else
558 { 560 {
559 int res = ETP_FINISH (req); 561 int res = ETP_FINISH (req);
560 if (expect_false (res)) 562 if (ecb_expect_false (res))
561 return res; 563 return res;
562 } 564 }
563 565
564 if (expect_false (maxreqs && !--maxreqs)) 566 if (ecb_expect_false (maxreqs && !--maxreqs))
565 break; 567 break;
566 568
567 if (maxtime) 569 if (maxtime)
568 { 570 {
569 gettimeofday (&tv_now, 0); 571 gettimeofday (&tv_now, 0);
575 577
576 errno = EAGAIN; 578 errno = EAGAIN;
577 return -1; 579 return -1;
578} 580}
579 581
582static void
580static void etp_cancel (ETP_REQ *req) 583etp_cancel (ETP_REQ *req)
581{ 584{
582 X_LOCK (wrklock); 585 req->cancelled = 1;
583 req->flags |= EIO_FLAG_CANCELLED;
584 X_UNLOCK (wrklock);
585 586
586 eio_grp_cancel (req); 587 eio_grp_cancel (req);
587} 588}
588 589
590static void
589static void etp_submit (ETP_REQ *req) 591etp_submit (ETP_REQ *req)
590{ 592{
591 req->pri -= ETP_PRI_MIN; 593 req->pri -= ETP_PRI_MIN;
592 594
593 if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; 595 if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN;
594 if (expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 596 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
595 597
596 if (expect_false (req->type == EIO_GROUP)) 598 if (ecb_expect_false (req->type == EIO_GROUP))
597 { 599 {
598 /* I hope this is worth it :/ */ 600 /* I hope this is worth it :/ */
599 X_LOCK (reqlock); 601 X_LOCK (reqlock);
600 ++nreqs; 602 ++nreqs;
601 X_UNLOCK (reqlock); 603 X_UNLOCK (reqlock);
620 622
621 etp_maybe_start_thread (); 623 etp_maybe_start_thread ();
622 } 624 }
623} 625}
624 626
627static void ecb_cold
625static void etp_set_max_poll_time (double nseconds) 628etp_set_max_poll_time (double nseconds)
626{ 629{
627 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 630 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
628 max_poll_time = nseconds * EIO_TICKS; 631 max_poll_time = nseconds * EIO_TICKS;
629 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 632 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
630} 633}
631 634
635static void ecb_cold
632static void etp_set_max_poll_reqs (unsigned int maxreqs) 636etp_set_max_poll_reqs (unsigned int maxreqs)
633{ 637{
634 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 638 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
635 max_poll_reqs = maxreqs; 639 max_poll_reqs = maxreqs;
636 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 640 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
637} 641}
638 642
643static void ecb_cold
639static void etp_set_max_idle (unsigned int nthreads) 644etp_set_max_idle (unsigned int nthreads)
640{ 645{
641 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 646 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
642 max_idle = nthreads; 647 max_idle = nthreads;
643 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 648 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
644} 649}
645 650
651static void ecb_cold
646static void etp_set_idle_timeout (unsigned int seconds) 652etp_set_idle_timeout (unsigned int seconds)
647{ 653{
648 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 654 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
649 idle_timeout = seconds; 655 idle_timeout = seconds;
650 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 656 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
651} 657}
652 658
659static void ecb_cold
653static void etp_set_min_parallel (unsigned int nthreads) 660etp_set_min_parallel (unsigned int nthreads)
654{ 661{
655 if (wanted < nthreads) 662 if (wanted < nthreads)
656 wanted = nthreads; 663 wanted = nthreads;
657} 664}
658 665
666static void ecb_cold
659static void etp_set_max_parallel (unsigned int nthreads) 667etp_set_max_parallel (unsigned int nthreads)
660{ 668{
661 if (wanted > nthreads) 669 if (wanted > nthreads)
662 wanted = nthreads; 670 wanted = nthreads;
663 671
664 while (started > wanted) 672 while (started > wanted)
665 etp_end_thread (); 673 etp_end_thread ();
666} 674}
667 675
668/*****************************************************************************/ 676/*****************************************************************************/
669 677
678static void
670static void grp_try_feed (eio_req *grp) 679grp_try_feed (eio_req *grp)
671{ 680{
672 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 681 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
673 { 682 {
674 grp->flags &= ~EIO_FLAG_GROUPADD; 683 grp->flags &= ~EIO_FLAG_GROUPADD;
675 684
682 break; 691 break;
683 } 692 }
684 } 693 }
685} 694}
686 695
696static int
687static int grp_dec (eio_req *grp) 697grp_dec (eio_req *grp)
688{ 698{
689 --grp->size; 699 --grp->size;
690 700
691 /* call feeder, if applicable */ 701 /* call feeder, if applicable */
692 grp_try_feed (grp); 702 grp_try_feed (grp);
696 return eio_finish (grp); 706 return eio_finish (grp);
697 else 707 else
698 return 0; 708 return 0;
699} 709}
700 710
711static void
701void eio_destroy (eio_req *req) 712eio_destroy (eio_req *req)
702{ 713{
703 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); 714 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
704 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); 715 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
705 716
706 EIO_DESTROY (req); 717 EIO_DESTROY (req);
707} 718}
708 719
720static int
709static int eio_finish (eio_req *req) 721eio_finish (eio_req *req)
710{ 722{
711 int res = EIO_FINISH (req); 723 int res = EIO_FINISH (req);
712 724
713 if (req->grp) 725 if (req->grp)
714 { 726 {
722 if (grp->grp_first == req) 734 if (grp->grp_first == req)
723 grp->grp_first = req->grp_next; 735 grp->grp_first = req->grp_next;
724 736
725 res2 = grp_dec (grp); 737 res2 = grp_dec (grp);
726 738
727 if (!res && res2) 739 if (!res)
728 res = res2; 740 res = res2;
729 } 741 }
730 742
731 eio_destroy (req); 743 eio_destroy (req);
732 744
733 return res; 745 return res;
734} 746}
735 747
748void
736void eio_grp_cancel (eio_req *grp) 749eio_grp_cancel (eio_req *grp)
737{ 750{
738 for (grp = grp->grp_first; grp; grp = grp->grp_next) 751 for (grp = grp->grp_first; grp; grp = grp->grp_next)
739 eio_cancel (grp); 752 eio_cancel (grp);
740} 753}
741 754
755void
742void eio_cancel (eio_req *req) 756eio_cancel (eio_req *req)
743{ 757{
744 etp_cancel (req); 758 etp_cancel (req);
745} 759}
746 760
761void
747void eio_submit (eio_req *req) 762eio_submit (eio_req *req)
748{ 763{
749 etp_submit (req); 764 etp_submit (req);
750} 765}
751 766
752unsigned int eio_nreqs (void) 767unsigned int
768eio_nreqs (void)
753{ 769{
754 return etp_nreqs (); 770 return etp_nreqs ();
755} 771}
756 772
757unsigned int eio_nready (void) 773unsigned int
774eio_nready (void)
758{ 775{
759 return etp_nready (); 776 return etp_nready ();
760} 777}
761 778
762unsigned int eio_npending (void) 779unsigned int
780eio_npending (void)
763{ 781{
764 return etp_npending (); 782 return etp_npending ();
765} 783}
766 784
767unsigned int eio_nthreads (void) 785unsigned int ecb_cold
786eio_nthreads (void)
768{ 787{
769 return etp_nthreads (); 788 return etp_nthreads ();
770} 789}
771 790
791void ecb_cold
772void eio_set_max_poll_time (double nseconds) 792eio_set_max_poll_time (double nseconds)
773{ 793{
774 etp_set_max_poll_time (nseconds); 794 etp_set_max_poll_time (nseconds);
775} 795}
776 796
797void ecb_cold
777void eio_set_max_poll_reqs (unsigned int maxreqs) 798eio_set_max_poll_reqs (unsigned int maxreqs)
778{ 799{
779 etp_set_max_poll_reqs (maxreqs); 800 etp_set_max_poll_reqs (maxreqs);
780} 801}
781 802
803void ecb_cold
782void eio_set_max_idle (unsigned int nthreads) 804eio_set_max_idle (unsigned int nthreads)
783{ 805{
784 etp_set_max_idle (nthreads); 806 etp_set_max_idle (nthreads);
785} 807}
786 808
809void ecb_cold
787void eio_set_idle_timeout (unsigned int seconds) 810eio_set_idle_timeout (unsigned int seconds)
788{ 811{
789 etp_set_idle_timeout (seconds); 812 etp_set_idle_timeout (seconds);
790} 813}
791 814
815void ecb_cold
792void eio_set_min_parallel (unsigned int nthreads) 816eio_set_min_parallel (unsigned int nthreads)
793{ 817{
794 etp_set_min_parallel (nthreads); 818 etp_set_min_parallel (nthreads);
795} 819}
796 820
821void ecb_cold
797void eio_set_max_parallel (unsigned int nthreads) 822eio_set_max_parallel (unsigned int nthreads)
798{ 823{
799 etp_set_max_parallel (nthreads); 824 etp_set_max_parallel (nthreads);
800} 825}
801 826
802int eio_poll (void) 827int eio_poll (void)
872#ifndef HAVE_FUTIMES 897#ifndef HAVE_FUTIMES
873 898
874# undef futimes 899# undef futimes
875# define futimes(fd,times) eio__futimes (fd, times) 900# define futimes(fd,times) eio__futimes (fd, times)
876 901
902static int
877static int eio__futimes (int fd, const struct timeval tv[2]) 903eio__futimes (int fd, const struct timeval tv[2])
878{ 904{
879 errno = ENOSYS; 905 errno = ENOSYS;
880 return -1; 906 return -1;
881} 907}
882 908
886# undef fdatasync 912# undef fdatasync
887# define fdatasync(fd) fsync (fd) 913# define fdatasync(fd) fsync (fd)
888#endif 914#endif
889 915
890/* sync_file_range always needs emulation */ 916/* sync_file_range always needs emulation */
891int 917static int
892eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) 918eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags)
893{ 919{
894#if HAVE_SYNC_FILE_RANGE 920#if HAVE_SYNC_FILE_RANGE
895 int res; 921 int res;
896 922
913 /* even though we could play tricks with the flags, it's better to always 939 /* even though we could play tricks with the flags, it's better to always
914 * call fdatasync, as that matches the expectation of its users best */ 940 * call fdatasync, as that matches the expectation of its users best */
915 return fdatasync (fd); 941 return fdatasync (fd);
916} 942}
917 943
944static int
945eio__fallocate (int fd, int mode, off_t offset, size_t len)
946{
947#if HAVE_FALLOCATE
948 return fallocate (fd, mode, offset, len);
949#else
950 errno = ENOSYS;
951 return -1;
952#endif
953}
954
918#if !HAVE_READAHEAD 955#if !HAVE_READAHEAD
919# undef readahead 956# undef readahead
920# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) 957# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self)
921 958
922static ssize_t 959static ssize_t
942 979
943/* sendfile always needs emulation */ 980/* sendfile always needs emulation */
944static ssize_t 981static ssize_t
945eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self) 982eio__sendfile (int ofd, int ifd, off_t offset, size_t count, etp_worker *self)
946{ 983{
984 ssize_t written = 0;
947 ssize_t res; 985 ssize_t res;
948 986
949 if (!count) 987 if (!count)
950 return 0; 988 return 0;
951 989
990 for (;;)
991 {
992#ifdef __APPLE__
993# undef HAVE_SENDFILE /* broken, as everything on os x */
994#endif
952#if HAVE_SENDFILE 995#if HAVE_SENDFILE
953# if __linux 996# if __linux
997 off_t soffset = offset;
954 res = sendfile (ofd, ifd, &offset, count); 998 res = sendfile (ofd, ifd, &soffset, count);
955 999
956# elif __FreeBSD__ 1000# elif __FreeBSD__
957 /* 1001 /*
958 * Of course, the freebsd sendfile is a dire hack with no thoughts 1002 * Of course, the freebsd sendfile is a dire hack with no thoughts
959 * wasted on making it similar to other I/O functions. 1003 * wasted on making it similar to other I/O functions.
960 */ 1004 */
961 {
962 off_t sbytes; 1005 off_t sbytes;
963 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 1006 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
964 1007
965 #if 0 /* according to the manpage, this is correct, but broken behaviour */ 1008 #if 0 /* according to the manpage, this is correct, but broken behaviour */
966 /* freebsd' sendfile will return 0 on success */ 1009 /* freebsd' sendfile will return 0 on success */
967 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */ 1010 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */
968 /* not on e.g. EIO or EPIPE - sounds broken */ 1011 /* not on e.g. EIO or EPIPE - sounds broken */
969 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) 1012 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0)
970 res = sbytes; 1013 res = sbytes;
971 #endif 1014 #endif
972 1015
973 /* according to source inspection, this is correct, and useful behaviour */ 1016 /* according to source inspection, this is correct, and useful behaviour */
974 if (sbytes) 1017 if (sbytes)
975 res = sbytes; 1018 res = sbytes;
976 }
977 1019
978# elif defined (__APPLE__) 1020# elif defined (__APPLE__)
979
980 {
981 off_t sbytes = count; 1021 off_t sbytes = count;
982 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 1022 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
983 1023
984 /* according to the manpage, sbytes is always valid */ 1024 /* according to the manpage, sbytes is always valid */
985 if (sbytes) 1025 if (sbytes)
986 res = sbytes; 1026 res = sbytes;
987 }
988 1027
989# elif __hpux 1028# elif __hpux
990 res = sendfile (ofd, ifd, offset, count, 0, 0); 1029 res = sendfile (ofd, ifd, offset, count, 0, 0);
991 1030
992# elif __solaris 1031# elif __solaris
993 {
994 struct sendfilevec vec; 1032 struct sendfilevec vec;
995 size_t sbytes; 1033 size_t sbytes;
996 1034
997 vec.sfv_fd = ifd; 1035 vec.sfv_fd = ifd;
998 vec.sfv_flag = 0; 1036 vec.sfv_flag = 0;
999 vec.sfv_off = offset; 1037 vec.sfv_off = offset;
1000 vec.sfv_len = count; 1038 vec.sfv_len = count;
1001 1039
1002 res = sendfilev (ofd, &vec, 1, &sbytes); 1040 res = sendfilev (ofd, &vec, 1, &sbytes);
1003 1041
1004 if (res < 0 && sbytes) 1042 if (res < 0 && sbytes)
1005 res = sbytes; 1043 res = sbytes;
1006 }
1007 1044
1008# endif 1045# endif
1009 1046
1010#elif defined (_WIN32) 1047#elif defined (_WIN32)
1011
1012 /* does not work, just for documentation of what would need to be done */ 1048 /* does not work, just for documentation of what would need to be done */
1013 { 1049 /* actually, cannot be done like this, as TransmitFile changes the file offset, */
1050 /* libeio guarantees that the file offset does not change, and windows */
1051 /* has no way to get an independent handle to the same file description */
1014 HANDLE h = TO_SOCKET (ifd); 1052 HANDLE h = TO_SOCKET (ifd);
1015 SetFilePointer (h, offset, 0, FILE_BEGIN); 1053 SetFilePointer (h, offset, 0, FILE_BEGIN);
1016 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 1054 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1017 }
1018 1055
1019#else 1056#else
1020 res = -1; 1057 res = -1;
1021 errno = ENOSYS; 1058 errno = ENOSYS;
1022#endif 1059#endif
1023 1060
1061 /* we assume sendfile can copy at least 128mb in one go */
1062 if (res <= 128 * 1024 * 1024)
1063 {
1064 if (res > 0)
1065 written += res;
1066
1067 if (written)
1068 return written;
1069
1070 break;
1071 }
1072 else
1073 {
1074 /* if we requested more, then probably the kernel was lazy */
1075 written += res;
1076 offset += res;
1077 count -= res;
1078
1079 if (!count)
1080 return written;
1081 }
1082 }
1083
1024 if (res < 0 1084 if (res < 0
1025 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1085 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1026 /* BSDs */ 1086 /* BSDs */
1027#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1087#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1028 || errno == ENOTSUP 1088 || errno == ENOTSUP
1029#endif 1089#endif
1064 count -= cnt; 1124 count -= cnt;
1065 } 1125 }
1066 } 1126 }
1067 1127
1068 return res; 1128 return res;
1129}
1130
1131#ifdef PAGESIZE
1132# define eio_pagesize() PAGESIZE
1133#else
1134static intptr_t
1135eio_pagesize (void)
1136{
1137 static intptr_t page;
1138
1139 if (!page)
1140 page = sysconf (_SC_PAGESIZE);
1141
1142 return page;
1143}
1144#endif
1145
1146static void
1147eio_page_align (void **addr, size_t *length)
1148{
1149 intptr_t mask = eio_pagesize () - 1;
1150
1151 /* round down addr */
1152 intptr_t adj = mask & (intptr_t)*addr;
1153
1154 *addr = (void *)((intptr_t)*addr - adj);
1155 *length += adj;
1156
1157 /* round up length */
1158 *length = (*length + mask) & ~mask;
1159}
1160
1161#if !_POSIX_MEMLOCK
1162# define eio__mlockall(a) ((errno = ENOSYS), -1)
1163#else
1164
1165static int
1166eio__mlockall (int flags)
1167{
1168 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1169 extern int mallopt (int, int);
1170 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1171 #endif
1172
1173 if (EIO_MCL_CURRENT != MCL_CURRENT
1174 || EIO_MCL_FUTURE != MCL_FUTURE)
1175 {
1176 flags = 0
1177 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1178 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1179 }
1180
1181 return mlockall (flags);
1182}
1183#endif
1184
1185#if !_POSIX_MEMLOCK_RANGE
1186# define eio__mlock(a,b) ((errno = ENOSYS), -1)
1187#else
1188
1189static int
1190eio__mlock (void *addr, size_t length)
1191{
1192 eio_page_align (&addr, &length);
1193
1194 return mlock (addr, length);
1195}
1196
1197#endif
1198
1199#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1200# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1201#else
1202
1203static int
1204eio__msync (void *mem, size_t len, int flags)
1205{
1206 eio_page_align (&mem, &len);
1207
1208 if (EIO_MS_ASYNC != MS_SYNC
1209 || EIO_MS_INVALIDATE != MS_INVALIDATE
1210 || EIO_MS_SYNC != MS_SYNC)
1211 {
1212 flags = 0
1213 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1214 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1215 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1216 }
1217
1218 return msync (mem, len, flags);
1219}
1220
1221#endif
1222
1223static int
1224eio__mtouch (eio_req *req)
1225{
1226 void *mem = req->ptr2;
1227 size_t len = req->size;
1228 int flags = req->int1;
1229
1230 eio_page_align (&mem, &len);
1231
1232 {
1233 intptr_t addr = (intptr_t)mem;
1234 intptr_t end = addr + len;
1235 intptr_t page = eio_pagesize ();
1236
1237 if (addr < end)
1238 if (flags & EIO_MT_MODIFY) /* modify */
1239 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1240 else
1241 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1242 }
1243
1244 return 0;
1245}
1246
1247/*****************************************************************************/
1248/* requests implemented outside eio_execute, because they are so large */
1249
1250static void
1251eio__realpath (eio_req *req, etp_worker *self)
1252{
1253 char *rel = req->ptr1;
1254 char *res;
1255 char *tmp1, *tmp2;
1256#if SYMLOOP_MAX > 32
1257 int symlinks = SYMLOOP_MAX;
1258#else
1259 int symlinks = 32;
1260#endif
1261
1262 req->result = -1;
1263
1264 errno = EINVAL;
1265 if (!rel)
1266 return;
1267
1268 errno = ENOENT;
1269 if (!*rel)
1270 return;
1271
1272 if (!req->ptr2)
1273 {
1274 X_LOCK (wrklock);
1275 req->flags |= EIO_FLAG_PTR2_FREE;
1276 X_UNLOCK (wrklock);
1277 req->ptr2 = malloc (PATH_MAX * 3);
1278
1279 errno = ENOMEM;
1280 if (!req->ptr2)
1281 return;
1282 }
1283
1284 res = req->ptr2;
1285 tmp1 = res + PATH_MAX;
1286 tmp2 = tmp1 + PATH_MAX;
1287
1288#if 0 /* disabled, the musl way to do things is just too racy */
1289#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1290 /* on linux we may be able to ask the kernel */
1291 {
1292 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1293
1294 if (fd >= 0)
1295 {
1296 sprintf (tmp1, "/proc/self/fd/%d", fd);
1297 req->result = readlink (tmp1, res, PATH_MAX);
1298 close (fd);
1299
1300 /* here we should probably stat the open file and the disk file, to make sure they still match */
1301
1302 if (req->result > 0)
1303 goto done;
1304 }
1305 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1306 return;
1307 }
1308#endif
1309#endif
1310
1311 if (*rel != '/')
1312 {
1313 if (!getcwd (res, PATH_MAX))
1314 return;
1315
1316 if (res [1]) /* only use if not / */
1317 res += strlen (res);
1318 }
1319
1320 while (*rel)
1321 {
1322 ssize_t len, linklen;
1323 char *beg = rel;
1324
1325 while (*rel && *rel != '/')
1326 ++rel;
1327
1328 len = rel - beg;
1329
1330 if (!len) /* skip slashes */
1331 {
1332 ++rel;
1333 continue;
1334 }
1335
1336 if (beg [0] == '.')
1337 {
1338 if (len == 1)
1339 continue; /* . - nop */
1340
1341 if (beg [1] == '.' && len == 2)
1342 {
1343 /* .. - back up one component, if possible */
1344
1345 while (res != req->ptr2)
1346 if (*--res == '/')
1347 break;
1348
1349 continue;
1350 }
1351 }
1352
1353 errno = ENAMETOOLONG;
1354 if (res + 1 + len + 1 >= tmp1)
1355 return;
1356
1357 /* copy one component */
1358 *res = '/';
1359 memcpy (res + 1, beg, len);
1360
1361 /* zero-terminate, for readlink */
1362 res [len + 1] = 0;
1363
1364 /* now check if it's a symlink */
1365 linklen = readlink (req->ptr2, tmp1, PATH_MAX);
1366
1367 if (linklen < 0)
1368 {
1369 if (errno != EINVAL)
1370 return;
1371
1372 /* it's a normal directory. hopefully */
1373 res += len + 1;
1374 }
1375 else
1376 {
1377 /* yay, it was a symlink - build new path in tmp2 */
1378 int rellen = strlen (rel);
1379
1380 errno = ENAMETOOLONG;
1381 if (linklen + 1 + rellen >= PATH_MAX)
1382 return;
1383
1384 errno = ELOOP;
1385 if (!--symlinks)
1386 return;
1387
1388 if (*tmp1 == '/')
1389 res = req->ptr2; /* symlink resolves to an absolute path */
1390
1391 /* we need to be careful, as rel might point into tmp2 already */
1392 memmove (tmp2 + linklen + 1, rel, rellen + 1);
1393 tmp2 [linklen] = '/';
1394 memcpy (tmp2, tmp1, linklen);
1395
1396 rel = tmp2;
1397 }
1398 }
1399
1400 /* special case for the lone root path */
1401 if (res == req->ptr2)
1402 *res++ = '/';
1403
1404 req->result = res - (char *)req->ptr2;
1405
1406done:
1407 req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */
1069} 1408}
1070 1409
1071static signed char 1410static signed char
1072eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1411eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1073{ 1412{
1255 1594
1256 X_LOCK (wrklock); 1595 X_LOCK (wrklock);
1257 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1596 /* the corresponding closedir is in ETP_WORKER_CLEAR */
1258 self->dirp = dirp = opendir (req->ptr1); 1597 self->dirp = dirp = opendir (req->ptr1);
1259 1598
1599 if (req->flags & EIO_FLAG_PTR1_FREE)
1600 free (req->ptr1);
1601
1260 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1602 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1261 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1603 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1262 req->ptr2 = names = malloc (namesalloc); 1604 req->ptr2 = names = malloc (namesalloc);
1263 X_UNLOCK (wrklock); 1605 X_UNLOCK (wrklock);
1264 1606
1315 /* skip . and .. entries */ 1657 /* skip . and .. entries */
1316 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1658 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1317 { 1659 {
1318 int len = D_NAMLEN (entp) + 1; 1660 int len = D_NAMLEN (entp) + 1;
1319 1661
1320 while (expect_false (namesoffs + len > namesalloc)) 1662 while (ecb_expect_false (namesoffs + len > namesalloc))
1321 { 1663 {
1322 namesalloc *= 2; 1664 namesalloc *= 2;
1323 X_LOCK (wrklock); 1665 X_LOCK (wrklock);
1324 req->ptr2 = names = realloc (names, namesalloc); 1666 req->ptr2 = names = realloc (names, namesalloc);
1325 X_UNLOCK (wrklock); 1667 X_UNLOCK (wrklock);
1332 1674
1333 if (dents) 1675 if (dents)
1334 { 1676 {
1335 struct eio_dirent *ent; 1677 struct eio_dirent *ent;
1336 1678
1337 if (expect_false (dentoffs == dentalloc)) 1679 if (ecb_expect_false (dentoffs == dentalloc))
1338 { 1680 {
1339 dentalloc *= 2; 1681 dentalloc *= 2;
1340 X_LOCK (wrklock); 1682 X_LOCK (wrklock);
1341 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1683 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1342 X_UNLOCK (wrklock); 1684 X_UNLOCK (wrklock);
1430 break; 1772 break;
1431 } 1773 }
1432 } 1774 }
1433} 1775}
1434 1776
1435#ifdef PAGESIZE
1436# define eio_pagesize() PAGESIZE
1437#else
1438static intptr_t
1439eio_pagesize (void)
1440{
1441 static intptr_t page;
1442
1443 if (!page)
1444 page = sysconf (_SC_PAGESIZE);
1445
1446 return page;
1447}
1448#endif
1449
1450static void
1451eio_page_align (void **addr, size_t *length)
1452{
1453 intptr_t mask = eio_pagesize () - 1;
1454
1455 /* round down addr */
1456 intptr_t adj = mask & (intptr_t)*addr;
1457
1458 *addr = (void *)((intptr_t)*addr - adj);
1459 *length += adj;
1460
1461 /* round up length */
1462 *length = (*length + mask) & ~mask;
1463}
1464
1465#if !_POSIX_MEMLOCK
1466# define eio__mlockall(a) ((errno = ENOSYS), -1)
1467#else
1468
1469static int
1470eio__mlockall (int flags)
1471{
1472 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1473 extern int mallopt (int, int);
1474 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1475 #endif
1476
1477 if (EIO_MCL_CURRENT != MCL_CURRENT
1478 || EIO_MCL_FUTURE != MCL_FUTURE)
1479 {
1480 flags = 0
1481 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1482 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1483 }
1484
1485 return mlockall (flags);
1486}
1487#endif
1488
1489#if !_POSIX_MEMLOCK_RANGE
1490# define eio__mlock(a,b) ((errno = ENOSYS), -1)
1491#else
1492
1493static int
1494eio__mlock (void *addr, size_t length)
1495{
1496 eio_page_align (&addr, &length);
1497
1498 return mlock (addr, length);
1499}
1500
1501#endif
1502
1503#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1504# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1505#else
1506
1507int
1508eio__msync (void *mem, size_t len, int flags)
1509{
1510 eio_page_align (&mem, &len);
1511
1512 if (EIO_MS_ASYNC != MS_SYNC
1513 || EIO_MS_INVALIDATE != MS_INVALIDATE
1514 || EIO_MS_SYNC != MS_SYNC)
1515 {
1516 flags = 0
1517 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1518 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1519 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1520 }
1521
1522 return msync (mem, len, flags);
1523}
1524
1525#endif
1526
1527int
1528eio__mtouch (void *mem, size_t len, int flags)
1529{
1530 eio_page_align (&mem, &len);
1531
1532 {
1533 intptr_t addr = (intptr_t)mem;
1534 intptr_t end = addr + len;
1535 intptr_t page = eio_pagesize ();
1536
1537 if (addr < end)
1538 if (flags & EIO_MT_MODIFY) /* modify */
1539 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len);
1540 else
1541 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len);
1542 }
1543
1544 return 0;
1545}
1546
1547/*****************************************************************************/ 1777/*****************************************************************************/
1548 1778
1549#define ALLOC(len) \ 1779#define ALLOC(len) \
1550 if (!req->ptr2) \ 1780 if (!req->ptr2) \
1551 { \ 1781 { \
1564X_THREAD_PROC (etp_proc) 1794X_THREAD_PROC (etp_proc)
1565{ 1795{
1566 ETP_REQ *req; 1796 ETP_REQ *req;
1567 struct timespec ts; 1797 struct timespec ts;
1568 etp_worker *self = (etp_worker *)thr_arg; 1798 etp_worker *self = (etp_worker *)thr_arg;
1799 int timeout;
1569 1800
1570 /* try to distribute timeouts somewhat randomly */ 1801 /* try to distribute timeouts somewhat evenly */
1571 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); 1802 ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL);
1572 1803
1573 for (;;) 1804 for (;;)
1574 { 1805 {
1806 ts.tv_sec = 0;
1807
1575 X_LOCK (reqlock); 1808 X_LOCK (reqlock);
1576 1809
1577 for (;;) 1810 for (;;)
1578 { 1811 {
1579 self->req = req = reqq_shift (&req_queue); 1812 self->req = req = reqq_shift (&req_queue);
1580 1813
1581 if (req) 1814 if (req)
1582 break; 1815 break;
1583 1816
1817 if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */
1818 {
1819 X_UNLOCK (reqlock);
1820 X_LOCK (wrklock);
1821 --started;
1822 X_UNLOCK (wrklock);
1823 goto quit;
1824 }
1825
1584 ++idle; 1826 ++idle;
1585 1827
1586 ts.tv_sec = time (0) + idle_timeout; 1828 if (idle <= max_idle)
1587 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) 1829 /* we are allowed to idle, so do so without any timeout */
1830 X_COND_WAIT (reqwait, reqlock);
1831 else
1588 { 1832 {
1589 if (idle > max_idle) 1833 /* initialise timeout once */
1590 { 1834 if (!ts.tv_sec)
1591 --idle; 1835 ts.tv_sec = time (0) + idle_timeout;
1592 X_UNLOCK (reqlock);
1593 X_LOCK (wrklock);
1594 --started;
1595 X_UNLOCK (wrklock);
1596 goto quit;
1597 }
1598 1836
1599 /* we are allowed to idle, so do so without any timeout */
1600 X_COND_WAIT (reqwait, reqlock); 1837 if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT)
1838 ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */
1601 } 1839 }
1602 1840
1603 --idle; 1841 --idle;
1604 } 1842 }
1605 1843
1608 X_UNLOCK (reqlock); 1846 X_UNLOCK (reqlock);
1609 1847
1610 if (req->type < 0) 1848 if (req->type < 0)
1611 goto quit; 1849 goto quit;
1612 1850
1613 if (!EIO_CANCELLED (req))
1614 ETP_EXECUTE (self, req); 1851 ETP_EXECUTE (self, req);
1615 1852
1616 X_LOCK (reslock); 1853 X_LOCK (reslock);
1617 1854
1618 ++npending; 1855 ++npending;
1619 1856
1634 return 0; 1871 return 0;
1635} 1872}
1636 1873
1637/*****************************************************************************/ 1874/*****************************************************************************/
1638 1875
1876int ecb_cold
1639int eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1877eio_init (void (*want_poll)(void), void (*done_poll)(void))
1640{ 1878{
1641 return etp_init (want_poll, done_poll); 1879 return etp_init (want_poll, done_poll);
1642} 1880}
1643 1881
1882ecb_inline void
1644static void eio_api_destroy (eio_req *req) 1883eio_api_destroy (eio_req *req)
1645{ 1884{
1646 free (req); 1885 free (req);
1647} 1886}
1648 1887
1649#define REQ(rtype) \ 1888#define REQ(rtype) \
1668 { \ 1907 { \
1669 eio_api_destroy (req); \ 1908 eio_api_destroy (req); \
1670 return 0; \ 1909 return 0; \
1671 } 1910 }
1672 1911
1912static void
1673static void eio_execute (etp_worker *self, eio_req *req) 1913eio_execute (etp_worker *self, eio_req *req)
1674{ 1914{
1915 if (ecb_expect_false (EIO_CANCELLED (req)))
1916 {
1917 req->result = -1;
1918 req->errorno = ECANCELED;
1919 return;
1920 }
1921
1675 switch (req->type) 1922 switch (req->type)
1676 { 1923 {
1677 case EIO_READ: ALLOC (req->size); 1924 case EIO_READ: ALLOC (req->size);
1678 req->result = req->offs >= 0 1925 req->result = req->offs >= 0
1679 ? pread (req->int1, req->ptr2, req->size, req->offs) 1926 ? pread (req->int1, req->ptr2, req->size, req->offs)
1713 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; 1960 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1714 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; 1961 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1715 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; 1962 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break;
1716 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; 1963 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
1717 1964
1965 case EIO_REALPATH: eio__realpath (req, self); break;
1966
1718 case EIO_READLINK: ALLOC (PATH_MAX); 1967 case EIO_READLINK: ALLOC (PATH_MAX);
1719 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; 1968 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break;
1720 1969
1721 case EIO_SYNC: req->result = 0; sync (); break; 1970 case EIO_SYNC: req->result = 0; sync (); break;
1722 case EIO_FSYNC: req->result = fsync (req->int1); break; 1971 case EIO_FSYNC: req->result = fsync (req->int1); break;
1723 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 1972 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1724 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 1973 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break;
1725 case EIO_MTOUCH: req->result = eio__mtouch (req->ptr2, req->size, req->int1); break; 1974 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1726 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 1975 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1727 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 1976 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break;
1728 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; 1977 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
1978 case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break;
1729 1979
1730 case EIO_READDIR: eio__scandir (req, self); break; 1980 case EIO_READDIR: eio__scandir (req, self); break;
1731 1981
1732 case EIO_BUSY: 1982 case EIO_BUSY:
1733#ifdef _WIN32 1983#ifdef _WIN32
1774 case EIO_NOP: 2024 case EIO_NOP:
1775 req->result = 0; 2025 req->result = 0;
1776 break; 2026 break;
1777 2027
1778 case EIO_CUSTOM: 2028 case EIO_CUSTOM:
1779 ((void (*)(eio_req *))req->feed) (req); 2029 req->feed (req);
1780 break; 2030 break;
1781 2031
1782 default: 2032 default:
1783 errno = ENOSYS; 2033 errno = ENOSYS;
1784 req->result = -1; 2034 req->result = -1;
1833eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) 2083eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data)
1834{ 2084{
1835 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; 2085 REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND;
1836} 2086}
1837 2087
2088eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data)
2089{
2090 REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND;
2091}
2092
1838eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) 2093eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data)
1839{ 2094{
1840 REQ (EIO_FDATASYNC); req->int1 = fd; SEND; 2095 REQ (EIO_FDATASYNC); req->int1 = fd; SEND;
1841} 2096}
1842 2097
1937} 2192}
1938 2193
1939eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) 2194eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data)
1940{ 2195{
1941 return eio__1path (EIO_READLINK, path, pri, cb, data); 2196 return eio__1path (EIO_READLINK, path, pri, cb, data);
2197}
2198
2199eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data)
2200{
2201 return eio__1path (EIO_REALPATH, path, pri, cb, data);
1942} 2202}
1943 2203
1944eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) 2204eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data)
1945{ 2205{
1946 return eio__1path (EIO_STAT, path, pri, cb, data); 2206 return eio__1path (EIO_STAT, path, pri, cb, data);
2005eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) 2265eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2006{ 2266{
2007 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2267 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
2008} 2268}
2009 2269
2010eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) 2270eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
2011{ 2271{
2012 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; 2272 REQ (EIO_CUSTOM); req->feed = execute; SEND;
2013} 2273}
2014 2274
2015#endif 2275#endif
2016 2276
2017eio_req *eio_grp (eio_cb cb, void *data) 2277eio_req *eio_grp (eio_cb cb, void *data)
2026#undef SEND 2286#undef SEND
2027 2287
2028/*****************************************************************************/ 2288/*****************************************************************************/
2029/* grp functions */ 2289/* grp functions */
2030 2290
2291void
2031void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) 2292eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
2032{ 2293{
2033 grp->int2 = limit; 2294 grp->int2 = limit;
2034 grp->feed = feed; 2295 grp->feed = feed;
2035 2296
2036 grp_try_feed (grp); 2297 grp_try_feed (grp);
2037} 2298}
2038 2299
2300void
2039void eio_grp_limit (eio_req *grp, int limit) 2301eio_grp_limit (eio_req *grp, int limit)
2040{ 2302{
2041 grp->int2 = limit; 2303 grp->int2 = limit;
2042 2304
2043 grp_try_feed (grp); 2305 grp_try_feed (grp);
2044} 2306}
2045 2307
2308void
2046void eio_grp_add (eio_req *grp, eio_req *req) 2309eio_grp_add (eio_req *grp, eio_req *req)
2047{ 2310{
2048 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2311 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2049 2312
2050 grp->flags |= EIO_FLAG_GROUPADD; 2313 grp->flags |= EIO_FLAG_GROUPADD;
2051 2314
2062} 2325}
2063 2326
2064/*****************************************************************************/ 2327/*****************************************************************************/
2065/* misc garbage */ 2328/* misc garbage */
2066 2329
2330ssize_t
2067ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2331eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2068{ 2332{
2069 etp_worker wrk; 2333 etp_worker wrk;
2070 ssize_t ret; 2334 ssize_t ret;
2071 2335
2072 wrk.dbuf = 0; 2336 wrk.dbuf = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines