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.80 by root, Tue Jul 5 18:59:28 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{
382 X_MUTEX_CREATE (wrklock); 386 X_MUTEX_CREATE (wrklock);
383 X_MUTEX_CREATE (reslock); 387 X_MUTEX_CREATE (reslock);
384 X_MUTEX_CREATE (reqlock); 388 X_MUTEX_CREATE (reqlock);
385 X_COND_CREATE (reqwait); 389 X_COND_CREATE (reqwait);
386} 390}
387 391
392static void ecb_cold
388static void etp_atfork_prepare (void) 393etp_atfork_prepare (void)
389{ 394{
390 X_LOCK (wrklock); 395 X_LOCK (wrklock);
391 X_LOCK (reqlock); 396 X_LOCK (reqlock);
392 X_LOCK (reslock); 397 X_LOCK (reslock);
393#if !HAVE_PREADWRITE 398#if !HAVE_PREADWRITE
394 X_LOCK (preadwritelock); 399 X_LOCK (preadwritelock);
395#endif 400#endif
396} 401}
397 402
403static void ecb_cold
398static void etp_atfork_parent (void) 404etp_atfork_parent (void)
399{ 405{
400#if !HAVE_PREADWRITE 406#if !HAVE_PREADWRITE
401 X_UNLOCK (preadwritelock); 407 X_UNLOCK (preadwritelock);
402#endif 408#endif
403 X_UNLOCK (reslock); 409 X_UNLOCK (reslock);
404 X_UNLOCK (reqlock); 410 X_UNLOCK (reqlock);
405 X_UNLOCK (wrklock); 411 X_UNLOCK (wrklock);
406} 412}
407 413
414static void ecb_cold
408static void etp_atfork_child (void) 415etp_atfork_child (void)
409{ 416{
410 ETP_REQ *prv; 417 ETP_REQ *prv;
411 418
412 while ((prv = reqq_shift (&req_queue))) 419 while ((prv = reqq_shift (&req_queue)))
413 ETP_DESTROY (prv); 420 ETP_DESTROY (prv);
433 npending = 0; 440 npending = 0;
434 441
435 etp_thread_init (); 442 etp_thread_init ();
436} 443}
437 444
438static void 445static void ecb_cold
439etp_once_init (void) 446etp_once_init (void)
440{ 447{
441 etp_thread_init (); 448 etp_thread_init ();
442 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child); 449 X_THREAD_ATFORK (etp_atfork_prepare, etp_atfork_parent, etp_atfork_child);
443} 450}
444 451
445static int 452static int ecb_cold
446etp_init (void (*want_poll)(void), void (*done_poll)(void)) 453etp_init (void (*want_poll)(void), void (*done_poll)(void))
447{ 454{
448 static pthread_once_t doinit = PTHREAD_ONCE_INIT; 455 static pthread_once_t doinit = PTHREAD_ONCE_INIT;
449 456
450 pthread_once (&doinit, etp_once_init); 457 pthread_once (&doinit, etp_once_init);
455 return 0; 462 return 0;
456} 463}
457 464
458X_THREAD_PROC (etp_proc); 465X_THREAD_PROC (etp_proc);
459 466
467static void ecb_cold
460static void etp_start_thread (void) 468etp_start_thread (void)
461{ 469{
462 etp_worker *wrk = calloc (1, sizeof (etp_worker)); 470 etp_worker *wrk = calloc (1, sizeof (etp_worker));
463 471
464 /*TODO*/ 472 /*TODO*/
465 assert (("unable to allocate worker thread data", wrk)); 473 assert (("unable to allocate worker thread data", wrk));
478 free (wrk); 486 free (wrk);
479 487
480 X_UNLOCK (wrklock); 488 X_UNLOCK (wrklock);
481} 489}
482 490
491static void
483static void etp_maybe_start_thread (void) 492etp_maybe_start_thread (void)
484{ 493{
485 if (expect_true (etp_nthreads () >= wanted)) 494 if (ecb_expect_true (etp_nthreads () >= wanted))
486 return; 495 return;
487 496
488 /* todo: maybe use idle here, but might be less exact */ 497 /* todo: maybe use idle here, but might be less exact */
489 if (expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) 498 if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ()))
490 return; 499 return;
491 500
492 etp_start_thread (); 501 etp_start_thread ();
493} 502}
494 503
504static void ecb_cold
495static void etp_end_thread (void) 505etp_end_thread (void)
496{ 506{
497 eio_req *req = calloc (1, sizeof (eio_req)); 507 eio_req *req = calloc (1, sizeof (eio_req));
498 508
499 req->type = -1; 509 req->type = -1;
500 req->pri = ETP_PRI_MAX - ETP_PRI_MIN; 510 req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
507 X_LOCK (wrklock); 517 X_LOCK (wrklock);
508 --started; 518 --started;
509 X_UNLOCK (wrklock); 519 X_UNLOCK (wrklock);
510} 520}
511 521
512static int etp_poll (void) 522static int
523etp_poll (void)
513{ 524{
514 unsigned int maxreqs; 525 unsigned int maxreqs;
515 unsigned int maxtime; 526 unsigned int maxtime;
516 struct timeval tv_start, tv_now; 527 struct timeval tv_start, tv_now;
517 528
547 558
548 X_LOCK (reqlock); 559 X_LOCK (reqlock);
549 --nreqs; 560 --nreqs;
550 X_UNLOCK (reqlock); 561 X_UNLOCK (reqlock);
551 562
552 if (expect_false (req->type == EIO_GROUP && req->size)) 563 if (ecb_expect_false (req->type == EIO_GROUP && req->size))
553 { 564 {
554 req->int1 = 1; /* mark request as delayed */ 565 req->int1 = 1; /* mark request as delayed */
555 continue; 566 continue;
556 } 567 }
557 else 568 else
558 { 569 {
559 int res = ETP_FINISH (req); 570 int res = ETP_FINISH (req);
560 if (expect_false (res)) 571 if (ecb_expect_false (res))
561 return res; 572 return res;
562 } 573 }
563 574
564 if (expect_false (maxreqs && !--maxreqs)) 575 if (ecb_expect_false (maxreqs && !--maxreqs))
565 break; 576 break;
566 577
567 if (maxtime) 578 if (maxtime)
568 { 579 {
569 gettimeofday (&tv_now, 0); 580 gettimeofday (&tv_now, 0);
575 586
576 errno = EAGAIN; 587 errno = EAGAIN;
577 return -1; 588 return -1;
578} 589}
579 590
591static void
580static void etp_cancel (ETP_REQ *req) 592etp_cancel (ETP_REQ *req)
581{ 593{
582 X_LOCK (wrklock); 594 X_LOCK (wrklock);
583 req->flags |= EIO_FLAG_CANCELLED; 595 req->flags |= EIO_FLAG_CANCELLED;
584 X_UNLOCK (wrklock); 596 X_UNLOCK (wrklock);
585 597
586 eio_grp_cancel (req); 598 eio_grp_cancel (req);
587} 599}
588 600
601static void
589static void etp_submit (ETP_REQ *req) 602etp_submit (ETP_REQ *req)
590{ 603{
591 req->pri -= ETP_PRI_MIN; 604 req->pri -= ETP_PRI_MIN;
592 605
593 if (expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; 606 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; 607 if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN;
595 608
596 if (expect_false (req->type == EIO_GROUP)) 609 if (ecb_expect_false (req->type == EIO_GROUP))
597 { 610 {
598 /* I hope this is worth it :/ */ 611 /* I hope this is worth it :/ */
599 X_LOCK (reqlock); 612 X_LOCK (reqlock);
600 ++nreqs; 613 ++nreqs;
601 X_UNLOCK (reqlock); 614 X_UNLOCK (reqlock);
620 633
621 etp_maybe_start_thread (); 634 etp_maybe_start_thread ();
622 } 635 }
623} 636}
624 637
638static void ecb_cold
625static void etp_set_max_poll_time (double nseconds) 639etp_set_max_poll_time (double nseconds)
626{ 640{
627 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 641 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
628 max_poll_time = nseconds * EIO_TICKS; 642 max_poll_time = nseconds * EIO_TICKS;
629 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 643 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
630} 644}
631 645
646static void ecb_cold
632static void etp_set_max_poll_reqs (unsigned int maxreqs) 647etp_set_max_poll_reqs (unsigned int maxreqs)
633{ 648{
634 if (WORDACCESS_UNSAFE) X_LOCK (reslock); 649 if (WORDACCESS_UNSAFE) X_LOCK (reslock);
635 max_poll_reqs = maxreqs; 650 max_poll_reqs = maxreqs;
636 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); 651 if (WORDACCESS_UNSAFE) X_UNLOCK (reslock);
637} 652}
638 653
654static void ecb_cold
639static void etp_set_max_idle (unsigned int nthreads) 655etp_set_max_idle (unsigned int nthreads)
640{ 656{
641 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 657 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
642 max_idle = nthreads; 658 max_idle = nthreads;
643 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 659 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
644} 660}
645 661
662static void ecb_cold
646static void etp_set_idle_timeout (unsigned int seconds) 663etp_set_idle_timeout (unsigned int seconds)
647{ 664{
648 if (WORDACCESS_UNSAFE) X_LOCK (reqlock); 665 if (WORDACCESS_UNSAFE) X_LOCK (reqlock);
649 idle_timeout = seconds; 666 idle_timeout = seconds;
650 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); 667 if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock);
651} 668}
652 669
670static void ecb_cold
653static void etp_set_min_parallel (unsigned int nthreads) 671etp_set_min_parallel (unsigned int nthreads)
654{ 672{
655 if (wanted < nthreads) 673 if (wanted < nthreads)
656 wanted = nthreads; 674 wanted = nthreads;
657} 675}
658 676
677static void ecb_cold
659static void etp_set_max_parallel (unsigned int nthreads) 678etp_set_max_parallel (unsigned int nthreads)
660{ 679{
661 if (wanted > nthreads) 680 if (wanted > nthreads)
662 wanted = nthreads; 681 wanted = nthreads;
663 682
664 while (started > wanted) 683 while (started > wanted)
665 etp_end_thread (); 684 etp_end_thread ();
666} 685}
667 686
668/*****************************************************************************/ 687/*****************************************************************************/
669 688
689static void
670static void grp_try_feed (eio_req *grp) 690grp_try_feed (eio_req *grp)
671{ 691{
672 while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) 692 while (grp->size < grp->int2 && !EIO_CANCELLED (grp))
673 { 693 {
674 grp->flags &= ~EIO_FLAG_GROUPADD; 694 grp->flags &= ~EIO_FLAG_GROUPADD;
675 695
682 break; 702 break;
683 } 703 }
684 } 704 }
685} 705}
686 706
707static int
687static int grp_dec (eio_req *grp) 708grp_dec (eio_req *grp)
688{ 709{
689 --grp->size; 710 --grp->size;
690 711
691 /* call feeder, if applicable */ 712 /* call feeder, if applicable */
692 grp_try_feed (grp); 713 grp_try_feed (grp);
696 return eio_finish (grp); 717 return eio_finish (grp);
697 else 718 else
698 return 0; 719 return 0;
699} 720}
700 721
722static void
701void eio_destroy (eio_req *req) 723eio_destroy (eio_req *req)
702{ 724{
703 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); 725 if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1);
704 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); 726 if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2);
705 727
706 EIO_DESTROY (req); 728 EIO_DESTROY (req);
707} 729}
708 730
731static int
709static int eio_finish (eio_req *req) 732eio_finish (eio_req *req)
710{ 733{
711 int res = EIO_FINISH (req); 734 int res = EIO_FINISH (req);
712 735
713 if (req->grp) 736 if (req->grp)
714 { 737 {
722 if (grp->grp_first == req) 745 if (grp->grp_first == req)
723 grp->grp_first = req->grp_next; 746 grp->grp_first = req->grp_next;
724 747
725 res2 = grp_dec (grp); 748 res2 = grp_dec (grp);
726 749
727 if (!res && res2) 750 if (!res)
728 res = res2; 751 res = res2;
729 } 752 }
730 753
731 eio_destroy (req); 754 eio_destroy (req);
732 755
733 return res; 756 return res;
734} 757}
735 758
759void
736void eio_grp_cancel (eio_req *grp) 760eio_grp_cancel (eio_req *grp)
737{ 761{
738 for (grp = grp->grp_first; grp; grp = grp->grp_next) 762 for (grp = grp->grp_first; grp; grp = grp->grp_next)
739 eio_cancel (grp); 763 eio_cancel (grp);
740} 764}
741 765
766void
742void eio_cancel (eio_req *req) 767eio_cancel (eio_req *req)
743{ 768{
744 etp_cancel (req); 769 etp_cancel (req);
745} 770}
746 771
772void
747void eio_submit (eio_req *req) 773eio_submit (eio_req *req)
748{ 774{
749 etp_submit (req); 775 etp_submit (req);
750} 776}
751 777
752unsigned int eio_nreqs (void) 778unsigned int
779eio_nreqs (void)
753{ 780{
754 return etp_nreqs (); 781 return etp_nreqs ();
755} 782}
756 783
757unsigned int eio_nready (void) 784unsigned int
785eio_nready (void)
758{ 786{
759 return etp_nready (); 787 return etp_nready ();
760} 788}
761 789
762unsigned int eio_npending (void) 790unsigned int
791eio_npending (void)
763{ 792{
764 return etp_npending (); 793 return etp_npending ();
765} 794}
766 795
767unsigned int eio_nthreads (void) 796unsigned int ecb_cold
797eio_nthreads (void)
768{ 798{
769 return etp_nthreads (); 799 return etp_nthreads ();
770} 800}
771 801
802void ecb_cold
772void eio_set_max_poll_time (double nseconds) 803eio_set_max_poll_time (double nseconds)
773{ 804{
774 etp_set_max_poll_time (nseconds); 805 etp_set_max_poll_time (nseconds);
775} 806}
776 807
808void ecb_cold
777void eio_set_max_poll_reqs (unsigned int maxreqs) 809eio_set_max_poll_reqs (unsigned int maxreqs)
778{ 810{
779 etp_set_max_poll_reqs (maxreqs); 811 etp_set_max_poll_reqs (maxreqs);
780} 812}
781 813
814void ecb_cold
782void eio_set_max_idle (unsigned int nthreads) 815eio_set_max_idle (unsigned int nthreads)
783{ 816{
784 etp_set_max_idle (nthreads); 817 etp_set_max_idle (nthreads);
785} 818}
786 819
820void ecb_cold
787void eio_set_idle_timeout (unsigned int seconds) 821eio_set_idle_timeout (unsigned int seconds)
788{ 822{
789 etp_set_idle_timeout (seconds); 823 etp_set_idle_timeout (seconds);
790} 824}
791 825
826void ecb_cold
792void eio_set_min_parallel (unsigned int nthreads) 827eio_set_min_parallel (unsigned int nthreads)
793{ 828{
794 etp_set_min_parallel (nthreads); 829 etp_set_min_parallel (nthreads);
795} 830}
796 831
832void ecb_cold
797void eio_set_max_parallel (unsigned int nthreads) 833eio_set_max_parallel (unsigned int nthreads)
798{ 834{
799 etp_set_max_parallel (nthreads); 835 etp_set_max_parallel (nthreads);
800} 836}
801 837
802int eio_poll (void) 838int eio_poll (void)
872#ifndef HAVE_FUTIMES 908#ifndef HAVE_FUTIMES
873 909
874# undef futimes 910# undef futimes
875# define futimes(fd,times) eio__futimes (fd, times) 911# define futimes(fd,times) eio__futimes (fd, times)
876 912
913static int
877static int eio__futimes (int fd, const struct timeval tv[2]) 914eio__futimes (int fd, const struct timeval tv[2])
878{ 915{
879 errno = ENOSYS; 916 errno = ENOSYS;
880 return -1; 917 return -1;
881} 918}
882 919
886# undef fdatasync 923# undef fdatasync
887# define fdatasync(fd) fsync (fd) 924# define fdatasync(fd) fsync (fd)
888#endif 925#endif
889 926
890/* sync_file_range always needs emulation */ 927/* sync_file_range always needs emulation */
891int 928static int
892eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) 929eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags)
893{ 930{
894#if HAVE_SYNC_FILE_RANGE 931#if HAVE_SYNC_FILE_RANGE
895 int res; 932 int res;
896 933
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 {
952#if HAVE_SENDFILE 992#if HAVE_SENDFILE
953# if __linux 993# if __linux
994 off_t soffset = offset;
954 res = sendfile (ofd, ifd, &offset, count); 995 res = sendfile (ofd, ifd, &soffset, count);
955 996
956# elif __FreeBSD__ 997# elif __FreeBSD__
957 /* 998 /*
958 * Of course, the freebsd sendfile is a dire hack with no thoughts 999 * Of course, the freebsd sendfile is a dire hack with no thoughts
959 * wasted on making it similar to other I/O functions. 1000 * wasted on making it similar to other I/O functions.
960 */ 1001 */
961 {
962 off_t sbytes; 1002 off_t sbytes;
963 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); 1003 res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0);
964 1004
965 #if 0 /* according to the manpage, this is correct, but broken behaviour */ 1005 #if 0 /* according to the manpage, this is correct, but broken behaviour */
966 /* freebsd' sendfile will return 0 on success */ 1006 /* freebsd' sendfile will return 0 on success */
967 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */ 1007 /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */
968 /* not on e.g. EIO or EPIPE - sounds broken */ 1008 /* not on e.g. EIO or EPIPE - sounds broken */
969 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) 1009 if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0)
970 res = sbytes; 1010 res = sbytes;
971 #endif 1011 #endif
972 1012
973 /* according to source inspection, this is correct, and useful behaviour */ 1013 /* according to source inspection, this is correct, and useful behaviour */
974 if (sbytes) 1014 if (sbytes)
975 res = sbytes; 1015 res = sbytes;
976 }
977 1016
978# elif defined (__APPLE__) 1017# elif defined (__APPLE__) && 0 /* broken, as everything on os x */
979
980 {
981 off_t sbytes = count; 1018 off_t sbytes = count;
982 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); 1019 res = sendfile (ifd, ofd, offset, &sbytes, 0, 0);
983 1020
984 /* according to the manpage, sbytes is always valid */ 1021 /* according to the manpage, sbytes is always valid */
985 if (sbytes) 1022 if (sbytes)
986 res = sbytes; 1023 res = sbytes;
987 }
988 1024
989# elif __hpux 1025# elif __hpux
990 res = sendfile (ofd, ifd, offset, count, 0, 0); 1026 res = sendfile (ofd, ifd, offset, count, 0, 0);
991 1027
992# elif __solaris 1028# elif __solaris
993 {
994 struct sendfilevec vec; 1029 struct sendfilevec vec;
995 size_t sbytes; 1030 size_t sbytes;
996 1031
997 vec.sfv_fd = ifd; 1032 vec.sfv_fd = ifd;
998 vec.sfv_flag = 0; 1033 vec.sfv_flag = 0;
999 vec.sfv_off = offset; 1034 vec.sfv_off = offset;
1000 vec.sfv_len = count; 1035 vec.sfv_len = count;
1001 1036
1002 res = sendfilev (ofd, &vec, 1, &sbytes); 1037 res = sendfilev (ofd, &vec, 1, &sbytes);
1003 1038
1004 if (res < 0 && sbytes) 1039 if (res < 0 && sbytes)
1005 res = sbytes; 1040 res = sbytes;
1006 }
1007 1041
1008# endif 1042# endif
1009 1043
1010#elif defined (_WIN32) 1044#elif defined (_WIN32)
1011
1012 /* does not work, just for documentation of what would need to be done */ 1045 /* does not work, just for documentation of what would need to be done */
1013 { 1046 /* actually, cannot be done like this, as TransmitFile changes the file offset, */
1047 /* libeio guarantees that the file offset does not change, and windows */
1048 /* has no way to get an independent handle to the same file description */
1014 HANDLE h = TO_SOCKET (ifd); 1049 HANDLE h = TO_SOCKET (ifd);
1015 SetFilePointer (h, offset, 0, FILE_BEGIN); 1050 SetFilePointer (h, offset, 0, FILE_BEGIN);
1016 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); 1051 res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0);
1017 }
1018 1052
1019#else 1053#else
1020 res = -1; 1054 res = -1;
1021 errno = ENOSYS; 1055 errno = ENOSYS;
1022#endif 1056#endif
1023 1057
1058 /* we assume sendfile can copy at least 128mb in one go */
1059 if (res <= 128 * 1024 * 1024)
1060 {
1061 if (res > 0)
1062 written += res;
1063
1064 if (written)
1065 return written;
1066
1067 break;
1068 }
1069 else
1070 {
1071 /* if we requested more, then probably the kernel was lazy */
1072 written += res;
1073 offset += res;
1074 count -= res;
1075
1076 if (!count)
1077 return written;
1078 }
1079 }
1080
1024 if (res < 0 1081 if (res < 0
1025 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK 1082 && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK
1026 /* BSDs */ 1083 /* BSDs */
1027#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ 1084#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */
1028 || errno == ENOTSUP 1085 || errno == ENOTSUP
1029#endif 1086#endif
1064 count -= cnt; 1121 count -= cnt;
1065 } 1122 }
1066 } 1123 }
1067 1124
1068 return res; 1125 return res;
1126}
1127
1128#ifdef PAGESIZE
1129# define eio_pagesize() PAGESIZE
1130#else
1131static intptr_t
1132eio_pagesize (void)
1133{
1134 static intptr_t page;
1135
1136 if (!page)
1137 page = sysconf (_SC_PAGESIZE);
1138
1139 return page;
1140}
1141#endif
1142
1143static void
1144eio_page_align (void **addr, size_t *length)
1145{
1146 intptr_t mask = eio_pagesize () - 1;
1147
1148 /* round down addr */
1149 intptr_t adj = mask & (intptr_t)*addr;
1150
1151 *addr = (void *)((intptr_t)*addr - adj);
1152 *length += adj;
1153
1154 /* round up length */
1155 *length = (*length + mask) & ~mask;
1156}
1157
1158#if !_POSIX_MEMLOCK
1159# define eio__mlockall(a) ((errno = ENOSYS), -1)
1160#else
1161
1162static int
1163eio__mlockall (int flags)
1164{
1165 #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7
1166 extern int mallopt (int, int);
1167 mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */
1168 #endif
1169
1170 if (EIO_MCL_CURRENT != MCL_CURRENT
1171 || EIO_MCL_FUTURE != MCL_FUTURE)
1172 {
1173 flags = 0
1174 | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0)
1175 | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0);
1176 }
1177
1178 return mlockall (flags);
1179}
1180#endif
1181
1182#if !_POSIX_MEMLOCK_RANGE
1183# define eio__mlock(a,b) ((errno = ENOSYS), -1)
1184#else
1185
1186static int
1187eio__mlock (void *addr, size_t length)
1188{
1189 eio_page_align (&addr, &length);
1190
1191 return mlock (addr, length);
1192}
1193
1194#endif
1195
1196#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO)
1197# define eio__msync(a,b,c) ((errno = ENOSYS), -1)
1198#else
1199
1200static int
1201eio__msync (void *mem, size_t len, int flags)
1202{
1203 eio_page_align (&mem, &len);
1204
1205 if (EIO_MS_ASYNC != MS_SYNC
1206 || EIO_MS_INVALIDATE != MS_INVALIDATE
1207 || EIO_MS_SYNC != MS_SYNC)
1208 {
1209 flags = 0
1210 | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0)
1211 | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0)
1212 | (flags & EIO_MS_SYNC ? MS_SYNC : 0);
1213 }
1214
1215 return msync (mem, len, flags);
1216}
1217
1218#endif
1219
1220static int
1221eio__mtouch (eio_req *req)
1222{
1223 void *mem = req->ptr2;
1224 size_t len = req->size;
1225 int flags = req->int1;
1226
1227 eio_page_align (&mem, &len);
1228
1229 {
1230 intptr_t addr = (intptr_t)mem;
1231 intptr_t end = addr + len;
1232 intptr_t page = eio_pagesize ();
1233
1234 if (addr < end)
1235 if (flags & EIO_MT_MODIFY) /* modify */
1236 do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req));
1237 else
1238 do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req));
1239 }
1240
1241 return 0;
1242}
1243
1244/*****************************************************************************/
1245/* requests implemented outside eio_execute, because they are so large */
1246
1247static void
1248eio__realpath (eio_req *req, etp_worker *self)
1249{
1250 char *rel = req->ptr1;
1251 char *res;
1252 char *tmp1, *tmp2;
1253#if SYMLOOP_MAX > 32
1254 int symlinks = SYMLOOP_MAX;
1255#else
1256 int symlinks = 32;
1257#endif
1258
1259 req->result = -1;
1260
1261 errno = EINVAL;
1262 if (!rel)
1263 return;
1264
1265 errno = ENOENT;
1266 if (!*rel)
1267 return;
1268
1269 if (!req->ptr2)
1270 {
1271 X_LOCK (wrklock);
1272 req->flags |= EIO_FLAG_PTR2_FREE;
1273 X_UNLOCK (wrklock);
1274 req->ptr2 = malloc (PATH_MAX * 3);
1275
1276 errno = ENOMEM;
1277 if (!req->ptr2)
1278 return;
1279 }
1280
1281 res = req->ptr2;
1282 tmp1 = res + PATH_MAX;
1283 tmp2 = tmp1 + PATH_MAX;
1284
1285#if 0 /* disabled, the musl way to do things is just too racy */
1286#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME)
1287 /* on linux we may be able to ask the kernel */
1288 {
1289 int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME);
1290
1291 if (fd >= 0)
1292 {
1293 sprintf (tmp1, "/proc/self/fd/%d", fd);
1294 req->result = readlink (tmp1, res, PATH_MAX);
1295 close (fd);
1296
1297 /* here we should probably stat the open file and the disk file, to make sure they still match */
1298
1299 if (req->result > 0)
1300 goto done;
1301 }
1302 else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO)
1303 return;
1304 }
1305#endif
1306#endif
1307
1308 if (*rel != '/')
1309 {
1310 if (!getcwd (res, PATH_MAX))
1311 return;
1312
1313 if (res [1]) /* only use if not / */
1314 res += strlen (res);
1315 }
1316
1317 while (*rel)
1318 {
1319 ssize_t len, linklen;
1320 char *beg = rel;
1321
1322 while (*rel && *rel != '/')
1323 ++rel;
1324
1325 len = rel - beg;
1326
1327 if (!len) /* skip slashes */
1328 {
1329 ++rel;
1330 continue;
1331 }
1332
1333 if (beg [0] == '.')
1334 {
1335 if (len == 1)
1336 continue; /* . - nop */
1337
1338 if (beg [1] == '.' && len == 2)
1339 {
1340 /* .. - back up one component, if possible */
1341
1342 while (res != req->ptr2)
1343 if (*--res == '/')
1344 break;
1345
1346 continue;
1347 }
1348 }
1349
1350 errno = ENAMETOOLONG;
1351 if (res + 1 + len + 1 >= tmp1)
1352 return;
1353
1354 /* copy one component */
1355 *res = '/';
1356 memcpy (res + 1, beg, len);
1357
1358 /* zero-terminate, for readlink */
1359 res [len + 1] = 0;
1360
1361 /* now check if it's a symlink */
1362 linklen = readlink (req->ptr2, tmp1, PATH_MAX);
1363
1364 if (linklen < 0)
1365 {
1366 if (errno != EINVAL)
1367 return;
1368
1369 /* it's a normal directory. hopefully */
1370 res += len + 1;
1371 }
1372 else
1373 {
1374 /* yay, it was a symlink - build new path in tmp2 */
1375 int rellen = strlen (rel);
1376
1377 errno = ENAMETOOLONG;
1378 if (linklen + 1 + rellen >= PATH_MAX)
1379 return;
1380
1381 errno = ELOOP;
1382 if (!--symlinks)
1383 return;
1384
1385 if (*tmp1 == '/')
1386 res = req->ptr2; /* symlink resolves to an absolute path */
1387
1388 /* we need to be careful, as rel might point into tmp2 already */
1389 memmove (tmp2 + linklen + 1, rel, rellen + 1);
1390 tmp2 [linklen] = '/';
1391 memcpy (tmp2, tmp1, linklen);
1392
1393 rel = tmp2;
1394 }
1395 }
1396
1397 /* special case for the lone root path */
1398 if (res == req->ptr2)
1399 *res++ = '/';
1400
1401 req->result = res - (char *)req->ptr2;
1402
1403done:
1404 req->ptr2 = realloc (req->ptr2, req->result); /* trade time for space savings */
1069} 1405}
1070 1406
1071static signed char 1407static signed char
1072eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) 1408eio_dent_cmp (const eio_dirent *a, const eio_dirent *b)
1073{ 1409{
1255 1591
1256 X_LOCK (wrklock); 1592 X_LOCK (wrklock);
1257 /* the corresponding closedir is in ETP_WORKER_CLEAR */ 1593 /* the corresponding closedir is in ETP_WORKER_CLEAR */
1258 self->dirp = dirp = opendir (req->ptr1); 1594 self->dirp = dirp = opendir (req->ptr1);
1259 1595
1596 if (req->flags & EIO_FLAG_PTR1_FREE)
1597 free (req->ptr1);
1598
1260 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; 1599 req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE;
1261 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; 1600 req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0;
1262 req->ptr2 = names = malloc (namesalloc); 1601 req->ptr2 = names = malloc (namesalloc);
1263 X_UNLOCK (wrklock); 1602 X_UNLOCK (wrklock);
1264 1603
1315 /* skip . and .. entries */ 1654 /* skip . and .. entries */
1316 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) 1655 if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2])))
1317 { 1656 {
1318 int len = D_NAMLEN (entp) + 1; 1657 int len = D_NAMLEN (entp) + 1;
1319 1658
1320 while (expect_false (namesoffs + len > namesalloc)) 1659 while (ecb_expect_false (namesoffs + len > namesalloc))
1321 { 1660 {
1322 namesalloc *= 2; 1661 namesalloc *= 2;
1323 X_LOCK (wrklock); 1662 X_LOCK (wrklock);
1324 req->ptr2 = names = realloc (names, namesalloc); 1663 req->ptr2 = names = realloc (names, namesalloc);
1325 X_UNLOCK (wrklock); 1664 X_UNLOCK (wrklock);
1332 1671
1333 if (dents) 1672 if (dents)
1334 { 1673 {
1335 struct eio_dirent *ent; 1674 struct eio_dirent *ent;
1336 1675
1337 if (expect_false (dentoffs == dentalloc)) 1676 if (ecb_expect_false (dentoffs == dentalloc))
1338 { 1677 {
1339 dentalloc *= 2; 1678 dentalloc *= 2;
1340 X_LOCK (wrklock); 1679 X_LOCK (wrklock);
1341 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); 1680 req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent));
1342 X_UNLOCK (wrklock); 1681 X_UNLOCK (wrklock);
1430 break; 1769 break;
1431 } 1770 }
1432 } 1771 }
1433} 1772}
1434 1773
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/*****************************************************************************/ 1774/*****************************************************************************/
1548 1775
1549#define ALLOC(len) \ 1776#define ALLOC(len) \
1550 if (!req->ptr2) \ 1777 if (!req->ptr2) \
1551 { \ 1778 { \
1608 X_UNLOCK (reqlock); 1835 X_UNLOCK (reqlock);
1609 1836
1610 if (req->type < 0) 1837 if (req->type < 0)
1611 goto quit; 1838 goto quit;
1612 1839
1613 if (!EIO_CANCELLED (req))
1614 ETP_EXECUTE (self, req); 1840 ETP_EXECUTE (self, req);
1615 1841
1616 X_LOCK (reslock); 1842 X_LOCK (reslock);
1617 1843
1618 ++npending; 1844 ++npending;
1619 1845
1634 return 0; 1860 return 0;
1635} 1861}
1636 1862
1637/*****************************************************************************/ 1863/*****************************************************************************/
1638 1864
1865int ecb_cold
1639int eio_init (void (*want_poll)(void), void (*done_poll)(void)) 1866eio_init (void (*want_poll)(void), void (*done_poll)(void))
1640{ 1867{
1641 return etp_init (want_poll, done_poll); 1868 return etp_init (want_poll, done_poll);
1642} 1869}
1643 1870
1871ecb_inline void
1644static void eio_api_destroy (eio_req *req) 1872eio_api_destroy (eio_req *req)
1645{ 1873{
1646 free (req); 1874 free (req);
1647} 1875}
1648 1876
1649#define REQ(rtype) \ 1877#define REQ(rtype) \
1668 { \ 1896 { \
1669 eio_api_destroy (req); \ 1897 eio_api_destroy (req); \
1670 return 0; \ 1898 return 0; \
1671 } 1899 }
1672 1900
1901static void
1673static void eio_execute (etp_worker *self, eio_req *req) 1902eio_execute (etp_worker *self, eio_req *req)
1674{ 1903{
1904 if (ecb_expect_false (EIO_CANCELLED (req)))
1905 {
1906 req->result = -1;
1907 req->errorno = ECANCELED;
1908 return;
1909 }
1910
1675 switch (req->type) 1911 switch (req->type)
1676 { 1912 {
1677 case EIO_READ: ALLOC (req->size); 1913 case EIO_READ: ALLOC (req->size);
1678 req->result = req->offs >= 0 1914 req->result = req->offs >= 0
1679 ? pread (req->int1, req->ptr2, req->size, req->offs) 1915 ? pread (req->int1, req->ptr2, req->size, req->offs)
1713 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break; 1949 case EIO_RENAME: req->result = rename (req->ptr1, req->ptr2); break;
1714 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break; 1950 case EIO_LINK: req->result = link (req->ptr1, req->ptr2); break;
1715 case EIO_SYMLINK: req->result = symlink (req->ptr1, req->ptr2); break; 1951 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; 1952 case EIO_MKNOD: req->result = mknod (req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break;
1717 1953
1954 case EIO_REALPATH: eio__realpath (req, self); break;
1955
1718 case EIO_READLINK: ALLOC (PATH_MAX); 1956 case EIO_READLINK: ALLOC (PATH_MAX);
1719 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break; 1957 req->result = readlink (req->ptr1, req->ptr2, PATH_MAX); break;
1720 1958
1721 case EIO_SYNC: req->result = 0; sync (); break; 1959 case EIO_SYNC: req->result = 0; sync (); break;
1722 case EIO_FSYNC: req->result = fsync (req->int1); break; 1960 case EIO_FSYNC: req->result = fsync (req->int1); break;
1723 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; 1961 case EIO_FDATASYNC: req->result = fdatasync (req->int1); break;
1724 case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; 1962 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; 1963 case EIO_MTOUCH: req->result = eio__mtouch (req); break;
1726 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; 1964 case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break;
1727 case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; 1965 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; 1966 case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break;
1729 1967
1730 case EIO_READDIR: eio__scandir (req, self); break; 1968 case EIO_READDIR: eio__scandir (req, self); break;
1774 case EIO_NOP: 2012 case EIO_NOP:
1775 req->result = 0; 2013 req->result = 0;
1776 break; 2014 break;
1777 2015
1778 case EIO_CUSTOM: 2016 case EIO_CUSTOM:
1779 ((void (*)(eio_req *))req->feed) (req); 2017 req->feed (req);
1780 break; 2018 break;
1781 2019
1782 default: 2020 default:
1783 errno = ENOSYS; 2021 errno = ENOSYS;
1784 req->result = -1; 2022 req->result = -1;
1937} 2175}
1938 2176
1939eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) 2177eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data)
1940{ 2178{
1941 return eio__1path (EIO_READLINK, path, pri, cb, data); 2179 return eio__1path (EIO_READLINK, path, pri, cb, data);
2180}
2181
2182eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data)
2183{
2184 return eio__1path (EIO_REALPATH, path, pri, cb, data);
1942} 2185}
1943 2186
1944eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) 2187eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data)
1945{ 2188{
1946 return eio__1path (EIO_STAT, path, pri, cb, data); 2189 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) 2248eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data)
2006{ 2249{
2007 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); 2250 return eio__2path (EIO_RENAME, path, new_path, pri, cb, data);
2008} 2251}
2009 2252
2010eio_req *eio_custom (eio_cb execute, int pri, eio_cb cb, void *data) 2253eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data)
2011{ 2254{
2012 REQ (EIO_CUSTOM); req->feed = (void (*)(eio_req *))execute; SEND; 2255 REQ (EIO_CUSTOM); req->feed = execute; SEND;
2013} 2256}
2014 2257
2015#endif 2258#endif
2016 2259
2017eio_req *eio_grp (eio_cb cb, void *data) 2260eio_req *eio_grp (eio_cb cb, void *data)
2026#undef SEND 2269#undef SEND
2027 2270
2028/*****************************************************************************/ 2271/*****************************************************************************/
2029/* grp functions */ 2272/* grp functions */
2030 2273
2274void
2031void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) 2275eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit)
2032{ 2276{
2033 grp->int2 = limit; 2277 grp->int2 = limit;
2034 grp->feed = feed; 2278 grp->feed = feed;
2035 2279
2036 grp_try_feed (grp); 2280 grp_try_feed (grp);
2037} 2281}
2038 2282
2283void
2039void eio_grp_limit (eio_req *grp, int limit) 2284eio_grp_limit (eio_req *grp, int limit)
2040{ 2285{
2041 grp->int2 = limit; 2286 grp->int2 = limit;
2042 2287
2043 grp_try_feed (grp); 2288 grp_try_feed (grp);
2044} 2289}
2045 2290
2291void
2046void eio_grp_add (eio_req *grp, eio_req *req) 2292eio_grp_add (eio_req *grp, eio_req *req)
2047{ 2293{
2048 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); 2294 assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2));
2049 2295
2050 grp->flags |= EIO_FLAG_GROUPADD; 2296 grp->flags |= EIO_FLAG_GROUPADD;
2051 2297
2062} 2308}
2063 2309
2064/*****************************************************************************/ 2310/*****************************************************************************/
2065/* misc garbage */ 2311/* misc garbage */
2066 2312
2313ssize_t
2067ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) 2314eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count)
2068{ 2315{
2069 etp_worker wrk; 2316 etp_worker wrk;
2070 ssize_t ret; 2317 ssize_t ret;
2071 2318
2072 wrk.dbuf = 0; 2319 wrk.dbuf = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines